R/conttables.h.R

Defines functions contTables

Documented in contTables

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

contTablesOptions <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "contTablesOptions",
    inherit = jmvcore::Options,
    public = list(
        initialize = function(
            rows = NULL,
            cols = NULL,
            counts = NULL,
            layers = NULL,
            chiSq = TRUE,
            chiSqCorr = FALSE,
            zProp = FALSE,
            likeRat = FALSE,
            fisher = FALSE,
            contCoef = FALSE,
            phiCra = FALSE,
            diffProp = FALSE,
            logOdds = FALSE,
            odds = FALSE,
            relRisk = FALSE,
            ci = TRUE,
            ciWidth = 95,
            compare = "rows",
            hypothesis = "different",
            gamma = FALSE,
            taub = FALSE,
            mh = FALSE,
            obs = TRUE,
            exp = FALSE,
            pcRow = FALSE,
            pcCol = FALSE,
            pcTot = FALSE,
            barplot = FALSE,
            yaxis = "ycounts",
            yaxisPc = "total_pc",
            xaxis = "xrows",
            bartype = "dodge", ...) {

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

            private$..rows <- jmvcore::OptionVariable$new(
                "rows",
                rows,
                suggested=list(
                    "nominal",
                    "ordinal"),
                permitted=list(
                    "factor"))
            private$..cols <- jmvcore::OptionVariable$new(
                "cols",
                cols,
                suggested=list(
                    "nominal",
                    "ordinal"),
                permitted=list(
                    "factor"))
            private$..counts <- jmvcore::OptionVariable$new(
                "counts",
                counts,
                suggested=list(
                    "continuous"),
                permitted=list(
                    "numeric"),
                default=NULL)
            private$..layers <- jmvcore::OptionVariables$new(
                "layers",
                layers,
                default=NULL,
                permitted=list(
                    "factor"))
            private$..chiSq <- jmvcore::OptionBool$new(
                "chiSq",
                chiSq,
                default=TRUE)
            private$..chiSqCorr <- jmvcore::OptionBool$new(
                "chiSqCorr",
                chiSqCorr,
                default=FALSE)
            private$..zProp <- jmvcore::OptionBool$new(
                "zProp",
                zProp,
                default=FALSE)
            private$..likeRat <- jmvcore::OptionBool$new(
                "likeRat",
                likeRat,
                default=FALSE)
            private$..fisher <- jmvcore::OptionBool$new(
                "fisher",
                fisher,
                default=FALSE)
            private$..contCoef <- jmvcore::OptionBool$new(
                "contCoef",
                contCoef,
                default=FALSE)
            private$..phiCra <- jmvcore::OptionBool$new(
                "phiCra",
                phiCra,
                default=FALSE)
            private$..diffProp <- jmvcore::OptionBool$new(
                "diffProp",
                diffProp,
                default=FALSE)
            private$..logOdds <- jmvcore::OptionBool$new(
                "logOdds",
                logOdds,
                default=FALSE)
            private$..odds <- jmvcore::OptionBool$new(
                "odds",
                odds,
                default=FALSE)
            private$..relRisk <- jmvcore::OptionBool$new(
                "relRisk",
                relRisk,
                default=FALSE)
            private$..ci <- jmvcore::OptionBool$new(
                "ci",
                ci,
                default=TRUE)
            private$..ciWidth <- jmvcore::OptionNumber$new(
                "ciWidth",
                ciWidth,
                min=50,
                max=99.9,
                default=95)
            private$..compare <- jmvcore::OptionList$new(
                "compare",
                compare,
                options=list(
                    "rows",
                    "columns"),
                default="rows")
            private$..hypothesis <- jmvcore::OptionList$new(
                "hypothesis",
                hypothesis,
                options=list(
                    "different",
                    "oneGreater",
                    "twoGreater"),
                default="different")
            private$..gamma <- jmvcore::OptionBool$new(
                "gamma",
                gamma,
                default=FALSE)
            private$..taub <- jmvcore::OptionBool$new(
                "taub",
                taub,
                default=FALSE)
            private$..mh <- jmvcore::OptionBool$new(
                "mh",
                mh,
                default=FALSE)
            private$..obs <- jmvcore::OptionBool$new(
                "obs",
                obs,
                default=TRUE)
            private$..exp <- jmvcore::OptionBool$new(
                "exp",
                exp,
                default=FALSE)
            private$..pcRow <- jmvcore::OptionBool$new(
                "pcRow",
                pcRow,
                default=FALSE)
            private$..pcCol <- jmvcore::OptionBool$new(
                "pcCol",
                pcCol,
                default=FALSE)
            private$..pcTot <- jmvcore::OptionBool$new(
                "pcTot",
                pcTot,
                default=FALSE)
            private$..barplot <- jmvcore::OptionBool$new(
                "barplot",
                barplot,
                default=FALSE)
            private$..yaxis <- jmvcore::OptionList$new(
                "yaxis",
                yaxis,
                options=list(
                    "ycounts",
                    "ypc"),
                default="ycounts")
            private$..yaxisPc <- jmvcore::OptionList$new(
                "yaxisPc",
                yaxisPc,
                options=list(
                    "total_pc",
                    "column_pc",
                    "row_pc"),
                default="total_pc")
            private$..xaxis <- jmvcore::OptionList$new(
                "xaxis",
                xaxis,
                options=list(
                    "xrows",
                    "xcols"),
                default="xrows")
            private$..bartype <- jmvcore::OptionList$new(
                "bartype",
                bartype,
                options=list(
                    "dodge",
                    "stack"),
                default="dodge")

            self$.addOption(private$..rows)
            self$.addOption(private$..cols)
            self$.addOption(private$..counts)
            self$.addOption(private$..layers)
            self$.addOption(private$..chiSq)
            self$.addOption(private$..chiSqCorr)
            self$.addOption(private$..zProp)
            self$.addOption(private$..likeRat)
            self$.addOption(private$..fisher)
            self$.addOption(private$..contCoef)
            self$.addOption(private$..phiCra)
            self$.addOption(private$..diffProp)
            self$.addOption(private$..logOdds)
            self$.addOption(private$..odds)
            self$.addOption(private$..relRisk)
            self$.addOption(private$..ci)
            self$.addOption(private$..ciWidth)
            self$.addOption(private$..compare)
            self$.addOption(private$..hypothesis)
            self$.addOption(private$..gamma)
            self$.addOption(private$..taub)
            self$.addOption(private$..mh)
            self$.addOption(private$..obs)
            self$.addOption(private$..exp)
            self$.addOption(private$..pcRow)
            self$.addOption(private$..pcCol)
            self$.addOption(private$..pcTot)
            self$.addOption(private$..barplot)
            self$.addOption(private$..yaxis)
            self$.addOption(private$..yaxisPc)
            self$.addOption(private$..xaxis)
            self$.addOption(private$..bartype)
        }),
    active = list(
        rows = function() private$..rows$value,
        cols = function() private$..cols$value,
        counts = function() private$..counts$value,
        layers = function() private$..layers$value,
        chiSq = function() private$..chiSq$value,
        chiSqCorr = function() private$..chiSqCorr$value,
        zProp = function() private$..zProp$value,
        likeRat = function() private$..likeRat$value,
        fisher = function() private$..fisher$value,
        contCoef = function() private$..contCoef$value,
        phiCra = function() private$..phiCra$value,
        diffProp = function() private$..diffProp$value,
        logOdds = function() private$..logOdds$value,
        odds = function() private$..odds$value,
        relRisk = function() private$..relRisk$value,
        ci = function() private$..ci$value,
        ciWidth = function() private$..ciWidth$value,
        compare = function() private$..compare$value,
        hypothesis = function() private$..hypothesis$value,
        gamma = function() private$..gamma$value,
        taub = function() private$..taub$value,
        mh = function() private$..mh$value,
        obs = function() private$..obs$value,
        exp = function() private$..exp$value,
        pcRow = function() private$..pcRow$value,
        pcCol = function() private$..pcCol$value,
        pcTot = function() private$..pcTot$value,
        barplot = function() private$..barplot$value,
        yaxis = function() private$..yaxis$value,
        yaxisPc = function() private$..yaxisPc$value,
        xaxis = function() private$..xaxis$value,
        bartype = function() private$..bartype$value),
    private = list(
        ..rows = NA,
        ..cols = NA,
        ..counts = NA,
        ..layers = NA,
        ..chiSq = NA,
        ..chiSqCorr = NA,
        ..zProp = NA,
        ..likeRat = NA,
        ..fisher = NA,
        ..contCoef = NA,
        ..phiCra = NA,
        ..diffProp = NA,
        ..logOdds = NA,
        ..odds = NA,
        ..relRisk = NA,
        ..ci = NA,
        ..ciWidth = NA,
        ..compare = NA,
        ..hypothesis = NA,
        ..gamma = NA,
        ..taub = NA,
        ..mh = NA,
        ..obs = NA,
        ..exp = NA,
        ..pcRow = NA,
        ..pcCol = NA,
        ..pcTot = NA,
        ..barplot = NA,
        ..yaxis = NA,
        ..yaxisPc = NA,
        ..xaxis = NA,
        ..bartype = NA)
)

