If you have access to data on an entire population, say the size of every house in Ames, Iowa, it’s straight forward to answer questions like, “How big is the typical house in Ames?” and “How much variation is there in sizes of houses?”. If you have access to only a sample of the population, as is often the case, the task becomes more complicated. What is your best guess for the typical size if you only know the sizes of several dozen houses? This sort of situation requires that you use your sample to make inference on what your population looks like.
We will explore the data using the dplyr package and visualize it using the ggplot2 package for data visualization. The data can be found in the companion package for this course, statsr.
Let’s load the packages.
library(statsr)
library(dplyr)
library(ggplot2)We consider real estate data from the city of Ames, Iowa. This is the same dataset used in the previous lab. The details of every real estate transaction in Ames is recorded by the City Assessor’s office. Our particular focus for this lab will be all residential home sales in Ames between 2006 and 2010. This collection represents our population of interest. In this lab we would like to learn about these home sales by taking smaller samples from the full population. Let’s load the data.
data(ames)In this lab we’ll start with a simple random sample of size 60 from the population. Specifically, this is a simple random sample of size 60. Note that the data set has information on many housing variables, but for the first portion of the lab we’ll focus on the size of the house, represented by the variable area.
n <- 60
samp <- sample_n(ames, n)Describe the distribution of homes in your sample. What would you say is the “typical” size within your sample? Also state precisely what you interpreted “typical” to mean.
ggplot(samp, aes(x = area)) +
geom_histogram(binwidth = 100)
We can see that the sample is right skewed.
Return for a moment to the question that first motivated this lab: based on this sample, what can we infer about the population? Based only on this single sample, the best estimate of the average living area of houses sold in Ames would be the sample mean, usually denoted as \(\bar{x}\) (here we’re calling it x_bar). That serves as a good point estimate but it would be useful to also communicate how uncertain we are of that estimate. This uncertainty can be quantified using a confidence interval.
A confidence interval for a population mean is of the following form \[ \bar{x} \pm z^\star \frac{s}{\sqrt{n}} \]
You should by now be comfortable with calculating the mean and standard deviation of a sample in R. And we know that the sample size is 60. So the only remaining building block is finding the appropriate critical value for a given confidence level. We can use the qnorm function for this task, which will give the critical value associated with a given percentile under the normal distribution. Remember that confidence levels and percentiles are not equivalent. For example, a 95% confidence level refers to the middle 95% of the distribution, and the critical value associated with this area will correspond to the 97.5th percentile.
Below we will be discussing the situation when the confidence level is 99%.
We can find the critical value for a 99% confidence interal using
z_star_99 <- abs(qnorm((1 - 0.99) / 2))
z_star_99## [1] 2.575829
which is roughly equal to the value critical value 2.58.
Let’s calculate the confidence interval:
samp %>%
summarise(lower = mean(area) - z_star_99 * (sd(area) / sqrt(n)),
upper = mean(area) + z_star_99 * (sd(area) / sqrt(n)))## # A tibble: 1 x 2
## lower upper
## <dbl> <dbl>
## 1 1363. 1660.
To recap: even though we don’t know what the full population looks like, we’re 99% confident that the true average size of houses in Ames lies between the values lower and upper. There are a few conditions that must be met for this interval to be valid.
What does a 99% confidence level mean? Answer: 99% of random samples of size 60 will yield confidence intervals that contain the true average area of houses in Ames, Iowa.
In this case we have the rare luxury of knowing the true population mean since we have data on the entire population. Let’s calculate this value so that we can determine if our confidence intervals actually capture it. We’ll store it in a data frame called params (short for population parameters), and name it mu.
params <- ames %>%
summarise(mu = mean(area))Does our confidence interval capture the true average size of houses in Ames?
samp %>%
summarise(lower = mean(area) - z_star_99*(sd(area)/sqrt(n)),
upper = mean(area) + z_star_99*(sd(area)/sqrt(n)))## # A tibble: 1 x 2
## lower upper
## <dbl> <dbl>
## 1 1363. 1660.
It does!
Using R, we’re going to collect many samples to learn more about how sample means and confidence intervals vary from one sample to another.
Here is the rough outline:
CI<- ames %>%
rep_sample_n(size = n, reps = 50, replace = TRUE) %>%
summarise(lower = mean(area) - z_star_99*(sd(area)/sqrt(n)),
upper = mean(area) + z_star_99*(sd(area)/sqrt(n)))Let’s view the first five intervals:
CI %>%
slice(1:5)## # A tibble: 5 x 3
## replicate lower upper
## <int> <dbl> <dbl>
## 1 1 1284. 1600.
## 2 2 1340. 1616.
## 3 3 1244. 1516.
## 4 4 1384. 1720.
## 5 5 1384. 1665.
Next we’ll create a plot similar to Figure 4.8 on page 175 of OpenIntro Statistics, 3rd Edition. First step will be to create a new variable in the CI data frame that indicates whether the interval does or does not capture the true population mean. Note that capturing this value would mean the lower bound of the confidence interval is below the value and upper bound of the confidence interval is above the value. Remember that we create new variables using the mutate function.
CI<-CI %>%
mutate(capture_mu = ifelse(lower < params$mu & upper > params$mu, 'Yes', 'No'))We now have all the information we need to create the plot, but we need to re-organize our data a bit for easy plotting. Specifically, we need to organize the data in a new data frame where each row represents one bound, as opposed to one interval. We can accomplish this using the following:
CI_data<-data.frame(CI_id = c(1:50, 1:50),
CI_bounds = c(CI$lower, CI$upper),
capture_mu = c(CI$capture_mu, CI$capture_mu))CI_data %>%
slice(1:10)## CI_id CI_bounds capture_mu
## 1 1 1283.552 Yes
## 2 2 1340.482 Yes
## 3 3 1243.549 Yes
## 4 4 1384.035 Yes
## 5 5 1383.637 Yes
## 6 6 1281.840 Yes
## 7 7 1267.885 Yes
## 8 8 1432.453 Yes
## 9 9 1371.074 Yes
## 10 10 1317.026 Yes
And finally we can create the plot using the following:
ggplot(CI_data, aes(x = CI_bounds, y = CI_id, group = CI_id, color = capture_mu))+
geom_line()+
geom_point()+
geom_vline(xintercept = params$mu, color = 'black')According to the graph, 100% of the confidence intervals include the true population mean, which is very close to our confidence level 99%.