File failed to load: /extensions/MathZoom.js

Estimating Survival (Time-to-Event) Models with rstanarm

Sam Brilleman

2019-06-18

Preamble

This vignette provides an introduction to the stan_surv modelling function in the rstanarm package. The stan_surv function allows the user to fit survival models (sometimes known as time-to-event models) under a Bayesian framework.

Introduction

Survival (a.k.a. time-to-event) analysis is generally concerned with the time from some defined baseline (e.g. diagnosis of a disease) until an event of interest occurs (e.g. death or disease progression).

In standard survival analysis, one event time is measured for each observational unit. In practice however that event time may be unobserved due to left, right, or interval censoring, in which case the event time is only known to have occurred within the relevant censoring interval.

There are two common approaches to modelling survival data. The first is to model the rate of the event (known as the hazard) as a function of time – the class of models known as proportional and non-proportional hazards regression models. The second is to model the event time directly – the class of models known as accelerated failure time (AFT) models. In addition, a number of extensions to standard survival analysis have been proposed. These include the handling of multiple (recurrent) events, competing events, clustered survival data, cure models, and more.

The intention is for the stan_surv modelling function in the rstanarm package to provide functionality for fitting a wide range of Bayesian survival models. The current implementation allows for the following model formulations:

Under each of those model formulations the following are allowed:

Technical details

Data and notation

We assume that a true event time for individual i (i=1,...,N) exists, denoted Ti, but that in practice it may or may not observed due to left, right, or interval censoring. Therefore, in practice we observe outcome data Di={Ti,TiU,TiE,di} for individual i where:

and di{0,1,2,3} denotes an event indicator taking value:

The hazard rate, cumulative hazard, and survival probability

The hazard of the event at time t is the instantaneous rate of occurrence for the event at time t. Mathematically, it is defined as:

hi(t)=limΔt0P(tTi<t+Δt|Ti>t)Δt

where Δt is the width of some small time interval. The numerator is the conditional probability of the individual experiencing the event during the time interval [t,t+Δt), given that they were still at risk of the event at time t. The denominator converts the conditional probability to a rate per unit of time. As Δt approaches the limit, the width of the interval approaches zero and the instantaneous event rate is obtained.

The cumulative hazard is defined as:

Hi(t)=s=0thi(s)ds

and the survival probability is defined as:
Si(t)=exp[Hi(t)]=exp[s=0thi(s)ds]

It can be seen here that in the standard survival analysis setting there is a one-to-one relationship between each of the hazard, the cumulative hazard, and the survival probability. These quantities are also used to form the likelihood for the survival model described in the later sections.

Hazard scale formulations

When basehaz is set equal to "exp", "weibull", "gompertz", "ms" (the default), or "bs" then the model is defined on the hazard scale as described by the following parameterisations.

We model the hazard of the event for individual i at time t using the regression model:

hi(t)=h0(t)exp[ηi(t)]

where h0(t) is the baseline hazard (i.e. the hazard for an individual with all covariates set equal to zero) at time t, and ηi(t) denotes the linear predictor evaluated for individual i at time t. For full generality, we allow the linear predictor to be time-varying; that is, it may be a function of time-varying covariates or time-varying coefficients (i.e. a time-varying hazard ratio). However, if there are no time-varying covariates or time-varying coefficients in the model, then the linear predictor reduces to a time-fixed quantity and the definition of the hazard function reduces to:
hi(t)=h0(t)exp[ηi]

Our linear predictor is defined as:

ηi(t)=β0+p=1Pβp(t)xip(t)

where β0 denotes the intercept parameter, xip(t) denotes the observed value of pth (p=1,...,P) covariate for the ith (i=1,...,N) individual at time t, and βp(t) denotes the coefficient for the pth covariate.

The quantity exp(βp(t)) is referred to as a “hazard ratio”. The hazard ratio (HR) quantifies the relative increase in the hazard that is associated with a unit-increase in the relevant covariate, xip; e.g. a hazard ratio of 2 means that a unit-increase in the covariate leads to a doubling in the hazard (i.e. the instantaneous rate) of the event. The hazard ratio can be treated as a time-fixed quantity (i.e. proportional hazards) or time-varying quantity (i.e. non-proportional hazards), as described in later sections.

Distributions

  • Exponential model (basehaz = "exp"): for scale parameter λi(t)=exp(ηi(t)) we have:

    hi(t)=λi(t)

  • Weibull model (basehaz = "weibull"): for scale parameter λi(t)=exp(ηi(t)) and shape parameter γ>0 we have:

    hi(t)=γtγ1λi(t)

  • Gompertz model (basehaz = "gompertz"): for shape parameter λi(t)=exp(ηi(t)) and scale parameter γ>0 we have:

    hi(t)=exp(γt)λi(t)

  • M-splines model (basehaz = "ms", the default): letting M(t;γ,k0,δ) denote a degree δ M-spline function with basis evaluated at a vector of knot locations $ = $ and parameter vector γ>0 we have:

    hi(t)=M(t;γ,k0,δ)exp(ηi(t))

    The M-spline function is calculated using the method described in Ramsay (1988) and implemented in the splines2 R package (Wang and Yan (2018)). To ensure that the hazard function hi(t) is not constrained to zero at the origin (i.e. when t approaches 0) the M-spline basis incorporates an intercept. To ensure identifiability of both the intercept parameter in the M-spline function and the intercept parameter in the linear predictor (i.e. β0) we constrain the M-spline coefficients to a simplex, that is, j=1Jγj=1. The default degree in rstanarm is δ=3; that is, cubic M-splines. However this can be controlled by the user via the basehaz_ops argument. It is worthwhile noting that δ=0 would correspond to a piecewise constant baseline hazard.

  • B-splines model (for the log baseline hazard): letting B(t;γ,k0,δ) denote a degree δ B-spline function with basis evaluated at a vector of knot locations k0 and parameter vector γ we have:

    hi(t)=exp(B(t;γ,k0,δ)+ηi(t))

    The B-spline function is calculated using the method implemented in the splines2 R package (Wang and Yan (2018)). The B-spline basis does not require an intercept and therefore does not include one; any constant shift in the log hazard is fully captured via the intercept in the linear predictor (i.e. β0).

