[R] Whiskers on the default boxplot {graphics}

Joshua Wiley jwiley.psych at gmail.com
Thu May 13 19:36:00 CEST 2010


On Thu, May 13, 2010 at 7:55 AM, David Winsemius <dwinsemius at comcast.net> wrote:
> Yes, and experimentation leads me to the conclusion that the only possible
> candidate for matching up the results of fivenum[c(2,4]  with quantile(y,
> c(1,3)/4, type=i) is for type=5. I'm not able to prove that to myself from
> mathematical arguments. since I do not quite understand the formalism in the
> quantile page. If the match is not exact, this would be a tenth definition
> of IQR.

David,

Here is some sample data, and the most parsimonious code I could come
up with for how quantile() computes the quartiles when using type=5.
The code for fivenum() seems simple enough, but I am not quite able to
make enough sense of the code for type=5 from quantile() to say
confidently why they are different.

I am open to the possibility that my attempts to extract relevant code
from quantile were flawed, but my tentative conclusion is that
quantile(x, type=5) != fivenum(x).

##########################
x <- c(0.643796386452606, -0.605277531056206, -0.339239367816402,
1.12408365699422, 0.615753476531243, -1.10545696568758,
0.666533406841698, 1.42794492209271, 0.624752921945051,
2.02317205214712, -0.365586657432646, 0.821742701084307,
-0.874753498321076, -0.0298783402061118, 1.18037670706428,
-0.178274986836195, 0.308703365439049, 0.619700844646392,
0.54977981430092, -1.82161514610448, -1.28413556650749,
-0.0443852992196351, 0.704196760556652, -1.88596816676741,
-0.420811351737096)
oldx <- x #this is just a backup because x will be transformed

##Start from quantile()
probs <- c(0, 0.25, 0.5, 0.75, 1)
type <- 5
n <- length(x)
switch(type - 3, {
  a <- 0
  b <- 1
}, a <- b <- 0.5, a <- b <- 0, a <- b <- 1, a <- b <- 1/3, a <- b <- 3/8)
fuzz <- 4 * .Machine$double.eps
nppm <- a + probs * (n + 1 - a - b)
j <- floor(nppm + fuzz)
h <- nppm - j
h <- ifelse(abs(h) < fuzz, 0, h)
x <- sort(x, partial = unique(c(1, j[j > 0L & j <= n], (j + 1)[j > 0L
& j < n], n)))
x <- c(x[1L], x[1L], x, x[n], x[n])
qs <- x[j + 2L]
qs[h == 1] <- x[j + 3L][h == 1]
other <- (h > 0) && (h < 1)
if (any(other)) qs[other] <- ((1 - h) * x[j + 2L] + h * x[j + 3L])[other]
##End from quantile

qs # from the calculations above
quantile(oldx, type=5) #this should match qs
fivenum(oldx) #the 25% does not match
############################################

<everything else snipped>

Josh



More information about the R-help mailing list