therneau / survival

Survival package for R
381 stars 104 forks source link

`survift` mentions survcheck flags>0, but `survcheck` returns no flags #258

Closed Dpananos closed 3 months ago

Dpananos commented 3 months ago

I have created a longitudinal dataset using tmerge. I've excluded time dependent covariates to isolate the problem, so there may be more rows than there are state transitions.

When I try to use survfit to fit a multistate model, I am met with an error stating Error in survfitCI(X, newY, weights = casewt, stype = stype, ctype = ctype, : one or more flags are >0 in survcheck. However, survcheck$ flags is all 0. See below for a reproducible example.

library(survival)
model_data <- structure(list(study_id = c(17, 17, 17, 17, 17, 17, 17, 26, 26, 
                            26, 26, 26, 26, 26, 104, 104, 104, 104, 104, 104, 104), tstart = c(0, 
                                                                                               1, 7, 221, 248, 249, 251, 0, 2, 7, 128, 129, 156, 164, 0, 629, 
                                                                                               1044, 1045, 1057, 1373, 1666), tstop = c(1, 7, 221, 248, 249, 
                                                                                                                                        251, 253, 2, 7, 128, 129, 156, 164, 344, 629, 1044, 1045, 1057, 
                                                                                                                                        1373, 1666, 1724), init_stat = c("(s0)", "(s0)", "(s0)", "(s0)", 
                                                                                                                                                                         "(s0)", "(s0)", "(s0)", "(s0)", "(s0)", "(s0)", "(s0)", "(s0)", 
                                                                                                                                                                         "(s0)", "(s0)", "Waitlisted", "Waitlisted", "Waitlisted", "Waitlisted", 
                                                                                                                                                                         "Waitlisted", "Waitlisted", "Waitlisted"), event = c("", "Waitlisted", 
                                                                                                                                                                                                                              "", "Transplanted", "Waitlisted", "Transplanted", "Dead", "", 
                                                                                                                                                                                                                              "Waitlisted", "", "", "", "Transplanted", "Dead", "Removed from Waitlist", 
                                                                                                                                                                                                                              "", "", "Waitlisted", "", "Removed from Waitlist", "Dead")), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                         -21L), class = "data.frame")

check <- survcheck(  Surv(
  time = tstart,
  time2 = tstop,
  event = event,
  type = 'mstate'
) ~ 1, data = model_data, id = study_id)

check$flag
#>   overlap       gap      jump  teleport duplicate 
#>         0         0         0         0         0

fit <- survfit(
  Surv(
    time = tstart,
    time2 = tstop,
    event = event,
    type = 'mstate'
  ) ~ 1, data = model_data, id = study_id,
  istate = init_stat
)
#> Error in survfitCI(X, newY, weights = casewt, stype = stype, ctype = ctype, : one or more flags are >0 in survcheck

Created on 2024-06-06 with reprex v2.1.0

However, if I remove the istate argument, then the model fits. This suggests I've perhaps done something wrong with the istate variable.

Is this a problem with survfit, or a problem with what I pass to istate? How should this be rectified if it is a user error?

bethatkinson commented 3 months ago

If you put istate in the survcheck call you'll note that you have teleport errors. You're init_stat variable is causing you problems. Take a look at the istate variable created by survcheck to better understand how to define the variable. Generally I avoid trying to define istate unless people are entering the study with a different state because it can be tricky to get it right. My suggestion is to start with the istate created by survcheck, then modify it slightly as needed.

check <- survcheck( Surv( time = tstart, time2 = tstop, event = event, type = 'mstate' ) ~ 1, data = model_data, id = study_id, istate=init_stat)

check$teleport $row [1] 3 4 5 6 7 10 11 12 13 14 16 17 18 21

$id [1] 17 26 104

If I look at the natural istate values that come from the data check2 <- survcheck( Surv( time = tstart, time2 = tstop, event = event, type = 'mstate' ) ~ 1, data = model_data, id = study_id)

model_data$new_istate <- check2$istate

