Compute the median, quantiles or variance of a set of numbers which have weights associated with them.

`weighted.median(x, w, na.rm = TRUE, type=2, collapse=TRUE)`weighted.quantile(x, w, probs=seq(0,1,0.25), na.rm = TRUE, type=4, collapse=TRUE)

weighted.var(x, w, na.rm = TRUE)

A numeric value or vector.

- x
Data values. A vector of numeric values, for which the median or quantiles are required.

- w
Weights. A vector of nonnegative numbers, of the same length as

`x`

.- probs
Probabilities for which the quantiles should be computed. A numeric vector of values between 0 and 1.

- na.rm
Logical. Whether to ignore

`NA`

values.- type
Integer specifying the rule for calculating the median or quantile, corresponding to the rules available for

`quantile`

. The only valid choices are`type=1`

,`2`

or`4`

. See Details.- collapse
Research use only.

Adrian Baddeley Adrian.Baddeley@curtin.edu.au.

The `i`

th observation `x[i]`

is treated as having
a weight proportional to `w[i]`

.

The weighted median is a value `m`

such that the total weight of data less than or equal to `m`

is equal to half the total weight. More generally, the weighted quantile with
probability `p`

is a value `q`

such that the total weight of data less than or equal to `q`

is equal to `p`

times the total weight.

If there is no such value, then

if

`type=1`

, the next largest value is returned (this is the right-continuous inverse of the left-continuous cumulative distribution function);if

`type=2`

, the average of the two surrounding values is returned (the average of the right-continuous and left-continuous inverses);if

`type=4`

, linear interpolation is performed.

Note that the default rule for `weighted.median`

is
`type=2`

, consistent with the traditional definition of the median,
while the default for `weighted.quantile`

is `type=4`

.

```
x <- 1:20
w <- runif(20)
weighted.median(x, w)
weighted.quantile(x, w)
weighted.var(x, w)
```

Run the code above in your browser using DataLab