# Calculating interquartile range

I have the following numbers:

$$\{0, 1, 2, 5, 8, 8, 9, 10, 12, 14, 18, 20, 21, 23, 25, 27, 34, 43\}$$

and need to calculate the IQR. My calculations gave me:

$$18/4=4.5$$ $$Q1=(5+8)/2=6.5$$ $$Q2=(12+14)/2=24$$ $$Q3=(23+25)/2=24$$ $$IQR=Q3-Q1=24-6.5=17.5$$

The book says they're:

$$Q1=7.25, Q2=13, Q3=23.5, IQR=16.25$$

and Wolfram|Alpha gives:

$$Q1=8, Q3=23, IQR=15$$

Could someone please explain all these discrepancies?

-

As the varied answers indicate, extracting quantiles is something of an inexact science when you have few enough samples that the rounding between two neighbor samples matters.

A bit abstractly expressed, you have tabulated values for $f(1)$, $f(2)$, ... $f(18)$, but getting from there to actual quartiles requires at least two semi-arbitrary choices:

1. How do we define values of $f$ for non-integral arguments when "a quarter way through the sample set" happens not to hit one particular sample exactly? Linear interpolation between neighbor samples is a popular choice, but it seems that Wolfram Alpha instead extends $f$ to a step function. Even step functions can be done in different ways: round up? round down? round to nearest? In the latter case, what about the point exactly halfway between samples?

2. What is actually the interval that we want to find quarter-way points in? One natural choice is $[1,18]$, which makes the zeroth and fourth quartile exactly the minimum and maximum. But a different natural choice is $[0.5, 18.5]$ such that each sample counts for the same amount of x-axis. In the latter case there is a risk that one will have to find $f(x)$ for $x<1$ or $x>18$, where a linear interpolation does not make sense. More decisions to make then.

It looks like your book is using yet a third interval, namely $[0, 19]$! Then, by linear interpolation, we get $$Q1 = f(4.75) = 5+0.75\times(8-5) = 7.25$$ $$Q3 = f(14.25) = 23+0.25\times(25-23) = 23.5$$

I'm not sure how you get your own suggestions for quartiles. Since you divide 18 by 4, I assume you use an interval of length 18, but if you're using linear interpolation, you compute Q1 as $f(4.5)$ and Q2 as $f(9.5)$, with a distance of only 4 rather than 4.5. Or are you completing $f$ such that every non-integral $x$ maps to the midpoint between neighbor samples?

-

Let's not confuse you with so much theories. Just calculate according to these steps:

1. find the position of the Q1 and Q3

Q1 = (n+1)/4

Q3 = 3(n+1)/4

Q1 = (18+1)/4 = 4.75

Q3 = 3(18+1)/4 = 14.25

1. Now what you get from above is just the position

{0, 1, 2, 5, 8, 8, 9, 10, 12, 14, 18, 20, 21, 23, 25, 27, 34, 43}

4.75 falls between 5 and 8

14.25 falls between and 23 and 25

1. Now you interpolate using this formula

Q1 = 5 + 3/4(8-5) = 7.25 explanation: - 5 is the lower part taken from 5 and 8 (where the 4.75 falls within) - 3/4 is the 4.75 (convert from 0.75) - 8-5 is the 5 and 8 you got from previous step

Q3 = 23 + 1/4(25-23) = 23.5

-

I have run some commands in R to easily determine which answer is correct. Well:

> x <- c(0, 1, 2, 5, 8, 8, 9, 10, 12, 14, 18, 20, 21, 23, 25, 27, 34, 43)
> summary(x)
Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
0.0     8.0    13.0    15.6    22.5    43.0
> IQR(x)
[1] 14.5


So we have a forth solution :)

How is it possible? Checking the R help (?quantile) is really helpful:

 All sample quantiles are defined as weighted averages of
consecutive order statistics. Sample quantiles of type i are
defined by:

Q[i](p) = (1 - gamma) x[j] + gamma x[j+1],

where 1 <= i <= 9, (j-m)/n <= p < (j-m+1)/n, x[j] is the jth order
statistic, n is the sample size, the value of gamma is a function
of j = floor(np + m) and g = np + m - j, and m is a constant
determined by the sample quantile type.

*Discontinuous sample quantile types 1, 2, and 3*

For types 1, 2 and 3, Q[i](p) is a discontinuous function of p,
with m = 0 when i = 1 and i = 2, and m = -1/2 when i = 3.

Type 1 Inverse of empirical distribution function.  gamma = 0 if g
= 0, and 1 otherwise.

Type 2 Similar to type 1 but with averaging at discontinuities.
gamma = 0.5 if g = 0, and 1 otherwise.

Type 3 SAS definition: nearest even order statistic.  gamma = 0 if
g = 0 and j is even, and 1 otherwise.
*Continuous sample quantile types 4 through 9*

For types 4 through 9, Q[i](p) is a continuous function of p, with
gamma = g and m given below. The sample quantiles can be obtained
equivalently by linear interpolation between the points
(p[k],x[k]) where x[k] is the kth order statistic.  Specific
expressions for p[k] are given below.

Type 4 m = 0. p[k] = k / n.  That is, linear interpolation of the
empirical cdf.

Type 5 m = 1/2.  p[k] = (k - 0.5) / n.  That is a piecewise linear
function where the knots are the values midway through the
steps of the empirical cdf.  This is popular amongst
hydrologists.

Type 6 m = p. p[k] = k / (n + 1).  Thus p[k] = E[F(x[k])].  This
is used by Minitab and by SPSS.

Type 7 m = 1-p.  p[k] = (k - 1) / (n - 1).  In this case, p[k] =
mode[F(x[k])].  This is used by S.

Type 8 m = (p+1)/3.  p[k] = (k - 1/3) / (n + 1/3).  Then p[k] =~
median[F(x[k])].  The resulting quantile estimates are
approximately median-unbiased regardless of the distribution
of ‘x’.

Type 9 m = p/4 + 3/8.  p[k] = (k - 3/8) / (n + 1/4).  The
resulting quantile estimates are approximately unbiased for
the expected order statistics if ‘x’ is normally distributed.


So each program was using a different quantile algorithm.

-