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:
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
R: 4.1.3 64-bit, mstate: 0.3.2, survival: 3.3.1, Win11
Problem
The data frame passed to
msfit
'snewdata
argument must have a variable namedstrata
, permsfit
's help file. However, R hard crashes if that variable's not present and thecoxph
formula has nostrata()
term.Problem's Source
There isn't a check inside
msfit
to ensure thenewdata
object has astrata
column *whencoxph
lacks astrata()
term.* When the.agmssurv
C routine runs (lines 283–293), the entire R session hard crashes, presumably because (?).agmssurv
hasn't received thestrata
variable in the way it expects.There is a line that ensures
newdata$strata
exists, but only when the Cox model object has astrata()
term (msfit
, line 272):A
strata()
term would makehas.strata=TRUE
, based on line 19:If there's no strata term in the original model, though,
has.strata=FALSE
, and line 272'sif()
never gets triggered.MWE