load("more/ames.RData")
population <- ames$Gr.Liv.Area
set.seed(999999)
samp <- sample(population, 60)
hist(samp)The distribution is essentially unimodal, and nearing a normal distribution. Typical size of the sample is 1500, as indicated by the frequency indicated in the histogram.
The distribution will be different because we are generating a random sample. I have set a seed to prevent a different outcome when running this markdown, or else running the code would alter the sample, and by definition the distribution.
sample_mean <- mean(samp)
se <- sd(samp) / sqrt(60)
lower <- sample_mean - 1.96 * se
upper <- sample_mean + 1.96 * se
c(lower, upper)## [1] 1322.756 1504.977
Samples must be independent and sampling must be random with sample size greater than 30.
This confidence refers to the probability that the population mean lies in the range specified by the confidence interval.
mean(population)## [1] 1499.69
With mean 1499.6904437 between 1322.756475 and 1504.9768583 the confidence interval captures the true average.
I would expect that the mean would lie in the condfidence interval 95% of the time by definition.
samp_mean <- rep(NA, 50)
samp_sd <- rep(NA, 50)
n <- 60
for(i in 1:50){
samp <- sample(population, n) # obtain a sample of size n = 60 from the population
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
}
lower_vector <- samp_mean - 1.96 * samp_sd / sqrt(n)
upper_vector <- samp_mean + 1.96 * samp_sd / sqrt(n)
c(lower_vector[1], upper_vector[1])## [1] 1267.564 1495.136
Using the following function (which was downloaded with the data set), plot all intervals. What proportion of your confidence intervals include the true population mean? Is this proportion exactly equal to the confidence level? If not, explain why.
plot_ci(lower_vector, upper_vector, mean(population))Only 3 of the 50 (6%) confidence intervals do not encompass the population, resulting in correct estimation 94% of the time.
For a 99% confidence level, the critical value is 2.575.
plot_ci function, plot all intervals and calculate the proportion of intervals that include the true population mean. How does this percentage compare to the confidence level selected for the intervals?confidence<-2.575
for(i in 1:50){
samp <- sample(population, n) # obtain a sample of size n = 60 from the population
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
}
lower_vector <- samp_mean - confidence * samp_sd / sqrt(n)
upper_vector <- samp_mean + confidence * samp_sd / sqrt(n)
plot_ci(lower_vector, upper_vector, mean(population))I chose a 99% confidence level and this result confirms the value, with 100% of the intervals containing the mean. Let’s try 120 intervals.
confidence<-2.575
for(i in 1:120){
samp <- sample(population, n) # obtain a sample of size n = 60 from the population
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
}
lower_vector <- samp_mean - confidence * samp_sd / sqrt(n)
upper_vector <- samp_mean + confidence * samp_sd / sqrt(n)
plot_ci(lower_vector, upper_vector, mean(population))All means are still encompassed. Let’s try 500.
confidence<-2.575
for(i in 1:500){
samp <- sample(population, n) # obtain a sample of size n = 60 from the population
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
}
lower_vector <- samp_mean - confidence * samp_sd / sqrt(n)
upper_vector <- samp_mean + confidence * samp_sd / sqrt(n)
plot_ci(lower_vector, upper_vector, mean(population))Finally, with 500 confidence intervals, we have 2 of 500 intervals that do not encompass the mean, yielding 99.6% accuracy.