Note: When the linear predictor is not time-varying (i.e. under proportional hazards) there is a closed form expression for the survival probability (except for the B-splines model); details shown in the appendix. However, when the linear predictor is time-varying (i.e. under non-proportional hazards) there is no closed form expression for the survival probability; instead, quadrature is used to evaluate the survival probability for inclusion in the likelihood. Extended details on the parameterisations are given in the appendix.

Accelerated failure time formulations

When basehaz is set equal to "exp-aft", or "weibull-aft" then the model is defined on the accelerated failure time scale as described by the following parameterisations.

Following Hougaard (1999), we model the survival probability for individual i at time t using the regression model:

Si(t)=S0(0texp[ηi(u)]du)

where S0(t) is the baseline survival probability at time t, and ηi(t) denotes the linear predictor evaluated for individual i at time t. For full generality, we allow the linear predictor to be time-varying; that is, it may be a function of time-varying covariates or time-varying coefficients (i.e. a time-varying acceleration factor). However, if there are no time-varying covariates or time-varying coefficients in the model, then the linear predictor reduces to a time-fixed quantity (i.e. ηi(t)=ηi) and the definition of the survival probability reduces to:
Si(t)=S0(texp[ηi])

Our linear predictor is defined as:

ηi(t)=β0+p=1Pβp(t)xip(t)

where β0 denotes the intercept parameter, xip(t) denotes the observed value of pth (p=1,...,P) covariate for the ith (i=1,...,N) individual at time t, and βp(t) denotes the coefficient for the pth covariate.

The quantity exp(βp(t)) is referred to as an “acceleration factor” and the quantity exp(βp(t)) is referred to as a “survival time ratio”. The acceleration factor (AF) quantifies the acceleration (or deceleration) of the event process that is associated with a unit-increase in the relevant covariate, xip; e.g. an acceleration factor of 0.5 means that a unit-increase in the covariate leads to an individual approaching the event at half the speed. If you find that somewhat confusing, then it may be easier to think about the survival time ratio. The survival time ratio (STR) is interpreted as the increase (or decrease) in the expected survival time that is associated with a unit-increase in the relevant covariate, xip; e.g. a survival time ratio of 2 (which is equivalent to an acceleration factor of 0.5) means that a unit-increase in the covariate leads to an doubling in the expected survival time. The survival time ratio is equal to the inverse of the acceleration factor (i.e. STR=1/AF).

Distributions

  • Exponential model (basehaz = "exp-aft"): When the linear predictor is time-varying we have:

    Si(t)=exp(0texp(ηi(u))du)

    and when the linear predictor is time-fixed we have:
    Si(t)=exp(tλi)

    for scale parameter λi=exp(ηi).

  • Weibull model (basehaz = "weibull-aft"): When the linear predictor is time-varying we have:

    Si(t)=exp([0texp(ηi(u))du]γ)

    for shape parameter γ>0 and when the linear predictor is time-fixed we have:
    Si(t)=exp(tγλi)

    for scale parameter λi=exp(γηi) and shape parameter γ>0.

Note: When the linear predictor is not time-varying (i.e. under time-fixed acceleration), there is a closed form expression for both the hazard function and survival function; details shown in the appendix. However, when the linear predictor is time-varying (i.e. under time-varying acceleration) there is no closed form expression for the hazard function or survival probability; instead, quadrature is used to evaluate the cumulative acceleration factor, which in turn is used to evaluate the hazard function and survival probability for inclusion in the likelihood. Extended details on the parameterisations are given in the appendix.

Time-fixed and time-varying effects of covariates

The coefficient βp(t) (i.e. the log hazard ratio) or βp(t) (i.e. log survival time ratio) can be treated as a time-fixed quantity (e.g. βp(t)=βp) or as a time-varying quantity. We refer to the latter as time-varying effects because the effect of the covariate is allowed to change as a function of time. In stan_surv time-varying effects are specified by using the tde function in the model formula. Note that in the following definitions we only refer to βp(t) (i.e. the log hazard ratio) but the same methodology applies to βp(t) (i.e. the log survival time ratio).

Without time-varying effects we have:

βp(t)=θp0

such that θp0 is a time-fixed log hazard ratio (or log survival time ratio).

With time-varying effects modelled using B-splines we have:

βp(t)=θp0+m=1MθpmBm(t;k,δ)

where θp0 is a constant, Bm(t;k,δ) is the mth (m=1,...,M) basis term for a degree δ B-spline function evaluated at a vector of knot locations k={k1,...,kJ}, and θpm is the mth B-spline coefficient. By default cubic B-splines are used (i.e. δ=3). These allow the log hazard ratio (or log survival time ratio) to be modelled as a smooth function of time.

The degrees of freedom is equal to the number of additional parameters required to estimate a time-varying coefficient relative to a time-fixed coefficient. When a B-spline function is used to model the time-varying coefficient the degrees of freedom are M=J+δ2 where J is the total number of knots (including boundary knots).

With time-varying effects modelled using a piecewise constant function we have:

βp(t)=θp0+m=1MθpmI(km+1<tkm+2)

