This report investigates the Central Limit Theorem using simulated exponential random variables and examines the ToothGrowth dataset. The simulation compares sample means and variances with theoretical values and evaluates approximate normality. The second analysis uses exploratory summaries, confidence intervals, and hypothesis tests to investigate the effects of supplement type and dose on tooth growth.
We generate 1,000 simulations, each containing 40 observations from an exponential distribution with rate lambda = 0.2. The theoretical population mean is 1/lambda = 5, and the population variance is 1/lambda^2 = 25. The theoretical variance of the sample mean is 25/40 = 0.625.
set.seed(123)
lambda <- 0.2
n <- 40
simulations <- 1000
# Generate 1,000 samples, each with 40 observations
samples <- matrix(
rexp(simulations * n, rate = lambda),
nrow = simulations,
ncol = n
)
sample_means <- rowMeans(samples)
theoretical_mean <- 1 / lambda
theoretical_variance <- 1 / lambda^2
theoretical_mean_variance <- theoretical_variance / n
c(
Sample_Mean = mean(sample_means),
Theoretical_Mean = theoretical_mean,
Sample_Variance = var(sample_means),
Theoretical_Variance = theoretical_mean_variance
)
## Sample_Mean Theoretical_Mean Sample_Variance
## 5.0119113 5.0000000 0.6088292
## Theoretical_Variance
## 0.6250000
The original exponential distribution is right-skewed. By contrast, the distribution of averages from samples of size 40 should be approximately normal under the Central Limit Theorem.
par(mfrow = c(1, 2))
hist(
as.vector(samples),
breaks = 40,
probability = TRUE,
main = "Individual Exponential Values",
xlab = "Value",
col = "lightblue"
)
hist(
sample_means,
breaks = 30,
probability = TRUE,
main = "Means of 40 Observations",
xlab = "Sample Mean",
col = "lightgreen"
)
curve(
dnorm(x, mean = theoretical_mean,
sd = sqrt(theoretical_mean_variance)),
add = TRUE,
col = "red",
lwd = 2
)
abline(v = theoretical_mean, col = "blue", lwd = 2, lty = 2)
par(mfrow = c(1, 1))
The simulated mean should be close to the theoretical mean of 5. The variance of the simulated sample means should be close to 0.625, which is much smaller than the population variance of 25. The histogram of sample means should be more symmetric and bell-shaped than the histogram of individual observations, supporting the Central Limit Theorem.
The ToothGrowth dataset contains tooth length measurements for 60 guinea pigs receiving vitamin C through orange juice (OJ) or ascorbic acid (VC) at three dose levels.
data("ToothGrowth")
str(ToothGrowth)
## 'data.frame': 60 obs. of 3 variables:
## $ len : num 4.2 11.5 7.3 5.8 6.4 10 11.2 11.2 5.2 7 ...
## $ supp: Factor w/ 2 levels "OJ","VC": 2 2 2 2 2 2 2 2 2 2 ...
## $ dose: num 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 ...
summary(ToothGrowth)
## len supp dose
## Min. : 4.20 OJ:30 Min. :0.500
## 1st Qu.:13.07 VC:30 1st Qu.:0.500
## Median :19.25 Median :1.000
## Mean :18.81 Mean :1.167
## 3rd Qu.:25.27 3rd Qu.:2.000
## Max. :33.90 Max. :2.000
table(ToothGrowth$supp, ToothGrowth$dose)
##
## 0.5 1 2
## OJ 10 10 10
## VC 10 10 10
ToothGrowth$dose <- factor(ToothGrowth$dose)
aggregate(len ~ supp + dose, data = ToothGrowth, FUN = mean)
## supp dose len
## 1 OJ 0.5 13.23
## 2 VC 0.5 7.98
## 3 OJ 1 22.70
## 4 VC 1 16.77
## 5 OJ 2 26.06
## 6 VC 2 26.14
boxplot(
len ~ interaction(supp, dose),
data = ToothGrowth,
col = c("lightblue", "lightgreen"),
main = "Tooth Growth by Supplement and Dose",
xlab = "Supplement and Dose",
ylab = "Tooth Length"
)
The boxplot compares tooth lengths across supplement and dose combinations. Mean tooth length generally increases with dose. Differences between supplements may depend on the dose.
Welch two-sample t-tests are used to compare supplements at each dose. The tests use a 5% significance level and do not assume equal population variances.
# Compare OJ and VC at each dose
supplement_tests <- lapply(levels(ToothGrowth$dose), function(d) {
subset_data <- subset(ToothGrowth, dose == d)
test <- t.test(len ~ supp, data = subset_data)
data.frame(
Dose = d,
Mean_OJ = mean(subset_data$len[subset_data$supp == "OJ"]),
Mean_VC = mean(subset_data$len[subset_data$supp == "VC"]),
Difference_OJ_minus_VC =
mean(subset_data$len[subset_data$supp == "OJ"]) -
mean(subset_data$len[subset_data$supp == "VC"]),
CI_Lower = test$conf.int[1],
CI_Upper = test$conf.int[2],
P_Value = test$p.value
)
})
do.call(rbind, supplement_tests)
## Dose Mean_OJ Mean_VC Difference_OJ_minus_VC CI_Lower CI_Upper P_Value
## 1 0.5 13.23 7.98 5.25 1.719057 8.780943 0.006358607
## 2 1 22.70 16.77 5.93 2.802148 9.057852 0.001038376
## 3 2 26.06 26.14 -0.08 -3.798070 3.638070 0.963851589
We also compare doses within each supplement using Welch t-tests.
dose_tests <- list(
OJ_0.5_vs_1 = t.test(
len ~ dose,
data = subset(ToothGrowth, supp == "OJ" & dose %in% c("0.5", "1"))
),
OJ_1_vs_2 = t.test(
len ~ dose,
data = subset(ToothGrowth, supp == "OJ" & dose %in% c("1", "2"))
),
VC_0.5_vs_1 = t.test(
len ~ dose,
data = subset(ToothGrowth, supp == "VC" & dose %in% c("0.5", "1"))
),
VC_1_vs_2 = t.test(
len ~ dose,
data = subset(ToothGrowth, supp == "VC" & dose %in% c("1", "2"))
)
)
lapply(dose_tests, function(x) {
c(
Mean_Difference = unname(diff(x$estimate)),
CI_Lower = x$conf.int[1],
CI_Upper = x$conf.int[2],
P_Value = x$p.value
)
})
## $OJ_0.5_vs_1
## Mean_Difference CI_Lower CI_Upper P_Value
## 9.470000e+00 -1.341563e+01 -5.524366e+00 8.784919e-05
##
## $OJ_1_vs_2
## Mean_Difference CI_Lower CI_Upper P_Value
## 3.36000000 -6.53144253 -0.18855747 0.03919514
##
## $VC_0.5_vs_1
## Mean_Difference CI_Lower CI_Upper P_Value
## 8.790000e+00 -1.126571e+01 -6.314288e+00 6.811018e-07
##
## $VC_1_vs_2
## Mean_Difference CI_Lower CI_Upper P_Value
## 9.370000e+00 -1.305427e+01 -5.685733e+00 9.155603e-05
The simulation illustrates that sample means are centered near the population mean and have lower variance than individual observations. Their distribution is approximately normal, as expected from the Central Limit Theorem.
The ToothGrowth analysis compares supplement types at each dose and examines dose-related differences within each supplement. Statistical significance is assessed using p-values and 95% confidence intervals. A p-value below 0.05 provides evidence of a difference for the comparison tested; it does not establish causation.
These analyses assume independent observations and, for the t-tests, that the group distributions are sufficiently well behaved for inference based on sample means. Welch tests do not require equal population variances. Multiple comparisons increase the chance of false-positive findings, so results should be interpreted with that limitation in mind.