Distribution-free confidence interval for a sample quantile
Source:R/utils-helpers.R
ci_quantile.RdComputes a nonparametric confidence interval for a sample quantile using
the order-statistic method (Conover, Practical Nonparametric
Statistics): the interval endpoints are order statistics of x, chosen
via the binomial distribution of ranks so that no assumption is made
about the shape of x's distribution.
Value
Named numeric vector (lower, upper), with confidence level
stored as an attribute. Returns c(lower = NA, upper = NA) if fewer
than 2 non-missing values are supplied.
Details
Used by er_vpc_add_observed() to compute a confidence interval
for each requested percentile of the observed response within an
exposure bin (the observed-side analogue of the across-replicate
percentile interval er_vpc_add_simulated() gets from simulated data,
powering er_style_vpc_observed_quantile_errorbar()). Like
ci_clopper_pearson(), this interval is exact for its target coverage
but conservative – the discreteness of the binomial rank distribution
means the achieved coverage can exceed the nominal conf_level,
especially for a small bin or an extreme prob. The candidate rank
indices are clipped to [1, length(x)], so a very small or extreme-prob
bin returns a (still valid, but wider-than-nominal) interval built from
the most extreme order statistics available rather than NA.
Examples
ci_quantile(rnorm(100), prob = 0.1)
#> lower upper
#> -1.9100875 -0.9140748
#> attr(,"conf_level")
#> [1] 0.95