download.file("http://www.openintro.org/stat/data/ames.RData", destfile = "ames.RData")
load("ames.RData")
area <- ames$Gr.Liv.Area
price <- ames$SalePrice
summary(area)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     334    1126    1442    1500    1743    5642
hist(area)

Ex 1, Describe this population distribution.

Answer: The population distribution is unimodal with a right skew, mean 1500 and range 334-5642 and an IQR of 617.

Ex 2, Describe the distribution of this sample. How does it compare to the distribution of the population?

samp1 <- sample(area, 50)
mean(samp1)
## [1] 1546.8

Ex 3, 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?

samp2 <- sample(area, 50)
mean(samp2)
## [1] 1418.66
sample_means50 <- rep(NA, 5000)

for(i in 1:5000){
   samp <- sample(area, 50)
   sample_means50[i] <- mean(samp)
   }

hist(sample_means50)

Answer: The mean of samp2 differs slightly from the mean of samp1. If we were to take two more samples of size 100 and 1000, the sample of size 1000 would provide the more accurate estimate of population mean

Ex 4, 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?

Answer: There are 5000 elements in the vector sample_means50. The sampling distribution is symmetric, with a center at approximately 1500. If we collected 50,000 sample means, I would expect the curve to become more normal and with its center even closer to 1500

Ex 5, 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?

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] 1452.96 1555.32 1524.94 1504.70 1537.68 1287.74 1398.84 1465.08
##   [9] 1559.52 1557.24 1494.10 1545.84 1626.80 1407.70 1604.96 1368.14
##  [17] 1550.08 1577.24 1462.02 1497.10 1502.42 1421.50 1527.46 1598.68
##  [25] 1542.82 1482.86 1543.38 1659.58 1591.94 1327.44 1357.00 1555.46
##  [33] 1542.08 1615.08 1492.96 1530.86 1445.44 1562.42 1473.34 1542.26
##  [41] 1427.18 1557.34 1515.26 1464.12 1579.88 1524.72 1484.28 1565.00
##  [49] 1421.74 1419.18 1433.68 1393.30 1454.38 1533.98 1545.46 1640.56
##  [57] 1517.94 1596.32 1537.78 1547.24 1611.84 1407.52 1494.12 1496.70
##  [65] 1504.08 1624.18 1403.62 1331.08 1583.80 1594.84 1510.34 1426.68
##  [73] 1425.94 1422.26 1446.94 1520.28 1421.84 1570.00 1453.00 1449.06
##  [81] 1523.52 1556.12 1465.36 1456.08 1471.66 1434.98 1469.02 1423.44
##  [89] 1479.84 1485.86 1593.08 1605.08 1490.52 1592.04 1547.80 1483.14
##  [97] 1473.46 1483.38 1545.56 1476.28

Ex 6, When the sample size is larger, what happens to the center? What about the spread?

Answer: Center even closer to population mean. Spread narrows.

On Your Own

1, Take a random sample of size 50 from price. Using this sample, what is your best point estimate of the population mean?

samp1 <- sample(price, 50)
mean(samp1)
## [1] 162118.4

2, Since you have access to the population, simulate the sampling distribution for 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.

sample_means50 <- rep(NA, 5000)

for(i in 1:5000){
   samp <- sample(price, 50)
   sample_means50[i] <- mean(samp)
   }

hist(sample_means50)

summary(sample_means50)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  147160  173381  180581  181037  188033  227694
mean(sample_means50)
## [1] 181036.7

3, 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?

sample_means150 <- rep(NA, 5000)

for(i in 1:5000){
  samp <- sample(price, 150)
  sample_means150[i] <- mean(samp)
}
hist(sample_means150)

summary(sample_means150)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  151808  176493  180700  180850  185055  203767
mean(sample_means150)
## [1] 180849.5

4, Of the sampling distributions from 2 and 3, which has a smaller spread? If we’re concerned with making estimates that are more often close to the true value, would we prefer a distribution with a large or small spread?

Answer: Large sample size has smaller spread. Smaller spread is more representative of population parameters