Setting up the variables

set.seed(100)
lambda <- 0.2
n <- 40 
simulation <- 1000

Calculate the simulations

simulations_exp <- replicate(simulation, rexp(n,lambda))
str(simulations_exp)
##  num [1:40, 1:1000] 4.621 3.619 0.523 15.487 3.124 ...
means_exp <-apply(simulations_exp,2,mean)
str(means_exp)
##  num [1:1000] 4.14 6.05 4.42 4.4 3.21 ...
mean_theory <- 1/lambda
mean_theory
## [1] 5
standardDeviation_theory <- (1/lambda)/sqrt(n)
standardDeviation_theory
## [1] 0.7905694
variance_theory <- standardDeviation_theory^2
variance_theory
## [1] 0.625

Question 1:

Show the sample mean and compare it to the theoretical mean of the distribution

mean_simulation <- mean(means_exp)
mean_simulation
## [1] 4.999702
hist(means_exp,xlab = "means", main = "Distrbution of means of Exponential Simulation")
abline(v= mean_theory, col = "red",lwd =3,lty=1)
abline(v= mean_simulation, col = "yellow", lwd =3,lty=2)

Question 2

Show how variable the sample is (via variance) and compare it to the theoretical variance of the distribution

standarddeviation_simulation<- sd(means_exp)
variance_simulation <- standarddeviation_simulation^2
variance_simulation
## [1] 0.6432442

Question 3

Show that the distribution is approximately normal

library(ggplot2) 
means <- data.frame(means_exp)
names(means) <- "mean"
g<- ggplot(data = means, aes(x=mean))
g<- g+ geom_histogram(aes(y= ..density..), fill = I("green4"),binwidth = 0.2,color=I("red4"))
g<- g+ stat_function(fun = dnorm,args = list(mean = mean_theory,sd = standardDeviation_theory), color = I("red1"),lwd = 2)

g

qqnorm(means_exp)
qqline(means_exp, col = 2)