library(tidyverse)

gss_survey <- read_csv("http://www.cknuckles.com/statsprogramming/data/gss_survey_data.csv")
workhours_2018 <- gss_survey |>
  filter(year == "2018") |>
  select(workhours) |>
  drop_na(workhours)

set.seed(4)   # Same random results as in the book

# Example - Bootstrap CI using Core R
########################################################################################

resample_means <- replicate(10000, mean(sample(workhours_2018$workhours, replace = TRUE)))
quantile(resample_means, c(.025, .975))   # percentile method 95% CI


# Example - Bootstrap CI using the boot Package (installed with R, so it only has to be loaded)
########################################################################################

library(boot)
mean_fun <- function(data, indices) mean(data[indices])    # the form boot() requires
boot_results <- boot(workhours_2018$workhours, statistic = mean_fun, R = 10000)
boot.ci(boot_results, conf = .95, type = "perc")           # percentile method 95% CI
