wmedian | R Documentation |
This calculates the weighted median of a vector x
using the weights in w
. Weights are re-scaled based on their sum.
wmedian(x, w = rep(1, length(x)))
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. |
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
.
numeric. The weighted median of x
using w
as the weights.
This is not compatible with complex data.
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.
stats::median
xx <- rnorm(10, 4L, 1.5)
ww <- runif(10)
wmedian(xx, ww)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.