Tutorials › Generalized Linear Mixed Models (GLMM) › Simulation-Based Residuals for GLMMs

Diagnostics and Predictive Checks for Sparse and Overdispersed Outcomes · Tutorial 612 of 698

Simulation-Based Residuals for GLMMs

Learn how to simulate outcomes from a fitted GLMM, turn their ranks into residuals for discrete data, and assess what those residuals can—and cannot—reveal.

Intermediate 10 min read

What You'll Learn

  • Generate replicate outcomes that preserve the fitted GLMM’s cluster structure.
  • Construct rank-based residuals for discrete outcomes, including ties.
  • Transform randomized ranks to normal scores and interpret their scale.
  • Distinguish approximate plug-in residual calibration from exact calibration under a known model.
  • Use simulation-based checks to investigate individual and overall predictive discrepancies.

Why Compare Observed Outcomes With Simulations?

A response residual compares an observed outcome with a fitted value, but in a generalized linear mixed model (GLMM) the response can be discrete, its variability depends on its mean, and observations within a cluster are related. A residual measured in raw response units may therefore be hard to compare across observations. Simulation-based residuals address this by asking where each observed outcome falls among outcomes the fitted model could have generated.

The central object is not a single simulated data set. It is a collection of replicate outcomes generated under the fitted model, using the same design and preserving the model’s dependence structure. The observed value is then compared with that simulated reference. For discrete outcomes, many replicates can equal the observed value, so the residual construction must handle ties explicitly.

Definition: For observation \(i\), let \(y_i\) be the observed response and \(Y_i^{(1)},\ldots,Y_i^{(B)}\) be its outcomes in \(B\) model-generated replicate data sets. A simulation-based rank residual locates \(y_i\) in the ordered set consisting of the observed value and its \(B\) simulated counterparts, with randomization to handle ties. A normal-score residual is the inverse standard normal transformation of the resulting randomized rank.

Generating Replicates From a Fitted GLMM

A GLMM specifies a distribution for the responses conditional on latent random effects and a distribution for those effects. For a cluster \(j\), write \(Y_{ij}\) for the response of unit \(i\), \(b_j\) for the cluster’s random effect, and \(\theta\) for the model parameters. A full-model replicate is generated according to the fitted model: generate the random effects under their fitted distribution and then generate the responses conditional on those effects. The same simulated \(b_j\) is shared by observations in cluster \(j\), so the replicate retains the model’s within-cluster dependence.

The design must also be held fixed: use the same covariates, cluster membership, offsets, and observation pattern as in the data. If an outcome is a binomial count with a known number of trials, for example, retain that number of trials in each replicate. A collection of independent draws for each row that ignores shared random effects is not a valid replicate of a GLMM’s joint response structure.

This tutorial uses full-model predictive replicates to illustrate the construction. A diagnostic must always state its simulation target: a different target can answer a different question. The rank formula itself does not decide whether the random effects were generated or held fixed; the simulation scheme does.

1
Fit and specify the target.
Record the fitted GLMM, the observed design, and what is being simulated. For a full-model check, include the fitted random-effects distribution and its shared cluster structure.
2
Generate replicate data sets.
For each replicate, generate responses under the fitted model at the original covariate values and cluster assignments. Use enough replicates for the rank resolution required.
3
Compare each observed outcome with its replicates.
Count simulated outcomes below and equal to the observed value, then randomize within the tied rank interval.
4
Inspect residuals and predictive discrepancies.
Use the ranks as standardized diagnostic quantities, and compare selected data summaries with their distributions across complete simulated data sets.

Randomized Ranks for Discrete Outcomes

Let \(L_i\) be the number of simulated outcomes strictly less than \(y_i\), and let \(E_i\) be the number equal to \(y_i\). Introduce \(V_i\sim\operatorname{Uniform}(0,1)\), independently for the randomization, and define

$$ U_i=\frac{L_i+V_i(E_i+1)}{B+1}. $$

The \(E_i+1\) counts the observed outcome as well as all simulated ties. Thus \(U_i\) is randomized across the whole rank interval occupied by the observed value and its ties. This prevents a discrete outcome with many repeated values from producing only a few possible residual values. The corresponding normal-score residual is

$$ R_i=\Phi^{-1}(U_i), $$

