Based on Chapter 8 of ModernDive. Code for Quiz 12.
-Load the R package we will use.
install.packages('fivethirtyeightdata', repos =
'https://fivethirtyeightdata.github.io/drat/', type = 'source')
Replace all the instances of ???. These are answers on your moodle quiz. Run all the individual code chunks to make sure the answers in this file correspond with your quiz answers After you check all your code chunks run then you can knit it. It won’t knit until the ??? are replaced Save a plot to be your preview plot Look at the variable definitions in congress_age What is the average age of members that have served in congress? Set random seed generator to 123 Take a sample of 100 from the dataset congress_age and assign it to congress_age_100
set.seed(4346)
congress_age
# A tibble: 18,635 x 13
congress chamber bioguide firstname middlename lastname suffix
<int> <chr> <chr> <chr> <chr> <chr> <chr>
1 80 house M000112 Joseph Jefferson Mansfie… <NA>
2 80 house D000448 Robert Lee Doughton <NA>
3 80 house S000001 Adolph Joachim Sabath <NA>
4 80 house E000023 Charles Aubrey Eaton <NA>
5 80 house L000296 William <NA> Lewis <NA>
6 80 house G000017 James A. Gallagh… <NA>
7 80 house W000265 Richard Joseph Welch <NA>
8 80 house B000565 Sol <NA> Bloom <NA>
9 80 house H000943 Merlin <NA> Hull <NA>
10 80 house G000169 Charles Laceille Gifford <NA>
# … with 18,625 more rows, and 6 more variables: birthday <date>,
# state <chr>, party <chr>, incumbent <lgl>, termstart <date>,
# age <dbl>
congress_age_100 <- congress_age %>%
rep_sample_n(size=100)
congress_age is the population and congress_age_100 is the sample 18635 is number of observations in the the population and 100 is the number of observations in your sample
congress_age_100 %>%
specify(response = age)
Response: age (numeric)
# A tibble: 100 x 1
age
<dbl>
1 58
2 27.3
3 59.4
4 47.8
5 36.4
6 62.3
7 52.5
8 55.5
9 44
10 48
# … with 90 more rows
Response: age (numeric)
# A tibble: 100,000 x 2
# Groups: replicate [1,000]
replicate age
<int> <dbl>
1 1 55.2
2 1 40.8
3 1 55.7
4 1 52.5
5 1 54.5
6 1 35.8
7 1 44.5
8 1 47.9
9 1 40.8
10 1 37.4
# … with 99,990 more rows
The output has 10,000 rows 3. calculate the mean for each replicate -Assign to bootstrap_distribution_mean_age -Display bootstrap_distribution_mean_age
bootstrap_distribution_mean_age <- congress_age_100 %>%
specify(response = age) %>%
generate(reps = 1000, type = "bootstrap") %>%
calculate(stat = "mean")
bootstrap_distribution_mean_age
# A tibble: 1,000 x 2
replicate stat
* <int> <dbl>
1 1 51.3
2 2 48.2
3 3 49.7
4 4 50.5
5 5 51.6
6 6 47.9
7 7 49.5
8 8 50.0
9 9 51.0
10 10 51.0
# … with 990 more rows
-The bootstrap_distribution_mean_age has 1000 means 4. visualize the bootstrap distribution
visualize(bootstrap_distribution_mean_age)
ggsave(filename = "preview.png",
path = here::here("_posts", "2021-05-04-bootstrapping-and-confidence-intervals"))
-Assign the output to congress_ci_percentile -Display congress_ci_percentile
congress_ci_percentile <- bootstrap_distribution_mean_age %>%
get_confidence_interval(type = "percentile", level = .95)
congress_ci_percentile
# A tibble: 1 x 2
lower_ci upper_ci
<dbl> <dbl>
1 48.5 52.7
-Display obs_mean_age
obs_mean_age <- congress_age_100 %>%
specify(response = age) %>%
calculate(stat = "mean") %>%
pull()
obs_mean_age
[1] 50.533
-Shade the confidence interval -Add a line at the observed mean, obs_mean_age, to your visualization and color it “hotpink”
visualize(bootstrap_distribution_mean_age) +
shade_confidence_interval(endpoints = congress_ci_percentile) +
geom_vline(xintercept = 53.597, color = "hotpink", size = 1 )
#Calculate the population mean to see if it is in the 95% confidence interval
-Assign the output to pop_mean_age -Display pop_mean_age
pop_mean_age <- congress_age %>%
summarize(pop_mean= mean(age)) %>% pull()
pop_mean_age
[1] 53.31373
-Add a line to the visualization at the, population mean, pop_mean_age, to the plot color it “purple”
visualize(bootstrap_distribution_mean_age) +
shade_confidence_interval(endpoints = congress_ci_percentile) +
geom_vline(xintercept = 53.313, color = "hotpink", size = 1) +
geom_vline(xintercept = 53.597 , color = "purple", size = 3)
Is population mean in the 95% confidence interval constructed using the bootstrap distribution? YES -Change set.seed(123) to set.seed(4346). Rerun all the code. -When you change the seed is the population mean in the 95% confidence interval constructed using the bootstrap distribution? NO -If you construct 100 95% confidence intervals approximately how many do you expect will contain the population mean? 0