where I(x) is an indicator function taking value 1 if x is true and 0 otherwise, θp0 is a constant corresponding to the log hazard ratio (or log survival time ratio for AFT models) in the first time interval, θpm is the deviation in the log hazard ratio (or log survival time ratio) between the first and (m+1)th (m=1,...,M) time interval, and k={k1,...,kJ} is a sequence of knot locations (i.e. break points) that includes the lower and upper boundary knots. This allows the log hazard ratio (or log survival time ratio) to be modelled as a piecewise constant function of time.

The degrees of freedom is equal to the number of additional parameters required to estimate a time-varying coefficient relative to a time-fixed coefficient. When a piecewise constant function is used to model the time-varying coefficient the degrees of freedom are M=J2 where J is the total number of knots (including boundary knots).

Default knot locations: The vector of knot locations k={k1,...,kJ} includes a lower boundary knot k1 at the earliest entry time (equal to zero if there isn’t delayed entry) and an upper boundary knot kJ at the latest event or censoring time. The boundary knots cannot be changed by the user. Internal knot locations – that is k2,...,k(J1) when J3 – can be explicitly specified by the user (see the knots argument to the tve function) or are determined by default. The default is to place the internal knots at equally spaced percentiles of the distribution of uncensored event times. When a B-spline function is specified, the tve function uses default values M=3 (degrees of freedom) and δ=3 (cubic splines) which in fact corresponds to a cubic B-spline function with no internal knots. When a piecewise constant function is specified, the tve function uses a default value of M=3 (degrees of freedom) which corresponds to internal knots at the 25th, 50th, and 75th percentiles of the distribution of the uncensored event times.

Note on subscripts: We have dropped the subscript p from the knot locations k and degree of the B-splines δ discussed above. This is just for simplicity of the notation. In fact, if a model has time-varying effects estimated for more than one covariate, then each these can be modelled using different knot locations and/or degree if the user desires.

Likelihood

Allowing for the three forms of censoring and potential delayed entry (i.e. left truncation) the likelihood for the survival model takes the form:

p(Di|γ,β)=[hi(Ti)]I(di=1)×[Si(Ti)]I(di{0,1})×[1Si(Ti)]I(di=2)×[Si(Ti)Si(TiU)]I(di=3)×[Si(TiE)]1

Priors

The prior distribution for the so-called “auxiliary” parameters (i.e. γ for the Weibull and Gompertz models, or γ for the M-spline and B-spline models) is specified via the prior_aux argument to stan_surv. Choices of prior distribution include:

These choices are described in greater detail in the stan_surv or priors help file.

The prior distribution for the intercept parameter in the linear predictor is specified via the prior_intercept argument to stan_surv. Choices include the normal, t, or Cauchy distributions. The default is a normal distribution with mean zero and scale 20. Note that – internally (but not in the reported parameter estimates) – the prior is placed on the intercept after centering the predictors at their sample means and after applying a constant shift of log(ET) where E is the total number of events and T is the total follow up time. For example, a prior specified by the user as prior_intercept = normal(0,20) is in fact not centered on an intercept of zero when all predictors are at their sample means, but rather, it is centered on the log crude event rate when all predictors are at their means. This is intended to help with numerical stability and sampling, but does not impact on the reported estimates (i.e. the intercept is back-transformed before being returned to the user).

The choice of prior distribution for the time-fixed coefficients θp0 (p=1,...,P) is specified via the prior argument to stan_surv. This can any of the standard prior distributions allowed for regression coefficients in the rstanarm package; see the priors vignette and the stan_surv help file for details.

The additional coefficients required for estimating time-varying effects (i.e. the B-spline coefficients or the interval-specific deviations in the piecewise constant function) are given a random walk prior of the form θp,1N(0,1) and θp,mN(θp,m1,τp) for m=2,...,M, where M is the total number of cubic B-spline basis terms. The prior distribution for the hyperparameter τp is specified via the prior_smooth argument to stan_surv. Lower values of τp lead to a less flexible (i.e. smoother) function. Choices of prior distribution for the hyperparameter τp include an exponential, half-normal, half-t, or half-Cauchy distribution, and these are detailed in the stan_surv help file.

Usage examples

Example: A flexible parametric proportional hazards model

We will use the German Breast Cancer Study Group dataset (see ?rstanarm-datasets for details and references). In brief, the data consist of N=686 patients with primary node positive breast cancer recruited between 1984-1989. The primary response is time to recurrence or death. Median follow-up time was 1084 days. Overall, there were 299 (44%) events and the remaining 387 (56%) individuals were right censored. We concern our analysis here with a 3-category baseline covariate for cancer prognosis (good/medium/poor).

First, let us load the data and fit the proportional hazards model

The model here is estimated using the default cubic M-splines (with 5 degrees of freedom) for modelling the baseline hazard. Since there are no time-varying effects in the model (i.e. we did not wrap any covariates in the tve() function) there is a closed form expression for the cumulative hazard and survival function and so the model is relatively fast to fit. Specifically, the model takes ~3.5 sec for each MCMC chain based on the default 2000 (1000 warm up, 1000 sampling) MCMC iterations.

We can easily obtain the estimated hazard ratios for the 3-catgeory group covariate using the generic print method for stansurv objects, as follows

stan_surv
 baseline hazard: M-splines on hazard scale
 formula:         Surv(recyrs, status) ~ group
 observations:    686
 events:          299 (43.6%)
 right censored:  387 (56.4%)
 delayed entry:   no
------
                Median MAD_SD exp(Median)
(Intercept)     -0.671  0.202     NA     
groupMedium      0.798  0.200  2.222     
groupPoor        1.585  0.169  4.880     
m-splines-coef1  0.001  0.001     NA     
m-splines-coef2  0.006  0.005     NA     
m-splines-coef3  0.147  0.029     NA     
m-splines-coef4  0.268  0.055     NA     
m-splines-coef5  0.102  0.078     NA     
m-splines-coef6  0.214  0.142     NA     
m-splines-coef7  0.235  0.164     NA     

