Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Visualization
  • Statistics in R
  • Machine Learning in R
  • Data Science in R
  • Packages in R
  • Data Types
  • String
  • Array
  • Vector
  • Lists
  • Matrices
  • Oops in R
Open In App
Next Article:
What is the Glmnet package in R?
Next article icon

What is the Glmnet package in R?

Last Updated : 16 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 coefficients- Geeksforgeeks
L1 Norm vs Estimated coefficients

In 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
coef(model, , s = 0.1) 

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-validation - Geeksforgeeks
cross-validation

Step 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 predicted - Geeksforgeeks
Actual (y) vs predicted

Refer this google colab for entire code.


Next Article
What is the Glmnet package in R?

H

harshanutakki
Improve
Article Tags :
  • R Language

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences