Skip to content

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

6 messages · Greg Snow, Martin Maechler, Timur Shtatland

#
Your code shows no errors only correct responses.

If you want an error when R is generating correct results, you will need to check for the conditions yourself.

-----Original Message-----
From: "Timur Shtatland" <tshtatland at gmail.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Sent: 9/10/08 3:51 PM
Subject: [R] making spearman correlation cor() call fail with log(0) as input


Hi,

How can I make the cor(x, y, method="spearman") call to produce an
error when the input to it (x, y) produces an error? Here is a simple
example:
## error:
[1]      -Inf 0.0000000 0.6931472

## error, as expected:
[1] NaN

## not an error any more (not expected):
[1] -0.5
[1] -0.5
R version 2.6.1 (2007-11-26)
i686-pc-linux-gnu

locale:
LC_CTYPE=en_US;LC_NUMERIC=C;LC_TIME=en_US;LC_COLLATE=en_US;LC_MONETARY=en_US;LC_MESSAGES=en_US;LC_PAPER=en_US;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US;LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base
Thank you for your help.

Best regards,

Timur Shtatland

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
#
You are right, Inf and -Inf are not considered errors in R, they are
accepted as input to Spearman's cor(), and so I will have to check the
input myself for such condition.
Thank you for pointing this out!

Timur
On Wed, Sep 10, 2008 at 8:50 PM, Greg Snow <Greg.Snow at imail.org> wrote:
#
Just out of curiosity, why do you want the spearman rank correlation to error in this case?

One of the advantages of the spearman correlation is that it is invariant to monotone transformations, so most people that use it see the fact that corr(x,y, method='spearman') gives the same answer as cor(log(x), exp(y), method='spearman') to be the desired result.

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
(801) 408-8111
#
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)
1 day later