R/removeBatch.R

Defines functions remove_batch

remove_batch <- function(X, batch) {
    batch <- as.factor(batch)

    # Design matrix
    H <- model.matrix(~batch)

    # Solve coefficients: beta = (H'H)^(-1) H' X'
    HtH_inv <- solve(crossprod(H))
    HtX <- crossprod(H, t(X))
    beta <- HtH_inv %*% HtX

    # Correction: H * beta, then subtract
    X <- t(t(X) - H %*% beta)

    # Clear memory
    gc()
    return(X)
}

Try the SCORPION package in your browser

Any scripts or data that you put into this service are public.

SCORPION documentation built on Feb. 5, 2026, 1:06 a.m.