------
* For help interpreting the printed output see ?print.stanreg
* For info on the priors used see ?prior_summary.stanreg

We see from this output we see that individuals in the groups with Poor or Medium prognosis have much higher rates of death relative to the group with Good prognosis (as we might expect!). The hazard of death in the Poor prognosis group is approximately 5.0-fold higher than the hazard of death in the Good prognosis group. Similarly, the hazard of death in the Medium prognosis group is approximately 2.3-fold higher than the hazard of death in the Good prognosis group.

It may also be of interest to compare the different types of the baseline hazard we could potentially use. Here, we will fit a series of models, each with a different baseline hazard specification

and then plot the baseline hazards with 95% posterior uncertainty limits using the generic plot method for stansurv objects (note that the default plot for stansurv objects is the estimated baseline hazard). We will write a little helper function to adjust the y-axis limits, add a title, and centre the title, on each plot, as follows

We can also compare the fit of these models using the loo method for stansurv objects


Model comparison: 
(ordered by highest ELPD)

              elpd_diff se_diff
mod1_mspline1   0.0       0.0  
mod1_bspline   -1.2       1.3  
mod1_mspline2  -1.9       1.7  
mod1_weibull  -18.7       5.5  
mod1_gompertz -32.3       6.0  
mod1_exp      -36.7       5.6  

where we see that models with a flexible parametric (spline-based) baseline hazard fit the data best followed by the standard parametric (Weibull, Gompertz, exponential) models. Roughly speaking, the B-spline and M-spline models seem to fit the data equally well since the differences in elpd or looic between the models are very small relative to their standard errors. Moreover, increasing the degrees of freedom for the M-splines from 5 to 10 doesn’t seem to improve the fit (that is, the default degrees of freedom df = 5 seems to provide sufficient flexibility to model the baseline hazard).

After fitting the survival model, we often want to estimate the predicted survival function for individual’s with different covariate patterns. Here, let us estimate the predicted survival function between 0 and 5 years for an individual in each of the prognostic groups. To do this, we can use the posterior_survfit method for stansurv objects, and it’s associated plot method. First let us construct the prediction (covariate) data

   group
1   Good
2 Medium
3   Poor

and then we will generate the posterior predictions

stan_surv predictions
 num. individuals: 3 
 prediction type:  event free probability 
 standardised?:    no 
 conditional?:     no 

  id cond_time   time median  ci_lb  ci_ub
1  1        NA 0.0000 1.0000 1.0000 1.0000
2  1        NA 0.3571 0.9966 0.9937 0.9979
3  1        NA 0.7143 0.9838 0.9769 0.9882
4  1        NA 1.0714 0.9604 0.9463 0.9711
5  1        NA 1.4286 0.9316 0.9085 0.9486
6  1        NA 1.7857 0.9034 0.8750 0.9271

Here we note that the id variable in the data frame of posterior predictions identifies which row of newdata the predictions correspond to. For demonstration purposes we have also shown a couple of other arguments in the posterior_survfit call, namely

Let us now plot the survival predictions. We will relabel the id variable with meaningful labels identifying the covariate profile of each new individual in our prediction data

We can see from the plot that predicted survival is worst for patients with a Poor diagnosis, and best for patients with a Good diagnosis, as we would expect based on our previous model estimates.

Alternatively, if we wanted to obtain and plot the predicted hazard function for each individual in our new data (instead of their survival function), then we just need to specify type = "haz" in our posterior_survfit call (the default is type = "surv"), as follows

We can quite clearly see in the plot the assumption of proportional hazards. We can also see that the hazard is highest in the Poor prognosis group (i.e. worst survival) and the hazard is lowest in the Good prognosis group (i.e. best survival). This corresponds to what we saw in the plot of the survival functions previously.

Example: Non-proportional hazards modelled using B-splines

To demonstrate the implementation of time-varying effects in stan_surv we will use a simulated dataset, generated using the simsurv package (Brilleman, 2018).

We will simulate a dataset with N=200 individuals with event times generated under the following Weibull hazard function

hi(t)=γtγ1λexp(β(t)xi)

with scale parameter λ=0.1, shape parameter γ=1.5, binary baseline covariate XiBern(0.5), and time-varying hazard ratio β(t)=0.5+0.2t. We will enforce administrative censoring at 5 years if an individual’s simulated event time is >5 years.

  id eventtime status trt
1  1  3.202809      1   0
2  2  4.907130      1   1
3  3  4.453174      1   1
4  4  2.566302      1   0
5  5  5.000000      0   1
6  6  4.262667      1   1

Now that we have our simulated dataset, let us fit a model with time-varying hazard ratio for trt

The tve function is used in the model formula to state that we want a time-varying effect (i.e. a time-varying coefficient) to be estimated for the variable trt. By default, a cubic B-spline basis with 3 degrees of freedom (i.e. two boundary knots placed at the limits of the range of event times, but no internal knots) is used for modelling the time-varying log hazard ratio. If we wanted to change the degree, knot locations, or degrees of freedom for the B-spline function we can specify additional arguments to the tve function.

For example, to model the time-varying log hazard ratio using quadratic B-splines with 4 degrees of freedom (i.e. two boundary knots placed at the limits of the range of event times, as well as two internal knots placed – by default – at the 33.3rd and 66.6th percentiles of the distribution of uncensored event times) we could specify the model formula as

Let us now plot the estimated time-varying hazard ratio from the fitted model. We can do this using the generic plot method for stansurv objects, for which we can specify the plotfun = "tve" argument. (Note that in this case, there is only one covariate in the model with a time-varying effect, but if there were others, we could specify which covariate(s) we want to plot the time-varying effect for by specifying the pars argument to the plot call).

