Skip to content

ComBat and Its Limits

The second remedy rewrites the data instead of the model. ComBat, from the sva package, estimates for each batch and each gene a location shift and a scale change, shrinks those estimates across genes with an empirical Bayes prior, and subtracts the batch part. What is left is a matrix in which the batch is gone and the rest of the structure is kept, in principle.

The design route of the previous page is the default because it keeps the raw counts as the model input and keeps the batch in the error model. The removal route earns its place when the downstream method has no design formula to receive a batch term: a clustering, a heatmap, an integration of two studies, a plot that a reviewer wants to see without the batch in it. This page runs ComBat on the transformed pasilla matrix, then compares differential expression after ComBat-seq with the model that includes library type in its design. The comparison measures how the results change; it has no truth set that identifies false discoveries.

ComBat expects data on a roughly Gaussian scale, so it runs on the variance-stabilized matrix rather than the counts. The mod argument carries the condition, which tells ComBat what to protect while it removes the batch. Leaving mod out is a real mistake here: the library type and the condition are correlated in this design, so an unprotected ComBat would remove part of the knockdown signal along with the batch.

The count and sample import below adapts the Count matrix input example in the DESeq2 vignette, by Michael I. Love, Simon Anders, and Wolfgang Huber. It reads the pasilla files, aligns count columns with the sample table, and uses this lesson’s total-count filter. The link points to the DESeq2 1.30.1 version of the vignette.

The compared DESeq2 1.30.1 vignette ships in its source archive under LGPL version 3 or later. The LGPL text and its GPL terms accompany this credit. The count and annotation files come from Wolfgang Huber and Alejandro Reyes’s pasilla 1.40.0 ExperimentData package. Its source notice records the package grant and the Brooks experiment. This lesson filters those counts and derives its displayed results from them.

library(DESeq2)
library(pasilla)
# Counts and annotation, loaded the way the DESeq2 vignette does.
pas_anno <- system.file("extdata", "pasilla_sample_annotation.csv",
package = "pasilla", mustWork = TRUE)
coldata <- read.csv(pas_anno, row.names = 1)
coldata <- coldata[, c("condition", "type")]
rownames(coldata) <- sub("fb", "", rownames(coldata))
coldata$condition <- relevel(factor(coldata$condition), ref = "untreated")
coldata$type <- factor(coldata$type)
pas_cts <- system.file("extdata", "pasilla_gene_counts.tsv",
package = "pasilla", mustWork = TRUE)
cts <- as.matrix(read.csv(pas_cts, sep = "\t", row.names = "gene_id"))
cts <- cts[, rownames(coldata)]
# Keep genes with at least 10 reads total.
keep <- rowSums(cts) >= 10
cts <- cts[keep, ]
dds <- DESeqDataSetFromMatrix(cts, coldata, design = ~ condition)
vsd <- vst(dds, blind = TRUE)
# The PCA before correction, for the comparison.
pca_data <- plotPCA(vsd, intgroup = c("condition", "type"),
returnData = TRUE)
library(sva)
# Remove the library type, with the condition protected.
combat_mat <- ComBat(assay(vsd), batch = coldata$type,
mod = model.matrix(~ condition, coldata),
par.prior = TRUE)
Found 30 genes with uniform expression within a single batch (all zeros); these will not be adjusted for batch.

The message is part of the output worth reading. Thirty genes are all zeros within one batch, so ComBat has no information to adjust them with and leaves them alone. An adjustment method that reports what it cannot do is more trustworthy than one that adjusts silently.

# The PCA of the corrected matrix, computed the same way.
vsd_combat <- vsd
assay(vsd_combat) <- combat_mat
pca_after <- plotPCA(vsd_combat, intgroup = c("condition", "type"),
returnData = TRUE)
pca_after[, c("PC1", "PC2", "condition", "type")]
attr(pca_after, "percentVar")
PC1 PC2 condition type
treated1 -10.889226 0.97629296 treated single-read
treated2 -11.281014 -0.47683727 treated paired-end
treated3 -10.978810 -0.37292181 treated paired-end
untreated1 8.118550 -6.13952372 untreated single-read
untreated2 8.365270 2.56693483 untreated single-read
untreated3 8.559306 3.51822133 untreated paired-end
untreated4 8.105924 -0.07216631 untreated paired-end
[1] 0.83867422 0.07584055
# Correlation of PC2 with the library type, before and after.
type_code <- as.numeric(coldata$type == "paired-end")
round(c(before = cor(pca_data$PC2, type_code),
after = cor(pca_after$PC2, type_code)), 3)
before after
-0.94 0.26

The second axis no longer separates the library types, and with it the structure of the matrix changes shape. PC1 now carries 84 percent of the variance, up from 56, because the batch that used to fill the second axis is gone, and PC2 is down from 30 percent to 8. The correlation of PC2 with the type indicator falls from -0.94 before the correction to 0.26 after, which is the removal working as advertised. The first axis still separates treated from untreated, which is the condition surviving. The next figure shows both panels side by side, on the same axes so the shrink of the second axis is visible.

