R/cotest.h.R

Defines functions cotest

Documented in cotest

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

cotestOptions <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "cotestOptions",
    inherit = jmvcore::Options,
    public = list(
        initialize = function(
            test1_sens = 0.8,
            test1_spec = 0.9,
            test2_sens = 0.75,
            test2_spec = 0.95,
            indep = TRUE,
            cond_dep_pos = 0.05,
            cond_dep_neg = 0.05,
            prevalence = 0.1,
            fnote = FALSE,
            fagan = FALSE, ...) {

            super$initialize(
                package="ClinicoPath",
                name="cotest",
                requiresData=FALSE,
                ...)

            private$..test1_sens <- jmvcore::OptionNumber$new(
                "test1_sens",
                test1_sens,
                default=0.8,
                min=0.01,
                max=0.99)
            private$..test1_spec <- jmvcore::OptionNumber$new(
                "test1_spec",
                test1_spec,
                default=0.9,
                min=0.01,
                max=0.99)
            private$..test2_sens <- jmvcore::OptionNumber$new(
                "test2_sens",
                test2_sens,
                default=0.75,
                min=0.01,
                max=0.99)
            private$..test2_spec <- jmvcore::OptionNumber$new(
                "test2_spec",
                test2_spec,
                default=0.95,
                min=0.01,
                max=0.99)
            private$..indep <- jmvcore::OptionBool$new(
                "indep",
                indep,
                default=TRUE)
            private$..cond_dep_pos <- jmvcore::OptionNumber$new(
                "cond_dep_pos",
                cond_dep_pos,
                default=0.05,
                min=0,
                max=1)
            private$..cond_dep_neg <- jmvcore::OptionNumber$new(
                "cond_dep_neg",
                cond_dep_neg,
                default=0.05,
                min=0,
                max=1)
            private$..prevalence <- jmvcore::OptionNumber$new(
                "prevalence",
                prevalence,
                default=0.1,
                min=0.001,
                max=0.999)
            private$..fnote <- jmvcore::OptionBool$new(
                "fnote",
                fnote,
                default=FALSE)
            private$..fagan <- jmvcore::OptionBool$new(
                "fagan",
                fagan,
                default=FALSE)

            self$.addOption(private$..test1_sens)
            self$.addOption(private$..test1_spec)
            self$.addOption(private$..test2_sens)
            self$.addOption(private$..test2_spec)
            self$.addOption(private$..indep)
            self$.addOption(private$..cond_dep_pos)
            self$.addOption(private$..cond_dep_neg)
            self$.addOption(private$..prevalence)
            self$.addOption(private$..fnote)
            self$.addOption(private$..fagan)
        }),
    active = list(
        test1_sens = function() private$..test1_sens$value,
        test1_spec = function() private$..test1_spec$value,
        test2_sens = function() private$..test2_sens$value,
        test2_spec = function() private$..test2_spec$value,
        indep = function() private$..indep$value,
        cond_dep_pos = function() private$..cond_dep_pos$value,
        cond_dep_neg = function() private$..cond_dep_neg$value,
        prevalence = function() private$..prevalence$value,
        fnote = function() private$..fnote$value,
        fagan = function() private$..fagan$value),
    private = list(
        ..test1_sens = NA,
        ..test1_spec = NA,
        ..test2_sens = NA,
        ..test2_spec = NA,
        ..indep = NA,
        ..cond_dep_pos = NA,
        ..cond_dep_neg = NA,
        ..prevalence = NA,
        ..fnote = NA,
        ..fagan = NA)
)

