Tutorials › Generalized Linear Mixed Models (GLMM) › Diagnosing MCMC Mixing for Variance Components

Bayesian GLMM and Prior Specification for Variance Components · Tutorial 281 of 698

Diagnosing MCMC Mixing for Variance Components

Use traces and between-chain comparisons to identify poor exploration or chain disagreement for variance components, and understand what these diagnostics can—and cannot—establish.

Advanced 10 min read

What You'll Learn

  • Recognize trends, sticking, and restricted exploration in variance-component trace plots
  • Compare chains by their location, spread, and range after warmup
  • Calculate and interpret the classical potential scale reduction factor
  • Diagnose why a pooled summary can conceal disagreement between chains
  • Understand the assumptions and limitations of common mixing diagnostics
  • Distinguish evidence of poor mixing from proof of convergence

Why Mixing Diagnostics Matter

A retained MCMC draw is useful for posterior inference only if the chain has explored the relevant parts of its target distribution. A chain that remains near its starting value, drifts slowly, or visits only one region can produce many draws without representing the full posterior. For a variance component, this problem can be particularly consequential: posterior distributions may be strongly concentrated near zero, skewed, or difficult to explore alongside group-level effects.

Consider a random-intercept GLMM with group-level effects \(b_j\) and variance component \(\tau^2\):

$$ \eta_{ij}=x_{ij}^{\mathsf T}\beta+b_j,\qquad b_j\mid\tau^2\sim N(0,\tau^2). $$

A Bayesian MCMC fit produces draws of \(\tau^2\) (and usually other model parameters) from a chain intended to target the posterior distribution. This tutorial concerns whether the retained chains appear to explore that posterior adequately for a variance-component parameter. It does not provide a proof that the chains have converged.

Definition: A chain is mixing well for a parameter when it moves through the parameter’s relevant posterior regions often enough to represent its target distribution. Poor mixing may appear as persistent trends, long periods of little movement, or disagreement between chains. A trace plot graphs the successive retained draws against their iteration number.

The diagnostics below assume that warmup and adaptation have ended, that all chains use the same model and target posterior, and that the retained draws are intended to be stationary draws from that target. Chains should be run separately, with distinct random-number streams; starting them at deliberately dispersed plausible values can make disagreement easier to detect. These conditions do not guarantee adequate exploration, but without them comparisons are not meaningful.

Reading Trace Plots and Comparing Chains

Plot each chain’s retained values against iteration, preferably with a different color for each chain. Inspect the variance component on its modeled scale, such as \(\tau^2\), and consider the standard-deviation scale \(\tau\) when that is the quantity to be interpreted. A healthy-looking trace usually fluctuates around a stable range, with chains overlapping and moving repeatedly through similar regions. “Healthy-looking” is evidence to consider, not a certificate of convergence.

  • Trend: a sustained upward or downward movement suggests that the chain may still be moving toward a different region, or may be exploring the target too slowly.
  • Sticking: long runs of nearly identical values or very small changes can indicate that the chain rarely moves. This can be especially visible for a variance component near zero.
  • Restricted range: one chain may occupy a narrow band while other chains visit a wider range or a different region.
  • Chain disagreement: chains may have different centers, ranges, or patterns even when each individual trace looks stable.

Also compare chain-specific summaries, such as the mean, median, and range of the retained draws. Similar means alone are not enough: chains can have similar means but very different spreads or shapes. Conversely, small differences in finite-sample summaries do not automatically imply a problem. Judge the summaries alongside the traces and the parameter’s scale.

Worked Example: A Trace That Barely Moves

Suppose two hypothetical post-warmup chains each contain these 10 draws of a group-level variance, in squared outcome units:

Chain A: 0.080, 0.081, 0.080, 0.082, 0.081, 0.080, 0.081, 0.080, 0.082, 0.081.
Chain B: 0.079, 0.080, 0.081, 0.080, 0.079, 0.081, 0.080, 0.079, 0.080, 0.081.

The means are \(0.808/10=0.0808\) for Chain A and \(0.800/10=0.0800\) for Chain B. Their centers are close, but both traces occupy an extremely narrow band: Chain A ranges from 0.080 to 0.082, and Chain B from 0.079 to 0.081. A plot would show almost no visible movement.

