R/decisioncompare.h.R

Defines functions decisioncompare

Documented in decisioncompare

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

decisioncompareOptions <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "decisioncompareOptions",
    inherit = jmvcore::Options,
    public = list(
        initialize = function(
            gold = NULL,
            goldPositive = NULL,
            test1 = NULL,
            test1Positive = NULL,
            test2 = NULL,
            test2Positive = NULL,
            test3 = NULL,
            test3Positive = NULL,
            pp = FALSE,
            pprob = 0.3,
            od = FALSE,
            fnote = FALSE,
            ci = FALSE,
            plot = FALSE,
            statComp = FALSE, ...) {

            super$initialize(
                package="ClinicoPath",
                name="decisioncompare",
                requiresData=TRUE,
                ...)

            private$..gold <- jmvcore::OptionVariable$new(
                "gold",
                gold,
                suggested=list(
                    "nominal"),
                permitted=list(
                    "factor"))
            private$..goldPositive <- jmvcore::OptionLevel$new(
                "goldPositive",
                goldPositive,
                variable="(gold)")
            private$..test1 <- jmvcore::OptionVariable$new(
                "test1",
                test1,
                suggested=list(
                    "nominal"),
                permitted=list(
                    "factor"))
            private$..test1Positive <- jmvcore::OptionLevel$new(
                "test1Positive",
                test1Positive,
                variable="(test1)")
            private$..test2 <- jmvcore::OptionVariable$new(
                "test2",
                test2,
                suggested=list(
                    "nominal"),
                permitted=list(
                    "factor"))
            private$..test2Positive <- jmvcore::OptionLevel$new(
                "test2Positive",
                test2Positive,
                variable="(test2)")
            private$..test3 <- jmvcore::OptionVariable$new(
                "test3",
                test3,
                suggested=list(
                    "nominal"),
                permitted=list(
                    "factor"))
            private$..test3Positive <- jmvcore::OptionLevel$new(
                "test3Positive",
                test3Positive,
                variable="(test3)")
            private$..pp <- jmvcore::OptionBool$new(
                "pp",
                pp,
                default=FALSE)
            private$..pprob <- jmvcore::OptionNumber$new(
                "pprob",
                pprob,
                default=0.3,
                min=0.001,
                max=0.999)
            private$..od <- jmvcore::OptionBool$new(
                "od",
                od,
                default=FALSE)
            private$..fnote <- jmvcore::OptionBool$new(
                "fnote",
                fnote,
                default=FALSE)
            private$..ci <- jmvcore::OptionBool$new(
                "ci",
                ci,
                default=FALSE)
            private$..plot <- jmvcore::OptionBool$new(
                "plot",
                plot,
                default=FALSE)
            private$..statComp <- jmvcore::OptionBool$new(
                "statComp",
                statComp,
                default=FALSE)

            self$.addOption(private$..gold)
            self$.addOption(private$..goldPositive)
            self$.addOption(private$..test1)
            self$.addOption(private$..test1Positive)
            self$.addOption(private$..test2)
            self$.addOption(private$..test2Positive)
            self$.addOption(private$..test3)
            self$.addOption(private$..test3Positive)
            self$.addOption(private$..pp)
            self$.addOption(private$..pprob)
            self$.addOption(private$..od)
            self$.addOption(private$..fnote)
            self$.addOption(private$..ci)
            self$.addOption(private$..plot)
            self$.addOption(private$..statComp)
        }),
    active = list(
        gold = function() private$..gold$value,
        goldPositive = function() private$..goldPositive$value,
        test1 = function() private$..test1$value,
        test1Positive = function() private$..test1Positive$value,
        test2 = function() private$..test2$value,
        test2Positive = function() private$..test2Positive$value,
        test3 = function() private$..test3$value,
        test3Positive = function() private$..test3Positive$value,
        pp = function() private$..pp$value,
        pprob = function() private$..pprob$value,
        od = function() private$..od$value,
        fnote = function() private$..fnote$value,
        ci = function() private$..ci$value,
        plot = function() private$..plot$value,
        statComp = function() private$..statComp$value),
    private = list(
        ..gold = NA,
        ..goldPositive = NA,
        ..test1 = NA,
        ..test1Positive = NA,
        ..test2 = NA,
        ..test2Positive = NA,
        ..test3 = NA,
        ..test3Positive = NA,
        ..pp = NA,
        ..pprob = NA,
        ..od = NA,
        ..fnote = NA,
        ..ci = NA,
        ..plot = NA,
        ..statComp = NA)
)