where \(\Phi\) is the standard normal cumulative distribution function. Values of \(U_i\) near \(0.5\) give normal scores near zero; small and large \(U_i\) give negative and positive scores, respectively. The sign describes whether the observation falls low or high relative to its simulated reference, not whether a covariate has a positive or negative effect.

The rank has finite resolution: it depends on \(B\), and randomization avoids exact endpoints. With small \(B\), the residuals are coarse and their extreme values are poorly resolved. Increasing \(B\) improves Monte Carlo resolution but does not repair a misspecified model or remove uncertainty caused by estimating its parameters.

Why the Rank Is Calibrated Under an Exact Model

Suppose the model and its parameters are known, and the observed response and the \(B\) simulated responses are exchangeable under the predictive distribution used for the comparison. Exchangeable means that permuting their labels does not change their joint distribution. If all values are distinct, each of the \(B+1\) possible ranks for the observed value is then equally likely. With ties, random ordering within the tied group gives the same result: the observed value’s randomized rank is equally likely to occupy any of the \(B+1\) rank positions.

$$ P(\text{randomized rank}=r)=\frac{1}{B+1}, \qquad r=1,\ldots,B+1. $$

Adding an independent uniform jitter within the rank’s interval makes \(U_i\) uniform on \((0,1)\) under this exact exchangeability condition. The tie formula above is equivalent to randomizing continuously across the tied rank interval. Applying \(\Phi^{-1}\) then gives a standard normal marginal distribution for \(R_i\). This is a calibration result under the stated assumptions; it does not imply that residuals from a fitted GLMM are independent of each other.

In practice, the model parameters are usually estimated from the same data being diagnosed. Conditional on the fitted estimates, the observed outcomes are not generally exchangeable with new outcomes generated as if those estimates were fixed and known. Consequently, plug-in simulation-based residuals are useful diagnostics, but their exact uniform or normal calibration is not guaranteed. Estimation, dependence, and Monte Carlo error should all be considered when interpreting their distribution.

Worked Example: A Randomized Residual for a Binary Outcome

Suppose a fitted binary GLMM is used to generate \(B=9\) replicate outcomes for one hypothetical patient. The observed outcome is \(y_i=1\). Among the nine simulations, six equal 1 and three equal 0. Thus \(L_i=0\) and \(E_i=6\). For one realization of the tie randomization, take \(V_i=0.40\).

The randomized rank is

$$ U_i=\frac{0+0.40(6+1)}{9+1} =\frac{2.8}{10} =0.280. $$

The normal-score residual is \(R_i=\Phi^{-1}(0.280)\approx -0.583\). The observed 1 is therefore at a lower randomized rank than the midpoint of this simulated reference. It is not evidence by itself of model failure: with only nine replicates, the rank is coarse, and a single residual must be interpreted alongside the simulation target, model fit, and other observations.

Worked Example: Ranking a Count Outcome

Consider a hypothetical count response for one unit in a cluster. Replicates are generated from the fitted count GLMM with the observed design and the cluster structure preserved. For this unit, the observed count is \(y_i=4\), and \(B=19\) simulated counts consist of 8 values below 4, 3 values equal to 4, and 8 values above 4. Hence \(L_i=8\) and \(E_i=3\). Suppose the tie randomization gives \(V_i=0.25\).

The residual’s randomized rank is

$$ U_i=\frac{8+0.25(3+1)}{19+1} =\frac{8+1}{20} =0.450. $$

Therefore \(R_i=\Phi^{-1}(0.450)\approx -0.126\), which is close to zero. The observed count lies near the middle of its simulated rank distribution. This conclusion concerns its position relative to the model-generated counts; it does not mean that the fitted expected count is 4.5 or that the response is normally distributed.

Using Residuals and Checking Model Predictions

Simulation-based residuals are useful because the reference distribution can reflect a GLMM’s response distribution and dependence, rather than imposing a constant-variance normal approximation. They extend the idea behind randomized quantile residuals: locate an observation relative to its fitted distribution, with randomization for discrete outcomes. The practical difference is that the reference distribution can be constructed from simulated data, which is especially useful when the GLMM’s predictive distribution is not available in a convenient closed form.

Individual residuals are only one diagnostic view. A useful complementary check compares a data summary with the same summary calculated for every complete replicate data set. The summary should target a specific concern, such as the total number of zeros or the maximum cluster count. Because the replicate data sets retain the fitted clustering, their summary distribution reflects the dependence implied by the model.

