
Detect batch effect
detect_batch_effect.Rd
Use ANOVA to detect if batch effect is present in the data.
If group
is provided, it will be used as a covariate in the ANOVA model.
Arguments
- x
Either a
glyexp_experiment
object or a matrix. If a matrix, rows should be variables and columns should be samples.- batch
Either a factor/character vector specifying batch assignments for each sample, or a string specifying the column name in sample_info (for experiment input only). Default to "batch" for experiment input.
- group
Either a factor/character vector specifying group assignments for each sample, or a string specifying the column name in sample_info (for experiment input only). If provided, it will be used as a covariate in the ANOVA model. This is useful when you have an unbalanced design. Default to NULL.
Value
A double vector of p-values for each variable,
i.e., the same length as nrow(x)
(for matrix) or nrow(get_expr_mat(x))
(for experiment)
Examples
# With glyexp_experiment and column names
exp <- glyexp::toy_experiment()
exp$sample_info$batch <- c("A", "A", "A", "B", "B", "B")
exp$sample_info$group <- c("Ctrl", "Ctrl", "Treat", "Ctrl", "Treat", "Treat")
p_values <- detect_batch_effect(exp, batch = "batch", group = "group")
#> ℹ Detecting batch effects using ANOVA for 4 variables...
#> ✔ Batch effect detection completed. 4 out of 4 variables show significant batch effects (p < 0.05).
# With matrix and factor vectors
mat <- matrix(rnorm(200), nrow = 20, ncol = 10)
batch_factor <- factor(rep(c("A", "B"), each = 5))
group_factor <- factor(rep(c("Ctrl", "Treat"), times = 5))
p_values <- detect_batch_effect(mat, batch = batch_factor, group = group_factor)
#> ℹ Detecting batch effects using ANOVA for 20 variables...
#> ✔ Batch effect detection completed. 0 out of 20 variables show significant batch effects (p < 0.05).