R/disc.h.R

Defines functions disc

Documented in disc

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

discOptions <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "discOptions",
    inherit = jmvcore::Options,
    public = list(
        initialize = function(
            dep = NULL,
            covs = NULL,
            per = 1,
            prior = TRUE,
            gm = FALSE,
            coef = FALSE,
            nl = FALSE,
            prop = FALSE,
            tra = FALSE,
            tes = FALSE,
            gc = TRUE,
            plot = FALSE,
            plot1 = FALSE,
            width = 500,
            height = 500,
            width1 = 500,
            height1 = 500, ...) {

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

            private$..dep <- jmvcore::OptionVariable$new(
                "dep",
                dep,
                suggested=list(
                    "nominal"),
                permitted=list(
                    "factor"))
            private$..covs <- jmvcore::OptionVariables$new(
                "covs",
                covs,
                suggested=list(
                    "continuous"),
                permitted=list(
                    "numeric"))
            private$..per <- jmvcore::OptionNumber$new(
                "per",
                per,
                min=0.1,
                max=1,
                default=1)
            private$..prior <- jmvcore::OptionBool$new(
                "prior",
                prior,
                default=TRUE)
            private$..gm <- jmvcore::OptionBool$new(
                "gm",
                gm,
                default=FALSE)
            private$..coef <- jmvcore::OptionBool$new(
                "coef",
                coef,
                default=FALSE)
            private$..nl <- jmvcore::OptionBool$new(
                "nl",
                nl,
                default=FALSE)
            private$..prop <- jmvcore::OptionBool$new(
                "prop",
                prop,
                default=FALSE)
            private$..tra <- jmvcore::OptionBool$new(
                "tra",
                tra,
                default=FALSE)
            private$..tes <- jmvcore::OptionBool$new(
                "tes",
                tes,
                default=FALSE)
            private$..gc <- jmvcore::OptionBool$new(
                "gc",
                gc,
                default=TRUE)
            private$..plot <- jmvcore::OptionBool$new(
                "plot",
                plot,
                default=FALSE)
            private$..plot1 <- jmvcore::OptionBool$new(
                "plot1",
                plot1,
                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)

            self$.addOption(private$..dep)
            self$.addOption(private$..covs)
            self$.addOption(private$..per)
            self$.addOption(private$..prior)
            self$.addOption(private$..gm)
            self$.addOption(private$..coef)
            self$.addOption(private$..nl)
            self$.addOption(private$..prop)
            self$.addOption(private$..tra)
            self$.addOption(private$..tes)
            self$.addOption(private$..gc)
            self$.addOption(private$..plot)
            self$.addOption(private$..plot1)
            self$.addOption(private$..width)
            self$.addOption(private$..height)
            self$.addOption(private$..width1)
            self$.addOption(private$..height1)
        }),
    active = list(
        dep = function() private$..dep$value,
        covs = function() private$..covs$value,
        per = function() private$..per$value,
        prior = function() private$..prior$value,
        gm = function() private$..gm$value,
        coef = function() private$..coef$value,
        nl = function() private$..nl$value,
        prop = function() private$..prop$value,
        tra = function() private$..tra$value,
        tes = function() private$..tes$value,
        gc = function() private$..gc$value,
        plot = function() private$..plot$value,
        plot1 = function() private$..plot1$value,
        width = function() private$..width$value,
        height = function() private$..height$value,
        width1 = function() private$..width1$value,
        height1 = function() private$..height1$value),
    private = list(
        ..dep = NA,
        ..covs = NA,
        ..per = NA,
        ..prior = NA,
        ..gm = NA,
        ..coef = NA,
        ..nl = NA,
        ..prop = NA,
        ..tra = NA,
        ..tes = NA,
        ..gc = NA,
        ..plot = NA,
        ..plot1 = NA,
        ..width = NA,
        ..height = NA,
        ..width1 = NA,
        ..height1 = NA)
)

discResults <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "discResults",
    inherit = jmvcore::Group,
    active = list(
        instructions = function() private$.items[["instructions"]],
        text = function() private$.items[["text"]],
        prior = function() private$.items[["prior"]],
        gm = function() private$.items[["gm"]],
        coef = function() private$.items[["coef"]],
        nl = function() private$.items[["nl"]],
        prop = function() private$.items[["prop"]],
        tra = function() private$.items[["tra"]],
        tes = function() private$.items[["tes"]],
        plot = function() private$.items[["plot"]],
        gc = function() private$.items[["gc"]],
        plot1 = function() private$.items[["plot1"]]),
    private = list(),
    public=list(
        initialize=function(options) {
            super$initialize(
                options=options,
                name="",
                title="Linear Discriminant Analysis",
                refs="snowCluster")
            self$add(jmvcore::Html$new(
                options=options,
                name="instructions",
                title="Instructions",
                visible=TRUE))
            self$add(jmvcore::Preformatted$new(
                options=options,
                name="text",
                title=" "))
            self$add(jmvcore::Table$new(
                options=options,
                name="prior",
                title="Prior probability of groups",
                visible="(prior)",
                clearWith=list(
                    "covs",
                    "dep",
                    "per"),
                columns=list(
                    list(
                        `name`="name", 
                        `title`="", 
                        `type`="text", 
                        `content`="($key)"),
                    list(
                        `name`="value", 
                        `title`="Value"))))
            self$add(jmvcore::Table$new(
                options=options,
                name="gm",
                title="Group means",
                visible="(gm)",
                clearWith=list(
                    "covs",
                    "dep",
                    "per"),
                columns=list(
                    list(
                        `name`="name", 
                        `title`="", 
                        `type`="text", 
                        `content`="($key)"))))
            self$add(jmvcore::Table$new(
                options=options,
                name="coef",
                title="Coefficients of linear discriminants",
                refs="MASS",
                visible="(coef)",
                clearWith=list(
                    "covs",
                    "dep",
                    "per"),
                columns=list(
                    list(
                        `name`="name", 
                        `title`="", 
                        `type`="text", 
                        `content`="($key)"))))
            self$add(jmvcore::Table$new(
                options=options,
                name="nl",
                title="Normalized loadings",
                refs="MASS",
                visible="(nl)",
                clearWith=list(
                    "covs",
                    "dep",
                    "per"),
                columns=list(
                    list(
                        `name`="name", 
                        `title`="", 
                        `type`="text", 
                        `content`="($key)"))))
            self$add(jmvcore::Table$new(
                options=options,
                name="prop",
                title="Proportion of trace",
                visible="(prop)",
                rows=1,
                clearWith=list(
                    "covs",
                    "dep",
                    "per"),
                columns=list(
                    list(
                        `name`="name", 
                        `title`="", 
                        `type`="text", 
                        `content`="Proportion(%)"),
                    list(
                        `name`="LD1", 
                        `type`="number"),
                    list(
                        `name`="LD2", 
                        `type`="number"))))
            self$add(jmvcore::Table$new(
                options=options,
                name="tra",
                title="Confusion matrix with training set",
                visible="(tra)",
                clearWith=list(
                    "covs",
                    "dep",
                    "per"),
                columns=list(
                    list(
                        `name`="name", 
                        `title`="", 
                        `type`="text", 
                        `content`="($key)"))))
            self$add(jmvcore::Table$new(
                options=options,
                name="tes",
                title="Confusion matrix with test set",
                visible="(tes)",
                clearWith=list(
                    "covs",
                    "dep",
                    "per"),
                columns=list(
                    list(
                        `name`="name", 
                        `title`="", 
                        `type`="text", 
                        `content`="($key)"))))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot",
                title="Linear discriminant plot",
                requiresData=TRUE,
                visible="(plot)",
                renderFun=".plot",
                clearWith=list(
                    "covs",
                    "dep",
                    "per",
                    "width",
                    "height")))
            self$add(jmvcore::Table$new(
                options=options,
                name="gc",
                title="Group centroids",
                refs="MASS",
                visible="(gc)",
                clearWith=list(
                    "covs",
                    "dep",
                    "per"),
                columns=list(
                    list(
                        `name`="name", 
                        `title`="Groups", 
                        `type`="text", 
                        `content`="($key)"),
                    list(
                        `name`="ld1", 
                        `title`="LD1", 
                        `type`="number"),
                    list(
                        `name`="ld2", 
                        `title`="LD2", 
                        `type`="number"))))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot1",
                title="Histogram",
                requiresData=TRUE,
                visible="(plot1)",
                renderFun=".plot1",
                clearWith=list(
                    "covs",
                    "dep",
                    "per",
                    "width1",
                    "height1")))}))

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

#' Linear Discriminant Analysis
#'
#' 
#' @param data .
#' @param dep .
#' @param covs .
#' @param per .
#' @param prior .
#' @param gm .
#' @param coef .
#' @param nl .
#' @param prop .
#' @param tra .
#' @param tes .
#' @param gc .
#' @param plot .
#' @param plot1 .
#' @param width .
#' @param height .
#' @param width1 .
#' @param height1 .
#' @return A results object containing:
#' \tabular{llllll}{
#'   \code{results$instructions} \tab \tab \tab \tab \tab a html \cr
#'   \code{results$text} \tab \tab \tab \tab \tab a preformatted \cr
#'   \code{results$prior} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$gm} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$coef} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$nl} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$prop} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$tra} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$tes} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$plot} \tab \tab \tab \tab \tab an image \cr
#'   \code{results$gc} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$plot1} \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$prior$asDF}
#'
#' \code{as.data.frame(results$prior)}
#'
#' @export
disc <- function(
    data,
    dep,
    covs,
    per = 1,
    prior = TRUE,
    gm = FALSE,
    coef = FALSE,
    nl = FALSE,
    prop = FALSE,
    tra = FALSE,
    tes = FALSE,
    gc = TRUE,
    plot = FALSE,
    plot1 = FALSE,
    width = 500,
    height = 500,
    width1 = 500,
    height1 = 500) {

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

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

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

    options <- discOptions$new(
        dep = dep,
        covs = covs,
        per = per,
        prior = prior,
        gm = gm,
        coef = coef,
        nl = nl,
        prop = prop,
        tra = tra,
        tes = tes,
        gc = gc,
        plot = plot,
        plot1 = plot1,
        width = width,
        height = height,
        width1 = width1,
        height1 = height1)

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

    analysis$run()

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