cotestResults <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "cotestResults",
    inherit = jmvcore::Group,
    active = list(
        testParamsTable = function() private$.items[["testParamsTable"]],
        cotestResultsTable = function() private$.items[["cotestResultsTable"]],
        dependenceInfo = function() private$.items[["dependenceInfo"]],
        dependenceExplanation = function() private$.items[["dependenceExplanation"]],
        explanation = function() private$.items[["explanation"]],
        plot1 = function() private$.items[["plot1"]]),
    private = list(),
    public=list(
        initialize=function(options) {
            super$initialize(
                options=options,
                name="",
                title="Co-Testing Analysis",
                refs=list(
                    "DiagnosticTests",
                    "MultipleDiagnosticTests"))
            self$add(jmvcore::Table$new(
                options=options,
                name="testParamsTable",
                title="Test Parameters",
                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`="plr", 
                        `title`="Positive LR", 
                        `type`="number"),
                    list(
                        `name`="nlr", 
                        `title`="Negative LR", 
                        `type`="number"))))
            self$add(jmvcore::Table$new(
                options=options,
                name="cotestResultsTable",
                title="Co-Testing Results",
                rows=0,
                columns=list(
                    list(
                        `name`="scenario", 
                        `title`="Scenario", 
                        `type`="text"),
                    list(
                        `name`="postProb", 
                        `title`="Post-test Probability", 
                        `type`="number", 
                        `format`="pc"),
                    list(
                        `name`="relativeProbability", 
                        `title`="Relative to Prevalence", 
                        `type`="number"),
                    list(
                        `name`="orValue", 
                        `title`="Odds Ratio", 
                        `type`="number"))))
            self$add(jmvcore::Html$new(
                options=options,
                name="dependenceInfo",
                title="Test Dependence Information",
                visible="(!indep)"))
            self$add(jmvcore::Html$new(
                options=options,
                name="dependenceExplanation",
                title="Understanding Test Dependence"))
            self$add(jmvcore::Html$new(
                options=options,
                name="explanation",
                title="Explanation"))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot1",
                title="Fagan nomogram",
                width=600,
                height=450,
                renderFun=".plot1",
                requiresData=TRUE,
                visible="(fagan)",
                clearWith=list(
                    "test1_sens",
                    "test1_spec",
                    "test2_sens",
                    "test2_spec",
                    "prevalence",
                    "fagan")))}))

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

#' Co-Testing Analysis
#'
#' Function for analyzing combined results of two concurrent diagnostic tests. 
#' Calculates post-test probabilities based on various scenarios (either test 
#' positive, both positive, both negative).
#' 
#'
#' @examples
#' \donttest{
#' # example will be added
#'}
#' @param test1_sens Sensitivity (true positive rate) of Test 1.
#' @param test1_spec Specificity (true negative rate) of Test 1.
#' @param test2_sens Sensitivity (true positive rate) of Test 2.
#' @param test2_spec Specificity (true negative rate) of Test 2.
#' @param indep Assume tests are conditionally independent (default is true).
#' @param cond_dep_pos Conditional dependence between tests for subjects with
#'   disease. Value between 0 (independence) and 1 (complete dependence).
#' @param cond_dep_neg Conditional dependence between tests for subjects
#'   without disease. Value between 0 (independence) and 1 (complete
#'   dependence).
#' @param prevalence Prior probability (disease prevalence in the population).
#'   Requires a value between 0.001 and 0.999.
#' @param fnote .
#' @param fagan .
#' @return A results object containing:
#' \tabular{llllll}{
#'   \code{results$testParamsTable} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$cotestResultsTable} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$dependenceInfo} \tab \tab \tab \tab \tab a html \cr
#'   \code{results$dependenceExplanation} \tab \tab \tab \tab \tab a html \cr
#'   \code{results$explanation} \tab \tab \tab \tab \tab a html \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$testParamsTable$asDF}
#'
#' \code{as.data.frame(results$testParamsTable)}
#'
#' @export
cotest <- function(
    test1_sens = 0.8,
    test1_spec = 0.9,
    test2_sens = 0.75,
    test2_spec = 0.95,
    indep = TRUE,
    cond_dep_pos = 0.05,
    cond_dep_neg = 0.05,
    prevalence = 0.1,
    fnote = FALSE,
    fagan = FALSE) {

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


    options <- cotestOptions$new(
        test1_sens = test1_sens,
        test1_spec = test1_spec,
        test2_sens = test2_sens,
        test2_spec = test2_spec,
        indep = indep,
        cond_dep_pos = cond_dep_pos,
        cond_dep_neg = cond_dep_neg,
        prevalence = prevalence,
        fnote = fnote,
        fagan = fagan)

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

    analysis$run()

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