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.
Arguments
- x
a (non-empty) numeric vector of data values.
- 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 at.test.- method
defining the type of interval that should be calculated (one out of
"exact","boot","hd"). Default is"exact". See Details.- probs
numeric vector of probabilities with values in
[0,1]. Values up to2e-14outside that range are accepted and moved to the nearby endpoint.- na.rm
logical. Should missing values be removed? Defaults to
FALSE.- ...
bootstrap arguments for the methods
"boot"and"hd":R,type,parallelandncpus. Seeboot::boot.ci()for details.
Value
A numeric matrix with one row per element of probs and
columns:
estestimated quantile: the sample quantile (type 7 of
quantile()), withmethod = "hd"the Harrell-Davis estimate.lcilower confidence interval bound.
uciupper confidence interval bound.
For the "exact" method, the attribute
conf.level reports the achieved coverage (which may differ from the
requested level).
Details
The "exact" method searches the order statistics near the binomial
quantiles for the interval whose coverage is closest to, but not below,
conf.level; the achieved coverage is reported.
The boot confidence interval type is
calculated by means of boot::boot.ci() with default type
"bca".
method = "hd" uses the Harrell-Davis estimator (Harrell & Davis 1982)
instead of the sample quantile, both for the point estimate in est
and for the interval. The estimator is a weighted average of all order
statistics, see DescToolsX::quantileX(type = "hd"). The interval is
the bootstrap distribution of the estimator, by default the percentile
interval (Wilcox's qcipb(), which he recommends for all levels and
in particular with tied values; Wilcox 2022, sec. 4.5). The weights of
the order statistics depend only on \(n\) and probs, so they are
computed once and each resample costs one sort. type may be "perc"
(default), "basic", "norm" or "bca"; R defaults to 999.
At probs 0 and 1 the estimate is the sample extreme and no bootstrap
interval exists; lci and uci are NA there.
References
Harrell, F. E. and Davis, C. E. (1982). A new distribution-free quantile estimator. Biometrika, 69(3), 635–640. doi:10.1093/biomet/69.3.635
Wilcox, R. R. (2022) Introduction to Robust Estimation and Hypothesis Testing, 5th ed. Academic Press.
See also
DescToolsX::quantileX, quantile(),
Other ci.location:
meanCI(),
meanCIn(),
meanDiffCI(),
medianCI(),
medianDiffCI(),
sumCI()
Examples
x <- mtcars$mpg
quantileCI(x, probs=0.25, na.rm=TRUE)
#> est lci uci
#> 25% 15.425 13.3 17.8
#> attr(,"conf.level")
#> [1] 0.9555407
quantileCI(x, na.rm=TRUE)
#> est lci uci
#> 0% 10.400 -Inf 10.4
#> 25% 15.425 13.3 17.8
#> 50% 19.200 15.8 21.4
#> 75% 22.800 21.0 30.4
#> 100% 33.900 30.4 Inf
#> attr(,"conf.level")
#> [1] 1.0000000 0.9555407 0.9649180 0.9555407 1.0000000
quantileCI(x, conf.level=0.99, na.rm=TRUE)
#> est lci uci
#> 0% 10.400 -Inf 10.4
#> 25% 15.425 13.3 19.2
#> 50% 19.200 15.5 22.8
#> 75% 22.800 19.2 30.4
#> 100% 33.900 30.4 Inf
#> attr(,"conf.level")
#> [1] 1.0000000 0.9912891 0.9929996 0.9912891 1.0000000
# multiple probs
quantileCI(1:100, method="exact" , probs = c(0.25, 0.75, .80, 0.95))
#> est lci uci
#> 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 lci uci
#> 25% 25.75 16.38201 33.75
#> 75% 75.25 66.18216 83.25
#> 80% 80.20 72.20000 87.20
#> 95% 95.05 90.00000 98.05
# Harrell-Davis estimator with percentile bootstrap interval
set.seed(1)
quantileCI(x, method = "hd", probs = c(0.1, 0.5, 0.9))
#> est lci uci
#> 10% 13.21366 10.87787 15.19380
#> 50% 19.28385 16.80127 21.33751
#> 90% 29.80512 24.48154 32.68771
# works with ties, where the exact interval is conservative
set.seed(1)
quantileCI(round(x), method = "hd", probs = 0.5, R = 1999)
#> est lci uci
#> 50% 19.2698 16.90769 21.3325
