Search

Search papers and lessons

Batch Effect

Level 3 · 11 min read

A batch effect is a systematic difference in measurements caused by when, where or how samples were processed, and it can be mistaken for biology unless it is modelled and removed.

On this page

  • Colour = topic mix
  • Calculus
  • Molecular biology
  • Classical mechanics
  • Linear algebra
  • Probability
  • Discrete math
  • ReadReady to readNot yet

TL;DR

A batch effect is a difference in measurements that comes from how samples were handled, not from the biology being studied. Samples prepared on one day, by one person, with one lot of reagent, often differ from samples prepared on another day in ways that have nothing to do with the question. The effect can be removed with a model of the batches, but only if the batches are not tangled up with the biology.

Builds on: RNA Sequencing, which counts reads per gene, and Linear Regression, which estimates a shift between groups · Leads to: Differential Expression Analysis, where a batch effect can look like a gene that is switched on, and Harmony, a method for merging many batches of single-cell data

Where it's used

Single-cell atlases. A study that profiles many cells often runs them in several batches, because one run cannot hold every sample. When the batches are merged, the cells from one batch can cluster apart from the cells of the same type in another batch. Researchers must separate that clustering from real differences between cell types before they name the clusters.

Gene expression studies. A lab that compares patient and control samples over several months will often process the groups on different dates. Any gene expression level that shifts with the date will look different between the groups, even when the biology is the same. Analysts use differential expression analysis to find real differences, and they need a batch term in the model so that batch differences are not called real.

Microarray and sequencing archives. Public datasets are often combined from many labs and many years. Their processing differs, so combining them without correction can make unrelated studies look alike or different. This was a widely reported problem in the 2000s, when microarray data were first pooled at scale.

How it works

Take one gene measured in four samples. The biology is the same in every sample, so the true level is the same everywhere. The samples were processed in two batches, A and B, and the measured values are these:

SampleBatchMeasured value
1A10
2A12
3B16
4B18

Batch B reads about 6 higher than batch A, even though the biology is identical. The steps below find that shift and remove it.

  1. Detect the batches. A first check is to plot the samples on their two largest directions of variation with Principal Component Analysis. If the samples split by batch rather than by biology, a batch effect is present. Here the two batches are already obvious from the values. Check: the batch means are 11 for A and 17 for B, a gap of 6.

  2. Compute the overall mean. Add all four values and divide by four. (10+12+16+18)/4=56/4=14(10 + 12 + 16 + 18) / 4 = 56 / 4 = 14. This is the level the data would have if there were no batches. Check: two values sit below 14 and two above, as expected.

  3. Find each batch's offset. The offset is the batch mean minus the overall mean. For batch A it is 11−14=−311 - 14 = -3. For batch B it is 17−14=+317 - 14 = +3. Check: the two offsets sum to zero, so the correction does not shift the whole dataset.

  4. Subtract the offset. Batch A samples become 10+3=1310 + 3 = 13 and 12+3=1512 + 3 = 15. Batch B samples become 16−3=1316 - 3 = 13 and 18−3=1518 - 3 = 15. Check: both batches now have mean 14, and the gap of 6 is gone.

  5. Check that the biology survived. The within-batch spread, 13 and 15 in each batch, is unchanged. The correction moved the batches, not the samples within them. Check: the spread of 2 within each batch is the same before and after.

Step 3 is the key step. The offsets are estimated from the batch means, and the correction is only correct when each batch contains a similar mix of biology. If batch A held all the controls and batch B all the patients, the offset would also remove the real difference between them.

The math

Definition: Batch effect model

For a measured value yijy_{ij} from sample jj in batch b(j)b(j), a simple model is yij=μ+αb(j)+εijy_{ij} = \mu + \alpha_{b(j)} + \varepsilon_{ij} where μ\mu is the overall level, αb\alpha_b is the offset of batch bb, and εij\varepsilon_{ij} is the noise. When each offset is weighted by its batch size, the offsets sum to zero, so μ\mu is the average over all samples.

In words: every sample is the overall level, plus the shift of its batch, plus random noise. Correction means estimating the αb\alpha_b and subtracting them.

Step 1 in symbols: the batches as indicator variables

