In: Statistics and Probability
Coding Language: R
Generate 100 observations from the normal distribution with mean 3 and variance 1.
Compute the sample average, the standard error for the sample average, and the 95% confidence interval.
Repeat the above two steps 1000 times. Report (a) the mean of the 1000 sample means, (b) the standard deviation of the 1000 sample means, (c) the mean of the 1000 standard errors, and (d) how many times (out of 1000) the 95% confidence intervals include the population mean.
approximately 950 time confidence interval will include population mean.
#Generating 100 observations from the N(mean=3,Var=1)
rand_n<-rnorm(100,mean=3,sd=1)
# sample average
sample_mean=mean(rand_n)
#standard error for mean
se <- sd(rand_n) / sqrt(length(rand_n))
#We can calculate a 95% confidence interval for a sample mean by
adding and subtracting 1.96 standard errors to the point estimate
(See Section 4.2.3 if you are unfamiliar with this formula).
lower <- sample_mean - 1.96 * se
upper <- sample_mean + 1.96 * se
c(lower, upper)
# to repeat the above steps
#we need to first create empty vectors where we can save the means
and standard deviations that will be calculated from each
sample.
samp_mean <- rep(NA, 1000)
samp_sd <- rep(NA, 1000)
n <- 1200
for(i in 1:1000){
samp <- rnorm(100,mean=3,sd=1) # obtain a sample of size 100
observations from the N(mean=3,Var=1)
samp_mean[i] <- mean(samp) # save sample mean in ith element of
samp_mean
samp_sd[i] <- sd(samp) # save sample sd in ith element of
samp_sd
samp_s[i] <- sd(samp) / sqrt(length(samp)) # save sample se in
ith element of samp_s
}
lower_vector <- samp_mean - 1.96 * samp_sd / sqrt(n)
upper_vector <- samp_mean + 1.96 * samp_sd / sqrt(n)
c(lower_vector, upper_vector)
#