The articles Flexible Bayesian Additive Joint Models with an Application to Type 1 Diabetes Research (Köhler et al. 2017) and Nonlinear Association Structures in Flexible Bayesian Additive Joint Models (Köhler et al. 2018) present a flexible framework for estimating joint models for longitudinal and survival data using MCMC. This vignette provides some example code how to set up joint models using bamlss.
This vignette explains how to estimate joint models with the
following hazard rate model:
The longitudinal response
at time points
is modeled as
with independent normal errors
.
In this model the predictors are a composition of where functions , are unspecified functions of generic covariates , e.g., that are modeled by splines. There are two types of parameters in this model, time-varying (, and ) and time-constant ( and ) parameters. Note that parameter includes the baseline hazard effect.
For more details on model specifications and methodology see Köhler et al. (2017) and Köhler et al. (2018).
This example is taken from the JMbayes package (Rizopoulos 2016). The serum bilirubin level is one of the most important body markers associated with the PBC liver disease. Here, we try to investigate how the mean bilirubin level influences survival. Therefore, we decompose the time-varying effect in parameter into a main effect and a functional random intercept, to account for individual deviations.
The data can be loaded with
## id years status drug age sex year ascites hepatomegaly
## 1 1 1.09517 dead D-penicil 58.76684 female 0.0000000 Yes Yes
## 2 1 1.09517 dead D-penicil 58.76684 female 0.5256817 Yes Yes
## 3 2 14.15234 alive D-penicil 56.44782 female 0.0000000 No Yes
## 4 2 14.15234 alive D-penicil 56.44782 female 0.4983025 No Yes
## 5 2 14.15234 alive D-penicil 56.44782 female 0.9993429 No Yes
## 6 2 14.15234 alive D-penicil 56.44782 female 2.1027270 No Yes
## spiders edema serBilir serChol albumin alkaline SGOT
## 1 Yes edema despite diuretics 14.5 261 2.60 1718 138.0
## 2 Yes edema despite diuretics 21.3 NA 2.94 1612 6.2
## 3 Yes No edema 1.1 302 4.14 7395 113.5
## 4 Yes No edema 0.8 NA 3.60 2107 139.5
## 5 Yes No edema 1.0 NA 3.55 1711 144.2
## 6 Yes No edema 1.9 NA 3.92 1365 144.2
## platelets prothrombin histologic status2
## 1 190 12.2 4 1
## 2 183 11.2 4 1
## 3 221 10.6 3 0
## 4 188 11.0 3 0
## 5 161 11.6 3 0
## 6 122 10.6 3 0
and contains several potentially relevant covariates explaining the
disease. Note that for the survival part, the time variable is
years and for the longitudinal part year in
this data set. To set up the joint model we need to specify formulas for
each parameter
(,
,
,
,
)
of the model.
## Set up the model formula including
## functional random intercepts using ti().
f <- list(
Surv2(years, status2, obs = log(serBilir)) ~ s(years,k=20),
gamma ~ s(age,k=20) + drug + sex,
mu ~ ti(id,bs="re") +
ti(year,k=20) +
ti(id,year,bs=c("re","cr"),k=c(nlevels(pbc2$id),8)),
sigma ~ 1,
alpha ~ s(years,k=20),
dalpha ~ -1
)The model is estimated by first running a backfitting algorithm, to find appropriate starting values, afterwards the MCMC sampler is started.
## Set the seed for reproducibility.
set.seed(123)
## Estimate model.
b <- bamlss(f, data = pbc2, family = "jm",
timevar = "year", idvar = "id")The model summary gives
summary(b)##
## Call:
## bamlss(formula = f, family = "jm", data = pbc2, timevar = "year",
## idvar = "id")
## ---
## Family: jm
## Link function: lambda = log, gamma = log, mu = identity, sigma = log, alpha = identity, dalpha = identity
## *---
## Formula lambda:
## ---
## Surv2(years, status2, obs = log(serBilir)) ~ s(years, k = 20)
## -
## Smooth terms:
## Mean 2.5% 50% 97.5% parameters
## s(years).tau21 2.378e-03 7.130e-05 2.121e-04 1.272e-02 0
## s(years).edf 1.012e+00 9.995e-01 1.001e+00 1.064e+00 1
## s(years).alpha 8.712e-02 2.215e-07 2.592e-03 1.000e+00 NA
## ---
## Formula gamma:
## ---
## gamma ~ s(age, k = 20) + drug + sex
## -
## Parametric coefficients:
## Mean 2.5% 50% 97.5% parameters
## (Intercept) -3.27871 -5.29945 -2.53736 -1.84889 -1.921
## drugD-penicil -0.08995 -0.43507 -0.09113 0.26304 -0.014
## sexfemale -0.18905 -0.81739 -0.23754 0.59404 -0.610
## -
## Acceptance probability:
## Mean 2.5% 50% 97.5%
## alpha 0.83931 0.02626 0.96894 1
## -
## Smooth terms:
## Mean 2.5% 50% 97.5% parameters
## s(age).tau21 2.867e-02 8.802e-05 2.304e-03 2.416e-01 0.000
## s(age).edf 1.145e+00 1.000e+00 1.022e+00 1.978e+00 0.998
## s(age).alpha 9.564e-01 5.899e-01 9.998e-01 1.000e+00 NA
## ---
## Formula mu:
## ---
## mu ~ ti(id, bs = "re") + ti(year, k = 20) + ti(id, year, bs = c("re",
## "cr"), k = c(nlevels(pbc2$id), 8))
## -
## Parametric coefficients:
## Mean 2.5% 50% 97.5% parameters
## (Intercept) 0.9644 0.8312 0.9648 1.0652 0.729
## -
## Acceptance probability:
## Mean 2.5% 50% 97.5%
## alpha 1.0000 0.9998 1.0000 1
## -
## Smooth terms:
## Mean 2.5% 50% 97.5% parameters
## ti(id).tau21 1.267e+00 1.053e+00 1.257e+00 1.521e+00 0.726
## ti(id).edf 3.067e+02 3.056e+02 3.068e+02 3.078e+02 302.618
## ti(id).alpha 9.220e-01 3.986e-01 1.000e+00 1.000e+00 NA
## ti(year).tau21 1.440e-04 3.750e-05 1.150e-04 4.269e-04 0.000
## ti(year).edf -5.856e-02 -1.927e-01 -5.226e-33 -2.691e-33 7.419
## ti(year).alpha 9.996e-01 9.959e-01 1.000e+00 1.000e+00 NA
## ti(id,year).tau21 1.297e+01 9.251e+00 1.264e+01 1.811e+01 2.292
## ti(id,year).tau22 2.028e-02 1.453e-02 1.996e-02 2.612e-02 0.003
## ti(id,year).edf 5.607e+02 5.183e+02 5.607e+02 5.983e+02 268.263
## ti(id,year).alpha 8.657e-01 8.870e-02 1.000e+00 1.000e+00 NA
## ---
## Formula sigma:
## ---
## sigma ~ 1
## -
## Parametric coefficients:
## Mean 2.5% 50% 97.5% parameters
## (Intercept) -1.256 -1.301 -1.257 -1.210 -1.235
## -
## Acceptance probability:
## Mean 2.5% 50% 97.5%
## alpha 0.9849 0.8868 0.9983 1
## ---
## Formula alpha:
## ---
## alpha ~ s(years, k = 20)
## -
## Parametric coefficients:
## Mean 2.5% 50% 97.5% parameters
## (Intercept) 6.072e-01 2.220e-16 2.220e-16 1.597e+00 0
## -
## Acceptance probability:
## Mean 2.5% 50% 97.5%
## alpha 4.158e-01 0.000e+00 2.441e-45 1
## -
## Smooth terms:
## Mean 2.5% 50% 97.5% parameters
## s(years).tau21 3.804e-01 1.216e-04 7.084e-01 7.084e-01 0.708
## s(years).edf 2.192e+00 1.001e+00 3.000e+00 3.000e+00 0.000
## s(years).alpha 4.652e-01 6.337e-105 3.285e-03 1.000e+00 NA
## ---
## Formula dalpha:
## ---
## dalpha ~ -1
## ---
## Sampler summary:
## -
## DIC = 2300.044 logLik = -747.29 logPost = 1189.16
## pd = 805.4643 runtime = 1086.63
## ---
## Optimizer summary:
## -
## logLik = -867.5908 logPost = 3958.818 time = 55.946
Here, the estimated parameters from the backfitting step are quite
different from the posterior mean calculated from the MCMC samples. This
might indicate that the default number of iterations,
n.iter = 1200, is not enough to obtain convergence of the
MCMC chains. To check this issue we look at some traceplots.
plot(b, which = "samples")
For convenience, we only show the traceplots of the intercept for
parameter
and
.
The traceplot for parameter
clearly shows an upwards trend of the MCMC chain. To overcome this
issue, we estimate the model again and increase the burnin-in phase and
the thinning parameter.
set.seed(321)
## Run model
m <- bamlss(f, data = pbc2, family = "jm",
timevar = "year", idvar = "id", optimizer = FALSE,
jm.start = coef(b), n.iter = 12000, burnin = 2000, thin = 10)Note, executing the code will take some time because of the large
number of parameters for the functional random intercept terms
().
Another solution to speed up estimation is to use multiple cores for
sampling. This can be done using the bamlss() wrapper by
setting, e.g., cores = 4. Then, internally the
mclapply() function of the parallel (R Core Team 2019) package is called. Now, the
samples from model m look much better.
plot(m, which = "samples", lag = 100)
However, there is still some autocorrelation for parameter
,
which could be further reduced by, e.g., increasing the thinning
parameter.
The estimated effects can be plotted instantly, e.g., for the time-varying parameters and :
The left plot is the estimated baseline hazard effect, the right plot
the estimated association parameter. Both effects seem to be quite
linear, however, according the 95% credible intervals the effect for
parameter alpha is not significant.
The age effect for the time-constant parameter
of the risk model is plotted with:
plot(m, model = "gamma")
Again, the effect of age on the logarithmic risk scale is basically
linear and increasing. The estimated effects for the bodymarker model in
the
parameter are plotted in similar fashion:
plot(m, model = "mu")
The left plot shows the estimated random effect for each individual, the
middle plot the estimated functional random intercepts and the right
plot the corresponding main effect of year, which is
linearly increasing.
Another feature that is implemented is the plot of estimated survival probabilities for single individuals.
jm_survplot(m, id = 21)
The upper plot shows the predicted survival probability up to 14 years
of individual 21. The lower plot is the predicted effect of time in the
longitudinal model
.
Here, the dots represent the observed bilirubin level of this
individual.