R/rmc.h.R

Defines functions rmc

Documented in rmc

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

rmcOptions <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "rmcOptions",
    inherit = jmvcore::Options,
    public = list(
        initialize = function(
            id = NULL,
            dep = NULL,
            covs = NULL,
            rc = FALSE,
            plot = FALSE,
            width = 500,
            height = 500,
            cc = FALSE,
            plot1 = FALSE,
            width1 = 500,
            height1 = 500, ...) {

            super$initialize(
                package="seolmatrix",
                name="rmc",
                requiresData=TRUE,
                ...)

            private$..id <- jmvcore::OptionVariable$new(
                "id",
                id,
                suggested=list(
                    "nominal"),
                permitted=list(
                    "factor"))
            private$..dep <- jmvcore::OptionVariable$new(
                "dep",
                dep,
                suggested=list(
                    "continuous"),
                permitted=list(
                    "numeric"))
            private$..covs <- jmvcore::OptionVariable$new(
                "covs",
                covs,
                suggested=list(
                    "continuous"),
                permitted=list(
                    "numeric"))
            private$..rc <- jmvcore::OptionBool$new(
                "rc",
                rc,
                default=FALSE)
            private$..plot <- jmvcore::OptionBool$new(
                "plot",
                plot,
                default=FALSE)
            private$..width <- jmvcore::OptionInteger$new(
                "width",
                width,
                default=500)
            private$..height <- jmvcore::OptionInteger$new(
                "height",
                height,
                default=500)
            private$..cc <- jmvcore::OptionBool$new(
                "cc",
                cc,
                default=FALSE)
            private$..plot1 <- jmvcore::OptionBool$new(
                "plot1",
                plot1,
                default=FALSE)
            private$..width1 <- jmvcore::OptionInteger$new(
                "width1",
                width1,
                default=500)
            private$..height1 <- jmvcore::OptionInteger$new(
                "height1",
                height1,
                default=500)

            self$.addOption(private$..id)
            self$.addOption(private$..dep)
            self$.addOption(private$..covs)
            self$.addOption(private$..rc)
            self$.addOption(private$..plot)
            self$.addOption(private$..width)
            self$.addOption(private$..height)
            self$.addOption(private$..cc)
            self$.addOption(private$..plot1)
            self$.addOption(private$..width1)
            self$.addOption(private$..height1)
        }),
    active = list(
        id = function() private$..id$value,
        dep = function() private$..dep$value,
        covs = function() private$..covs$value,
        rc = function() private$..rc$value,
        plot = function() private$..plot$value,
        width = function() private$..width$value,
        height = function() private$..height$value,
        cc = function() private$..cc$value,
        plot1 = function() private$..plot1$value,
        width1 = function() private$..width1$value,
        height1 = function() private$..height1$value),
    private = list(
        ..id = NA,
        ..dep = NA,
        ..covs = NA,
        ..rc = NA,
        ..plot = NA,
        ..width = NA,
        ..height = NA,
        ..cc = NA,
        ..plot1 = NA,
        ..width1 = NA,
        ..height1 = NA)
)

rmcResults <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "rmcResults",
    inherit = jmvcore::Group,
    active = list(
        instructions = function() private$.items[["instructions"]],
        rc = function() private$.items[["rc"]],
        plot = function() private$.items[["plot"]],
        cc = function() private$.items[["cc"]],
        plot1 = function() private$.items[["plot1"]]),
    private = list(),
    public=list(
        initialize=function(options) {
            super$initialize(
                options=options,
                name="",
                title="Repeated & Cross Correlation",
                refs="seolmatrix")
            self$add(jmvcore::Html$new(
                options=options,
                name="instructions",
                title="Instructions",
                visible=TRUE))
            self$add(jmvcore::Table$new(
                options=options,
                name="rc",
                title="Repeated correlation coefficient",
                rows=1,
                refs="rmcorr",
                visible="(rc)",
                clearWith=list(
                    "id",
                    "dep",
                    "covs"),
                columns=list(
                    list(
                        `name`="r", 
                        `title`="Coefficient", 
                        `type`="number"),
                    list(
                        `name`="df", 
                        `title`="df", 
                        `type`="integer"),
                    list(
                        `name`="p", 
                        `title`="p", 
                        `type`="number", 
                        `format`="zto,pvalue"),
                    list(
                        `name`="lower", 
                        `title`="Lower", 
                        `type`="number", 
                        `superTitle`="95% CI"),
                    list(
                        `name`="upper", 
                        `title`="Upper", 
                        `type`="number", 
                        `superTitle`="95% CI"))))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot",
                title="Scatterplot for Repeated Correlation",
                renderFun=".plot",
                visible="(plot)",
                refs="rmcorr",
                requiresData=TRUE,
                clearWith=list(
                    "id",
                    "dep",
                    "covs",
                    "width",
                    "height")))
            self$add(jmvcore::Table$new(
                options=options,
                name="cc",
                title="Cross correlation values",
                visible="(cc)",
                clearWith=list(
                    "dep",
                    "covs"),
                columns=list(
                    list(
                        `name`="lag", 
                        `title`="Lag", 
                        `type`="integer"),
                    list(
                        `name`="value", 
                        `title`="Value", 
                        `type`="number"))))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot1",
                title="Cross Correlation plot",
                renderFun=".plot1",
                visible="(plot1)",
                requiresData=TRUE,
                clearWith=list(
                    "dep",
                    "covs",
                    "width1",
                    "height1")))}))

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

#' Repeated & Cross Correlation
#'
#' 
#' @param data .
#' @param id .
#' @param dep .
#' @param covs .
#' @param rc .
#' @param plot .
#' @param width .
#' @param height .
#' @param cc .
#' @param plot1 .
#' @param width1 .
#' @param height1 .
#' @return A results object containing:
#' \tabular{llllll}{
#'   \code{results$instructions} \tab \tab \tab \tab \tab a html \cr
#'   \code{results$rc} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$plot} \tab \tab \tab \tab \tab an image \cr
#'   \code{results$cc} \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$rc$asDF}
#'
#' \code{as.data.frame(results$rc)}
#'
#' @export
rmc <- function(
    data,
    id,
    dep,
    covs,
    rc = FALSE,
    plot = FALSE,
    width = 500,
    height = 500,
    cc = FALSE,
    plot1 = FALSE,
    width1 = 500,
    height1 = 500) {

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

    if ( ! missing(id)) id <- jmvcore::resolveQuo(jmvcore::enquo(id))
    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(id), id, NULL),
            `if`( ! missing(dep), dep, NULL),
            `if`( ! missing(covs), covs, NULL))

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

    options <- rmcOptions$new(
        id = id,
        dep = dep,
        covs = covs,
        rc = rc,
        plot = plot,
        width = width,
        height = height,
        cc = cc,
        plot1 = plot1,
        width1 = width1,
        height1 = height1)

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

    analysis$run()

    analysis$results
}
hyunsooseol/seolmatrix documentation built on July 25, 2024, 4:42 a.m.