Skip to main content

Try it in R: Interactive Simulation Labs

Hands-on R simulation labs from the lexicon, running in your browser with webR.
Published

November 11, 2026

Modified

November 15, 2026

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.

TipThe four-step fake-data workflow in R

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:

  1. 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.
  2. Simulate one fake dataset. Draw random data from that world (rnorm(), rbinom()) and plot it to see what a single study looks like.
  3. 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.
  4. Repeat across many worlds. Wrap the single study in replicate() or sapply() to measure long-run exaggeration (Type M error), wrong-sign claims (Type S error), or out-of-sample fit.
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
No matching items
Back to top