R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Part 1: Simulation Exercise

Introduction

This simulation demonstrates the Central Limit Theorem (CLT) using an exponential distribution. We generate 1000 sample means, where each sample mean is calculated from 40 exponentially distributed observations with rate parameter \(\lambda = 0.2\).

1. Theoretical Values

For an exponential distribution:

\[ \mu = \frac{1}{\lambda} \]

and

\[ \sigma^2 = \frac{1}{\lambda^2} \]

lambda <- 0.2

theoretical_mean <- 1 / lambda
theoretical_variance <- 1 / (lambda^2)

theoretical_mean
## [1] 5
theoretical_variance
## [1] 25

The theoretical mean is 5 and the theoretical variance is 25.

Since each sample contains 40 observations, the theoretical variance of the sample mean is:

\[ Var(\bar X) = \frac{\sigma^2}{n} \]

n <- 40

theoretical_mean_sampling <- 1 / lambda
theoretical_variance_sampling <- 1 / (lambda^2 * n)
theoretical_sd_sampling <- sqrt(theoretical_variance_sampling)

theoretical_mean_sampling
## [1] 5
theoretical_variance_sampling
## [1] 0.625
theoretical_sd_sampling
## [1] 0.7905694

2. Simulation

We now generate 1000 sample means. Each sample contains 40 observations from an exponential distribution with \(\lambda = 0.2\).

set.seed(123)

means <- NULL

for(i in 1:1000) {
  means <- c(means, mean(rexp(40, rate = lambda)))
}

head(means)
## [1] 4.811212 5.360077 4.592871 4.900051 5.516619 5.612835

3. Sample Mean vs Theoretical Mean

The simulated mean of the 1000 sample means is:

sample_mean <- mean(means)

sample_mean
## [1] 5.011911

We compare this with the theoretical mean:

cat("Sample mean:", sample_mean, "\n")
## Sample mean: 5.011911
cat("Theoretical mean:", theoretical_mean_sampling, "\n")
## Theoretical mean: 5

4. Sample Variance vs Theoretical Variance

The variance of the simulated sample means is:

sample_variance <- var(means)

sample_variance
## [1] 0.6004928

The theoretical variance is:

cat("Sample variance:", sample_variance, "\n")
## Sample variance: 0.6004928
cat("Theoretical variance:", theoretical_variance_sampling, "\n")
## Theoretical variance: 0.625

The theoretical variance of the sample mean is:

\[ \frac{25}{40} = 0.625 \]

5. Distribution of the Original Exponential Data

First, we examine the distribution of individual exponential observations.

exp_data <- rexp(1000, rate = lambda)

hist(exp_data,
     breaks = 30,
     main = "Distribution of Exponential Random Variables",
     xlab = "Value",
     col = "lightblue")

The exponential distribution is right-skewed.

6. Distribution of the Sample Means

We now examine the distribution of the 1000 sample means.

hist(means,
     breaks = 30,
     main = "Distribution of 1000 Sample Means",
     xlab = "Sample Mean",
     col = "lightblue")

abline(v = theoretical_mean_sampling,
       lwd = 2)

abline(v = sample_mean,
       lwd = 2,
       lty = 2)

The distribution of the sample means is much more symmetric and approximately bell-shaped.

7. Normal Distribution Comparison

According to the Central Limit Theorem, the sampling distribution of the mean is approximately normal.

The theoretical standard deviation is:

theoretical_sd_sampling
## [1] 0.7905694

We can overlay the theoretical normal curve on the histogram.

hist(means,
     breaks = 30,
     probability = TRUE,
     main = "Sampling Distribution with Normal Curve",
     xlab = "Sample Mean",
     col = "lightblue")

curve(dnorm(x,
            mean = theoretical_mean_sampling,
            sd = theoretical_sd_sampling),
      add = TRUE,
      lwd = 2)

abline(v = theoretical_mean_sampling,
       lwd = 2,
       lty = 2)

8. Conclusion

The simulation demonstrates the Central Limit Theorem. Although the original exponential distribution is right-skewed, the distribution of the means of samples containing 40 observations is approximately normal. The simulated mean is close to the theoretical mean of 5, and the simulated variance is close to the theoretical variance of 0.625.

This shows that increasing the sample size makes the sampling distribution of the mean approximately normal, even when the original population distribution is not normal.