I've been getting unexpected results using the wtd.iqr
function from the reldist
package (version 1.6.6) to calculate a weighted interquartile range (as opposed to the unweighted interquartile range returned by IQR
from the vanilla R stats
package). To explore the problem, I tried comparing the output of reldist::wtd.iqr
to the output of IQR
.
To my surprise, I found that IQR
and reldist::wtd.iqr
return completely different output values for the same input values even when the input values are equally weighted (i.e. when weighting should make no difference).
> x <- rnorm(10000)
> wt <- rep(1, length(x))
> paste(c('IQR:', IQR(x), 'wtd.iqr:', reldist::wtd.iqr(x, weight = wt)))
[1] "IQR:" "1.34879539936654" "wtd.iqr:" "0.675866062623211"
>
In the above test, IQR
seems always to return an output value approximately double the value that wtd.iqr
returns for the same input values.
With input values that do not follow the above distribution, this relationship does not necessarily hold true: in fact, with real data, I sometimes get negative values from wtd.iqr
, which I would have assumed to be impossible, but have never found that to happen with IQR
.
In fact, it seems to me that wtd.iqr
may actually be returning not the interquartile range but one of the quartiles. But if there's a bug here, it surely can't be such an obvious one --- can it?
Presumably the two functions define the concept of interquartile range in a different way, but there is no clue in the documentation. The documentation for IQR
states that it 'computes interquartile range of the x values', while the documentation for wtd.iqr
states that it 'Returns an empirical interquartile range from a weighted sample'.