R Lab - Confidence Intervals

Load libraries and set the seed

library(tidyverse)
library(openintro)
library(infer)
set.seed(123)

Get the dataset

In this lab, you will assume this 62% is a true population proportion and learn about how sample proportions can vary from sample to sample by taking smaller samples from the population. We will first create our population assuming a population size of 100,000. This means 62,000 (62%) of the adult population think climate change impacts their community, and the remaining 38,000 does not think so.

us_adults <- tibble(
  climate_change_affects = c(rep("Yes", 62000), rep("No", 38000))
)

Bar chart Visualization

ggplot(us_adults, aes(x = climate_change_affects)) +
  geom_bar() +
  labs(
    x = "", y = "",
    title = "Do you think climate change is affecting your local community?"
  ) +
  coord_flip() 

Summary

us_adults %>%
  count(climate_change_affects) %>%
  mutate(p = n /sum(n))
# A tibble: 2 × 3
  climate_change_affects     n     p
  <chr>                  <int> <dbl>
1 No                     38000  0.38
2 Yes                    62000  0.62

Creating a sample with sample size 60

n <- 60
samp <- us_adults %>%
  sample_n(size = n)

Practice question A.1

What percent of the adults in your sample think climate change affects their local community? Hint: Just like we did with the population, we can calculate the proportion of those in this sample who think climate change affects their local community.

samp |>
  count(climate_change_affects) |>
  mutate(p = n /sum(n))
# A tibble: 2 × 3
  climate_change_affects     n     p
  <chr>                  <int> <dbl>
1 No                        21  0.35
2 Yes                       39  0.65
# The proportion of adults in my sample that think climate change affects
# their local community is 0.65

Practice question A.2

Would you expect another student’s sample proportion to be identical to yours? Would you expect it to be similar? Why or why not?

# No, it is highly unlikely that another student's sample proportion is
# identical to mine, I would expect that it is similar though. This is because
# the sample is coming from the same population.

Create a confidence interval

samp %>%
  specify(response = climate_change_affects, success = "Yes") %>%
  generate(reps = 1000, type = "bootstrap") %>%
  calculate(stat = "prop") %>%
  get_ci(level = 0.95)
# A tibble: 1 × 2
  lower_ci upper_ci
     <dbl>    <dbl>
1    0.533    0.767

Practice question B.1

In the interpretation above, we used the phrase “95% confident”. What does “95% confidence” mean?

# What this means is that if we repeated this simulation 100 times stemming
# from 100 different samples of the population, we expect that 95 out of those
# 100 confidence intervals contains the true proportion

Practice question C.1

Does your confidence interval capture the true population proportion of US adults who think climate change affects their local community? If you are working on this lab in a classroom, does your neighbor’s interval capture this value?

# My confidence interval does capture the true proportion

Practice question C.2

Each student should have gotten a slightly different confidence interval. What proportion of those intervals would you expect to capture the true population mean? Why?

# I would expect that 95% of these confidence intervals would capture the true
# population proportion, this is becausd this is what a 95% confidence interval
# means

Practice question D.1

Given a sample size of 60, 1000 bootstrap samples for each interval, and 50 confidence intervals constructed (the default values for the above app), what proportion of your confidence intervals include the true population proportion? Is this proportion exactly equal to the confidence level? If not, explain why. Make sure to include your plot in your answer.

# The proportion of confidence intervals which contain the true population
# proportion is 0.90, this is not exactly equal to the confidence level but
# that is just due to the natural imperfection of statistical methods.

Practice question E.1

Choose a different confidence level than 95%. Would you expect a confidence interval at this level to be wider or narrower than the confidence interval you calculated at the 95% confidence level? Explain your reasoning.

# A confidence interval other than 95% is 90%
# As the confidence level gets lower, I expect that the confidence interval
# would get narrower. This is because as the CI gets narrower, the likelihood
# that the true proportion falls within the CI gets lower. This explains why
# we are less confident and have a lower confidence level as the CI gets
# more and more narrow.

Practice question E.2

Choose a different confidence level than 95%. Would you expect a confidence interval at this level to be wider or narrower than the confidence interval you calculated at the 95% confidence level? Explain your reasoning.

# A confidence interval other than 95% is 99%
# As the confidence level gets higher, I expect that the confidence interval
# would get wider. This is because as the CI gets wider, the likelihood
# that the true proportion falls within the CI gets higher. This explains why
# we are more confident and have a higher confidence level as the CI gets
# increasingly wider.

Practice question E.3

Using code from the infer package and data from the one sample you have (samp), find a confidence interval for the proportion of US Adults who think climate change is affecting their local community with a confidence level of your choosing (other than 95%) and interpret it.

samp |>
  specify(response = climate_change_affects, success = "Yes") |>
  generate(reps = 1000, type = "bootstrap") |>
  calculate(stat = "prop") |>
  get_ci(level = 0.99)
# A tibble: 1 × 2
  lower_ci upper_ci
     <dbl>    <dbl>
1    0.500      0.8
# With a 99% confidence interval, we are 99% confident that the true proportion
# of US Adults who think climate change is affecting their local community
# falls between 0.50 and 0.8

Practice question E.4

Using the app, calculate 50 confidence intervals at the confidence level you chose in the previous question, and plot all intervals on one plot, and calculate the proportion of intervals that include the true population proportion. How does this percentage compare to the confidence level selected for the intervals?

# The proportion of intervals that had the true proportion within them was 1.00,
# or 100%, this is greater than the 50 confidence intervals at 95%, which only
# contained 90% of the true population means.

Practice question E.5

Lastly, try one more (different) confidence level. First, state how you expect the width of this interval to compare to previous ones you calculated. Then, calculate the bounds of the interval using the infer package and data from samp and interpret it. Finally, use the app to generate many intervals and calculate the proportion of intervals that are capture the true population proportion.

# We're going to choose a 50% confidence interval this time. This confidence
# interval will be much narrower than the previous 2.

samp |>
  specify(response = climate_change_affects, success = "Yes") |>
  generate(reps = 1000, type = "bootstrap") |>
  calculate(stat = "prop") |>
  get_ci(level = 0.50)
# A tibble: 1 × 2
  lower_ci upper_ci
     <dbl>    <dbl>
1    0.617    0.683
# Only 23 of the 50 CIs captured the true population proportion, which is a
# proportion of 0.46 (very close to 0.50)

Practice question E.6

Using the app, experiment with different sample sizes and comment on how the widths of intervals change as sample size changes (increases and decreases).

# They don't neccessarily get wider or narrower as the sample size increases or
# decreases, but when the sample size is lower, there is less uniformity in how
# wide the confidence interval is, and vice versa.

Practice question E.7

Finally, given a sample size (say, 60), how does the width of the interval change as you increase the number of bootstrap samples. Hint: Does changing the number of bootstrap samples affect the standard error?

# As we increase the number of bootstrap samples, the width of the confidence intervals shrinks.