What is the Glmnet package in R? (original) (raw)

Last Updated : 23 Jul, 2025

The glmnet package in R is used to build linear regression models with special techniques called Lasso (L1) and Ridge (L2). These techniques add a small penalty to the model to avoid making it too complex which helps prevent overfitting and makes the model work better on new data.

Regularized Regression

A type of regression that adds a penalty term to the cost function to reduce overfitting.

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:

Fitting a Lasso Regression Model

Lasso regression helps prevent overfitting by shrinking less important feature coefficients using an L1 penalty. We will now implement it using the glmnet package.

1. Installing and loading the glmnet package

We first install and load the glmnet package which provides tools for regularized regression.

install.packages("glmnet") library(glmnet)

`

2. Loading and preparing the data

We use the built-in mtcars dataset and split it into predictor and response variables.

data(mtcars)

X <- as.matrix(mtcars[, -1])

y <- mtcars[, 1]

`

3. Fitting the Lasso regression model

We now fit the Lasso model using the glmnet() function.

model = glmnet(X, y, family = "gaussian", alpha = 1)

summary(model)

`

**Output:

Screenshot-2025-06-28-155406

Output

4. Plotting the model

We visualize how coefficients shrink as the regularization strength increases.

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.

5. Getting model coefficients

We extract the coefficients at a specific value of lambda.

coef(model, , s = 0.1)

`

**Output:

Screenshot-2025-06-28-155504

Output

6. Making predictions with the model

We use the trained model to predict response values based on the predictors.

y_pred <- predict(model, X)

`

Using Cross-Validation for Lasso Model

Cross-validation helps us choose the best value of the regularization parameter lambda, improving the model’s performance and generalization.

1. Fitting a Lasso model with cross-validation

To automatically find the best lambda, we use the cv.glmnet() function which performs k-fold cross-validation on a Lasso model.

fit <- cv.glmnet(X, y, alpha = 1, nfolds = 5)

summary(fit)

`

**Output:

Screenshot-2025-06-28-160329

Output

2. Plotting cross-validation results

To visualize how the model performed across different lambda values, we use the plot() function.

plot(fit)

`

**Output:

cross-validation - Geeksforgeeks

cross-validation

3. Making predictions and plotting actual vs predicted

Once the model is fitted, we use predict() to generate predictions and plot() to visually compare predicted vs actual values.

y_pred <- predict(fit, X)

plot(y, y_pred, xlab = 'Actual', ylab = 'Predicted', main = 'Actual vs Predicted')

`

**Output:

Actual (y) vs predicted - Geeksforgeeks

Actual (y) vs predicted

The scatter plot shows a strong positive relationship between actual and predicted values, indicating that the Lasso model made accurate predictions.