Skip to content

Instantly share code, notes, and snippets.

@StefanM98
Created August 25, 2021 17:34
Show Gist options
  • Save StefanM98/73e73b6e7983f8bf8ea486ef3f7636ab to your computer and use it in GitHub Desktop.
Save StefanM98/73e73b6e7983f8bf8ea486ef3f7636ab to your computer and use it in GitHub Desktop.
## Creating a 95% Bootstrap Confidence Interval for the sample standard deviation ### General Steps 1. Take the original sample, which has a sample size of sample_size, and treat it like the population. 2. Take a sample of size sample_size, with rep
create_bootstrap <- function (.data, num_samples, sample_size) {
# Create "num_samples" samples, of size "sample_size", from the original population ".data"
samples <- rerun(num_samples, sample(.data, sample_size, replace = TRUE))
# Find the standard deviation of each dataset
std_devs <- samples %>% map_dbl(sd)
# Find the bounds of the 95% confidence interval
interval <- std_devs %>% quantile(c(.025, .975))
# Return a list containing the confidence interval and the standard deviations of each dataset.
list(interval = interval, std_devs = std_devs)
}
### Usage ###
# The original sample
set.seed(26)
samp <- rnorm(20, mean = 10, sd = 6)
# Create the bootstrap
bootstrap <- create_bootstrap(samp, 10000, 20)
# Print the interval
bootstrap$interval
# Plot a histogram of the standard deviations to see the bootstrap distribution
hist(bootstrap$std_devs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment