[R] Survival analysis: ERROR: Time and status are different lengths

120 views
Skip to first unread message

Marleen Hamoen

unread,
Dec 3, 2015, 12:45:54 PM12/3/15
to r-h...@r-project.org
Hi,

I am fitting an AFT model assuming a Weibull distribution and I would like
to check the residuals compared to the Kaplan Meier residuals, but when I
try to create the Kaplan Meier residuals I get an error: Time and status
are different lengths.

I am using the following script:

# Fitting the AFT model
fit.weib <- survreg(Surv(TimeDeath, event) ~ age + sex + mutation +
ventilation + BM1 + BM2, data = DF, dist = "weibull")
fits <- fit.weib$linear.predictors
resids <- (fit.weib$y[, 1] - fits)/fit.weib$scale
resKM <- survfit(Surv(resids, event) ~ 1, data = DF)

I get the error from the last line of the script.

I tried some things that didn't work and I was wondering if anyone could
help me.
If you need more information please let me know.

Thanks in advance,

M

[[alternative HTML version deleted]]

______________________________________________
R-h...@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Therneau, Terry M., Ph.D.

unread,
Dec 4, 2015, 9:02:58 AM12/4/15
to r-h...@r-project.org, Marleen Hamoen
I expect that reading the result of print(fit.weib) will answer your question. If there
were any missing values in the data set, then the fit.weib$linear.predictors will be
shorter than the original data set,
and the printout will have a note about "...deleted due to missing".

The simplest solution to this is to set
options(na.action="na.exclude")
before doing the fit. Then predict(fit) and resid(fit) will return vectors of the same
length as the input data, containing NA in the appropriate positions. The default
na.action of "na.omit" leaves missing out of both the fit and the residuals.

(Unfortunately, only a few modeling functions in R pay attention to the difference between
these two na.action options.)

Terry Therneau


On 12/04/2015 05:00 AM, r-help-...@r-project.org wrote:
> Hi,
>
> I am fitting an AFT model assuming a Weibull distribution and I would like
> to check the residuals compared to the Kaplan Meier residuals, but when I
> try to create the Kaplan Meier residuals I get an error: Time and status
> are different lengths.
>
> I am using the following script:
>
> # Fitting the AFT model
> fit.weib <- survreg(Surv(TimeDeath, event) ~ age + sex + mutation +
> ventilation + BM1 + BM2, data = DF, dist = "weibull")
> fits <- fit.weib$linear.predictors
> resids <- (fit.weib$y[, 1] - fits)/fit.weib$scale
> resKM <- survfit(Surv(resids, event) ~ 1, data = DF)
>
> I get the error from the last line of the script.
>
> I tried some things that didn't work and I was wondering if anyone could
> help me.
> If you need more information please let me know.
>
> Thanks in advance,

Marleen Hamoen

unread,
Dec 4, 2015, 3:53:50 PM12/4/15
to Therneau, Terry M., Ph.D., r-h...@r-project.org
Hi Terry,

I already suspected it had something to do with missing values in one of
the covariates. I couldn't get the na.action="na.exclude" to work (perhaps
this is because I am relatively unexperienced with R), but I managed to
solve the problem by using the following command with "complete.cases":

DF <- with(DF,DF[complete.cases(TimeDeath, event, age, sex, mutation,
ventilation, BM1, BM2), ])

Thank you very much for your help!
[[alternative HTML version deleted]]
Reply all
Reply to author
Forward
0 new messages