hputter / mstate

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

`msfit`: Hard crash when `is.null(newdata$strata)` + no Cox `strata()` term #15

Closed MetzgerSK closed 1 year ago

MetzgerSK commented 2 years ago

R: 4.1.3 64-bit, mstate: 0.3.2, survival: 3.3.1, Win11

Problem

The data frame passed to msfit's newdata argument must have a variable named strata, per msfit's help file. However, R hard crashes if that variable's not present and the coxph formula has no strata() term.

Problem's Source

There isn't a check inside msfit to ensure the newdata object has a strata column *when coxph lacks a strata() term.* When the .agmssurv C routine runs (lines 283–293), the entire R session hard crashes, presumably because (?) .agmssurv hasn't received the strata variable in the way it expects.

There is a line that ensures newdata$strata exists, but only when the Cox model object has a strata() term (msfit, line 272):

if (has.strata & is.null(newdata$strata))
                stop("no \"strata\" column present in newdata")

A strata() term would make has.strata=TRUE, based on line 19:

has.strata <- !is.null(attr(object$terms, "specials")$strata)

If there's no strata term in the original model, though, has.strata=FALSE, and line 272's if() never gets triggered.

MWE

library(survival)
library(mstate)
library(dplyr)

# Load demo data
dat <- haven::read_dta("http://www.stata-press.com/data/r17/catheter.dta")
dat <- dat %>%
         mutate(start = 0,
                stop = time,
                status = infect,
                from = 1,
                to = 2)

    ## Trans mat
    tmat <- transMat(list(c(2),c()))

    ## Set mstate attributes
    attr(dat, "trans") <- tmat
    class(dat) <- c("msdata", "data.frame")

# Model
mod <- coxph(Surv(start,stop,status) ~ age + female, data=dat)

# msfit
cov.prf <- data.frame(age=25, female=1, strata=1)
msfit(mod, cov.prf, trans=tmat) # R doesn't crash
                                ## (throws a different error [issue #16], but that
                                ##  error occurs after the .agmssurv call)
cov.prf$strata <- NULL
msfit(mod, cov.prf, trans=tmat) # WARNING: will hard crash R
edbonneville commented 1 year ago

Closed along with #14 - use of strata() now obligatory.