weighted_quantile: Weighted Quantile

View source: R/weighted_quantile.R

weighted_quantileR Documentation

Weighted Quantile

Description

Weighted population quantile.

Usage

weighted_quantile(x, w, probs, na.rm = FALSE)

Arguments

x

[numeric vector] data.

w

[numeric vector] weights (same length as x).

probs

[numeric vector] vector of probabilities with values in [0,1].

na.rm

[logical] indicating whether NA values should be removed before the computation proceeds (default: FALSE).

Details

Overview.

weighted_quantile computes the weighted sample quantiles; argument probs allows vector inputs.

Implementation.

The function is based on a weighted version of the quickselect/Find algorithm with the Bentley and McIlroy (1993) 3-way partitioning scheme. For very small arrays, we use insertion sort.

Compatibility.

For equal weighting, i.e., when all elements in w are equal, weighted_quantile is identical with type = 2 of stats::quantile; see also Hyndman and Fan (1996).

Value

Weighted estimate of the population quantiles

References

Bentley, J. L. and McIlroy, D. M. (1993). Engineering a Sort Function, Software - Practice and Experience 23, 1249–1265. doi: 10.1002/spe.4380231105

Hyndman, R.J. and Fan, Y. (1996). Sample Quantiles in Statistical Packages, The American Statistician 50, 361–365. doi: 10.1080/00031305.1996.10473566

See Also

Overview (of all implemented functions)

weighted_median

Examples

data(workplace)

# Weighted 25% quantile (1st quartile)
weighted_quantile(workplace$employment, workplace$weight, 0.25)

robsurvey documentation built on Jan. 6, 2023, 5:09 p.m.