From the plot, we can see how the hazard ratio (i.e. the effect of treatment on the hazard of the event) changes as a function of time. The treatment appears to be protective during the first few years following baseline (i.e. HR < 1), and then the treatment appears to become harmful after about 4 years post-baseline. Thankfully, this is a reflection of the model we simulated under!

The plot shows a large amount of uncertainty around the estimated time-varying hazard ratio. This is to be expected, since we only simulated a dataset of 200 individuals of which only around 70% experienced the event before being censored at 5 years. So, there is very little data (i.e. very few events) with which to reliably estimate the time-varying hazard ratio. We can also see this reflected in the differences between our data generating model and the estimates from our fitted model. In our data generating model, the time-varying hazard ratio equals 1 (i.e. the log hazard ratio equals 0) at 2.5 years, but in our fitted model the median estimate for our time-varying hazard ratio equals 1 at around ~3 years. This is a reflection of the large amount of sampling error, due to our simulated dataset being so small.

Example: Non-proportional hazards modelled using a piecewise constant function

In the previous example we showed how non-proportional hazards can be modelled by using a smooth B-spline function for the time-varying log hazard ratio. This is the default approach when the tve function is used to estimate a time-varying effect for a covariate in the model formula. However, another approach for modelling a time-varying log hazard ratio is to use a piecewise constant function. If we want to use a piecewise constant for the time-varying log hazard ratio (instead of the smooth B-spline function) then we just have to specify the type argument to the tve function.

We will again simulate some survival data using the simsurv package to show how a piecewise constant hazard ratio can be estimated using stan_surv.

Similar to the previous example, we will simulate a dataset with N=500 individuals with event times generated under a Weibull hazard function with scale parameter λ=0.1, shape parameter γ=1.5, and binary baseline covariate XiBern(0.5). However, in this example our time-varying hazard ratio will be defined as β(t)=0.5+0.7×I(t>2.5) where I(X) is the indicator function taking the value 1 if X is true and 0 otherwise. This corresponds to a piecewise constant log hazard ratio with just two “pieces” or time intervals. The first time interval is [0,2.5] during which the true hazard ratio is exp(0.5)=0.61. The second time interval is (2.5,] during which the true log hazard ratio is exp(0.5+0.7)=1.22. Our example uses only two time intervals for simplicity, but in general we could easily have considered more (although it would have required couple of additional lines of code to simulate the data). We will again enforce administrative censoring at 5 years if an individual’s simulated event time is >5 years.

  id eventtime status trt
1  1  4.045842      1   0
2  2  5.000000      0   1
3  3  5.000000      0   1
4  4  5.000000      0   1
5  5  2.773858      1   0
6  6  1.007948      1   0

We now estimate a model with a piecewise constant time-varying effect for the covariate trt as

This time we specify some additional arguments to the tve function, so that our time-varying effect corresponds to the true data generating model used to simulate our event times. Specifically, we specify type = "pw" to say that we want the time-varying effect (i.e. the time-varying log hazard ratio) to be estimated using a piecewise constant function and knots = 2.5 says that we only want one internal knot placed at the time t=2.5.

We can again use the generic plot function with argument plotfun = "tve" to examine our estimated hazard ratio for treatment

Here we see that the estimated hazard ratio reasonably reflects our true data generating model (i.e. a hazard ratio of 0.6 during the first time interval and a hazard ratio of 1.2 during the second time interval) although there is a slight discrepancy due to the sampling variation in the simulated event times.

Example: Hierarchical survival models

To demonstrate the estimation of a hierarchical model for survival data in stan_surv we will use the frail dataset (see help("rstanarm-datasets") for a description). The frail datasets contains simulated event times for 200 patients clustered within 20 hospital sites (10 patients per hospital site). The event times are simulated from a parametric proportional hazards model under the following assumptions: (i) a constant (i.e. exponential) baseline hazard rate of 0.1; (ii) a fixed treatment effect with log hazard ratio of 0.3; and (iii) a site-specific random intercept (specified on the log hazard scale) drawn from a N(0,1) distribution.

Let’s look at the first few rows of the data:

  id site trt         b eventtime status
1  1    1   0 0.4229517 0.9058188      1
2  2    1   1 0.4229517 5.9190576      1
3  3    1   0 0.4229517 7.8525219      1
4  4    1   0 0.4229517 1.2066141      1
5  5    1   1 0.4229517 1.1703645      1
6  6    1   0 0.4229517 2.6209007      1

To fit a hierarchical model for clustered survival data we use a formula syntax similar to what is used in the lme4 R package (Bates et al. (2015)). Let’s consider the following model (which aligns with the model used to generate the simulated data):

The model contains a baseline covariate for treatment (0 or 1) as well as a site-specific intercept to allow for correlation in the event times for patients from the same site. We’ve call the model object mod_randint to denote the fact that it includes a site-specific (random) intercept. Let’s examine the parameter estimates from the model:

stan_surv
 baseline hazard: exponential
 formula:         Surv(eventtime, status) ~ trt + (1 | site)
 observations:    200
 events:          152 (76%)
 right censored:  48 (24%)
 delayed entry:   no
------
            Median MAD_SD exp(Median)
(Intercept) -2.30   0.31     NA      
trt          0.46   0.19   1.59      

Error terms:
 Groups Name        Std.Dev.
 site   (Intercept) 1.15    
Num. levels: site 20 

------
* For help interpreting the printed output see ?print.stanreg
* For info on the priors used see ?prior_summary.stanreg

We see that the estimated log hazard ratio for treatment (β^(trt)=0.46) is a bit larger than the “true” log hazard ratio used in the data generating model (β(trt)=0.3). The estimated baseline hazard rate is exp(2.3716)=0.093, which is pretty close to the baseline hazard rate used in the data generating model (0.1). Of course, the differences between the estimated parameters and the true parameters from the data generating model are attributable to sampling noise.

