Skip to content
Back to skills

R Ml Regularization

ASecurity

R regularized regression. Use for lasso, ridge, elastic-net with glmnet, and penalized regression.

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

Security analysis

A100/100

Pro scans all 2 files and shows the line behind each finding

Scanned June 4, 2026

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

Installs into .claude/skills of the current project.

Are you the author of R Ml Regularization?

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

Security grade badge for R Ml Regularization
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/leolin990405-r-ml-regularization/badge)](https://www.skillsdirectory.com/skills/leolin990405-r-ml-regularization)

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: r-ml-regularization
description: R regularized regression. Use for lasso, ridge, elastic-net with glmnet, and penalized regression.
---

# R Regularized Regression

Lasso, ridge, and elastic-net models.

## glmnet

```r
library(glmnet)

# Prepare data (matrix format required)
x <- as.matrix(train[, -which(names(train) == "target")])
y <- train$target

# Ridge regression (alpha = 0)
ridge <- glmnet(x, y, alpha = 0)

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

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

# Cross-validation for lambda
cv_lasso <- cv.glmnet(x, y, alpha = 1, nfolds = 10)
plot(cv_lasso)

# Best lambda
cv_lasso$lambda.min      # Minimum CV error
cv_lasso$lambda.1se      # 1 SE rule (more regularization)

# Coefficients at best lambda
coef(cv_lasso, s = "lambda.min")

# Predictions
x_test <- as.matrix(test[, -which(names(test) == "target")])
pred <- predict(cv_lasso, newx = x_test, s = "lambda.min")

# Classification (logistic)
cv_logistic <- cv.glmnet(x, y, family = "binomial", alpha = 1)
pred_prob <- predict(cv_logistic, newx = x_test, s = "lambda.min", type = "response")

# Multinomial
cv_multi <- cv.glmnet(x, y, family = "multinomial", alpha = 1)

# Poisson
cv_poisson <- cv.glmnet(x, y, family = "poisson", alpha = 1)
```

## Feature Selection with Lasso

```r
# Fit lasso
cv_lasso <- cv.glmnet(x, y, alpha = 1)

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

# Coefficient path
plot(lasso, xvar = "lambda", label = TRUE)
```

## Grouped Lasso

```r
library(grpreg)

# Define groups
group <- c(1, 1, 2, 2, 2, 3, 3)  # Feature group assignments

# Fit
model <- grpreg(x, y, group, penalty = "grLasso")

# Cross-validation
cv_model <- cv.grpreg(x, y, group, penalty = "grLasso")
plot(cv_model)

# Coefficients
coef(cv_model, s = "lambda.min")
```

## Adaptive Lasso

```r
# Step 1: Get initial weights from ridge
ridge <- cv.glmnet(x, y, alpha = 0)
ridge_coef <- coef(ridge, s = "lambda.min")[-1]
weights <- 1 / abs(ridge_coef)

# Step 2: Adaptive lasso
adaptive_lasso <- cv.glmnet(x, y, alpha = 1, penalty.factor = weights)
```

## tidymodels Integration

```r
library(tidymodels)

# Lasso specification
lasso_spec <- linear_reg(
  penalty = tune(),
  mixture = 1  # 1 = lasso, 0 = ridge
) %>%
  set_engine("glmnet")

# Ridge specification
ridge_spec <- linear_reg(
  penalty = tune(),
  mixture = 0
) %>%
  set_engine("glmnet")

# Elastic-net
enet_spec <- linear_reg(
  penalty = tune(),
  mixture = tune()
) %>%
  set_engine("glmnet")

# Tune
tune_results <- tune_grid(
  workflow() %>% add_model(enet_spec) %>% add_formula(target ~ .),
  resamples = vfold_cv(train, v = 5),
  grid = grid_regular(penalty(), mixture(), levels = 10)
)
```

## Comparison

| Method | alpha | Effect |
|--------|-------|--------|
| Ridge | 0 | Shrinks all coefficients |
| Lasso | 1 | Sparse (feature selection) |
| Elastic-net | 0-1 | Balance of both |

Files in this skill

  • SKILL.md3 KB
  • glmnet/SKILL.md2.5 KB

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.