Regresses each feature (row) of x on the covariate design and
returns the residuals, using a precomputed projection for memory-
efficient chunked processing of large (e.g. 450k-probe) matrices.
This is the workhorse for removing cell composition, batch, and
technical factors before extracting negative-control PCs.
Arguments
- x
Numeric matrix, features x samples.
- covariates
data.frame or matrix with one row per sample (i.e.
nrow(covariates) == ncol(x)). An intercept is added internally. Factors should already be expanded to dummies (seestats::model.matrix()).- chunk_size
Integer: number of features processed per block. Default 5000.
Examples
x <- matrix(rnorm(100 * 40), nrow = 100,
dimnames = list(paste0("f", 1:100), paste0("S", 1:40)))
batch <- factor(rep(c("A", "B"), each = 20))
cv <- model.matrix(~ batch)[, -1, drop = FALSE]
xr <- residualize_matrix(x, cv)
cor(as.numeric(xr[1, ]), as.numeric(cv))
#> [1] 2.89022e-17
