Search

Search papers and lessons

Forward Stepwise Selection

Level 2 · 9 min read

Forward stepwise selection builds a regression one predictor at a time, always adding the one that helps most, and stops when a model-selection score says more predictors no longer help.

On this page

  • Colour = topic mix
  • Probability
  • Learning basics
  • Calculus
  • Linear algebra
  • Discrete math
  • Inference
  • Sign in to see your progress

TL;DR

Forward stepwise selection starts with a model that has no predictors, only an intercept. At each step it tries every predictor not yet in the model, adds the one that reduces the residual error the most, and repeats. You stop when a score such as AIC, CpC_p or validation error says the next addition is not worth it. The result is a nested sequence of models, each one a little bigger than the last.

Where you'll see it: choosing which of dozens of measured variables to keep in a regression, such as which genes, sensors or survey questions explain an outcome, when the full set of candidates is too large to fit all at once.

Intuition

Imagine you are packing a small bag from a shelf of forty items, and you want the bag to hold only what matters. You could try every combination, but there are about a trillion of them for forty items. Instead you grab the single most useful item first, then, with that in the bag, grab the next most useful one, and so on. You stop when the next item no longer earns its space.

That is forward stepwise selection. It is greedy: each step makes the best local choice and never goes back to revisit an earlier one. It is not guaranteed to find the best subset, but it finds a good one at a tiny fraction of the cost of checking every subset.

The cost difference matters. With pp candidate predictors, checking every subset means fitting 2p2^p models. Forward stepwise fits at most p(p+1)/2p(p+1)/2. For p=40p = 40 that is 820 fits against more than a trillion.

flowchart TD
  A[Start: intercept only] --> B[Try each unused predictor]
  B --> C[Add the one with the lowest residual error]
  C --> D{Score still improving?}
  D -- yes --> B
  D -- no --> E[Keep the model with the best score]

The math

Let SS be the set of predictors currently in the model and RSS(S)\mathrm{RSS}(S) the residual sum of squares (the sum of squared differences between observed and fitted responses) of the least-squares fit on SS, with the intercept always included. The rule at each step is

j∗=arg⁡min⁡j∉SRSS(S∪{j}),S←S∪{j∗}.j^\ast = \arg\min_{j \notin S} \mathrm{RSS}(S \cup \{j\}), \qquad S \leftarrow S \cup \{j^\ast\}.

Adding a predictor never increases RSS\mathrm{RSS}, so the training error alone would always say to add everything. The stopping rule is what does the work, and it is a choice from Model Selection: a held-out error, AIC, or Mallows' CpC_p.

When the predictors are uncorrelated

The arithmetic simplifies when the centred predictors are mutually orthogonal, xi⊤xj=0x_i^\top x_j = 0 for i≠ji \neq j. Then adding predictor jj to a model that already contains others reduces the residual sum of squares by exactly

Δj=(xj⊤y)2xj⊤xj.\Delta_j = \frac{(x_j^\top y)^2}{x_j^\top x_j}.

Each predictor's contribution is independent of which others are already in, so the greedy path is simply the predictors ranked by Δj\Delta_j. Real predictors are rarely orthogonal, but the toy case shows the mechanism cleanly.

Stopping with CpC_p

For least squares, the full model with pfullp_{\text{full}} coefficients gives the noise estimate σ^2=RSSfull/(n−pfull)\hat\sigma^2 = \mathrm{RSS}_{\text{full}} / (n - p_{\text{full}}), and a submodel with pp coefficients has

Cp=RSSpσ^2−n+2p.C_p = \frac{\mathrm{RSS}_p}{\hat\sigma^2} - n + 2p.

Along the greedy path, pick the step with the smallest CpC_p. This is the standard recipe, and Mallows Cp explains why a no-bias submodel has CpC_p near pp.

Algorithm
Forward stepwise selection
  1. Start with S=∅S = \varnothing (intercept only). Record RSS(S)\mathrm{RSS}(S).
  2. For each predictor j∉Sj \notin S, fit the regression on S∪{j}S \cup \{j\} and record its RSS.
  3. Add the predictor with the smallest RSS to SS.
  4. Record the score (AIC, CpC_p or validation error) for the new model.
  5. Repeat steps 2 to 4 until all predictors are in or the score stops improving for a set number of steps.
  6. Return the model along the path with the best score. Check it on a test set that did not drive the path.

Worked examples

Example
A toy case with uncorrelated predictors

We have n=50n = 50 observations and three centred predictors x1x_1, x2x_2, x3x_3 that are mutually orthogonal. The total sum of squares of the centred response is RSS(∅)=100\mathrm{RSS}(\varnothing) = 100. Adding each predictor to the empty model reduces the RSS by:

  • x1x_1: reduction 60, so RSS=40\mathrm{RSS} = 40.
  • x2x_2: reduction 25, so RSS=15\mathrm{RSS} = 15.
  • x3x_3: reduction 0.5, so RSS=14.5\mathrm{RSS} = 14.5.