model_data[model_data$study_id==17,] study_id tstart tstop init_stat event new_istate 1 17 0 1 (s0) (s0) 2 17 1 7 (s0) Waitlisted (s0) 3 17 7 221 (s0) Waitlisted 4 17 221 248 (s0) Transplanted Waitlisted 5 17 248 249 (s0) Waitlisted Transplanted 6 17 249 251 (s0) Transplanted Waitlisted 7 17 251 253 (s0) Dead Transplanted

From: Demetri Pananos @.> Sent: Wednesday, June 05, 2024 11:12 PM To: therneau/survival @.> Cc: Subscribed @.***> Subject: [EXTERNAL] [therneau/survival] survift mentions survcheck flags>0, but survcheck returns no flags (Issue #258)

I have created a longitudinal dataset using tmerge. I've excluded time dependent covariates to isolate the problem, so there may be more rows than there are state transitions.

When I try to use survfit to fit a multistate model, I am met with an error stating Error in survfitCI(X, newY, weights = casewt, stype = stype, ctype = ctype, : one or more flags are >0 in survcheck. However, survcheck$ flags is all 0. See below for a reproducible example.

library(survival)

model_data <- structure(list(study_id = c(17, 17, 17, 17, 17, 17, 17, 26, 26,

                        26, 26, 26, 26, 26, 104, 104, 104, 104, 104, 104, 104), tstart = c(0,

                                                                                           1, 7, 221, 248, 249, 251, 0, 2, 7, 128, 129, 156, 164, 0, 629,

                                                                                           1044, 1045, 1057, 1373, 1666), tstop = c(1, 7, 221, 248, 249,

                                                                                                                                    251, 253, 2, 7, 128, 129, 156, 164, 344, 629, 1044, 1045, 1057,

                                                                                                                                    1373, 1666, 1724), init_stat = c("(s0)", "(s0)", "(s0)", "(s0)",

                                                                                                                                                                     "(s0)", "(s0)", "(s0)", "(s0)", "(s0)", "(s0)", "(s0)", "(s0)",

                                                                                                                                                                     "(s0)", "(s0)", "Waitlisted", "Waitlisted", "Waitlisted", "Waitlisted",

                                                                                                                                                                     "Waitlisted", "Waitlisted", "Waitlisted"), event = c("", "Waitlisted",

                                                                                                                                                                                                                          "", "Transplanted", "Waitlisted", "Transplanted", "Dead", "",

                                                                                                                                                                                                                          "Waitlisted", "", "", "", "Transplanted", "Dead", "Removed from Waitlist",

                                                                                                                                                                                                                          "", "", "Waitlisted", "", "Removed from Waitlist", "Dead")), row.names = c(NA,

                                                                                                                                                                                                                                                                                                     -21L), class = "data.frame")

check <- survcheck( Surv(

time = tstart,

time2 = tstop,

event = event,

type = 'mstate'

) ~ 1, data = model_data, id = study_id)

check$flag

> overlap gap jump teleport duplicate

> 0 0 0 0 0

fit <- survfit(

Surv(

time = tstart,

time2 = tstop,

event = event,

type = 'mstate'

) ~ 1, data = model_data, id = study_id,

istate = init_stat

)

> Error in survfitCI(X, newY, weights = casewt, stype = stype, ctype = ctype, : one or more flags are >0 in survcheck

Created on 2024-06-06 with reprex v2.1.0https://reprex.tidyverse.org/

However, if I remove the istate argument, then the model fits. This suggests I've perhaps done something wrong with the istate variable.

Is this a problem with survfit, or a problem with what I pass to istate? How should this be rectified if it is a user error?

- Reply to this email directly, view it on GitHubhttps://github.com/therneau/survival/issues/258, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ACWQG565QTGFHYX6TXBYEITZF7OPXAVCNFSM6AAAAABI35JTRWVHI2DSMVQWIX3LMV43ASLTON2WKOZSGMZTOMRWHA2TIMY. You are receiving this because you are subscribed to this thread.Message ID: @.**@.>>

therneau commented 3 months ago

As Beth noted above, coxph calls survcheck internally and will include istate, so to track down details about the error you need to call survcheck yourself in exactly the same manner.