Write one indicator xBx_B for batch B, which is 1 for a sample in batch B and 0 otherwise. Then the model is a linear regression on xBx_B: y=β0+β1xB+εy = \beta_0 + \beta_1 x_B + \varepsilon Here β0\beta_0 is the mean of batch A and β1\beta_1 is the difference between the batch means.

Step 2 in symbols: the least-squares estimates

With the indicator model, β^0=yˉA=(10+12)/2=11\hat\beta_0 = \bar y_A = (10 + 12)/2 = 11 and β^1=yˉB−yˉA=17−11=6\hat\beta_1 = \bar y_B - \bar y_A = 17 - 11 = 6. These match the measured gap of 6 in the walkthrough. The offsets in the sum-to-zero form are α^b=yˉb−yˉ,yˉ=1n∑jyj\hat\alpha_b = \bar y_b - \bar y, \qquad \bar y = \frac{1}{n}\sum_j y_j With yˉA=11\bar y_A = 11, yˉB=17\bar y_B = 17 and yˉ=14\bar y = 14, this gives α^A=−3\hat\alpha_A = -3 and α^B=+3\hat\alpha_B = +3.

Step 3 in symbols: the correction

The corrected value is yij−α^b(j)y_{ij} - \hat\alpha_{b(j)}. For sample 2 in batch A, 12−(−3)=1512 - (-3) = 15. For sample 3 in batch B, 16−3=1316 - 3 = 13. Both batches now have mean yˉ=14\bar y = 14.

Step 4 in symbols: the check

After correction, the batch means are equal by construction. A useful check is that the residual spread within each batch matches the spread before correction, since only the mean was moved.

Worked examples

Example
1. Four samples, two batches (tiny)

Use the four values from the walkthrough: batch A holds 10 and 12, and batch B holds 16 and 18.

  1. Batch means: yˉA=(10+12)/2=11\bar y_A = (10 + 12)/2 = 11 and yˉB=(16+18)/2=17\bar y_B = (16 + 18)/2 = 17.
  2. Gap: 17−11=617 - 11 = 6.
  3. Overall mean: (10+12+16+18)/4=14(10 + 12 + 16 + 18)/4 = 14.
  4. Offsets: 11−14=−311 - 14 = -3 for A and 17−14=317 - 14 = 3 for B.
  5. Corrected values: 13,1513, 15 for batch A and 13,1513, 15 for batch B.

Interpretation: after the correction the four samples form two identical groups, so the gap that came from processing has been removed.

Example
2. Two batches with unequal sizes (standard)

A gene is measured in batch A, with three samples at 8, 9 and 10, and in batch B, with one sample at 14. The batches have different sizes, so the overall mean must weight each sample equally.

  1. Overall mean over all four samples: (8+9+10+14)/4=41/4=10.25(8 + 9 + 10 + 14) / 4 = 41 / 4 = 10.25.
  2. Batch A mean: (8+9+10)/3=9(8 + 9 + 10) / 3 = 9.
  3. Batch B mean: 1414, since there is one sample.
  4. Offsets: 9−10.25=−1.259 - 10.25 = -1.25 for A and 14−10.25=3.7514 - 10.25 = 3.75 for B.
  5. Check: the offsets weighted by sample count sum to zero: 3×(−1.25)+1×3.75=−3.75+3.75=03 \times (-1.25) + 1 \times 3.75 = -3.75 + 3.75 = 0.
  6. Corrected sample in batch B: 14−3.75=10.2514 - 3.75 = 10.25.

Interpretation: the single sample in batch B is pulled to the overall mean. With one sample, its batch offset is estimated from very little data, which is a warning sign. The estimate is noisy, and this is where the methods in the tradeoffs section help.

Example
3. A single-cell gene in three batches (realistic)

A single-cell study measures a gene as a log2-normalised expression value. The cells were run in three batches with equal numbers of cells in each, and the batch means were 4.14.1, 5.05.0 and 3.63.6.

  1. Overall mean: (4.1+5.0+3.6)/3=12.7/3≈4.2333(4.1 + 5.0 + 3.6) / 3 = 12.7 / 3 \approx 4.2333.
  2. Offset for batch 1: 4.1−4.2333≈−0.13334.1 - 4.2333 \approx -0.1333.
  3. Offset for batch 2: 5.0−4.2333≈+0.76675.0 - 4.2333 \approx +0.7667.
  4. Offset for batch 3: 3.6−4.2333≈−0.63333.6 - 4.2333 \approx -0.6333.
  5. Check: the three offsets sum to about zero: −0.1333+0.7667−0.6333≈0-0.1333 + 0.7667 - 0.6333 \approx 0.
  6. Corrected cell in batch 2 with a measured value of 5.6: 5.6−0.7667≈4.83335.6 - 0.7667 \approx 4.8333.

