Calculates the confidence interval for any quantile. Although bootstrapping might be a good approach for getting senisble confidence intervals there's sometimes need to have a nonparameteric alternative. This function offers one.

QuantileCI(x, probs=seq(0, 1, .25), conf.level = 0.95, 
           sides = c("two.sided", "left", "right"),
           na.rm = FALSE, method = c("exact", "boot"), R = 999)

Arguments

x

a (non-empty) numeric vector of data values.

probs

numeric vector of probabilities with values in [0,1]. (Values up to 2e-14 outside that range are accepted and moved to the nearby endpoint.)

conf.level

confidence level of the interval

sides

a character string specifying the side of the confidence interval, must be one of "two.sided" (default), "left" or "right" (abbreviations allowed).
"left" would be analogue to a "greater" hypothesis in a t.test.

na.rm

logical. Should missing values be removed? Defaults to FALSE.

method

defining the type of interval that should be calculated (one out of "exact", "boot"). Default is "exact". See Details.

R

The number of bootstrap replicates. Usually this will be a single positive integer. See boot.ci for details.

Details

The "exact" method corresponds to the way the confidence interval for the median is calculated in SAS.
The boot confidence interval type is calculated by means of boot.ci with default type "basic".

Value

if probs was of length 1 a numeric vector with 3 elements:

est

est

lwr.ci

lower bound of the confidence interval

upr.ci

upper bound of the confidence interval

or, if probs was a vector, a matrix with 3 columns consisting of estimate, lower ci, upper ci

est, lwr.ci, upr.ci

Author

Andri Signorell <andri@signorell.net> based on code of W Huber on StackExchange

Examples

QuantileCI(d.pizza$price, probs=0.25, na.rm=TRUE)
#>    est lwr.ci upr.ci 
#>  30.98  29.98  33.98 
#> attr(,"conf.level")
#> [1] 0.9502599

QuantileCI(d.pizza$price, na.rm=TRUE)
#>          est   lwr.ci upr.ci
#> 0%     8.792       NA  8.792
#> 25%   30.980  29.9800 33.980
#> 50%   46.764  44.9700 47.970
#> 75%   63.180  61.0056 65.655
#> 100% 134.334 124.4340     NA
#> attr(,"conf.level")
#> [1] 1.0000000 0.9502599 0.9503030 0.9502599 1.0000000
QuantileCI(d.pizza$price, conf.level=0.99, na.rm=TRUE)
#>          est  lwr.ci upr.ci
#> 0%     8.792      NA  8.792
#> 25%   30.980  29.980 35.176
#> 50%   46.764  44.970 47.970
#> 75%   63.180  59.660 66.360
#> 100% 134.334 124.434     NA
#> attr(,"conf.level")
#> [1] 1.0000000 0.9901407 0.9902694 0.9901407 1.0000000

# multiple probs
QuantileCI(1:100, method="exact" , probs = c(0.25, 0.75, .80, 0.95))
#>       est lwr.ci upr.ci
#> 25% 25.75     17     34
#> 75% 75.25     67     84
#> 80% 80.20     73     89
#> 95% 95.05     90     99
#> attr(,"conf.level")
#> [1] 0.9512948 0.9512948 0.9532735 0.9514464
QuantileCI(1:100, method="boot" , probs = c(0.25, 0.75, .80, 0.95))
#>       est lwr.ci upr.ci
#> 25% 25.75  16.25  34.00
#> 75% 75.25  67.50  85.25
#> 80% 80.20  73.20  89.20
#> 95% 95.05  92.05 100.90