If you have access to data on an entire population, say the size of every house in Ames, Iowa, it’s straight forward to answer questions like, “How big is the typical house in Ames?” and “How much variation is there in sizes of houses?”. If you have access to only a sample of the population, as is often the case, the task becomes more complicated. What is your best guess for the typical size if you only know the sizes of several dozen houses? This sort of situation requires that you use your sample to make inference on what your population looks like.
In the previous lab, ``Sampling Distributions’’, we looked at the population data of houses from Ames, Iowa. Let’s start by loading that data set.
load("more/ames.RData")
In this lab we’ll start with a simple random sample of size 60 from the population. Specifically, this is a simple random sample of size 60. Note that the data set has information on many housing variables, but for the first portion of the lab we’ll focus on the size of the house, represented by the variable Gr.Liv.Area
.
population <- ames$Gr.Liv.Area
samp <- sample(population, 60)
hist(samp)
mean(samp)
## [1] 1465.383
median(samp)
## [1] 1486
sd(samp)
## [1] 411.1396
getmode <- function(v) {
uniqv <- unique(v)
uniqv[which.max(tabulate(match(v, uniqv)))]
}
getmode(samp)
## [1] 1092
No another student’s distribution may not be exactly identical to mine because the sample that they could of pulled would be most likely a different subset of the data as a whole
One of the most common ways to describe the typical or central value of a distribution is to use the mean. In this case we can calculate the mean of the sample using,
sample_mean <- mean(samp)
Return for a moment to the question that first motivated this lab: based on this sample, what can we infer about the population? Based only on this single sample, the best estimate of the average living area of houses sold in Ames would be the sample mean, usually denoted as \(\bar{x}\) (here we’re calling it sample_mean
). That serves as a good point estimate but it would be useful to also communicate how uncertain we are of that estimate. This can be captured by using a confidence interval.
We can calculate a 95% confidence interval for a sample mean by adding and subtracting 1.96 standard errors to the point estimate (See Section 4.2.3 if you are unfamiliar with this formula).
se <- sd(samp) / sqrt(60)
lower <- sample_mean - 1.96 * se
upper <- sample_mean + 1.96 * se
c(lower, upper)
## [1] 1361.351 1569.416
This is an important inference that we’ve just made: even though we don’t know what the full population looks like, we’re 95% confident that the true average size of houses in Ames lies between the values lower and upper. There are a few conditions that must be met for this interval to be valid.
In this case we have the luxury of knowing the true population mean since we have data on the entire population. This value can be calculated using the following command:
mean(population)
## [1] 1499.69
Does your confidence interval capture the true average size of houses in Ames? If you are working on this lab in a classroom, does your neighbor’s interval capture this value? #5 My confindence interval of 1374 to 1617 does capture the true average size of houses in ames
Each student in your class should have gotten a slightly different confidence interval. What proportion of those intervals would you expect to capture the true population mean? Why? If you are working in this lab in a classroom, collect data on the intervals created by other students in the class and calculate the proportion of intervals that capture the true population mean. #6 I would expect that about 95% of the time the student’s interval captures the actual mean of the population because it is a 95% confidence interval
for(i in 1:50){
samp <- sample(population, 60)
sample_mean <- mean(samp)
se <- sd(samp) / sqrt(60)
lower <- sample_mean - 1.96 * se
upper <- sample_mean + 1.96 * se
range <- c(lower, upper)
print(range)
true_or_false <- mean(population) > lower & mean(population) < upper
print(true_or_false)
}
## [1] 1309.046 1534.087
## [1] TRUE
## [1] 1504.916 1717.984
## [1] FALSE
## [1] 1435.987 1694.947
## [1] TRUE
## [1] 1410.975 1842.159
## [1] TRUE
## [1] 1505.148 1775.219
## [1] FALSE
## [1] 1404.156 1657.177
## [1] TRUE
## [1] 1233.751 1577.049
## [1] TRUE
## [1] 1298.594 1562.173
## [1] TRUE
## [1] 1417.841 1634.626
## [1] TRUE
## [1] 1441.366 1659.767
## [1] TRUE
## [1] 1340.490 1572.976
## [1] TRUE
## [1] 1417.61 1678.29
## [1] TRUE
## [1] 1357.760 1598.473
## [1] TRUE
## [1] 1314.599 1566.368
## [1] TRUE
## [1] 1357.029 1639.238
## [1] TRUE
## [1] 1456.370 1736.463
## [1] TRUE
## [1] 1371.744 1625.390
## [1] TRUE
## [1] 1350.863 1586.271
## [1] TRUE
## [1] 1398.853 1651.447
## [1] TRUE
## [1] 1383.175 1632.525
## [1] TRUE
## [1] 1384.938 1637.995
## [1] TRUE
## [1] 1327.738 1601.695
## [1] TRUE
## [1] 1303.196 1572.937
## [1] TRUE
## [1] 1354.314 1578.919
## [1] TRUE
## [1] 1407.881 1628.586
## [1] TRUE
## [1] 1332.844 1572.089
## [1] TRUE
## [1] 1460.513 1706.354
## [1] TRUE
## [1] 1442.412 1709.888
## [1] TRUE
## [1] 1433.798 1676.336
## [1] TRUE
## [1] 1413.659 1664.508
## [1] TRUE
## [1] 1352.538 1579.595
## [1] TRUE
## [1] 1315.588 1601.078
## [1] TRUE
## [1] 1369.979 1589.121
## [1] TRUE
## [1] 1387.404 1662.796
## [1] TRUE
## [1] 1320.896 1625.237
## [1] TRUE
## [1] 1372.992 1588.775
## [1] TRUE
## [1] 1375.188 1656.312
## [1] TRUE
## [1] 1334.762 1554.372
## [1] TRUE
## [1] 1341.690 1691.677
## [1] TRUE
## [1] 1479.314 1733.152
## [1] TRUE
## [1] 1409.544 1635.556
## [1] TRUE
## [1] 1352.260 1716.173
## [1] TRUE
## [1] 1423.523 1685.144
## [1] TRUE
## [1] 1446.068 1707.399
## [1] TRUE
## [1] 1227.766 1445.734
## [1] FALSE
## [1] 1318.275 1531.659
## [1] TRUE
## [1] 1393.529 1610.004
## [1] TRUE
## [1] 1360.746 1639.554
## [1] TRUE
## [1] 1374.602 1630.332
## [1] TRUE
## [1] 1308.240 1585.427
## [1] TRUE
Using R, we’re going to recreate many samples to learn more about how sample means and confidence intervals vary from one sample to another. Loops come in handy here (If you are unfamiliar with loops, review the Sampling Distribution Lab).
Here is the rough outline:
But before we do all of this, we need to first create empty vectors where we can save the means and standard deviations that will be calculated from each sample. And while we’re at it, let’s also store the desired sample size as n
.
samp_mean <- rep(NA, 50)
samp_sd <- rep(NA, 50)
n <- 60
Now we’re ready for the loop where we calculate the means and standard deviations of 50 random samples.
for(i in 1:50){
samp <- sample(population, n) # obtain a sample of size n = 60 from the population
samp_mean[i] <- mean(samp) # save sample mean in ith element of samp_mean
samp_sd[i] <- sd(samp) # save sample sd in ith element of samp_sd
}
Lastly, we construct the confidence intervals.
lower_vector <- samp_mean - 1.96 * samp_sd / sqrt(n)
upper_vector <- samp_mean + 1.96 * samp_sd / sqrt(n)
Lower bounds of these 50 confidence intervals are stored in lower_vector
, and the upper bounds are in upper_vector
. Let’s view the first interval.
c(lower_vector[1], upper_vector[1])
## [1] 1352.324 1648.509
THe proporation of the time that the actual average falls within the confidence interval may be below or above 95% because there can be a lot of variance since I am only looking at 50 confidence intervals
trues <- 0
for(i in 1:50){
samp <- sample(population, 50)
sample_mean <- mean(samp)
se <- sd(samp) / sqrt(60)
lower <- sample_mean - 1.96 * se
upper <- sample_mean + 1.96 * se
range <- c(lower, upper)
print(range)
true_or_false <- mean(population) > lower & mean(population) < upper
print(true_or_false)
trues <- ifelse(true_or_false == "TRUE", trues <- trues +1, trues <- trues +0)
print(trues)
}
## [1] 1301.502 1499.698
## [1] TRUE
## [1] 1
## [1] 1292.516 1572.804
## [1] TRUE
## [1] 2
## [1] 1353.09 1680.31
## [1] TRUE
## [1] 3
## [1] 1313.817 1530.103
## [1] TRUE
## [1] 4
## [1] 1392.859 1630.621
## [1] TRUE
## [1] 5
## [1] 1399.659 1619.101
## [1] TRUE
## [1] 6
## [1] 1456.743 1688.177
## [1] TRUE
## [1] 7
## [1] 1405.753 1789.287
## [1] TRUE
## [1] 8
## [1] 1350.296 1583.304
## [1] TRUE
## [1] 9
## [1] 1289.323 1528.037
## [1] TRUE
## [1] 10
## [1] 1382.315 1648.805
## [1] TRUE
## [1] 11
## [1] 1444.279 1715.241
## [1] TRUE
## [1] 12
## [1] 1503.628 1846.812
## [1] FALSE
## [1] 12
## [1] 1318.986 1581.014
## [1] TRUE
## [1] 13
## [1] 1296.849 1481.431
## [1] FALSE
## [1] 13
## [1] 1476.449 1748.991
## [1] TRUE
## [1] 14
## [1] 1372.633 1618.687
## [1] TRUE
## [1] 15
## [1] 1485.036 1776.324
## [1] TRUE
## [1] 16
## [1] 1352.776 1560.704
## [1] TRUE
## [1] 17
## [1] 1368.634 1617.046
## [1] TRUE
## [1] 18
## [1] 1306.99 1636.85
## [1] TRUE
## [1] 19
## [1] 1419.237 1665.963
## [1] TRUE
## [1] 20
## [1] 1342.513 1579.487
## [1] TRUE
## [1] 21
## [1] 1371.387 1584.373
## [1] TRUE
## [1] 22
## [1] 1368.986 1592.134
## [1] TRUE
## [1] 23
## [1] 1339.179 1620.341
## [1] TRUE
## [1] 24
## [1] 1395.30 1640.94
## [1] TRUE
## [1] 25
## [1] 1313.071 1684.369
## [1] TRUE
## [1] 26
## [1] 1482.632 1749.008
## [1] TRUE
## [1] 27
## [1] 1402.11 1669.97
## [1] TRUE
## [1] 28
## [1] 1413.53 1695.47
## [1] TRUE
## [1] 29
## [1] 1342.225 1619.295
## [1] TRUE
## [1] 30
## [1] 1384.061 1626.099
## [1] TRUE
## [1] 31
## [1] 1362.226 1588.134
## [1] TRUE
## [1] 32
## [1] 1331.344 1598.816
## [1] TRUE
## [1] 33
## [1] 1222.924 1427.716
## [1] FALSE
## [1] 33
## [1] 1359.951 1635.529
## [1] TRUE
## [1] 34
## [1] 1394.877 1649.563
## [1] TRUE
## [1] 35
## [1] 1319.559 1588.801
## [1] TRUE
## [1] 36
## [1] 1353.867 1601.933
## [1] TRUE
## [1] 37
## [1] 1466.121 1708.399
## [1] TRUE
## [1] 38
## [1] 1417.857 1740.343
## [1] TRUE
## [1] 39
## [1] 1406.205 1634.595
## [1] TRUE
## [1] 40
## [1] 1426.445 1699.955
## [1] TRUE
## [1] 41
## [1] 1501.981 1761.299
## [1] FALSE
## [1] 41
## [1] 1232.506 1492.174
## [1] FALSE
## [1] 41
## [1] 1263.221 1459.979
## [1] FALSE
## [1] 41
## [1] 1388.854 1633.386
## [1] TRUE
## [1] 42
## [1] 1382.841 1665.399
## [1] TRUE
## [1] 43
## [1] 1408.491 1644.189
## [1] TRUE
## [1] 44
ratio <- trues/50
print(ratio)
## [1] 0.88
```r
plot_ci(lower_vector, upper_vector, mean(population))
```
<img src="qqz0117-confidence_intervals_files/figure-html/plot-ci-1.png" width="672" />
Pick a confidence level of your choosing, provided it is not 95%. What is the appropriate critical value?
If I chose a confidence interval of 99% then it’s critical value or z value would be 2.58
Calculate 50 confidence intervals at the confidence level you chose in the previous question. You do not need to obtain new samples, simply calculate new intervals based on the sample means and standard deviations you have already collected. Using the plot_ci
function, plot all intervals and calculate the proportion of intervals that include the true population mean. How does this percentage compare to the confidence level selected for the intervals?
Only one of the intervals did not contain the actual mean of the population so roughly 98% of the intervals did contain the population mean
lower_vector <- samp_mean - 2.58 * samp_sd / sqrt(n)
upper_vector <- samp_mean + 2.58 * samp_sd / sqrt(n)
plot_ci(lower_vector, upper_vector, mean(population))
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.