Elastic Net — L1 + L2 regularization
Elastic Net combines the L1 penalty (Lasso) and the L2 penalty (Ridge). It both selects variables (like Lasso) and handles highly correlated variable groups well (like Ridge) — fixing Lasso's instability under correlation.
When to use
Use Elastic Net when you have many variables and there are groups of highly correlated variables that you want to keep/drop together rather than picking one at random.
Model specification
- is the mixing ratio: ⇒ Lasso; ⇒ Ridge.
- controls the total penalty.
Running in EcoLab
- Modeling module → Regularized regression family → Elastic Net.
- Select , the variables; standardize; choose and (2-D grid CV).
- Read the retained variables + coefficients; export the replication code.
Replication code
- Stata
- R
- Python
* ---- Elastic Net with cross-validation ----
use "macro_data.dta", clear
elasticnet linear y x1-x20, selection(cv) alphas(0.5)
* Display selected coefficients
lassocoef, display(coef, standardized)
# ---- Elastic Net (alpha = 0.5) with cross-validation ----
library(glmnet)
# Load and prepare data (illustrative)
df <- read.csv("macro_data.csv")
X <- as.matrix(df[, paste0("x", 1:20)])
y <- df$y
# Elastic Net with alpha = 0.5 (midpoint between Ridge and Lasso)
cv_enet <- cv.glmnet(X, y, alpha = 0.5)
plot(cv_enet)
# Best lambda and coefficients
best_lambda <- cv_enet$lambda.min
coef(cv_enet, s = best_lambda)
# ---- Elastic Net with cross-validation ----
from sklearn.linear_model import ElasticNetCV
from sklearn.preprocessing import StandardScaler
import pandas as pd
# Load data (illustrative)
df = pd.read_csv("macro_data.csv")
X = df[[f"x{i}" for i in range(1, 21)]]
y = df["y"]
# Standardize
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Elastic Net: l1_ratio = alpha (0.5 = equal L1 + L2)
model = ElasticNetCV(l1_ratio=0.5, cv=5).fit(X_scaled, y)
print(f"Best alpha (lambda): {model.alpha_}")
print(f"Non-zero coefficients: {sum(model.coef_ != 0)}")
print(f"Coefficients: {model.coef_}")
Limitations
- Has two tuning parameters (, ) ⇒ heavier CV.
- Still a prediction-oriented method; causal interpretation requires care.
Video tutorial
Video Tutorial: Running Elastic Net in EcoLab
See also
- Ridge · Lasso · Adaptive Lasso · Catalog