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

initial_validation_split() stratification for Surv objects #558

Closed
krz opened this issue Jan 24, 2025 · 2 comments
Closed

initial_validation_split() stratification for Surv objects #558

krz opened this issue Jan 24, 2025 · 2 comments

Comments

@krz
Copy link

krz commented Jan 24, 2025

Hi,

I'm using tidymodels for a survial prediction task. My data has very few events so I want to make sure the validation split is stratified by the event variable. However, this seems to no be implemented currently.

Here is an example:

# example data
library(survival)
attach(kidney)

kidney <- kidney %>% 
  mutate(
    kidney_surv = Surv(time, status), 
    .keep = "unused"
  )

kidney_split <- initial_validation_split(kidney, strata = kidney_surv)

I do get the following error message: strata cannot be a Surv object. Use the time or event variable directly.

But it is unclear to me how it is supposed to work. None of these work:

kidney_split <- initial_validation_split(kidney, strata = kidney_surv)
kidney_split <- initial_validation_split(kidney, strata = kidney_surv[, "status"])
kidney_split <- initial_validation_split(kidney, strata = "kidney_surv[, "status"]")
kidney_split <- initial_validation_split(kidney, strata = kidney_surv[, 2])
@hfrick
Copy link
Member

hfrick commented Jan 24, 2025

This is indeed a little bumpy. We have an issue to revisit this when we feel comfortable that we have a good solution: #445 .

Our current idea is to remove the original time and status variables not when you make the Surv object but rather after you've made your resampling object, i.e., in a recipe:

library(rsample)
library(recipes)
# [...]
library(survival)
attach(kidney)

kidney <- kidney %>%
  mutate(
    surv = Surv(time, status) # keep the original variables here
  )

# so you can use them for stratification
kidney_split <- initial_validation_split(kidney, strata = status)

# then remove them in your recipe
kidney_rec <- recipe(surv ~ ., data = training(kidney_split)) %>% 
  step_rm(time, status)

Created on 2025-01-24 with reprex v2.1.1

@krz
Copy link
Author

krz commented Jan 28, 2025

thanks, this works for me

@krz krz closed this as completed Jan 28, 2025
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

2 participants