Skip to content

glmnet breaks in tune #365

@hfrick

Description

@hfrick

This looks like a bug and needs a bunch of different things to come together but since one of them is "glmnet", I'm guessing it's a censored issue.

# stackoverflow post -----------------------------------------------------

# Source - https://stackoverflow.com/q/79929921
# Posted by Lukas D. Sauer
# Retrieved 2026-04-22, License - CC BY-SA 4.0

# minimal reprex

library(tidymodels)
library(censored)
#> Loading required package: survival
n <- 800

set.seed(403)
df <- data.frame(
  treatment = sample(c("A", "B", "C"), n, replace = TRUE),
  age = runif(n, 18, 99),
  #sex = sample(c("m", "f"), n, replace = TRUE),
  time = rexp(n),
  event = sample(c(0, 1), n, replace = TRUE)
) %>%
  filter(time > 0) %>%
  mutate(event_surv = Surv(time, event), .keep = "unused")

cv <- vfold_cv(df, v = 10)

spec <- proportional_hazards(penalty = 0.1, mixture = 1) %>%
  set_engine("glmnet") %>%
  set_mode("censored regression")

rec <- recipe(event_surv ~ ., data = df)

wflow <- workflow() %>%
  add_recipe(rec) %>%
  add_model(spec)

res <- fit_resamples(
  wflow,
  resamples = cv,
  metrics = metric_set(concordance_survival)
)
#> → A | error:   error in evaluating the argument 'x' in selecting a method for function 'as.matrix': non-conformable arguments
#> There were issues with some computations   A: x1
#> Warning: All models failed. Run `show_notes(.Last.tune.result)` for more
#> information.
#> There were issues with some computations   A: x10
#> 


#  how we got here -------------------------------------------------------

# has to be a recipe because formula works
res <- workflow() %>%
  add_formula(event_surv ~ .) %>%
  add_model(spec) %>%
  fit_resamples(
    resamples = cv,
    metrics = metric_set(concordance_survival)
  )

# has to be resampling because a single fit works
res <- workflow() %>%
  add_recipe(rec) %>%
  add_model(spec) %>%
  fit(df)

# has to be glmnet because a different model works
res <- workflow() %>%
  add_recipe(rec) %>%
  add_model(survival_reg()) %>%
  fit_resamples(
    resamples = cv,
    metrics = metric_set(concordance_survival)
  )

# has to do with nominal predictor because two numeric predictors work
set.seed(403)
df <- data.frame(
  #treatment = sample(c("A", "B", "C"), n, replace = TRUE),
  age = runif(n, 18, 99),
  age2 = runif(n, 30, 100),
  #sex = sample(c("m", "f"), n, replace = TRUE),
  time = rexp(n),
  event = sample(c(0, 1), n, replace = TRUE)
) %>%
  filter(time > 0) %>%
  mutate(event_surv = Surv(time, event), .keep = "unused")

res <- workflow() %>%
  add_recipe(recipe(event_surv ~ ., data = df)) %>%
  add_model(spec) %>%
  fit_resamples(
    resamples = vfold_cv(df, v = 10),
    metrics = metric_set(concordance_survival)
  )

# has to include `treatment` for that particular error
# keeping only `sex` produces warning about NA coercion
# (can't remove age because glmnet needs at least 2 predictors)
set.seed(403)
df <- data.frame(
  #treatment = sample(c("A", "B", "C"), n, replace = TRUE),
  age = runif(n, 18, 99),
  sex = sample(c("m", "f"), n, replace = TRUE),
  time = rexp(n),
  event = sample(c(0, 1), n, replace = TRUE)
) %>%
  filter(time > 0) %>%
  mutate(event_surv = Surv(time, event), .keep = "unused")

res <- workflow() %>%
  add_recipe(recipe(event_surv ~ ., data = df)) %>%
  add_model(spec) %>%
  fit_resamples(
    resamples = vfold_cv(df, v = 10),
    metrics = metric_set(concordance_survival)
  )
#> → A | warning: NAs introduced by coercion
#> There were issues with some computations   A: x8
#> There were issues with some computations   A: x10
#> 

Created on 2026-04-22 with reprex v2.1.1

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