Skip to content
Prev 15251 / 63424 Next

bug? quantile() can return decreasing sample quantiles for increasing probabilities

On Tue, 22 Feb 2005 13:43:47 -0700, Tony Plate
<tplate@blackmesacapital.com> wrote :
I'd say it's not a bug in that rounding error is something you should
expect in a calculation like this, but it does look wrong.  And it
isn't only restricted to type 7.  If you make a vector of copies of
that bad value, you'll see it in more cases:
+ type=type))<0), "\n")
1 FALSE 
2 FALSE 
3 FALSE 
4 FALSE 
5 TRUE 
6 TRUE 
7 TRUE 
8 FALSE 
9 TRUE 

(at least on Windows).  What's happening is that R is doing linear
interpolation between two equal values, and not getting the same value
back, because of rounding.  

The offending line appears to be this one:

qs[i] <- ifelse(h == 0, qs[i], (1 - h) * qs[i] + h * x[hi[i]])

The equivalent calculation in the approx function (which doesn't
appear to have this problem) is

qs[i] + (x[hi[i]] - qs[i]) * h

Can anyone think of why this would not be better?  (The same sort of
calculation shows up again later in quantile().)

Duncan Murdoch