# Cox regression with time-varying coefficients

Hello everyone,
I am attempting to build Cox regression model with time-varying coefficients in Stan.

I am stuck because I do not know how to describe time functions?
Is there any example or R package that does Cox regression with time-varying coefficients in a Bayesian framework?

Not exactly. Search for survival models in the top right of Discourse for a bunch of threads.

ok, thanks!

To have a start, I recommend have a look at survivalstan. It uses a piecewise exponential hazard and the Poisson representation of it. It supports time varying coefficients, however its interface is in Python. Nevertheless the GitHub contains Stan models, you should be able to work with. I recommend to carefully check the Stan code, though.

We currently work on time varying coefficients in a model that uses splines for the log cumulative baseline hazard. Essentially the proposal of Royston&Parmar, however there we have to find a way to work around the potentially negative likelihoods in the original formulation… see https://github.com/stan-dev/rstanarm/issues/280.

Also I wonder whether for your type of problem you couldn’t work with stan_jm? It’s not giving you directly time dependent coefficients but you could work with the association structures in such models, provided you have longitudinal measurements per individual? Or do you really have only one measurement for each covariate and individual and you really want to get away from proportional hazards?

Thank you!
I have looked at it before, and didn’t get what are the groups, I mean how can I split the data to groups!?
I was looking for the solution, I am not sure if it’s valid or not, but I splitted the data to 3 groups just by using survSplit() function of the “survival” package. And used Cox regression for each of group, since the coefficients and baseline should be estimated for groups separately.
however, I am not sure if that is ok?!

An alternative worth to consider, would be to use an proportional odds model where you model the log of the odds O(t;\mathbf{z})=[1-S(t;\mathbf{z}])/S(t;\mathbf{z}) of an individual at time t with covariate vector \mathbf{z} as

\ln O(t;\mathbf{z}) = s(\ln t;\gamma) + \beta^T \mathbf{z}

where s(x;\gamma) can be an i-spline or natural cubic spline. See Royston & Parmar. It turns out that in such a formulation you also have time dependent hazard ratios, i.e.

\frac{h(t;\mathbf{z}_1)}{h(t;\mathbf{z}_2)} = \frac{1+e^{s(\ln t;\gamma) + \beta^T \mathbf{z_2}}}{1+e^{s(\ln t;\gamma) + \beta^T \mathbf{z_1}}} e^{\beta^T (\mathbf{z}_1 - \mathbf{z}_2)}

I implemented the proportional-hazard model of RP, using i-splines, here. I have tested it on various datasets. It should be straightforward to extend this to the proportional-odds model (see RP paper); you just need to change the log-likelihood calculation.

Just found this fully self-contained PyMC3 tutorial, which also covers time varying effects:

https://docs.pymc.io/notebooks/survival_analysis.html

If you are willing to consider Python + PyMC3, that could get you quite far…

Thanks!
Just an amazing explanation of each detail!