This pattern warrants investigation rather than reassurance from the similar means. These short sequences alone cannot establish whether the posterior is genuinely concentrated in this narrow range or whether the chains are stuck. Inspect longer traces, compare with chains initialized at dispersed plausible values, and verify that the sampling procedure is making effective moves. No conclusion about the correct posterior spread follows from these 20 values alone.

Between-Chain Comparison and the Potential Scale Reduction Factor

A common numerical comparison is the classical potential scale reduction factor, often written \(\hat R\). For a scalar parameter, suppose there are \(m\) chains with \(n\) retained draws each. Let \(\theta_{jt}\) be draw \(t\) in chain \(j\), and let \(\bar{\theta}_j\) and \(s_j^2\) be that chain’s sample mean and sample variance. Define the average within-chain variance \(W\), the between-chain variance \(B\), an estimate \(\widehat V\) of posterior variance, and \(\hat R\) as follows:

$$ W=\frac{1}{m}\sum_{j=1}^{m}s_j^2,\qquad B=\frac{n}{m-1}\sum_{j=1}^{m}(\bar{\theta}_j-\bar{\theta}_{\cdot})^2, \qquad \bar{\theta}_{\cdot}=\frac{1}{m}\sum_{j=1}^{m}\bar{\theta}_j, $$
$$ \widehat V=\frac{n-1}{n}W+\frac{1}{n}B, \qquad \hat R=\sqrt{\frac{\widehat V}{W}}. $$

The calculation compares variation within chains with variation between their means. If chains target the same stationary distribution and explore it similarly, their means should differ only by sampling variation, and \(B\) should be comparable to \(W\). If their means disagree substantially, \(B\) increases \(\widehat V\), making \(\hat R\) greater than 1. Values appreciably above 1 warn that the chains may not yet be exploring the same distribution. A value near 1 is reassuring only in combination with traces and other checks; it cannot establish convergence by itself.

Derivation: Under stationary sampling, each chain’s sample variance \(s_j^2\) estimates the target variance, so \(W\) estimates within-chain variation. If chains have not mixed, their means can differ; the variance of those means, scaled by \(n\), gives \(B\), which reflects between-chain variation. The weighted estimate \(\widehat V\) combines within-chain information with the observed between-chain discrepancy. When \(B>W\), \(\widehat V>W\), hence \(\hat R>1\). This explains why chain disagreement raises the diagnostic; it does not show that a small \(\hat R-1\) rules out every failure mode.

Worked Example: Calculating a Between-Chain Diagnostic

Four hypothetical chains each provide five retained draws of a variance component, in squared outcome units:

Chain 1: 0.16, 0.18, 0.20, 0.22, 0.24.
Chain 2: 0.18, 0.20, 0.22, 0.24, 0.26.
Chain 3: 0.20, 0.22, 0.24, 0.26, 0.28.
Chain 4: 0.22, 0.24, 0.26, 0.28, 0.30.

The chain means are 0.20, 0.22, 0.24, and 0.26; their grand mean is \((0.20+0.22+0.24+0.26)/4=0.23\). Each chain has sample variance \(s_j^2=0.001\), so \(W=0.001\). The squared deviations of the chain means from 0.23 sum to \(0.03^2+0.01^2+0.01^2+0.03^2=0.002\). Thus

$$ B=\frac{5}{4-1}(0.002)=0.003333,\qquad \widehat V=\frac{4}{5}(0.001)+\frac{1}{5}(0.003333)=0.001467, $$
$$ \hat R=\sqrt{\frac{0.001467}{0.001}}=1.211. $$

This value indicates substantial disagreement relative to within-chain variation, so these chains should not be treated as convincingly mixed. The traces add important information: every listed chain increases at each iteration. That shared upward pattern suggests possible drift, while the different centers show chain separation. With only five draws per chain, the numerical estimate is itself highly limited; the appropriate response is to investigate and run longer chains, not to report the pooled draws as an adequate posterior sample.

Variance Components Require Careful Diagnostics

A variance component is constrained to be nonnegative, and its posterior may place substantial mass near zero. A trace that appears compressed against zero can therefore reflect a genuinely concentrated posterior, poor movement near the boundary, or both. Examine the actual numerical scale and compare chains from different plausible starting values. If using a transformation such as \(\log(\tau^2)\), remember that it is undefined at zero; adding an arbitrary constant changes the quantity being diagnosed. Do not silently treat such a transformed trace as equivalent to a trace on the variance scale.

