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.

(Alexander Ng Response)
The population distribution is right skewed with a mean of 1500, median of 1442.
House square footage are right skewed because a small number of people own very large homes. Sizes cannot dip below a threshold of 334 square feet because that house would be too small to live in.

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.

** FIX THE SEED FOR NEXT USAGE **

set.seed(13)
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?

(Alexander Ng Response)

summary(samp1)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     715    1050    1292    1385    1621    3395
hist(samp1)

The samp1 histogram is close to the one population one. We can see that the mean, median and IQR of samp1 are consistent to population statistics. The histogram shows the same right skew property.

The summary statistics of sampl1 are quite similar to population statistics of ames.Rdata

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

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?

(Alexander Ng’s Response)

The distribution of samp2 is slightly different from samp1.

The mean of samp1 is close to that of samp2.
6.84 square feet difference is the diff in means.

Both are somewhat far from the true population mean of 1500.

samp2 is -108.2 sq ft away from population mean.

set.seed(16)
samp2 = sample( area, 50 )

summary(samp2)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     599    1078    1388    1391    1591    2554
hist(samp2)

( diff_in_mean = mean(samp2) - mean(samp1) )
## [1] 6.84
mean(samp2) - mean(area)
## [1] -108.2304

For a sample size of 100, the difference in mean from the true population mean is -39.7.

set.seed(19)

samp3 = sample( area, 100)

summary(samp3)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     725    1072    1326    1460    1650    3608
mean(samp3) - mean(area)
## [1] -39.70044

Lastly, for a sample of size 1000, the difference from the true mean is evening smaller, namely, 15.39.

set.seed(21)
samp4 = sample( area, 1000)

summary(samp4)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     334    1150    1456    1515    1764    5642
mean(samp4) - mean(area)
## [1] 15.39356

It is plain to see that the samp2 is farthest from the population mean compared to samp3 and samp4. We expect samp4 to have the smallest difference because of the central limit theorem.

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?

(Alexander Ng response) sample_means50 has length 5000. Sampling distribution has mean = 1500 which equals the population mean.

If we collected 50,000 sample means, the sampling distribution mean would still be 1500 and the standard deviation would remain the same. This is because the standard error is a function of the population standard deviation and the sample size (50).

summary( sample_means50)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    1247    1451    1497    1500    1548    1775
length(sample_means50)
## [1] 5000
sd(sample_means50)
## [1] 71.10492

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:

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.

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.

  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?

(Alexander Ng’s RESPONSE) In the code below, we construct a sampling distribution of 100 repetitions of a mean of size 50 and then print all the elements

We can see the sample_means_small vector displayed below the for loop. There are 100 elements. They represent the hypothetical sample means of each samples.

sample_means_small = rep(NA, 100)

for( k in 1:100)
{
    sample_means_small[k] = mean( sample( area, 50 ))
}
sample_means_small
##   [1] 1497.10 1430.90 1523.82 1440.80 1504.76 1484.62 1471.38 1544.64
##   [9] 1584.90 1493.28 1582.28 1452.80 1395.64 1524.94 1478.52 1397.76
##  [17] 1498.76 1563.76 1583.96 1513.52 1460.28 1549.70 1516.92 1526.02
##  [25] 1562.32 1436.24 1472.28 1432.94 1613.58 1547.36 1604.58 1480.08
##  [33] 1505.38 1631.18 1516.34 1501.48 1463.50 1554.32 1328.96 1428.24
##  [41] 1621.34 1458.06 1529.64 1417.32 1750.44 1453.74 1500.58 1536.16
##  [49] 1506.70 1471.52 1512.46 1512.46 1568.36 1402.14 1581.02 1476.88
##  [57] 1510.00 1425.12 1573.56 1592.66 1508.40 1510.70 1520.72 1564.60
##  [65] 1443.82 1564.20 1510.40 1428.18 1535.82 1553.90 1472.70 1511.44
##  [73] 1536.86 1483.48 1577.40 1302.76 1352.76 1455.50 1490.30 1523.98
##  [81] 1487.84 1502.32 1514.44 1439.68 1496.02 1386.98 1512.68 1420.90
##  [89] 1584.96 1523.10 1484.58 1417.58 1636.64 1491.74 1514.48 1368.22
##  [97] 1454.30 1507.38 1477.48 1551.44

**

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?

(Alexander Ng’s Response) When the sample size is bigger, then the center is unchanged but the spread decreases accordingly to the law:

\[ SE = \frac{\sigma}{\sqrt{n} } \] where \(\sigma\) is the population standard deviation.


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.

(Alexander Ng’s RESPONSE to all 3 parts)

The best estimate of the population mean price from the random sample below is $175,566.90 for a house in Ames as shown below.

sample_means50_px = sample(price, 50)
mean(sample_means50_px)
## [1] 157579.5

The next code chunk estimates the sampling distribution of prices based on samples of size 50. The mean of the sampling distribution is 180863.50. The shape of the sampling distribution is normal. We guess that mean home price is 180863.50 on that basis.
Below we see the sampling distribution has standard deviation 11225.53.

sample_means50 = rep(NA, 5000)

for(j in 1:5000){
  
  sample_means50[j] = mean( sample( price, 50  ) )
}

hist(sample_means50 , breaks=20)

mean(sample_means50)
## [1] 180823.9
sd(sample_means50)
## [1] 11210.49

The actual population mean of housing price is 180796.10 as shown below.
The sampling distribution mean is very close to the population mean.

mean(price)
## [1] 180796.1

The next code chunk uses a sample size of 150 to produce a sampling distribution. The shape of this newer sampling distribution is also normal. The mean of the sampling distribution is 180656.50. We use this as the estimate of the population mean house price. The standard deviation is 6397.30. The shape of the distribution is normal.

The sampling distribution with sample size 50 has a larger spread (i.e. large standard deviation) than with the sample size 150.

sample_means150 = rep(NA, 5000)

for(j in 1:5000){
  
  sample_means150[j] = mean( sample( price, 150  ) )
}

hist(sample_means150 , breaks=20)

mean(sample_means150)
## [1] 180641.8
sd(sample_means150)
## [1] 6339.711

We know that the sampling distributions have a standard deviation equal to:

\[ SE_{150} = \frac{\sigma}{\sqrt{150}} = \frac{1}{\sqrt{3}} \frac{\sigma}{\sqrt{50}} = \frac{1}{\sqrt{3}}SE_{50} \] \[ \frac{SE_{150}}{SE_{50}} = \frac{1}{\sqrt{3}} \approx 0.577 \]

The ratio of standard errors of the two distributions should be around 0.577. This is validated empirically by the actual data.

sd(sample_means150) / sd( sample_means50)
## [1] 0.565516
1/sqrt(3)
## [1] 0.5773503

Of the two above distributions, distribution of part 3 has a smaller spread. We prefer a distribution with a narrower spread since it has a higher confidence of estimating the true population mean.