The greedy path is x1x_1, then x2x_2, then x3x_3.

  1. Full model (all three predictors, pfull=4p_{\text{full}} = 4 with the intercept): σ^2=14.5/(50−4)=0.3152\hat\sigma^2 = 14.5 / (50 - 4) = 0.3152.
  2. Model with x1x_1 and x2x_2 (p=3p = 3): C3=15/0.3152−50+6=47.59−44=3.59C_3 = 15 / 0.3152 - 50 + 6 = 47.59 - 44 = 3.59.
  3. Full model: C4=14.5/0.3152−50+8=46−42=4C_4 = 14.5 / 0.3152 - 50 + 8 = 46 - 42 = 4. This is always exactly pp for the full model.
  4. Model with x1x_1 alone (p=2p = 2): C2=40/0.3152−50+4=126.9−46=80.9C_2 = 40 / 0.3152 - 50 + 4 = 126.9 - 46 = 80.9, far too high.
  5. The smallest CpC_p is at p=3p = 3, so the selected model is {x1,x2}\{x_1, x_2\}.

Interpretation: x3x_3 reduces the residual error by only 0.5, which is about 1.6 units of CpC_p worth of fit for two units of penalty, so it does not earn its place. Dropping it costs nothing measurable on this data.

Example
A realistic case: choosing among many candidate predictors

A dataset has 30 candidate predictors for a regression of hospital readmission times (not for clinical use). Forward stepwise selection adds predictors one at a time. Validation error on a held-out set falls for the first six additions, then rises slightly on the seventh and eighth.

  1. The validation error after six predictors is the lowest on the path.
  2. Predictors seven and eight each increased the fit on the training data but worsened validation error, the mark of overfitting.
  3. The team stops at six predictors and refits on training plus validation data.
  4. They report performance on a test set that was not used to build the path. Had they used the training error, the path would have kept growing until all thirty predictors were in, and the final model would have looked far better than it performs.

Interpretation: the stopping rule, not the greedy search, is what controls overfitting. The search only orders the predictors.

Tradeoffs and failure modes

SituationWhat goes wrongBetter choice
Two predictors are useful only togetherGreedy never adds either, since each alone looks weakExhaustive search over small subsets, or regularised regression
Predictors are highly correlatedGreedy picks one of a near-duplicate pair at random, and the choice is unstableLasso Regression, or ridge with tuned strength
The path is used for inferenceReported p-values and confidence intervals ignore the selection step and look too narrowSample splitting, or treat the selected model as exploratory
Few candidatesExhaustive search is affordable and finds the true best subsetBest-subset selection
Many candidatesStepwise is fast but can overfit if the stopping rule is looseValidation-based stopping, or L2 Regularization

Forward stepwise is a search method, not a model-selection criterion. Its output is only as good as the score you stop with.

History

Stepwise procedures appeared in applied regression in the 1950s and 1960s, when computing an exact best subset was impractical and fitting even a few hundred regressions by hand or on early machines was slow. Greedy addition and deletion were the practical answer, and they spread through statistics textbooks and software because they were easy to run. Later work showed that the p-values reported after stepwise selection are misleading, because the procedure searched many models and then reported the winner as if it had been fixed in advance. Modern practice treats stepwise as a heuristic for ordering candidates and relies on held-out data or penalised methods to decide how many to keep.

Check yourself

QuestionRecall: why does forward stepwise need a stopping rule at all, when adding a predictor never increases the training residual sum of squares?Show answer

Because the training RSS can only fall or stay the same as predictors are added, so it always favours the largest model. A stopping rule such as validation error, AIC or CpC_p penalises complexity or measures performance on data not used for fitting, which lets the procedure stop before it starts fitting noise.

QuestionComputation: with RSS(∅)=80\mathrm{RSS}(\varnothing) = 80, adding x1x_1 alone gives RSS 30, adding x2x_2 alone gives RSS 45, and adding x2x_2 to the model with x1x_1 gives RSS 26. Which predictor does the greedy rule add first, and what is each step's reduction?Show answer

The first step adds x1x_1, because its RSS of 30 is lower than 45; the reduction is 80−30=5080 - 30 = 50. The second step adds x2x_2 and reduces RSS by 30−26=430 - 26 = 4. So x1x_1 is far more useful, and x2x_2 adds a small amount of fit. Whether it is worth keeping depends on whether 4/σ^24 / \hat\sigma^2 exceeds the CpC_p penalty of 2 for one more coefficient.

QuestionWhy: a greedy path misses a pair of predictors that together explain the response well but individually explain little. Explain the mechanism.Show answer

Greedy selection scores each candidate by how much it helps the current model. A predictor that is only useful in combination with another has a small marginal contribution when the partner is absent, so it ranks below predictors that look good alone. Once one partner is in, the other may become useful, but greedy may have already filled the model with other candidates, or the stopping rule may have halted first. The search never evaluates the pair jointly, which is why exhaustive or regularised methods can find what the greedy path misses.

QuestionApplied: a researcher uses forward stepwise on 200 candidate features, selects 12, then reports that the selected features each have p-values below 0.01. Why is that report misleading?Show answer

The p-values assume the 12 features were fixed before looking at the data. Stepwise chose them from 200 candidates using the same data, so the selection itself inflates apparent significance; some selected features would look significant under pure noise. The honest analysis either uses a separate dataset for the selection and the testing, or reports the result as exploratory and checks it on new data.

QuestionRecall: what does a CpC_p value close to pp tell you about a submodel?Show answer

It suggests the submodel has little bias: on average its fit is about what the full model would give, after accounting for the penalty of 2 per coefficient. A CpC_p well above pp means the submodel leaves out useful terms and is biased.

Sources