Skip to content

Penalty + Ridge #1308

@LucyMcGowan

Description

@LucyMcGowan

I am seeing unexpected behavior when trying to compare Ridge, Lasso, and Elastic Net (this is for a class lab and I want to make sure I'm pointing my students in the right direction). Below we create a grid of potential penalties and examine ridge (mixture = 0) lasso (mixture = 1) and elastic net (mixture = 0.5) -- we noticed that the penalty doesn't seem to be passing for ridge. I believe this is related to #431 and #486, and when we specify the path_values it does work but I'm just trying to understand whether this is expected behavior, if I am teaching students to use tidymodels + ridge do they always need to pass path_values for their penalties? it seems the penalty is ignored without notice (but maybe I missed something!)

suppressPackageStartupMessages(library(tidyverse))
library(tidymodels)
library(ISLR)

set.seed(7)
cv <- vfold_cv(Carseats, v = 5)

rec <- recipe(Sales ~ ., data = Carseats) |>
  step_dummy(all_nominal_predictors()) |>
  step_normalize(all_predictors())

elas_spec <- linear_reg(
  penalty = tune(),
  mixture = tune()
) |>
  set_engine("glmnet")

grid <- expand_grid(
  penalty = seq(0.01, 0.1, by = 0.01),
  mixture = c(0, 0.5, 1)
)

elas_wf <- workflow() |>
  add_recipe(rec) |>
  add_model(elas_spec)

elas_tune <- tune_grid(elas_wf, grid = grid, resamples = cv)

collect_metrics(elas_tune) |> 
  filter(.metric == "rmse") |>
  ggplot(aes(x = penalty, y = mean, group = mixture, color = mixture)) +
  geom_point() +
  geom_line()

## with path values

elas_spec_pv <- linear_reg(
  penalty = tune(),
  mixture = tune()
) |>
  set_engine(
    "glmnet",
    path_values = seq(0, 0.1, by = 0.01)
    )

elas_wf_pv <- workflow() |>
  add_recipe(rec) |>
  add_model(elas_spec_pv)

elas_tune_pv <- tune_grid(elas_wf_pv, grid = grid, resamples = cv)

collect_metrics(elas_tune_pv) |> 
  filter(.metric == "rmse") |>
  ggplot(aes(x = penalty, y = mean, group = mixture, color = mixture)) +
  geom_point() +
  geom_line()

Created on 2025-11-21 with reprex v2.1.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions