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
summary(population)## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 334 1126 1442 1500 1743 5642
samp <- sample(population, 60)
hist(samp)Answer :- It is symmetrical and unimodal. A typical size would be 1650 , typical means the average size of the house in the sample would be 1650.
Answer :- Yes, other students distribution can be identical. Yes it can be similar as the sample is random and less than 10% of the overall population so there can chances where there can similarity between to samples of other students sample.
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)
sample_mean## [1] 1421.45
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)
se## [1] 58.07417
lower <- sample_mean - 1.96 * se
upper <- sample_mean + 1.96 * se
c(lower, upper)## [1] 1307.625 1535.275
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.
Answer :- Standard error should not be more than 5% . Sample size should be < 10% of the total population.
Answer :- 95% confidence means , that out of 100 times 95 times the mean of the actual population will fall within this range.
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
Answer :- As per the interval we calculated the range is (1465.741 , 1718.359) so the mean of the population is 1499.69 which falls well within the range.It should if he/she applies the 95% formula correctly, but saying this as each sample will have different mean and the range can vary for each students sample and if there are not too many outliers then nearly everyon’s sample should be close by.
Answer :- 95% should capture the ture population mean.Lets compare each student to 32 iterations of the different sample so over the time these samples should have common mean with 5% variance. As I don’t work in a classroom , but I can assume that there are 32 students and if I create run 32 iterations with random sample each time which is < 10% of the total population then ideally it should give me the same results.
#n=60
sample_mean80 <- rep(NA,80)
sample_sd80 <- rep(NA,80)
for(i in 1:32){
sampl <- sample(population, 60)
sample_mean80[i] <- mean(sampl)
sample_sd80[i] <- sd(sampl)
}
sampMean <- mean(sample_mean80)
sampSD <- sd(sample_sd80)
sampMean## [1] NA
sampSD## [1] NA
Se <- 83.34/sqrt(60)
upper_limit <- 1503 + 1.96*Se
lower_limit <- 1503-1.96*Se
#range shld be (1481.912, 1524.088)
c(lower_limit[1],upper_limit[1])## [1] 1481.912 1524.088
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 <- 60Now 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] 1312.465 1536.535
Using the following function (which was downloaded with the data set), plot all intervals. What proportion of your confidence intervals include the true population mean? Is this proportion exactly equal to the confidence level? If not, explain why.
plot_ci(lower_vector, upper_vector, mean(population))Answer :- 1-(5/60)= 91.67% 1-(5/50) = .90 = 90%
The proportion is 5% less than the 95% confidence interval in one case(n=50) and 3.33% less in other case(where n=60), but confidence interval is a range and which states that 95% times population mean should be within this range.
Answer :- 90% , 1.65 is the critical value.
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?Answer :- 1-(9/50) = .82 = 82% which is less than the range selected.
lower_limit <- samp_mean - 1.65 * samp_sd/sqrt(n)
upper_limit <- samp_mean + 1.65 * samp_sd/sqrt(n)
c(lower_limit[1], upper_limit[1])## [1] 1330.185 1518.815
plot_ci(lower_limit, upper_limit, mean(population)) 1-(9/50)= 82%
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.