download.file("http://www.openintro.org/stat/data/ames.RData", destfile = "ames.RData")
load("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)
Describe the distribution of your sample. What would you say is the “typical” size within your sample? Also state precisely what you interpreted “typical” to mean.
The distribution is strongly skewed and has outliers. The typical size of the sample is between 1500 and 2000. The typical size in this scenario is the range of values were the mean should be found.
hist(samp)
Would you expect another student’s distribution to be identical to yours? Would you expect it to be similar? Why or why not?
Not identical but similar, beacause the distribution is strongly skewed with some outliers that will affect the similarities between different samples.
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)
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] 1412.439 1707.195
For the confidence interval to be valid, the sample mean must be normally distributed and have standard error s/√n. What conditions must be met for this to be true?
Random sampling, the sample observations are independent, the sample size is larger than 30 or if it is lower than 30 then it should not have any strong skewing or outliers.
What does “95% confidence” mean? If you’re not sure, see Section 4.2.2.
95% of intervals from differents samples will include the true population mean.
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?
No, it does not include the true population mean.
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.
Using a 95% confidence interval, we would expect that .95 proportions of the intervals would capture the true population mean.
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] 1412.479 1653.587
plot_ci(lower_vector, upper_vector, mean(population))
Answer:
47/50 = 0.94 proportions of the confidence intervals include the true population mean.This porportion is not exactly equal to the confidence interval, because the sample size is not large enough to compensate for the strong skewed distribtuion.
Answer:
For a 90% confidence level, the appropiate critical value is 1.64.
lower_vector <- samp_mean - 1.64 * samp_sd / sqrt(n)
upper_vector <- samp_mean + 1.64 * samp_sd / sqrt(n)
c(lower_vector[1], upper_vector[1])
## [1] 1432.162 1633.905
plot_ci(lower_vector, upper_vector, mean(population))
Answer:
46/50 = 0.92 proportions of the confidence intervals include the true population mean. We obtained a smaller proportion of confidence intervals that caputure the true population mean from .94 to .92, because we lowered the confidence interval from 95% to 90%.