If you have access to data on an entire population, say the size of every house in Ames, Iowa, it’s straightforward 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.
Setting a seed: We will take some random samples and build sampling distributions in this lab, which means you should set a seed on top of your lab. If this concept is new to you, review the lab concerning probability.
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.
use "ames.dta"
In this lab we’ll start with a simple random sample of size 60 from the population. Do not forget to preserve
the original dataset within your Stata session. See lab 5 on sampling distributions for more information.
preserve
sample 60, count
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
.
Describe the distribution of house area in your sample. What would you say is the “typical” size within your sample? Also state precisely what you interpreted “typical” to mean.
Would you expect another student’s distribution to be identical to yours? Would you expect it to be similar? Why or why not?
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} + z^\star \frac{s}{\sqrt{n}} \]
You should by now be comfortable with calculating the mean and standard deviation of a sample in Stata. 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 invnormal
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.
We can find the critical value for a 95% confidence interal using
display invnormal(0.975)
which is roughly equal to the value critical value 1.96 that you’re likely familiar with by now. Recall that display
makes Stata perform as a calculator.
Let’s finally calculate the confidence interval using the equation above. Use summarize
to find the sample size, sample mean, and sample standard deviation.
To recap: even though we don’t know what the full population looks like, we’re 95% confident that the true average size of houses in Ames lies between the lower and upper endpoints of your confidence interval. There are a few conditions that must be met for this interval to be valid.
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. Remember to restore the original data before using the summarize
command.
restore, preserve
summarize area
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?
Each student should have gotten a slightly different confidence interval. What proportion of those intervals would you expect to capture the true population mean? Why?
Using Stata, we’re going to collect many samples to learn more about how sample means and confidence intervals vary from one sample to another.
First, let’s introduce the Stata function mean
, which gives the sample mean, standard error, and 95% confidence interval for the population mean.
mean area
Here is the rough outline:
confint.dta
We can accomplish this using the bootstrap
function that we introduced in lab 5 that covered sampling distributions. First, the bootstrap command creates a new dataset, confint.dta
that saves the mean and standard deviation from each of 50 samples of size 60.
bootstrap mean=r(mean) sd=r(sd), saving("confint.dta", replace) size(60) reps(50): summarize area
Then, using this dataset of all the means and standard deviations, we compute the standard error as \(s / \sqrt{n}\). Last, we compute the lower and upper bounds using \(\bar{x} \pm 1.96 \times \text{SE}\)
use "confint.dta", clear
generate se = sd / sqrt(60)
generate lower = mean - 1.96 * se
generate upper = mean + 1.96 * se
Open up the Data Editor to view the first five intervals.
Next we’ll create a plot similar to Figure 4.8 on page 175 of OpenIntro Statistics, 3rd Edition. The first step will be to create a new variable capturemu
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.
generate capturemu = "no"
replace capturemu = "yes" if lower < 1499.69 & upper > 1499.69
You’ll notice we created the new variable, capturemu
in two steps. First, we set capturemu
to “no” for all observations. Then, we set capturemu
= “yes” for observations where the lower bound was less than the population mean and the upper bound was greater than the population mean, or observations where the interval contains the population mean.
Then, we can plot the mean and 1.96 times the standard error using the serrbar
command. Note that we first must create an “id” variable to indicate the different samples and different confidence intervals. The “id” variable will be on the x-axis in our final plot.
generate id = _n
serrbar mean se id, scale (1.96)
tabulate
to explore the variable capturemu
.Pick a confidence level of your choosing, provided it is not 95%. What is the appropriate critical value?
Calculate 50 confidence intervals of area
at the confidence level you chose in the previous question using samples of size 60. Plot all intervals on one plot, 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?
This is a product of OpenIntro that is released under a Creative Commons Attribution-ShareAlike 3.0 Unported. This lab was adapted for Stata by Jenna R Krall and written for OpenIntro by Andrew Bray and Mine Çetinkaya-Rundel.