median_ci returns bootstrapped confidence intervals for the median of a numeric vector. This function provides a simplified user interface to the boot and boot.ci functions similarly to the one.boot function but retains more of the boot package's functionality, most notably including options for parallelization. For convenience, when operating in parallel the user's operating system is automatically detected so that the appropriate parallelization engine is used (e.g. snow for Windows, multicore otherwise) by the parallel package. Since the mean and median are common descriptive statistics for which confidence intervals are estimated, these have their own dedicated functions. To obtain bootstrapped confidence intervals for other summary statistics use codestat_ci instead.

median_ci(
  y,
  replicates = 2000,
  ci_level = 0.95,
  ci_type = c("bca", "perc", "basic", "norm"),
  parallel = FALSE,
  cores = NULL,
  na.rm = TRUE
)

Arguments

y

A vector/variable (required).

replicates

The number of bootstrap replicates to use. Default is 2,000, as recommended by Efron & Tibshirani (1993). For publications, or if you need more precise estimates, more replications (e.g. >= 5,000) are recommended. N.B. more replications will of course take longer to run. If you get the error: "estimated adjustment 'a' is NA" when ci_type is set to "bca" then try again with more replications.

ci_level

The confidence level to use for constructing confidence intervals. Default is set to ci_level = 0.95 for 95 percent CIs.

ci_type

The type of confidence intervals to calculate from the bootstrap samples. Most of the options available in the underlying boot.ci function are implemented (except for studentized intervals): "norm" for an approximation based on the normal distribution, "perc" for percentile, "basic" for basic, and "bca" for bias-corrected and accelerated. BCa intervals are the default since these tend to provide the most accurate/least-biased results (Efron, 1987), however they require more time to calculate and may not be much better than the other methods for large sample sizes (e.g. >= 100,000 rows of data). See boot.ci for details.

parallel

set to TRUE if you want to use multiple cores or FALSE if you don't (the default). Note that there is some processing overhead involved when operating in parallel so speed gains may not be very noticeable for smaller samples (and may even take longer than sequential processing). Due to the nature of the underlying parallelization architecture, performance gains will likely be greater on non-Windows machines that can use the "multicore" implementation instead of "snow". For obvious reasons this option only works on machines with more than 1 logical processing core.

cores

If parallel is set to TRUE, this determines the number of cores to use. To see how many cores are available on your machine, use parallel::detectCores()

na.rm

should missing values be removed before attempting to calculate the median and confidence intervals? Default is TRUE.

References

Efron, B. (1987). Better bootstrap confidence intervals. Journal of the American statistical Association, 82(397), 171-185.

Efron, B., & Tibshirani, R. J. (1993). An introduction to the bootstrap. New York: Chapman & Hall.

Author

Craig P. Hutton, craig.hutton@gov.bc.ca

Examples


y1 <- rnorm(1:10000, 100, 10)

#using a single core (sequential processing)
median_ci(y1, ci_type = "perc")
#>    median     lower     upper 
#>  99.98204  99.72300 100.26620 

if (FALSE) {
#using multiple cores (parallel processing)
median_ci(y1, parallel = TRUE, cores = 2, ci_type = "perc")
}