Tutorials › High-Dimensional Genomic Data Analysis › ComBat Empirical Bayes Batch Adjustment

Normalization and Batch Effects · Tutorial 93 of 795

ComBat Empirical Bayes Batch Adjustment

Learn how ComBat estimates and shrinks batch-specific expression shifts and scales, then uses those estimates to adjust values while retaining modeled biological effects.

Intermediate 12 min read

What You'll Learn

  • Describe ComBat’s location-scale model for transformed expression values.
  • Explain the normal-error assumption needed for exact conjugate posterior calculations.
  • Distinguish gene-specific batch estimates from batch-wide empirical Bayes priors.
  • Calculate posterior-shrunk location and scale estimates in worked examples.
  • Apply ComBat’s adjustment formula while preserving modeled biological effects.
  • Recognize when confounding, unsuitable inputs, or model assumptions limit adjustment.

Why ComBat Uses Empirical Bayes Shrinkage

A batch can shift expression values for many genes, and it can also change their spread. Removing a separate location and scale estimate for every gene and batch is possible, but estimates from small batches can be unstable. ComBat addresses this by estimating gene-specific batch effects and shrinking them toward batch-wide distributions learned across genes. The aim is to reduce technical differences while retaining modeled biological variation.

ComBat is intended for expression measurements on a suitable transformed, approximately continuous scale, such as log-scale expression, rather than raw sequencing counts. The earlier material on library-size normalization and variance-stabilizing transformations establishes why counts commonly need appropriate normalization and transformation before this kind of location-scale adjustment. ComBat does not make confounded biology and batch separately identifiable: if batch and condition are perfectly confounded, the data cannot determine which explains their shared differences.

Definition: In ComBat, \(Y_{ij}\) is the transformed expression for gene \(i\) in sample \(j\), \(b(j)\) is that sample’s batch, and \(X_j\) contains observed biological covariates to preserve. A location effect \(\gamma_{i,b}\) shifts gene \(i\)'s expression in batch \(b\); a scale effect \(\delta_{i,b}\) changes its residual spread. Empirical Bayes shrinkage estimates these effects using both the gene’s data and distributions of effects across genes.

The Location-Scale Model and Assumptions

A common ComBat model is

$$ Y_{ij}=\alpha_i+X_j\beta_i+\gamma_{i,b(j)}+\delta_{i,b(j)}\epsilon_{ij}. $$

Here, \(\alpha_i+X_j\beta_i\) represents the gene-specific baseline and modeled biological effects, while \(\gamma_{i,b(j)}\) and \(\delta_{i,b(j)}\) represent batch location and scale effects. The model assumes \(\delta_{i,b}>0\). For the usual exact normal likelihood and conjugate posterior calculations, assume the errors are independent and normally distributed, \(\epsilon_{ij}\sim N(0,1)\), conditional on the modeled effects. Mean zero and variance one alone do not imply normality, so they are not sufficient for those exact calculations.

ComBat first estimates and removes the modeled baseline and biological covariates, then standardizes each gene using a pooled residual scale \(s_i\). Write the standardized residual as

$$ Z_{ij}=\frac{Y_{ij}-\hat{\alpha}_i-X_j\hat{\beta}_i}{s_i}. $$

In this standardized scale, a batch’s gene-specific observations are modeled as \(Z_{ij}=\gamma_{i,b}+\delta_{i,b}\epsilon_{ij}\). Standardization gives genes comparable scales for estimating the across-gene priors. In practice, the fitted baseline and standardization are estimated from the data; their adequacy and the resulting residual batch structure require careful assessment.

For a batch \(b\), ComBat uses distributions across genes such as \(\gamma_{i,b}\sim N(\bar{\gamma}_b,\tau_b^2)\) and \(\delta_{i,b}^2\sim \operatorname{InvGamma}(\lambda_b,\theta_b)\). The inverse-gamma parameterization here has density proportional to \(v^{-\lambda_b-1}\exp(-\theta_b/v)\) for \(v>0\). The prior hyperparameters are estimated across genes within a batch. The assumption is that genes in that batch provide a useful shared distribution for stabilizing one another’s estimates; it does not require all genes to have the same batch effect.

Assumptions to check: The expression scale is suitable for a continuous location-scale model; the batch labels and modeled biological covariates are correctly specified; there is enough overlap between batches and biological conditions to distinguish their effects; and genes within a batch provide a reasonable basis for estimating shared prior distributions. The exact normal posterior formulas below additionally require normally distributed errors. If that assumption is not credible, those formulas are not exact, even when the errors have mean zero and variance one.

How Shrinkage Is Calculated

For one gene and batch, let \(n_b\) be the number of samples in the batch and \(\bar{Z}_{i,b}\) the mean standardized residual. If the batch scale variance \(\delta_{i,b}^2\) is treated as known and the standardized errors are independent normal, the batch mean has the exact likelihood

$$ \bar{Z}_{i,b}\mid\gamma_{i,b},\delta_{i,b}^2 \sim N\left(\gamma_{i,b},\frac{\delta_{i,b}^2}{n_b}\right). $$

Combining this likelihood with the normal prior on \(\gamma_{i,b}\) gives a normal posterior. Its mean is a weighted average of the gene’s observed batch mean and the batch-wide prior mean:

$$ \gamma^*_{i,b} = \frac{n_b\tau_b^2\bar{Z}_{i,b}+\delta_{i,b}^2\bar{\gamma}_b} {n_b\tau_b^2+\delta_{i,b}^2}, \qquad \operatorname{Var}(\gamma_{i,b}\mid Z,\delta_{i,b}^2) = \frac{\delta_{i,b}^2\tau_b^2} {n_b\tau_b^2+\delta_{i,b}^2}. $$

The observed mean receives more weight when the batch has more samples or the prior variance \(\tau_b^2\) is large. The prior mean receives more weight when the batch is small or the gene-level residual variance is large. This is partial pooling: estimates are drawn toward a shared center, not forced to equal it.

For scale, conditional on a fixed location \(\gamma_{i,b}\), the standardized residual sum of squares is \(R_{i,b}=\sum_{j:b(j)=b}(Z_{ij}-\gamma_{i,b})^2\). With the inverse-gamma prior above and normal errors, the conditional posterior is inverse-gamma with shape \(\lambda_b+n_b/2\) and scale \(\theta_b+R_{i,b}/2\). When the shape exceeds one, its mean is

$$ E(\delta_{i,b}^2\mid Z,\gamma_{i,b}) = \frac{\theta_b+R_{i,b}/2}{\lambda_b+n_b/2-1}. $$

In the empirical Bayes procedure, location and scale estimates are updated using one another, with batch-wide hyperparameters estimated across genes. A calculation that plugs a shrunk location estimate into the residual sum of squares is a conditional empirical Bayes update, not a claim that the fully joint posterior has this simple independent form. The normal likelihood is essential for the stated exact conjugate updates.

Worked Example: Shrinking a Batch Location Estimate

Suppose a hypothetical gene has standardized residuals \(1.2, 1.4, 1.0,\) and \(1.4\) in a batch. Assume independent normal errors, a known scale variance \(\delta_{i,b}^2=1\), and a batch-wide prior \(\gamma_{i,b}\sim N(0,0.25)\). These assumptions make the normal posterior calculation exact for the location.

The batch mean is \(\bar{Z}_{i,b}=(1.2+1.4+1.0+1.4)/4=5.0/4=1.25\), and \(n_b=4\). Thus

$$ \gamma^*_{i,b} = \frac{4(0.25)(1.25)+(1)(0)}{4(0.25)+1} = \frac{1.25}{2} = 0.625. $$

The unshrunk location estimate is \(1.25\), but the posterior estimate is \(0.625\), halfway between that mean and the prior mean of zero. For this location-only calculation, the adjusted standardized residuals are \(Z_{ij}-0.625\): \(0.575,\ 0.775,\ 0.375,\) and \(0.775\). The correction reduces the estimated batch shift; it does not claim that each individual value is error-free or that the posterior location is known exactly.

Worked Example: Shrinking a Batch Scale Estimate

Consider hypothetical standardized residuals \(-1,\ 1,\ -2,\ 2\) in one batch. Assume independent normal errors, a location fixed at zero for this conditional scale calculation, and a prior \(\delta_{i,b}^2\sim\operatorname{InvGamma}(3,2)\). The sample mean is zero, so zero is also the observed location for these data.

The residual sum of squares is \(R_{i,b}=(-1)^2+1^2+(-2)^2+2^2=1+1+4+4=10\). The posterior shape is \(3+4/2=5\), and the posterior scale is \(2+10/2=7\). Since the shape exceeds one, the conditional posterior mean is

$$ E(\delta_{i,b}^2\mid Z,\gamma_{i,b}=0) = \frac{7}{5-1} = 1.75. $$

The corresponding posterior mean of the variance is \(1.75\); using its square root as a scale estimate gives \(\sqrt{1.75}\approx1.323\). This scale estimate is pulled toward the prior distribution rather than being only the unshrunk sample variance. The calculation is conditional on the stated fixed location; estimating location jointly requires the iterative empirical Bayes updates described above.

Adjusting Expression Values

After estimating batch location and scale effects, ComBat removes them from the standardized residuals and restores the gene’s original scale and modeled biological component. In the notation above, the adjusted value is

$$ Y^{\mathrm{adj}}_{ij} = \hat{\alpha}_i+X_j\hat{\beta}_i + s_i\frac{Z_{ij}-\gamma^*_{i,b(j)}}{\delta^*_{i,b(j)}}. $$

The location estimate is subtracted and the residual is divided by the estimated batch scale. The biological component \(\hat{\alpha}_i+X_j\hat{\beta}_i\) is retained. The adjustment therefore depends on the fitted covariate model and on the assumption that batch effects are separable from the biology represented by \(X\).

Worked Example: Applying the Location-Scale Adjustment

