Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

workflows confuses an 'id' special for dplyr::id() #268

Open
topepo opened this issue Nov 6, 2024 · 1 comment
Open

workflows confuses an 'id' special for dplyr::id() #268

topepo opened this issue Nov 6, 2024 · 1 comment

Comments

@topepo
Copy link
Member

topepo commented Nov 6, 2024

See https://stackoverflow.com/questions/79163528/multistate-survival-models-with-censored-and-tidymodels

This might end up being a parsnip issue but we should start here.

library(tidyverse)
library(tidymodels)
library(censored)
#> Loading required package: survival

df <- tibble(
  sample_id = seq(1:1000),
  sex = sample(c("Male","Female"), 1000, replace = T),
  duration = rnorm(1000, mean = 75, sd = 7),
  outcome = sample(c("censor","Lost","Won"), 1000, replace = T)
  
) %>%
  mutate(
    outcome = factor(outcome)
  )

mod_df <- df %>%
  mutate(
    sale_surv = Surv(duration, outcome),
    .keep = "unused"
  )

set.seed(1988)

mod_split <- initial_split(mod_df)
mod_train <- training(mod_split)
mod_test <- testing(mod_split)

mod_rec <- recipe(sale_surv ~ sex, data = mod_train) # no `id` here 

mod_spec <- proportional_hazards() %>%
  set_mode("censored regression") %>%
  set_engine("survival") 

mod_wflow <- workflow() %>%
  add_recipe(mod_rec) %>%
  add_model(mod_spec, formula = sale_surv ~ sex + id(sample_id)) # add a formula here

mod_fit <- fit(mod_wflow, data = mod_train)
#> Error:
#> ! `id()` was deprecated in dplyr 0.5.0 and is now defunct.
#> ℹ Please use `vctrs::vec_group_id()` instead.
@topepo
Copy link
Member Author

topepo commented Nov 7, 2024

Tracing the error, it happens when .fit_model() invokes fit.action_model(). In there, the formula is correctly extracted:

sale_surv ~ sex + id(sample_id)
<environment: 0x12ee76170>

From here, it calls fit_from_formula() when then calls fit.model_spec() via

fit(spec, formula = formula, data = data, case_weights = case_weights, 
    control = control_parsnip)

This leads to calling

survival::coxph(formula = sale_surv ~ sex + id(sample_id), data = data, 
                x = TRUE, model = TRUE)

id is a “formula special” and isn’t a function in the survival package (so using the conflicted isn’t a solution).

Note that the same call to results in the same error when run in the global environment:

> survival::coxph(formula = sale_surv ~ sex + id(sample_id), data = mod_df, 
+                 x = TRUE, model = TRUE)
Error:
! `id()` was deprecated in dplyr 0.5.0 and is now defunct.
ℹ Please use `vctrs::vec_group_id()` instead.
Run `rlang::last_trace()` to see where the error occurred.

@topepo topepo changed the title workflows confuses a 'id' special for dplyr::id() workflows confuses an 'id' special for dplyr::id() Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant