Overview

This assignmnet will evaluate the exponential distribution vs the normal distribution. The goal is to see if the normal distribution is a good estimator for the means of the exponential distribution.

Simulations

The first simuation is run replicating the random exponential function in r with lambda 0.2 and n = 40. The simulation is then put into a data frame to get it ready for ggplot

library(ggplot2)

ex.mean.sim <- apply(replicate(1000, rexp(40, 0.2)), 2, mean)
ex.data <- data.frame(ex.mean.sim)

Sample Mean vs Theoretical Mean

The mean.mean is the sample mean. This is evaluated against the theoretical mean of 5 (1/lambda) for the exponential distribution.

mean.mean <- mean(ex.mean.sim)
mean.mean
## [1] 4.994734
mean.mean - 5
## [1] -0.005266117

Sample Variance vs Theoretical Variance

A similar approach is taken to evaluate the sample variance(sd1) vs theoretical variance(sdt).

sd1 <- sd(ex.mean.sim)
sd1
## [1] 0.7849694
sdt <- (5)/sqrt(40)
sd1 - sdt
## [1] -0.005599982

Distributions

Now to plot the simuation. The red lines uses the theoretical sd and mean. The orange lines uses the simulated values for the sd and mean. Notice how they are very close but not exact. The blue line uses the density fucntion to have a more fitted distribution.

ggplot(ex.data, aes(x= ex.mean.sim )) +
        geom_histogram(aes(y =..density..), binwidth = .1, fill = I("steelblue"), col = I("black")) +
        geom_density(col = "blue") +
        stat_function(fun = dnorm, args = list(mean = mean.mean, sd = sd1), col = "orange") +
        stat_function(fun = dnorm, args = list(mean = 5, sd =sdt ), col = "red") +
        geom_vline(xintercept = 5, colour = "red") +
        geom_vline(xintercept = mean.mean, colour = "orange")+
        ggtitle("Histogram of simulated means of Exponential Dist")

This qqplot is used to take a finer look at the data. It shows that the data is not quiet normal on the tails. This goes along with the previous plot to show the the right tail is noticeibly longer in the simulation vs the normal distribution. However, overall the normal distribution is a decent fit.

ggplot(ex.data, aes(sample = ex.mean.sim)) +
        stat_qq(distribution = qnorm) +
        geom_abline(intercept = 5, slope = 1.5/2, col = "steelblue") +
        ggtitle("Qqplot of simulated dist vs normal dist")