Tutorials › Multi-Omics Data Integration › Residual Checks After Batch Adjustment

Batch Effects and Confounding Across Cohorts · Tutorial 103 of 597

Residual Checks After Batch Adjustment

Use residual plots, batch-stratified summaries, and covariate associations to assess what an adjustment has left unexplained—and what it may have changed.

Advanced 10 min read

What You'll Learn

  • Define feature-level residuals from an additive batch-adjustment model
  • Explain why in-sample residual means cannot verify adjustment for included batch terms
  • Detect residual spread differences and nonlinear covariate patterns
  • Calculate residual association with a biological covariate omitted from the fitted model
  • Distinguish residual diagnostics from checks of retained biological contrasts
  • Recognize when residual plots are constrained, noisy, or inconclusive

Why Inspect Residuals After Adjustment?

A batch adjustment is useful only to the extent that it reduces unwanted technical structure without obscuring biological patterns of interest. The adjusted values alone do not establish that either goal was achieved. Residuals—what remains after subtracting the fitted part of a model—provide a way to look for structure the model did not explain. They can reveal unequal residual spread across batches, unmodelled covariate patterns, or remaining association with a biological variable.

Residual checks are diagnostic, not proof that an adjustment is correct. A flat residual plot may simply reflect a small sample or a restriction imposed by the fitting procedure. Conversely, a visible pattern may reflect biology not included in the model rather than a technical failure. Interpretation therefore starts with the model that produced the residuals and with the scientific question the adjustment is intended to answer.

Residuals From a Batch-Adjustment Model

For sample \(i\) and feature \(j\), let \(y_{ij}\) denote the measured value, \(b_i\) its batch label, and \(x_i\) a vector of biological covariates intended to be accounted for. A simple feature-specific additive model is

$$ y_{ij}=\beta_{0j}+\gamma_{b_i j}+x_i^\mathsf{T}\theta_j+\epsilon_{ij}. $$

Here, \(\beta_{0j}\) is a feature-specific intercept, \(\gamma_{b_i j}\) is the fitted shift for sample \(i\)'s batch under a chosen reference coding, and \(\theta_j\) contains covariate coefficients. The fitted value is \(\hat{y}_{ij}\), and the raw residual is

$$ r_{ij}=y_{ij}-\hat{y}_{ij}. $$

The residual is in the same measurement units as the feature. Residual plots can use raw residuals, or residuals scaled to make features with different variability more comparable; any scaling should be stated. An additive model assumes the mean response is adequately represented by the included batch and covariate terms. Classical least-squares uncertainty calculations additionally rely on conditions such as independent errors with constant variance; normal errors are used for exact small-sample normal-theory inference, not for the basic definition of a residual.

Definition: A residual check after batch adjustment examines the residuals from the fitted adjustment model for patterns related to batch, included or omitted covariates, sample order, or other structure. A pattern indicates that the fitted mean or variance model may be inadequate, or that relevant signal remains unexplained; it does not by itself identify the cause.

A common least-squares result is important when checking residual batch association. If \(X\) is the model design matrix, including an intercept and the fitted batch indicators, the ordinary least-squares residual vector is \(r=y-X\hat{\beta}\), where \(\hat{\beta}\) minimizes the sum of squared residuals. The normal equations imply \(X^\mathsf{T}r=0\). In particular, included batch indicators have zero residual sum within each represented batch when the fit includes an intercept and the corresponding indicator columns.

Why Included Batch Means Can Be a Misleading Check

The orthogonality result follows directly from minimizing the residual sum of squares. For any coefficient vector \(\beta\), define \(Q(\beta)=(y-X\beta)^\mathsf{T}(y-X\beta)\). Differentiating and setting the gradient to zero at the least-squares estimate gives

$$ \frac{\partial Q}{\partial \beta}=-2X^\mathsf{T}(y-X\hat{\beta})=0 \quad\Longrightarrow\quad X^\mathsf{T}r=0. $$

Thus, plotting residuals by batch can still be informative, but a zero in-sample mean residual for every batch is often guaranteed by the fitting operation itself. It is not independent evidence that batch structure has been removed. The plot may instead reveal unequal spread, outliers, multimodality, or patterns against variables not represented by the fitted columns. A held-out assessment can provide another perspective, although its interpretation depends on how the model was estimated and how representative the held-out samples are.

Worked Example: Residual Spread Across Two Batches

Consider one hypothetical feature measured in six samples. Let \(x\) be a biological covariate, and fit \(y=10+4I(B)+2x\), where \(I(B)=1\) for batch B and \(0\) for batch A. The table gives measured values and the fitted values from this model.

