Investigation: Resampling methods#
A bootstrap approximates sampling variation; a permutation procedure constructs a reference distribution under a specified null and exchangeability assumptions. Resampling with or without replacement alone does not determine a scientifically valid test.
Question. Bootstrap participant-level mean reaction time, construct an interval, and use a two-sided permutation test on independent simulated groups with known null structure.
Before running: write the result you expect and what would challenge it. Data are simulated unless explicitly identified otherwise.
# Open this notebook from book/notebooks (or book/exercises).
course_path <- if (file.exists("../R/course.R")) "../R/course.R" else "book/R/course.R"
stopifnot(file.exists(course_path))
source(course_path)
set.seed(432)
d <- simulate_trials()
subject_mean <- aggregate(rt~subject,d,mean)$rt
B <- 1000
boot_mean <- replicate(B,mean(sample(subject_mean,length(subject_mean),replace=TRUE)))
c(estimate_ms=mean(subject_mean),quantile(boot_mean,c(.025,.975)))
hist(boot_mean,main="Participant bootstrap",xlab="Mean reaction time (ms)")
Which population does the interval address?#
Resampling subject means targets new subjects conditional on the observed item set. A population of new subjects and new items needs a design-aware method that reflects both sources of variation. Percentile intervals are an introductory approximation; inspect bias and coverage in simulations.
# Independent, exchangeable observations under the prespecified null.
x <- rnorm(30); y <- rnorm(30,.5)
values <- c(x,y)
group <- rep(c(0,1),each=30)
statistic <- function(g) mean(values[g==1])-mean(values[g==0])
observed <- statistic(group)
null <- replicate(B,statistic(sample(group)))
p_two_sided <- (1+sum(abs(null)>=abs(observed)))/(B+1)
c(difference=observed,permutation_p=p_two_sided)
hist(null,main="Null distribution",xlab="Difference in means")
abline(v=c(-abs(observed),abs(observed)),col="red",lty=2)
stopifnot(p_two_sided>0,p_two_sided<=1)
The +1 correction avoids claiming probability zero from a finite random sample of permutations. Define the null and tails before seeing results. A valid paired test would shuffle within pairs or use an appropriate sign-flip scheme, not freely permute every trial. Accuracy is not a correlation coefficient, and bootstrapping training accuracy does not repair its optimism.
Interpret and verify#
Name the resampling unit, statistic, null, and exchangeability restriction. Distinguish uncertainty in apparent accuracy from uncertainty in held-out performance.
Failure to consider: Resampling trials as if they were independent ignores participant clustering. Arbitrarily permuting one correlated predictor need not implement a valid conditional null.
Report the target, the numerical result with its units, the relevant uncertainty, and one limitation. Distinguish what is established by the simulation from what would require real data.
AI audit: critique the saved faulty claim in the companion exercise before using any live assistant. If you use AI, record the relevant suggestion, your independent check, and the final decision.
Keep the validation target fixed and use these uncertainty tools when interpreting indirect associations and study designs.
sessionInfo()
R version 4.2.1 (2022-06-23)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS 15.7.7
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib
locale:
[1] C.UTF-8/C.UTF-8/C.UTF-8/C/C.UTF-8/C.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] fansi_1.0.3 crayon_1.5.1 digest_0.6.29
[4] utf8_1.2.2 IRdisplay_1.1 repr_1.1.4
[7] lifecycle_1.0.1 jsonlite_1.8.0 evaluate_0.15
[10] pillar_1.8.0 rlang_1.0.6 cli_3.3.0
[13] uuid_1.1-0 vctrs_0.4.1 IRkernel_1.3.0.9000
[16] tools_4.2.1 glue_1.6.2 fastmap_1.1.0
[19] compiler_4.2.1 base64enc_0.1-3 pbdZMQ_0.3-7
[22] htmltools_0.5.3
Attribution#
Notebook authored by Ven Popov and edited by Krista Bond, Charles Wu, Patience Stevens, and Amy Sentis.
Revised investigation: 2026 course revision. Original authorship is retained; examples and explanatory text have been updated.