Lesson 5.3 · 9 min read · about 50 min to master at a very slow pace

Best Subset and Stepwise Selection

Best subset selection tries every combination of predictors; forward and backward stepwise approximate it greedily. The algorithms step by step, how many models each fits, and a worked forward-stepwise example.

Not startedJump to quiz

The L0 penalty

Subset selection penalises the number of non-zero coefficients:

Best subset selection

  1. For each size : fit all models with exactly predictors and keep the one with the lowest training RSS. Call it .
  2. Choose among using a measure that accounts for complexity: validation or CV error, adjusted , AIC/BIC. Not training RSS, which always favours .

Benefit: directly implements parsimony and finds the best subset of each size. Problem: it fits models. : 1,024. : over a million. : hopeless.

Greedy approximations

Forward stepwiseBackward stepwise
  1. Start with (mean only).
  2. For : try adding each remaining predictor; keep the one that most improves the fit, giving .
  3. Choose the best of by validation/CV.
  1. Start with the full model .
  2. For : try removing each predictor; keep the removal that hurts least, giving .
  3. Choose the best size by validation/CV.

Models fitted: . For : 56 instead of 1,024; for : 211 instead of 1,048,576.

110¹10²10³10⁴10⁵10⁶15101520number of candidate predictors pmodels fitted (log scale)
Best subset: 2ᵖStepwise: 1 + p(p+1)/2
Best subset grows exponentially; stepwise grows only quadratically. In one benchmark, best subset took 163 s for 9 variables and was infeasible at 10, while stepwise handled 64 in milliseconds.

Trade-off: stepwise isn't guaranteed to find the best subset. Once forward stepwise adds a variable, it never removes it, even if a later combination would be better without it.

Worked example: forward stepwise by hand

Three candidate predictors A, B, C. TSS .

  • Step 0: (mean only): RSS .
  • Step 1: one-variable models: A 60, B 45, C 80. Keep B: .
  • Step 2: add one more to B: A+B 30, B+C 40. Keep .
  • Step 3: : RSS 28.

Training RSS keeps falling (100, 45, 30, 28), as it always does. Now compare by validation RMSE: 10.0, 7.0, 5.5, 5.8. Choose .

Models fitted: , versus for best subset. The gap grows explosively with .

Cheatsheet

Forward stepwise

Start . Repeatedly add the predictor that most lowers RSS → . Choose the size by validation/CV.

Best subset

For each , best of models by RSS → . Pick among by CV / adj. / AIC / BIC. Cost .

Model counts

Best subset: . Stepwise: .

See every cheatsheet by category →

Check your understanding

Score 75% or more to master this lesson and earn XP. Answer first, then check.

Q1calculateHow many models does best subset selection fit with candidate predictors, including the empty model?
Q2calculateHow many models does forward stepwise selection fit with , including the empty model?
Q3conceptBest subset gives the best model of each size, . How should you choose among them?
Q4applyForward stepwise picks B first, then adds A. But the best two-variable model is actually {A, C}. How is that possible?
Q5conceptWhich penalty corresponds to subset selection?