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")

We see that there are quite a few variables in the data set, enough to do a very in-depth analysis. For this lab, we’ll restrict our attention to just two of the variables: the above ground living area of the house in square feet (Gr.Liv.Area) and the sale price (SalePrice). 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.

These data are strongly skewed, which can see by the long right tail with a few ouliers.

The unknown sampling distribution

In this lab we have access to the entire population, but this is rarely the case in real life. Gathering information on an entire population is often extremely costly or impossible. Because of this, we often take a sample of the population and use that to understand the properties of the population.

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.

samp1 <- sample(area, 50)

This command collects a simple random sample of size 50 from the vector area, which is assigned to samp1. This is like going into the City Assessor’s database and pulling up the files on 50 random home sales. Working with these 50 files would be considerably simpler than working with all 2930 home sales.

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

These data are strongly skewed, which can see by the long right tail. Because sample size is small, distribution is less normal. And approximatition can be poor.

hist(samp1)

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] 1662.24

Depending on which 50 homes you selected, your estimate could be a bit above or a bit below the true population mean of 1499.69 square feet. In general, though, the sample mean turns out to be a pretty good estimate of the average living area, and we were able to get it by sampling less than 3% of the population.

  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?

First 2 samples are far from population mean, because of small sample size. Increase of sample size will decrease difference between sample mean and population mean. Sample of 1000 will provide most accurate result than samples 1-3.

samp2 <- sample(area, 50)
mean(samp2)
## [1] 1380.14
samp3 <- sample(area, 100)
mean(samp3)
## [1] 1449.82
samp4 <- sample(area, 1000)
mean(samp4)
## [1] 1495.871

Not surprisingly, every time we take another random sample, we get a different sample mean. It’s useful to get a sense of just how much variability we should expect when estimating the population mean this way. The distribution of sample means, called the sampling distribution, can help us understand this variability. In this lab, because we have access to the population, we can build up the sampling distribution for the sample mean by repeating the above steps many times. Here we will generate 5000 samples and compute the sample mean of each.

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?

We used 5000 samples. It’s normal distribution without skew, with center around 1490. If sample size will increase to 50,000 it will become more normal.

Interlude: The for loop

  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, representing the mean from 100 samples.

sample_means_small <- rep(0, 100)

for(i in 1:100){
   samp5 <- sample(area, 50)
   sample_means_small[i] <- mean(samp5)
}
sample_means_small
##   [1] 1555.92 1423.26 1470.58 1556.98 1415.00 1466.66 1482.06 1467.52
##   [9] 1449.24 1551.76 1573.22 1473.54 1465.86 1561.58 1410.22 1556.28
##  [17] 1414.80 1417.80 1401.30 1451.70 1440.30 1585.72 1604.08 1538.72
##  [25] 1416.24 1529.80 1461.24 1538.44 1506.14 1538.94 1542.62 1517.94
##  [33] 1414.60 1428.60 1399.62 1443.10 1443.48 1500.40 1497.24 1572.32
##  [41] 1502.86 1528.18 1410.78 1616.14 1436.44 1450.50 1596.86 1615.50
##  [49] 1424.06 1443.14 1506.48 1465.64 1522.06 1431.90 1375.78 1504.18
##  [57] 1513.94 1487.06 1486.84 1470.54 1575.26 1488.58 1489.60 1428.10
##  [65] 1474.74 1510.10 1530.10 1558.00 1539.60 1586.90 1446.14 1415.32
##  [73] 1526.76 1490.54 1539.30 1488.88 1455.64 1539.90 1422.80 1530.28
##  [81] 1552.44 1383.70 1619.68 1589.66 1543.08 1388.74 1370.42 1453.30
##  [89] 1569.42 1514.60 1493.40 1496.40 1489.44 1420.82 1402.08 1455.26
##  [97] 1631.52 1488.00 1593.60 1563.92

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)

The first command specifies that you’d like to divide the plotting area into 3 rows and 1 column of plots (to return to the default setting of plotting one at a time, use par(mfrow = c(1, 1))). The breaks argument specifies the number of bins used in constructing the histogram. The xlim argument specifies the range of the x-axis of the histogram, and by setting it equal to xlimits for each histogram, we ensure that all three histograms will be plotted with the same limits on the x-axis.

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

Center area is taller, ouliers are less visible. It is more narrow and not as strongly skewed as histogram with small sample size.


On your own

1

samp_price <- sample(price, 50)
mean(samp_price)
## [1] 176615.2

2

samp_price_50 <- rep(NA, 5000)

for(i in 1:5000){
   samp_p <- sample(price, 50)
   samp_price_50[i] <- mean(samp_p)
}
hist(samp_price_50, breaks =20)

It’s normal and unimodal with center around 180,000 (around population mean). It’s right skewed due to few ouliers.

mean(samp_price_50)
## [1] 180641
mean(price)
## [1] 180796.1

3

samp_price_150 <- rep(NA, 5000)

for(i in 1:5000){
   samp_p <- sample(price, 150)
   samp_price_150[i] <- mean(samp_p)
}
hist(samp_price_150,breaks =20)

mean(samp_price_150)
## [1] 180733

It’s more normal. Right skew is less visible and it’s more narrow. Mean is a littlr bit over 180,000

4

3 has smaller spread because sample size is larger. I would prefer distribution with smaller spread.

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.