[R] making spearman correlation cor() call fail with log(0) as input
tshtatland at gmail.com
Thu Sep 11 01:29:13 CEST 2008
Thank you for the reply. I know that this is one way to stop
cor(log()...) from failing.
However, my question was exactly the opposite: how to *make* it fail.
cor() silently succeeds and even returns a value even when the input
to it fails on its own (e.g., log(0) fails). Note that this only
happens with Spearman, rather than Pearson correlation.
This behavior may be inconvenient when one wants the code to fail
loudly when it encounters an error, rather than happily proceed.
If such behavior is okay for many users, at least I would like to be
able to optionally turn it off.
In the toy example I showed, the alternative is to create the input
separately, rather than on-the-fly inside cor(). This takes care of
failing, but may not be very efficient or elegant.
On Wed, Sep 10, 2008 at 6:17 PM, milton ruser <milton.ruser at gmail.com> wrote:
> Hi Timur,
> cor(log(a+1), log(b+1), method="pearson")
> On Wed, Sep 10, 2008 at 6:04 PM, Timur Shtatland <tshtatland at gmail.com>
>> 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
>> > a <- c(0, 1, 2)
>> > b <- c(100, 2, 4)
>> ## error:
>> > log(a)
>>  -Inf 0.0000000 0.6931472
>> ## error, as expected:
>> > cor(log(a), log(b), method="pearson")
>>  NaN
>> ## not an error any more (not expected):
>> > cor(log(a), log(b), method="spearman")
>>  -0.5
>> > cor(log(a), log(b), method="spearman", use="all.obs")
>>  -0.5
>> > sessionInfo()
>> R version 2.6.1 (2007-11-26)
>> attached base packages:
>>  stats graphics grDevices utils datasets methods base
>> Thank you for your help.
>> Best regards,
>> Timur Shtatland
>> R-help at r-project.org mailing list
>> PLEASE do read the posting guide
>> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help