chjackson / flexsurv

The flexsurv R package for flexible parametric survival and multi-state modelling
http://chjackson.github.io/flexsurv/
55 stars 27 forks source link

flexsurvspline: calculating the value of log cumulative hazard #47

Closed StephenBarron closed 4 years ago

StephenBarron commented 6 years ago

I'm using your excellent flexsurv package and I'd appreciate your help with a question. I'm trying to calculate the value of the log cumulative hazard linear predictor from the output of flexsurvsline (see below)

m1 <- flexsurvspline(Surv(time, event) ~ variable1, data, k=3, scale="hazard") m1$res est
gamma0 -2.4815072 gamma1 0.2363739 gamma2 -0.2427882 gamma3 0.6080033 gamma4 -0.6974513 variable1 1.0097192

In general, I would calculate linear predictors as the linear combination of the variable parameter estimates and the corresponding data values. In this case, I think gamma's should be multiplied by corresponding values of the basis functions? If so, how/where do I get the basis functions used for the model? My aim is to calculate the log cumulative hazard, and then survival probability, for different values of my variable1.

Many thanks for your help and time Stephen

chjackson commented 6 years ago

The cumulative hazard can be calculated from a fitted model for given covariate values using summary.flexsurvreg(..., newdata=..., type="cumhaz"), or type="survival" for the survival probability. Alternatively Hsurvspline calculates the cumulative hazard for given parameter values and knot locations for this class of model, see also psurvspline.

Have a look at the functions in the source https://github.com/chjackson/flexsurv-dev/blob/master/R/spline.R to see how it works at a lower level. The actual basis functions are implemented in C++ in https://github.com/chjackson/flexsurv-dev/blob/master/src/splines.cpp

StephenBarron commented 6 years ago

Thanks very much.