Tutorials › Joint Modeling of Longitudinal and Time-to-Event Data › Fitting Bayesian Joint Models With MCMC

Software and Workflow · Tutorial 661 of 697

Fitting Bayesian Joint Models With MCMC

Specify a Bayesian shared-random-effects joint model, run MCMC, and interpret its posterior summaries and basic sampling diagnostics.

Intermediate 12 min read

What You'll Learn

  • Write the longitudinal and event components of a shared-random-effects model.
  • Choose and interpret priors for regression, association, residual, and random-effects parameters.
  • Understand how MCMC draws represent the joint posterior distribution.
  • Convert posterior summaries of an association parameter into hazard ratios and credible intervals.
  • Use trace plots, effective sample sizes, and Monte Carlo standard errors to assess simulation precision.
  • Recognize what MCMC diagnostics can and cannot establish about a fitted model.

Why Use MCMC for a Joint Model?

A shared-random-effects joint model links a subject’s longitudinal trajectory to their event risk through latent subject-specific effects. Maximum-likelihood fitting integrates over those random effects, often using numerical approximations. Bayesian fitting instead assigns prior distributions to model parameters and uses Markov chain Monte Carlo (MCMC) to approximate their joint posterior distribution. This gives direct posterior summaries for an association parameter, while requiring care about prior choices and whether the simulation has adequately explored the posterior.

The focus here is fitting and assessing MCMC for a shared-random-effects model. The event association will be through the marker’s current latent value. Other association structures require corresponding changes to the model specification; the interpretation of an association coefficient always depends on the quantity it multiplies.

Model and Assumptions

For subject \(i\), let \(Y_{ij}\) be the marker measured at time \(t_{ij}\), \(T_i\) the observed event or censoring time, and \(\delta_i\) the event indicator, equal to 1 for an observed event and 0 for right censoring. Let \(b_i\) be a vector of subject-specific random effects. A Gaussian longitudinal submodel and a current-value event submodel can be written as follows:

$$ Y_{ij}=x_{ij}^{T}\beta+z_{ij}^{T}b_i+\epsilon_{ij}, \qquad b_i\sim N(0,D), \qquad \epsilon_{ij}\sim N(0,\sigma^2), $$
$$ m_i(t)=x_i(t)^T\beta+z_i(t)^Tb_i,\qquad h_i(t\mid b_i)=h_0(t)\exp\{\gamma^Tw_i+\eta m_i(t)\}. $$

Here \(m_i(t)\) is the latent marker trajectory, \(w_i\) contains event-submodel covariates, and \(h_0(t)\) is the baseline hazard. The association parameter \(\eta\) describes how the current latent marker value relates to the hazard, conditional on the other model terms. For a marker difference of \(\Delta\) units, the corresponding conditional hazard ratio is \(\exp(\eta\Delta)\). Thus, the units and scaling of the marker matter.

The usual assumptions include independent subjects; the specified longitudinal and event distributions conditional on the random effects and covariates; and a censoring process that is noninformative conditional on the modeled information. The longitudinal measurement errors are conditionally independent with the stated variance. The visit schedule is treated as fixed or noninformative under the model. A piecewise-constant baseline hazard is one practical choice: it assigns a constant hazard \(\lambda_k\) to each prespecified time interval \(k\).

These assumptions are substantive, not consequences of running MCMC. For example, if the event process still depends on a subject-specific feature after conditioning on \(b_i\) and the covariates, the stated event model may not capture that dependence. MCMC can sample from the posterior of a specified model; it does not make a misspecified model correct.

Specify Priors Before Sampling

A Bayesian fit combines the likelihood with priors to define the posterior. One reasonable starting specification uses normal priors for regression coefficients and the association parameter, a positive prior for the residual standard deviation, and a structured prior for the random-effects covariance matrix. For instance:

$$ \beta\sim N(0,\,s_\beta^2 I),\quad \gamma\sim N(0,\,s_\gamma^2 I),\quad \eta\sim N(0,\,s_\eta^2),\quad \sigma\sim \text{half-}t(\nu,0,s_\sigma). $$

A half-\(t\) distribution is a \(t\) distribution restricted to positive values. For \(D\), a common strategy separates random-effect standard deviations from their correlation matrix, assigns half-\(t\) priors to the standard deviations, and uses an LKJ prior for the correlation matrix. For a piecewise-constant baseline hazard, one may place normal priors on \(\log(\lambda_k)\), ensuring each \(\lambda_k\) is positive. These are examples of prior families, not universal defaults: scales should be chosen with the units and plausible ranges of the problem in mind.

Continuous predictors are often centered and scaled before fitting, with the same transformation used consistently in both submodels. A prior on \(\eta\) then has a more interpretable scale, and sampling may be better behaved. Prior predictive checks—simulating trajectories and event times from the priors before fitting—can reveal implausible implications, such as nearly all events occurring immediately or trajectories far outside plausible ranges.

Worked Example: Choosing a Prior for the Association Parameter

Suppose the marker has been standardized so that one unit is one standard deviation. A hypothetical analyst chooses \(\eta\sim N(0,0.5^2)\). What range of hazard ratios does this prior imply for a one-standard-deviation increase in the current marker value?

The hazard ratio for a one-unit increase is \(\exp(\eta)\). The central 95% interval of the normal prior is approximately \(0\pm1.96(0.5)\), or \((-0.98,0.98)\). Transforming both endpoints gives:

$$ \left(\exp(-0.98),\exp(0.98)\right)\approx(0.375,2.66). $$

Thus, before seeing the data, this prior places about 95% of its probability on hazard ratios from approximately 0.375 to 2.66 per one-standard-deviation higher current marker value. This calculation helps assess whether the prior scale is suitably broad for the scientific context. It is not a claim that the posterior effect must lie in that range.

From the Joint Likelihood to Posterior Draws

Conditional on \(b_i\), the subject’s event contribution is the event hazard raised to the event indicator, multiplied by the survival probability through the observed time. If \(H_i(t\mid b_i)\) is the cumulative hazard, that contribution is \(h_i(T_i\mid b_i)^{\delta_i}\exp\{-H_i(T_i\mid b_i)\}\). Multiplying it by the longitudinal densities and the random-effects density gives the subject’s conditional contribution.

$$ L_i(b_i)= \left\{\prod_j f_Y(Y_{ij}\mid b_i,\beta,\sigma)\right\} h_i(T_i\mid b_i)^{\delta_i} \exp\{-H_i(T_i\mid b_i)\} f_b(b_i\mid D). $$

The full posterior is proportional to the product of these contributions over subjects, multiplied by the priors for all parameters. This follows directly from Bayes’ rule: posterior density is proportional to likelihood times prior density. The random effects are unknown latent quantities and can be sampled along with the population-level parameters.

The computational reason MCMC is useful is that the marginal likelihood would require integrating over all subjects’ random effects and, often, other parameters. Instead, a sampler generates dependent draws from the joint posterior of the parameters and latent effects. After initial draws used for warm-up, retained draws approximate the posterior distribution if the chains have reached and adequately explored their stationary distribution. This is an approximation that must be assessed, not a guarantee supplied by the algorithm.

Hamiltonian Monte Carlo, including its No-U-Turn Sampler (NUTS) variant, is widely used for continuous hierarchical models. It proposes moves using posterior gradients and can explore correlated parameter spaces more efficiently than simple random-walk proposals. Other samplers may combine Gibbs updates for parameters with convenient conditional distributions and Metropolis updates for the rest. The appropriate implementation depends on the software and model; whatever the algorithm, retain multiple chains initialized at dispersed plausible values and examine the resulting draws.

Worked Example: Interpreting a Posterior Summary

In a hypothetical fit, the retained draws for the standardized-marker association parameter have posterior mean \(0.42\), posterior standard deviation \(0.20\), and a 95% equal-tailed credible interval \((0.04,0.82)\). Convert this result to a hazard-ratio scale and state its interpretation.

For a one-unit increase in the standardized current marker value, the posterior mean association corresponds to:

$$ \exp(0.42)\approx1.52. $$

Transforming the interval endpoints gives \((\exp(0.04),\exp(0.82))\approx(1.04,2.27)\). Under the fitted model and prior, the 95% posterior credible interval for the conditional hazard ratio per one-standard-deviation increase is approximately 1.04 to 2.27. The posterior mean on the hazard-ratio scale is not generally \(\exp(0.42)\); that value is the exponential of the posterior mean of \(\eta\). The interval’s posterior probability statement is conditional on the model and prior, not a frequentist confidence-interval interpretation.

Trace Plots, Effective Sample Size, and Precision

A trace plot displays sampled values against iteration. For key quantities—especially \(\eta\), important variance components, and selected latent effects—look for chains that overlap and fluctuate across a stable range without persistent trends or long periods trapped in distinct regions. A trace plot is a visual diagnostic: a reassuring appearance does not prove convergence, and a troubling appearance is reason to investigate rather than to report an unqualified summary.

Because successive MCMC draws are dependent, the retained draw count is not the same as the number of independent draws. For a scalar quantity with \(M\) retained draws and autocorrelation \(\rho_k\) at lag \(k\), an approximate effective sample size is:

$$ ESS\approx\frac{M}{1+2\sum_{k=1}^{\infty}\rho_k}. $$

In practice, software estimates this using a suitable truncation or positive-sequence method. A small ESS means the chain contains less information for estimating that quantity than the raw draw count suggests. The approximate Monte Carlo standard error for a posterior mean is the posterior standard deviation divided by \(\sqrt{ESS}\). It measures simulation precision, not uncertainty about the parameter itself.