Worked Example: Checking the Number of Zeros

In a hypothetical study with 40 binary outcomes, 18 observed outcomes are zero, so the observed zero proportion is \(18/40=0.450\). Generate \(B=999\) complete replicate data sets under the fitted GLMM, preserving the 40 observed covariate patterns and cluster assignments. Suppose 19 replicates have a zero proportion at least as large as 0.450.

The estimated upper-tail predictive proportion, using a plus-one correction, is

$$ \frac{1+\#\{\text{replicate zero proportions}\geq 0.450\}}{B+1} =\frac{1+19}{999+1} =0.020. $$

The observed zero proportion is unusually high relative to this fitted-model simulation distribution: only about 2.0% of the corrected set of simulated comparisons are at least as large. This is a one-sided predictive check of the zero proportion, not the probability that the model is true or that a null hypothesis is true. The result calls for investigation of why zeros are underpredicted; it does not identify the cause on its own.

Assumptions, Diagnostics, and Limitations

A residual’s apparent calibration depends on how well the simulations reproduce the intended predictive distribution. Before interpreting a pattern, check that the response family, link, random-effects structure, covariates, offsets, and observation design used to simulate are the ones specified by the fitted model. Verify that shared random effects are shared in each replicate and that all observations are generated together when the diagnostic targets the joint response. If replicates are generated independently by row despite cluster dependence in the fitted GLMM, the reference distribution is wrong.

Also check Monte Carlo adequacy. Repeat the simulation with more replicates when ranks or tail proportions are unstable, and recognize that an estimate based on few simulations has substantial Monte Carlo variation. A visibly uneven residual distribution can signal a predictive mismatch, but it can also reflect finite simulation error, parameter estimation, or dependence among residuals. Likewise, a seemingly well-behaved set of ranks does not prove that every part of a GLMM is correct: a diagnostic summary can be insensitive to some misspecifications.

Compared with response, Pearson, and deviance residuals, simulation-based ranks provide a common reference scale without relying on a normal approximation to the response. Compared with analytic randomized quantile residuals, they use an empirical reference generated from the fitted model. Their cost is computational: simulation is required, rank resolution is limited by \(B\), and uncertainty from model fitting is not automatically captured by fixed-parameter simulation. These residuals complement, rather than replace, checks of model structure and targeted predictive summaries.

Common pitfall: A researcher sees several identical binary outcomes and assigns them identical residuals, reasoning that equal responses must have equal residuals. For discrete outcomes, ties are expected; leaving them unrandomized creates a restricted set of values and can distort diagnostic displays. Randomize within each observed outcome’s tied rank interval. Conversely, do not interpret the resulting normal scores as exactly independent standard normal observations: that calibration requires exchangeability, and plug-in fitted-model simulations do not generally ensure it.

Check Your Understanding

Answer each question, then compare your reasoning with the short solutions.

  1. For \(B=15\) simulations, an observed count has 5 simulated values below it and 4 equal to it. If \(V=0.50\), calculate \(U\). What does a value below 0.5 indicate?
  2. Why must replicate data sets preserve a GLMM’s shared cluster effects when the check targets the full joint predictive behavior?
  3. Under what condition does the randomized rank have an exact uniform distribution? Why may plug-in residuals from a fitted GLMM depart from that condition?
  4. In a global predictive check, 7 of \(B=499\) simulations have a summary at least as large as the observed summary. Calculate the plus-one upper-tail proportion and state its interpretation.

Solutions. (1) \(U=[5+0.50(4+1)]/(15+1)=7.5/16=0.469\). A value below 0.5 places the randomized observation below the midpoint of its simulated reference. (2) The shared cluster effect induces dependence among observations in the same cluster; independently simulating rows would fail to reproduce that joint structure. (3) The observed value and all simulated values must be exchangeable under the predictive distribution, as when the correct model and parameters are known and the simulations follow that distribution. With estimated parameters, the observed data helped determine the estimates while new replicates are generated conditional on them, so exchangeability is not generally exact. (4) The corrected proportion is \((1+7)/(499+1)=8/500=0.016\). About 1.6% of the corrected simulated comparisons are at least as large as the observed summary; this indicates an upper-tail predictive discrepancy, not the probability that the model is true.

Clinical Biostats