decisioncompareResults <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "decisioncompareResults",
    inherit = jmvcore::Group,
    active = list(
        text1 = function() private$.items[["text1"]],
        text2 = function() private$.items[["text2"]],
        cTable1 = function() private$.items[["cTable1"]],
        epirTable1 = function() private$.items[["epirTable1"]],
        cTable2 = function() private$.items[["cTable2"]],
        epirTable2 = function() private$.items[["epirTable2"]],
        cTable3 = function() private$.items[["cTable3"]],
        epirTable3 = function() private$.items[["epirTable3"]],
        comparisonTable = function() private$.items[["comparisonTable"]],
        mcnemarTable = function() private$.items[["mcnemarTable"]],
        diffTable = function() private$.items[["diffTable"]],
        plot1 = function() private$.items[["plot1"]]),
    private = list(),
    public=list(
        initialize=function(options) {
            super$initialize(
                options=options,
                name="",
                title="Compare Medical Decision Tests",
                refs=list(
                    "DiagnosticTests",
                    "ClinicoPathJamoviModule",
                    "stats"))
            self$add(jmvcore::Preformatted$new(
                options=options,
                name="text1",
                title="Original Data",
                visible="(od)"))
            self$add(jmvcore::Html$new(
                options=options,
                name="text2",
                title="Original Data",
                visible="(od)"))
            self$add(jmvcore::Table$new(
                options=options,
                name="cTable1",
                title="Test 1 - Recoded Data",
                rows=0,
                columns=list(
                    list(
                        `name`="newtest", 
                        `title`="", 
                        `type`="text"),
                    list(
                        `name`="GP", 
                        `title`="Gold Positive", 
                        `type`="number"),
                    list(
                        `name`="GN", 
                        `title`="Gold Negative", 
                        `type`="number"),
                    list(
                        `name`="Total", 
                        `title`="Total", 
                        `type`="number"))))
            self$add(jmvcore::Table$new(
                options=options,
                name="epirTable1",
                title="Test 1 - Confidence Intervals",
                rows=0,
                columns=list(
                    list(
                        `name`="statsnames", 
                        `title`="Decision Statistics", 
                        `type`="text"),
                    list(
                        `name`="est", 
                        `title`="Estimate", 
                        `type`="number", 
                        `format`="pc"),
                    list(
                        `name`="lower", 
                        `title`="Lower", 
                        `superTitle`="95% Confidence Interval", 
                        `type`="number", 
                        `format`="pc"),
                    list(
                        `name`="upper", 
                        `title`="Upper", 
                        `superTitle`="95% Confidence Interval", 
                        `type`="number", 
                        `format`="pc"))))
            self$add(jmvcore::Table$new(
                options=options,
                name="cTable2",
                title="Test 2 - Recoded Data",
                rows=0,
                columns=list(
                    list(
                        `name`="newtest", 
                        `title`="", 
                        `type`="text"),
                    list(
                        `name`="GP", 
                        `title`="Gold Positive", 
                        `type`="number"),
                    list(
                        `name`="GN", 
                        `title`="Gold Negative", 
                        `type`="number"),
                    list(
                        `name`="Total", 
                        `title`="Total", 
                        `type`="number"))))
            self$add(jmvcore::Table$new(
                options=options,
                name="epirTable2",
                title="Test 2 - Confidence Intervals",
                rows=0,
                columns=list(
                    list(
                        `name`="statsnames", 
                        `title`="Decision Statistics", 
                        `type`="text"),
                    list(
                        `name`="est", 
                        `title`="Estimate", 
                        `type`="number", 
                        `format`="pc"),
                    list(
                        `name`="lower", 
                        `title`="Lower", 
                        `superTitle`="95% Confidence Interval", 
                        `type`="number", 
                        `format`="pc"),
                    list(
                        `name`="upper", 
                        `title`="Upper", 
                        `superTitle`="95% Confidence Interval", 
                        `type`="number", 
                        `format`="pc"))))
            self$add(jmvcore::Table$new(
                options=options,
                name="cTable3",
                title="Test 3 - Recoded Data",
                rows=0,
                columns=list(
                    list(
                        `name`="newtest", 
                        `title`="", 
                        `type`="text"),
                    list(
                        `name`="GP", 
                        `title`="Gold Positive", 
                        `type`="number"),
                    list(
                        `name`="GN", 
                        `title`="Gold Negative", 
                        `type`="number"),
                    list(
                        `name`="Total", 
                        `title`="Total", 
                        `type`="number"))))
            self$add(jmvcore::Table$new(
                options=options,
                name="epirTable3",
                title="Test 3 - Confidence Intervals",
                rows=0,
                columns=list(
                    list(
                        `name`="statsnames", 
                        `title`="Decision Statistics", 
                        `type`="text"),
                    list(
                        `name`="est", 
                        `title`="Estimate", 
                        `type`="number", 
                        `format`="pc"),
                    list(
                        `name`="lower", 
                        `title`="Lower", 
                        `superTitle`="95% Confidence Interval", 
                        `type`="number", 
                        `format`="pc"),
                    list(
                        `name`="upper", 
                        `title`="Upper", 
                        `superTitle`="95% Confidence Interval", 
                        `type`="number", 
                        `format`="pc"))))
            self$add(jmvcore::Table$new(
                options=options,
                name="comparisonTable",
                title="Decision Test Comparison",
                rows=0,
                columns=list(
                    list(
                        `name`="test", 
                        `title`="Test", 
                        `type`="text"),
                    list(
                        `name`="Sens", 
                        `title`="Sensitivity", 
                        `type`="number", 
                        `format`="pc"),
                    list(
                        `name`="Spec", 
                        `title`="Specificity", 
                        `type`="number", 
                        `format`="pc"),
                    list(
                        `name`="AccurT", 
                        `title`="Accuracy", 
                        `type`="number", 
                        `format`="pc"),
                    list(
                        `name`="PPV", 
                        `title`="Positive Predictive Value", 
                        `type`="number", 
                        `format`="pc"),
                    list(
                        `name`="NPV", 
                        `title`="Negative Predictive Value", 
                        `type`="number", 
                        `format`="pc"),
                    list(
                        `name`="LRP", 
                        `title`="Positive Likelihood Ratio", 
                        `type`="number"),
                    list(
                        `name`="LRN", 
                        `title`="Negative Likelihood Ratio", 
                        `type`="number")),
                clearWith=list(
                    "pp",
                    "pprob")))
            self$add(jmvcore::Table$new(
                options=options,
                name="mcnemarTable",
                title="McNemar's Test for Test Comparison",
                visible="(statComp)",
                rows=0,
                columns=list(
                    list(
                        `name`="comparison", 
                        `title`="Comparison", 
                        `type`="text"),
                    list(
                        `name`="stat", 
                        `title`="McNemar's Chi-squared", 
                        `type`="number"),
                    list(
                        `name`="df", 
                        `title`="df", 
                        `type`="integer"),
                    list(
                        `name`="p", 
                        `title`="p-value", 
                        `type`="number", 
                        `format`="zto,pvalue"))))
            self$add(jmvcore::Table$new(
                options=options,
                name="diffTable",
                title="Differences with 95% Confidence Intervals",
                visible="(statComp)",
                rows=0,
                columns=list(
                    list(
                        `name`="comparison", 
                        `title`="Comparison", 
                        `type`="text"),
                    list(
                        `name`="metric", 
                        `title`="Metric", 
                        `type`="text"),
                    list(
                        `name`="diff", 
                        `title`="Difference", 
                        `type`="number", 
                        `format`="pc"),
                    list(
                        `name`="lower", 
                        `title`="Lower", 
                        `superTitle`="95% Confidence Interval", 
                        `type`="number", 
                        `format`="pc"),
                    list(
                        `name`="upper", 
                        `title`="Upper", 
                        `superTitle`="95% Confidence Interval", 
                        `type`="number", 
                        `format`="pc"))))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot1",
                title="Test Comparison",
                width=600,
                height=450,
                renderFun=".plot1",
                requiresData=TRUE,
                visible="(plot)",
                clearWith=list(
                    "plot"),
                refs=list(
                    "ggplot2")))}))

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

