Two women can have breast cancer of the same stage, and only one of them develops metastases. In 2002, van ’t Veer and colleagues measured the activity of thousands of genes in breast tumours from 117 young patients. They looked for a gene signature that predicts early distant metastases in women with no cancer in their lymph nodes. The genes they found work in a few processes: cell cycle, invasion, metastasis and the growth of new blood vessels.
A study like this has far more genes than patients. Genes in one process rise and fall together, so many predictors are strongly correlated. The clinic wants a prediction for each new patient. The laboratory wants a short list of genes to study further.
We do not have their data, so we use simulated data with the same shape on a smaller scale. In simulated data, we know the true effects.
There are 60 patients and 300 genes. Three pathways of 10 genes each drive the outcome. Within a pathway, every pair of genes has correlation 0.9. The other 270 genes are noise. We also simulate 1000 new patients to measure prediction error.
Which genes predict the outcome, and how well can we predict it in new patients?
OLS cannot answer either part. With 300 genes and 60 patients, \(X^\top X\) has no inverse. lm() estimates only as many coefficients as the data allow:
ols_genes <-lm(y ~ X)sum(is.na(coef(ols_genes)))
[1] 241
lm() returns NA for 241 of the 301 coefficients. Ridge regression (Chapter 17) gives an answer for all 300 genes, but it keeps every gene in the model and gives no short list. We need a penalty that sets most coefficients exactly to zero.
18.2 Intuitive solution
18.2.1 Two kinds of pull
Ridge adds \(\lambda\beta^2\) to the RSS. The lasso adds \(\lambda|\beta|\). Each penalty pulls the coefficient towards zero. The strength of the pull is the slope of the penalty:
Ridge: the slope of \(\lambda\beta^2\) is \(2\lambda\beta\). The pull gets weaker as \(\beta\) gets smaller. Near zero it almost disappears, so \(\beta\) gets small but stops before zero.
Lasso: the slope of \(\lambda|\beta|\) is \(\lambda\) for every \(\beta \ne 0\). The pull stays the same, however small \(\beta\) is. If the data push back with less force than \(\lambda\), the coefficient goes all the way to zero and stays there.
Figure 18.1: The ridge penalty β² and the lasso penalty |β| for λ = 1 (left), and the pull towards zero, which is the slope of each penalty (right). The ridge pull fades to zero near β = 0. The lasso pull stays at λ.
18.2.2 One coefficient at a time
We can solve both problems exactly in the simplest case. Assume the predictors are standardised and uncorrelated. Then the RSS separates into one term for each coefficient. Call the OLS estimate of one coefficient \(z\). It measures the support in the data for that predictor. Up to a constant, each coefficient solves its own problem:
\[
\min_\beta \; (z - \beta)^2 + P(\beta).
\]
For ridge, \(P(\beta) = \lambda\beta^2\). The penalty is smooth, so we set the derivative to zero: \(-2(z - \beta) + 2\lambda\beta = 0\), which gives
Ridge divides every coefficient by the same factor. The estimate is zero only if \(z\) is zero.
For the lasso, \(P(\beta) = \lambda|\beta|\). The penalty has no derivative at zero, so we solve for each sign separately. If \(\beta > 0\), the derivative is \(-2(z - \beta) + \lambda = 0\), so \(\beta = z - \lambda/2\). This answer is consistent only if \(z > \lambda/2\).
If \(\beta < 0\), the same steps give \(\beta = z + \lambda/2\), consistent only if \(z < -\lambda/2\). When \(|z| \le \lambda/2\), neither sign is consistent, and the minimum is at \(\beta = 0\). Together:
where \((a)_+ = \max(a, 0)\). This is soft thresholding. The lasso subtracts a fixed amount from each coefficient. If the coefficient is weaker than the threshold, the lasso sets it to exactly zero.
For the elastic net, \(P(\beta) = \lambda_1|\beta| + \lambda_2\beta^2\). The same steps give
With \(\lambda = 1\), ridge halves every coefficient. The lasso sets the two weak coefficients (\(z = 0.2\) and \(z = 0.4\)) to zero. It moves the strong ones towards zero by \(\lambda/2 = 0.5\). The elastic net (with \(\lambda_1 = \lambda_2 = 1\)) also drops the weak ones and then halves the others.
Figure 18.2: The estimate against the OLS value z for one standardised predictor (λ = 1, and λ1 = λ2 = 1 for the elastic net). The grey diagonal is OLS. Ridge is a line through zero with a smaller slope. The lasso and elastic net are exactly zero for |z| ≤ 0.5.
18.3 Formal solution
18.3.1 The lasso
The lasso (least absolute shrinkage and selection operator) minimises
The penalty is the L1 norm of the coefficients. As for ridge, we standardise the predictors, we do not penalise the intercept, and we choose \(\lambda\) by cross-validation. There is no closed form for correlated predictors. glmnet() solves the problem by coordinate descent. It applies the soft-threshold formula to one coefficient at a time, and cycles through the coefficients until they stop changing.
Robert Tibshirani introduced the lasso in Tibshirani (1996). He used the prostate data of this book as one of his examples.
18.3.2 The geometry: diamond and circle
An equivalent form of each problem minimises the RSS subject to a budget on the coefficients. Ridge requires \(\sum_j \beta_j^2 \le t\), a circle in two dimensions. The lasso requires \(\sum_j |\beta_j| \le t\), a diamond. The contours of the RSS are ellipses around the OLS estimate. The solution is the first point where an ellipse touches the constraint region.
Figure 18.3: Contours of the RSS (ellipses) around the OLS estimate (cross). The lasso constraint |β1| + |β2| ≤ 1 is on the left, and the ridge constraint β1² + β2² ≤ 1 is on the right. The first contour that touches the diamond touches it at a corner, where β2 = 0. The circle has no corners.
In Figure 18.3 the lasso solution is at the corner \((1, 0)\), so \(\hat\beta_2 = 0\) exactly. The OLS estimate is \((2, 0.5)\). The ridge solution, \((0.87, 0.49)\), has both coefficients non-zero. In many dimensions the diamond has many corners and edges, where some coefficients are zero. These corners let the lasso select variables.
18.3.3 The Bayesian view
Each penalty corresponds to a prior distribution on the coefficients. With normal errors, minimising \(\text{RSS} + \lambda P(\beta)\) gives the most probable \(\beta\) (the posterior mode) under a prior proportional to \(e^{-c\,P(\beta)}\) for a constant \(c\).
Ridge corresponds to a normal (Gaussian) prior, \(p(\beta) \propto e^{-c\beta^2}\).
The lasso corresponds to a Laplace (double exponential) prior, \(p(\beta) \propto e^{-c|\beta|}\).
Figure 18.4: The normal prior behind ridge and the Laplace prior behind the lasso, both with variance 1. The Laplace prior has a sharp peak at zero. It puts more belief on coefficients that are exactly or nearly zero, and also on a few large ones.
The prior also shows when each method suits the problem. If you expect many small effects, ridge fits that belief. If you expect a few real effects among many zeros, the lasso fits it.
18.3.4 The lasso on the gene data
We fit ridge (alpha = 0) and the lasso (alpha = 1) to the 60 patients, with \(\lambda\) chosen by 10-fold cross-validation. We count the pathway genes and noise genes that each model keeps, and the test MSE in the 1000 new patients.
fit_genes <-function(X, y, X_new, y_new, alpha, seed) {set.seed(seed) cv <-cv.glmnet(X, y, alpha = alpha) b <-as.vector(coef(cv, s ="lambda.min"))[-1]c(pathway_kept =sum(b[in_pathway] !=0), noise_kept =sum(b[!in_pathway] !=0),test_mse =mean((y_new -predict(cv, X_new, s ="lambda.min"))^2))}one_run <-sapply(c(ridge =0, lasso =1, elastic_net =0.5), \(a) fit_genes(X, y, X_new, y_new, alpha = a, seed =50))round(one_run, 2)
Ridge keeps all 300 genes. The lasso keeps 14 of the 30 pathway genes and 6 noise genes. Its test MSE is 6.6, against 22.4 for ridge. The lasso predicts better here because most genes are noise, and ridge gives every noise gene a small coefficient.
The short list of the lasso misses many pathway genes. Within a pathway, the 10 genes carry almost the same information. Once the lasso has one or two of them, the others add little, and the threshold removes them. The lasso also has a hard limit. When \(p > n\), it can select at most \(n\) variables, so with 60 patients it cannot keep 61 genes.
The L1 term sets weak coefficients to zero. The L2 term makes correlated predictors share their weight, so they enter or leave the model together. glmnet() writes the penalty with one \(\lambda\) and a mixing parameter \(\alpha\):
alpha = 1 is the lasso, alpha = 0 is ridge, and values between give the elastic net. The third column of the table above uses alpha = 0.5. The elastic net keeps 21 pathway genes and 16 noise genes, with a test MSE of 6.6.
Figure 18.5: Coefficients for the 300 simulated genes (lambda.min). Pathway genes are blue, noise genes are grey. Ridge gives every gene a small coefficient. The lasso keeps a few genes from each pathway. The elastic net keeps more of each pathway.
A single simulated dataset can give an unusual result. We repeat the whole experiment 50 times, with new patients each time, and average the results.
set.seed(51)seeds <-sample(1e5, 50)many <-sapply(seeds, \(s) {set.seed(s) X <-simulate_genes(60); y <-drop(X %*% beta_true +rnorm(60, sd =2)) Xn <-simulate_genes(1000); yn <-drop(Xn %*% beta_true +rnorm(1000, sd =2))sapply(c(ridge =0, lasso =1, elastic_net =0.5), \(a) fit_genes(X, y, Xn, yn, a, s))}, simplify ="array")avg <-apply(many, c(1, 2), mean)round(avg, 1)
Over 50 simulations, the lasso keeps on average 15.4 of the 30 pathway genes, and the elastic net keeps 20.2. The elastic net also keeps more noise genes (18.3 against 15.6). Its average test MSE is 6.6, against 7.0 for the lasso and 21.6 for ridge. For a gene study, the elastic net gives a fuller list of each pathway and predicts at least as well as the lasso.
18.3.6 The lasso and two correlated predictors
The gene example shows the problem in a large model. A model with two predictors shows it more clearly. Both predictors have a true slope of 1, and their correlation is 0.99. We choose \(\lambda\) for each method once by cross-validation, and then refit each method on 200 bootstrap resamples of the 50 people.
Figure 18.6: Fitted coefficients for two predictors with correlation 0.99 (both true slopes 1), on 200 bootstrap resamples. Ridge and the elastic net share the weight between the two predictors. The lasso often puts all the weight on one predictor and sets the other to zero, and the choice changes from resample to resample.
The lasso drops one of the two predictors in 74% of the resamples. In 75% of these it drops \(X_1\), and in the rest it drops \(X_2\). Ridge drops one in 0% of the resamples, because ridge never sets a coefficient exactly to zero. The elastic net drops one in 9%. The exact percentages change with the simulated sample and the folds.
The three methods give similar predictions, because the sum \(\hat\beta_1 + \hat\beta_2\) is similar. Its mean over the resamples is 1.65 for ridge, 1.70 for the lasso and 1.62 for the elastic net. They differ in what they tell you about the individual predictors. A list of “selected” variables from the lasso can change if you collect the data again.
18.3.7 All methods on the prostate data
We now compare every method from this part of the book on the same 67 training men and 30 test men. We set the same seed before each cross-validation, so that all three penalised models use the same folds.
Figure 18.7: Lasso coefficient paths for the prostate training data, on the standardised scale. Moving from right (large λ) to left, lcavol enters first, and the coefficients become non-zero one at a time. The dashed line marks lambda.min.
Model
Test MSE
OLS, 8 predictors
0.521
Stepwise (AIC, both directions)
0.517
Ridge
0.494
Lasso
0.497
Elastic net (\(\alpha = 0.5\))
0.496
The three penalised methods have a slightly smaller test MSE than OLS and stepwise selection. At lambda.min, the lasso sets the coefficient of gleason to zero. The gaps between the methods are small. There are 30 test men, and a different seed can give different folds and a different \(\lambda\).
A difference in the third decimal place is no reason to prefer one method. The prostate data have more men than predictors and moderate correlation, so all these methods give a similar test error. The penalties make a large difference when \(p\) is close to or larger than \(n\), as in the gene data.
18.4 Summary
The lasso penalty \(\lambda\sum_j|\beta_j|\) pulls each coefficient towards zero with constant force. For one standardised predictor, it subtracts \(\lambda/2\) and sets weak coefficients to exactly zero (soft thresholding). Ridge divides every coefficient by \(1 + \lambda\).
The lasso constraint is a diamond with corners on the axes, and the ridge constraint is a circle. In Bayesian terms, ridge uses a normal prior and the lasso a Laplace prior.
The lasso tends to keep one of a group of highly correlated predictors and drop the others, and the choice is unstable. The elastic net adds an L2 term so that correlated predictors enter together.
The argument alpha of glmnet() sets the penalty: alpha = 0 is ridge, alpha = 1 is the lasso, and values between are the elastic net. The penalty \(\lambda\) comes from cv.glmnet() with a fixed seed.