R/asso_mat.R

Defines functions asso_mat

Documented in asso_mat

#' @title asso_mat
#'
#' @description A function to estimate an association matrix. 
#'   This function also includes re-estimation for leave-one-out sample.
#'
#' @param OTUdat An OTU table with subjects in rows and taxa in columns.
#' @param group Indices of the subjects in a category of binary group variable.
#'
#' @return A list of an association matrix and reestimated association matrix
#'   is returned, which are estimated via SparCC.
#'
#' @examples
#' \donttest{
#' # In this example, the subset of the American Gut Project data will be used.
#' data(combinedamgut) # A complete data containing columns with taxa and clinical covariates.
#'
#' # Note: The line below will use a toy example with the first 30 out of 138 taxa.
#' OTUtab = combinedamgut[ , 8:37]
#' 
#' # Obtain indices of each grouping factor
#' # In this example, a variable indicating the status of living
#' # with a dog was chosen (i.e. bin_dog).
#' # Accordingly, Groups A and B imply living without and with a dog, respectively.
#' newindex_grpA = which(combinedamgut$bin_dog == 0)
#' newindex_grpB = which(combinedamgut$bin_dog == 1)
#'
#' # Now, we estimate (and re-estimate) association matrices 
#' # for each group separately.
#' asso_matA = asso_mat(OTUdat=OTUtab, group=newindex_grpA) 
#' asso_matB = asso_mat(OTUdat=OTUtab, group=newindex_grpB) 
#' 
#' }
#' @export
#' @import gtools
#' @import parallel



asso_mat <- function(OTUdat, group) {
        est_method = sparcc # estimate the association matrix/network via SparCC
        
        OTUtab = OTUdat[group, ]
        assomat = est_method(x = OTUtab)$cor.w
        # Re-estimation part
        reest.assomat = mclapply(group, function(j) sparcc(OTUtab[-j, ])$cor.w)
        
        return(list(assomat=assomat, reest.assomat=reest.assomat))
}


        

Try the SOHPIE package in your browser

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

SOHPIE documentation built on Oct. 24, 2023, 1:06 a.m.