Foundations for statistical inference - Confidence intervals

Author

Megan Vollbrecht

#Set seed to create reproducible lab report

set.seed(61616)
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.0
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(openintro)
Loading required package: airports
Loading required package: cherryblossom
Loading required package: usdata
library(infer)
Warning: package 'infer' was built under R version 4.3.3

#Create population

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

#Visualize responses

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 statistics

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

#Start with sample of size 60

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

##1 What percent of the adults in your sample 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                        29 0.483
2 Yes                       31 0.517

#51.2% of the adults in this sample believe that climate change affects their local community.

##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?

I would not expect another student’s sample proportion to be identical to mine as we are taking different samples from a much wider population so it would be very unlikely that we would have the exact same sample proportion. I would expect it to be somewhat similar to mine though, as we are assuming the true population proportion is 62% so it is more likely to be around this percentage.

#Find 95% 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.383     0.65

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

This means that 95% of the time the true proportion will be contained in the sample.

##Does your confidence interval capture the true population proportion of US adults who think climate change affects their local community? # Yes it does as 62% is between 40% and 65%.

##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 95% to capture the true population mean as that is the confidence that we all chose, so 95% of the time it should contain the true population proportion.

##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.
# 5 out of 50 of the samples did not contain the true population proportion. This is not exactly equal to the confidence level as there is only 50 samples which were used so it is less reliable and more likely to not perfectly match the confidence interval.

##More Practice

##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 # I choose 90%. I expect it to be narrower than the confidence level at 95% as it contains less samples within the 90%. There are less samples in the 90% than in 95%.

##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.90)
# A tibble: 1 × 2
  lower_ci upper_ci
     <dbl>    <dbl>
1    0.417    0.617

There is 90% confident that the true proportion of US adults who think climate change affects their local community is between 41.7% and 63.3%.

##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? #9 out of 50 of the intervals did not include the true population proportion. 82% of the samples included the true population proportion. This is lower than the confidence level selected for the intervals.

##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.

I am chosing 98% confidence. This interval should be wider than the previous 2.

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

#There is 98% confident that the true proportion of US adults who think climate change affects their local community is between 36.7% and 66.7%.

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

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?

The width of the interval does not change as you increase the number of samples, but you are more likely to have closer to the confidence interval of the percentage of samples containing the true population proportion.