16Correlated predictors: VIF and stepwise selection
16.1 The problem
Thomas Stamey and his colleagues at Stanford measured the prostate-specific antigen (PSA) in the blood of 102 men, and published the study in 1989. Each man had prostate cancer and was about to have a radical prostatectomy. The study is Stamey et al. (1989) in the Journal of Urology. Hastie, Tibshirani and Friedman use the data in The Elements of Statistical Learning. They use 97 of the men and publish the data with a fixed split into a training set and a test set.
The outcome is lpsa, the log of PSA. There are 8 clinical measurements:
Which of these 8 measurements do we need to predict log(PSA), and what does each one add?
Multiple regression (Chapter 14) gives one coefficient for each predictor. The predictors in this study are correlated with each other. A large tumour (lcavol) is more likely to penetrate the capsule (lcp) and to invade the seminal vesicles (svi). The two Gleason measures describe the same tumour specimen. Figure 16.1 shows the correlations.
Show code
cors <-cor(all97 |>select(-lpsa))as_tibble(cors, rownames ="a") |>pivot_longer(-a, names_to ="b", values_to ="r") |>mutate(a =factor(a, colnames(cors)), b =factor(b, rev(colnames(cors)))) |>ggplot(aes(a, b, fill = r)) +geom_tile(colour ="white") +geom_text(aes(label =sprintf("%.2f", r)), size =3) +scale_fill_gradient2(low ="firebrick", mid ="white", high ="steelblue", limits =c(-1, 1)) +labs(x =NULL, y =NULL, fill ="r") +theme(axis.text.x =element_text(angle =45, hjust =1))
Figure 16.1: Correlations between the 8 predictors in the 97 men. The largest values join the tumour measures (lcavol, svi, lcp) and the two Gleason measures.
The largest correlation is 0.75, between gleason and pgg45. The correlation between lcavol and lcp is 0.68. When two predictors move together, the data contain little information about the effect of one with the other held constant.
16.2 Intuitive solution
We simulate the problem. We take two predictors, \(X_1\) and \(X_2\), and an outcome \(Y = 1 + X_1 + X_2 + \varepsilon\). Both true slopes are 1. We draw 500 samples of 50 people, fit the regression to each sample, and keep the pair of estimates \((\hat\beta_1, \hat\beta_2)\). We do this twice: once with uncorrelated predictors, and once with a correlation of 0.9 between them.
simulate_slopes <-function(r, n =50, reps =500) { S <-matrix(c(1, r, r, 1), 2)t(replicate(reps, { X <-mvrnorm(n, mu =c(0, 0), Sigma = S) y <-1+ X[, 1] + X[, 2] +rnorm(n)coef(lm(y ~ X))[2:3] }))}set.seed(35)b_r0 <-simulate_slopes(r =0)b_r9 <-simulate_slopes(r =0.9)sd_ratio <-sd(b_r9[, 1]) /sd(b_r0[, 1])round(c(sd_r0 =sd(b_r0[, 1]), sd_r9 =sd(b_r9[, 1]), ratio = sd_ratio,cor_estimates_r9 =cor(b_r9[, 1], b_r9[, 2])), 2)
sd_r0 sd_r9 ratio cor_estimates_r9
0.16 0.35 2.23 -0.90
Figure 16.2: Each dot is the pair of estimated slopes from one of 500 simulated samples. The cross marks the true values (1, 1). With correlated predictors, the cloud spreads along a line. When one estimate is too high, the other is too low.
In Figure 16.2 both clouds are centred on the truth, so the estimates are still unbiased. With \(r = 0.9\), the standard deviation of \(\hat\beta_1\) is 2.2 times larger. The two estimates also have a correlation of -0.9. The data can tell us the sum \(\beta_1 + \beta_2\) well, because the cloud is narrow in that direction. The data cannot tell us how to divide the sum between \(X_1\) and \(X_2\).
This is multicollinearity. The fitted values stay almost the same. The individual coefficients become unstable, so a small change in the data can move weight from one predictor to the other.
16.3 Formal solution
16.3.1 The variance inflation factor
Regress predictor \(X_j\) on all the other predictors and call the \(R^2\) of this regression \(R_j^2\). The variance inflation factor of \(X_j\) is
\[
\text{VIF}_j = \frac{1}{1 - R_j^2}.
\]
If \(X_j\) is uncorrelated with the other predictors, \(R_j^2 = 0\) and \(\text{VIF}_j = 1\). If the others predict \(X_j\) almost perfectly, \(R_j^2\) is close to 1 and the VIF is large. The variance of \(\hat\beta_j\) is
where \(s_{X_j}^2\) is the sample variance of \(X_j\). The first factor is the variance that \(\hat\beta_j\) has when \(X_j\) is uncorrelated with the other predictors. The VIF multiplies this variance, so it multiplies the standard error by \(\sqrt{\text{VIF}_j}\).
In our simulation, \(R_1^2 = 0.9^2\) and \(\sqrt{\text{VIF}} = \sqrt{1/(1 - 0.81)} = 2.29\). The simulated ratio of standard deviations was 2.23, close to this value. The name and the measure come from Marquardt (1970).
16.3.2 VIF for the prostate data
The function vif() in the car package calculates the VIF for each predictor of a fitted model. We use all 97 men.
fit_all <-lm(lpsa ~ ., data = all97)v <-vif(fit_all)round(v, 2)
lcavol lweight age lbph svi lcp gleason pgg45
2.10 1.45 1.34 1.39 1.96 3.10 2.47 2.97
The largest VIF is 3.1, for lcp. We can calculate it from the definition:
The other 7 predictors explain 68% of the variation in lcp. The standard error of the lcp coefficient is 1.8 times its value with uncorrelated predictors. A rule of thumb flags a VIF above 5, or above 10 in some texts. By this rule, the correlation here is moderate.
16.3.3 The sign of an adjusted coefficient
A high VIF also changes the meaning of a coefficient. Compare lcp alone with lcp adjusted for the other 7 predictors:
Estimate Std. Error t value Pr(>|t|)
alone 0.453 0.071 6.399 0.000
adjusted -0.106 0.090 -1.180 0.241
Alone, each unit of lcp adds 0.45 to log(PSA), with p < 0.001. Adjusted for the other predictors, the slope is -0.11, with \(p = 0.24\). The two slopes answer different questions. The first slope says that men with more capsular penetration have higher PSA. The second says that, among men with the same tumour volume, invasion and Gleason scores, capsular penetration adds nothing detectable. Most of the information in lcp is already in lcavol and svi.
With a high VIF, you can drop one predictor of a correlated pair, or combine the pair into one score. You can also keep all predictors and shrink the coefficients with ridge regression (Chapter 17). For prediction alone, a high VIF does no harm, because the fitted values stay stable.
16.3.4 Stepwise selection
An older approach lets an algorithm choose the predictors. M. A. Efroymson described stepwise regression in 1960, in the book Mathematical Methods for Digital Computers (Ralston and Wilf, editors). The method adds or removes one predictor at a time.
Forward selection starts with the intercept only. At each step it adds the predictor that improves the model most.
Backward elimination starts with all predictors. At each step it removes the predictor that contributes least.
Mixed selection (both directions) starts like forward selection, but at each step it can also remove a predictor that is no longer useful.
R’s step() uses Akaike’s information criterion, \(\text{AIC} = n \log(\text{RSS}/n) + 2k\), where \(k\) is the number of coefficients. A predictor enters only if its fall in RSS outweighs the 2 that its extra coefficient adds to the AIC. The procedure stops when no single change reduces the AIC. We fit all three versions on the 67 training men.
full <-lm(lpsa ~ ., data = train)null <-lm(lpsa ~1, data = train)step_both <-step(full, direction ="both", trace =0)step_back <-step(full, direction ="backward", trace =0)step_fwd <-step(null, scope =formula(full), direction ="forward", trace =0)list(both =formula(step_both), backward =formula(step_back), forward =formula(step_fwd))
$both
lpsa ~ lcavol + lweight + age + lbph + svi + lcp + pgg45
$backward
lpsa ~ lcavol + lweight + age + lbph + svi + lcp + pgg45
$forward
lpsa ~ lcavol + lweight + svi + lbph
Backward elimination and the two-direction search agree: they drop gleason and keep the other 7 predictors. Forward selection stops earlier, with 4 predictors. The three searches start from different places and take one step at a time, so they can stop at different models. None of the three checks all 256 possible subsets of the 8 predictors.
The test mean squared error (MSE) is 0.521 for the full model and 0.517 for the stepwise model. The difference is small. The smaller forward model has a test MSE of 0.456. With 30 test men, differences of this size can come from chance. Chapter 18 compares more methods on the same split.
16.3.5 The instability of stepwise selection
We check if the selected model changes when the data change a little. We take 200 bootstrap resamples of the 67 training men. Each resample draws 67 men with replacement. We run the same two-direction step() on each resample and record which predictors it keeps.
set.seed(37)chosen <-replicate(200, { rows <-sample(nrow(train), replace =TRUE) m <-step(lm(lpsa ~ ., data = train[rows, ]), direction ="both", trace =0)paste(sort(attr(terms(m), "term.labels")), collapse =" + ")})model_counts <-sort(table(chosen), decreasing =TRUE)c(distinct_models =length(model_counts),most_common_pct =100*as.numeric(model_counts[1]) /200)
keep <-sapply(c("lcavol", "lweight", "age", "lbph", "svi", "lcp", "gleason", "pgg45"), \(v) mean(str_detect(chosen, paste0("\\b", v, "\\b"))))tibble(predictor =names(keep), share = keep) |>ggplot(aes(share, reorder(predictor, share))) +geom_col(fill ="steelblue") +scale_x_continuous(labels = scales::percent, limits =c(0, 1)) +labs(x ="share of resamples in which the predictor is kept", y =NULL)
Figure 16.3: How often each predictor is kept by two-direction stepwise selection over 200 bootstrap resamples of the 67 training men.
The 200 resamples give 50 different models. The most common model appears in 22% of them. Figure 16.3 shows the reason. lcavol is kept in 100% of the resamples. lcp is kept in 68% and gleason in 22%. A small change in the data changes which member of a correlated group looks most useful.
16.3.6 Inference after selection
The p-values that summary() prints for a stepwise model assume that we chose the model before we saw the data. A stepwise search used the data, so the assumption is false. Look at pgg45 in the full model and in the stepwise model:
full after_stepwise cor_gleason_pgg45
0.088 0.033 0.757
In the full model, pgg45 has \(p = 0.088\). Stepwise selection removes gleason, which has a correlation of 0.76 with pgg45. Now pgg45 carries the Gleason information alone, and its p-value falls to 0.033. The t-test does not know that a search took place. Freedman (1983) showed the extreme case: screening pure noise predictors and refitting gives many “significant” results.
Stepwise selection can help to explore data, but its p-values and confidence intervals are not valid. Chapter 17 keeps every predictor and shrinks all the coefficients towards zero.
16.4 Summary
Correlated predictors leave the coefficients unbiased and inflate the variance of each estimate. The standard error grows by \(\sqrt{\text{VIF}_j}\), where \(\text{VIF}_j = 1/(1 - R_j^2)\).
A coefficient adjusted for correlated predictors can differ from the unadjusted one, and it can have the opposite sign.
Stepwise selection by AIC is unstable, because a small change in the data gives a different model. Forward and backward searches can also disagree.
The p-values after selection are too small, because the test does not know about the search.