load(url("http://www.openintro.org/stat/data/ames.RData"))
population <- ames$Gr.Liv.Area
samp <- sample(population, 60)
  1. Describe the distribution of your sample. What would you say is the “typical” size within your sample? Also state precisely what you interpreted “typical” to mean.
hist(samp, breaks=25)

summary(samp)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     800    1092    1447    1473    1687    2790
  1. Would you expect another student’s distribution to be identical to yours? Would you expect it to be similar? Why or why not?
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] 1355.720 1589.447
  1. For the confidence interval to be valid, the sample mean must be normally distributed and have standard error \(s / \sqrt{n}\). What conditions must be met for this to be true?

Confidence levels

  1. What does “95% confidence” mean? If you’re not sure, see Section 4.2.2.
  • confidence: \(Point Estimate ± Confidence Interval * SE\)
  • confidence_interval = 1.96 in case of 95% confidence
mean(population)
## [1] 1499.69
  1. Does your confidence interval capture the true average size of houses in Ames? If you are working on this lab in a classroom, does your neighbor’s interval capture this value?
if (mean(population) > lower & mean(population) < upper){
  printo<-"TRUE"
} else{
  printo<-"FALSE"
}
  • TRUE. My classmates’ interval should capture a very close value to this value.
  1. Each student in your class should have gotten a slightly different confidence interval. What proportion of those intervals would you expect to capture the true population mean? Why? If you are working in this lab in a classroom, collect data on the intervals created by other students in the class and calculate the proportion of intervals that capture the true population mean.
sd_samp_classmates<-c()
mean_samp_classmates<-c()
for(i in 1:35){
  samp_classmates<-sample(samp, 60)
  sd_samp_classmates[i]<-sd(samp_classmates)
  mean_samp_classmates[i]<-mean(samp_classmates)
}
upper_samp_classmates<-mean_samp_classmates+(1.96*sd_samp_classmates)
lower_samp_classmates<-mean_samp_classmates-(1.96*sd_samp_classmates)
  • Taking a sample of 35 with the classmates sampling, 95% confidence is expected to be captured closes to the population mean: (567.3622729, 2377.8043938)

Using R, we’re going to recreate many samples to learn more about how sample means and confidence intervals vary from one sample to another. Loops come in handy here (If you are unfamiliar with loops, review the Sampling Distribution Lab).

Here is the rough outline:

But before we do all of this, we need to first create empty vectors where we can save the means and standard deviations that will be calculated from each sample. And while we’re at it, let’s also store the desired sample size as n.

samp_mean <- rep(NA, 50)
samp_sd <- rep(NA, 50)
n <- 60

Now we’re ready for the loop where we calculate the means and standard deviations of 50 random samples.

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
}

Lastly, we construct the confidence intervals.

lower_vector <- samp_mean - 1.96 * samp_sd / sqrt(n) 
upper_vector <- samp_mean + 1.96 * samp_sd / sqrt(n)

Lower bounds of these 50 confidence intervals are stored in lower_vector, and the upper bounds are in upper_vector. Let’s view the first interval.

c(lower_vector[1], upper_vector[1])
## [1] 1328.970 1550.997

On your own

df <- data.frame(lower_vector, upper_vector)

low <- sum(df$upper_vector < mean(population))
up <- sum(df$lower_vector > mean(population))

without_mean_proportion <- round((low + up)/60 ,2)
  • 95 close to main confidence.
  • Pickcing 90% confidence level will have a confidence level of 1.65.
lower_vector <- samp_mean - 1.65 * samp_sd / sqrt(n) 
upper_vector <- samp_mean + 1.65 * samp_sd / sqrt(n)
plot_ci(lower_vector, upper_vector, mean(population))

df <- data.frame(lower_vector, upper_vector)

left <- sum(df$upper_vector < mean(population))
right <- sum(df$lower_vector > mean(population))

without_mean_proportion <- round((left + right)/60 ,2)
  • 90 close to main confidence.