Choosing a method¶
Penalized regression answers two different kinds of question, and the right method depends on which one you are asking:
Prediction. You want a good forecast of the outcome from many predictors, or a short list of the ones that matter. The coefficients are a means to that end, and they are biased toward zero on purpose. Use a penalized fit: the lasso, ridge, the elastic net or the group lasso, with the penalty chosen by cross-validation.
Inference on a few coefficients. You want the effect of one variable (a treatment, a policy, a characteristic), with a standard error, while controlling for many others that might confound it. A penalized fit alone does not give valid confidence intervals: the selection step changes the distribution of the estimate (Leeb and Pötscher 2005). Use a method built for inference after selection: post-double-selection, double machine learning, or the debiased lasso.
If you only want a place to start, go to A starting point.
Prediction¶
Function |
The penalty |
Good when |
|
|---|---|---|---|
Lasso |
|
the sum of absolute coefficients |
a few predictors matter; you want a short list |
Ridge |
|
the sum of squared coefficients |
many predictors each matter a little; they are correlated |
Elastic net |
|
a mix of the two |
correlated predictors, and you still want a short list |
Group lasso |
|
each group’s coefficients together |
categorical predictors that should enter or leave whole |
Lasso logit or probit |
|
as the lasso and elastic net |
a binary outcome |
Lasso Poisson |
|
as the lasso and elastic net |
a count |
Lasso multinomial logit |
|
as the lasso and elastic net, on each level’s coefficient (or, |
an outcome with unordered levels |
Lasso ordered logit or probit |
|
as the lasso and elastic net; the cutpoints are not penalized |
an outcome with ordered levels |
The lasso¶
The lasso (Tibshirani 1996) sets many coefficients exactly to zero, so it selects as it fits. It does best when the outcome depends on a few of the predictors. Among a group of correlated predictors it tends to pick one and drop the rest, and which one it picks can change from sample to sample, so do not read too much into the particular variables it selects (see stability selection, below).
Ridge¶
Ridge regression (Hoerl and Kennard 1970) shrinks every coefficient toward zero but sets none to zero. When many predictors each carry a little information, and especially when they are correlated, it often predicts better than the lasso (Tibshirani 1996; Hastie, Tibshirani and Friedman 2009). It keeps every predictor, so it is not a selection method.
The elastic net¶
The elastic net (Zou and Hastie 2005) mixes the two penalties. It selects
like the lasso, but keeps groups of correlated predictors together instead
of picking one. l1_ratio=1 is the lasso; values near 0 approach ridge.
l1_ratio=0.5 is a common choice; cross-validating over a few values
(fit each, compare cv_mean at lambda_min) is better.
The group lasso¶
A categorical predictor enters a formula as one indicator per level but its
reference. The plain lasso treats those indicators as separate predictors, so
it can keep some levels and drop others, and the result depends on which
level is the reference. The group lasso (Yuan and Lin 2006) penalizes each
group of indicators together, so a factor is in or out as a whole
(groups="factors", or a dict naming your own groups). Use it when the
question is which factors matter, not which levels.
Choosing the penalty¶
|
What it does |
Use it for |
|---|---|---|
|
the penalty with the smallest cross-validated error (lambda_min) |
prediction |
|
the largest penalty within one standard error of that (lambda_1se) |
prediction with fewer predictors, at little cost in error (Hastie, Tibshirani and Friedman 2009) |
|
an information criterion (lassopack’s |
a choice without folds; the BIC and EBIC select fewer predictors than cross-validation |
|
the plug-in penalty of Belloni, Chen, Chernozhukov and Hansen (2012) |
the selection steps of inference (below); not for prediction |
a number |
that penalty |
reproducing a fit, or your own rule |
Cross-validation costs nothing extra here: the one pass over the data keeps
each fold’s cross-products apart, so the whole path and every fold’s error
come from them (How it works). Folds are made of rows, or
of the groups of a column (fold_by=); with fixed effects, by default, of the
groups of the largest one. Make the folds of groups whenever rows within a
group are related (the same person, firm or school), so that the held-out
error is that of new groups.
With fixed effects¶
Fixed effects after | are partialled out and never penalized. In the
cross-validation they are partialled out once from all the rows
(cv_fixed_effects="full_sample", the default), or estimated again from each
fold’s training rows ("refit", exact). The default is exact with one fixed
effect and folds of its groups, and otherwise usually chooses the same penalty
or a neighboring one (Fixed effects in the
cross-validation).
Inference after selection¶
Function |
Estimates |
Relies on |
Good when |
|
|---|---|---|---|---|
Post-double-selection |
|
the effect of one or a few treatments, with the controls two lassos select |
a few controls matter (approximate sparsity) |
the usual first choice; a familiar regression at the end |
Post-double-selection, binary outcome |
|
the same in a logit |
as above |
a binary outcome |
Post-double-selection with instruments |
|
a 2SLS effect, with selected controls (and, optionally, instruments) |
as above, and valid instruments |
an endogenous treatment |
Double machine learning |
|
the effect in a partially linear model, from cross-fitted nuisances |
the nuisance fits predicting well |
many relevant controls; less reliance on sparsity |
Debiased lasso |
|
confidence intervals for any or all of a lasso’s coefficients |
sparsity of the outcome model and of each predictor’s model |
intervals for many coefficients at once |
Stability selection |
|
which predictors are selected reliably, with a bound on false selections |
— |
the question is which variables matter, not their effects |
Post-double-selection¶
Belloni, Chernozhukov and Hansen (2014) run two lassos: of the outcome on the controls, and of the treatment on the controls. The effect is then the ordinary regression of the outcome on the treatment and the union of the two selected sets. Selecting on the outcome alone misses controls that are weakly related to the outcome but strongly related to the treatment, which bias the estimate; the second lasso catches them. The result is an hdfe-stream regression with the usual standard errors (robust or clustered), and it reports and exports like one.
Use the plug-in penalty (penalty="rigorous", the default here): its theory
is what makes the inference valid, and it does not depend on random folds.
Controls you want in the model regardless go in unpenalized=. With
groups= (and a penalty other than the plug-in), each factor among the
controls is selected or left out whole.
With an endogenous treatment and instruments, double_selection_iv_stream
selects the controls for the outcome, the treatment and each instrument
(Chernozhukov, Hansen and Spindler 2015); with many instruments,
select_instruments=True selects them too (Belloni, Chen, Chernozhukov and
Hansen 2012). The default estimate is 2SLS on the union of the selected
controls; method="partialling_out" gives hdm’s estimate, 2SLS on each
variable’s residual from its own selected controls. Both are valid; they
differ in finite samples. Neither guards against weak instruments: check the
first-stage F (f_stat_1st_stage), and when it is low, or when there are many
instruments, add sup_score=True. The sup-score test (Belloni, Chen,
Chernozhukov and Hansen 2012) tests the effect without relying on the
instruments’ strength. Its confidence set (result.double_selection.sup_score)
is wide or unbounded when the instruments say little about the effect,
where the 2SLS interval would be misleadingly narrow. It is conservative:
with the default bound on the critical value, which lassopack’s ivlasso also
uses, it rejects a true effect less often than its level, and
sup_score_options={"method": "bootstrap"} gives a tighter test.
Double machine learning¶
Double machine learning (Chernozhukov et al. 2018) fits the outcome and the
treatment on the controls, each by a lasso or elastic net, on one part of the
data, takes residuals on the other part (cross-fitting), and regresses the
outcome’s residuals on the treatment’s. It relies less on a few controls
mattering than post-double-selection does: what it needs is that the two
nuisance fits predict well. Repeat the cross-fitting (n_repeats=) to
make the estimate depend less on the random split.
The debiased lasso¶
The debiased (desparsified) lasso (van de Geer, Bühlmann, Ritov and Dezeure 2014) corrects each lasso coefficient for the shrinkage, using a lasso of each predictor on the others, and gives a confidence interval for each. Use it when you want intervals for many coefficients of one model at once, rather than the effect of one treatment. It costs one lasso per coefficient, all from the same cross-products.
Stability selection¶
Which predictors a lasso selects can change from sample to sample. Stability selection (Meinshausen and Bühlmann 2010; Shah and Samworth 2013) refits on many half-samples and keeps the predictors selected in most of them, with a bound on the expected number of false selections. Use it when the list of selected variables is itself the result you report. It does not estimate effects.
What matters more than the method¶
The controls you offer. Post-double-selection and double machine learning choose among the controls you give them; they do not find a confounder you left out. Offer the controls you would put in a regression if you could, and nothing the treatment affects.
Folds of groups. When rows within a person, firm or school are related, make the folds of those groups (
fold_by=), in prediction and in double machine learning; otherwise the held-out error and the cross-fitting overstate how well the fits generalize.Standard errors. Cluster them where the data call for it (
vcov={"CRV1": "firm"}), as you would without selection. The plug-in penalty’s loadings follow the vcov’s cluster.Reading the selected set. In prediction, the selected predictors are the ones that help forecast, not the ones that cause the outcome. Report stability selection if the list itself matters.
A starting point¶
For prediction:
fit = lasso_stream("y ~ x1 + x2 + C(region) + C(industry) | firm", data,
fold_by="firm") # folds of firms
fit.summary() # the selected predictors at lambda_min
fit.plot_cv() # the cross-validated error along the path
Then compare ridge_stream and elastic_net_stream on the same folds
(cv_mean at their lambda_min), and consider penalty="cv_1se" for a
shorter list.
For the effect of a treatment:
effect = double_selection_stream("y ~ treat | firm", data,
controls="x1 + x2 + C(region) + C(industry)",
vcov={"CRV1": "firm"})
effect.summary() # the effect, with clustered standard errors
effect.double_selection.summary_text() # which controls were selected, and for what
Then check the estimate against double_ml_stream with the same controls.
If the two differ by much more than their standard errors, the result
depends on how the controls enter, and that is worth knowing before you
report it.
References¶
Belloni, A., D. Chen, V. Chernozhukov and C. Hansen (2012). Sparse models and methods for optimal instruments with an application to eminent domain. Econometrica 80(6).
Belloni, A., V. Chernozhukov and C. Hansen (2014). Inference on treatment effects after selection among high-dimensional controls. Review of Economic Studies 81(2).
Belloni, A., V. Chernozhukov and Y. Wei (2016). Post-selection inference for generalized linear models with many controls. Journal of Business & Economic Statistics 34(4).
Chernozhukov, V., D. Chetverikov, M. Demirer, E. Duflo, C. Hansen, W. Newey and J. Robins (2018). Double/debiased machine learning for treatment and structural parameters. Econometrics Journal 21(1).
Chernozhukov, V., C. Hansen and M. Spindler (2015). Post-selection and post-regularization inference in linear models with many controls and instruments. American Economic Review 105(5).
Hastie, T., R. Tibshirani and J. Friedman (2009). The Elements of Statistical Learning, 2nd edition. Springer.
Hoerl, A. and R. Kennard (1970). Ridge regression: biased estimation for nonorthogonal problems. Technometrics 12(1).
Leeb, H. and B. Pötscher (2005). Model selection and inference: facts and fiction. Econometric Theory 21(1).
Meinshausen, N. and P. Bühlmann (2010). Stability selection. Journal of the Royal Statistical Society B 72(4).
Shah, R. and R. Samworth (2013). Variable selection with error control: another look at stability selection. Journal of the Royal Statistical Society B 75(1).
Tibshirani, R. (1996). Regression shrinkage and selection via the lasso. Journal of the Royal Statistical Society B 58(1).
van de Geer, S., P. Bühlmann, Y. Ritov and R. Dezeure (2014). On asymptotically optimal confidence regions and tests for high-dimensional models. Annals of Statistics 42(3).
Yuan, M. and Y. Lin (2006). Model selection and estimation in regression with grouped variables. Journal of the Royal Statistical Society B 68(1).
Zou, H. and T. Hastie (2005). Regularization and variable selection via the elastic net. Journal of the Royal Statistical Society B 67(2).