R/pca.h.R

Defines functions pca

Documented in pca

# This file is automatically generated, you probably don't want to edit this

pcaOptions <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "pcaOptions",
    inherit = jmvcore::Options,
    public = list(
        initialize = function(
            mode = "simple",
            labels = NULL,
            vars = NULL,
            eigen = FALSE,
            plot = FALSE,
            plot1 = FALSE,
            plot2 = FALSE,
            vars1 = NULL,
            facs = NULL,
            plot3 = FALSE,
            plot4 = FALSE,
            width = 500,
            height = 500,
            width1 = 500,
            height1 = 500,
            width2 = 500,
            height2 = 500,
            width3 = 500,
            height3 = 500,
            width4 = 500,
            height4 = 500, ...) {

            super$initialize(
                package="snowCluster",
                name="pca",
                requiresData=TRUE,
                ...)

            private$..mode <- jmvcore::OptionList$new(
                "mode",
                mode,
                options=list(
                    "simple",
                    "complex"),
                default="simple")
            private$..labels <- jmvcore::OptionVariable$new(
                "labels",
                labels,
                suggested=list(
                    "nominal"),
                permitted=list(
                    "id",
                    "factor"))
            private$..vars <- jmvcore::OptionVariables$new(
                "vars",
                vars,
                suggested=list(
                    "continuous"),
                permitted=list(
                    "numeric"))
            private$..eigen <- jmvcore::OptionBool$new(
                "eigen",
                eigen,
                default=FALSE)
            private$..plot <- jmvcore::OptionBool$new(
                "plot",
                plot,
                default=FALSE)
            private$..plot1 <- jmvcore::OptionBool$new(
                "plot1",
                plot1,
                default=FALSE)
            private$..plot2 <- jmvcore::OptionBool$new(
                "plot2",
                plot2,
                default=FALSE)
            private$..vars1 <- jmvcore::OptionVariables$new(
                "vars1",
                vars1,
                suggested=list(
                    "continuous"),
                permitted=list(
                    "numeric"))
            private$..facs <- jmvcore::OptionVariable$new(
                "facs",
                facs,
                suggested=list(
                    "nominal"),
                permitted=list(
                    "factor"))
            private$..plot3 <- jmvcore::OptionBool$new(
                "plot3",
                plot3,
                default=FALSE)
            private$..plot4 <- jmvcore::OptionBool$new(
                "plot4",
                plot4,
                default=FALSE)
            private$..width <- jmvcore::OptionInteger$new(
                "width",
                width,
                default=500)
            private$..height <- jmvcore::OptionInteger$new(
                "height",
                height,
                default=500)
            private$..width1 <- jmvcore::OptionInteger$new(
                "width1",
                width1,
                default=500)
            private$..height1 <- jmvcore::OptionInteger$new(
                "height1",
                height1,
                default=500)
            private$..width2 <- jmvcore::OptionInteger$new(
                "width2",
                width2,
                default=500)
            private$..height2 <- jmvcore::OptionInteger$new(
                "height2",
                height2,
                default=500)
            private$..width3 <- jmvcore::OptionInteger$new(
                "width3",
                width3,
                default=500)
            private$..height3 <- jmvcore::OptionInteger$new(
                "height3",
                height3,
                default=500)
            private$..width4 <- jmvcore::OptionInteger$new(
                "width4",
                width4,
                default=500)
            private$..height4 <- jmvcore::OptionInteger$new(
                "height4",
                height4,
                default=500)

            self$.addOption(private$..mode)
            self$.addOption(private$..labels)
            self$.addOption(private$..vars)
            self$.addOption(private$..eigen)
            self$.addOption(private$..plot)
            self$.addOption(private$..plot1)
            self$.addOption(private$..plot2)
            self$.addOption(private$..vars1)
            self$.addOption(private$..facs)
            self$.addOption(private$..plot3)
            self$.addOption(private$..plot4)
            self$.addOption(private$..width)
            self$.addOption(private$..height)
            self$.addOption(private$..width1)
            self$.addOption(private$..height1)
            self$.addOption(private$..width2)
            self$.addOption(private$..height2)
            self$.addOption(private$..width3)
            self$.addOption(private$..height3)
            self$.addOption(private$..width4)
            self$.addOption(private$..height4)
        }),
    active = list(
        mode = function() private$..mode$value,
        labels = function() private$..labels$value,
        vars = function() private$..vars$value,
        eigen = function() private$..eigen$value,
        plot = function() private$..plot$value,
        plot1 = function() private$..plot1$value,
        plot2 = function() private$..plot2$value,
        vars1 = function() private$..vars1$value,
        facs = function() private$..facs$value,
        plot3 = function() private$..plot3$value,
        plot4 = function() private$..plot4$value,
        width = function() private$..width$value,
        height = function() private$..height$value,
        width1 = function() private$..width1$value,
        height1 = function() private$..height1$value,
        width2 = function() private$..width2$value,
        height2 = function() private$..height2$value,
        width3 = function() private$..width3$value,
        height3 = function() private$..height3$value,
        width4 = function() private$..width4$value,
        height4 = function() private$..height4$value),
    private = list(
        ..mode = NA,
        ..labels = NA,
        ..vars = NA,
        ..eigen = NA,
        ..plot = NA,
        ..plot1 = NA,
        ..plot2 = NA,
        ..vars1 = NA,
        ..facs = NA,
        ..plot3 = NA,
        ..plot4 = NA,
        ..width = NA,
        ..height = NA,
        ..width1 = NA,
        ..height1 = NA,
        ..width2 = NA,
        ..height2 = NA,
        ..width3 = NA,
        ..height3 = NA,
        ..width4 = NA,
        ..height4 = NA)
)