If this were a real analysis, we might wonder whether the site-specific estimates are necessary! Well, we can assess that by fitting an alternative model that does not include the site-specific intercepts and compare it to the model we just estimated. We will compare it using the loo function. We first need to fit the model without the site-specific intercept. To do this, we will just use the generic update method for stansurv objects, since all we are changing is the model formula:

Let’s calculate the loo for both these models and compare them:


Model comparison: 
(negative 'elpd_diff' favors 1st model, positive favors 2nd) 

elpd_diff        se 
     56.8       9.7 

We see strong evidence in favour of the model with the site-specific intercepts!

But let’s not quite finish there. What about if we want to generalise the random effects structure further. For instance, is the site-specific intercept enough? Perhaps we should consider estimating both a site-specific intercept and a site-specific treatment effect. We have minimal data to estimate such a model (recall that there is only 20 sites and 10 patients per site) but for the sake of demonstration we will forge on nonetheless. Let’s fit a model with both a site-specific intercept and a site-specific coefficient for the covariate trt (i.e. treatment):

stan_surv
 baseline hazard: exponential
 formula:         Surv(eventtime, status) ~ trt + (trt | site)
 observations:    200
 events:          152 (76%)
 right censored:  48 (24%)
 delayed entry:   no
------
            Median MAD_SD exp(Median)
(Intercept) -2.27   0.26     NA      
trt          0.47   0.20   1.60      

Error terms:
 Groups Name        Std.Dev. Corr 
 site   (Intercept) 1.104         
        trt         0.443    -0.23
Num. levels: site 20 

------
* For help interpreting the printed output see ?print.stanreg
* For info on the priors used see ?prior_summary.stanreg

We see that we have an estimated standard deviation for the site-specific intercepts and the site-specific coefficients for trt, as well as the estimated correlation between those site-specific parameters.

Let’s now compare all three of these models based on loo:


Model comparison: 
(ordered by highest ELPD)

            elpd_diff se_diff
mod_randint   0.0       0.0  
mod_randtrt  -0.4       0.8  
mod_fixed   -56.8       9.7  

It appears that the model with just a site-specific intercept is the best fitting model. It is much better than the model without a site-specific intercept, and slightly better than the model with both a site-specific intercept and a site-specific treatment effect. In other words, including a site-specific intercept appears important, but including a site-specific treatment effect is not. This conclusion is reassuring, because it aligns with the data generating model we used to simulate the data!

References

Bates D, Maechler M, Bolker B, Walker S. Fitting Linear Mixed-Effects Models Using lme4. Journal of Statistical Software 2015;67(1):1–48.

Brilleman S. (2018) simsurv: Simulate Survival Data. R package version 0.2.2.

Hougaard P. Fundamentals of Survival Data. Biometrics 1999;55:13–22.

Ramsay JO. Monotone Regression Splines in Action. Statistical Science 1988;3(4):425–461.

Wang W, Yan J. (2018) splines2: Regression Spline Functions and Classes. R package version 0.2.8.

Appendix A: Parameterisations on the hazard scale

When basehaz is set equal to "exp", "weibull", "gompertz", "ms" (the default), or "bs" then the model is defined on the hazard scale using the following parameterisations.

Exponential model

The exponential model is parameterised with scale parameter λi=exp(ηi) where ηi=β0+p=1Pβpxip denotes our linear predictor.

For individual i we have:

hi(Ti)=λi=exp(ηi)Hi(Ti)=Tiλi=Tiexp(ηi)Si(Ti)=exp(Tiλi)=exp(Tiexp(ηi))Fi(Ti)=1exp(Tiλi)=1exp(Tiexp(ηi))Si(Ti)Si(TiU)=exp(Tiλi)exp(TiUλi)=exp(Tiexp(ηi))exp(TiUexp(ηi))

or on the log scale:

loghi(Ti)=logλi=ηilogHi(Ti)=log(Ti)+logλi=log(Ti)+ηilogSi(Ti)=Tiλi=Tiexp(ηi)logFi(Ti)=log(1exp(Tiλi))=log(1exp(Tiexp(ηi)))log(Si(Ti)Si(TiU))=log[exp(Tiλi)exp(TiUλi)]=log[exp(Tiexp(ηi))exp(TiUexp(ηi))]

The definition of λ for the baseline is:

λ0=exp(β0)β0=log(λ0)

Weibull model

The Weibull model is parameterised with scale parameter λi=exp(ηi) and shape parameter γ>0 where ηi=β0+p=1Pβpxip denotes our linear predictor.

For individual i we have:

hi(Ti)=γtγ1λi=γtγ1exp(ηi)Hi(Ti)=Tiγλi=Tiγexp(ηi)Si(Ti)=exp(Tiγλi)=exp(Tiγexp(ηi))Fi(Ti)=1exp((Ti)γλi)=1exp((Ti)γexp(ηi))Si(Ti)Si(TiU)=exp((Ti)γλi)exp((TiU)γλi)=exp((Ti)γexp(ηi))exp((TiU)γexp(ηi))

or on the log scale:

loghi(Ti)=log(γ)+(γ1)log(t)+logλi=log(γ)+(γ1)log(t)+ηilogHi(Ti)=γlog(Ti)+logλi=γlog(Ti)+ηilogSi(Ti)=Tiγλi=Tiγexp(ηi)logFi(Ti)=log(1exp((Ti)γλi))=log(1exp((Ti)γexp(ηi)))log(Si(Ti)Si(TiU))=log[exp((Ti)γλi)exp((TiU)γλi)]=log[exp((Ti)γexp(ηi))exp((TiU)γexp(ηi))]

