weighted_ecdf function

Weighted empirical cumulative distribution function

Weighted empirical cumulative distribution function

A variation of ecdf() that can be applied to weighted samples.

weighted_ecdf(x, weights = NULL, na.rm = FALSE)

Arguments

  • x: numeric vector: sample values

  • weights: Weights for the sample. One of:

    • numeric vector of same length as x: weights for corresponding values in x, which will be normalized to sum to 1.
    • NULL: indicates no weights are provided, so the unweighted empirical cumulative distribution function (equivalent to ecdf()) is returned.
  • na.rm: logical: if TRUE, corresponding entries in x and weights

    are removed if either is NA.

Returns

weighted_ecdf() returns a function of class "weighted_ecdf", which also inherits from the stepfun() class. Thus, it also has plot() and print()

methods. Like ecdf(), weighted_ecdf() also provides a quantile() method, which dispatches to weighted_quantile().

Details

Generates a weighted empirical cumulative distribution function, F(x)F(x). Given xx, a sorted vector (derived from x), and wiw_i, the corresponding weight for xix_i, F(x)F(x) is a step function with steps at each xix_i

with F(xi)F(x_i) equal to the sum of all weights up to and including wiw_i.

Examples

weighted_ecdf(1:3, weights = 1:3) plot(weighted_ecdf(1:3, weights = 1:3)) quantile(weighted_ecdf(1:3, weights = 1:3), 0.4)

See Also

weighted_quantile()