Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Predict out of sample using flexsurvreg in R

I have the following model in R

library(flexsurv)

data(ovarian)

model = flexsurvreg(Surv(futime, fustat) ~ ecog.ps + rx, data = ovarian, dist='weibull')

model

predict(model,data = ovarian, type = 'response')

The model summary looks like this flexsurvreg model output

I am trying to predict the survival time using the predict function in R and get the following error

error while trying to predict

How can I predict expected lifetime using this flexsurvreg model?

I understand that the documentation mentions a totlos.fs function, but this data does not seem to have a trans variable that totlos.fs requires to provide an output.

If there is no other alternative to totlos.fs how can I create a trans variable in this data and handle it along with existing covariates?

Please advise.

like image 359
Nik Avatar asked Jan 19 '26 07:01

Nik


1 Answers

Section 3 of the supplementary examples doc for the flexsurv documentation has an example in which the predicted values are calculated directly using the model equation. As you are using the Weibull distribution (with n=2 parameters) I believe this should work:

pred.model <- model.matrix(model) %*% model$res[-(1:n),"est"]

Cheers

like image 83
Kivevi Avatar answered Jan 20 '26 23:01

Kivevi