# =============================================================================
# Advanced Statistical Programming (ACCA7022)
# Inferential Statistics Part 1: From Samples to Populations
# =============================================================================
# -----------------------------------------------------------------------------
# SETUP: Install and Load Required Packages
# -----------------------------------------------------------------------------
# Install packages if not already installed
if (!require("gapminder")) install.packages("gapminder")
if (!require("dplyr")) install.packages("dplyr")
if (!require("ggplot2")) install.packages("ggplot2")
if (!require("ggpubr")) install.packages("ggpubr")
# Load libraries
library(gapminder)
library(dplyr)
library(ggplot2)
library(ggpubr)
# =============================================================================
# PART 1: SAMPLING DISTRIBUTIONS AND CENTRAL LIMIT THEOREM
# =============================================================================
# -----------------------------------------------------------------------------
# 1.1 Exploring the Data
# -----------------------------------------------------------------------------
# View the gapminder dataset
head(gapminder)
summary(gapminder)
# Extract life expectancy data for sampling exercises
life_data <- gapminder$lifeExp
# -----------------------------------------------------------------------------
# 1.2 Simulation: Sampling Distribution of Sample Mean
# -----------------------------------------------------------------------------
# Generate 1000 sample means (sample size = 30)
set.seed(123)  # For reproducibility
sample_means <- replicate(1000, mean(sample(life_data, size = 30, replace = TRUE)))
# Visualise the sampling distribution
hist(sample_means,
main = "Sampling Distribution of Sample Mean",
xlab = "Mean Life Expectancy",
col = "lightblue",
border = "white")
# -----------------------------------------------------------------------------
# 1.3 Effect of Sample Size on Sampling Distribution
# -----------------------------------------------------------------------------
# Compare sample sizes n = 10 and n = 100
set.seed(123)
means_10 <- replicate(1000, mean(sample(life_data, 10, replace = TRUE)))
means_100 <- replicate(1000, mean(sample(life_data, 100, replace = TRUE)))
# Plot histograms side by side
par(mfrow = c(1, 2))
hist(means_10, main = "n = 10", xlab = "Mean Life Expectancy", col = "coral")
hist(means_100, main = "n = 100", xlab = "Mean Life Expectancy", col = "steelblue")
par(mfrow = c(1, 1))  # Reset plot layout
# Calculate standard errors
cat("\nStandard Error Comparison:\n")
cat("SE for n = 10:", sd(means_10), "\n")
cat("SE for n = 100:", sd(means_100), "\n")
# -----------------------------------------------------------------------------
# 1.4 Activity: Sampling Distribution Exploration
# -----------------------------------------------------------------------------
# Function to simulate sampling means
simulate_means <- function(n, reps = 1000) {
replicate(reps, mean(sample(life_data, size = n, replace = TRUE)))
}
# Test with different sample sizes
sample_sizes <- c(10, 30, 50, 100)
# Plot histograms for each sample size
par(mfrow = c(2, 2))
for (n in sample_sizes) {
hist(simulate_means(n),
main = paste("n =", n),
xlab = "Mean Life Expectancy",
col = "lightgreen",
border = "white")
}
par(mfrow = c(1, 1))  # Reset plot layout
# Calculate and display standard errors for each sample size
cat("\nStandard Errors by Sample Size:\n")
for (n in sample_sizes) {
se <- sd(simulate_means(n))
cat("n =", n, "-> SE =", round(se, 4), "\n")
}
# =============================================================================
# PART 2: CORRELATION ANALYSIS
# =============================================================================
# -----------------------------------------------------------------------------
# 2.1 Visualise Relationship Between GDP and Life Expectancy
# -----------------------------------------------------------------------------
# Scatter plot of GDP per capita vs Life Expectancy
ggplot(gapminder, aes(x = gdpPercap, y = lifeExp)) +
geom_point(alpha = 0.5, color = "steelblue") +
labs(title = "GDP per Capita vs Life Expectancy",
x = "GDP per Capita",
y = "Life Expectancy") +
theme_minimal()
