R/covar_basic.R

Defines functions .covar_basic

# basic covariance formula for an R genotype matrix X, to checks the more
# elaborate .covar_BEDMatrix against this
.covar_basic <- function(X) {
    if (missing(X))
        stop("Genotype matrix `X` is required!")
    if (!is.matrix(X))
        stop("`X` must be a matrix!")

    # standard mean
    X_mean <- rowMeans(X, na.rm=TRUE)

    # center before cross product...
    X <- X - X_mean

    # before applying cross product, to prevent NA errors, just set those
    # values to zero and it works out!
    if (anyNA(X))
        X[is.na(X)] <- 0

    # cross product matrix is what we desire
    covar <- crossprod(X)

    return(covar)
}
StoreyLab/lfa documentation built on March 7, 2024, 9:59 p.m.