R/detectClusters.R

Defines functions detectClusters

Documented in detectClusters

#' Function to estimate the number of clusters in IC coefficients or PC projections.
#'
#' Performing ICA on a dataset and create a list object with results.
#' @param input_list input_list ICA or PCA object generated by \code{run_ica()} or \code{runPCA()}.
#' @return Running \code{detectClusters()} will generate a copy of the input_list with additional entries \code{n_clust,mclust_result}.
#'         \code{n_clust} will contain the number of estimated clusters for each component, and \code{mclust_result} is a list with
#'         each result of \code{mclust} saved for all components.
#'
#' @import mclust
#'
#' @examples
#'
#' data(expr_data)
#'
#' pca_result <- runPCA(expr_data)
#' pca_result <- detectClusters(pca_result)
#'
#' @export
detectClusters <- function(input_list = NULL){

    if(is.null(input_list)){
        stop("Input is missing, please specify an ICA or PCA object")
    }


    if(class(input_list) == "ICAobject"){
        comp_coeff_mx <- input_list$A
    } else if (class(input_list) == "PCAobject"){
        comp_coeff_mx <- t(input_list$x)
    }

    # Turn off warnings to prevent unneccessary panic
    options(warn=-1)
    mclust_result <- apply(comp_coeff_mx, 1, function(x) Mclust(x))
    # Turn warnings back on to prevent disasters
    options(warn=0)


    input_list$n_clust <- unlist(lapply(mclust_result, function(x) x$G))
    input_list$mclust_result <- mclust_result

    attr(input_list, 'clustering') <- "yes"

    return(input_list)
}
jinhyunju/picaplot documentation built on May 19, 2019, 10:35 a.m.