我正在尝试使用 tidymodels 的新审查功能来创建多状态生存模型,但遇到了问题。当我尝试拟合我的模型时,我看到以下错误。通常这可以通过在 coxph() 函数中设置 id 变量来解决,但我无法让它与 tidymodels 一起使用。
生存错误::coxph(公式 = x ~ ., data = data, x = TRUE, model = TRUE) : 多状态模型需要 id 声明
下面的可重现示例
library(tidyverse)
library(tidymodels)
library(censored)
df <- tibble(
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, id = id)
mod_spec <- proportional_hazards() %>%
set_mode("censored regression") %>%
set_engine("survival")
mod_wflow <- workflow() %>%
add_recipe(mod_rec) %>%
add_model(mod_spec)
mod_fit <- fit(mod_wflow, data = mod_train)