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.
Before we do anything else, let’s set the seed so our samples will be reproducible.
set.seed(1392)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$SalePriceLet’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)This data is definitely right-skewed compared to a perfectly normal 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.
par(mfrow=c(1,2))
hist(area,freq=FALSE,xlab="Area",main="Population")
hist(samp1,freq=FALSE,xlab="Area",main="Sample (n=50)",
breaks=seq(from=0,to=3000,by=500))par(mfrow=c(1,1))
summary(area)## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 334 1126 1442 1500 1743 5642
summary(samp1)## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 864 1273 1428 1509 1713 2978
mean(area);sd(area)## [1] 1499.69
## [1] 505.5089
mean(samp1);sd(samp1)## [1] 1509.04
## [1] 420.9422
length(which(area < 500))## [1] 6
length(which(area > 3000))## [1] 26
The distribution of this sample is overall pretty similar to that of the population.
It has a very similar mean.
However, it has a smaller standard deviation.
The major difference is that for values that are very rare in the population, we may not always find them in the sample. For example, the population has 6 homes with less than 500 square feet of living space, and 26 with greater than 3,000 square feet of living space.
Meanwhile we do not see any homes with less than 500 square feet or more than 3,000 square feet in our sample. This makes sense, as the frequency of these values in the population is less than the ratio of n to the population size. So it is more likely than not that we would not find these values (though we could still find them by chance sometimes).
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] 1509.04
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.
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?samp2 <- sample(area,50)
mean(samp1);mean(samp2)## [1] 1509.04
## [1] 1486.38
The means of the two samples are similar, but not exactly the same.
The larger sample (size 1000) would provide a more accurate estimate of the population mean.
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)mean(area)## [1] 1499.69
mean(sample_means50)## [1] 1499.976
sd(area)/sqrt(50)## [1] 71.48975
sd(sample_means50)## [1] 70.06694
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.
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 5,000 elements in sample_means50.
The center of the sampling distribution is near the population mean.
The standard deviation of the sampling distribution is near the population sd over the square root of n.
The sampling distribution is very close in shape to a normal distribution.
We would not expect the sampling distribution to change substantially as we take more samples. With very few samples, the sampling distribution may start out not that close to normal (especially if the population is a bit skewed). But here, we already have 5,000 samples, and the sampling distribution is already quite close to normal. It will not change any of its characteristics much by adding more samples at this point.
for loopLet’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:
sample_means50 <- rep(NA, 5000)
samp <- sample(area, 50)
sample_means50[1] <- mean(samp)
samp <- sample(area, 50)
sample_means50[2] <- mean(samp)
samp <- sample(area, 50)
sample_means50[3] <- mean(samp)
samp <- sample(area, 50)
sample_means50[4] <- mean(samp)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.
Edit: I am actually commenting out the line to print i each time so the Rmarkdown is not too long.
sample_means50 <- rep(NA, 5000)
for(i in 1:5000){
samp <- sample(area, 50)
sample_means50[i] <- mean(samp)
#print(i)
}Let’s consider this code line by line to figure out what it does. In the first line we initialized a vector. In this case, we created a vector of 5000 zeros called sample_means50. This vector will will store values generated within the for loop.
The second line calls the for loop itself. The syntax can be loosely read as, “for every element i from 1 to 5000, run the following lines of code”. You can think of i as the counter that keeps track of which loop you’re on. Therefore, more precisely, the loop will run once when i = 1, then once when i = 2, and so on up to i = 5000.
The body of the for loop is the part inside the curly braces, and this set of code is run for each value of i. Here, on every loop, we take a random sample of size 50 from area, take its mean, and store it as the \(i\)th element of sample_means50.
In order to display that this is really happening, we asked R to print i at each iteration. This line of code is optional and is only used for displaying what’s going on while the for loop is running.
The for loop allows us to not just run the code 5000 times, but to neatly package the results, element by element, into the empty vector that we initialized at the outset.
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?sample_means_small <- rep(0,times=100)
for(i in 1:100)
{
sample_means_small[i] <- mean(sample(area,50))
}
sample_means_small## [1] 1538.66 1388.92 1479.66 1621.92 1423.92 1519.24 1389.90 1525.06
## [9] 1477.38 1578.68 1432.62 1530.12 1373.04 1537.94 1485.60 1365.50
## [17] 1510.28 1517.48 1464.80 1557.00 1513.80 1594.18 1531.90 1513.76
## [25] 1490.54 1535.50 1550.26 1575.46 1570.56 1446.90 1449.42 1551.02
## [33] 1621.66 1498.64 1449.50 1564.74 1420.78 1623.20 1409.64 1425.90
## [41] 1442.24 1476.20 1725.62 1547.54 1382.20 1444.72 1615.28 1406.88
## [49] 1505.72 1476.98 1510.20 1519.90 1578.24 1599.74 1550.86 1383.38
## [57] 1507.36 1556.06 1427.50 1443.92 1393.00 1487.92 1546.46 1520.04
## [65] 1601.68 1471.60 1484.98 1613.70 1534.80 1523.34 1715.28 1469.64
## [73] 1507.42 1397.80 1508.98 1363.38 1447.26 1572.74 1657.60 1506.38
## [81] 1498.74 1530.96 1498.74 1544.86 1612.40 1408.46 1605.10 1437.98
## [89] 1521.50 1367.34 1430.00 1515.80 1442.72 1420.96 1538.24 1387.92
## [97] 1581.46 1520.42 1406.30 1501.58
There are 100 elements in sample_means_small. Each element represents the mean of one n=50 sample of area.
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.
As the sample size gets larger, the center remains roughly the same, while the spread decreases.
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. Using this sample, what is your best point estimate of the population mean?mean(sample(price,50))## [1] 185966.8
Based on this, the mean price for homes in this area is estimated at around $185,967.
sample_means50. Plot the data, then describe the shape of this sampling distribution. Based on this sampling distribution, what would you guess the mean home price of the population to be? Finally, calculate and report the population mean.sample_means50_price <- rep(NA,times=5000)
for(i in 1:5000)
{
sample_means50_price[i] <- mean(sample(price,50))
}
hist(sample_means50_price)mean(sample_means50_price)## [1] 180647.9
mean(price)## [1] 180796.1
Based on this sampling distribution, I would guess the mean home price of the population to be around $180,648. This is very close to the actual population mean of $180,796.
sample_means150. Describe the shape of this sampling distribution, and compare it to the sampling distribution for a sample size of 50. Based on this sampling distribution, what would you guess to be the mean sale price of homes in Ames?sample_means150_price <- rep(NA,times=5000)
for(i in 1:5000)
{
sample_means150_price[i] <- mean(sample(price,150))
}
mean(sample_means150_price)## [1] 180696.3
Based on this sampling distribution, I would guess the mean home price of the population to be around $180,696.
The sampling distribution using a larger n (150 vs. 50) has a smaller spread. We would prefer a smaller spread (and thus a larger n) if we want our point estimate to be more often close to the true value.
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.