R/mca.h.R

Defines functions mca

Documented in mca

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

mcaOptions <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "mcaOptions",
    inherit = jmvcore::Options,
    public = list(
        initialize = function(
            facs = NULL,
            vars = NULL,
            nd = 2,
            eigen = TRUE,
            rowvar = "coordinates",
            loadingind = FALSE,
            colvar = "coordinates",
            loadingvar = FALSE,
            plot1 = FALSE,
            plot2 = FALSE,
            plot3 = FALSE,
            plot4 = FALSE,
            plot5 = TRUE,
            plot6 = FALSE,
            width1 = 500,
            height1 = 500,
            width2 = 500,
            height2 = 500,
            width3 = 500,
            height3 = 500,
            width4 = 500,
            height4 = 500,
            width5 = 500,
            height5 = 500,
            width6 = 500,
            height6 = 500, ...) {

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

            private$..facs <- jmvcore::OptionVariable$new(
                "facs",
                facs,
                suggested=list(
                    "nominal"),
                permitted=list(
                    "factor"))
            private$..vars <- jmvcore::OptionVariables$new(
                "vars",
                vars,
                suggested=list(
                    "nominal",
                    "ordinal"),
                permitted=list(
                    "factor"))
            private$..nd <- jmvcore::OptionInteger$new(
                "nd",
                nd,
                default=2,
                min=2)
            private$..eigen <- jmvcore::OptionBool$new(
                "eigen",
                eigen,
                default=TRUE)
            private$..rowvar <- jmvcore::OptionList$new(
                "rowvar",
                rowvar,
                options=list(
                    "coordinates",
                    "cos2",
                    "contribution"),
                default="coordinates")
            private$..loadingind <- jmvcore::OptionBool$new(
                "loadingind",
                loadingind,
                default=FALSE)
            private$..colvar <- jmvcore::OptionList$new(
                "colvar",
                colvar,
                options=list(
                    "coordinates",
                    "cos2",
                    "contribution"),
                default="coordinates")
            private$..loadingvar <- jmvcore::OptionBool$new(
                "loadingvar",
                loadingvar,
                default=FALSE)
            private$..plot1 <- jmvcore::OptionBool$new(
                "plot1",
                plot1,
                default=FALSE)
            private$..plot2 <- jmvcore::OptionBool$new(
                "plot2",
                plot2,
                default=FALSE)
            private$..plot3 <- jmvcore::OptionBool$new(
                "plot3",
                plot3,
                default=FALSE)
            private$..plot4 <- jmvcore::OptionBool$new(
                "plot4",
                plot4,
                default=FALSE)
            private$..plot5 <- jmvcore::OptionBool$new(
                "plot5",
                plot5,
                default=TRUE)
            private$..plot6 <- jmvcore::OptionBool$new(
                "plot6",
                plot6,
                default=FALSE)
            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)
            private$..width5 <- jmvcore::OptionInteger$new(
                "width5",
                width5,
                default=500)
            private$..height5 <- jmvcore::OptionInteger$new(
                "height5",
                height5,
                default=500)
            private$..width6 <- jmvcore::OptionInteger$new(
                "width6",
                width6,
                default=500)
            private$..height6 <- jmvcore::OptionInteger$new(
                "height6",
                height6,
                default=500)

            self$.addOption(private$..facs)
            self$.addOption(private$..vars)
            self$.addOption(private$..nd)
            self$.addOption(private$..eigen)
            self$.addOption(private$..rowvar)
            self$.addOption(private$..loadingind)
            self$.addOption(private$..colvar)
            self$.addOption(private$..loadingvar)
            self$.addOption(private$..plot1)
            self$.addOption(private$..plot2)
            self$.addOption(private$..plot3)
            self$.addOption(private$..plot4)
            self$.addOption(private$..plot5)
            self$.addOption(private$..plot6)
            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)
            self$.addOption(private$..width5)
            self$.addOption(private$..height5)
            self$.addOption(private$..width6)
            self$.addOption(private$..height6)
        }),
    active = list(
        facs = function() private$..facs$value,
        vars = function() private$..vars$value,
        nd = function() private$..nd$value,
        eigen = function() private$..eigen$value,
        rowvar = function() private$..rowvar$value,
        loadingind = function() private$..loadingind$value,
        colvar = function() private$..colvar$value,
        loadingvar = function() private$..loadingvar$value,
        plot1 = function() private$..plot1$value,
        plot2 = function() private$..plot2$value,
        plot3 = function() private$..plot3$value,
        plot4 = function() private$..plot4$value,
        plot5 = function() private$..plot5$value,
        plot6 = function() private$..plot6$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,
        width5 = function() private$..width5$value,
        height5 = function() private$..height5$value,
        width6 = function() private$..width6$value,
        height6 = function() private$..height6$value),
    private = list(
        ..facs = NA,
        ..vars = NA,
        ..nd = NA,
        ..eigen = NA,
        ..rowvar = NA,
        ..loadingind = NA,
        ..colvar = NA,
        ..loadingvar = NA,
        ..plot1 = NA,
        ..plot2 = NA,
        ..plot3 = NA,
        ..plot4 = NA,
        ..plot5 = NA,
        ..plot6 = NA,
        ..width1 = NA,
        ..height1 = NA,
        ..width2 = NA,
        ..height2 = NA,
        ..width3 = NA,
        ..height3 = NA,
        ..width4 = NA,
        ..height4 = NA,
        ..width5 = NA,
        ..height5 = NA,
        ..width6 = NA,
        ..height6 = NA)
)

mcaResults <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "mcaResults",
    inherit = jmvcore::Group,
    active = list(
        instructions = function() private$.items[["instructions"]],
        eigen = function() private$.items[["eigen"]],
        loadingvar = function() private$.items[["loadingvar"]],
        loadingind = function() private$.items[["loadingind"]],
        plot5 = function() private$.items[["plot5"]],
        plot6 = function() private$.items[["plot6"]],
        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="Multiple Correspondence Analysis",
                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(
                    "vars",
                    "facs",
                    "rowvar",
                    "colvar",
                    "nd"),
                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::Table$new(
                options=options,
                name="loadingvar",
                title="`Variable categories across dimensions - ${colvar}`",
                visible="(loadingvar)",
                clearWith=list(
                    "vars",
                    "facs",
                    "rowvar",
                    "colvar",
                    "nd"),
                columns=list(
                    list(
                        `name`="name", 
                        `title`="", 
                        `type`="text", 
                        `content`="($key)"),
                    list(
                        `name`="pc1", 
                        `title`="1", 
                        `type`="number", 
                        `superTitle`="Dimension"))))
            self$add(jmvcore::Table$new(
                options=options,
                name="loadingind",
                title="`Individuals across dimensions - ${rowvar}`",
                visible="(loadingind)",
                clearWith=list(
                    "vars",
                    "facs",
                    "rowvar",
                    "colvar",
                    "nd"),
                columns=list(
                    list(
                        `name`="name", 
                        `title`="", 
                        `type`="text", 
                        `content`="($key)"),
                    list(
                        `name`="pc1", 
                        `title`="1", 
                        `type`="number", 
                        `superTitle`="Dimension"))))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot5",
                title="Scree plot",
                requiresData=TRUE,
                refs="factoextra",
                visible="(plot5)",
                renderFun=".plot5",
                clearWith=list(
                    "vars",
                    "facs",
                    "rowvar",
                    "colvar",
                    "width5",
                    "height5",
                    "nd")))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot6",
                title="Biplot",
                refs="factoextra",
                visible="(plot6)",
                renderFun=".plot6",
                clearWith=list(
                    "vars",
                    "facs",
                    "rowvar",
                    "colvar",
                    "width6",
                    "height6",
                    "nd")))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot1",
                title="Correlation between variables and dimensions",
                requiresData=TRUE,
                refs="factoextra",
                visible="(plot1)",
                renderFun=".plot1",
                clearWith=list(
                    "vars",
                    "facs",
                    "rowvar",
                    "colvar",
                    "width1",
                    "height1",
                    "nd")))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot2",
                title="Coordinates of variable categories",
                requiresData=TRUE,
                refs="factoextra",
                visible="(plot2)",
                renderFun=".plot2",
                clearWith=list(
                    "vars",
                    "facs",
                    "rowvar",
                    "colvar",
                    "width2",
                    "height2",
                    "nd")))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot3",
                title="Plot of individuals",
                requiresData=TRUE,
                refs="factoextra",
                visible="(plot3)",
                renderFun=".plot3",
                clearWith=list(
                    "vars",
                    "facs",
                    "rowvar",
                    "colvar",
                    "width3",
                    "height3",
                    "nd")))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot4",
                title="Individuals by groups",
                requiresData=TRUE,
                refs="factoextra",
                visible="(plot4)",
                renderFun=".plot4",
                clearWith=list(
                    "vars",
                    "facs",
                    "rowvar",
                    "colvar",
                    "width4",
                    "height4",
                    "nd")))}))

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

#' Multiple Correspondence Analysis
#'
#' 
#' @param data The data as a data frame.
#' @param facs .
#' @param vars .
#' @param nd .
#' @param eigen .
#' @param rowvar .
#' @param loadingind .
#' @param colvar .
#' @param loadingvar .
#' @param plot1 .
#' @param plot2 .
#' @param plot3 .
#' @param plot4 .
#' @param plot5 .
#' @param plot6 .
#' @param width1 .
#' @param height1 .
#' @param width2 .
#' @param height2 .
#' @param width3 .
#' @param height3 .
#' @param width4 .
#' @param height4 .
#' @param width5 .
#' @param height5 .
#' @param width6 .
#' @param height6 .
#' @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$loadingvar} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$loadingind} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$plot5} \tab \tab \tab \tab \tab an image \cr
#'   \code{results$plot6} \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
mca <- function(
    data,
    facs,
    vars,
    nd = 2,
    eigen = TRUE,
    rowvar = "coordinates",
    loadingind = FALSE,
    colvar = "coordinates",
    loadingvar = FALSE,
    plot1 = FALSE,
    plot2 = FALSE,
    plot3 = FALSE,
    plot4 = FALSE,
    plot5 = TRUE,
    plot6 = FALSE,
    width1 = 500,
    height1 = 500,
    width2 = 500,
    height2 = 500,
    width3 = 500,
    height3 = 500,
    width4 = 500,
    height4 = 500,
    width5 = 500,
    height5 = 500,
    width6 = 500,
    height6 = 500) {

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

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

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

    options <- mcaOptions$new(
        facs = facs,
        vars = vars,
        nd = nd,
        eigen = eigen,
        rowvar = rowvar,
        loadingind = loadingind,
        colvar = colvar,
        loadingvar = loadingvar,
        plot1 = plot1,
        plot2 = plot2,
        plot3 = plot3,
        plot4 = plot4,
        plot5 = plot5,
        plot6 = plot6,
        width1 = width1,
        height1 = height1,
        width2 = width2,
        height2 = height2,
        width3 = width3,
        height3 = height3,
        width4 = width4,
        height4 = height4,
        width5 = width5,
        height5 = height5,
        width6 = width6,
        height6 = height6)

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

    analysis$run()

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