library(ggplot2)
pca_data$panel <- "before"
pca_after$panel <- "after"
both_pca <- rbind(pca_data, pca_after)
combat_plot <- ggplot(both_pca, aes(PC1, PC2, color = condition,
shape = type)) +
geom_point(size = 3.5) +
facet_wrap(~ panel) +
scale_color_manual(values = c(untreated = "#2166ac", treated = "#b2182b")) +
theme_minimal()
ggsave("outputs/combat-pca.png", combat_plot, width = 9, height = 4.5,
dpi = 150, bg = "white")

PCA of the pasilla samples before and after ComBat. Before the correction, single-read and paired-end samples separate along the second axis. After it, the shapes mix on both axes while the colors still separate on the first.

For a heatmap, a clustering, or a figure for a paper, this is the matrix to use. For a differential expression model it is not, and the reason is not a rule of thumb but a property of the method: ComBat returns point estimates with the batch removed and no accounting of the uncertainty that the removal added.

ComBat-seq models batch differences in both the mean and dispersion of counts, then returns adjusted integer counts for downstream analysis. The following comparison runs DESeq2 on those counts with condition in the design, after ComBat-seq protects condition during the adjustment.

# ComBat_seq on the counts, protecting the condition.
adj_counts <- ComBat_seq(cts, batch = coldata$type,
group = coldata$condition)
# A differential run on the corrected counts, batch already removed.
dds_cseq <- DESeqDataSetFromMatrix(adj_counts, coldata,
design = ~ condition)
dds_cseq <- DESeq(dds_cseq, quiet = TRUE)
res_cseq <- results(dds_cseq,
contrast = c("condition", "treated", "untreated"),
alpha = 0.05)
summary(res_cseq)
out of 9921 with nonzero total read count
adjusted p-value < 0.05
LFC > 0 (up) : 755, 7.6%
LFC < 0 (down) : 875, 8.8%
outliers [1] : 1, 0.01%
low counts [2] : 1347, 14%
(mean count < 5)
[1] see 'cooksCutoff' argument of ?results
[2] see 'independentFiltering' argument of ?results
# The design route from the previous page, for the comparison.
dds_batch <- DESeqDataSetFromMatrix(cts, coldata,
design = ~ type + condition)
dds_batch <- DESeq(dds_batch, quiet = TRUE)
res_batch <- results(dds_batch,
contrast = c("condition", "treated", "untreated"),
alpha = 0.05)
sig_batch <- rownames(res_batch)[!is.na(res_batch$padj) &
res_batch$padj < 0.05]
sig_cseq <- rownames(res_cseq)[!is.na(res_cseq$padj) &
res_cseq$padj < 0.05]
length(sig_batch)
length(sig_cseq)
length(intersect(sig_batch, sig_cseq))
[1] 1086
[1] 1630
[1] 1078

The corrected counts yield 1630 significant genes, compared with 1086 for the design route, and the two lists share 1078 genes. Those counts show that the choice of method changes the result. They do not tell us which additional calls are true or false, or whether either method controls its false discovery rate on this experiment.

The ComBat-seq study used simulated data with known differential genes to measure that distinction. Its results depended on whether dispersion differed between batches. Adjustment improved power in settings with dispersion effects, while a separate dispersion adjustment increased false positives in settings without them. A larger discovery list alone cannot select the better method.

For this differential expression example, keep raw counts and include the recorded library type in the design. First check that the design can estimate the condition effect separately from the batch effect. ComBat on transformed data serves a different purpose here: it supplies the adjusted matrix for the PCA. Preserve the biological variables through mod and compare the plots before and after adjustment.

For another study, assess the batch structure and the intended downstream analysis before choosing a correction. If you compare ComBat-seq with a design adjustment, report the changed estimates and assumptions rather than treating one discovery list as the truth.

When the batch is fully confounded with the condition, neither route can help, because the data never contained the information that separates them. That case is decided at the bench, on the day the samples are randomized.

The ComBat-seq study describes the negative binomial adjustment and its simulation results. Its count-preserving model and dispersion scenarios support the method explanation; this lesson’s discovery counts remain results of its own captured run.

Love, Huber, and Anders, 2014 describe the DESeq2 count model and its shrinkage of dispersion and fold change estimates.

The pasilla data package by Huber and Reyes holds the read counts of the Pasilla knock-down experiment by Brooks and colleagues, 2011.

The sva package by Leek and colleagues supplies surrogate variable analysis and ComBat batch correction.

The DESeq2 1.30.1 vignette explains count import, variance transformations, and designs with several factors. Compare its sample alignment and total-count filter with the import above; its treatment and batch terms provide the model context for these lessons.

The pasilla preparation vignette traces the counts to the Brooks experiment and describes the sample annotation. It explains why condition and library type are separate columns in this example.

The ComBat reference defines the batch and model-matrix arguments used to preserve condition. The ComBat_seq reference describes the count input and the biological group argument. These are different input scales and different adjustment models.