[R] More on ties with rank()

arcriswell arc at arcriswell.com
Tue May 7 09:14:36 CEST 2002


I am grateful to Prof. Ripley for his explanation. Indeed, rounding explains
it all.

I take the difference between two vectors and call it "z"

method.a <- c(6.3, 6.3, 3.5, 5.1, 5.5, 7.7, 6.3, 2.8, 3.4, 5.7, 5.6, 6.2,
6.6,
                       7.7, 7.4, 5.6, 6.3, 8.4, 5.6, 4.8, 4.3, 4.2, 3.3,
3.8, 5.7, 4.1)

method.b <- c(5.2, 6.6, 2.3, 4.4, 4.1, 6.4, 5.4, 2.3, 3.2, 5.2, 4.9, 6.1,
6.3,
                       7.4, 7.4, 4.9, 5.4, 8.4, 5.1, 4.4, 4.3, 4.1, 2.2,
4.0, 5.8, 4.0)

z <- sort(abs(method.a - method.b))

If I do as suggested, and apply dput(z), the rounding error shows its ugly
head.

> dput(z)
c(0.0999999999999996, 0.0999999999999996, 0.100000000000001,
0.100000000000001, 0.200000000000000, 0.2, 0.3, 0.3, 0.3, 0.399999999999999,
0.5, 0.5, 0.5, 0.699999999999999, 0.699999999999999, 0.699999999999999,
0.9, 0.9, 1.1, 1.1, 1.2, 1.3, 1.4)
>

Now, if I define a new variable rounding off the errors, I get the correct
response.

> k <- round(dput(z),1)
c(0.0999999999999996, 0.0999999999999996, 0.100000000000001,
0.100000000000001, 0.200000000000000, 0.2, 0.3, 0.3, 0.3, 0.399999999999999,
0.5, 0.5, 0.5, 0.699999999999999, 0.699999999999999, 0.699999999999999,
0.9, 0.9, 1.1, 1.1, 1.2, 1.3, 1.4)

> rank(k)
 [1]  2.5  2.5  2.5  2.5  5.5  5.5  8.0  8.0  8.0 10.0 12.0 12.0 12.0 15.0
15.0
[16] 15.0 17.5 17.5 19.5 19.5 21.0 22.0 23.0
>

But it strikes me as odd, that such an innocent calculation could lead to
such a significant miscalculation based on rounding error.

Thanks,
ANDREW



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list