We rarely observe an entire population; instead we work with a sample and ask what its statistics tell us about the population that produced it. This chapter introduces sampling distributions (how a statistic varies from sample to sample), the two profound results that govern them (the Law of Large Numbers and the Central Limit Theorem), and the resampling tools (jackknife, bootstrap) that approximate sampling variability from a single dataset.
Sampling
In practice we almost never observe the entire group we care about; we observe a slice and have to make judgments from that slice.
A population is the entire collection of units of interest.
A sample is a subset of the population that we actually observe.
A simple random sample is a sample where every subset of size \(n\) has the same probability of being chosen.
The simple random sample is useful as the default baseline: every other sampling scheme is described by how it departs from it, and the mathematical results in the rest of the chapter assume a simple random sample.
Code
# Simple random sample (no duplicates, equal probability)
population_values <- c(1, 2, 3, 4) # population
sample(population_values, 2, replace=FALSE) #sample
## [1] 1 3
How many possible samples of two are there from a population with this data: \({7,10,122,55}\)?
Factorials are used for counting permutations: different ways of rearranging \(n\) distinct objects into a sequence. The factorial is denoted as \(n!=1\times2\times3 ... (n-2)\times(n-1)\times n\).
E.g., how many ways are there to order the numbers \(\{1, 2, 3\}\)?
Code
#{1, 2, 3} {1, 3, 2}
#{2, 1, 3} {2, 3, 1}
#{3, 2, 1} {3, 1, 2}
factorial(3)
## [1] 6
The binomial coefficient counts the subsets of \(k\) elements from a set with \(n\) elements, and is mathematically defined as \(\tbinom{n}{k}=\frac{n!}{k!(n-k)!}\).
For example, how many subsets with \(k=2\) are there for the set \(\{1,2,3,4\}\)?
Code
#Ways to draw k=2 from a set with n=4
#{1, 2} {1, 3} {1, 4}
# {2, 3} {2, 4}
# {3, 4}
choose(4, 2)
## [1] 6
More generally, counting outcomes relies on a few rules. The multiplication rule says that if one choice has \(a\) options and an independent second choice has \(b\) options, then together they have \(a \times b\) options. When we select \(k\) items from \(n\) and the order matters, there are \(n \times (n-1) \times \cdots \times (n-k+1)\) permutations. When the order does not matter, we divide out the \(k!\) rearrangements and count \(\binom{n}{k}\) combinations.
License plates. A plate has the format AB1 23C: three letter slots and three number slots, with no letter and no number repeated. The letters can be filled \(26 \times 25 \times 24\) ways and the numbers \(10 \times 9 \times 8\) ways, so by the multiplication rule there are \((26 \times 25 \times 24)(10 \times 9 \times 8)\) possible plates.
Bank-account sample. From \(20\) accounts we draw a sample of \(3\), and the order of selection does not matter. The number of possible samples is \(\binom{20}{3}\).
HR committee. In the morning, \(2\) of \(25\) employees are chosen for distinct roles (manager, then assistant manager), so order matters: \(25 \times 24\) ways. In the afternoon, \(5\) of \(23\) employees are chosen for termination, where order does not matter: \(\binom{23}{5}\) ways. The two decisions are independent, so the multiplication rule gives \((25 \times 24)\binom{23}{5}\) outcomes for the day.
Code
# License plates: 3 distinct letters, 3 distinct numbers
(26 * 25 * 24) * (10 * 9 * 8)
## [1] 11232000
# Bank-account sample of 3 from 20 (order irrelevant)
choose(20, 3)
## [1] 1140
# HR committee: morning ordered, afternoon unordered
(25 * 24) * choose(23, 5)
## [1] 20189400
Often, we think of the population as being infinitely large. This is an approximation that makes mathematical and computational work much simpler.
Intuition for infinite populations: imagine drawing names from a giant urn. If the urn has only \(10\) names, then removing one name slightly changes the composition of the urn, and the probabilities shift for the next name you draw. Now imagine the urn has \(100\) billion names, so that removing one makes no noticeable difference. We can pretend the composition never changes: each draw is essentially identical and independent (iid). We can actually guarantee the names are iid by putting any names drawn back into the urn (sampling with replacement).
Sampling with replacement from the small population \(\{1, 2, 3, 4\}\) allows duplicates, so every draw has the same four possible values.
Code
#All possible samples of two from a bag of numbers {1, 2, 3, 4} with replacement
#{1, 1} {1, 2} {1, 3}, {3, 4}
#{2, 2} {2, 3} {2, 4}
#{3, 3} {3, 4}
#{4, 4}
# Simple random sample (duplicates, equal probability)
population_values <- c(1, 2, 3, 4) # population
sample(population_values, 2, replace=TRUE)
## [1] 2 2
Wages as a Population
In practice, we never see the whole population, so we cannot check how close a sample statistic is to the truth. To make such checks possible, this chapter pretends that the \(3294\) workers in the Wages1 dataset are the entire population. They are really a sample themselves, but pretending lets us compute the true population mean and compare it with what one sample of workers tells us. The data come from the Ecdat package, which you need to install once.
Code
# install.packages('Ecdat') # only once
data(Wages1, package='Ecdat')
# Pretend these 3294 hourly wages are the whole population
population_wages <- Wages1[, 'wage']
population_wage_mean <- mean(population_wages)
population_wage_mean
## [1] 5.757585
# One simple random sample of 50 workers
x_sim <- sample(population_wages, 50, replace=FALSE)
mean(x_sim)
## [1] 5.93255
Each time you run the last two lines, you get a different sample of workers and a different sample mean.
Sampling Distributions
A statistic computed on one sample is just one number; we want to know how much that number would change if we drew a different sample.
The sampling distribution of a statistic is the probability distribution of the statistic’s value across all possible samples of size \(n\) from the same population. It tells us how much a statistic varies from one sample to the next.
The sampling distribution is useful for quantifying uncertainty: its spread shows how much the statistic typically moves from sample to sample, and its center shows where it is targeted on average. For example, the sampling distribution of the mean shows how \(\hat{m}\) varies from sample to sample; it can also be called the probability distribution of the sample mean.
Given ages for population of \(4\) students, compute the sampling distribution for the mean with samples of \(n=2\).
Code
population_values <- c(18, 20, 22, 24) # Ages for student population
# six possible samples
m1_hat <- mean( population_values[c(1, 2)] ) #{1, 2}
m2_hat <- mean( population_values[c(1, 3)] ) #{1, 3}
m3_hat <- mean( population_values[c(1, 4)] ) #{1, 4}
m4_hat <- mean( population_values[c(2, 3)] ) #{2, 3}
m5_hat <- mean( population_values[c(2, 4)] ) #{2, 4}
m6_hat <- mean( population_values[c(3, 4)] ) #{3, 4}
# sampling distribution
sample_means <- c(m1_hat, m2_hat, m3_hat, m4_hat, m5_hat, m6_hat)
hist(sample_means,
freq=FALSE, breaks=100,
main=NA, border=NA)
Now compute the sampling distribution for the median with samples of \(n=3\). There are four possible samples, \(\{18,20,22\}\), \(\{18,20,24\}\), \(\{18,22,24\}\), and \(\{20,22,24\}\), with medians \(20\), \(20\), \(22\), and \(22\).
Code
population_values <- c(18, 20, 22, 24) # Ages for student population
# four possible samples
med1_hat <- median( population_values[c(1, 2, 3)] )
med2_hat <- median( population_values[c(1, 2, 4)] )
med3_hat <- median( population_values[c(1, 3, 4)] )
med4_hat <- median( population_values[c(2, 3, 4)] )
sample_medians <- c(med1_hat, med2_hat, med3_hat, med4_hat)
sample_medians
## [1] 20 20 22 22
To consider an infinite population, draw from a uniform distribution instead of a list of values.
Code
# Three Sample Example from infinite population
x1_sim <- runif(100)
m1_hat <- mean(x1_sim)
x2_sim <- runif(100)
m2_hat <- mean(x2_sim)
x3_sim <- runif(100)
m3_hat <- mean(x3_sim)
sample_means <- c(m1_hat, m2_hat, m3_hat)
sample_means
## [1] 0.5212836 0.4964472 0.4911301
# An Equivalent Approach: fill vector in a loop
sample_means <- rep(NA, 3)
for(i in seq(sample_means)){
x_sim <- runif(100)
m_hat <- mean(x_sim)
sample_means[i] <- m_hat
}
sample_means
## [1] 0.4849007 0.5224332 0.5620205
For more on loops, see Loops.
Each sample has its own histogram and its own mean.
Code
# Three Sample Example w/ Visual
par(mfrow=c(1, 3))
for(b in 1:3){
x_sim <- runif(100)
m_hat <- mean(x_sim)
hist(x_sim,
breaks=seq(0, 1, by=.1), #for comparability
freq=FALSE, main=NA, border=NA)
abline(v=m_hat, col=rgb(1, 0, 0, .8), lwd=2)
title(paste0('mean= ', round(m_hat, 2)), font.main=1)
}
Expanding the loop to many samples gives the sampling distribution of the mean.
Code
# Many sample example
sample_means <- rep(NA, 500)
for(i in seq_along(sample_means)){
x_sim <- runif(1000)
m_hat <- mean(x_sim)
sample_means[i] <- m_hat
}
hist(sample_means,
breaks=seq(0.45, 0.55, by=.001),
border=NA, freq=FALSE,
col=rgb(1, 0, 0, .5),
xlab=expression(hat(m)),
main=NA)
title('Sampling Distribution of the mean', font.main=1)
Now draw three samples of \(50\) workers from the wage population. Each sample mean (red) lands near, but not on, the population mean (black).
Code
# Three samples of workers, each with its own mean
par(mfrow=c(1, 3))
for(b in 1:3){
x_sim <- sample(population_wages, 50, replace=FALSE)
m_hat <- mean(x_sim)
hist(x_sim,
breaks=seq(0, 40, by=2), #for comparability
freq=FALSE, main=NA, border=NA,
xlab='hourly wage')
abline(v=m_hat, col=rgb(1, 0, 0, .8), lwd=2)
abline(v=population_wage_mean)
title(paste0('mean= ', round(m_hat, 2)), font.main=1)
}
Repeating this for many samples of workers gives the sampling distribution of the mean wage.
Code
# Many samples of workers
wage_means <- rep(NA, 500)
for(i in seq_along(wage_means)){
x_sim <- sample(population_wages, 50, replace=FALSE)
m_hat <- mean(x_sim)
wage_means[i] <- m_hat
}
hist(wage_means,
breaks=30,
border=NA, freq=FALSE,
col=rgb(1, 0, 0, .5),
xlab=expression(hat(m)),
main=NA)
abline(v=population_wage_mean)
title('Sampling Distribution of the mean wage', font.main=1)
In this figure, you see one of the most profound results known in statistics: the sampling distribution of the mean is approximately Normal around the true mean.
Central Limit Theorem (CLT)
The Central Limit Theorem (CLT) says that as \(n\) grows, the sampling distribution of \(\hat{m}\) is approximately Normal, regardless of the shape of the underlying population (under mild conditions on variance).
The CLT is useful because it lets us treat sample means as approximately Normal even when we know little about the population distribution, which is why so many statistical procedures (intervals, hypothesis tests) are built on Normal calculations. There are different variants of the theorem, but all say some version of “the sampling distribution of the mean is approximately Normal”; the histogram shown above is one such example.
The means of uniform samples are approximately Normal.
Code
sample_means <- rep(NA, 500)
for(i in seq_along(sample_means)){
x_sim <- runif(1000)
m_hat <- mean(x_sim)
sample_means[i] <- m_hat
}
hist(sample_means,
breaks=seq(0.45, 0.55, by=.001),
border=NA, freq=FALSE,
col=rgb(1, 0, 0, .5),
xlab=expression(hat(m)),
main=NA)
title('Sampling Distribution of the mean', font.main=1)
# Approximately normal?
m_sampling_hat <- mean(sample_means)
s_sampling_hat <- sd(sample_means)
x <- seq(0.1, 0.9, by=0.001)
fx <- dnorm(x, m_sampling_hat, s_sampling_hat)
lines(x, fx, col=rgb(1, 0, 0, .8))
The wage population is a harder case, because wages are strongly skewed: most workers earn a moderate wage and a few earn much more. The means of samples of \(50\) workers are not skewed in the same way.
Code
par(mfrow=c(1, 2))
# Population: skewed
hist(population_wages,
breaks=seq(0, 40, by=1),
border=NA, freq=FALSE,
xlab='hourly wage',
main=NA)
abline(v=population_wage_mean)
title('Population', font.main=1)
# Sampling distribution: approximately Normal?
hist(wage_means,
breaks=30,
border=NA, freq=FALSE,
col=rgb(1, 0, 0, .5),
xlab=expression(hat(m)),
main=NA)
m_sampling_hat <- mean(wage_means)
s_sampling_hat <- sd(wage_means)
x <- seq(min(wage_means), max(wage_means), length.out=200)
fx <- dnorm(x, m_sampling_hat, s_sampling_hat)
lines(x, fx, col=rgb(1, 0, 0, .8))
title('Sample means, n=50', font.main=1)
Many statistics have an approximately Normal sampling distribution.
For an example with another statistic, let’s the sampling distribution of the standard deviation.
Code
# CLT example of the 'sd' statistic
sample_sds <- rep(NA, 1000)
for(i in seq_along(sample_sds)){
x_sim <- runif(100) # same distribution
s_hat <- sd(x_sim) # different statistic
sample_sds[i] <- s_hat
}
hist(sample_sds,
breaks=seq(0.2, 0.4, by=.01),
border=NA, freq=FALSE,
col=rgb(0, 0, 1, .5),
xlab=expression(hat(s)),
main=NA)
title('Sampling Distribution of the sd', font.main=1)
# Approximately normal?
m_sampling_hat <- mean(sample_sds)
s_sampling_hat <- sd(sample_sds)
x <- seq(0.1, 0.9, by=0.001)
fx <- dnorm(x, m_sampling_hat, s_sampling_hat)
lines(x, fx, col=rgb(0, 0, 1, .8))
Code
# try another random variable, such as rexp(100) instead of runif(100)
It is beyond this class to prove this result mathematically, but you should know that not all sampling distributions are standard normal. The CLT approximation is better for “large \(n\)” datasets with “well behaved” variances. The CLT also does not apply to “extreme” statistics.
For example of “extreme” statistics, examine the sampling distribution of min and max statistics.
Code
# Create 300 samples, each with 1000 random uniform variables
X_samples <- matrix(nrow=300, ncol=1000)
for(i in seq(1, nrow(X_samples))){
X_samples[i, ] <- runif(1000)
}
# Each row is a new sample
length(X_samples[1, ])
## [1] 1000
# Compute min and max for each sample
x_mins <- apply(X_samples, 1, quantile, probs=0)
x_maxs <- apply(X_samples, 1, quantile, probs=1)
# Plot the sampling distributions of min, median, and max
# Median looks normal. Maximum and minimum do not!
par(mfrow=c(1, 2))
hist(x_mins, breaks=100, main=NA,
xlab=expression(hat(q)[0]), border=NA, freq=FALSE)
title('Min', font.main=1)
hist(x_maxs, breaks=100, main=NA,
xlab=expression(hat(q)[1]), border=NA, freq=FALSE)
title('Max', font.main=1)
title('Sampling Distributions', outer=TRUE, line=-1, adj=0, font.main=1)
Explore the sampling distribution of another statistic, such as the range.
Code
my_function <- function(x){ diff(range(x)) }
x_ranges <- apply(X_samples, 1, my_function)
hist(x_ranges, breaks=100, main=NA,
xlab='Sample Range', border=NA, freq=FALSE)
Here is an example where variance is not “well behaved” .
Code
sample_means <- rep(NA, 999)
for(i in seq_along(sample_means)){
x_sim <- rcauchy(1000, 0, 10)
m_hat <- mean(x_sim)
sample_means[i] <- m_hat
}
hist(sample_means, breaks=100,
main='',
border=NA, freq=FALSE) # Tails look too 'fat'
Law of Large Numbers (LLN)
The CLT tells us the sample mean is Normally distributed around the true mean. Another profound result is that the sampling distribution tightens with more observations.
The Law of Large Numbers (LLN) says the sample mean concentrates around the population mean as \(n\) grows: with more data, the sampling distribution of \(\hat{m}\) tightens around \(\mathbb{E}[X_i]\).
The LLN is useful as a guarantee that averaging works: even when individual observations are noisy, the sample mean of a large sample is close to the population mean. There are different variants of the theorem (weak vs. strong, with vs. without finite variance), but they all say some version of “the sample mean becomes more tightly centered around the true mean as we get more data”.
Uniform draws on \([0,1]\) have a population mean of \(0.5\). Notice where the sampling distribution is centered
Code
sample_means <- rep(NA, 500)
for(i in seq_along(sample_means)){
x_sim <- runif(1000)
sample_means[i] <- mean(x_sim)
}
m_sampling_hat <- mean(sample_means)
round(m_sampling_hat, 3)
## [1] 0.5
and more tightly centered with more data
Code
par(mfrow=c(1, 3))
for(n in c(5, 50, 500)){
sample_means_n <- rep(NA, 299)
for(i in seq_along(sample_means_n)){
x_sim <- runif(n)
m_hat <- mean(x_sim)
sample_means_n[i] <- m_hat
}
hist(sample_means_n,
breaks=seq(0, 1, by=.01),
border=NA, freq=FALSE,
col=rgb(1, 0, 0, .5),
xlab=expression(hat(m)),
main=NA)
title(paste0('n=', n), font.main=1)
}
In the wage population, the sample means are centered on the population mean
Code
round(mean(wage_means), 3) # center of the sampling distribution
## [1] 5.737
round(population_wage_mean, 3) # population mean
## [1] 5.758
and are more tightly centered with larger samples of workers.
Code
par(mfrow=c(1, 3))
for(n in c(5, 50, 500)){
wage_means_n <- rep(NA, 299)
for(i in seq_along(wage_means_n)){
x_sim <- sample(population_wages, n, replace=FALSE)
m_hat <- mean(x_sim)
wage_means_n[i] <- m_hat
}
hist(wage_means_n,
breaks=seq(0, 20, by=.25),
border=NA, freq=FALSE,
col=rgb(1, 0, 0, .5),
xlab=expression(hat(m)),
main=NA)
abline(v=population_wage_mean)
title(paste0('n=', n), font.main=1)
}
Plot the variability of the sample mean as a function of sample size
Code
n_seq <- seq(1, 40)
sd_seq <- rep(NA, length(n_seq))
for(n in seq_along(sd_seq)){
sample_means_n <- rep(NA, 499)
for(i in seq_along(sample_means_n)){
x_sim <- runif(n)
m_hat <- mean(x_sim)
sample_means_n[i] <- m_hat
}
sd_seq[n] <- sd(sample_means_n)
}
plot(n_seq, sd_seq, pch=16, col=grey(0, 0.5),
xlab='n', ylab='sd of sample means', main=NA)
Here is the intuition for estimating the mean weight of an apple:
- With \(n=1\) apple, your estimate depends entirely on that one draw. If it happens to be unusually large or small, your estimate can be far off.
- With \(n=2\) apples, the estimate averages out their idiosyncrasies. An unusually heavy apple can be balanced by a lighter one, lowering how far off you can be. You are less likely to get two extreme values than just one.
- With \(n=100\) apples, individual apples barely move the needle. The average becomes stable.
Here is an intuitive example, from a small discrete population. Notice the extreme values
Code
population_values <- c(18, 20, 22, 24) #student ages (population values)
mean(population_values) # Population mean
## [1] 21
# six possible samples of size 2
m1_hat <- mean( population_values[c(1, 2)] ) #{1, 2}
m2_hat <- mean( population_values[c(1, 3)] ) #{1, 3}
m3_hat <- mean( population_values[c(1, 4)] ) #{1, 4}
m4_hat <- mean( population_values[c(2, 3)] ) #{2, 3}
m5_hat <- mean( population_values[c(2, 4)] ) #{2, 4}
m6_hat <- mean( population_values[c(3, 4)] ) #{3, 4}
means_2 <- c(m1_hat, m2_hat, m3_hat, m4_hat, m5_hat, m6_hat)
sort(means_2)
## [1] 19 20 21 21 22 23
# four possible samples of size 3
m1_hat <- mean( population_values[c(1, 2, 3)] )
m2_hat <- mean( population_values[c(1, 2, 4)] )
m3_hat <- mean( population_values[c(1, 3, 4)] )
m4_hat <- mean( population_values[c(2, 3, 4)] )
means_3 <- c(m1_hat, m2_hat, m3_hat, m4_hat)
sort(means_3)
## [1] 20.00000 20.66667 21.33333 22.00000
The Fundamental Theorem of Statistics
The Law of Large Numbers generalizes to many other statistics, like the median or sd from Summary Statistics.
Here is a sampling distribution for a quantile, for three different sample sizes
Code
par(mfrow=c(1, 3))
for(n in c(5, 50, 500)){
sample_quants_n <- rep(NA, 299)
for(i in seq_along(sample_quants_n)){
x_sim <- runif(n)
q_hat <- quantile(x_sim, probs=0.75) #upper quartile
sample_quants_n[i] <- q_hat
}
hist(sample_quants_n,
breaks=seq(0, 1, by=.01),
border=NA, freq=FALSE,
col=rgb(1, 0, 0, .5),
xlab='Sample Quantile',
main=NA)
title(paste0('n=', n), font.main=1)
}
Here is a sampling distribution for a proportion, for three different sample sizes
Code
par(mfrow=c(1, 3))
for(n in c(5, 50, 500)){
sample_props_n <- rep(NA, 299)
for(i in seq_along(sample_props_n)){
x_sim <- sample(1:5, size=n, prob=c(1:5)/15, replace=TRUE)
p3_hat <- mean(x_sim==3)
sample_props_n[i] <- p3_hat
}
hist(sample_props_n,
breaks=seq(0, 1, by=.01),
border=NA, freq=FALSE,
col=rgb(1, 0, 0, .5),
xlab='Sample Proportion',
main=NA)
title(paste0('n=', n), font.main=1)
}
In fact, the entire empirical distribution converges, not just individual statistics like the mean, median, or a proportion.
The Fundamental Theorem of Statistics says that the ECDF converges to the true CDF as \(n\) grows: the two curves get uniformly close across the entire range of values.
This theorem is useful as the most general consistency guarantee: because so many statistics (means, quantiles, proportions) are just functions of the ECDF, its convergence is what ultimately backs the LLN and the consistency of those other statistics.
Code
par(mfrow = c(1, 3))
for (n in c(50, 500, 5000)) {
x_sim <- runif(n)
F_hat <- ecdf(x_sim)
plot(F_hat, main=NA)
title(paste0('n=', n), font.main=1)
}
In the wage population, the ECDF of a sample of workers (red) gets closer to the population CDF (black) as the sample grows.
Code
F_population <- ecdf(population_wages)
par(mfrow=c(1, 3))
for(n in c(5, 50, 500)){
x_sim <- sample(population_wages, n, replace=FALSE)
F_hat <- ecdf(x_sim)
plot(F_population, do.points=FALSE,
xlim=c(0, 20), xlab='hourly wage', main=NA)
plot(F_hat, do.points=FALSE, add=TRUE, col=rgb(1, 0, 0, .8))
title(paste0('n=', n), font.main=1)
}
Resampling Distributions
With the wage population, we could draw as many samples as we liked. Often, we only have one sample, and no population to draw more samples from. The lab data on murder arrests are one such sample. How then can we estimate the sampling distribution of a statistic?
Code
x_hat <- USArrests[, 'Murder'] # lab data: murder arrests per 100,000
m_hat <- mean(x_hat)
m_hat
## [1] 7.788
We can resample our data. Hesterberg (2015) provides a nice illustration of the idea. The two most basic versions are the jackknife and the bootstrap, which are discussed below.
Note that we do not use the mean of the resampled statistics as a replacement for the original estimate. This is because the resampled distributions are centered at the observed statistic, not the population parameter. (The bootstrapped mean is centered at the sample mean, for example, not the population mean.) This means that we do not use resampling to improve on \(\hat{m}\). We use resampling to estimate sampling variability.
Jackknife Distribution
Here, we compute all “leave-one-out” estimates. Specifically, for a dataset with \(n\) observations, the jackknife uses \(n-1\) observations other than \(i\) for each unique subsample.
Given the sample \(\{1,6,7,22\}\), compute the jackknife estimate of the median. Leave out each observation in turn and take the median of the remaining three.
- Leave out \(1\): median of \(\{6,7,22\}\) is \(7\).
- Leave out \(6\): median of \(\{1,7,22\}\) is \(7\).
- Leave out \(7\): median of \(\{1,6,22\}\) is \(6\).
- Leave out \(22\): median of \(\{1,6,7\}\) is \(6\).
The jackknife distribution of the median is \(\{7,7,6,6\}\), centered at \((7+7+6+6)/4 = 6.5\).
Code
x0_hat <- c(1, 6, 7, 22)
jackknife_meds <- rep(NA, length(x0_hat))
for(i in seq_along(jackknife_meds)){
jackknife_meds[i] <- median(x0_hat[-i])
}
jackknife_meds
## [1] 7 7 6 6
mean(jackknife_meds)
## [1] 6.5
Code
m_hat <- mean(x_hat)
# Jackknife Estimates
n <- length(x_hat)
jack_means <- rep(NA, n)
for(i in seq_along(jack_means)){
x_jack <- x_hat[-i]
m_i_hat <- mean(x_jack)
jack_means[i] <- m_i_hat
}
hist(jack_means, breaks=25,
border=NA, freq=FALSE,
main=NA, xlab=expression(hat(m)^group('(', -i, ')')))
abline(v=m_hat, col=rgb(1, 0, 0, .8), lty=2)
Bootstrap Distribution
The jackknife uses only \(n\) leave-one-out subsamples, which tends to underestimate variability; a more flexible approach builds many resamples of full size \(n\) by allowing duplicates.
The bootstrap resamples \(n\) observations from the original data with replacement, computes the statistic on each resample, and uses the spread of those values to approximate the sampling distribution.
The bootstrap is useful when no closed-form expression for the sampling distribution is available (common for medians, quantiles, ratios, or any custom statistic), because the recipe is identical regardless of the statistic computed on each resample. Each bootstrap sample \(b=1\dots B\) produces one value of the statistic, and we typically repeat the resampling many times (e.g., \(B=9999\)) to estimate the sampling distribution.
Given the sample \(\{1,6,7,22\}\), compute the bootstrap estimate of the median with \(B=8\) resamples. Each resample draws \(n=4\) values from \(\{1,6,7,22\}\) with replacement, then takes the median. For instance, one resample might be \(\{6,6,7,22\}\): sorted, the two middle values are \(6\) and \(7\), so the median is \((6+7)/2=6.5\). Another might be \(\{1,1,7,7\}\), with median \((1+7)/2=4\). Repeating this \(B=8\) times gives the bootstrap distribution of the median.
Code
set.seed(1)
x0_hat <- c(1, 6, 7, 22)
boot_medians <- rep(NA, 8)
for(b in seq_along(boot_medians)){
x_boot <- sample(x0_hat, replace=TRUE)
boot_medians[b] <- median(x_boot)
}
boot_medians
## [1] 4.0 6.5 6.5 1.0 6.0 1.0 1.0 6.0
Code
# Bootstrap estimates
boot_means <- rep(NA, 9999)
for(b in seq_along(boot_means)){
x_boot <- sample(x_hat, replace=TRUE) # c.f. jackknife
m_b_hat <- mean(x_boot)
boot_means[b] <-m_b_hat
}
hist(boot_means, breaks=25,
border=NA, freq=FALSE,
main=NA, xlab=expression(hat(m)^group('(', b, ')')))
abline(v=m_hat, col=rgb(1, 0, 0, .8), lty=2)
Why does this work? The sample: \(\{\hat{x}_{1}, \hat{x}_{2}, ... \hat{x}_{n}\}\) is drawn from a CDF \(F\). Each bootstrap sample: \(\{\hat{x}_{1}^{(b)}, \hat{x}_{2}^{(b)}, ... \hat{x}_{n}^{(b)}\}\) is drawn from the ECDF \(\hat{F}\). With \(\hat{F} \approx F\) by the Fundamental-Theorem above, each bootstrap sample is approximately a random sample. So when we compute a statistic on each bootstrap sample, we approximate the sampling distribution of the statistic.
Here is an intuitive example with Bernoulli random variables (unfair coin flips)
Code
# theoretical probabilities
x <- c(0, 1)
x_probs <- c(1/4, 3/4)
# sample draws
coin_hat <- sample(x, prob=x_probs, 1000, replace=TRUE)
F_hat <- ecdf(coin_hat)
x_probs_boot <- c(F_hat(0), 1-F_hat(0))
x_probs_boot # approximately the theoretical value
## [1] 0.277 0.723
coin_boot <- sample(x, prob=x_probs_boot, 999, replace=TRUE)
# any draw from here is almost the same as the original process
Comparison
Note that both Jackknife and Bootstrap resampling methods provide imperfect estimates, and can give different numbers.
- Jackknife resamples are often less variable than they should be and sample \(n-1\) instead of \(n\).
- Bootstrap resamples have the right \(n\) but often have duplicated data.
Standard Errors
Having an entire sampling distribution is conceptually clear but unwieldy in practice; we usually compress it to a single number describing its spread.
The standard error of a statistic is the standard deviation of its sampling distribution. For the sample mean, denote it \(SE(M)\). The standard error is distinct from the sample standard deviation \(\hat{s}\), which measures spread of observations within one sample.
The standard error is useful for reporting how precise an estimate is in a single number: a small \(SE\) means the statistic would change little across samples, a large \(SE\) means it could swing widely. The two “standard” quantities are easy to confuse but answer different questions:
- sample standard deviation \(\hat{s}\): variability of individual observations within a single sample.
- standard error: variability of a statistic across repeated samples.
For a bootstrap example
- bootstrapped estimate: \(\hat{m}^{(b)}= \frac{1}{n} \sum_{i=1}^{n} \hat{x}_{i}^{(b)}\), for resampled data \(\hat{x}_{i}^{(b)}\)
- mean of the bootstrap distribution: \(\hat{m}^{\text{boot}}= \frac{1}{B} \sum_{b} \hat{m}^{(b)}\).
- standard deviation of the bootstrap distribution: \(\hat{SE}^{\text{boot}}= \sqrt{ \frac{1}{B} \sum_{b=1}^{B} \left[\hat{m}^{(b)} - \hat{m}^{\text{boot}} \right]^2}\).
Here, the superscript \((b)\) identifies one bootstrap replicate, while \(\text{boot}\) labels summaries computed across the replicates.
Using the bootstrap distribution above, with many replicates, we can compute this easily as
Code
sd(boot_means) # standard error estimate for the mean
## [1] 0.5991904
sd(x_hat) # standard deviation
## [1] 4.35551
We can also calculate this explicitly for a small number of replicates. Suppose we have five bootstrap estimates of the sample mean: \(\{3,5,6,7,9\}\). Then
\[\begin{aligned}
\hat{m}^{\text{boot}} &= \frac{3 + 5 + 6 + 7 + 9}{5} = 6 \\
\hat{SE}^{\text{boot}} &= \sqrt{\frac{1}{5}\left[(3-6)^2 + (5-6)^2 + (6-6)^2 + (7-6)^2 + (9-6)^2\right]} \\
&= \sqrt{\frac{1}{5}\left[9 + 1 + 0 + 1 + 9\right]} = \sqrt{\frac{20}{5}} = 2
\end{aligned}\]
For a jackknife example, we have
- jackknifed estimates: \(\hat{m}^{(-i)}=\frac{1}{n-1} \sum_{j \neq i}^{n} \hat{x}_{j}\).
- mean of the jackknife distribution: \(\hat{m}^{\text{jack}}=\frac{1}{n} \sum_{i}^{n} \hat{m}^{(-i)}\).
- standard deviation of the jackknife distribution: \(\hat{SE}^{\text{jack}} = \sqrt{ \frac{n-1}{n} \sum_{i}^{n} \left[\hat{m}^{(-i)} - \hat{m}^{\text{jack}} \right]^2 } \approx \hat{sd}(\hat{m}^{(-i)}) \sqrt{n}\).
The superscript \((-i)\) marks the sample with observation \(i\) omitted.
Code
sd(jack_means)*sqrt(n) # standard error estimate for the mean
## [1] 0.6285328
sd(boot_means) # standard error estimate for the mean
## [1] 0.5991904
We modify the sd calculation with a correction, \(\sqrt{n}\), because leave-one-out estimates barely move (only one point changes out of \(n\)). Their spread is therefore of order \(1/n\) smaller than the real sampling spread, and the correction puts us back on the right scale.
The wage population lets us check whether the bootstrap works. Take one sample of \(50\) workers, and pretend it is all we observed. Its bootstrap standard error can then be compared with the true standard error: the standard deviation of the \(500\) sample means wage_means drawn from the population in Sampling Distributions.
Code
# One sample of 50 workers, as if it were all we observed
x_sim <- sample(population_wages, 50, replace=FALSE)
# Bootstrap standard error, from that one sample
boot_means_wage <- rep(NA, 9999)
for(b in seq_along(boot_means_wage)){
x_boot <- sample(x_sim, replace=TRUE)
m_b_hat <- mean(x_boot)
boot_means_wage[b] <- m_b_hat
}
round(sd(boot_means_wage), 3)
## [1] 0.546
# True standard error, from many samples of the population
round(sd(wage_means), 3)
## [1] 0.453
The bootstrap used only one sample, so its answer is itself noisy. It lands in the same range as the true standard error, but not exactly on it. With skewed data like wages, one sample may include a few very high earners or none at all, which moves the bootstrap estimate. Run the chunk again to see how much the bootstrap estimate itself changes from one sample to the next.
Also note that each additional data point you have provides more information, which ultimately decreases the standard error of your estimates. This is why statisticians will often recommend that you to get more data. However, the improvement in the standard error increases at a diminishing rate. In economics, this is known as diminishing returns and why economists may recommend you do not get more data.
Code
n_boot <- 999 # number of bootstrap samples
sample_sizes <- seq(1, length(x_hat), by=1) # different resample sizes
## For each sample size, compute the bootstrap SE
se_by_n <- rep(NA, length(sample_sizes))
for(n in seq_along(sample_sizes)){
boot_means_n <- rep(NA, n_boot)
for(b in seq(1, n_boot)){
x_boot <- sample(x_hat, size=n, replace=TRUE)
m_b_hat <- mean(x_boot) # statistic of interest
boot_means_n[b] <- m_b_hat
}
se_n <- sd(boot_means_n) # How much the statistic varies across samples
se_by_n[n] <- se_n
}
plot(sample_sizes, se_by_n, pch=16, col=grey(0, 0.5),
ylab='standard error', xlab='sample size', main=NA)
Generalization
The above procedure works for many different statistics
Code
med_hat <- quantile(x_hat, prob=0.5)
# Bootstrap estimates
boot_medians <- rep(NA, 9999)
for(b in seq_along(boot_medians)){
x_boot <- sample(x_hat, replace=TRUE) # c.f. jackknife
med_b_hat <- quantile(x_boot, prob=0.5)
boot_medians[b] <- med_b_hat
}
hist(boot_medians, breaks=25,
border=NA, freq=FALSE,
main=NA, xlab=expression(Med[b]))
abline(v=med_hat, col=rgb(1, 0, 0, .8), lty=2)
This means we can compute bootstrap SEs for other statistics, too.
Taking the bootstrap distribution of the median, for example, we have
- bootstrapped estimate: \(\tilde{m}_{b}^{\text{boot}}= \text{Med}(\hat{x}_{i}^{(b)})\), for resampled data \(\hat{x}_{i}^{(b)}\)
- mean of the bootstrap distribution: \(\hat{m}^{\text{boot}}= \frac{1}{B} \sum_{b} \tilde{m}_{b}^{\text{boot}}\).
- standard deviation of the bootstrap distribution: \(\hat{SE}^{\text{boot}}= \sqrt{ \frac{1}{B} \sum_{b=1}^{B} \left[\hat{m}^{(b)} - \hat{m}^{\text{boot}} \right]^2}\).
Code
# Standard error estimate for the median
sd(boot_medians)
## [1] 0.8657553
Exercises
Comment the script you wrote for this chapter, then restart R and check that it runs from a clean session, then check the script with AI as explained in Working with AI. Write three sentences from memory on the main statistical idea of this chapter, and ask the assistant what is wrong, vague, or missing. Finish with your own questions about whatever you found hardest.
In your own words, explain the difference between the standard deviation of a sample and the standard error of the sample mean. Why does the standard error decrease as \(n\) grows, while the standard deviation does not necessarily change?
Consider a population of four students with ages \(\{18, 20, 22, 24\}\). List all possible simple random samples of size \(n=2\) (without replacement). Compute the sample mean for each. Then compute the mean and standard deviation of those sample means. Verify that the mean of the sample means equals the population mean.
Load USArrests in R and extract the UrbanPop column. Write a bootstrap loop with \(B=5000\) resamples to estimate the standard error of the median. That is, for each resample draw n observations with replacement, compute the median, store it, and then compute sd of the stored medians. Compare this bootstrap standard error to the bootstrap standard error of the mean from the same data.
Recall
This chapter introduced sampling distributions (how a statistic varies across samples), the Law of Large Numbers and Central Limit Theorem that govern the sample mean, and the jackknife and bootstrap methods that approximate sampling variability from a single dataset, summarized by the standard error. The four-student example tied these ideas to concrete numbers: from ages \(\{18, 20, 22, 24\}\) there are six possible samples of size \(n=2\) giving six sample means \(\{19, 20, 21, 21, 22, 23\}\) centered on the population mean \(21\), and the histogram of those six values is the sampling distribution of \(\hat{m}\).
Hesterberg, Tim C. 2015.
“What Teachers Should Know about the Bootstrap: Resampling in the Undergraduate Statistics Curriculum.” The American Statistician 69 (4): 371–86.
https://doi.org/10.1080/00031305.2015.1089789.