pcaResults <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "pcaResults",
    inherit = jmvcore::Group,
    active = list(
        instructions = function() private$.items[["instructions"]],
        eigen = function() private$.items[["eigen"]],
        plot = function() private$.items[["plot"]],
        plot1 = function() private$.items[["plot1"]],
        plot2 = function() private$.items[["plot2"]],
        plot3 = function() private$.items[["plot3"]],
        plot4 = function() private$.items[["plot4"]]),
    private = list(),
    public=list(
        initialize=function(options) {
            super$initialize(
                options=options,
                name="",
                title="PCA & Group plot",
                refs="snowCluster")
            self$add(jmvcore::Html$new(
                options=options,
                name="instructions",
                title="Instructions",
                visible=TRUE))
            self$add(jmvcore::Table$new(
                options=options,
                name="eigen",
                title="Eigenvalues",
                visible="(eigen)",
                clearWith=list(
                    "mode",
                    "vars",
                    "labels"),
                columns=list(
                    list(
                        `name`="comp", 
                        `title`="Component", 
                        `type`="text"),
                    list(
                        `name`="eigen", 
                        `title`="Eigenvalue", 
                        `type`="number"),
                    list(
                        `name`="varProp", 
                        `title`="% of Variance", 
                        `type`="number"),
                    list(
                        `name`="varCum", 
                        `title`="Cumulative %", 
                        `type`="number"))))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot",
                title="Variable Contributions",
                requiresData=TRUE,
                refs="factoextra",
                visible="(plot)",
                renderFun=".plot",
                clearWith=list(
                    "mode",
                    "vars",
                    "labels",
                    "width",
                    "height")))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot1",
                title="Individual Plot",
                requiresData=TRUE,
                refs="factoextra",
                visible="(plot1)",
                renderFun=".plot1",
                clearWith=list(
                    "mode",
                    "vars",
                    "labels",
                    "width1",
                    "height1")))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot2",
                title="Biplot",
                requiresData=TRUE,
                refs="factoextra",
                visible="(plot2)",
                renderFun=".plot2",
                clearWith=list(
                    "mode",
                    "vars",
                    "labels",
                    "width2",
                    "height2")))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot3",
                title="Individuals by groups",
                requiresData=TRUE,
                refs="factoextra",
                visible="(plot3)",
                renderFun=".plot3",
                clearWith=list(
                    "mode",
                    "vars1",
                    "labels",
                    "width3",
                    "height3")))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot4",
                title="PCA-Biplot",
                requiresData=TRUE,
                refs="factoextra",
                visible="(plot4)",
                renderFun=".plot4",
                clearWith=list(
                    "mode",
                    "vars1",
                    "labels",
                    "width4",
                    "height4")))}))