#' Compare Medical Decision Tests
#'
#' Function for comparing multiple Medical Decision Tests. Compares 
#' sensitivity, specificity, positive predictive value, negative predictive 
#' value, and other metrics between different tests against the same golden 
#' standard. Includes statistical comparison using McNemar's test and 
#' confidence intervals for differences.
#' 
#'
#' @examples
#' \donttest{
#' # example will be added
#'}
#' @param data The data as a data frame.
#' @param gold .
#' @param goldPositive .
#' @param test1 .
#' @param test1Positive .
#' @param test2 .
#' @param test2Positive .
#' @param test3 .
#' @param test3Positive .
#' @param pp .
#' @param pprob Prior probability (disease prevalence in the community).
#'   Requires a value between 0.001 and 0.999, default 0.300.
#' @param od Boolean selection whether to show frequency tables. Default is
#'   'false'.
#' @param fnote .
#' @param ci .
#' @param plot .
#' @param statComp Perform statistical comparison between tests (McNemar's
#'   test and confidence intervals for differences).
#' @return A results object containing:
#' \tabular{llllll}{
#'   \code{results$text1} \tab \tab \tab \tab \tab a preformatted \cr
#'   \code{results$text2} \tab \tab \tab \tab \tab a html \cr
#'   \code{results$cTable1} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$epirTable1} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$cTable2} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$epirTable2} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$cTable3} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$epirTable3} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$comparisonTable} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$mcnemarTable} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$diffTable} \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$cTable1$asDF}
#'
#' \code{as.data.frame(results$cTable1)}
#'
#' @export
decisioncompare <- function(
    data,
    gold,
    goldPositive,
    test1,
    test1Positive,
    test2,
    test2Positive,
    test3,
    test3Positive,
    pp = FALSE,
    pprob = 0.3,
    od = FALSE,
    fnote = FALSE,
    ci = FALSE,
    plot = FALSE,
    statComp = FALSE) {

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

    if ( ! missing(gold)) gold <- jmvcore::resolveQuo(jmvcore::enquo(gold))
    if ( ! missing(test1)) test1 <- jmvcore::resolveQuo(jmvcore::enquo(test1))
    if ( ! missing(test2)) test2 <- jmvcore::resolveQuo(jmvcore::enquo(test2))
    if ( ! missing(test3)) test3 <- jmvcore::resolveQuo(jmvcore::enquo(test3))
    if (missing(data))
        data <- jmvcore::marshalData(
            parent.frame(),
            `if`( ! missing(gold), gold, NULL),
            `if`( ! missing(test1), test1, NULL),
            `if`( ! missing(test2), test2, NULL),
            `if`( ! missing(test3), test3, NULL))

    for (v in gold) if (v %in% names(data)) data[[v]] <- as.factor(data[[v]])
    for (v in test1) if (v %in% names(data)) data[[v]] <- as.factor(data[[v]])
    for (v in test2) if (v %in% names(data)) data[[v]] <- as.factor(data[[v]])
    for (v in test3) if (v %in% names(data)) data[[v]] <- as.factor(data[[v]])

    options <- decisioncompareOptions$new(
        gold = gold,
        goldPositive = goldPositive,
        test1 = test1,
        test1Positive = test1Positive,
        test2 = test2,
        test2Positive = test2Positive,
        test3 = test3,
        test3Positive = test3Positive,
        pp = pp,
        pprob = pprob,
        od = od,
        fnote = fnote,
        ci = ci,
        plot = plot,
        statComp = statComp)

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

    analysis$run()

    analysis$results
}
sbalci/ClinicoPathJamoviModule documentation built on June 13, 2025, 9:34 a.m.