R/timeroc.h.R

Defines functions timeroc

Documented in timeroc

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

timerocOptions <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "timerocOptions",
    inherit = jmvcore::Options,
    public = list(
        initialize = function(
            elapsedtime = NULL,
            outcome = NULL,
            outcomeLevel = NULL,
            marker = NULL,
            timepoints = "12, 36, 60",
            method = "incident",
            bootstrapCI = FALSE,
            nboot = 100,
            plotROC = TRUE,
            plotAUC = TRUE, ...) {

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

            private$..elapsedtime <- jmvcore::OptionVariable$new(
                "elapsedtime",
                elapsedtime,
                suggested=list(
                    "continuous"),
                permitted=list(
                    "numeric"))
            private$..outcome <- jmvcore::OptionVariable$new(
                "outcome",
                outcome,
                suggested=list(
                    "ordinal",
                    "nominal",
                    "continuous"),
                permitted=list(
                    "factor",
                    "numeric"))
            private$..outcomeLevel <- jmvcore::OptionLevel$new(
                "outcomeLevel",
                outcomeLevel,
                variable="(outcome)")
            private$..marker <- jmvcore::OptionVariable$new(
                "marker",
                marker,
                suggested=list(
                    "continuous"),
                permitted=list(
                    "numeric"))
            private$..timepoints <- jmvcore::OptionString$new(
                "timepoints",
                timepoints,
                default="12, 36, 60")
            private$..method <- jmvcore::OptionList$new(
                "method",
                method,
                options=list(
                    "incident",
                    "cumulative",
                    "static"),
                default="incident")
            private$..bootstrapCI <- jmvcore::OptionBool$new(
                "bootstrapCI",
                bootstrapCI,
                default=FALSE)
            private$..nboot <- jmvcore::OptionInteger$new(
                "nboot",
                nboot,
                default=100,
                min=10,
                max=1000)
            private$..plotROC <- jmvcore::OptionBool$new(
                "plotROC",
                plotROC,
                default=TRUE)
            private$..plotAUC <- jmvcore::OptionBool$new(
                "plotAUC",
                plotAUC,
                default=TRUE)

            self$.addOption(private$..elapsedtime)
            self$.addOption(private$..outcome)
            self$.addOption(private$..outcomeLevel)
            self$.addOption(private$..marker)
            self$.addOption(private$..timepoints)
            self$.addOption(private$..method)
            self$.addOption(private$..bootstrapCI)
            self$.addOption(private$..nboot)
            self$.addOption(private$..plotROC)
            self$.addOption(private$..plotAUC)
        }),
    active = list(
        elapsedtime = function() private$..elapsedtime$value,
        outcome = function() private$..outcome$value,
        outcomeLevel = function() private$..outcomeLevel$value,
        marker = function() private$..marker$value,
        timepoints = function() private$..timepoints$value,
        method = function() private$..method$value,
        bootstrapCI = function() private$..bootstrapCI$value,
        nboot = function() private$..nboot$value,
        plotROC = function() private$..plotROC$value,
        plotAUC = function() private$..plotAUC$value),
    private = list(
        ..elapsedtime = NA,
        ..outcome = NA,
        ..outcomeLevel = NA,
        ..marker = NA,
        ..timepoints = NA,
        ..method = NA,
        ..bootstrapCI = NA,
        ..nboot = NA,
        ..plotROC = NA,
        ..plotAUC = NA)
)

timerocResults <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "timerocResults",
    inherit = jmvcore::Group,
    active = list(
        text = function() private$.items[["text"]],
        aucTable = function() private$.items[["aucTable"]],
        rocPlot = function() private$.items[["rocPlot"]],
        aucPlot = function() private$.items[["aucPlot"]]),
    private = list(),
    public=list(
        initialize=function(options) {
            super$initialize(
                options=options,
                name="",
                title="Time-Dependent ROC Analysis")
            self$add(jmvcore::Html$new(
                options=options,
                name="text",
                title="Time-Dependent ROC Analysis Results"))
            self$add(jmvcore::Table$new(
                options=options,
                name="aucTable",
                title="Area Under ROC Curve (AUC)",
                rows=0,
                columns=list(
                    list(
                        `name`="timepoint", 
                        `title`="Timepoint", 
                        `type`="integer"),
                    list(
                        `name`="auc", 
                        `title`="AUC", 
                        `type`="number"),
                    list(
                        `name`="se", 
                        `title`="SE", 
                        `type`="number"),
                    list(
                        `name`="ci_lower", 
                        `title`="95% CI Lower", 
                        `type`="number"),
                    list(
                        `name`="ci_upper", 
                        `title`="95% CI Upper", 
                        `type`="number"))))
            self$add(jmvcore::Image$new(
                options=options,
                name="rocPlot",
                title="ROC Curves",
                width=600,
                height=450,
                renderFun=".plotROC",
                visible="(plotROC)"))
            self$add(jmvcore::Image$new(
                options=options,
                name="aucPlot",
                title="AUC Over Time",
                width=600,
                height=450,
                renderFun=".plotAUC",
                visible="(plotAUC)"))}))

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

#' Time-Dependent ROC Analysis
#'
#' 
#' @param data .
#' @param elapsedtime .
#' @param outcome .
#' @param outcomeLevel .
#' @param marker .
#' @param timepoints .
#' @param method .
#' @param bootstrapCI .
#' @param nboot .
#' @param plotROC .
#' @param plotAUC .
#' @return A results object containing:
#' \tabular{llllll}{
#'   \code{results$text} \tab \tab \tab \tab \tab a html \cr
#'   \code{results$aucTable} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$rocPlot} \tab \tab \tab \tab \tab an image \cr
#'   \code{results$aucPlot} \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$aucTable$asDF}
#'
#' \code{as.data.frame(results$aucTable)}
#'
#' @export
timeroc <- function(
    data,
    elapsedtime,
    outcome,
    outcomeLevel,
    marker,
    timepoints = "12, 36, 60",
    method = "incident",
    bootstrapCI = FALSE,
    nboot = 100,
    plotROC = TRUE,
    plotAUC = TRUE) {

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

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


    options <- timerocOptions$new(
        elapsedtime = elapsedtime,
        outcome = outcome,
        outcomeLevel = outcomeLevel,
        marker = marker,
        timepoints = timepoints,
        method = method,
        bootstrapCI = bootstrapCI,
        nboot = nboot,
        plotROC = plotROC,
        plotAUC = plotAUC)

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

    analysis$run()

    analysis$results
}
sbalci/ClinicoPathJamoviModule documentation built on Feb. 25, 2025, 6:34 a.m.