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.
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)The distribution is unimodal, slightly right skewed, but approximately normal and appears to be centered around 1400sqft or so.
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.
summary(samp1)## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 616 1080 1420 1464 1742 2944
hist(samp1)The sample distribution is a very close approximation to the population area, though it will be a different sample each time it’s sampled.
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] 1464
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?The more samples taken give a closer estimate to the population distribution.
samp2 <- sample(area, 50)
mean(samp2)## [1] 1522.34
samp100 <- sample(area, 100)
mean(samp100)## [1] 1513.77
samp1000 <- sample(area, 1000)
mean(samp1000)## [1] 1505.796
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.
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 in sample_means50. The distribution is centered just about 1500 and is approximately normal but slightly right skewed just like the population. The sample_means50000 distribution will not likely change much as this is already a close approximation to the population.
for loopsample_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(NA, 100)
for(i in 1:100){
samp <- sample(area, 50)
sample_means_small[i] <- mean(samp)
}
sample_means_small## [1] 1472.34 1430.72 1482.28 1558.92 1531.62 1434.32 1520.94 1613.66
## [9] 1395.24 1500.36 1600.78 1571.74 1398.70 1448.08 1495.58 1314.28
## [17] 1441.20 1389.00 1470.18 1493.78 1555.18 1514.42 1519.06 1428.16
## [25] 1463.22 1466.62 1453.58 1523.06 1479.08 1486.60 1465.62 1457.74
## [33] 1504.72 1575.76 1475.40 1644.80 1632.12 1420.52 1509.98 1576.88
## [41] 1301.60 1575.18 1462.30 1510.28 1456.50 1584.28 1591.36 1550.66
## [49] 1373.54 1611.64 1584.22 1584.54 1567.18 1571.60 1388.38 1465.78
## [57] 1471.52 1522.60 1612.84 1658.56 1560.88 1462.46 1511.46 1570.56
## [65] 1523.96 1432.10 1633.68 1478.14 1450.56 1460.14 1432.96 1602.58
## [73] 1476.84 1494.64 1383.62 1719.42 1548.14 1554.88 1530.74 1427.64
## [81] 1412.70 1435.80 1608.94 1530.12 1441.46 1533.18 1434.96 1585.58
## [89] 1393.30 1706.36 1414.38 1395.82 1589.00 1471.38 1384.42 1577.30
## [97] 1475.74 1639.90 1524.90 1455.80
There are 100 elements each representing the mean of the sample of 50 population data points.
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.
The distribution of the sample means gets closer to the mean of the populaion with the spread from variabililty getting smaller with the additional sample means being included.
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?samp_price <- sample(price, 50)
hist(samp_price)The population mean of the sample of price appears to be between $100,000 and $200,000.
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 <- rep(NA, 5000)
for(i in 1:5000){
samp <- sample(price, 50)
sample_means50[i] <- mean(samp)
}
hist(sample_means50)mean(price)## [1] 180796.1
The sample distribution mean is now between $180,000 and $185,000.
The actual mean is around $180,000.
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 <- rep(NA, 5000)
for(i in 1:5000){
samp <- sample(price, 150)
sample_means150[i] <- mean(samp)
}
hist(sample_means150, breaks = 10)The center of the sample distribution of 150 means is closer to the true mean of aproximately $180,000 and I would have guessed around $182,000 for the mean sale price in Ames, Iowa.
The sample distribution of 150 different means has a smaller spread. We would prefer a smaller spread as this would show that there is less variability from the mean.