What is the Glmnet package in R?
Last Updated : 16 Apr, 2025
The glmnet package in R is a robust package for L1 and L2 regularized linear and logistic regression model fitting, useful for avoiding overfitting and enhancing the generalization of the models. Lasso (L1) and Ridge (L2) regression techniques add a penalty term to the objective function in the model to simplify the model. Here, we will show how to conduct regularized regression using the glmnet package, which involves fitting a Lasso model, cross-validation tuning, and prediction.
Regularized Regression
A type of regression that adds a penalty term to the cost function to reduce overfitting.
- Lasso Regression: A type of regularized regression that adds an L1 penalty term to the cost function.
- Ridge Regression: A type of regularized regression that includes an L1 penalty term in the cost function.
- Elastic Net Regression: A type of regularized regression that includes an L2 penalty term in the cost function.
Syntax
glmnet(X, y, family = "gaussian", alpha = 1, lambda = NULL)
The main function in the glmnet package is glmnet(), which fits a regularized generalized linear model. The function accepts a number of important arguments:
- x: The matrix of predictor variables.
- y: The response variable.
- alpha: Declares the type of regularization (Lasso: alpha = 1, Ridge: alpha = 0, Elastic Net: 0 < alpha < 1).
- lambda: Regularization parameter that affects the strength of the penalty.
- family: specifies the type of response variable (e.g., Gaussian, binomial, Poisson).
Example 1: Fitting a Lasso Regression Model
Step 1: Install and Load the glmnet
Package
First, install and load the necessary package.
R install.packages("glmnet") library(glmnet)
Step 2: Load and Prepare the Data
Here, we load the mtcars dataset from the R package and use its data on car models and their features.
R data(mtcars) # Select all columns except the first as predictors. X <- as.matrix(mtcars[, -1]) # Select the first column as the response. y <- mtcars[, 1]
Step 3: Fit a Lasso Regression Model
The following code fits a Lasso regression model, and the Summary(model) provides information on the fitted model, like the number of non-zero coefficients, the value of the regularization parameter lambda used, and the coefficients themselves.
R # Fit a regularized linear regression model model = glmnet(X, y, family = "gaussian", alpha = 1) summary(model)
Output:
Length Class Mode
a0 79 -none- numeric
beta 790 dgCMatrix S4
df 79 -none- numeric
dim 2 -none- numeric
lambda 79 -none- numeric
dev.ratio 79 -none- numeric
nulldev 1 -none- numeric
npasses 1 -none- numeric
jerr 1 -none- numeric
offset 1 -none- logical
call 5 -none- call
nobs 1 -none- numeric
Step 4: Plot the model
plot(model) will plot the relationship between the regularization parameter lambda and the estimate coefficients.
R plot(model, label = TRUE)
Output
L1 Norm vs Estimated coefficientsIn the above graph, each curve represents the path of the coefficients against the L1 norm as lambda varies.
Step 5: Get the model coefficients
R
Output:
1 x 1 sparse Matrix of class "dgCMatrix"
s1
(Intercept) 20.12070307
cyl -0.21987003
disp .
hp -0.01300595
drat 0.77162507
wt -2.63787681
qsec 0.46074875
vs 0.11747113
am 2.11349978
gear 0.30437026
carb -0.46452172
Step 6: Prediction
Predict values for new data using the predict function. For example, the following code predicts values for new data using the Lasso regression model:
R # Predict the response variable (y) using the fitted model and the predictor variables (x). y_pred <- predict(model, X)
Example 2: Using Cross-Validation for Lasso Model
Step 1: Fit a Lasso Model with Cross-Validation
For more robust model selection, you can use cross-validation to find the optimal value of lambda. The cv.glmnet() function automatically performs k-fold cross-validation.
R # Fit a Lasso model with cross-validation fit <- cv.glmnet(X, y, alpha = 1, nfolds = 5) # Display a summary of the model summary(fit)
Output:
Length Class Mode
lambda 79 -none- numeric
cvm 79 -none- numeric
cvsd 79 -none- numeric
cvup 79 -none- numeric
cvlo 79 -none- numeric
nzero 79 -none- numeric
call 5 -none- call
name 1 -none- character
glmnet.fit 12 elnet list
lambda.min 1 -none- numeric
lambda.1se 1 -none- numeric
index 2 -none- numeric
Step 2: Plot Cross-Validation Results
You can plot the results of the cross-validation to visualize the selection of lambda
.
R # Plot the cross-validation results using the "plot" function. plot(fit)
Output
cross-validationStep 3: Make Predictions and Plot Actual vs Predicted
Once the model is fitted, you can use it to make predictions and visualize the results.
R # Predict the response variable (y) y_pred <- predict(fit, X) # Plot Actual vs Predicted plot(y, y_pred, xlab = 'Actual', ylab = 'Predicted', main = 'Actual vs Predicted')
Output
Actual (y) vs predictedRefer this google colab for entire code.
Similar Reads
What Is CRAN In R Language? CRAN (Comprehensive R Archive Network) is the primary repository for R packages, and it hosts thousands of packages that users can download and install to extend the functionality of the R Programming Language. These packages are created by R users and developers from around the world and cover a wi
4 min read
What is the Most Efficient K-Means Clustering Package in R? K-means clustering is one of the most popular unsupervised machine learning algorithms used for grouping data points into a specified number of clusters. Each data point is assigned to the cluster with the nearest mean, serving as a prototype of the cluster. In R, several packages provide implementa
6 min read
Introduction to the Matrix package in R Matrices are fundamental mathematical objects used in various fields, including linear algebra, statistics, and machine learning. In R, the Matrix package provides a powerful framework for creating, manipulating, and performing operations on matrices efficiently. This article serves as an introducti
3 min read
What Are the Tidyverse Packages in R Language? When working with Data Science in R then Tidyverse packages is widely used. They were created specifically for data science tasks and follow a consistent design making them easy to use and efficient. Understanding Tidyverse Packages in RThere are eight core Tidyverse packages namely ggplot2, dplyr,
7 min read
What is Elasticnet in Sklearn? To minimize overfitting, in machine learning, regularizations techniques are applied which helps to enhance the modelâs generalization performance. ElasticNet is a regularized regression method in scikit-learn that combines the penalties of both Lasso (L1) and Ridge (L2) regression methods. This com
8 min read