This calculates the weighted median of a vector x using the weights in w. Weights are re-scaled based on their sum.
Usage
wmedian(x, w = rep(1, length(x)))
Value
numeric. The weighted median of x using w as the weights.
Arguments
x
numeric, a vector containing the data from which to calculate the weighted median.
w
numeric, a vector of weights to give the data in x.
Details
Sorts x and w by size of the elements of x. Then re-scales the elements of w to be between 0 and 1.
Then sets n equal to the sum of all scaled weights with values less than 0.5. If the (n+1)-th element of the
rescaled weights is greater than 0.5, the weighted median is the (n+1)-th element of the sorted x. Otherwise
it is the average of the (n+1)-th and (n+2)-th elements of the sorted x.
References
F. Y. Edgeworth, XXII. On a New Method of Reducing Observations Relating to Several Quantities, (1888).
Also see the Wikipedia article on weighted median for a very good explanation and a model algorithm.