The definition of λ for the baseline is:

λ0=exp(β0)β0=log(λ0)

Gompertz model

The Gompertz model is parameterised with shape parameter λi=exp(ηi) and scale parameter γ>0 where ηi=β0+p=1Pβpxip denotes our linear predictor.

For individual i we have:

hi(Ti)=exp(γTi)λi=exp(γTi)exp(ηi)Hi(Ti)=exp(γTi)1γλi=exp(γTi)1γexp(ηi)Si(Ti)=exp((exp(γTi)1)γλi)=exp((exp(γTi)1)γexp(ηi))Fi(Ti)=1exp((exp(γTi)1)γλi)=1exp((exp(γTi)1)γexp(ηi))Si(Ti)Si(TiU)=exp((exp(γTi)1)γλi)exp((exp(γTiU)1)γλi)=exp((exp(γTi)1)γexp(ηi))exp((exp(γTiU)1)γexp(ηi))

or on the log scale:

loghi(Ti)=γTi+logλi=γTi+ηilogHi(Ti)=log(exp(γTi)1)log(γ)+logλi=log(exp(γTi)1)log(γ)+ηilogSi(Ti)=(exp(γTi)1)γλi=(exp(γTi)1)γexp(ηi)logFi(Ti)=log(1exp((exp(γTi)1)γλi))=log(1exp((exp(γTi)1)γexp(ηi)))log(Si(Ti)Si(TiU))=log[exp((exp(γTi)1)γλi)exp((exp(γTiU)1)γλi)]=log[exp((exp(γTi)1)γexp(ηi))exp((exp(γTiU)1)γexp(ηi))]

The definition of λ for the baseline is:

λ0=exp(β0)β0=log(λ0)

M-spline model

The M-spline model is parameterised with vector of regression coefficients θ>0 for the baseline hazard and with covariate effects introduced through a linear predictor ηi=p=1Pβpxip. Note that there is no intercept in the linear predictor since it is absorbed into the baseline hazard spline function.

For individual i we have:

hi(Ti)=M(Ti;θ,k0)exp(ηi)Hi(Ti)=I(Ti;θ,k0)exp(ηi)Si(Ti)=exp(I(Ti;θ,k0)exp(ηi))Fi(Ti)=1exp(I(Ti;θ,k0)exp(ηi))Si(Ti)Si(TiU)=exp(I(Ti;θ,k0)exp(ηi))exp(I(TiU;θ,k0)exp(ηi))

or on the log scale:

loghi(Ti)=log(M(Ti;θ,k0))+ηilogHi(Ti)=log(I(Ti;θ,k0))+ηilogSi(Ti)=I(Ti;θ,k0)exp(ηi)logFi(Ti)=log[1exp(I(Ti;θ,k0)exp(ηi))]log(Si(Ti)Si(TiU))=log[exp(I(Ti;θ,k0)exp(ηi))exp(I(TiU;θ,k0)exp(ηi))]

where M(t;θ,k0) denotes a cubic M-spline function evaluated at time t with regression coefficients θ and basis evaluated using the vector of knot locations k0). Similarly, I(t;θ,k0) denotes a cubic I-spline function (i.e. integral of an M-spline) evaluated at time t with regression coefficients θ and basis evaluated using the vector of knot locations k0.

B-spline model

The B-spline model is parameterised with vector of regression coefficients θ and linear predictor where ηi=p=1Pβpxip denotes our linear predictor. Note that there is no intercept in the linear predictor since it is absorbed into the spline function.

For individual i we have:

hi(Ti)=exp(B(Ti;θ,k0)+ηi)

or on the log scale:

loghi(Ti)=B(Ti;θ,k0)+ηi

The cumulative hazard, survival function, and CDF for the B-spline model cannot be calculated analytically. Instead, the model is only defined analytically on the hazard scale and quadrature is used to evaluate the following:

Hi(Ti)=0Tihi(u)duSi(Ti)=exp(0Tihi(u)du)Fi(Ti)=1exp(0Tihi(u)du)Si(Ti)Si(TiU)=exp(0Tihi(u)du)exp(0TiUhi(u)du)

Extension to time-varying coefficients (i.e. non-proportional hazards)

We can extend the previous model formulations to allow for time-varying coefficients (i.e. non-proportional hazards). The time-varying linear predictor is introduced on the hazard scale. That is, ηi in our previous model definitions is instead replaced by ηi(t). This leads to an analytical form for the hazard and log hazard. However, in general, there is no longer a closed form expression for the cumulative hazard, survival function, or CDF. Therefore, when the linear predictor includes time-varying coefficients, quadrature is used to evaluate the following:

Hi(Ti)=0Tihi(u)duSi(Ti)=exp(0Tihi(u)du)Fi(Ti)=1exp(0Tihi(u)du)Si(Ti)Si(TiU)=exp(0Tihi(u)du)exp(0TiUhi(u)du)

Appendix B: Parameterisations under accelerated failure times

When basehaz is set equal to "exp-aft", or "weibull-aft" then the model is defined on the accelerated failure time scale using the following parameterisations.

Exponential model

The exponential model is parameterised with scale parameter λi=exp(ηi) where ηi=β0+p=1Pβpxip denotes our linear predictor.

For individual i we have:

hi(Ti)=λi=exp(ηi)Hi(Ti)=Tiλi=Tiexp(ηi)Si(Ti)=exp(Tiλi)=exp(Tiexp(ηi))Fi(Ti)=1exp(Tiλi)=1exp(Tiexp(ηi))Si(Ti)Si(TiU)=exp(Tiλi)exp(TiUλi)=exp(Tiexp(ηi))exp(TiUexp(ηi))

or on the log scale:

