R/oddsratio.h.R

Defines functions oddsratio

Documented in oddsratio

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

oddsratioOptions <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "oddsratioOptions",
    inherit = jmvcore::Options,
    public = list(
        initialize = function(
            explanatory = NULL,
            outcome = NULL,
            showNomogram = FALSE, ...) {

            super$initialize(
                package="jsurvival",
                name="oddsratio",
                requiresData=TRUE,
                ...)

            private$..explanatory <- jmvcore::OptionVariables$new(
                "explanatory",
                explanatory,
                suggested=list(
                    "nominal",
                    "continuous"),
                permitted=list(
                    "factor",
                    "numeric"))
            private$..outcome <- jmvcore::OptionVariable$new(
                "outcome",
                outcome,
                suggested=list(
                    "ordinal",
                    "nominal"),
                permitted=list(
                    "factor"))
            private$..showNomogram <- jmvcore::OptionBool$new(
                "showNomogram",
                showNomogram,
                default=FALSE)

            self$.addOption(private$..explanatory)
            self$.addOption(private$..outcome)
            self$.addOption(private$..showNomogram)
        }),
    active = list(
        explanatory = function() private$..explanatory$value,
        outcome = function() private$..outcome$value,
        showNomogram = function() private$..showNomogram$value),
    private = list(
        ..explanatory = NA,
        ..outcome = NA,
        ..showNomogram = NA)
)

oddsratioResults <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "oddsratioResults",
    inherit = jmvcore::Group,
    active = list(
        todo = function() private$.items[["todo"]],
        text = function() private$.items[["text"]],
        text2 = function() private$.items[["text2"]],
        plot = function() private$.items[["plot"]],
        nomogram = function() private$.items[["nomogram"]],
        mydataview_nomogram = function() private$.items[["mydataview_nomogram"]],
        plot_nomogram = function() private$.items[["plot_nomogram"]]),
    private = list(),
    public=list(
        initialize=function(options) {
            super$initialize(
                options=options,
                name="",
                title="Odds Ratio Table and Plot",
                refs=list(
                    "finalfit",
                    "rms",
                    "Hmisc",
                    "survival",
                    "survivaltutorial",
                    "ClinicoPathJamoviModule"))
            self$add(jmvcore::Html$new(
                options=options,
                name="todo",
                title="To Do",
                clearWith=list(
                    "explanatory",
                    "outcome")))
            self$add(jmvcore::Html$new(
                options=options,
                name="text",
                title="Odds Ratio Table",
                clearWith=list(
                    "explanatory",
                    "outcome")))
            self$add(jmvcore::Html$new(
                options=options,
                name="text2",
                title=""))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot",
                title="Odds Ratio Plot",
                width=600,
                height=450,
                renderFun=".plot",
                requiresData=TRUE,
                clearWith=list(
                    "explanatory",
                    "outcome")))
            self$add(jmvcore::Html$new(
                options=options,
                name="nomogram",
                title="Nomogram",
                visible="(showNomogram)"))
            self$add(jmvcore::Preformatted$new(
                options=options,
                name="mydataview_nomogram",
                title="Model Details",
                visible="(showNomogram)"))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot_nomogram",
                title="Nomogram",
                width=800,
                height=600,
                requiresData=TRUE,
                renderFun=".plot_nomogram",
                visible="(showNomogram)"))}))

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

#' Odds Ratio Table and Plot
#'
#' Function for Odds Ratio Table and Plot.
#'
#' @examples
#' \donttest{
#' # example will be added
#'}
#' @param data The data as a data frame.
#' @param explanatory The explanatory variables to be used in the analysis.
#' @param outcome The outcome variable to be used in the analysis.
#' @param showNomogram Display an interactive nomogram for converting pre-test
#'   to post-test  probabilities using likelihood ratios calculated from the
#'   data.
#' @return A results object containing:
#' \tabular{llllll}{
#'   \code{results$todo} \tab \tab \tab \tab \tab a html \cr
#'   \code{results$text} \tab \tab \tab \tab \tab a html \cr
#'   \code{results$text2} \tab \tab \tab \tab \tab a html \cr
#'   \code{results$plot} \tab \tab \tab \tab \tab an image \cr
#'   \code{results$nomogram} \tab \tab \tab \tab \tab a html \cr
#'   \code{results$mydataview_nomogram} \tab \tab \tab \tab \tab a preformatted \cr
#'   \code{results$plot_nomogram} \tab \tab \tab \tab \tab an image \cr
#' }
#'
#' @export
oddsratio <- function(
    data,
    explanatory,
    outcome,
    showNomogram = FALSE) {

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

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

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

    options <- oddsratioOptions$new(
        explanatory = explanatory,
        outcome = outcome,
        showNomogram = showNomogram)

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

    analysis$run()

    analysis$results
}
sbalci/jsurvival documentation built on Feb. 26, 2025, 1:20 a.m.