Elastic Net Regression in R, the game
The best of Ridge and Lasso in one model. Mix the penalties with α, slide λ on real data, and fit it in R with glmnet.
Best of both worlds
Ridge keeps every feature but can’t select. Lasso selects but struggles with correlated features, picking one from a group almost at random. Elastic Net blends both penalties. In glmnet’s notation:
The original article writes the formula with λ₁ and λ₂ and labels both as λ₁; the version above is the one glmnet actually uses.
Mix the penalty
Switch between Ridge, Elastic Net and Lasso, and slide λ. Watch how quickly features drop out under each mix.
- ✓Try all three mixes: Ridge, Elastic Net and Lasso+30 XP
- ✓Squeeze any model down to exactly 3 features+30 XP
- ✓Return to λ ≈ 0 (plain least squares)+20 XP
The playground uses the same algorithm as glmnet (coordinate descent on standardized features, 100 λ values) on R’s built-in mtcars data, predicting mpg from the other 10 columns. At the smallest λ the coefficients match ordinary least squares.
Code it in R
Step 1Load the data
library(glmnet)
data(mtcars)
x <- as.matrix(mtcars[, -1]) # predictors (everything except mpg)
y <- mtcars$mpgStep 2Fit and tune λ
# alpha = 0.5: an even mix of Lasso and Ridge
elastic_net <- glmnet(x, y, alpha = 0.5)
set.seed(123)
cv_model <- cv.glmnet(x, y, alpha = 0.5)
best_lambda <- cv_model$lambda.min
plot(cv_model)Step 3Inspect and predict
coef(cv_model, s = "lambda.min") # "." = dropped feature
predictions <- predict(cv_model, s = "lambda.min", newx = x)These predictions are on the training data for simplicity; use a held-out test set to judge real performance.
Step 4Bonus: tune α
# Tune alpha too: compare cross-validated error for a few mixes
for (a in c(0, 0.25, 0.5, 0.75, 1)) {
set.seed(123)
cv <- cv.glmnet(x, y, alpha = a)
cat("alpha", a, "min CV error", round(min(cv$cvm), 2), "\n")
}Which glmnet call fits an Elastic Net that is 75% Lasso and 25% Ridge?
Which model fits?
Pick the best regularized model for each situation.
Defeat the Elastic Boss 🐲
Six questions, three hearts.
You’re a Elastic Master!
Keep levelling up
You learned the Elastic Net penalty, what α and λ control, how it combines Lasso’s feature selection with Ridge’s stability, and how to fit and tune it with glmnet.