This project will explore the exponential distribution in R and compare it with the Central Limit Theorem.

library(ggplot2)
lambda <- 0.2
n<-40
nsims<-1:1000

set.seed(822)
means <- data.frame(x = sapply(nsims, function(x) {mean(rexp(n, lambda))}))

##plotting the means which looks like a normal distribution
ggplot(data = means, aes(x = x)) + 
  geom_histogram(binwidth=0.1, aes(y=..density..)) +
  labs(x="Means") +
  labs(y="Density")

Sample Mean Vs Theoretical Mean

##expected mean
expmean<-1/lambda


##Sample mean
sampmean(means$x)

The expected mean is 5 and the sample mean is 4.9868222

Sample Variance vs. Theoretical Variance

##expected standard of sample

expsd<-((1/lambda)/sqrt(n))

##expected variancee
expvar<-expsd^2

The expected variance is 0.625

##variance of means

meansvar<-var(means$x)

The variance of means is 0.6115885

Is the Distribution Normal?

we can see that the distribution of the simulated means (blue) approaches the normal distribution (red) and that their means (blue and red vertical lines, respectively) approach each other as well.

ggplot(data = means, aes(x = x)) + 
  geom_histogram(binwidth=0.1, aes(y=..density..), fill = I('#8A8A8A'),) +
  stat_function(fun = dnorm, arg = list(mean = simmu , sd = simsd), colour = "red", size=2) + 
  geom_vline(xintercept = simmu, size=1, colour="red") + 
  geom_density(colour="blue", size=2) +
  geom_vline(xintercept = simmean, size=1, colour="blue") + 
  labs(x="Means") +
  labs(y="Density")+
  ggtitle("Comparison of distribution of simulation mean(blue)\nwith normal distribution(red)")