Bootstrap Resampling in R
Implement bootstrap confidence intervals using sample() with replacement.
What Is Bootstrap?
The bootstrap is a resampling method that estimates the sampling distribution of a statistic by resampling with replacement from the observed data. It requires no distributional assumptions — the data is the distribution.
set.seed(42)
# Original sample of 20 observations
original_data <- c(14, 18, 11, 13, 6, 8, 2, 17,
20, 10, 15, 12, 9, 16, 7, 3,
19, 5, 4, 1)
n <- length(original_data)
# One bootstrap resample: sample WITH replacement
bootstrap_sample1 <- sample(original_data, n, replace = TRUE)
print(bootstrap_sample1)
# Contains repeats! Some original values are missing.
mean(original_data) # original mean
mean(bootstrap_sample1) # bootstrap mean (different)Bootstrap Distribution of the Mean
Generate many bootstrap resamples, compute the statistic on each, and collect the results. This bootstrap distribution approximates the sampling distribution of the statistic.
set.seed(42)
x <- c(14, 18, 11, 13, 6, 8, 2, 17, 20, 10,
15, 12, 9, 16, 7, 3, 19, 5, 4, 1)
n <- length(x)
B <- 5000 # number of bootstrap replicates
# Bootstrap distribution of the mean
boot_means <- replicate(B, {
x_star <- sample(x, n, replace = TRUE)
mean(x_star)
})
cat('Original mean:', mean(x), '\n')
cat('Bootstrap mean:', mean(boot_means), '\n')
cat('Bootstrap SE:', sd(boot_means), '\n')
cat('Theoretical SE:', sd(x)/sqrt(n))All lessons in this course
- set.seed() and Reproducibility
- Generating Random Distributions
- Monte Carlo Simulation Basics
- Bootstrap Resampling in R