Lab report

Load data:

download.file("http://www.openintro.org/stat/data/ames.RData", destfile = "ames.RData")
load("ames.RData")

Set a seed:

set.seed(1392)

Exercises:

Exercise 1:

population <- ames$Gr.Liv.Area
samp <- sample(population, 60)

hist(samp)

summary(samp)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     720    1051    1317    1418    1582    3608
mean(samp)
## [1] 1417.517

Since the data and the sample are both somewhat skewed to the right, it would be preferable to use the median rather than the mean to determine what a “typical” size within the sample is. If we follow through on this, the average size of a home in our sample comes in just under 1400 square feet.

The average home size in our sample would be somewhat less than 1500 square feet if we used the mean instead. #### Exercise 2:

sample_mean <- mean(samp)
se <- sd(samp) / sqrt(60)
lower <- sample_mean - 1.96 * se
upper <- sample_mean + 1.96 * se
c(lower, upper)
## [1] 1285.194 1549.840
z_star_95 <- qnorm(0.975)
z_star_95
## [1] 1.959964

We’ve just drawn a crucial conclusion: although the exact makeup of the population is unknown, we have a 95% confidence level that the actual average house size in Ames falls between the lower and upper ranges. For this interval to be legitimate, a few requirements need to be fulfilled.

Exercise 3:

Here, the usual presumptions are that n >= 30 and that the observations are independent. Furthermore, you might want n to be somewhat greater than 30 in order to have a fairly normal sampling distribution if the population distribution is not roughly normally distributed. The larger the n needed to obtain a sampling distribution that is roughly normal, the larger the skew.

Exercise 4:

The 95% confidence interval indicates the interval in which there is a 95% chance that the population mean will fall within it, assuming that the sampling distribution of the mean is normal. This is known as the 95% confidence level.

Put another way, we would expect the population mean to be within the interval in about 95% of the samples if you took a bunch of samples and their 95% confidence intervals.

Since we have data on the entire population, we are in the fortunate position of knowing the true population mean in this instance. The command below can be used to calculate this value:

mean(population)
## [1] 1499.69

Exercise 5:

Indeed, the population mean of 1500 falls between the 1375 and 1582 95% confidence intervals.

My neighbor’s interval most likely captures this value as well. The entire purpose of the 95% confidence interval, however, is to account for the possibility that, on occasion, a sample mean will be obtained that deviates significantly from the population mean in comparison to other samples. Therefore, there’s a chance that the population mean is not included in my neighbor’s interval.

Exercise 6:

true_population_mean <- mean(population)

confidence_interval_includes_population_mean <- rep(NA,times=1000)

for(i in 1:1000)
{
mysample <- sample(population,60)
sample_mean <- mean(mysample)
sample_sd <- sd(mysample)
lower <- sample_mean - 1.96 * (sample_sd/sqrt(60))
upper <- sample_mean + 1.96 * (sample_sd/sqrt(60))
confidence_interval_includes_population_mean[i] <- lower <= true_population_mean & upper >= true_population_mean
}

table(confidence_interval_includes_population_mean)
## confidence_interval_includes_population_mean
## FALSE  TRUE 
##    54   946
samp_mean <- rep(NA, 50)
samp_sd <- rep(NA, 50)
n <- 60

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
}

lower_vector <- samp_mean - 1.96 * samp_sd / sqrt(n) 
upper_vector <- samp_mean + 1.96 * samp_sd / sqrt(n)

c(lower_vector[1], upper_vector[1])
## [1] 1282.720 1452.747

We would expect that around 95% of those intervals would capture the true population mean.

When I simulated taking 1,000 samples and calculating confidence intervals, I discovered that, as I had anticipated, the confidence interval roughly 95% of the time accurately represented the true population mean. As a result, we got confirmation of what we expected.

I used a random sample to calculate and store the mean and standard deviation of the sample. I repeated this 50 times to calculate as many confidence intervals.

I used loops in R’Studio to calculate the means and standard deviations of 50 random samples. Last step is constructing the confidence intervals. 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.


On your own:

1:

i=1

plot(c(i,i),
c(lower_vector[i],upper_vector[i]),
type="l",
xlim=c(1,50),
ylim=range(c(lower_vector,upper_vector)),
main="Confidence intervals (50 samples)",
xlab="i",
ylab="Confidence interval range")

for(i in 2:50)
{
if(lower_vector[i] <= mean(population) & upper_vector[i] >= mean(population)){
    lines(c(i,i),
    c(lower_vector[i],upper_vector[i]),
    type="l")
    }
if(lower_vector[i] > mean(population) | upper_vector[i] < mean(population)){
    lines(c(i,i),
    c(lower_vector[i],upper_vector[i]),
    type="l",
    lwd=2,col="red")
    }   
}

abline(h=mean(population),lty=2)

