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.
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.
Let’s look at the distribution of area in our population of home sales by calculating a few summary statistics and making a histogram.
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 334 1126 1442 1500 1743 5642
ANSWER: The variable area is unimodal with median or most common living area at 1442 sq feet. The mean of the variable is 1500 sq foot area. There are outliers and the max sq foot being 5642 and minimum at 334 . The variable has a right skew.
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.
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.
ANSWER: The sample is also unimodal and right skewed. The median is 1448 sq ft and mean is 1536. The max value is 3005.
```r
summary(samp1)
```
```
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 616 1192 1448 1536 1716 3005
```
```r
hist(samp1)
```
<img src="Banu-sampling_distributions_New_files/figure-html/box2-1.png" width="672" />
```r
boxplot(samp1)
```
<img src="Banu-sampling_distributions_New_files/figure-html/box2-2.png" width="672" />
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.
## [1] 1536.44
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.
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?
ANSWER: Mean of sample samp2 is 1641.12 and mean of samp1 is 1536.44. The samp4 has a mean of 1496.21 which is closer to the population mean of variable ‘area’ which is 1499.69.
## [1] 1499.69
## [1] 1536.44
## [1] 1641.12
## [1] 1516.37
## [1] 1496.219
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)## num [1:5000] 1570 1464 1366 1462 1409 ...
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1274 1451 1500 1501 1549 1781
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.
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?ANSWER:
Elements in sample_means50 : 5000 since we are generating 5000 samples and computing the sample mean of each and storing it in sample_means50 Sampling Distribution: Center of the sample_means50 seems to be closer to the population mean of area variable. The histogram seems to look more like a normal distribution with median value being closer to the mean and more symmetric with a bell shaped curve. With the 50,000 sample means the distribution will follow the normal distribution similar to at 5000 sample means so the distribution doesn’t change but continue to follow normal distribution.
sample_means50a <- rep(NA, 50000)
for(i in 1:50000){
samp <- sample(area, 50)
sample_means50a[i] <- mean(samp)
}
hist(sample_means50a,breaks = 25)## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1238 1451 1498 1500 1546 1815
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_means50b <- rep(NA, 5000)
samp <- sample(area, 50)
sample_means50b[1] <- mean(samp)
samp <- sample(area, 50)
sample_means50b[2] <- mean(samp)
samp <- sample(area, 50)
sample_means50b[3] <- mean(samp)
samp <- sample(area, 50)
sample_means50b[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.
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?ANSWER: Elements in object sample_means_small is 100. We take a random sample of size 50 from area then save the mean of that sample into the sample_means_small vector that was pre-initialized to 100 0’S. We iterate through the loop 100 times or get sample size of 50 each time and then store that sample mean in the vector.
```r
sample_means_small <- rep(NA, 100)
for(i in 1:100){
samp <- sample(area, 50)
sample_means_small[i] <- mean(samp)
print(i)
}
```
```
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
## [1] 11
## [1] 12
## [1] 13
## [1] 14
## [1] 15
## [1] 16
## [1] 17
## [1] 18
## [1] 19
## [1] 20
## [1] 21
## [1] 22
## [1] 23
## [1] 24
## [1] 25
## [1] 26
## [1] 27
## [1] 28
## [1] 29
## [1] 30
## [1] 31
## [1] 32
## [1] 33
## [1] 34
## [1] 35
## [1] 36
## [1] 37
## [1] 38
## [1] 39
## [1] 40
## [1] 41
## [1] 42
## [1] 43
## [1] 44
## [1] 45
## [1] 46
## [1] 47
## [1] 48
## [1] 49
## [1] 50
## [1] 51
## [1] 52
## [1] 53
## [1] 54
## [1] 55
## [1] 56
## [1] 57
## [1] 58
## [1] 59
## [1] 60
## [1] 61
## [1] 62
## [1] 63
## [1] 64
## [1] 65
## [1] 66
## [1] 67
## [1] 68
## [1] 69
## [1] 70
## [1] 71
## [1] 72
## [1] 73
## [1] 74
## [1] 75
## [1] 76
## [1] 77
## [1] 78
## [1] 79
## [1] 80
## [1] 81
## [1] 82
## [1] 83
## [1] 84
## [1] 85
## [1] 86
## [1] 87
## [1] 88
## [1] 89
## [1] 90
## [1] 91
## [1] 92
## [1] 93
## [1] 94
## [1] 95
## [1] 96
## [1] 97
## [1] 98
## [1] 99
## [1] 100
```
```r
sample_means_small
```
```
## [1] 1569.16 1535.86 1527.30 1593.42 1418.18 1625.06 1345.48 1497.26 1618.44
## [10] 1684.78 1418.00 1475.08 1510.16 1540.54 1535.24 1559.54 1511.72 1519.52
## [19] 1616.50 1525.26 1512.36 1397.38 1413.22 1441.24 1545.84 1591.30 1633.44
## [28] 1450.62 1499.18 1485.20 1613.48 1555.86 1529.58 1455.96 1345.74 1611.50
## [37] 1457.22 1491.08 1418.44 1520.34 1492.32 1612.58 1480.30 1361.78 1469.02
## [46] 1545.40 1501.24 1517.90 1489.80 1322.80 1446.40 1352.24 1489.68 1473.94
## [55] 1426.72 1506.18 1516.00 1471.40 1424.62 1555.46 1702.56 1507.66 1552.88
## [64] 1407.82 1501.72 1375.52 1389.66 1474.02 1408.48 1656.50 1399.02 1518.30
## [73] 1561.82 1527.62 1569.62 1450.72 1498.14 1664.90 1469.06 1475.60 1554.52
## [82] 1553.22 1519.04 1421.72 1487.52 1597.52 1445.38 1541.50 1469.30 1526.86
## [91] 1541.86 1473.28 1471.04 1506.10 1464.34 1438.86 1452.16 1583.72 1579.20
## [100] 1555.88
```
```r
str(sample_means_small)
```
```
## num [1:100] 1569 1536 1527 1593 1418 ...
```
Mechanics aside, let’s return to the reason we used a for loop: to compute a sampling distribution, specifically, this one.
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] 1502.72
## [1] 160.6787
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1028 1389 1494 1503 1605 2316
## [1] 1500.806
## [1] 72.06536
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1274 1451 1500 1501 1549 1781
## [1] 1498.471
## [1] 49.22387
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1312 1464 1497 1498 1530 1686
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.
ANSWER: When the sample size is larger, the center aligns to the true mean of the population. The spread is the least widest and becomes symmetric bell curve with more values with median and mean close to each other.
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.
Take a random sample of size 50 from price. Using this sample, what is your best point estimate of the population mean?
ANSWER: Best point estimate is 173831 below calculating the mean of the sample.
## [1] 173831.1
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 58500 132375 157052 173831 212975 445000
Since you have access to the population, simulate the sampling distribution for \(\bar{x}_{price}\) by taking 5000 samples from the population of size 50 and computing 5000 sample means. Store these means in a vector called 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.
ANSWER: Shape of the distribution sampling is bell shaped and unimodal. Based on sampling distribution the mean home price of popultion is 181,160 and the standard error is 1582.272. The population mean is 180796.1.
## [1] 180796.1
sample_prmeans50 <- rep(NA, 5000)
sample_prmeans150 <- rep(NA, 5000)
set.seed(400)
for(i in 1:5000){
samp <- sample(price, 50)
sample_prmeans50[i] <- mean(samp)
samp <- sample(price, 150)
sample_prmeans150[i] <- mean(samp)
}
hist(sample_prmeans50, breaks = 50)## num [1:5000] 178771 169031 178724 190801 187531 ...
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 146047 173455 180870 181160 188481 231453
## [1] 1582.272
Change your sample size from 50 to 150, then compute the sampling distribution using the same method as above, and store these means in a new vector called 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?
ANSWER: The sampling distribution for 150 size seems to be more of a normal distribution with unimodal and symmetric. The sampling distribution with n=50 has mean higher than median and distribution with size = 150 has also mean is greater than median, however, on sample size 50, the median is further away from the mean. The variability or standard error of sample_prmeans150(516.3029) is lesser than the standard error of sample_prmeans50(1582.272). The variability indicates spread of the data and indicates that error is larger when sample size is smaller. The mean home price based on this sample is 180665.3
## num [1:5000] 181203 173546 184373 174527 187440 ...
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 160099 176237 180513 180665 184829 204294
## [1] 6323.393
## [1] 11188.35
## [1] 516.3029
## [1] 1582.272
## [1] 180796.1
## [1] 181160
## [1] 180665.3
ANSWER: spread or standard error should be smallerif we want to make estimates closer to the true value of the population parameter. Of the n=50 and n=150, the n-150 sample has a smaller standard error and thus smaller spread.