Compute Weighted Percentiles or Share of Distribution
compute_weighted_percentiles.Rd
This function computes weighted percentiles of a numeric vector, or the share of total value within specified percentile intervals.
Arguments
- var
A numeric vector of values (e.g., .x$dhi, .x$pi11).
- wgt
A numeric vector of weights (e.g., .x$hpopwgt, .x$pwgt). Must be the same length as
x
.- probs
A numeric vector of probabilities between 0 and 1 indicating which percentiles to compute. Default is
seq(0, 1, 0.25)
.- na.rm
Logical; if
TRUE
, missing values inx
andw
are removed before computation. Default isTRUE
.Logical; if
TRUE
, computes the share of total value (e.g., .x$dhi) within each interval defined byprobs
. IfFALSE
, returns the percentile values. Default isFALSE
. Note: This option always usestype = "type_4"
, and can not be used toghether withtype = "type_2"
.- type
A character string indicating which percentile definition to use. #' Either
"type_4"
(default, linear interpolation-based of the empirical cdf - continuous sample quantile) or"type_2"
(used in Stata commands like collapse and _pctile, inverse of empirical distribution function with averaging at discontinuities - discontinuous sample quantile).
Value
A named numeric vector. If share = FALSE
, returns weighted percentiles with names corresponding to the percentiles (e.g., "25%"). If share = TRUE
, returns the share of the total value in each percentile range (e.g., "0-25%").
Examples
if (FALSE) { # \dontrun{
data <- lissyrtools::lissyuse(data = "es22", vars = c("dhi", "age"))
compute_weighted_percentiles(data$es22$dhi, data$es22$ppopwgt)
compute_weighted_percentiles(data$es22$dhi, data$es22$ppopwgt, probs = c(0.03, 0.72, 0.48, .01))
compute_weighted_percentiles(data$es22$dhi, data$es22$ppopwgt, probs = c(0.03, 0.72, 0.48, .01), share = TRUE)
} # }