# Time-dependent coefficients in cox regression CPH (RMS)

I found in the R vignettes a nice article about perform time transformations in coxph (R function, package survival). This works fine for me in coxph, but I need to use cph (RMS package) because of the added functionality. This should be easy to translate, since cph is coxph with added features, but I can't get it to work.

Can some tell me how to add a continuous time transformation to variables in cph?

My code is as follow:

data(lung)
fit = coxph(Surv(time,status) ~ tt(ph.karno) + age, data=lung,x=T,y=T,tt=function(x,t,...) x*log(t))


Which works, but I need this to work:

data(lung)

fit = cph(Surv(time,status) ~ tt(ph.karno) + age, data=lung,x=T,y=T,tt=function(x,t,...) x*log(t))


## 1 Answer

I have struggled with coxph's tt subfunction as well. A (easy) workaround is to work with a 'counting process’ format dataset. In such a dataset the data for each individual is cut into periods of time. Each period will have a start time, stop time, and event status, and this is needed for each record/individual. In the resulting dataset you will be able to use an 'actual' time (is not the survival time) to model interactions with time. Finally, for the way I've used this workaround, you'll need to construct the interaction variable before entering it in the final cph code.

For your example/case I have done this as follows:

library(survival)
library(rms)

data(lung)
# original coxph tt fit
fit <- coxph(Surv(time,status) ~ ph.karno + tt(ph.karno) + age, data=lung,x=T,y=T,tt=function(x,t,...) x*log(t))

# Creating a countingprocess-format survival file using survSplit
lung.long<-survSplit(Surv(time,status)~.,data=lung,cut=1:max(lung$$time),episode="timegroup") # constructing the ph.karno and interaction with log-time variable lung.long$$ph.karnologtime<-lung.long$$ph.karno*log(lung.long$$timegroup)
# some settings required for cph and other rms functions to work
dd <- datadist(lung.long)
options(datadist="dd")
# fitting the cph model
fit.long<-cph(Surv(tstart,time,status)~ph.karno + ph.karnologtime + age,x=T,y=T,data=lung.long)

# comparing the resulting coefficients
exp(fit$$coefficients) exp(fit.long$$coefficients)


The only thing I had not encountered before is that there is a slight difference (on the sixth decimal) between these estimates. I am not aware of a (minor) difference between coxph or cph, but I could not find one in the handling of ties or other default settings of these functions. Anyone with insight into these differences, feel free to comment/edit this post.

• A minor typo in the answer: In the orignal coxph tt fit, a main effect of ph.karno should be added to make the results consistent with fit.long # original coxph tt fit (edited) fit <- coxph(Surv(time,status) ~ ph.karno + tt(ph.karno) + age, data=lung,x=T,y=T,tt=function(x,t,...) x*log(t)) Mar 23, 2020 at 14:39