10: Bootstrapping and Confidence Intervals

Based on Chapter 8 of ModernDive. Code for Quiz 12.

Load the R package we will use.

library(tidyverse)
library(moderndive) #install before loading
library(infer) #install before loading
library(fivethirtyeight) #install before loading

What is the average age of members that have served in congress?

set.seed(???)

??? <- ???  %>% 
  rep_sample_n(size=???)

Construct the confidence interval

1. Use specify to indicate the variable from congress_age_100 that you are interested in

congress_age_100  %>% 
  ???(response = ???)

2. generate 1000 replicates of your sample of 100

congress_age_100  %>% 
  specify(response = age)  %>% 
  ???(reps = 1000, type= "bootstrap")

The output has ??? rows


3. calculate the mean for each replicate

bootstrap_distribution_mean_age  <- congress_age_100  %>% 
  specify(response = age)  %>% 
  generate(reps = 1000, type = "bootstrap")  %>% 
  ???(stat = "???")

bootstrap_distribution_mean_age

4. visualize the bootstrap distribution

???(???) 

Calculate the 95% confidence interval using the percentile method

congress_ci_percentile  <- bootstrap_distribution_mean_age %>% 
  get_confidence_???(type = "???", level = ???)

congress_ci_percentile
obs_mean_age  <-  ???  %>% 
  specify(response = ???)  %>% 
  calculate(stat = "???")  %>% 
  pull()

obs_mean_age
visualize(bootstrap_distribution_mean_age) +
  shade_confidence_interval(endpoints = ???) + 
  geom_vline(xintercept = ???, color = "hotpink", size = 1 )
pop_mean_age  <- ???  %>% 
  summarize(pop_mean= mean(age))  %>% pull()

pop_mean_age
visualize(bootstrap_distribution_mean_age) +
  shade_confidence_interval(endpoints = congress_ci_percentile) + 
   geom_vline(xintercept = ???, color = "hotpink", size = 1) +
   geom_vline(xintercept = ??? , color = "purple", size = 3)