For a hypothetical gene, suppose a batch has standardized residuals \(1,1,3,3\), with \(\hat{\alpha}_i+X_j\hat{\beta}_i=10\) and \(s_i=2\). The corresponding original-scale values are \(10+2Z_{ij}\), or \(12,12,16,16\). Assume independent normal errors and use prior parameters \(\bar{\gamma}_b=0,\ \tau_b^2=0.25,\ \lambda_b=3,\) and \(\theta_b=2\).

The batch mean is \((1+1+3+3)/4=2\). For the location update, use the observed residual variance of \(1\) as the conditional value of \(\delta_{i,b}^2\). Then

$$ \gamma^*_{i,b} = \frac{4(0.25)(2)+(1)(0)}{4(0.25)+1} = \frac{2}{2} = 1. $$

Using this shrunk location in the conditional scale update, the residual sum of squares is \((1-1)^2+(1-1)^2+(3-1)^2+(3-1)^2=0+0+4+4=8\). The posterior shape is \(3+4/2=5\), and the posterior scale is \(2+8/2=6\), so the conditional posterior mean of the scale variance is \(6/(5-1)=1.5\). Use \(\delta^*_{i,b}=\sqrt{1.5}\approx1.225\).

For each observation with \(Z=1\), the adjusted value is \(10+2(1-1)/1.225=10\). For each with \(Z=3\), it is \(10+2(3-1)/1.225\approx13.27\). The four adjusted values are therefore approximately \(10,\ 10,\ 13.27,\ 13.27\). The calculation illustrates location and scale correction with plug-in empirical Bayes estimates; it is not an exact joint posterior calculation. The corrected values need not equal a common mean, because genuine sample-level variation remains.

What ComBat Adds, and Where It Can Fail

A standard linear adjustment with batch indicators estimates batch coefficients without borrowing strength across genes. That can be appropriate when its model is adequate and the estimates are sufficiently precise. ComBat adds empirical Bayes shrinkage, which can stabilize gene-specific estimates when many genes share information and batch sample sizes are limited. It is not a substitute for a paired or blocked design: those designs control batch through the study structure, whereas ComBat estimates and adjusts batch effects after data collection.

ComBat’s usefulness depends on design and scale. If every sample in one condition was processed in one batch and every sample in another condition in a different batch, condition and batch are confounded. No adjustment can reliably preserve the condition effect while removing the batch effect because the observed data do not distinguish them. The method also does not justify applying a normal location-scale model directly to raw counts, nor does it guarantee that all technical differences have been removed.

A key diagnostic is whether the model’s assumptions and estimated effects are plausible for the data: check the expression scale, sample sizes and condition-by-batch overlap, and whether the fitted covariates reflect the biology to retain. A later examination of ComBat fit and residual batch structure addresses diagnostics in more detail. For this tutorial, the essential caution is that shrinkage controls estimation variability under its model; it cannot repair an unidentifiable design or make a misspecified model correct.

Common mistake: “The errors have mean zero and variance one, so the batch mean is exactly normal and the conjugate posterior formulas apply.” This is false: independent errors taking values \(+1\) and \(-1\) with equal probability have mean zero and variance one, but their finite-sample mean is discrete. The exact normal likelihood and posterior formulas require normally distributed errors. Without that assumption, use the formulas only as an explicitly justified approximation, not as an exact result.
Key takeaway: ComBat estimates gene-specific batch shifts and scales, then shrinks them toward batch-wide empirical Bayes distributions before adjusting expression. Its exact conjugate calculations rely on normal errors, and its interpretation relies on a design that separates batch from the biology to retain.

Check Your Understanding

Answer each question, then compare with the solutions.

  1. Why does an error distribution with mean zero and variance one not, by itself, guarantee a normal likelihood for a batch mean?
  2. A gene’s batch mean is \(1.5\) from \(n_b=4\) samples. Let \(\delta_{i,b}^2=1\) and the prior be \(N(0,0.5)\). Calculate the posterior mean for the location.
  3. With an inverse-gamma prior having \(\lambda_b=2,\theta_b=1\), \(n_b=4\), and a fixed location yielding residual sum of squares \(R_{i,b}=6\), calculate the conditional posterior mean of \(\delta_{i,b}^2\).
  4. What happens to ComBat’s ability to retain a biological condition effect if that condition is perfectly confounded with batch?

Solutions. (1) Mean and variance do not determine the full distribution. For example, independent errors taking only \(+1\) or \(-1\) have mean zero and variance one, but their finite-sample mean is discrete. Exact normal likelihood formulas require normal errors. (2) Using \(\tau_b^2=0.5\) and \(\bar{\gamma}_b=0\), \(\gamma^*_{i,b}=[4(0.5)(1.5)+1(0)]/[4(0.5)+1]=3/3=1\). (3) The posterior shape is \(2+4/2=4\) and scale is \(1+6/2=4\), so the conditional posterior mean is \(4/(4-1)=1.333\). (4) The data cannot distinguish the condition effect from the batch effect; ComBat cannot reliably remove one while preserving the other.

Clinical Biostats