loghi(Ti)=logλi=ηilogHi(Ti)=log(Ti)+logλi=log(Ti)ηilogSi(Ti)=Tiλi=Tiexp(ηi)logFi(Ti)=log(1exp(Tiλi))=log(1exp(Tiexp(ηi)))log(Si(Ti)Si(TiU))=log[exp(Tiλi))exp(TiUλi)]=log[exp(Tiexp(ηi))exp(TiUexp(ηi))]

The definition of λ for the baseline is:

λ0=exp(β0)β0=log(λ0)

The relationship between coefficients under the PH (unstarred) and AFT (starred) parameterisations are as follows:

β0=β0βp=βp

Lastly, the general form for the hazard function and survival function under an AFT model with acceleration factor exp(ηi) can be used to derive the exponential AFT model defined here by setting h0(t)=1, S0(t)=exp(Ti), and λi=exp(ηi):

hi(Ti)=exp(ηi)h0(texp(ηi))=exp(ηi)=λi

Si(Ti)=S0(texp(ηi))=exp(Tiexp(ηi))=exp(Tiλi)

Weibull model

The Weibull model is parameterised with scale parameter λi=exp(γηi) and shape parameter γ>0 where ηi=β0+p=1Pβpxip denotes our linear predictor.

For individual i we have:

hi(Ti)=γtγ1λi=γtγ1exp(γηi)Hi(Ti)=Tiγλi=Tiγexp(γηi)Si(Ti)=exp(Tiγλi)=exp(Tiγexp(γηi))Fi(Ti)=1exp((Ti)γλi)=1exp((Ti)γexp(γηi))Si(Ti)Si(TiU)=exp((Ti)γλi)exp((TiU)γλi)=exp((Ti)γexp(γηi))exp((TiU)γexp(γηi))

or on the log scale:

loghi(Ti)=log(γ)+(γ1)log(t)+logλi=log(γ)+(γ1)log(t)γηilogHi(Ti)=γlog(Ti)+logλi=γlog(Ti)γηilogSi(Ti)=Tiγλi=Tiγexp(γηi)logFi(Ti)=log(1exp((Ti)γλi))=log(1exp((Ti)γexp(γηi)))log(Si(Ti)Si(TiU))=log[exp((Ti)γλi)exp((TiU)γλi)]=log[exp((Ti)γexp(γηi))exp((TiU)γexp(γηi))]

The definition of λ for the baseline is:

λ0=exp(γβ0)β0=log(λ0)γ

The relationship between coefficients under the PH (unstarred) and AFT (starred) parameterisations are as follows:

β0=γβ0βp=γβp

Lastly, the general form for the hazard function and survival function under an AFT model with acceleration factor exp(ηi) can be used to derive the Weibull AFT model defined here by setting h0(t)=γtγ1, S0(t)=exp(Tiγ), and λi=exp(γηi):

hi(Ti)=exp(ηi)h0(texp(ηi))=exp(ηi)γ(texp(ηi))γ1=exp(γηi)γtγ1=λiγtγ1

Si(Ti)=S0(texp(ηi))=exp((Tiexp(ηi))γ)=exp(Tiγ[exp(ηi)]γ)=exp(Tiγexp(γηi))=exp(Tiλi)

Extension to time-varying coefficients (i.e. time-varying acceleration factors)

We can extend the previous model formulations to allow for time-varying coefficients (i.e. time-varying acceleration factors).

The so-called “unmoderated” survival probability for an individual at time t is defined as the baseline survival probability at time t, i.e. Si(t)=S0(t). With a time-fixed acceleration factor, the survival probability for a so-called “moderated” individual is defined as the baseline survival probability but evaluated at “time t multiplied by the acceleration factor exp(ηi)”. That is, the survival probability for the moderated individual is Si(t)=S0(texp(ηi)).

However, with time-varying acceleration we cannot simply multiply time by a fixed (acceleration) constant. Instead, we must integrate the function for the time-varying acceleration factor over the interval 0 to t. In other words, we must evaluate:

Si(t)=S0(0texp(ηi(u))du)

as described by Hougaard (1999).

Hougaard also gives a general expression for the hazard function under time-varying acceleration, as follows:

hi(t)=exp(ηi(t))h0(0texp(ηi(u))du)

Note: It is interesting to note here that the hazard at time t is in fact a function of the full history of covariates and parameters (i.e. the linear predictor) from time 0 up until time t. This is different to the hazard scale formulation of time-varying effects (i.e. non-proportional hazards). Under the hazard scale formulation with time-varying effects, the survival probability is a function of the full history between times 0 and t, but the hazard is not; instead, the hazard is only a function of covariates and parameters as defined at the current time. This is particularly important to consider when fitting accelerated failure time models with time-varying effects in the presence of delayed entry (i.e. left truncation).

For the exponential distribution, this leads to:

Si(Ti)=S0(0Tiexp(ηi(u))du)=exp(0Tiexp(ηi(u))du)

hi(Ti)=exp(ηi(Ti))h0(0Tiexp(ηi(u))du)=exp(ηi(Ti))exp(0Tiexp(ηi(u))du)

and for the Weibull distribution, this leads to:

Si(Ti)=S0(0Tiexp(ηi(u))du)=exp([0Tiexp(ηi(u))du]γ)

hi(Ti)=exp(ηi(Ti))h0(0Tiexp(ηi(u))du)=exp(ηi(Ti))exp([0Tiexp(ηi(u))du]γ)

The general expressions for the hazard and survival function under an AFT model with a time-varying linear predictor are used to evaluate the likelihood for the accelerated failure time model in stan_surv when time-varying effects are specified in the model formula. Specifically, quadrature is used to evaluate the cumulative acceleration factor 0texp(ηi(u))du and this is then substituted into the relevant expressions for the hazard and survival.