therneau / survival

Survival package for R
381 stars 104 forks source link

survfit.coxph doesn't support Surv2-type data #264

Open steliosbl opened 2 months ago

steliosbl commented 2 months ago

Hello, I'm running into an error when trying to obtain survival curves from a CoxPH fitted using timeline-type data with a Surv2 formula.

Error in aeqSurv(Y) : argument is not a Surv object

Here is a barebones example, using the mgus data and the exact code from the vignette.

# Preparing the dataframe in timeline format
ctime <- with(mgus2, ifelse(pstat==1, ptime, futime))
cstat <- with(mgus2, ifelse(pstat==1, 1, 2*death))
cstat <- factor(cstat, 0:2, c("censor", "PCM", "death"))
tdata <- data.frame(id=mgus2$id, days=ctime, cstat=cstat)
mdata2 <- data.frame(mgus2[,1:7], days=0)
mdata2 <- merge(mdata2, tdata, all=TRUE)

# Fitting the cox model
cph <-  coxph(Surv2(days, cstat) ~ age + sex, id=id, mdata2)

# Attempting to obtain survival curves for two new records
ndata <- expand.grid(sex=c("F", "M"), age=c(60, 80))
survfit(cph, newdata=ndata) 
> Error in aeqSurv(Y) : argument is not a Surv object

I have traced the error inside survfit.coxph to the variable mf not taking the value it's supposed to take (survfit.coxph.R:83):

mf <- stats::model.frame(object)

When called with a coxph object that uses a Surv-type formula, mf is as expected. When the formula is a Surv2, however, it appears to return an empty frame, which then dominoes into the error I'm experiencing.

Am I making a mistake when calling survfit.coxph? Or does it currently not support Surv2?

Thank you

therneau commented 2 months ago

I have a few other things to do before I tackle this. Surv2 was an experimental idea which never worked out, at least in the sense of being useful to my own work, so I have not paid much attention to it. But I'll take a look.