First, we run a 1000 simulations each with 40 observations and λ(rate)=0.2. We generate these samples and calculate average for each simulation (set of 40).
The theoretical mean should be 1/λ and the sample mean is calculated by averaging all 1000 simulations.
theomean <- 1/lambda
actmean <- mean(samplemeans)
theomean
## [1] 5
actmean
## [1] 4.999702
The theoretical mean is 5 and the analytical mean (actmean) is 4.999702. We visualize the following simulation with a histogram.
hist(x=samplemeans, breaks = 20, main = "Histogram of Sample Means", xlab = "Sample Mean",ylab="Frequency")
abline(v=theomean, col = "blue")
abline(v=actmean, col = "red")
The formula to find the theoretical variance is: (1/λ)^2 / n. The variance of the sample means can be found using the var() function.
theovariance <- (1/lambda)^2 / n
actvariance <- var(samplemeans)
theovariance
## [1] 0.625
actvariance
## [1] 0.6335302
g <- ggplot(data.frame(x = samplemeans), aes(x = x)) +
geom_histogram(position="identity", fill="yellow", color="black", alpha=0.2,binwidth=0.5, aes(y= ..density..)) +
stat_function(fun = dnorm, color = "red", args=list(mean=5)) +
labs(x="Sample Mean",y="Frequency", title = "Distribution of Sample Means vs. Normal Distribution")
g