Skip to main content

Shrinkage & Cross-Validation

Week 6. Last week we chose predictors: each one in or out. This week we keep them and shrink the estimates toward something safe instead. Estimates from small samples are extreme partly by luck; pulling them in trades a little bias for much less variance. Mixed models do this to group effects (farms, blocks, years), and ridge and the lasso do it to regression slopes. How much to shrink is chosen by cross-validation: how well the model predicts data it did not see. Then the traps in cross-validation, and how to judge a model that predicts yes or no.

Problem Set 6: Shrinkage and cross-validation — due Monday, November 9, 1:10 PM. Submit on Canvas.

Answer key: Problem Set 6 key

Data: farm_plots.csv (112 plots on 12 farms: nitrogen rate, yield, a cover crop chosen per farm, and rust; simulated, so we know the truth), cover_crop.csv (the Week 5 cover-crop farms: 50 farms, 15 candidate predictors, simulated), pistacia_niche.csv (the Week 5 wild pistachio records), and for Problem Set 6: almond_orchards.csv (simulated) and wild_wheat.csv (the Problem Set 2 and 5 data)


This week — Shrinkage and cross-validation

One deck across both lectures, in six sections. First lecture: Shrinkage (the top-ranked farm had 2 plots and was lucky; shrinking each estimate toward the average; replaying the season 500 times to see that it helps), Mixed models (plots grouped on farms: farm as fixed vs random, partial pooling and the shrinkage weight, lmer(), p-values with lmerTest, a treatment chosen per farm and why lm() gets it wrong, random slopes, glmer() for yes/no and counts, checks, fixed or random), and Ridge and lasso (a random effect is a price on big offsets; ridge puts the same price on slopes; scaling; coefficient paths; the lasso sets slopes to exactly zero; lasso vs stepwise on resampled data; elastic net; glmnet()). Second lecture: Cross-validation (choosing λ: one split vs k-fold vs leave-one-out, cv.glmnet(), lambda.min vs lambda.1se, comparing models by CV), CV traps (selection outside the folds; grouped rows and leave-one-group-out), and Classification (thresholds, the confusion matrix, sensitivity and specificity, ROC curves and AUC; RMSE, MAE and R² for numeric responses).

  • Slides (PDF) — one deck for the week (the live-poll links are clickable)
  • Live coding in class — the short scripts we open in RStudio and run together, line by line: Week6_live_1_shrink.R (farms: lm vs lmer, shrinkage, lmerTest, cover crops, random slopes, glmer; ridge and lasso with glmnet) and Week6_live_2_cv.R (cross-validation, its traps, and classification).
  • The week’s code, with output — one page for the whole week, in the same order as the slides, with the code behind every figure and number in the lecture and the output under each block. Every block is labelled Core (the analysis you would run on your own data), Handy (useful, not essential), or Demo (simulations and by-hand sums for the lecture; folded). Start with the Core blocks for the problem set.
  • Week6_Shrinkage_CV.R — the same code as a plain script, each block tagged [CORE], [HANDY], or [DEMO]. It reads the data straight from the course website, so there is no working directory to set.

Fri Nov 6 — Making publication-ready figures


Learning objectives

  • Explain why the most extreme estimates come from the smallest samples, and what shrinking toward the average buys
  • Decide whether a grouping factor is fixed or random, fit lmer() and glmer() models, and read the two variance components
  • Explain partial pooling and the shrinkage weight σ²_group / (σ²_group + σ²/n)
  • Get p-values for a mixed model with lmerTest, and recognize pseudoreplication when a treatment is applied to whole groups
  • Test a random slope, and rescale a predictor when lmer() fails to converge
  • Fit ridge, lasso and elastic-net models with glmnet() on scaled predictors, and read coefficient paths
  • Choose λ by k-fold cross-validation (cv.glmnet(), lambda.min vs lambda.1se), and compare models by cross-validated error
  • Keep every step that learns from the response inside the folds, and fold by group when rows are grouped
  • Evaluate a classifier with a confusion matrix, sensitivity, specificity, and ROC/AUC

Also useful

  • Packages this week: lme4 and lmerTest (mixed models), glmnet (ridge, lasso, elastic net, cv.glmnet()), pROC (ROC curves and AUC), MASS (stepAIC(), for the comparison with Week 5), ggplot2
  • Textbook: ISLR (2nd edition) chapter 5, Resampling Methods (section 5.1, cross-validation), and chapter 6, Linear Model Selection and Regularization (section 6.2, shrinkage methods: ridge and the lasso)