R/trans_Laplace.R

Defines functions trans_Laplace

Documented in trans_Laplace

#' Get Laplace matrix from factor vector
#'
#' @param col_data A factor vector
#' @return \code{adjacency} The Laplace matrix of \code{col_data}
#' @export
#' @author Haidong Yi, Ayush T. Raman
#'
#' @examples
#' batch.factor <- c(rep('human',13),rep('mouse',13))
#' batch.factor <- as.factor(batch.factor)
#' adj <- trans_Laplace(batch.factor)
#'

trans_Laplace <- function(col_data) {
    adjacency <- matrix(0, nrow = length(col_data), ncol = length(col_data))
    iter <- length(levels(col_data))
    for (i in 1:iter) {
        FACTOR <- which(col_data == levels(col_data)[i])
        N <- length(FACTOR)
        for (j in 1:(N - 1)) {
            for (k in (j + 1):N) {
                adjacency[FACTOR[j], FACTOR[k]] <- 1
            }
        }
    }
    adjacency <- adjacency + t(adjacency)
    adjacency <- diag(rowSums(adjacency)) - adjacency
    return(adjacency)
}

Try the DASC package in your browser

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

DASC documentation built on Nov. 17, 2017, 9:36 a.m.