contTablesResults <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "contTablesResults",
    inherit = jmvcore::Group,
    active = list(
        freqs = function() private$.items[["freqs"]],
        chiSq = function() private$.items[["chiSq"]],
        odds = function() private$.items[["odds"]],
        nom = function() private$.items[["nom"]],
        gamma = function() private$.items[["gamma"]],
        taub = function() private$.items[["taub"]],
        mh = function() private$.items[["mh"]],
        barplot = function() private$.items[["barplot"]]),
    private = list(),
    public=list(
        initialize=function(options) {
            super$initialize(
                options=options,
                name="",
                title="Contingency Tables")
            self$add(jmvcore::Table$new(
                options=options,
                name="freqs",
                title="Contingency Tables",
                columns=list(),
                clearWith=list(
                    "rows",
                    "cols",
                    "counts",
                    "layers")))
            self$add(jmvcore::Table$new(
                options=options,
                name="chiSq",
                title="\u03C7\u00B2 Tests",
                clearWith=list(
                    "rows",
                    "cols",
                    "counts",
                    "layers",
                    "hypothesis",
                    "compare"),
                columns=list(
                    list(
                        `name`="test[chiSq]", 
                        `title`="", 
                        `type`="text", 
                        `content`="\u03C7\u00B2", 
                        `visible`="(chiSq)"),
                    list(
                        `name`="value[chiSq]", 
                        `title`="Value", 
                        `visible`="(chiSq)"),
                    list(
                        `name`="df[chiSq]", 
                        `title`="df", 
                        `type`="integer", 
                        `visible`="(chiSq)"),
                    list(
                        `name`="p[chiSq]", 
                        `title`="p", 
                        `type`="number", 
                        `format`="zto,pvalue", 
                        `visible`="(chiSq)"),
                    list(
                        `name`="test[chiSqCorr]", 
                        `title`="", 
                        `type`="text", 
                        `content`="\u03C7\u00B2 continuity correction", 
                        `visible`="(chiSqCorr)"),
                    list(
                        `name`="value[chiSqCorr]", 
                        `title`="Value", 
                        `visible`="(chiSqCorr)"),
                    list(
                        `name`="df[chiSqCorr]", 
                        `title`="df", 
                        `type`="integer", 
                        `visible`="(chiSqCorr)"),
                    list(
                        `name`="p[chiSqCorr]", 
                        `title`="p", 
                        `type`="number", 
                        `format`="zto,pvalue", 
                        `visible`="(chiSqCorr)"),
                    list(
                        `name`="test[zProp]", 
                        `title`="", 
                        `type`="text", 
                        `content`="z test difference in 2 proportions", 
                        `visible`="(zProp)"),
                    list(
                        `name`="value[zProp]", 
                        `title`="Value", 
                        `visible`="(zProp)"),
                    list(
                        `name`="df[zProp]", 
                        `title`="df", 
                        `visible`="(zProp)"),
                    list(
                        `name`="p[zProp]", 
                        `title`="p", 
                        `type`="number", 
                        `format`="zto,pvalue", 
                        `visible`="(zProp)"),
                    list(
                        `name`="test[likeRat]", 
                        `title`="", 
                        `type`="text", 
                        `content`="Likelihood ratio", 
                        `visible`="(likeRat)", 
                        `refs`="vcd"),
                    list(
                        `name`="value[likeRat]", 
                        `title`="Value", 
                        `visible`="(likeRat)"),
                    list(
                        `name`="df[likeRat]", 
                        `title`="df", 
                        `type`="integer", 
                        `visible`="(likeRat)"),
                    list(
                        `name`="p[likeRat]", 
                        `title`="p", 
                        `type`="number", 
                        `format`="zto,pvalue", 
                        `visible`="(likeRat)"),
                    list(
                        `name`="test[fisher]", 
                        `title`="", 
                        `type`="text", 
                        `content`="Fisher's exact test", 
                        `visible`="(fisher)"),
                    list(
                        `name`="value[fisher]", 
                        `title`="Value", 
                        `visible`="(fisher)"),
                    list(
                        `name`="p[fisher]", 
                        `title`="p", 
                        `type`="number", 
                        `format`="zto,pvalue", 
                        `visible`="(fisher)"),
                    list(
                        `name`="test[N]", 
                        `title`="", 
                        `type`="text", 
                        `content`="N"),
                    list(
                        `name`="value[N]", 
                        `title`="Value", 
                        `type`="integer"))))
            self$add(jmvcore::Table$new(
                options=options,
                name="odds",
                title="Comparative Measures",
                visible="(diffProp || logOdds || odds || relRisk)",
                clearWith=list(
                    "rows",
                    "cols",
                    "counts",
                    "layers",
                    "ciWidth",
                    "compare"),
                columns=list(
                    list(
                        `name`="t[dp]", 
                        `title`="", 
                        `type`="text", 
                        `content`="Difference in 2 proportions", 
                        `visible`="(diffProp)"),
                    list(
                        `name`="v[dp]", 
                        `title`="Value", 
                        `visible`="(diffProp)"),
                    list(
                        `name`="cil[dp]", 
                        `title`="Lower", 
                        `superTitle`="Confidence Intervals", 
                        `visible`="(diffProp && ci)"),
                    list(
                        `name`="ciu[dp]", 
                        `title`="Upper", 
                        `superTitle`="Confidence Intervals", 
                        `visible`="(diffProp && ci)"),
                    list(
                        `name`="t[lo]", 
                        `title`="", 
                        `type`="text", 
                        `content`="Log odds ratio", 
                        `visible`="(logOdds)", 
                        `refs`="vcd"),
                    list(
                        `name`="v[lo]", 
                        `title`="Value", 
                        `visible`="(logOdds)"),
                    list(
                        `name`="cil[lo]", 
                        `title`="Lower", 
                        `superTitle`="Confidence Intervals", 
                        `visible`="(logOdds && ci)"),
                    list(
                        `name`="ciu[lo]", 
                        `title`="Upper", 
                        `superTitle`="Confidence Intervals", 
                        `visible`="(logOdds && ci)"),
                    list(
                        `name`="t[o]", 
                        `title`="", 
                        `type`="text", 
                        `content`="Odds ratio", 
                        `visible`="(odds)"),
                    list(
                        `name`="v[o]", 
                        `title`="Value", 
                        `visible`="(odds)"),
                    list(
                        `name`="cil[o]", 
                        `title`="Lower", 
                        `superTitle`="Confidence Intervals", 
                        `visible`="(odds && ci)"),
                    list(
                        `name`="ciu[o]", 
                        `title`="Upper", 
                        `superTitle`="Confidence Intervals", 
                        `visible`="(odds && ci)"),
                    list(
                        `name`="t[rr]", 
                        `title`="", 
                        `type`="text", 
                        `content`="Relative risk", 
                        `visible`="(relRisk)"),
                    list(
                        `name`="v[rr]", 
                        `title`="Value", 
                        `visible`="(relRisk)"),
                    list(
                        `name`="cil[rr]", 
                        `title`="Lower", 
                        `superTitle`="Confidence Intervals", 
                        `visible`="(relRisk && ci)"),
                    list(
                        `name`="ciu[rr]", 
                        `title`="Upper", 
                        `superTitle`="Confidence Intervals", 
                        `visible`="(relRisk && ci)"))))
            self$add(jmvcore::Table$new(
                options=options,
                name="nom",
                title="Nominal",
                visible="(contCoef || phiCra)",
                columns=list(
                    list(
                        `name`="t[cont]", 
                        `title`="", 
                        `type`="text", 
                        `content`="Contingency coefficient", 
                        `visible`="(contCoef)"),
                    list(
                        `name`="v[cont]", 
                        `title`="Value", 
                        `visible`="(contCoef)"),
                    list(
                        `name`="t[phi]", 
                        `title`="", 
                        `type`="text", 
                        `content`="Phi-coefficient", 
                        `visible`="(phiCra)"),
                    list(
                        `name`="v[phi]", 
                        `title`="Value", 
                        `visible`="(phiCra)"),
                    list(
                        `name`="t[cra]", 
                        `title`="", 
                        `type`="text", 
                        `content`="Cramer's V", 
                        `visible`="(phiCra)"),
                    list(
                        `name`="v[cra]", 
                        `title`="Value", 
                        `visible`="(phiCra)"))))
            self$add(jmvcore::Table$new(
                options=options,
                name="gamma",
                title="Gamma",
                visible="(gamma)",
                refs="vcdExtra",
                clearWith=list(
                    "rows",
                    "cols",
                    "counts",
                    "layers"),
                columns=list(
                    list(
                        `name`="gamma", 
                        `title`="Gamma"),
                    list(
                        `name`="se", 
                        `title`="Standard Error"),
                    list(
                        `name`="cil", 
                        `title`="Lower", 
                        `superTitle`="Confidence Intervals"),
                    list(
                        `name`="ciu", 
                        `title`="Upper", 
                        `superTitle`="Confidence Intervals"))))
            self$add(jmvcore::Table$new(
                options=options,
                name="taub",
                title="Kendall's Tau-b",
                visible="(taub)",
                clearWith=list(
                    "rows",
                    "cols",
                    "counts",
                    "layers"),
                columns=list(
                    list(
                        `name`="taub", 
                        `title`="Kendall's Tau-B"),
                    list(
                        `name`="t", 
                        `title`="t"),
                    list(
                        `name`="p", 
                        `title`="p", 
                        `type`="number", 
                        `format`="zto,pvalue"))))
            self$add(jmvcore::Table$new(
                options=options,
                name="mh",
                title="Mantel-Haenszel Test for Trend",
                visible="(mh)",
                clearWith=list(
                    "rows",
                    "cols",
                    "counts",
                    "layers"),
                columns=list(
                    list(
                        `name`="chi2", 
                        `title`="\u03C7\u00B2"),
                    list(
                        `name`="df", 
                        `title`="df", 
                        `type`="integer"),
                    list(
                        `name`="p", 
                        `title`="p", 
                        `type`="number", 
                        `format`="zto,pvalue"))))
            self$add(jmvcore::Image$new(
                options=options,
                name="barplot",
                title="Plots",
                width=450,
                height=400,
                renderFun=".barPlot",
                visible="(barplot)",
                requiresData=TRUE,
                clearWith=list(
                    "rows",
                    "cols",
                    "counts",
                    "layers",
                    "yaxis",
                    "yaxisPc",
                    "xaxis",
                    "bartype")))}))

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

