# How to make predictions with time-dependent covariates with Cox regression

I learned about time-dependent covariates in Cox regression in R using the function survSplit of the package survival. I use this as an interaction term for covariates which do not follow the Cox proportionality assumption which works fine.

The problem is now, that I want to make predictions for new datapoints, but how can I do this as I do not know how long they will survive? How do I know which time-value to use in my calculations? If it helps, I wish to calculate the probability of one-year survival. Also, how can I calculate the calibration/discrimination since certain patients will be duplicated in the dataset?

Code:

rm(list = ls(all=T))
library("rms")
library("pec")
data(veteran)

vet2 <- survSplit(Surv(time, status) ~ ., data= veteran, cut=c(90, 180),
episode= "tgroup", id="id")
vfit2 <- cph(Surv(tstart, time, status) ~ trt + prior + karno*strat(tgroup), data=vet2,surv = T,X=T,Y=T)
predictSurvProb(vfit2,newdata=vet2[vet2$id==2,],times = c(121,190))  • In the bounty text you ask for more than what is required to answer the initial question. i.e. you ask for the how to implement time-dependent covariates and the coding to calculate calibration or discrimination using the RMS package. Aside from obtaining predicted probabilities (see my answer below), I believe these merit separate questions, which might possibly be better suited to be posted on stackoverflow.com... – IWS Jul 19 '17 at 13:53 • cont'd. Aside from this, the rms package help pages might contain whatever more you need to calculate calibration&discrimination statistics, and my answer here (stats.stackexchange.com/questions/286595/…) could be of help when modelling time dependent covariates. – IWS Jul 19 '17 at 13:53 ## 2 Answers ## Calculating predicted probabilities using a Cox model There is a way of obtaining prediction out of a Cox model, as survival probability at time $$t$$ ($$S(t)$$) depends on your cox model like so: $$S(t) = e^{-H_0(t) * exp(LP)}$$ in this formula $$H_0(t)$$ is called the baseline hazard at time $$t$$; and $$LP$$ is the linear predictor. If $$X_i$$ are the predictor variables $$1,2,...,i$$ in the Cox model, and $$β_i$$ are the corresponding coefficients from the Cox model, then the linear predictor is calculated like so: $$LP = X_1*β_1 + X_2*β_2 + ... + X_i*β_i$$ You might already be familiar with what a linear predictor is, but I added this for clarity sake. The baseline hazard is a little harder to obtain. Basically it is a function of time, which shows the hazard for an event of an individual who has a $$LP$$ of 0. Due to the way cox regression works this value is not estimated (look at this CV question or others, which sheds more light on baseline hazard). In R however (which I can see the OP uses), there is a function available called 'basehaz()' in the survival package, which will let you extract the baseline hazard at a specific timepoint based on your model's fit. If you extract this baseline hazard you can complete the formula above for any individual in your data, and for unseen data, as long as you are able to calculate the linear predictor for these individuals. Remember this formula results in the probability of survival or not having had the event at time $$t$$. If you want to know the probability of an event at time $$t$$, simply subtract the probability from 1: $$P(event|LP) = 1-S(t) = 1 - e^{-H_0(t) * exp(LP)}$$ Final remark: Note that the basehaz function gives you a baseline hazard at time $$t$$ based on your specific data. So, as is the case for the coefficients, extrapolating this to new cases might not result in a good fit/prediction, due to overfitting, bias, etc. ## Calibration & Discrimination With the predicted probabilities and the event status at one year you can calculate various calibration statistics. For the discrimination statistic (the c-index) there is a specific kind of rank correlation for a censored response variable. In R, the rcorr.cens function from the Hmisc package can provide you with it. Whether these statistics are the most suitable for your research I can not say, as that depends mostly on the specifics (e.g. are you building a new and first model for this setting, or are you comparing to previously developed ones?) • So if I used the SurvSplit at 90 and 180 days on my dataset, should I split any new datapoint obtained at T=0 also at 90 and 180 and calculate the survival probabilities at those times? The reason why I ask this question is that for a new datapoint I ONLY have the info at T=0 and do not know whether the patient survives 90 or 180 days. Lets say the time-dependent covariates are resp. A and B at 90 and 180 days, can I then estimate the survival rate ate 360 days by using the baseline hazard, A, B and the linear predictor? – Héctor van den Boorn Jul 21 '17 at 8:18 • Remember there are two things which can change over time in survival models: (1) the value of the predictor (== time-varying covariate) and (2) the effect of the predictor (== time-varying effect). When studying a causal association between a predictor and the outcome you can include both of these in a model based on your data. If you are specifically interested in predicting the probability of outcome after$t\$ amount of days however, you will have to decide at what moment the prediction takes place... – IWS Jul 21 '17 at 8:57
• If you want to predict at time 0, don't bother modelling time-varying covariates as you will not have this information at time 0. Time-varying effects, though, are something you could take into account (e.g. if you know having certain characteristics at time 0 increases the hazard of dying, but only for the first 30 days, that's something you can model). So before I can further answer your question, which is the purpose of your analysis: causal relation or prediction/prognosis? – IWS Jul 21 '17 at 8:59
• I want to use the model for prediction/prognosis. I was using only a baseline hazard function in combination with a linear predictor. But by looking at the cox.zph function on the model it finds a few predictors whose effect changes over time. E.G. the influence of metastasis is larger earlier on and diminishes over time. I think I need in cran.r-project.org/web/packages/survival/vignettes/timedep.pdf page 15, equation 2. So the beta is a function of time; so in order to calculate survival after 365 days, I think I need to calculate it between 0-90,90-180,180-365? Thanks! – Héctor van den Boorn Jul 21 '17 at 10:25
• OK, so only model time-varying effects. This can be done by modelling interactions with time. If you want to keep using the rms package look at my answer here stats.stackexchange.com/questions/286595/…. Note that you can make interactions with time in categories (0-90,90-180,180-365), or assume a linear or logarithm interaction with time as a continuous variable. Anyway, you will need to use survsplit to make cuts at every day (i.e. not only at 90, 180 and 360 days)! – IWS Jul 21 '17 at 10:29
How do I know which time-value to use in my calculations?


This isn't a question that your model can answer you. How did you choose the cutoff points?

The result of a Cox-PH model is a survival distribution over time for each patient. Without time dependence, one can look at the overall hazard of each patient to compare relative risks between patients. With time dependence, one could use the cumulative hazard.

If you are interested in time dependence in your data, I would suggest other approaches (such as Random Forest survival) rather than modified Cox-PH, which is ad hoc.

• I chose two cutoff points at 90 and 180 days based on the plots. Can I use these points to calculate the discrimination statistic/survival rate at 365 days? – Héctor van den Boorn Jul 14 '17 at 18:01
• A major problem I have is that I have the baseline characteristics for new patients, but only at time t0. How can I use the adjusted time dependent covariates for t=180 days and 360 days? Can you provide a small example? – Héctor van den Boorn Jul 19 '17 at 18:56