Correct for Batch Effects
batch_correct.RdThis function uses the removeBatchEffect method from the limma package to
adjust for known batch effects in the data.
Usage
batch_correct(object, batch_variable, bio_variables = NULL)
# S4 method for class 'SummarizedExperiment,character'
batch_correct(object, batch_variable, bio_variables = NULL)Arguments
- object
A
SummarizedExperimentobject. The first assay will be used.- batch_variable
A single character string specifying the column name in
colData(object)that contains the batch information.- bio_variables
An optional character vector of column names in
colData(object)that correspond to biological variables of interest. These effects will be preserved during the correction. IfNULL(the default), only an intercept is protected.
Value
A SummarizedExperiment object with a new assay named
"corrected" containing the batch-corrected data.
Examples
# --- Create a sample SummarizedExperiment object ---
set.seed(123)
counts <- matrix(rnorm(100 * 6, mean = 10, sd = 2), nrow = 100, ncol = 6)
colnames(counts) <- paste0("Sample", 1:6)
sample_info <- data.frame(
row.names = colnames(counts),
Condition = rep(c("A", "B"), each = 3),
Batch = rep(c("Batch1", "Batch2"), times = 3)
)
se <- SummarizedExperiment::SummarizedExperiment(assays = list(log_intensities = counts),
colData = sample_info)
# --- Run batch correction, preserving the "Condition" variable ---
corrected_se <- batch_correct(se,
batch_variable = "Batch",
bio_variables = "Condition")
#> Starting batch correction...
#> - Preserving biological variables: Condition
# --- Inspect the result ---
SummarizedExperiment::assayNames(corrected_se) # Now includes "corrected"
#> [1] "log_intensities"