| Variables | \(p\) | \(R^2\) | \(\bar R^2\) | \(MS_{Res}\) | \(C_p\) | AIC | BIC | PRESS |
|---|---|---|---|---|---|---|---|---|
| N | 2 | 0.310 | 0.286 | 7.055 | 8.645 | 147.679 | 151.882 | 243.571 |
| N, P | 3 | 0.409 | 0.365 | 6.272 | 5.702 | 145.060 | 150.665 | 237.112 |
| N, P, K | 4 | 0.487 | 0.427 | 5.656 | 3.793 | 142.826 | 149.832 | 241.988 |
| N, P, K, OC | 5 | 0.520 | 0.443 | 5.498 | 4.105 | 142.796 | 151.203 | 240.121 |
| N, P, K, OC, rain | 6 | 0.540 | 0.444 | 5.494 | 5.126 | 143.552 | 153.361 | 238.509 |
| N, P, K, OC, rain, Zn | 7 | 0.542 | 0.423 | 5.702 | 7.000 | 145.389 | 156.598 | 254.715 |
15 Selection of Variables and Model Building
In many studies a large number of candidate regressors is available: a yield forecasting study may record a dozen weather variables at several crop stages, and a farm survey may record dozens of inputs. Including all of them is rarely wise. The problem of variable selection is to find a subset that explains the response adequately while remaining simple, stable and interpretable (Montgomery, Peck, and Vining 2021; Draper and Smith 1998; Chatterjee and Hadi 2012).
15.1 Why not include every variable?
Let the true model be \(\mathbf y=\mathbf X_1\boldsymbol\beta_1+\mathbf X_2\boldsymbol\beta_2+\boldsymbol\varepsilon\).
- Omitting relevant variables (fitting only \(\mathbf X_1\)): \(E(\hat{\boldsymbol\beta}_1)=\boldsymbol\beta_1+(\mathbf X_1'\mathbf X_1)^{-1}\mathbf X_1'\mathbf X_2\boldsymbol\beta_2\), so the estimates are biased unless \(\mathbf X_1'\mathbf X_2=\mathbf 0\), and \(MS_{Res}\) overestimates \(\sigma^2\). On the other hand, the variances of the retained estimates are smaller.
- Including irrelevant variables (\(\boldsymbol\beta_2=\mathbf 0\) but \(\mathbf X_2\) fitted): estimates remain unbiased, but their variances increase (especially if \(\mathbf X_2\) is correlated with \(\mathbf X_1\)), and prediction variance increases.
Variable selection is therefore a trade-off between bias (too few variables) and variance (too many). In addition, a simpler model is cheaper to use (fewer variables to measure) and easier to explain.
15.2 Criteria for evaluating subset models
Let a subset model have \(p\) parameters (including the intercept) and residual sum of squares \(SS_{Res}(p)\).
Coefficient of determination \(R_p^2=1-SS_{Res}(p)/SS_T\). It always increases with \(p\), so it is used to see where additional variables stop giving worthwhile increases.
Adjusted \(R^2\) \(\bar R_p^2=1-\frac{n-1}{n-p}(1-R_p^2)\). Choose the subset with maximum \(\bar R^2\), equivalently minimum \(MS_{Res}(p)=SS_{Res}(p)/(n-p)\).
Mallows’ \(C_p\) (Mallows 1973): an estimate of the standardized total mean squared error of prediction,
\[C_p=\frac{SS_{Res}(p)}{\hat\sigma^2}-(n-2p), \tag{15.1}\]
where \(\hat\sigma^2\) is \(MS_{Res}\) from the model with all candidate variables. For a model with negligible bias, \(E(C_p)\approx p\). Good subsets have small \(C_p\) close to \(p\); a plot of \(C_p\) against \(p\) with the line \(C_p=p\) is helpful.
Akaike and Bayesian information criteria (Akaike 1974):
\[AIC=n\ln\left(\frac{SS_{Res}}n\right)+2p,\qquad BIC=n\ln\left(\frac{SS_{Res}}n\right)+p\ln n, \tag{15.2}\]
(up to additive constants). Smaller is better. BIC penalizes extra parameters more heavily and tends to choose smaller models.
PRESS (Section 8.2.3): leave-one-out prediction error. Choose the subset with small PRESS, i.e. large \(R^2_{\text{prediction}}\).
15.3 All possible regressions
With \(k\) candidate variables there are \(2^k-1\) subset models (63 for \(k=6\), over a million for \(k=20\)). For moderate \(k\) all of them can be fitted and compared by the criteria above; efficient algorithms (such as branch-and-bound “leaps”) find the best subsets of each size without fitting every model.
Example 15.1 The paddy data (csv/paddy.csv, \(n=30\)) contain six candidate regressors: N, P, K (kg/ha), soil organic carbon OC (%), seasonal rainfall (mm) and DTPA zinc (ppm). Find the best subset of each size by all possible regressions.
Solution
All \(2^6-1=63\) models were fitted. Table 15.1 shows the model with smallest \(C_p\) for each size, with \(\hat\sigma^2=5.702\) from the full model.
The criteria do not all point to the same model, which is common in practice. \(C_p\) is smallest for {N, P, K} (\(C_p=3.793\), close to \(p=4\)), and BIC also prefers {N, P, K}. AIC is smallest for {N, P, K, OC}, and \(\bar R^2\) is largest for {N, P, K, OC, rain}, although the differences among the models with four to six parameters are small. PRESS is smallest for {N, P, OC}, a reminder that farm 17 (Chapter 9) makes prediction from N-heavy models unreliable. Rainfall and zinc contribute at most marginally (the gain in \(\bar R^2\) from rainfall is in the third decimal), N and P appear in every good subset, and whether to keep K and OC is a matter of judgement and agronomic knowledge.
15.4 Stepwise-type procedures
When \(k\) is large, sequential methods examine only a small number of subsets. Each uses a partial \(F\) statistic (Section 7.7) for a single variable,
\[F=\frac{SS_R(x_j\mid\text{other variables in the model})}{MS_{Res}},\]
or equivalently \(t^2\), compared with a cut-off \(F_{IN}\) (to enter) or \(F_{OUT}\) (to remove), often 4 (roughly 5% significance) or a chosen \(\alpha\).
15.4.1 Forward selection
- Start with no regressors (intercept only).
- For each variable not in the model, compute the partial \(F\) for adding it. The first variable chosen is the one with the largest simple correlation with \(y\).
- If the largest partial \(F\) exceeds \(F_{IN}\), add that variable; otherwise stop.
- Repeat step 2–3 with the enlarged model.
A variable once entered is never removed, even if it becomes redundant later.
15.4.2 Backward elimination
- Start with all \(k\) candidate variables.
- Compute the partial \(F\) (or \(t^2\)) for each variable as if it were the last to enter.
- If the smallest partial \(F\) is less than \(F_{OUT}\), remove that variable; otherwise stop.
- Refit the reduced model and repeat.
Backward elimination considers each variable in the presence of all others, which is an advantage when variables act jointly, but it cannot be started if \(k\ge n\).
15.4.3 Stepwise regression
Stepwise regression (Efroymson’s algorithm (Efroymson 1960)) is forward selection with a check at each step: after a variable is added, all variables already in the model are re-examined by their partial \(F\) and any with \(F<F_{OUT}\) is removed. A variable that entered early may thus be dropped when later variables make it redundant. \(F_{IN}\ge F_{OUT}\) is required to prevent cycling.
15.4.4 Stagewise regression
In forward stagewise regression the coefficients are not re-estimated jointly. At each stage:
- Compute the residuals of the current model (initially \(y_i-\bar y\)).
- Find the candidate variable most highly correlated with the current residuals.
- Regress the residuals on that variable and add the fitted contribution to the model.
- Repeat with the new residuals until no variable is significantly correlated with them.
Each stage explains part of what is left unexplained. Because earlier coefficients are not adjusted when later variables enter, stagewise estimates are not least squares estimates (unless the regressors are orthogonal), and the method is mainly of historical interest; its modern small-step version underlies the lasso and boosting.
Example 15.2 Apply forward selection, backward elimination and stepwise regression to the paddy data, using AIC as the criterion.
Solution
- Forward selection: N, P, K, OC.
- Backward elimination: N, P, K, OC.
- Stepwise: N, P, K, OC.
The three AIC-based procedures arrive at the subset with the smallest AIC in Table 15.1. With strongly correlated candidates they often do not agree, and none is guaranteed to find the best subset of any size.
15.5 Cautions in variable selection
- Automatic procedures are aids to judgement, not substitutes for it. Variables that subject-matter knowledge says must be present (for example, the treatment of interest) should be forced into the model.
- The \(F\) and \(t\) values, \(p\)-values and \(R^2\) of the selected model are optimistic, because the same data were used to choose the model. Confidence intervals are too narrow.
- Selected models are unstable: a slightly different sample may select different variables, especially under multicollinearity.
- Different procedures and criteria may select different models; several “good” subsets usually exist, and all should be examined for residual patterns, influence and interpretability.
- Where possible, validate the chosen model on new data or by data splitting (fit on one part, evaluate prediction on the other).
15.6 R practical
pd <- read.csv("csv/paddy.csv")
full <- lm(yield ~ N + P + K + OC + rain + Zn, data = pd)
null <- lm(yield ~ 1, data = pd)
# Partial F tests for adding / dropping single variables
add1(null, scope = ~ N + P + K + OC + rain + Zn, test = "F")#> Single term additions
#>
#> Model:
#> yield ~ 1
#> Df Sum of Sq RSS AIC F value Pr(>F)
#> <none> 286.43 69.689
#> N 1 88.894 197.54 60.542 12.6002 0.001385 **
#> P 1 48.532 237.90 66.120 5.7120 0.023821 *
#> K 1 16.153 270.28 69.948 1.6734 0.206379
#> OC 1 38.703 247.73 67.334 4.3745 0.045674 *
#> rain 1 2.685 283.75 71.407 0.2649 0.610788
#> Zn 1 2.262 284.17 71.451 0.2229 0.640478
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
drop1(full, test = "F")#> Single term deletions
#>
#> Model:
#> yield ~ N + P + K + OC + rain + Zn
#> Df Sum of Sq RSS AIC F value Pr(>F)
#> <none> 131.14 58.252
#> N 1 47.823 178.96 65.580 8.3874 0.008145 **
#> P 1 18.897 150.04 60.291 3.3142 0.081721 .
#> K 1 28.973 160.11 62.241 5.0814 0.034012 *
#> OC 1 11.693 142.83 58.815 2.0508 0.165580
#> rain 1 6.065 137.21 57.609 1.0637 0.313095
#> Zn 1 0.718 131.86 56.416 0.1259 0.725974
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Forward, backward, stepwise (AIC criterion; use k = log(n) for BIC)
step(null, scope = formula(full), direction = "forward", trace = 0)#>
#> Call:
#> lm(formula = yield ~ N + P + K + OC, data = pd)
#>
#> Coefficients:
#> (Intercept) N P K OC
#> 26.30610 0.04249 0.07419 0.06114 2.24438
step(full, direction = "backward", trace = 0)#>
#> Call:
#> lm(formula = yield ~ N + P + K + OC, data = pd)
#>
#> Coefficients:
#> (Intercept) N P K OC
#> 26.30610 0.04249 0.07419 0.06114 2.24438
step(null, scope = formula(full), direction = "both", trace = 0)#>
#> Call:
#> lm(formula = yield ~ N + P + K + OC, data = pd)
#>
#> Coefficients:
#> (Intercept) N P K OC
#> 26.30610 0.04249 0.07419 0.06114 2.24438
# Mallows Cp for a chosen subset
s2 <- sigma(full)^2; sub <- lm(yield ~ N + P + K + OC, data = pd)
deviance(sub) / s2 - (nrow(pd) - 2 * length(coef(sub)))#> [1] 4.104667
# Forward stagewise regression (illustration)
r <- pd$yield - mean(pd$yield); X <- pd[, c("N", "P", "K", "OC", "rain", "Zn")]
for (stage in 1:4) {
cr <- sapply(X, function(x) cor(x, r))
j <- names(which.max(abs(cr)))
f <- lm(r ~ X[[j]])
cat("Stage", stage, ": enter", j, " slope =", round(coef(f)[2], 4), "\n")
r <- resid(f)
}#> Stage 1 : enter N slope = 0.0534
#> Stage 2 : enter P slope = 0.0699
#> Stage 3 : enter K slope = 0.06
#> Stage 4 : enter OC slope = 1.8727
From hand computation to Cp
Before computers, adding a variable to a regression meant inverting a larger matrix by hand, so economists and agricultural statisticians developed sequential methods that added one variable at a time. M. A. Efroymson’s stepwise algorithm (1960) was among the first statistical programs written for a digital computer (Efroymson 1960). Colin Mallows introduced \(C_p\) in a talk in 1964, publishing it only in 1973 (Mallows 1973), and Hirotugu Akaike’s information criterion (1974) (Akaike 1974) gave model selection a foundation in information theory.
“Everything should be made as simple as possible, but not simpler.”
- Attributed to Albert Einstein
15.7 Practical exercises
15.7.1 Practical 15.1: Choosing a subset from summary statistics
A crop–weather study (\(n=20\)) with four candidate regressors (maximum temperature \(T\), rainfall \(R\), relative humidity \(H\) and sunshine hours \(S\)) gave \(SS_T=480\) and the residual sums of squares below. Compute \(\bar R^2\), \(C_p\) and AIC for each model and choose the best subset.
| Model | \(p\) | \(SS_{Res}\) |
|---|---|---|
| R | 2 | 260 |
| T, R | 3 | 150 |
| R, S | 3 | 190 |
| T, R, S | 4 | 128 |
| T, R, H | 4 | 146 |
| T, R, H, S (full) | 5 | 124 |
Solution
\(\hat\sigma^2=124/15=8.2667\) from the full model. Using Equation 7.9, Equation 15.1 and Equation 15.2:
| Model | \(p\) | \(SS_{Res}\) | \(\bar R^2\) | \(C_p\) | AIC |
|---|---|---|---|---|---|
| R | 2 | 260 | 0.4282 | 15.45 | 55.30 |
| T, R | 3 | 150 | 0.6507 | 4.15 | 46.30 |
| R, S | 3 | 190 | 0.5576 | 8.98 | 51.03 |
| T, R, S | 4 | 128 | 0.6833 | 3.48 | 45.13 |
| T, R, H | 4 | 146 | 0.6388 | 5.66 | 47.76 |
| T, R, H, S | 5 | 124 | 0.6728 | 5.00 | 46.49 |
The model {T, R, S} has the smallest \(C_p\) (3.48, close to \(p=4\)), the smallest AIC (T, R, S) and the largest \(\bar R^2\) (T, R, S). Humidity adds nothing once T, R and S are in the model. The two-variable model {T, R} has \(C_p=4.15>p=3\), indicating bias from omitting sunshine.
Doing it in R
sub <- data.frame(model = c("R", "T,R", "R,S", "T,R,S", "T,R,H", "T,R,H,S"),
p = c(2, 3, 3, 4, 4, 5), SSres = c(260, 150, 190, 128, 146, 124))
n <- 20; SST <- 480; s2full <- 124 / (n - 5)
within(sub, {
AIC <- n * log(SSres / n) + 2 * p
Cp <- SSres / s2full - (n - 2 * p)
adjR2 <- 1 - (SSres / (n - p)) / (SST / (n - 1))
})#> model p SSres adjR2 Cp AIC
#> 1 R 2 260 0.4282407 15.451613 55.29899
#> 2 T,R 3 150 0.6507353 4.145161 46.29806
#> 3 R,S 3 190 0.5575980 8.983871 51.02584
#> 4 T,R,S 4 128 0.6833333 3.483871 45.12596
#> 5 T,R,H 4 146 0.6388021 5.661290 47.75749
#> 6 T,R,H,S 5 124 0.6727778 5.000000 46.49099
# With raw data: step(lm(y ~ 1, data), scope = ~ T + R + H + S, direction = "both")15.8 Chapter Summary
Fill in the blanks
Answers are given at the end of the chapter.
Omitting relevant variables makes the estimates __________.
Including irrelevant variables increases the __________ of the estimates.
Mallows’ \(C_p=\) __________.
For a model with negligible bias, \(C_p\) is close to __________.
Maximizing adjusted \(R^2\) is equivalent to minimizing __________.
With \(k\) candidate variables there are __________ possible subset models.
In forward selection, a variable once entered is __________ removed.
Stepwise regression is forward selection with a __________ step at each stage.
In stagewise regression, each new variable is fitted to the current __________.
__________ penalizes additional parameters more heavily than AIC.
Short-answer questions
Discuss the consequences of omitting relevant variables and of including irrelevant ones.
Describe the criteria used to compare subset regression models.
Explain Mallows’ \(C_p\) and its use.
Describe the method of all possible regressions.
Explain forward selection with its stopping rule.
Explain backward elimination.
Explain stepwise regression and how it differs from forward selection.
What is stagewise regression? How does it differ from stepwise regression?
State the cautions to be observed in automatic variable selection.
Why are \(p\)-values of a selected model optimistic?
Numerical and conceptual questions
Answers are given at the end of the chapter.
With \(n=25\) and \(\hat\sigma^2=4.0\) from the full model, a subset with \(p=4\) has \(SS_{Res}=92\). Find \(C_p\) and comment.
How many subset models are possible with 8 candidate variables?
Two models have \(SS_{Res}=120\) (\(p=3\)) and \(SS_{Res}=112\) (\(p=5\)) with \(n=40\). Compare them by AIC and BIC.
In forward selection, the partial \(F\) values for adding \(x_3\), \(x_4\), \(x_5\) are 6.2, 2.1 and 3.5 with \(F_{IN}=4\). What happens?
In backward elimination, the partial \(F\) values are 15.4, 3.2, 0.8 and 7.1 with \(F_{OUT}=4\). Which variable is removed first?
A subset has \(R^2=0.80\) with \(p=4\) and another \(R^2=0.81\) with \(p=7\); \(n=20\). Compare their adjusted \(R^2\).
Important formulae
Mallows’ \(C_p\):
\[ C_p=\frac{SS_{Res}(p)}{\hat\sigma^2}-(n-2p) \]
Information criteria:
\[ AIC=n\ln\frac{SS_{Res}}n+2p,\qquad BIC=n\ln\frac{SS_{Res}}n+p\ln n \]
Partial \(F\) for entry or removal:
\[ F=\frac{SS_R(x_j\mid\text{others})}{MS_{Res}} \]
Quick revision
- Too few variables → bias; too many → variance.
- Criteria → \(\bar R^2\), \(MS_{Res}\), \(C_p\approx p\), AIC, BIC, PRESS.
- All possible regressions → \(2^k-1\) models.
- Forward (add), backward (remove), stepwise (add and re-check), stagewise (fit residuals).
- Use judgement; validate; selected-model \(p\)-values are optimistic.
Answers to fill in the blanks
1. Biased 2. Variance 3. \(SS_{Res}(p)/\hat\sigma^2-(n-2p)\) 4. \(p\) 5. \(MS_{Res}\) 6. \(2^k-1\) 7. Never 8. Removal (backward check) 9. Residuals 10. BIC
Solutions to numerical and conceptual questions
By Equation 15.1, \(C_p=92/4-(25-8)=23-17=6>p=4\): the subset shows some bias.
\(2^8-1=255\).
AIC: \(40\ln3+6=49.94\) vs \(40\ln2.8+10=51.18\); BIC: \(40\ln3+3\ln40=55.01\) vs \(40\ln2.8+5\ln40=59.63\). The smaller model is preferred by both.
\(x_3\) (\(F=6.2>4\)) enters; then partial \(F\) values are recomputed for \(x_4\) and \(x_5\).
The variable with \(F=0.8\) (smallest and below 4) is removed first.
\(\bar R^2=1-\frac{19}{16}(0.20)=0.7625\) vs \(1-\frac{19}{13}(0.19)=0.7223\); the smaller model is better.