Skip to content
Back to skills

Glmnet

ASecurity

R glmnet package for regularized regression. Use for lasso, ridge, and elastic-net regularization.

  • 5 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added June 4, 2026
data

Security analysis

A100/100

Scanned June 4, 2026

npx -y skills add LeoLin990405/r-analytics-skill --skill glmnet --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Glmnet?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Glmnet
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/leolin990405-glmnet/badge)](https://www.skillsdirectory.com/skills/leolin990405-glmnet)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: glmnet
description: R glmnet package for regularized regression. Use for lasso, ridge, and elastic-net regularization.
---

# glmnet

Lasso and elastic-net regularization.

## Basic Usage

```r
library(glmnet)

# Prepare data (matrix required)
x <- as.matrix(train[, -1])
y <- train$target

# Fit model
model <- glmnet(x, y)

# Cross-validation
cv_model <- cv.glmnet(x, y)

# Best lambda
cv_model$lambda.min      # Lambda with min error
cv_model$lambda.1se      # Lambda within 1 SE of min

# Predict
pred <- predict(cv_model, newx = as.matrix(test[, -1]), s = "lambda.min")
```

## Model Types

```r
# Ridge (alpha = 0)
model <- glmnet(x, y, alpha = 0)

# Lasso (alpha = 1)
model <- glmnet(x, y, alpha = 1)

# Elastic net (0 < alpha < 1)
model <- glmnet(x, y, alpha = 0.5)

# Logistic regression
model <- glmnet(x, y, family = "binomial")

# Multinomial
model <- glmnet(x, y, family = "multinomial")

# Poisson
model <- glmnet(x, y, family = "poisson")

# Cox
model <- glmnet(x, Surv(time, status), family = "cox")
```

## Cross-Validation

```r
# CV with specific folds
cv_model <- cv.glmnet(
  x, y,
  alpha = 1,
  nfolds = 10,
  type.measure = "mse"  # mse, deviance, class, auc, mae
)

# Plot CV results
plot(cv_model)

# Coefficients at best lambda
coef(cv_model, s = "lambda.min")
coef(cv_model, s = "lambda.1se")
```

## Coefficients

```r
# All coefficients
coef(model)

# At specific lambda
coef(model, s = 0.01)

# Non-zero coefficients
coefs <- coef(cv_model, s = "lambda.min")
coefs[coefs[, 1] != 0, ]

# Number of non-zero
sum(coef(cv_model, s = "lambda.min") != 0)
```

## Prediction

```r
# Predict response
predict(model, newx = x_test, s = 0.01)

# Predict class (classification)
predict(model, newx = x_test, s = 0.01, type = "class")

# Predict probabilities
predict(model, newx = x_test, s = 0.01, type = "response")

# Predict coefficients
predict(model, s = 0.01, type = "coefficients")

# Predict non-zero
predict(model, s = 0.01, type = "nonzero")
```

## Tuning Alpha

```r
# Grid search for alpha
alphas <- seq(0, 1, by = 0.1)
results <- data.frame(alpha = alphas, cvm = NA)

for (i in seq_along(alphas)) {
  cv <- cv.glmnet(x, y, alpha = alphas[i])
  results$cvm[i] <- min(cv$cvm)
}

best_alpha <- results$alpha[which.min(results$cvm)]
```

## Grouped Lasso

```r
# Group lasso
library(gglasso)

model <- gglasso(x, y, group = c(1, 1, 2, 2, 3, 3))
```

## Sparse Matrix

```r
library(Matrix)

# Create sparse matrix
x_sparse <- Matrix(x, sparse = TRUE)

# Fit with sparse matrix
model <- glmnet(x_sparse, y)
```

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.