#' Contingency Tables
#'
#' The X² test of association (not to be confused with the X² goodness of fit) 
#' is used to test whether two categorical variables are independent or 
#' associated. If the p-value is low, it suggests the variables are not 
#' independent, and that there is a relationship between the two variables.
#' 
#'
#' @examples
#' data('HairEyeColor')
#' dat <- as.data.frame(HairEyeColor)
#'
#' contTables(formula = Freq ~ Hair:Eye, dat)
#'
#' #
#' #  CONTINGENCY TABLES
#' #
#' #  Contingency Tables
#' #  -----------------------------------------------------
#' #    Hair     Brown    Blue    Hazel    Green    Total
#' #  -----------------------------------------------------
#' #    Black       68      20       15        5      108
#' #    Brown      119      84       54       29      286
#' #    Red         26      17       14       14       71
#' #    Blond        7      94       10       16      127
#' #    Total      220     215       93       64      592
#' #  -----------------------------------------------------
#' #
#' #
#' #  X² Tests
#' #  -------------------------------
#' #          Value    df    p
#' #  -------------------------------
#' #    X²      138     9    < .001
#' #    N       592
#' #  -------------------------------
#' #
#'
#' # Alternatively, omit the left of the formula (`Freq`) if each row
#' # represents a single observation:
#'
#' contTables(formula = ~ Hair:Eye, dat)
#'
#' @param data the data as a data frame
#' @param rows the variable to use as the rows in the contingency table (not
#'   necessary when providing a formula, see the examples)
#' @param cols the variable to use as the columns in the contingency table
#'   (not necessary when providing a formula, see the examples)
#' @param counts the variable to use as the counts in the contingency table
#'   (not necessary when providing a formula, see the examples)
#' @param layers the variables to use to split the contingency table (not
#'   necessary when providing a formula, see the examples)
#' @param chiSq \code{TRUE} (default) or \code{FALSE}, provide X²
#' @param chiSqCorr \code{TRUE} or \code{FALSE} (default), provide X² with
#'   continuity correction
#' @param zProp \code{TRUE} or \code{FALSE} (default), provide a z test for
#'   differences between two proportions
#' @param likeRat \code{TRUE} or \code{FALSE} (default), provide the
#'   likelihood ratio
#' @param fisher \code{TRUE} or \code{FALSE} (default), provide Fisher's exact
#'   test
#' @param contCoef \code{TRUE} or \code{FALSE} (default), provide the
#'   contingency coefficient
#' @param phiCra \code{TRUE} or \code{FALSE} (default), provide Phi and
#'   Cramer's V
#' @param diffProp \code{TRUE} or \code{FALSE} (default), provide the
#'   differences in proportions (only available for 2x2 tables)
#' @param logOdds \code{TRUE} or \code{FALSE} (default), provide the log odds
#'   ratio (only available for 2x2 tables)
#' @param odds \code{TRUE} or \code{FALSE} (default), provide the odds ratio
#'   (only available for 2x2 tables)
#' @param relRisk \code{TRUE} or \code{FALSE} (default), provide the relative
#'   risk (only available for 2x2 tables)
#' @param ci \code{TRUE} or \code{FALSE} (default), provide confidence
#'   intervals for the comparative measures
#' @param ciWidth a number between 50 and 99.9 (default: 95), width of the
#'   confidence intervals to provide
#' @param compare \code{columns} or \code{rows} (default), compare
#'   columns/rows in difference of proportions or relative risks (2x2 tables)
#' @param hypothesis \code{'different'} (default), \code{'oneGreater'} or
#'   \code{'twoGreater'}, the alternative hypothesis; group 1 different to group
#'   2, group 1 greater than group 2, and group 2 greater than group 1
#'   respectively
#' @param gamma \code{TRUE} or \code{FALSE} (default), provide gamma
#' @param taub \code{TRUE} or \code{FALSE} (default), provide Kendall's tau-b
#' @param mh \code{TRUE} or \code{FALSE} (default), provide Mantel-Haenszel
#'   test for trend
#' @param obs \code{TRUE} or \code{FALSE} (default), provide the observed
#'   counts
#' @param exp \code{TRUE} or \code{FALSE} (default), provide the expected
#'   counts
#' @param pcRow \code{TRUE} or \code{FALSE} (default), provide row percentages
#' @param pcCol \code{TRUE} or \code{FALSE} (default), provide column
#'   percentages
#' @param pcTot \code{TRUE} or \code{FALSE} (default), provide total
#'   percentages
#' @param barplot \code{TRUE} or \code{FALSE} (default), show barplots
#' @param yaxis ycounts (default) or ypc. Use respectively \code{counts} or
#'   \code{percentages} for the bar plot y-axis
#' @param yaxisPc total_pc (default), column_pc, or row_pc. Use respectively
#'   percentages \code{of total}, \code{within columns}, or \code{within rows}
#'   for the bar plot y-axis.
#' @param xaxis rows (default), or columns in bar plot X axis
#' @param bartype stack or side by side (default), barplot type
#' @param formula (optional) the formula to use, see the examples
#' @return A results object containing:
#' \tabular{llllll}{
#'   \code{results$freqs} \tab \tab \tab \tab \tab a table of proportions \cr
#'   \code{results$chiSq} \tab \tab \tab \tab \tab a table of X² test results \cr
#'   \code{results$odds} \tab \tab \tab \tab \tab a table of comparative measures \cr
#'   \code{results$nom} \tab \tab \tab \tab \tab a table of the 'nominal' test results \cr
#'   \code{results$gamma} \tab \tab \tab \tab \tab a table of the gamma test results \cr
#'   \code{results$taub} \tab \tab \tab \tab \tab a table of the Kendall's tau-b test results \cr
#'   \code{results$mh} \tab \tab \tab \tab \tab a table of the Mantel-Haenszel test for trend \cr
#'   \code{results$barplot} \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$freqs$asDF}
#'
#' \code{as.data.frame(results$freqs)}
#'
#' @export
contTables <- function(
    data,
    rows,
    cols,
    counts = NULL,
    layers = NULL,
    chiSq = TRUE,
    chiSqCorr = FALSE,
    zProp = FALSE,
    likeRat = FALSE,
    fisher = FALSE,
    contCoef = FALSE,
    phiCra = FALSE,
    diffProp = FALSE,
    logOdds = FALSE,
    odds = FALSE,
    relRisk = FALSE,
    ci = TRUE,
    ciWidth = 95,
    compare = "rows",
    hypothesis = "different",
    gamma = FALSE,
    taub = FALSE,
    mh = FALSE,
    obs = TRUE,
    exp = FALSE,
    pcRow = FALSE,
    pcCol = FALSE,
    pcTot = FALSE,
    barplot = FALSE,
    yaxis = "ycounts",
    yaxisPc = "total_pc",
    xaxis = "xrows",
    bartype = "dodge",
    formula) {

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

    if ( ! missing(formula)) {
        if (missing(counts))
            counts <- jmvcore::marshalFormula(
                formula=formula,
                data=`if`( ! missing(data), data, NULL),
                from="lhs",
                type="vars",
                subset="1")
        if (missing(rows))
            rows <- jmvcore::marshalFormula(
                formula=formula,
                data=`if`( ! missing(data), data, NULL),
                from="rhs",
                type="vars",
                subset="1")
        if (missing(cols))
            cols <- jmvcore::marshalFormula(
                formula=formula,
                data=`if`( ! missing(data), data, NULL),
                from="rhs",
                type="vars",
                subset="2")
        if (missing(layers))
            layers <- jmvcore::marshalFormula(
                formula=formula,
                data=`if`( ! missing(data), data, NULL),
                from="rhs",
                type="vars",
                subset="3:")
    }

    if ( ! missing(rows)) rows <- jmvcore::resolveQuo(jmvcore::enquo(rows))
    if ( ! missing(cols)) cols <- jmvcore::resolveQuo(jmvcore::enquo(cols))
    if ( ! missing(counts)) counts <- jmvcore::resolveQuo(jmvcore::enquo(counts))
    if ( ! missing(layers)) layers <- jmvcore::resolveQuo(jmvcore::enquo(layers))
    if (missing(data))
        data <- jmvcore::marshalData(
            parent.frame(),
            `if`( ! missing(rows), rows, NULL),
            `if`( ! missing(cols), cols, NULL),
            `if`( ! missing(counts), counts, NULL),
            `if`( ! missing(layers), layers, NULL))

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

    options <- contTablesOptions$new(
        rows = rows,
        cols = cols,
        counts = counts,
        layers = layers,
        chiSq = chiSq,
        chiSqCorr = chiSqCorr,
        zProp = zProp,
        likeRat = likeRat,
        fisher = fisher,
        contCoef = contCoef,
        phiCra = phiCra,
        diffProp = diffProp,
        logOdds = logOdds,
        odds = odds,
        relRisk = relRisk,
        ci = ci,
        ciWidth = ciWidth,
        compare = compare,
        hypothesis = hypothesis,
        gamma = gamma,
        taub = taub,
        mh = mh,
        obs = obs,
        exp = exp,
        pcRow = pcRow,
        pcCol = pcCol,
        pcTot = pcTot,
        barplot = barplot,
        yaxis = yaxis,
        yaxisPc = yaxisPc,
        xaxis = xaxis,
        bartype = bartype)

    analysis <- contTablesClass$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.