library(tidyverse)
library(openintro)
library(BSDA)
#Data load
download.file("http://www.openintro.org/stat/data/ames.RData", destfile = "ames.RData")
load("ames.RData")
#this contains a sample size of n=60 from the population; size of the house<-Gr.Liv.Area
population <- ames$Gr.Liv.Area
samp <- sample(population, 60)
Q. 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.
1.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.
Q. Based on this sample, what can we infer about the population?
2.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 x¯(here we’re calling it sample_mean).
A. Essentially the summary of the sample size that tells us the shape and skew of the sample size.
summary(samp)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 715 1171 1361 1444 1653 2654
sample_mean <- mean(samp)
Q.Would you expect another student’s distribution to be identical to yours? Would you expect it to be similar? Why or why not?
A.Although somewhat similar the distribution from another sample will not be identical.Although sampling varies the population pooled is the same and as sample shape is a good infrence to the population they may have similar outcomes but not the same.
We can calculate a 95% confidence interval for a sample mean by adding and subtracting 1.96 standard errors to the point estimate .
# 95% sample mean =1.96 se
se <- sd(samp) / sqrt(60)
lower <- sample_mean - 1.96 * se
upper <- sample_mean + 1.96 * se
c(lower, upper)
## [1] 1334.297 1553.803
Q.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?
2.Independent observations must take place - n> population is subject to interdependance in outcome
Q.What does “95% confidence” mean?
I interpret it as 95% confidence that the intervals (upper and lower) of the sample capture the mean (overall essence) of the true population.
#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:
mepop<-mean(population)
#how close were we?
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?
A. Yes the population mean of is in between the intervals of the upper and lower intervals. if working on the same population , yes it should capture the same value for the true population mean but differnce confidence intervals.
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 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:
A. Obtain a random sample.
B. Calculate and store the sample’s mean and standard deviation.
C. Repeat steps (1) and (2) 50 times.
D.Use these stored statistics to calculate many confidence intervals.
# reate 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] 1403.315 1638.152
####Questions(On your own)
###Exercise A
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))
(plot_ci(lower_vector, upper_vector, mean(population))
)
## NULL
A. The 50-highlighted over the 50 is within confidence interval include the true population mean. It is not exactly equal to the confidence interval but should be close.
###Exercise B
Pick a confidence level of your choosing, provided it is not 95%. What is the appropriate critical value?
crit<-qnorm(.995)
c(crit)
## [1] 2.575829
###Exercise B
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.
se <- sd(samp) / sqrt(60)
lower <- sample_mean - 2.58* se
upper <- sample_mean + 2.58* se
c(lower, upper)
## [1] 1167.097 1721.003
2.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.
plot_ci(lower_vector, upper_vector, mean(population))
(plot_ci(lower_vector, upper_vector, mean(population))
)
## NULL