Elastic Net and Ridge (Machine Learning)
Lasso (α = 1) sets some coefficients to exactly zero. Ridge (α = 0) shrinks all coefficients but keeps every variable. Elastic net blends both. In this exercise, compare all three using plot_dataset.dta.
- Using the training set and the global macro
$inputs, runelasticnet linearwithalpha(1)(this is lasso — confirm it matches Exercise 2). Save results asalph1. - Run elastic net with
alpha(0.5). Save results asalph5. - Run ridge with
alpha(0). Save results asalph0. - Compare results using
lassocoef.
If a model does not converge, remember you can change the search space and/or the stopping rule.
-
How many variables with nonzero coefficients are in each model?
-
How does the number of selected variables change as α moves from 1 (lasso) to 0.5 (elastic net) to 0 (ridge)? Why?
-
Compare the out-of-sample MSE across the three models. Which performs best? Is the difference large?
- For the final model comparison challenge, you will need the predictions from your best-performing elastic net model. Assume
alpha(0.5)is the best. Useestimates restore alph5and then usepredict yhat_enetto generate predictions for the entire dataset.