Use multiple-chain convergence summaries as a complement to plots. The split-\(\hat R\) statistic compares variation within and between chains after splitting chains into halves; values close to 1 indicate that those sources of variation are similar. A value such as 1.01 is often used as a practical screening target, not as proof of convergence. Examine ESS and Monte Carlo error as well. Diagnostics should cover the parameters that matter to the scientific question, not just an easy-to-sample regression coefficient.

Worked Example: Assessing Simulation Precision

Suppose four hypothetical chains provide \(M=8{,}000\) retained draws in total for \(\eta\). Estimated autocorrelations are \(0.60\), \(0.35\), and \(0.18\) at lags 1, 2, and 3; for this illustration, assume subsequent contributions are negligible. The posterior standard deviation is \(0.20\). Approximate the effective sample size and Monte Carlo standard error of the posterior mean.

Using the stated autocorrelations:

$$ ESS\approx\frac{8{,}000}{1+2(0.60+0.35+0.18)} =\frac{8{,}000}{3.26}\approx2{,}454. $$

Then the approximate Monte Carlo standard error is \(0.20/\sqrt{2{,}454}\approx0.00404\). The 8,000 retained draws therefore provide roughly the mean-estimation precision of 2,454 independent draws under this approximation. The posterior standard deviation remains \(0.20\); it describes posterior spread, whereas \(0.00404\) describes simulation error in estimating the posterior mean.

Practical Fitting Sequence and Alternatives

1
Specify and scale.
Write down the longitudinal and event formulas, define the association structure, choose prior distributions and scales, and check prior predictive implications.
2
Run multiple chains.
Use dispersed initial values and enough warm-up for adaptation. Retain post-warm-up draws, recording the model, priors, software settings, and random seed.
3
Assess sampling and summarize.
Inspect traces and chain summaries for key parameters, review split-\(\hat R\), ESS, and Monte Carlo standard errors, then report posterior summaries with units and model context.

MCMC and maximum likelihood are different inferential approaches, not interchangeable computational settings. Maximum likelihood targets parameter values that maximize the likelihood, often using quadrature or approximations to integrate random effects. Bayesian MCMC targets a posterior distribution that also reflects prior information, and can provide direct uncertainty summaries for latent quantities. Bayesian credible intervals have a posterior probability interpretation conditional on the model and prior; frequentist confidence intervals have a different repeated-sampling interpretation. Neither approach removes the need to check the model’s assumptions or the stability of the computation.

Common Pitfalls and Limitations

  • Reading a coefficient without its scale. The hazard ratio is \(\exp(\eta\Delta)\), not simply \(\eta\). State the marker difference, units, and conditioning context.
  • Treating many draws as proof of convergence. Autocorrelation can make the effective sample size much smaller than the retained count. Review traces, multiple-chain summaries, ESS, and Monte Carlo error.
  • Assuming a good trace validates the model. Sampling diagnostics address computation, not whether the longitudinal distribution, event association, censoring assumption, or baseline hazard is appropriate.
  • Ignoring prior influence. With limited information, posterior results can depend materially on prior scales. Check prior implications and consider whether conclusions change under scientifically defensible alternatives.

MCMC can be costly for large or highly correlated joint models, and diagnostics cannot establish that every posterior region has been explored. Posterior summaries should therefore be reported with adequate simulation diagnostics and in context. If chains show persistent trends, do not present the resulting summaries as reliable merely because the software completed.

Key takeaway: A Bayesian joint-model fit is defined by its likelihood, priors, and sampling implementation. Interpret association parameters on the hazard-ratio scale, and treat trace plots, effective sample sizes, and Monte Carlo errors as complementary checks on the precision of posterior summaries.

Check Your Understanding

Answer each question, then compare with the solutions.

  1. If \(\eta=0.30\), what is the conditional hazard ratio for a two-unit increase in the current marker value?
  2. Why can 10,000 retained MCMC draws contain substantially less than 10,000 independent draws?
  3. What does a 95% posterior credible interval mean, and what does it not mean?
  4. A trace plot looks stable, but the chains occupy separate ranges and split-\(\hat R\) is 1.12. Is the posterior mean ready to report as reliable?

Solutions. 1. The ratio is \(\exp(0.30\times2)=\exp(0.60)\approx1.82\), for a two-unit increase, conditional on the model’s other terms. 2. Successive draws are autocorrelated, so their effective sample size can be far smaller than their count. 3. Conditional on the specified model and prior, the interval contains 95% of the posterior probability for the parameter. It is not a statement that there is a 95% repeated-sampling probability that a fixed parameter lies in this particular interval. 4. No. Separated chain ranges and a split-\(\hat R\) of 1.12 suggest the chains have not adequately mixed or explored the same distribution; investigate sampling before relying on the summary.

Clinical Biostats