Part I: Simulation Exercise
In this part of the project, I investigate the exponential distribution in R and compare it with the Central Limit Theorem
## load libraries and set constants
library(RColorBrewer)
lam = 0.2
nsim = 1000
reps = 40
## Sample vs theoretical distribution Mean
samDist = rexp(reps, lam)
samMean = mean(samDist)
sim = matrix(sample(samDist, nsim*reps, replace = TRUE), nrow = nsim, ncol = reps)
simMeans = apply(sim, 1, mean)
theorMean = mean(simMeans)
cols = brewer.pal(n = 11, name = "RdBu")
par(mfrow=c(1,1))
hist(simMeans, col=cols, main="Theoretical Distibution of Means", xlab=" ")
abline(v=samMean,col="black", lwd=4)
## Sample vs theoretical distribution variability
samVar = var(samDist)
theorVar = mean(apply(sim, 1, var))
hist(apply(sim, 1, var), col=cols, main="Thoretical Distibution of Variance", xlab=" ")
abline(v=samVar,col="black", lwd=4)
## Show dist is approx normal
par(mfrow=c(1,2))
hist(simMeans, col=cols, main="Theoretical population mean \n distribution", xlab=" ", xlim=range(simMeans))
hist(rexp(nsim, lam), col=cols, main="Distribution of 1000 random \n exponential values", xlab=" ")