A pooled histogram can also hide chain disagreement: it shows the combined distribution but not which chain contributed which values. Plot chains separately and compare their summaries before pooling. If different chains remain in different regions, the pooled average may describe neither chain’s exploration adequately.

Worked Example: A Pooled Summary Hides Chain Disagreement

Suppose four hypothetical chains each contain five post-warmup draws of \(\tau^2\), again in squared outcome units:

Chain 1: 0.00, 0.00, 0.01, 0.00, 0.01.
Chain 2: 0.01, 0.00, 0.01, 0.00, 0.01.
Chain 3: 0.18, 0.22, 0.20, 0.19, 0.21.
Chain 4: 0.19, 0.21, 0.18, 0.22, 0.20.

The first two chain means are \(0.02/5=0.004\) and \(0.03/5=0.006\). The last two are \(1.00/5=0.200\) each. The pooled mean is \((0.02+0.03+1.00+1.00)/20=2.05/20=0.1025\).

That pooled mean conceals a major discrepancy: two traces stay near zero, while two stay near 0.20. It is not evidence that any chain explores the region around 0.1025. The disagreement could reflect inadequate exploration, or a difficult posterior shape that requires further investigation; these values alone cannot decide which. Inspect chain-specific traces and summaries, verify that each chain targets the same posterior, and continue sampling only after addressing the source of the disagreement.

Limitations, Alternatives, and Practical Checks

The classical \(\hat R\) is a compact comparison, not a universal pass/fail test. Its value can be misleading when chains are too short, when a parameter’s posterior is strongly skewed, or when all chains fail in the same way. A shared failure may leave chain means similar and \(\hat R\) near 1 even though important posterior regions were never visited. A low \(\hat R\) also does not show that the chains have moved often enough within their ranges.

For practical assessment, combine rather than substitute diagnostics:

  • Use trace plots to see trends, sticking, and chain overlap over iteration.
  • Compare chain-specific centers, spreads, and ranges to identify discrepancies that a pooled summary hides.
  • Use \(\hat R\) as a numerical between-chain check, interpreted in light of trace behavior and chain length.
  • Check the variance component itself and, where meaningful, other model quantities whose exploration may be linked to it.

Modern software may report refinements such as split or rank-normalized versions of \(\hat R\), which are designed to detect some problems that the classical formula can miss. They remain diagnostics, not proofs. Effective sample size and Monte Carlo error answer different questions about the information in correlated draws; they complement mixing checks but do not replace examining chain disagreement and trace behavior.

Common mistake: “The chains produced thousands of draws and the pooled mean is stable, so mixing must be adequate.” Draw count and pooled stability do not establish exploration. If chains occupy different regions or show long periods of sticking, their combined mean can be deceptively precise-looking while representing inadequate sampling. Inspect separate traces and chain summaries, then interpret \(\hat R\) as supporting evidence rather than a guarantee.
1
Confirm comparability.
Check that warmup has ended and every chain uses the same model and posterior target.
2
Inspect traces.
Look for drift, sticking, restricted ranges, and persistent separation among chains.
3
Compare chains numerically.
Review chain-specific summaries and calculate a between-chain diagnostic such as \(\hat R\).
4
Investigate concerns.
Do not rely on pooled summaries when chains disagree; identify and address poor exploration before using the draws for inference.

Check Your Understanding

Answer each question using the diagnostic principles above. Brief solutions follow the questions.

  1. What does a sustained upward trend in a post-warmup trace suggest, and what does it not prove?
  2. If \(B\) is much larger than \(W\), what happens to the classical \(\hat R\), and why?
  3. Two chains have similar means, but one remains in a narrow range while the other explores a broad range. Is agreement of means sufficient to declare good mixing?
  4. Why can a pooled mean be misleading when different chains occupy separate regions?

Solutions. (1) It suggests possible drift or slow exploration; it does not prove that the posterior target is wrong or identify the cause. (2) \(\widehat V\) rises relative to \(W\), so \(\hat R=\sqrt{\widehat V/W}\) increases, warning of between-chain disagreement. (3) No. Different spreads or trace behavior can reveal disagreement that means alone miss. (4) The pooled mean averages values from chains that may not have explored the same regions; it may represent no chain’s typical exploration and can conceal inadequate mixing.

Clinical Biostats