Batch\(x\)\(y\)\(\hat{y}\)\(r=y-\hat{y}\)
A0910-1
A114122
A21314-1
B01214-2
B120164
B21618-2

For batch A, the residuals sum to \(-1+2-1=0\); for batch B, they sum to \(-2+4-2=0\). This is consistent with the included batch indicators and does not establish that all batch structure is gone. As a descriptive spread comparison, the sample variance of the A residuals is \(s_A^2=[(-1)^2+2^2+(-1)^2]/(3-1)=6/2=3\) measurement-units-squared. For B it is \(s_B^2=[(-2)^2+4^2+(-2)^2]/(3-1)=24/2=12\) measurement-units-squared.

The residual spread is four times as large in B in this tiny hypothetical example. That is a signal to inspect variance patterns, influential observations, and model assumptions; with only three observations per batch, it is not persuasive evidence of a population variance difference. The fit also deserves a basic check: within each batch the residuals sum to zero and their products with centered \(x\) sum to zero. For A, \((-1)(-1)+2(0)+(-1)(1)=0\); for B, \((-2)(-1)+4(0)+(-2)(1)=0\). This is what the fitted intercept, batch term, and linear covariate impose in this balanced example.

Look for Patterns Against Covariates

Residual-versus-covariate plots can reveal a mean relationship that the model did not capture. Curvature suggests that a linear term may be insufficient; a change in slope across batches suggests that an additive model may miss an interaction. Such patterns are clues, not automatic instructions to add terms: the proposed form must be scientifically defensible and estimable from the design. The earlier material on rank and connectivity checks for batch designs is relevant because a complicated model cannot resolve a contrast that the observed design does not identify.

Worked Example: Detecting Curvature Left by a Linear Covariate Term

Suppose a hypothetical feature follows \(y=10+4I(B)+2z+z^2\), but the adjustment model includes only an intercept, batch, and a linear term in \(z\). Each batch has one sample at each \(z=0,1,2,3\), so batch and \(z\) are not confounded in this constructed example. The quadratic contribution \(q=z^2\) has values \(0,1,4,9\). Its mean is \(3.5\); the mean of \(z\) is \(1.5\). The least-squares slope for regressing \(q\) on \(z\) is

$$ \frac{\sum (z_i-\bar{z})(q_i-\bar{q})} {\sum (z_i-\bar{z})^2} = \frac{(-1.5)(-3.5)+(-0.5)(-2.5)+(0.5)(0.5)+(1.5)(5.5)} {(-1.5)^2+(-0.5)^2+(0.5)^2+(1.5)^2} = \frac{15}{5}=3. $$

The fitted intercept for \(q\) is \(3.5-3(1.5)=-1\). The residuals from this linear approximation are therefore \(q-(-1+3z)\), giving \(1,-1,-1,1\) at \(z=0,1,2,3\). In the stated outcome model, the fitted linear covariate slope becomes \(2+3=5\), the fitted intercept becomes \(10-1=9\), and the batch shift remains \(4\). The residuals still follow \(1,-1,-1,1\) within each batch. A residual-versus-\(z\) plot would show a curved pattern: positive at the ends and negative in the middle. The arithmetic identifies a failure of the specified linear mean form for these data; it does not establish that a quadratic term is the right scientific explanation in an actual analysis.

Check Associations With Biological Variables

Residual association with a measured biological covariate that was not included in the adjustment model can indicate unexplained biological signal, an omitted confounder, or misspecification. It is not necessarily a batch problem. If the covariate was included in an ordinary least-squares fit, its residual association may be forced to zero by the same orthogonality property, so it cannot serve as an independent check of retention. Instead, compare the biological contrast estimated in a model that includes the covariate with the corresponding contrast before adjustment, and consider whether both estimates answer the intended question.

Worked Example: Residual Association With an Omitted Biological Covariate

Take the following hypothetical measurements for a feature. A batch-only model fits a separate mean for A and B. The covariate \(z\) is not included.

Batch\(z\)\(y\)Batch fitted meanResidual \(r\)
A-2810-2
A010100
A212102
B-11415-1
B015150
B116151

The fitted means are \( (8+10+12)/3=10\) for A and \((14+15+16)/3=15\) for B. The residual vector is exactly the \(z\) vector, \((-2,0,2,-1,0,1)\). Both have mean zero. Using the Pearson correlation formula, their centered cross-product sum is \(4+0+4+1+0+1=10\), and each sum of squares is \(4+0+4+1+0+1=10\). Thus,

$$ r_{r,z}= \frac{\sum_i(r_i-\bar{r})(z_i-\bar{z})} {\sqrt{\sum_i(r_i-\bar{r})^2\sum_i(z_i-\bar{z})^2}} = \frac{10}{\sqrt{10\cdot10}}=1. $$

