Interactive R programming · Machine learning

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.

~13 minIntermediateR · glmnet5 missions275 XP
Mission 1+30 XP✓ Complete

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:

Cost = (1/2n) Σ (yᵢ − ŷᵢ)² + λ [ (1 − α)/2 · Σ βⱼ² + α · Σ |βⱼ| ]λ sets the overall strength; α sets the mix between Ridge (L2) and Lasso (L1)

The original article writes the formula with λ₁ and λ₂ and labels both as λ₁; the version above is the one glmnet actually uses.

🃏Decode α and λ
0 / 4
Mission 2+80 XP✓ Complete

Mix the penalty

Switch between Ridge, Elastic Net and Lasso, and slide λ. Watch how quickly features drop out under each mix.

⚗️α–λ playground
+80 XP
–λ value
–features kept
–R² on training data
raises mpglowers mpgStandardized coefficients on mtcars, so bar sizes are comparable.
  • ✓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.

Mission 3+25 XP✓ Complete

Code it in R

Step 1Load the data

library(glmnet)
data(mtcars)
x <- as.matrix(mtcars[, -1])   # predictors (everything except mpg)
y <- mtcars$mpg

Step 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")
}
🧠Quick check
+25 XP

Which glmnet call fits an Elastic Net that is 75% Lasso and 25% Ridge?

Mission 4+50 XP✓ Complete

Which model fits?

Pick the best regularized model for each situation.

⚡Ridge, Lasso or Elastic Net?
1 / 5
correct on the first try
Final boss+90 XP✓ Defeated

Defeat the Elastic Boss 🐲

Six questions, three hearts.

Question 1 / 6
❤️❤️❤️

💥 The boss winsReview the missions above, then try again.

You’re a Elastic Master!

Share on LinkedIn
What’s next

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.

Lv 1 Rookie0 / 275 XP