tidymodels / censored

Parsnip wrappers for survival models
https://censored.tidymodels.org/
Other
123 stars 12 forks source link

Save training data as used in `coxnet_train()` #264

Closed hfrick closed 1 year ago

hfrick commented 1 year ago

Closes #263

It's survival_time_coxnet() which breaks because the training data object$training_data$x carries an intercept column when going into survival::survfit().

This happens because fit.workflow() creates a mold that includes the intercept because the encoding in censored says compute_intercept = TRUE.

That intercept column is still part of x when it reaches fit_xy.proportional_hazards() where it gets written into the object$training_data$x mentioned earlier.

The intercept column does not make it to the actual call to glmnet (so no bug when fitting) because we set remove_intercept = TRUE in the encodings which gets picked up in xy_form() which passes it to .convert_xy_to_form_fit() and the column is removed.

That means everything is okay here inside of censored::coxnet_train() and inside of fit_xy.proportional_hazards() we should take the training data as returned from coxnet_train(), like we do in fit.proportional_hazards().

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

set.seed(1)
sim_tr <- SimSurv(500) %>%
  mutate(event_time = Surv(time, event)) %>%
  select(event_time, X1, X2)
new_x <- sim_tr[1:5, 2:3]

glmn_spec <-
  proportional_hazards(penalty = .01) %>%
  set_engine("glmnet")

glmn_wflow <-
  workflow() %>%
  add_model(glmn_spec) %>%
  add_formula(event_time ~ X1 + X2)

glmn_wflow_fit <- fit(glmn_wflow, data = sim_tr)

predict(glmn_wflow_fit, new_data = new_x)
#> # A tibble: 5 × 1
#>   .pred_time
#>        <dbl>
#> 1       7.25
#> 2       5.00
#> 3      11.9 
#> 4       2.69
#> 5       7.63

Created on 2023-04-26 with reprex v2.0.2

github-actions[bot] commented 1 year ago

This pull request has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.