Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • 26d1524
  • /
  • boot_ci.R
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge
swh:1:cnt:d834e3abf49c04a3cfd81a6d98e1404eb4586534
directory badge
swh:1:dir:26d15248f18eb5be80e02b610bd4fcb500c3e047

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
boot_ci.R
#' Calculate bootstrap confidence intervals from a cutpointr object
#'
#' Given a \code{cutpointr} object that includes bootstrap results
#' this function calculates a bootstrap
#' confidence interval for a selected variable.
#' Missing values are removed before calculating the quantiles.
#' Values of the selected variable are returned for the percentiles alpha / 2
#' and 1 - alpha / 2. The metrics in the bootstrap data frames of
#' \code{cutpointr} are suffixed with \code{_b} and \code{_oob} to indicate
#' in-bag and out-of-bag, respectively. For example, to calculate quantiles
#' of the in-bag AUC \code{variable = AUC_b} should be set.
#'
#' @param x (character) The numeric independent (predictor) variable.
#' @param variable Variable to calculate CI for
#' @param alpha Alpha level. Quantiles of the bootstrapped values are returned
#' for (alpha / 2) and 1 - (alpha / 2).
#' @param in_bag Whether the in-bag or out-of-bag results should be used for testing
#' @return A data frame with the columns quantile and value
#' @examples
#' \dontrun{
#' opt_cut <- cutpointr(suicide, dsi, suicide, gender,
#'   metric = youden, boot_runs = 1000)
#' boot_ci(opt_cut, optimal_cutpoint, in_bag = FALSE, alpha = 0.05)
#' boot_ci(opt_cut, acc, in_bag = FALSE, alpha = 0.05)
#' boot_ci(opt_cut, cohens_kappa, in_bag = FALSE, alpha = 0.05)
#' boot_ci(opt_cut, AUC, in_bag = TRUE, alpha = 0.05)
#' }
#' @export
#' @family main cutpointr functions
boot_ci <- function(x, variable, in_bag = TRUE, alpha = 0.05) {
    if (alpha < 0 | alpha > 1) stop("alpha should be between 0 and 1.")
    if (!inherits(x, "cutpointr")) {
        stop("Only objects of type cutpointr are supported")
    }
    if (!has_boot_results(x)) {
        stop("No bootstrap results found. Was boot_runs > 0 in cutpointr?")
    }

    if (!("subgroup" %in% colnames(x))) {
        variable <- rlang::enquo(variable)
        variable <- rlang::as_name(variable)
        if (in_bag) suffix <- "_b" else suffix <- "_oob"
        if (variable == "optimal_cutpoint") {
            suffix <- ""
            in_bag = TRUE
        }
        variable <- paste0(variable, suffix)
        variable <- x %>%
            dplyr::select(.data$boot) %>%
            tidyr::unnest(cols = .data$boot) %>%
            dplyr::pull(variable)
        values <- stats::quantile(variable, probs = c(alpha / 2, 1 - alpha / 2),
                           na.rm = TRUE)
        res <- tibble::tibble(
            quantile = c(alpha / 2, 1 - alpha / 2),
            values = values
        )
        return(res)
    } else {
        res <- purrr::map2_dfr(.x = x$subgroup, .y = x$boot,
                               .f = function(s, b) {
            variable <- rlang::enquo(variable)
            variable <- rlang::as_name(variable)
            if (in_bag) suffix <- "_b" else suffix <- "_oob"
            if (variable == "optimal_cutpoint") {
                suffix <- ""
                in_bag = TRUE
            }
            variable <- paste0(variable, suffix)
            variable <- b %>%
                dplyr::pull(variable)
            values <- stats::quantile(variable, probs = c(alpha / 2, 1 - alpha / 2),
                               na.rm = TRUE)
            tibble::tibble(
                subgroup = s,
                quantile = c(alpha / 2, 1 - alpha / 2),
                values = values
            )
        })
        return(res)
    }
}

back to top

Software Heritage — Copyright (C) 2015–2026, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API