# Ames, Iowa dataset
download.file("http://www.openintro.org/stat/data/ames.RData", destfile = "ames.RData")
load("ames.RData")
# Generate a simple random sample of size 60. Look at size of house.
population <- ames$Gr.Liv.Area
samp <- sample(population, 60)

EXERCISE 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.

This sample has a right-skewed distributionm. The typical size is about 1526 sq ft. I interpreted ‘typical’ as where the median lies. I chose median over mean because there are upper outliers that skew the mean higher.

hist(samp, breaks=10)

summary(samp)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     572    1117    1403    1480    1721    4476

EXERCISE 2

Would you expect another student’s distribution to be identical to yours? Would you expect it to be similar? Why or why not?

No, not identical. It would be similar. The sample was randomly generated to mimic a random sample from the population. A little variation is expected due to sampling error.

sample_mean <- mean(samp)
# Calculate a 95% confidence interval for a sample mean
se <- sd(samp) / sqrt(60)
lower <- sample_mean - 1.96 * se
upper <- sample_mean + 1.96 * se
c(lower, upper)
## [1] 1330.528 1629.772

EXERCISE 3

For the confidence interval to be valid, the sample mean must be normally distributed and have standard error s/n‾√. What conditions must be met for this to be true?

The sample must be a random sample. The observations (i.e. homes) must be independent of each other. And, the population must be large compared to the sample size.

EXERCISE 4

What does “95% confidence” mean? If you’re not sure, see Section 4.2.2.

It means we are 95% confident that the population mean lies within the interval between a lower bound and an upper bound.

mean(population)
## [1] 1499.69

EXERCISE 5

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?

Yes, the confidence interval captures the true average size. The true population mean (1499.7) is between 1470.3 and 1714.0 sq ft.

EXERCISE 6

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.

I would expect 95% of the confidence intervals generated by other students to capture the true population mean. If a confidence interval is created for each sample in a meta study, then 95% of the confidence intervals will contain the true population mean.

NEXT SECTION

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:

  1. Obtain a random sample.
  2. Calculate and store the sample’s mean and standard deviation.
  3. Repeat steps (1) and (2) 50 times.
  4. Use these stored statistics to calculate many confidence intervals.
# 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 bounds of these 50 confidence intervals are stored in lower_vector, and the upper bounds are in upper_vector. 

lower_vector <- samp_mean - 1.96 * samp_sd / sqrt(n) 
upper_vector <- samp_mean + 1.96 * samp_sd / sqrt(n)
# Let’s view the first interval.
c(lower_vector[1], upper_vector[1])
## [1] 1311.882 1571.951

On Your Own

1. 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.

2 out 50 random samples do not include the true population mean. So, 48 out of 50, or 96% of the intervals, include the true population mean.

This proportion is not exactly equal to the confidence level, which is not unusual. The confidence interval can be interpreted as a probabilty (of an interval containing the true population mean).

plot_ci(lower_vector, upper_vector, mean(population))

2. Pick a confidence level of your choosing, provided it is not 95%. What is the appropriate critical value?

80% confidence level. Critical value is 1.299

# Calculate critical value for 80% CI

qt(0.90, df=49)  # n is 60
## [1] 1.299069

3. Calculate 50 confidence intervals at the confidence level you chose in the previous question. You do not need to obtain new samples, simply calculate new intervals based on the sample means and standard deviations you have already collected. Using the 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?

7 out of 50 samples do not include the true population mean. Therefore, the proportion of intervals that include the true population mean is 43 out of 50, or 86%.

This percentage is quite a bit higher than the 80% confidence level.

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

plot_ci(lower_vector, upper_vector, mean(population))