Try it in R: Interactive Simulation Labs
Every entry in the lexicon already includes a base-R version of its simulation under Show simulation code. These four Try it in R pages go a step further: they are hands-on labs that run R directly in your browser with webR (R compiled to WebAssembly), with nothing to install.
Each lab breaks a core idea from the Start here tour into small, editable steps, gives you concrete Predict → Modify → Observe experiments to try on the code, and ends with a short Your turn coding exercise with a hint and a worked solution.
Throughout his books and blog posts, Andrew Gelman advocates fake-data simulation as the best way to understand what a statistical method actually does before trusting it on real data. Every lab here follows the same four-step pattern in base R:
- Fix the known truth. Choose numbers for the true parameters (
theta <- 0.2,sigma <- 0.5,tau <- 0.1) so you know the right answer in advance. - Simulate one fake dataset. Draw random data from that world (
rnorm(),rbinom()) and plot it to see what a single study looks like. - Apply the estimator or filter. Run the rule under study (
abs(est) > 1.96 * se, regression, partial pooling) and compare its answer with the known truth. - Repeat across many worlds. Wrap the single study in
replicate()orsapply()to measure long-run exaggeration (Type M error), wrong-sign claims (Type S error), or out-of-sample fit.
Labs in recommended order
Start with Lab 1 and use the links at the bottom of each page (or the sidebar) to step through the sequence. The first lab you open downloads R into your browser (~30 MB), which takes up to a minute once and is cached after that.
| Lab | Topic | What you will build and test | Theme · Level |
|---|---|---|---|
| 1 | Try it in R: The Statistical Significance Filter | Simulate Type M (exaggeration) and Type S (wrong-sign) errors when noisy studies are filtered by p < 0.05. | Noise and evidence, Intermediate |
| 2 | Try it in R: 16 | Verify the 2 × 2 variance algebra showing why an interaction half as large as a main effect requires 16 times the sample size. | Noise and evidence, Intermediate |
| 3 | Try it in R: Multiple Comparisons | Build an empirical-Bayes multilevel shrinkage estimator in base R and compare its false discoveries and accuracy against no pooling. | Building and checking models, Intermediate |
| 4 | Try it in R: Mathematical Simplicity | Fit Gelman and Nolan’s geometric golf-putting model by maximum likelihood in base R and compare its extrapolation with logistic regression. | Building and checking models, Intermediate |