The perfect residual association is a strong descriptive sign that the batch-only fit leaves a pattern related to \(z\). It could represent biological signal, another systematic covariate, or both; the calculation alone cannot decide. A relevant next check is whether \(z\) should be represented in the adjustment model and whether the design supports separating its association from batch.

Practical Diagnostic Sequence and Alternatives

A useful residual review combines plots with summaries, and asks what each display can and cannot establish. For many features, inspect a manageable set of representative features and summaries across features; a single feature plot cannot describe the full data matrix. Keep the scale and preprocessing stage clear, because residuals are specific to the values and model that generated them.

1
Confirm the fitted model.
Record the response scale, batches, covariates, coding, and observations used. Check that the intended terms were estimable and that the fit converged where relevant.
2
Plot residuals against fitted values and batch.
Look for changing spread, outliers, clusters, or distributional differences. Do not treat zero batch-specific residual means as independent confirmation when batch indicators were fitted.
3
Plot against relevant covariates and sample order.
Look for curvature, slope changes, trends, or clusters that suggest omitted terms or unmodelled structure. Separate plausible technical explanations from biological ones.
4
Check biological contrasts separately.
Compare the target contrast under an appropriate model with and without the adjustment, and state the population and scale it describes. A residual association is not itself an effect estimate.
5
Assess sensitivity and limitations.
Ask whether patterns depend on a few samples or on a modelling choice. Report unresolved patterns and avoid claiming that diagnostics prove successful correction.

Residual PCA can summarize multivariate patterns that are hard to see feature by feature, while residual-versus-covariate plots retain a direct interpretation for selected features. The earlier material on detecting batch structure with PCA motivates using such multivariate views alongside plots, rather than replacing them. Formal association tests can quantify evidence under a specified null model, but the choice of test and dependence among many features matter; a small p-value does not tell whether a pattern is technical or biological. Visual checks are often more revealing about form, but can be subjective and have limited power. These approaches complement one another rather than providing interchangeable proofs.

Common Mistakes and Limitations

  • Taking zero residual batch means as proof of correction. In least squares, this may be imposed by the batch columns in the fitted design. Check residual spread and shape, and do not mistake a fitting constraint for independent validation.
  • Calling every residual pattern technical. A relationship with a biological covariate can be meaningful biology, omitted confounding, or misspecification. Use the study question and design to interpret it.
  • Treating a pattern as a model instruction. Curvature suggests a possible problem with the linear form, but adding terms without design support can create unstable or non-identifiable estimates.
  • Using a clean plot as proof of no remaining structure. Small samples, noisy measurements, and limited display choices can hide patterns. Diagnostics have limited ability to detect subtle effects.
  • Confusing residual behavior with preservation of a biological effect. The fitted biological coefficient or contrast and its uncertainty should be examined directly. Residuals are deviations from the fitted model, not a substitute for that estimand.

Residual checks are limited by the model, the observed covariate overlap, measurement quality, and sample size. They can identify incompatibility between data and a chosen model, but generally cannot determine which of several explanations is correct. In particular, where batch and biology overlap poorly, a visually attractive residual pattern does not resolve the underlying identification problem.

Key takeaway: Residual diagnostics ask what remains unexplained after a specified batch-adjustment model. Examine batch-stratified spread and shape, covariate patterns, and biological contrasts; interpret each in light of the algebraic constraints and limitations of the fit.

Check Your Understanding

Work through each question, then compare your reasoning with the solutions.

  1. Why does a least-squares model with batch indicators often have mean residual zero within each batch? What does this fact fail to demonstrate?
  2. A residual-versus-covariate plot shows a U-shaped pattern. Name one possible model issue and one reason not to add a quadratic term automatically.
  3. Residuals for a feature have a larger spread in one batch, but their means are zero in both batches. What should be concluded from this alone?
  4. A biological covariate was included in the fitted model, and residuals show no linear association with it. Why is that not an independent test that biological signal was retained?

Solutions. (1) The least-squares normal equations make residuals orthogonal to the included batch-indicator columns, so their within-batch sums are zero. This does not show that residual variance, shape, or all batch-related structure is the same. (2) A linear covariate term may fail to represent curvature. A quadratic term may be scientifically inappropriate or unsupported by the design. (3) The pattern merits investigation as a possible variance difference, but the mean result is constrained by the fit; sample size, outliers, and uncertainty must be considered before drawing a population conclusion. (4) Orthogonality to an included covariate is imposed by least squares. Assess the biological coefficient or contrast directly and consider whether the adjustment altered the target estimate.

Clinical Biostats