Try it: a cell from batch 3 has a measured value of 3.9. What is its corrected value?

Answer: 3.9−(−0.6333)=3.9+0.6333≈4.53333.9 - (-0.6333) = 3.9 + 0.6333 \approx 4.5333. The corrected value is about 4.53, which is higher than the measured value because batch 3 reads low.

Common mistakes and tradeoffs

It is tempting to think that batch effects are just noise, and that averaging over enough samples will remove them. In fact a batch effect is systematic: every sample in a batch shares the shift, so averaging does not cancel it. A batch that holds more of one group makes the group look different for reasons that have nothing to do with biology.

It is tempting to think that correction is always safe. In fact correction removes whatever differs between batches, and if the groups you are comparing are confounded with batch, the correction removes the real difference too. This is the confounding problem in another form. The fix is in the design, not the software: process groups in balanced batches, and randomise the order of processing.

It is tempting to think that stronger correction is always better. In fact aggressive integration can merge cell types that really differ, because two distinct types can look similar after the batch shift is removed. Check the result against known markers before naming clusters.

Use a simple offset or a regression term when the batches are known, the design is balanced, and there are few batches. Use a mixed model, which treats batch as a random effect and shrinks small or noisy batch estimates toward zero, when there are many batches or some batches are small. Use Empirical Bayes methods when many genes must be corrected at once and each batch estimate is noisy. Use an integration method, as in the single-cell tools that merge many batches at once, when the data are large and not a simple shift. None of these fixes an unbalanced design, so the design should come first.

History

Batch effects were noticed early in the age of microarrays, when labs in the late 1990s and early 2000s found that measurements shifted with the day and the reagent lot. Researchers at first treated the shifts as an annoyance to be avoided by careful handling.

By the late 2000s, large pooled datasets made the problem impossible to ignore. Leek and colleagues argued in 2010 that batch effects were widespread and could produce false findings in high-throughput studies, and that they needed to be modelled explicitly.

In 2007 Johnson, Li and Rabinovic published a method called ComBat, which estimates batch shifts with an Empirical Bayes approach that borrows strength across genes. It became a common tool for microarray and later sequencing data. Single-cell studies, which pool many batches of cells, pushed the field toward integration methods in the late 2010s.

Recap

  • A batch effect is a systematic shift in measurements from processing, and it is not biology.
  • A simple model writes each measurement as the overall level, plus a batch offset, plus noise, and the offsets are the differences between batch means and the overall mean.
  • Correction subtracts the estimated offsets and leaves the within-batch spread unchanged.
  • The correction is only valid when the biology is balanced across batches; if the groups are confounded with batch, the correction removes real signal.
  • Read next: Differential Expression Analysis for finding real differences once batches are handled, and Harmony for merging many batches of single-cell data.

Check yourself

QuestionRecall: what is a batch effect, and why can't averaging over more samples remove it?Show answer
A batch effect is a systematic shift in measurements caused by how samples were processed. Averaging does not remove it, because every sample in a batch carries the same shift, so the batch's mean stays offset from the others.
QuestionTwo batches have means 20 and 26, and the overall mean of all samples is 23. What is the offset of the batch with mean 26, and what is the corrected value of a sample in that batch measured at 29?Show answer
The offset is 26−23=326 - 23 = 3. The corrected value is 29−3=2629 - 3 = 26.
QuestionWhy is it dangerous to correct a batch effect when all the treated samples were processed in one batch?Show answer
Because the batch offset is estimated from the batch means, and the treated group's mean is part of the batch mean. The correction removes the batch shift, which is also the treatment difference, so the real effect is partly or wholly removed. The fix is a balanced design, with treated and control samples processed in every batch.
QuestionA lab has 5 batches of cells, but one batch has only 3 cells. Which method is safer for estimating that batch's offset, and why?Show answer
A mixed model or an empirical-Bayes method, because a batch with only 3 cells gives a noisy offset estimate. These methods shrink the estimate toward the average of the other batches, so a few unusual cells do not drive a large correction.

Sources