pcaBase <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "pcaBase",
    inherit = jmvcore::Analysis,
    public = list(
        initialize = function(options, data=NULL, datasetId="", analysisId="", revision=0) {
            super$initialize(
                package = "snowCluster",
                name = "pca",
                version = c(1,0,0),
                options = options,
                results = pcaResults$new(options=options),
                data = data,
                datasetId = datasetId,
                analysisId = analysisId,
                revision = revision,
                pause = NULL,
                completeWhenFilled = FALSE,
                requiresMissings = FALSE,
                weightsSupport = 'auto')
        }))

#' PCA & Group plot
#'
#' 
#' @param data The data as a data frame.
#' @param mode .
#' @param labels .
#' @param vars .
#' @param eigen .
#' @param plot .
#' @param plot1 .
#' @param plot2 .
#' @param vars1 .
#' @param facs .
#' @param plot3 .
#' @param plot4 .
#' @param width .
#' @param height .
#' @param width1 .
#' @param height1 .
#' @param width2 .
#' @param height2 .
#' @param width3 .
#' @param height3 .
#' @param width4 .
#' @param height4 .
#' @return A results object containing:
#' \tabular{llllll}{
#'   \code{results$instructions} \tab \tab \tab \tab \tab a html \cr
#'   \code{results$eigen} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$plot} \tab \tab \tab \tab \tab an image \cr
#'   \code{results$plot1} \tab \tab \tab \tab \tab an image \cr
#'   \code{results$plot2} \tab \tab \tab \tab \tab an image \cr
#'   \code{results$plot3} \tab \tab \tab \tab \tab an image \cr
#'   \code{results$plot4} \tab \tab \tab \tab \tab an image \cr
#' }
#'
#' Tables can be converted to data frames with \code{asDF} or \code{\link{as.data.frame}}. For example:
#'
#' \code{results$eigen$asDF}
#'
#' \code{as.data.frame(results$eigen)}
#'
#' @export
pca <- function(
    data,
    mode = "simple",
    labels,
    vars,
    eigen = FALSE,
    plot = FALSE,
    plot1 = FALSE,
    plot2 = FALSE,
    vars1,
    facs,
    plot3 = FALSE,
    plot4 = FALSE,
    width = 500,
    height = 500,
    width1 = 500,
    height1 = 500,
    width2 = 500,
    height2 = 500,
    width3 = 500,
    height3 = 500,
    width4 = 500,
    height4 = 500) {

    if ( ! requireNamespace("jmvcore", quietly=TRUE))
        stop("pca requires jmvcore to be installed (restart may be required)")

    if ( ! missing(labels)) labels <- jmvcore::resolveQuo(jmvcore::enquo(labels))
    if ( ! missing(vars)) vars <- jmvcore::resolveQuo(jmvcore::enquo(vars))
    if ( ! missing(vars1)) vars1 <- jmvcore::resolveQuo(jmvcore::enquo(vars1))
    if ( ! missing(facs)) facs <- jmvcore::resolveQuo(jmvcore::enquo(facs))
    if (missing(data))
        data <- jmvcore::marshalData(
            parent.frame(),
            `if`( ! missing(labels), labels, NULL),
            `if`( ! missing(vars), vars, NULL),
            `if`( ! missing(vars1), vars1, NULL),
            `if`( ! missing(facs), facs, NULL))

    for (v in facs) if (v %in% names(data)) data[[v]] <- as.factor(data[[v]])

    options <- pcaOptions$new(
        mode = mode,
        labels = labels,
        vars = vars,
        eigen = eigen,
        plot = plot,
        plot1 = plot1,
        plot2 = plot2,
        vars1 = vars1,
        facs = facs,
        plot3 = plot3,
        plot4 = plot4,
        width = width,
        height = height,
        width1 = width1,
        height1 = height1,
        width2 = width2,
        height2 = height2,
        width3 = width3,
        height3 = height3,
        width4 = width4,
        height4 = height4)

    analysis <- pcaClass$new(
        options = options,
        data = data)

    analysis$run()

    analysis$results
}
hyunsooseol/snowCluster documentation built on April 5, 2025, 2:06 a.m.