weighted_quantile {ggdist} | R Documentation |
Weighted sample quantiles
Description
A variation of quantile()
that can be applied to weighted samples.
Usage
weighted_quantile(
x,
probs = seq(0, 1, 0.25),
weights = NULL,
n = NULL,
na.rm = FALSE,
names = TRUE,
type = 7,
digits = 7
)
weighted_quantile_fun(x, weights = NULL, n = NULL, na.rm = FALSE, type = 7)
Arguments
x |
numeric vector: sample values |
probs |
numeric vector: probabilities in |
weights |
Weights for the sample. One of:
|
n |
Presumed effective sample size. If this is greater than 1 and
continuous quantiles (
|
na.rm |
logical: if |
names |
logical: If |
type |
integer between 1 and 9: determines the type of quantile estimator to be used. Types 1 to 3 are for discontinuous quantiles, types 4 to 9 are for continuous quantiles. See Details. |
digits |
numeric: the number of digits to use to format percentages
when |
Details
Calculates weighted quantiles using a variation of the quantile types based
on a generalization of quantile()
.
Type 1–3 (discontinuous) quantiles are directly a function of the inverse CDF as a step function, and so can be directly translated to the weighted case using the natural definition of the weighted ECDF as the cumulative sum of the normalized weights.
Type 4–9 (continuous) quantiles require some translation from the definitions
in quantile()
. quantile()
defines continuous estimators in terms of
, which is the
th order statistic, and
, which is a function of
and
(the sample size). In the weighted case, we instead take
as the
th
smallest value of
in the weighted sample (not necessarily an order statistic,
because of the weights). Then we can re-write the formulas for
in terms of
(the empirical CDF at
, i.e. the cumulative sum of normalized
weights) and
(the normalized weight at
), by using the
fact that, in the unweighted case,
and
:
- Type 4
- Type 5
- Type 6
- Type 7
- Type 8
- Type 9
Then the quantile function (inverse CDF) is the piece-wise linear function
defined by the points .
Value
weighted_quantile()
returns a numeric vector of length(probs)
with the
estimate of the corresponding quantile from probs
.
weighted_quantile_fun()
returns a function that takes a single argument,
a vector of probabilities, which itself returns the corresponding quantile
estimates. It may be useful when weighted_quantile()
needs to be called
repeatedly for the same sample, re-using some pre-computation.