library(knitr)
library(ggplot2)
lambda<-0.2
samples<-40
sims<-1000
simExp<-replicate(sims, rexp(samples, lambda))
# Calculate the means of the exponential simulations
simMean<-apply(simExp, 2, mean)
# Creating histogram for the simulations
hist(simMean, breaks=20, main="Simulation means", col="green")
sampleMean<-mean(simMean)
print(sampleMean)
## [1] 4.9539
theoreticalMean<-1/lambda
print(theoreticalMean)
## [1] 5
## Creating plot for the comparision of sample mean and theoretical mean
hist(simMean, col="darkblue", main="Sample Mean Vs Theoretical Mean",
breaks=20, xlab="Simulation means")
abline(v=theoreticalMean, lwd="5", col="green")
legend("topright", legend="Theoretical mean", box.lwd=0,box.col="white", col="green", pch=15)
sampleVar<- var(simMean)
print(sampleVar)
## [1] 0.6006456
theoreticalVar<-(1/lambda)^2/samples
print(theoreticalVar)
## [1] 0.625
hist(simMean, prob=TRUE, col="green", breaks=20,
xlab="Simulation means", main="Normal approximation")
# Creating curve for normal distribution approximation
curve( dnorm(x, mean=sampleMean, sd=sd(simMean)), add=TRUE, col="blue", lwd=4)