Skip to content

Setting relax = TRUE for glmnet models fails #82

@hfrick

Description

@hfrick

Slightly adapted from this SO post

library(poissonreg)
#> Loading required package: parsnip

set.seed(123)
df <- tibble::tibble(
  y = rpois(1000, lambda = 3),
  x_1 = 2 * y + rnorm(1000),
  x_2 = 0.1 * y + rnorm(1000)
)

# setting `relax` by itself works
mod <- poisson_reg(penalty = 0.5) %>%
  set_engine("glmnet", relax = TRUE) |> 
  fit(y ~ ., data = df) 

# setting `family` by itself works
mod <- poisson_reg(penalty = 0.5) %>%
  set_engine("glmnet", family = "poisson") |> 
  fit(y ~ ., data = df)

# setting both together fails
mod <- poisson_reg(penalty = 0.5) %>%
  set_engine("glmnet", family = "poisson", relax = TRUE) |> 
  fit(y ~ ., data = df)
#> Error in glmnet.path(x, y, weights, lambda, nlambda, lambda.min.ratio, : Invalid family argument; must be either character, function or family object

# glmnet directly works
mod <- glmnet::glmnet(x = as.matrix(df[,2:3]), y = df$y, family = "poisson")
mod <- glmnet::glmnet(x = as.matrix(df[,2:3]), y = df$y, family = "poisson", relax = TRUE)

Created on 2024-02-03 with reprex v2.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions