R/tree.h.R

Defines functions tree

Documented in tree

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

treeOptions <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "treeOptions",
    inherit = jmvcore::Options,
    public = list(
        initialize = function(
            dep = NULL,
            covs = NULL,
            facs = NULL,
            per = 0.7,
            over1 = TRUE,
            tab1 = FALSE,
            over2 = FALSE,
            tab2 = FALSE,
            cla = FALSE,
            plot = FALSE,
            plot1 = FALSE,
            width = 500,
            height = 500,
            width1 = 500,
            height1 = 500, ...) {

            super$initialize(
                package="snowCluster",
                name="tree",
                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$..facs <- jmvcore::OptionVariables$new(
                "facs",
                facs,
                suggested=list(
                    "nominal",
                    "ordinal"),
                permitted=list(
                    "factor"))
            private$..per <- jmvcore::OptionNumber$new(
                "per",
                per,
                min=0.1,
                max=1,
                default=0.7)
            private$..over1 <- jmvcore::OptionBool$new(
                "over1",
                over1,
                default=TRUE)
            private$..tab1 <- jmvcore::OptionBool$new(
                "tab1",
                tab1,
                default=FALSE)
            private$..over2 <- jmvcore::OptionBool$new(
                "over2",
                over2,
                default=FALSE)
            private$..tab2 <- jmvcore::OptionBool$new(
                "tab2",
                tab2,
                default=FALSE)
            private$..cla <- jmvcore::OptionBool$new(
                "cla",
                cla,
                default=FALSE)
            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$..facs)
            self$.addOption(private$..per)
            self$.addOption(private$..over1)
            self$.addOption(private$..tab1)
            self$.addOption(private$..over2)
            self$.addOption(private$..tab2)
            self$.addOption(private$..cla)
            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,
        facs = function() private$..facs$value,
        per = function() private$..per$value,
        over1 = function() private$..over1$value,
        tab1 = function() private$..tab1$value,
        over2 = function() private$..over2$value,
        tab2 = function() private$..tab2$value,
        cla = function() private$..cla$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,
        ..facs = NA,
        ..per = NA,
        ..over1 = NA,
        ..tab1 = NA,
        ..over2 = NA,
        ..tab2 = NA,
        ..cla = NA,
        ..plot = NA,
        ..plot1 = NA,
        ..width = NA,
        ..height = NA,
        ..width1 = NA,
        ..height1 = NA)
)

treeResults <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "treeResults",
    inherit = jmvcore::Group,
    active = list(
        instructions = function() private$.items[["instructions"]],
        text = function() private$.items[["text"]],
        over1 = function() private$.items[["over1"]],
        tab1 = function() private$.items[["tab1"]],
        over2 = function() private$.items[["over2"]],
        tab2 = function() private$.items[["tab2"]],
        cla = function() private$.items[["cla"]],
        plot = function() private$.items[["plot"]],
        plot1 = function() private$.items[["plot1"]]),
    private = list(),
    public=list(
        initialize=function(options) {
            super$initialize(
                options=options,
                name="",
                title="Decision Tree",
                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="over1",
                title="Overall statistics with training set",
                rows=1,
                visible="(over1)",
                clearWith=list(
                    "covs",
                    "dep",
                    "per",
                    "facs"),
                refs="caret",
                columns=list(
                    list(
                        `name`="accu", 
                        `title`="Accuracy", 
                        `type`="number"),
                    list(
                        `name`="lower", 
                        `title`="Lower", 
                        `type`="number", 
                        `superTitle`="Accuracy 95% CI"),
                    list(
                        `name`="upper", 
                        `title`="Upper", 
                        `type`="number", 
                        `superTitle`="Accuracy 95% CI"),
                    list(
                        `name`="kappa", 
                        `title`="Kappa", 
                        `type`="number"))))
            self$add(jmvcore::Table$new(
                options=options,
                name="tab1",
                title="Confusion Matrix with training set",
                visible="(tab1)",
                refs="caret",
                clearWith=list(
                    "covs",
                    "dep",
                    "per",
                    "facs"),
                columns=list(
                    list(
                        `name`="name", 
                        `title`="", 
                        `type`="text", 
                        `content`="($key)"))))
            self$add(jmvcore::Table$new(
                options=options,
                name="over2",
                title="Overall statistics with test set",
                rows=1,
                visible="(over2)",
                clearWith=list(
                    "covs",
                    "dep",
                    "per",
                    "facs"),
                refs="caret",
                columns=list(
                    list(
                        `name`="accu", 
                        `title`="Accuracy", 
                        `type`="number"),
                    list(
                        `name`="lower", 
                        `title`="Lower", 
                        `type`="number", 
                        `superTitle`="Accuracy 95% CI"),
                    list(
                        `name`="upper", 
                        `title`="Upper", 
                        `type`="number", 
                        `superTitle`="Accuracy 95% CI"),
                    list(
                        `name`="kappa", 
                        `title`="Kappa", 
                        `type`="number"))))
            self$add(jmvcore::Table$new(
                options=options,
                name="tab2",
                title="Confusion Matrix with test set",
                visible="(tab2)",
                refs="caret",
                clearWith=list(
                    "covs",
                    "dep",
                    "per",
                    "facs"),
                columns=list(
                    list(
                        `name`="name", 
                        `title`="", 
                        `type`="text", 
                        `content`="($key)"))))
            self$add(jmvcore::Table$new(
                options=options,
                name="cla",
                title="Statistics by class with test set",
                visible="(cla)",
                refs="caret",
                clearWith=list(
                    "covs",
                    "dep",
                    "per",
                    "facs"),
                columns=list(
                    list(
                        `name`="name", 
                        `title`="", 
                        `type`="text", 
                        `content`="($key)"))))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot",
                title="Decision tree plot with train set",
                visible="(plot)",
                requiresData=TRUE,
                renderFun=".plot",
                refs="party",
                clearWith=list(
                    "covs",
                    "dep",
                    "facs",
                    "per",
                    "width",
                    "height")))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot1",
                title="rpart plot with train set",
                visible="(plot1)",
                requiresData=TRUE,
                renderFun=".plot1",
                refs="rpart.plot",
                clearWith=list(
                    "covs",
                    "dep",
                    "facs",
                    "per",
                    "width1",
                    "height1")))}))

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

#' Decision Tree
#'
#' 
#' @param data .
#' @param dep .
#' @param covs .
#' @param facs .
#' @param per .
#' @param over1 .
#' @param tab1 .
#' @param over2 .
#' @param tab2 .
#' @param cla .
#' @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$over1} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$tab1} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$over2} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$tab2} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$cla} \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
#' }
#'
#' Tables can be converted to data frames with \code{asDF} or \code{\link{as.data.frame}}. For example:
#'
#' \code{results$over1$asDF}
#'
#' \code{as.data.frame(results$over1)}
#'
#' @export
tree <- function(
    data,
    dep,
    covs,
    facs,
    per = 0.7,
    over1 = TRUE,
    tab1 = FALSE,
    over2 = FALSE,
    tab2 = FALSE,
    cla = FALSE,
    plot = FALSE,
    plot1 = FALSE,
    width = 500,
    height = 500,
    width1 = 500,
    height1 = 500) {

    if ( ! requireNamespace("jmvcore", quietly=TRUE))
        stop("tree 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(facs)) facs <- jmvcore::resolveQuo(jmvcore::enquo(facs))
    if (missing(data))
        data <- jmvcore::marshalData(
            parent.frame(),
            `if`( ! missing(dep), dep, NULL),
            `if`( ! missing(covs), covs, NULL),
            `if`( ! missing(facs), facs, NULL))

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

    options <- treeOptions$new(
        dep = dep,
        covs = covs,
        facs = facs,
        per = per,
        over1 = over1,
        tab1 = tab1,
        over2 = over2,
        tab2 = tab2,
        cla = cla,
        plot = plot,
        plot1 = plot1,
        width = width,
        height = height,
        width1 = width1,
        height1 = height1)

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

    analysis$run()

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