Using a simulated population with an exponential distribution, we will explore the relationship of a sample’s mean and variance to its superpopulation’s mean and variance. We will then use this information to derive confidence intervals and perform hypothesis testing. In a sentence, we will be applying statistical inference to a sample to derive knowledge of its superpopulation.
Step 1: Run a simulation with 40 observations that has an exponential distribution. The rate parameter (lambda) will equal 0.2.
## set rate parameter
lambda <- 0.2
obs <- 40
## create mean and variance parameters for sample
mn <- 1/lambda
sd <- 1/lambda
var <- ((1/lambda)^2)/obs
## set seed for reproducibility
set.seed(1234)
## run simulation
sim <- rexp(obs, lambda)
## evaluate histogram of simulated population
hist(sim,
main = "Figure 1. Histogram of Simulated Population (40 Observations)",
col = "light blue")
abline(v = mean(sim), col = "red", lwd = 5)
## calculate mean, sd, var of simulated population
s_mn <- mean(sim)
s_sd <- sd(sim)
s_var <- var(sim)/obs
Step 2: Now run the same simulation but take the average mean for each 40 observations over 1000 iterations.
## set seed
set.seed(1234)
## number of observations, simulations, and rate (lambda)
obs <- 40
sim <- 1000
lambda <- 0.2
## set variables to place results
r_mean <- data.frame(mean = (sim))
r_std <- data.frame(sd = (sim))
r_vr <- data.frame(var = (sim))
## get mean for each sample and add to result dataframe
for (i in 1:sim) {
sim_sample <- rexp(obs, lambda)
r_mean[i, 1] <- mean(sim_sample)
r_std[i, 1] <- sd(sim_sample)
r_vr[i, 1] <- var(sim_sample)
}
## create dataframe of simulation 2 results
sim1000 <- cbind(r_mean, r_std, r_vr)
head(sim1000)
## mean sd var
## 1 4.969024 5.098330 25.992970
## 2 5.748249 4.974127 24.741939
## 3 3.315459 2.802944 7.856493
## 4 6.415867 6.713418 45.069977
## 5 4.665412 3.700098 13.690723
## 6 5.971307 6.872818 47.235632
## create mean and sd of result
r_mn <- mean(sim1000$mean)
r_sd <- mean(sim1000$sd)
r_var <- mean(sim1000$var/obs)
## create histogram of simulation 2 results
hist(sim1000$mean,
main = "Figure 2. Histogram of the Average Means (1000 Simulations)",
xlab = "Average Simulated Mean",
ylab = "Number of Samples",
col = "gray")
abline(v = r_mn, col = "red", lwd = 5)
With 1000 simulations, the average mean is 4.9742388 compared to the population mean or theoretical mean of 5. Note how the distribution changed from an exponential distribution (Figure 1) to a normal distribution (Figure 2) due to the central limit theorem, and the sample mean approaches the population (or theoretical) mean due to the law of large numbers.
Table 1. Comparison of Standard Deviation and Variance for Simulation 1 and Simulation 2 with the Theoretical Variance
| Model | Variance | Standard Deviation |
|---|---|---|
| Simulation 1 | 0.6498242 | 5.0983301 |
| Simulation 2 (1000) | 0.6094504 | 4.8350268 |
| Theoretical/Population | 0.625 | 5 |
As the number of simulations increase the variance approximates the population or theoretical variance.
qqnorm(sim1000$mean, main="Normal Quantile-Quantile Plot", xlab="Theoretical Quantiles", ylab="Sample Quantiles")
qqline(sim1000$mean, col="blue")
The Q-Q plot demonstrates that the simulated sample is normally distributed.
Using the formula taught in class – CI = mean(x) + c(-1, 1) * 1.96 * sd(x), we can calculate the confidence intervals for the simulated sample. The confidence intervals are 3.4936212, 6.4548564.
Using statistical inference, we were able to demonstrate the phenomena taught in the course:
The Law of Large Numbers - as the number of simulations increase (or trials as in the definition), the sample mean and variance converge on the theoretical mean and variance. This resulted in the sample mean (4.97) converging on the theoretical mean (5.0).
The Central Limit Theorem - the sampling distribution of the sampling means approximates the normal distribution as the number of samples increases (sample size increases). This was the case when the exponential distribution of the first simulated sample was sampled repeatedly (1000 times) as in the second simulation, which was found to be normally distributed.
Finally, we were able to demonstrate the validity of using a random sample to measure a superpopulation.