R/anovanp.h.R

Defines functions anovaNP

Documented in anovaNP

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

anovaNPOptions <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "anovaNPOptions",
    inherit = jmvcore::Options,
    public = list(
        initialize = function(
            deps = NULL,
            group = NULL,
            es = FALSE,
            pairs = FALSE, ...) {

            super$initialize(
                package="jmv",
                name="anovaNP",
                requiresData=TRUE,
                ...)

            private$..deps <- jmvcore::OptionVariables$new(
                "deps",
                deps,
                required=TRUE,
                suggested=list(
                    "continuous",
                    "ordinal"))
            private$..group <- jmvcore::OptionVariable$new(
                "group",
                group,
                required=TRUE,
                rejectUnusedLevels=TRUE,
                suggested=list(
                    "nominal",
                    "ordinal"),
                permitted=list(
                    "factor"))
            private$..es <- jmvcore::OptionBool$new(
                "es",
                es,
                default=FALSE)
            private$..pairs <- jmvcore::OptionBool$new(
                "pairs",
                pairs,
                default=FALSE)

            self$.addOption(private$..deps)
            self$.addOption(private$..group)
            self$.addOption(private$..es)
            self$.addOption(private$..pairs)
        }),
    active = list(
        deps = function() private$..deps$value,
        group = function() private$..group$value,
        es = function() private$..es$value,
        pairs = function() private$..pairs$value),
    private = list(
        ..deps = NA,
        ..group = NA,
        ..es = NA,
        ..pairs = NA)
)

anovaNPResults <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "anovaNPResults",
    inherit = jmvcore::Group,
    active = list(
        table = function() private$.items[["table"]],
        comparisons = function() private$.items[["comparisons"]]),
    private = list(),
    public=list(
        initialize=function(options) {
            super$initialize(
                options=options,
                name="",
                title="One-Way ANOVA (Non-parametric)")
            self$add(jmvcore::Table$new(
                options=options,
                name="table",
                title="Kruskal-Wallis",
                rows="(deps)",
                clearWith=list(
                    "group"),
                columns=list(
                    list(
                        `name`="name", 
                        `title`="", 
                        `content`="($key)", 
                        `type`="text"),
                    list(
                        `name`="chiSq", 
                        `title`="\u03C7\u00B2", 
                        `type`="number"),
                    list(
                        `name`="df", 
                        `title`="df", 
                        `type`="integer"),
                    list(
                        `name`="p", 
                        `title`="p", 
                        `type`="number", 
                        `format`="zto,pvalue"),
                    list(
                        `name`="es", 
                        `title`="\u03B5\u00B2", 
                        `type`="number", 
                        `visible`="(es)"))))
            self$add(jmvcore::Array$new(
                options=options,
                name="comparisons",
                title="Dwass-Steel-Critchlow-Fligner pairwise comparisons",
                items="(deps)",
                visible="(pairs)",
                clearWith=list(
                    "group"),
                template=jmvcore::Table$new(
                    options=options,
                    title="Pairwise comparisons - $key",
                    rows=0,
                    clearWith=NULL,
                    columns=list(
                        list(
                            `name`="p1", 
                            `title`="", 
                            `content`=".", 
                            `type`="text"),
                        list(
                            `name`="p2", 
                            `title`="", 
                            `content`=".", 
                            `type`="text"),
                        list(
                            `name`="W", 
                            `type`="number"),
                        list(
                            `name`="p", 
                            `title`="p", 
                            `type`="number", 
                            `format`="zto,pvalue")))))}))

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

#' One-Way ANOVA (Non-parametric)
#'
#' The Kruskal-Wallis test is used to explore the relationship between a 
#' continuous dependent variable, and a categorical explanatory variable. It 
#' is analagous to ANOVA, but with the advantage of being non-parametric and 
#' having fewer assumptions. However, it has the limitation that it can only 
#' test a single explanatory variable at a time.
#' 
#'
#' @examples
#' data('ToothGrowth')
#'
#' anovaNP(formula = len ~ dose, data=ToothGrowth)
#'
#' #
#' #  ONE-WAY ANOVA (NON-PARAMETRIC)
#' #
#' #  Kruskal-Wallis
#' #  -------------------------------
#' #           X²      df    p
#' #  -------------------------------
#' #    len    40.7     2    < .001
#' #  -------------------------------
#' #
#'
#' @param data the data as a data frame
#' @param deps a string naming the dependent variable in \code{data}
#' @param group a string naming the grouping or independent variable in
#'   \code{data}
#' @param es \code{TRUE} or \code{FALSE} (default), provide effect-sizes
#' @param pairs \code{TRUE} or \code{FALSE} (default), perform pairwise
#'   comparisons
#' @param formula (optional) the formula to use, see the examples
#' @return A results object containing:
#' \tabular{llllll}{
#'   \code{results$table} \tab \tab \tab \tab \tab a table of the test results \cr
#'   \code{results$comparisons} \tab \tab \tab \tab \tab an array of pairwise comparison tables \cr
#' }
#'
#' Tables can be converted to data frames with \code{asDF} or \code{\link{as.data.frame}}. For example:
#'
#' \code{results$table$asDF}
#'
#' \code{as.data.frame(results$table)}
#'
#' @export
anovaNP <- function(
    data,
    deps,
    group,
    es = FALSE,
    pairs = FALSE,
    formula) {

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

    if ( ! missing(formula)) {
        if (missing(deps))
            deps <- jmvcore::marshalFormula(
                formula=formula,
                data=`if`( ! missing(data), data, NULL),
                from="lhs",
                required=TRUE)
        if (missing(group))
            group <- jmvcore::marshalFormula(
                formula=formula,
                data=`if`( ! missing(data), data, NULL),
                from="rhs",
                subset="1")
    }

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

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

    options <- anovaNPOptions$new(
        deps = deps,
        group = group,
        es = es,
        pairs = pairs)

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

    analysis$run()

    analysis$results
}

Try the jmv package in your browser

Any scripts or data that you put into this service are public.

jmv documentation built on Oct. 12, 2023, 5:13 p.m.