I was writing an R Markdown for calculating measures of spread and was surprised to find that the IQR
function does not use the IQR calculation I am used to using, which is:
$$ \text{IQR} = \text{Q3} - \text{Q1} $$
So with a vector like this:
$$ x = \begin{bmatrix} 10,33,50,341,987,2006,2008 \end{bmatrix} $$
Q3 should be $2006$ and Q1 should be $33$. Therefore:
$$ \text{IQR} = 2006 - 33 = 1973 $$
However, when running this in R, this does not appear to be how it is derived in the software.
x7 <- c(10,33,50,341,987,2006,2008)
IQR(x7)
Which returns this:
[1] 1455
Looking through the help page, it gives this brief explanation:
Note that this function computes the quartiles using the quantile function rather than following Tukey's recommendations, i.e., IQR(x) = quantile(x, 3/4) - quantile(x, 1/4).
For normally N(m,1)N(m,1) distributed XX, the expected value of IQR(X) is 2*qnorm(3/4) = 1.3490, i.e., for a normal-consistent estimate of the standard deviation, use IQR(x) / 1.349.
My question is: why would this method be the preferred default if a lot of data is not normally distributed?
fivenum
. $\endgroup$fivenum
today. $\endgroup$hspread<-function(x) {f<-fivenum(x);f[4]-f[2]}
$\endgroup$