It appears that 94%, or 47/50, of the confidence intervals contain the actual population mean. Although not precisely equal, this proportion is quite close to the confidence level. One reason is that since 95% of 50 is a non-integer number, the percent of 50 confidence intervals we can obtain cannot be precisely 95%. Therefore, 94% or 96% would be the closest we could go. Even in the absence of this, it is still possible that we will occasionally receive a higher number of samples than anticipated with unusual values. In this case, there would be a greater than 5% of samples with confidence intervals outside of which the population mean falls.

2:

-1*qnorm(.005)
## [1] 2.575829

For a confidence level of 99%, the appropriate critical value is 2.576. #### 3:

critical_value <- -1*qnorm(.005)
n=60
lower_vector <- samp_mean - critical_value * samp_sd / sqrt(n)
upper_vector <- samp_mean + critical_value * samp_sd / sqrt(n)

i=1

plot(c(i,i),
c(lower_vector[i],upper_vector[i]),
type="l",
xlim=c(1,50),
ylim=range(c(lower_vector,upper_vector)),
main="Confidence intervals (50 samples)",
xlab="i",
ylab="Confidence interval range")

for(i in 2:50)
{
if(lower_vector[i] <= mean(population) & upper_vector[i] >= mean(population)){
        lines(c(i,i),
        c(lower_vector[i],upper_vector[i]),
        type="l")
        }
if(lower_vector[i] > mean(population) | upper_vector[i] < mean(population)){
        lines(c(i,i),
        c(lower_vector[i],upper_vector[i]),
        type="l",
        lwd=2,col="red")
        }
}

abline(h=mean(population),lty=2)

We would anticipate that the true population mean would be included in 99% of intervals based on the confidence interval. Given that 99% of 50 is 49.5, this would imply that for 50 samples, we would anticipate that all samples, or at the very least 49/50, would have the true population mean included in their confidence interval.

It is therefore a little odd to see 2/50 intervals in this case where the population mean is outside the confidence intervals. This is probably what happened in this case because the whole point of sampling is that occasionally, by chance, you can sample more of the outlier values more frequently than usual.

This time, let’s attempt collecting 1,000 samples and see what transpires.

samp_mean <- rep(NA, 1000)
samp_sd <- rep(NA, 1000)
n <- 60

for(i in 1:1000){
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
}

lower_vector <- samp_mean - critical_value * samp_sd / sqrt(n)
upper_vector <- samp_mean + critical_value * samp_sd / sqrt(n)

i=1

plot(c(i,i),
c(lower_vector[i],upper_vector[i]),
type="l",
xlim=c(1,1000),
ylim=range(c(lower_vector,upper_vector)),
main="Confidence intervals (1000 samples)\nOnly plotted for every 10th interval + intervals excl. population mean",
xlab="i",
ylab="Confidence interval range")

for(i in 2:1000)
{
if(lower_vector[i] <= mean(population) & upper_vector[i] >= mean(population) & i %% 10 == 0){
        lines(c(i,i),
        c(lower_vector[i],upper_vector[i]),
        type="l")
        }
if(lower_vector[i] > mean(population) | upper_vector[i] < mean(population)){
        lines(c(i,i),
        c(lower_vector[i],upper_vector[i]),
        type="l",
        lwd=2,col="red")
    print(paste0("Sample ",i," of 1,000 has population mean outside of confidence interval"))
        }
}
## [1] "Sample 18 of 1,000 has population mean outside of confidence interval"
## [1] "Sample 32 of 1,000 has population mean outside of confidence interval"
## [1] "Sample 132 of 1,000 has population mean outside of confidence interval"
## [1] "Sample 148 of 1,000 has population mean outside of confidence interval"
## [1] "Sample 187 of 1,000 has population mean outside of confidence interval"
## [1] "Sample 377 of 1,000 has population mean outside of confidence interval"
## [1] "Sample 672 of 1,000 has population mean outside of confidence interval"
## [1] "Sample 681 of 1,000 has population mean outside of confidence interval"
## [1] "Sample 710 of 1,000 has population mean outside of confidence interval"
## [1] "Sample 815 of 1,000 has population mean outside of confidence interval"
## [1] "Sample 884 of 1,000 has population mean outside of confidence interval"
## [1] "Sample 926 of 1,000 has population mean outside of confidence interval"
## [1] "Sample 961 of 1,000 has population mean outside of confidence interval"
## [1] "Sample 969 of 1,000 has population mean outside of confidence interval"
abline(h=mean(population),lty=2)

Taking more samples, we are less affected by getting one outlier sample. We find that the percentage of intervals that exclude the population mean is now very close to 1%. ## Teamwork report

Team member Attendance Author Contribution %
Name of member 1 Yes / No Yes / No 25%
Name of member 2 Yes / No Yes / No 25%
Name of member 3 Yes / No Yes / No 25%
Name of member 4 Yes / No Yes / No 25%
Total 100%