Why Diagnose Residuals After Integration?
A mixed-model integration estimates batch-associated shifts while accounting for measured cell state. The adjusted values are useful only to the extent that the fitted model represents the structure in the data. A model can leave systematic batch differences in its residuals, fit different batches with unequal accuracy, or mistake a biological pattern for a batch effect. Residual diagnostics help expose these problems before adjusted values are interpreted further.
The examples here concern a linear mixed model fitted to a transformed expression measurement, such as log-normalized expression for one gene. They do not assume that a Gaussian model is appropriate for raw counts. Diagnostics should be interpreted on the scale on which the model was fitted, and should be repeated across genes or features of interest rather than inferred from one illustrative feature.
For a random-intercept batch model, \(z_i\) identifies the batch of cell \(i\), and each batch has a gene-specific intercept deviation. More complex models can include random slopes or additional grouping levels. Residuals are defined relative to a particular fitted model, so a diagnostic is meaningful only when its fixed effects, random effects, response scale, and grouping structure are clear.
Residuals and Their Patterns
The conditional residual is the observed value minus the fitted value including estimated random effects: \(e_{ig}=y_{ig}-x_i^\top\hat\beta_g-z_i^\top\hat b_g\). It asks what remains after the model's estimated batch effects and fixed effects have been applied. A marginal residual omits the fitted random effects: \(e^{(m)}_{ig}=y_{ig}-x_i^\top\hat\beta_g\). It includes batch variation that the random effects are intended to represent, so it is not the right residual for checking whether that variation remains after conditional adjustment.
Under a correctly specified conditional mean, conditional residuals should not show a systematic mean pattern against fitted values, cell state, or other included predictors. Their spread should also be compatible with the assumed residual variance. These are expectations, not strict rules for a finite data set: fitting the model constrains residuals, observations may have different leverage, and estimated random effects are shrunk toward zero. Consequently, a raw residual's size is not directly comparable across all cells without considering its uncertainty.
Useful displays include conditional residuals against fitted values, against continuous state covariates, and grouped by batch and state. Look for curvature, trends, clusters, or a change in spread. A residual trend with state can indicate a missing nonlinear term, an omitted state-by-batch interaction, or an imperfect state representation. A residual shift across batches can indicate incomplete batch correction or a misspecified batch structure. Neither pattern alone identifies the cause: imbalance in cell-state composition, unmodeled sample structure, or measurement artifacts can produce similar evidence.
Worked Example: Detecting a Residual Trend With Fitted Expression
Suppose a hypothetical model for one gene gives the following conditional residuals (in log-normalized expression units) at three increasing fitted-expression levels. The table shows the mean residual among cells in each fitted-value group.
| Fitted-value group | Mean fitted expression | Mean conditional residual |
|---|---|---|
| Low | 1.0 | -0.30 |
| Middle | 2.0 | 0.00 |
| High | 3.0 | 0.30 |
The pattern is increasing, rather than fluctuating around zero without a trend. For example, the difference between the high- and low-group residual means is \(0.30-(-0.30)=0.60\) log-expression units. This is a diagnostic summary, not a test statistic, and the grouped means alone do not establish that the trend is statistically reliable.
If this pattern is also visible in a residual-versus-fitted plot, it suggests that the fitted mean may be too low at high expression and too high at low expression, or that another systematic feature is associated with fitted expression. Possible explanations include a nonlinear response relationship or a scale-dependent mean structure. The next check is to inspect residuals against relevant covariates and batches, and to verify that the pattern is not driven by a small number of cells or a particular state. It would be premature to label the pattern as remaining batch effect without batch-specific evidence.
Batch-Wise Residual Variance
A model with one residual variance \(\sigma_g^2\) assumes a common residual spread for the observations it models, subject to any explicitly modeled variance structure. To assess that assumption, calculate or plot conditional residual spread by batch. For \(n_b\) residuals in batch \(b\), a descriptive sample variance is \(s_{e,b}^2=\sum_{i\in b}(e_{ig}-\bar e_{g,b})^2/(n_b-1)\), where \(\bar e_{g,b}\) is the mean conditional residual in that batch. Large differences can indicate unequal noise, unmodeled cell-state composition, outliers, or other misspecification.
This variance comparison is not a formal test by itself. Small batches yield unstable variance estimates, and cells from the same donor, sample, or library may be dependent. Treating many correlated cells as independent can make apparent evidence much stronger than it is. When batch sizes differ, compare distributions and uncertainty as well as point estimates; consider grouping or resampling at the independent experimental-unit level where appropriate. Also compare like with like: if batches contain different proportions of cell states, a pooled within-batch variance can reflect that composition difference rather than a difference in residual noise.
Worked Example: Comparing Residual Variances Across Two Batches
Consider hypothetical conditional residuals for a gene, in log-normalized expression units. Batch A has residuals \(-0.2, 0, 0.2\), and batch B has residuals \(-0.6, 0, 0.6\). Both batch means are zero, so the variance calculation is not confounded by a nonzero residual mean in this small example.
For batch A, the sample variance is
For batch B, it is
The descriptive variance ratio is \(0.36/0.04=9\): batch B has nine times the sample residual variance of batch A in these data. This is a substantial difference in spread, but each estimate uses only three residuals. It would be inappropriate to conclude from this example alone that the underlying batch variances differ by a factor of nine. In an actual analysis, inspect the residual distributions, sample sizes, cell-state composition, and experimental-unit structure; then assess whether a variance structure or another model term is warranted.
Uncertainty in Fitted Batch Effects
Estimated random effects are not exact batch corrections. They are predictions that combine the observed data with the model's population distribution for batch effects. In a standard random-intercept model, this partial pooling shrinks noisy batch estimates toward the population mean, zero under the usual centered parameterization. A small fitted effect may therefore reflect either genuinely little evidence for a shift or limited information about that batch.
For a fitted effect, report its estimate together with an uncertainty measure supplied by the fitted model. Conditional prediction uncertainty for a random effect is not identical to the uncertainty of a fixed-effect coefficient; the precise calculation depends on the model and software. An approximate normal interval can be useful as a diagnostic summary when the model's approximation is reasonable, but it should not be read as a definitive test that a batch effect is present or absent. Variance-component estimation, model selection, and possible misspecification can add uncertainty not reflected in a simple interval.
Worked Example: Interpreting a Noisy Fitted Batch Effect
Suppose a hypothetical random-intercept fit for a gene estimates batch C's effect as \(\hat b_C=-0.24\) log-expression units, with conditional prediction standard error \(0.10\) units. Using a normal approximation, an illustrative 95% interval is
This interval indicates that, under the fitted model and its approximation, the effect estimate is negative and its conditional prediction uncertainty is of this approximate size. It is not a 95% probability statement about the fixed, realized batch effect, and it does not establish that the model has removed all batch-related structure. Inspect whether conditional residuals still differ systematically by batch or state. If substantial residual structure remains, a precise-looking effect estimate does not rescue a misspecified model.
What Residual Diagnostics Can and Cannot Establish
A useful justification for these checks follows from the model's conditional mean. If the model is correctly specified, then before estimating parameters, the error term has conditional expectation zero given the modeled predictors and random effects. Thus, a persistent residual trend against a predictor is evidence that the conditional mean may not capture some structure. Similarly, if the model assumes a common residual variance, persistent differences in residual spread across groups are evidence against that variance assumption. These implications motivate the diagnostics; they do not turn visual patterns into formal hypothesis tests.
In practice, parameters and random effects are estimated from the same data being checked. This makes fitted residuals constrained and can make their variability differ across cells. A useful standardized residual divides a conditional residual by an appropriate estimate of its own standard deviation, \(r_{ig}=e_{ig}/SE(e_{ig})\), when the fitting procedure provides a suitable residual standard error. This scaling can help identify unusually large residuals, but it does not make residuals independent or remove the need to examine their patterns. A universal cutoff should not replace inspection of the model and data.
Residual diagnostics complement rather than replace other integration approaches and checks. Anchor-based corrections estimate shifts from matched cell relationships; mixed-model residuals instead assess what remains after a specified hierarchical model has been fitted. For mixed models, conditional residuals assess the post-adjustment fit, while marginal residuals show variation before incorporating fitted batch effects. Neither residual plots nor fitted-effect intervals alone quantify overall batch mixing or biological conservation. They also cannot prove that a biological signal has been preserved: a model may remove a real state-specific difference if that difference is confounded with batch.
Plot conditional residuals against fitted values and relevant state covariates. Look for trends, curvature, or clusters that indicate a missing or misspecified mean term.
Compare residual distributions and variances by batch and, where possible, by cell state within batch. Note small group sizes and dependence among cells from the same experimental unit.
Review fitted batch-effect estimates alongside their prediction uncertainty. Avoid interpreting a shrunken estimate as known exactly or as proof that the residuals are satisfactory.
Check plausible causes such as omitted state structure, unequal variance, outliers, or sample-level dependence. Any model revision should be scientifically justified, followed by the same diagnostic checks.
Common Pitfall and Limitations
A common mistake is to see a fitted batch effect near zero and conclude that batch correction is complete. The flawed reasoning is: “The estimated random effect is small, so the adjusted data have no batch pattern.” That conclusion ignores shrinkage and uncertainty, and it says nothing about residual differences in spread, state-dependent shifts, or omitted grouping structure. The correction is to assess conditional residuals across batch and state, examine uncertainty in fitted effects, and interpret those checks in light of the model's assumptions.
Residual diagnostics can reveal incompatibility between the fitted model and observed data, but they rarely identify one unique repair. If batches have little overlap in biological states, the data may not distinguish a batch shift from a true biological difference. A visually clean residual plot cannot resolve that identification problem. Conversely, a few large residuals do not by themselves show that integration failed; they may be isolated observations or reflect the chosen expression scale. Diagnostics should guide investigation, not substitute for biological knowledge or a separate evaluation of what the integration is meant to preserve.
Check Your Understanding
Answer each question, then compare with the solutions below.
- Why are conditional residuals more directly relevant than marginal residuals when checking whether fitted batch effects have left systematic structure?
- A batch has four conditional residuals \(-0.3,-0.1,0.1,0.3\). Calculate their sample variance.
- A fitted batch effect is close to zero but has a large prediction standard error. What is a cautious interpretation?
- Why can unequal cell-state composition across batches complicate a comparison of pooled residual variances?
- Name one pattern that suggests a mean-structure problem and one pattern that suggests a variance-structure problem.
Solutions. 1. Conditional residuals subtract both fixed effects and fitted random batch effects, so they describe what remains after the modeled adjustment; marginal residuals omit those fitted random effects and retain batch variation the model was designed to represent. 2. The mean is zero, and the sample variance is \([(-0.3)^2+(-0.1)^2+(0.1)^2+(0.3)^2]/(4-1)=0.20/3=0.0667\) squared log-expression units. 3. The data provide limited precision about that batch effect; the near-zero estimate is not strong evidence that the true shift is negligible, especially because random-effect estimates are shrunk. 4. A pooled variance can reflect different mixtures of states with different expression distributions, rather than different residual noise within comparable states. 5. A residual trend against a state covariate suggests a mean-structure problem; consistently wider residual spread in one batch suggests a variance-structure problem, subject to sample size, dependence, and composition checks.