hputter / mstate

https://hputter.github.io/mstate/
7 stars 5 forks source link

`msfit`: Truncated `trans.new` Contents #27

Closed MetzgerSK closed 5 months ago

MetzgerSK commented 1 year ago

R: 4.3.0 64-bit, mstate: dev release @ 09f421f, survival: 3.5.5, Win11

Problem

One of the msfit strata-related temporary fixes from 09f421f isn't working as it should. Specifically, when vartype=="aalen", the trans.new object will contain a scalar instead of a vector of values. As a consequence, msfit will return an incomplete list of transition-specific cumulative hazards in Haz.

Problem's Source

msfit() (lines 159–160):

trans.new <- ifelse(is.null(sf0$strata), 1L, 
    as.numeric(sf0$strata))

ifelse is returning a result that's the same length as its first argument (test in the ifelse() documentation). is.null(sf0$strata) has length 1, meaning as.numeric(sf0$strata)'s contents will be truncated to length 1, regardless of its true length.

Looks like the issue's resolved if you switch the line in question to:

trans.new <- if(is.null(sf0$strata)) 1L else as.numeric(sf0$strata)

MWE

rm(list=ls())
library(mstate)

# Load data ----
dat <- structure(list(
         subject = structure(c(1, 1, 1, 1, 2, 2, 2, 2, 
            3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5)), 
         t0 = structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
            0, 0, 0, 0, 0, 0)), 
         t = structure(c(1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 
            3, 3, 3, 3, 3, 3)), 
         status = structure(c(1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 
            0, 0, 0, 1, 0, 0, 1, 0)), 
         trans = structure(c(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 
            2, 3, 4, 1, 2, 3, 4))), 
        class = c("data.frame"))

# Estm model ----
mod <- coxph(Surv(t0,t,status) ~ strata(trans), data=dat)

# Set mstate details ----
tmat <- trans.comprisk(4)

# survfit, for comparison
(sf.dat <- summary(survfit(mod)))   # has all four transitions

# msfit ---- 
## Aalen ====
msf.dat <- msfit(mod, trans=tmat) 
msf.dat$Haz                         # is missing transitions 2-4
## Greenwood ====
msf.datG <- msfit(mod, trans=tmat, 
                  vartype="greenwood") 
msf.datG$Haz                        # has all four transitions
edbonneville commented 5 months ago

Thanks for catching this @MetzgerSK -now fixed in the master branch!