Sie sind auf Seite 1von 3

Peer Graded Assignment: Statistical Inference Course

Project
Yuan Wei
August 7, 2016
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.2.5

Overview
This assignment investigate the exponential distribution in R and compare it with the Central Limit Theorem.
Running simulations
1000 simulations were run to investigate the distribution of averages of 40 exponentials. Ggplot 2 package
was used to generating the plot.
set.seed(100)
lambda <- 0.2
sim <- 1000
n <- 40

# Simulate and take mean


simulations <- replicate(sim, rexp(n,lambda))
sim.means <- colMeans(simulations)
To calculate the sample mean and compare it to the theoretical mean of the distribution.
# Calculate simulated mean and true mean
dis.mean <- mean(sim.means)
true.mean <- 1/lambda
# Plot histogram of simulated mean with true mean
g <- ggplot(as.data.frame(sim.means), aes(sim.means))
g + geom_histogram(fill = "white", colour = "blue", binwidth = 0.25) +
geom_vline(xintercept = true.mean) +
xlab("mean") +
ggtitle("Mean of 1000 simulations")

Mean of 1000 simulations


150

count

100

50

0
3

mean
Show how variable the sample is (via variance) and compare it to the theoretical variance of the distribution.
# Calculate true standard deviations and variance
true.std <- 1/lambda/sqrt(n)
true.var <- true.std^2
# Calculate simulated mean and true mean
dis.std <- sd(sim.means)
dis.var <- dis.std^2
# Display results
print("Distribution variance")
## [1] "Distribution variance"
dis.var
## [1] 0.6432442
print("Theoretical variance")
## [1] "Theoretical variance"

true.var
## [1] 0.625
Show that the distribution is approximately normal.
# Plot histogram of simulated mean with true mean
g <- ggplot(as.data.frame(sim.means), aes(sim.means))
g + geom_histogram(fill = "white", colour = "blue", binwidth = 0.25, aes(y=..density..)) +
stat_function(fun = dnorm, colour = "red", args = list(mean = true.mean, sd = true.std)) +
xlab("mean") +
ggtitle("Mean of 1000 simulations")

Mean of 1000 simulations


0.6

density

0.4

0.2

0.0
3

mean

Das könnte Ihnen auch gefallen