[R] making spearman correlation cor() call fail with log(0) as input

Martin Maechler maechler at stat.math.ethz.ch
Sat Sep 13 14:48:32 CEST 2008


>>>>> "TS" == Timur Shtatland <tshtatland at gmail.com>
>>>>>     on Fri, 12 Sep 2008 11:52:25 -0400 writes:

    TS> I am more used to getting an error if you try to take
    TS> the log of 0, like this (in Perl):

    TS> perl -le 'for my $num (1, 0, -1, -2) { print log $num;
    TS> }' 0 Can't take log of 0 at -e line 1.

    TS> R is different. With R, you do not even get a *warning*
    TS> about log(0). Only log() of negative number produces a
    TS> warning:

  [............]

and why do you think the perl behavior to be better??
R has been very carefully designed in such matters:

The principle is that *limits* should work (using +/-Inf) were
possible. 
For log(.) the limit only exists from the right and clearly is
-Inf, so that's a feature.

BTW,  S/R behavior of  1/0 |--> Inf   could be considered as
more dangerous, since really the +Inf is the limit from the
right only with the limit from the left being ``quite
different''.
But no, I'm not proposing to change R here (and actually would
"fight" to keep it if that was necessary).


    TS> I agree with you that Spearman's correlation's invariance to monotone
    TS> transformations is an advantage. It is R's happy
    TS> attitude to -Inf and Inf that puzzled me at
    TS> first. Anyhow, verifying and/or preprocessing the input
    TS> to cor() is the answer to my questions.  Thank you again
    TS> for the help!

So you now have understood that R's behavior of handling +/- Inf
in this respect is rather  excellent  than bogous ?

Martin Maechler, ETH Zurich (and R-core team)



More information about the R-help mailing list