In this lab, we investigate the ways in which the statistics from a random sample of data can serve as point estimates for population parameters. We’re interested in formulating a sampling distribution of our estimate in order to learn about the properties of the estimate, such as its distribution.

The data

We consider real estate data from the city of Ames, Iowa. 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.

load("more/ames.RData")

To save some effort throughout the lab, create two variables with short names that represent these two variables.

area <- ames$Gr.Liv.Area
price <- ames$SalePrice

Let’s look at the distribution of area in our population of home sales by calculating a few summary statistics and making a histogram.

summary(area)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     334    1126    1442    1500    1743    5642
hist(area)

  1. Describe this population distribution.

    It is fairly normally distributed

The unknown sampling distribution

If we were interested in estimating the mean living area in Ames based on a sample, we can use the following command to survey the population.

set.seed(5)
samp1 <- sample(area, 50)

summary(samp1)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     672    1050    1308    1399    1698    2696
hist(samp1)

  1. Describe the distribution of this sample. How does it compare to the distribution of the population?

    It is still basically normal.

If we’re interested in estimating the average living area in homes in Ames using the sample, our best single guess is the sample mean.

mean(samp1)
## [1] 1399.32
  1. Take a second sample, also of size 50, and call it samp2. How does the mean of samp2 compare with the mean of samp1? Suppose we took two more samples, one of size 100 and one of size 1000. Which would you think would provide a more accurate estimate of the population mean?
set.seed(42)
samp2 <- sample(area, 50)

summary(samp2)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     641    1143    1364    1420    1661    2322
hist(samp2)

The mean is a bit closer at 1420. The more samples that are taken the closer the point estimate should be to the population mean

Here we will generate 5000 samples and compute the sample mean of each.

set.seed(23)
sample_means50 <- rep(NA, 5000)

for(i in 1:5000){
   samp <- sample(area, 50)
   sample_means50[i] <- mean(samp)
   }

hist(sample_means50)

If you would like to adjust the bin width of your histogram to show a little more detail, you can do so by changing the breaks argument.

hist(sample_means50, breaks = 25)

Here we use R to take 5000 samples of size 50 from the population, calculate the mean of each sample, and store each result in a vector called sample_means50. On the next page, we’ll review how this set of code works.

  1. How many elements are there in sample_means50? Describe the sampling distribution, and be sure to specifically note its center. Would you expect the distribution to change if we instead collected 50,000 sample means?

    There are 5000 elements. The distribution is strongly normal, as one would expect from such a large collection of samples, with the mean right around 1500, the population mean. I doubt that the curve would change dramatically with 50K elements, but it would tend even more closely to a normal distribution.

Interlude: The for loop

Let’s take a break from the statistics for a moment to let that last block of code sink in. You have just run your first for loop, a cornerstone of computer programming. The idea behind the for loop is iteration: it allows you to execute code as many times as you want without having to type out every iteration. In the case above, we wanted to iterate the two lines of code inside the curly braces that take a random sample of size 50 from area then save the mean of that sample into the sample_means50 vector. Without the for loop, this would be painful:

and so on…

With the for loop, these thousands of lines of code are compressed into a handful of lines. We’ve added one extra line to the code below, which prints the variable i during each iteration of the for loop. Run this code.

  1. To make sure you understand what you’ve done in this loop, try running a smaller version. Initialize a vector of 100 zeros called sample_means_small. Run a loop that takes a sample of size 50 from area and stores the sample mean in sample_means_small, but only iterate from 1 to 100. Print the output to your screen (type sample_means_small into the console and press enter). How many elements are there in this object called sample_means_small? What does each element represent?

    There are 100 elements in this list. Each element is a point estimate for a sample of 50 observations from the area vector.

Sample size and the sampling distribution

Mechanics aside, let’s return to the reason we used a for loop: to compute a sampling distribution, specifically, this one.

hist(sample_means50)

The sampling distribution that we computed tells us much about estimating the average living area in homes in Ames. Because the sample mean is an unbiased estimator, the sampling distribution is centered at the true average living area of the the population, and the spread of the distribution indicates how much variability is induced by sampling only 50 home sales.

To get a sense of the effect that sample size has on our distribution, let’s build up two more sampling distributions: one based on a sample size of 10 and another based on a sample size of 100.

sample_means10 <- rep(NA, 5000)
sample_means100 <- rep(NA, 5000)

for(i in 1:5000){
  samp <- sample(area, 10)
  sample_means10[i] <- mean(samp)
  samp <- sample(area, 100)
  sample_means100[i] <- mean(samp)
}

Here we’re able to use a single for loop to build two distributions by adding additional lines inside the curly braces. Don’t worry about the fact that samp is used for the name of two different objects. In the second command of the for loop, the mean of samp is saved to the relevant place in the vector sample_means10. With the mean saved, we’re now free to overwrite the object samp with a new sample, this time of size 100. In general, anytime you create an object using a name that is already in use, the old object will get replaced with the new one.

To see the effect that different sample sizes have on the sampling distribution, plot the three distributions on top of one another.

par(mfrow = c(3, 1))

xlimits <- range(sample_means10)

hist(sample_means10, breaks = 20, xlim = xlimits)
hist(sample_means50, breaks = 20, xlim = xlimits)
hist(sample_means100, breaks = 20, xlim = xlimits)

  1. When the sample size is larger, what happens to the center? What about the spread?

    As the sample size grows the spread narrows and gathers towards the center


On your own

So far, we have only focused on estimating the mean living area in homes in Ames. Now you’ll try to estimate the mean home price.

price <- price/1000
summary(price)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   12.79  129.50  160.00  180.80  213.50  755.00
hist(price)

set.seed(23)
psamp1 <- sample(price, 50)

summary(psamp1)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    84.0   140.9   176.4   205.1   239.8   457.3
hist(psamp1)

This sample's point estimate of the population mean is $205.1K, higher than the actual population mean of $180.8K.
set.seed(23)
psample_means50 <- rep(NA, 5000)

for(i in 1:5000){
   psamp <- sample(price, 50)
   psample_means50[i] <- mean(psamp)
   }
summary(psample_means50)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   143.3   173.1   180.3   180.8   188.0   228.1
hist(psample_means50)

This is a nearly normal distribution with a mean of $180.8 (which I can see with the plot and also the summary. I'd already run the population numbers so I know that this is a frighteningly accurate point estimate (pop mean is also $180.8--I reran with a different seed just to make sure there wasn't a problem with the code). 
set.seed(23)
psample_means150 <- rep(NA, 5000)

for(i in 1:5000){
   psamp <- sample(price, 150)
   psample_means150[i] <- mean(psamp)
   }
summary(psample_means150)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   159.8   176.7   180.8   181.0   185.3   206.0
hist(psample_means150, breaks = 20)

I got $180.8K for the point estimate again. The default binning didn't really show the distribution being closer to normal but when I expanded the number of breaks it gave a very nice picture.

This is a product of OpenIntro that is released under a Creative Commons Attribution-ShareAlike 3.0 Unported. This lab was written for OpenIntro by Andrew Bray and Mine Çetinkaya-Rundel.