[Rd] rounding errors in max.col()

ripley@stats.ox.ac.uk ripley@stats.ox.ac.uk
Thu Jan 2 14:41:02 2003


Do read the help page:

     Ties are broken at random.  The determination of ``tie'' assumes
     that the entries are probabilities.

and then don't blame your tools when you misuse them ....

On Thu, 2 Jan 2003, Philippe Grosjean wrote:

> I suppose this is a general behavior with external function calls, so I do
> not post (yet) a specific bug report. Could someone explain this?
>
> a <- rep(1, 20) + rnorm(20, mean=0.00001, sd=0.0001)
> b <- embed(a, 3)
> # I want to know where the item in column 2 is greated than both col 1 and 3
> (peak)
> test1 <- max.col(b) == 2
> # ... or I could use a less optimal code
> test2 <- apply(b, 1, max) == b[, 2]
> any(test1 != test2)	# both are equivalent
>
> # but when numbers are very close
> a <- rep(1, 20) + rnorm(20, mean=0.0000001, sd=0.0000001)
> b <- embed(a, 3)
> test1 <- max.col(b) == 2
> test2 <- apply(b, 1, max) == b[, 2]
> any(test1 != test2)	# tests are now DIFFERENT!
> # Indeed, test2 is correct, and test1 suffers from wrong calculations due
> probably to rounding errors in max.col()

No, to max.col working as documented.

[Large waste of bandwidth deleted]

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595