Timur Shtatland
2008-Sep-10 20:04 UTC
[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:> a <- c(0, 1, 2) > b <- c(100, 2, 4)## error:> log(a)[1] -Inf 0.0000000 0.6931472 ## error, as expected:> cor(log(a), log(b), method="pearson")[1] NaN ## not an error any more (not expected):> cor(log(a), log(b), method="spearman")[1] -0.5> cor(log(a), log(b), method="spearman", use="all.obs")[1] -0.5> sessionInfo()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
milton ruser
2008-Sep-10 22:17 UTC
[R] making spearman correlation cor() call fail with log(0) as input
Hi Timur, try cor(log(a+1), log(b+1), method="pearson") HTH, miltinho brazil On Wed, Sep 10, 2008 at 6:04 PM, Timur Shtatland <tshtatland@gmail.com>wrote:> 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: > > > a <- c(0, 1, 2) > > b <- c(100, 2, 4) > > ## error: > > log(a) > [1] -Inf 0.0000000 0.6931472 > > ## error, as expected: > > cor(log(a), log(b), method="pearson") > [1] NaN > > ## not an error any more (not expected): > > cor(log(a), log(b), method="spearman") > [1] -0.5 > > cor(log(a), log(b), method="spearman", use="all.obs") > [1] -0.5 > > > sessionInfo() > 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@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. >[[alternative HTML version deleted]]
Timur Shtatland
2008-Sep-10 23:29 UTC
[R] making spearman correlation cor() call fail with log(0) as input
Hi Miltinho, 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. Timur On Wed, Sep 10, 2008 at 6:17 PM, milton ruser <milton.ruser at gmail.com> wrote:> Hi Timur, > try > cor(log(a+1), log(b+1), method="pearson") > > HTH, > miltinho > brazil > > On Wed, Sep 10, 2008 at 6:04 PM, Timur Shtatland <tshtatland at gmail.com> > wrote: >> >> 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: >> >> > a <- c(0, 1, 2) >> > b <- c(100, 2, 4) >> >> ## error: >> > log(a) >> [1] -Inf 0.0000000 0.6931472 >> >> ## error, as expected: >> > cor(log(a), log(b), method="pearson") >> [1] NaN >> >> ## not an error any more (not expected): >> > cor(log(a), log(b), method="spearman") >> [1] -0.5 >> > cor(log(a), log(b), method="spearman", use="all.obs") >> [1] -0.5 >> >> > sessionInfo() >> 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. > >
Greg Snow
2008-Sep-11 00:50 UTC
[R] making spearman correlation cor() call fail with log(0) as input
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:> a <- c(0, 1, 2) > b <- c(100, 2, 4)## error:> log(a)[1] -Inf 0.0000000 0.6931472 ## error, as expected:> cor(log(a), log(b), method="pearson")[1] NaN ## not an error any more (not expected):> cor(log(a), log(b), method="spearman")[1] -0.5> cor(log(a), log(b), method="spearman", use="all.obs")[1] -0.5> sessionInfo()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.
Maybe Matching Threads
- Wrong result with cor(x, y, method="spearman", use="complete.obs") with NA's???
- cor(..., method="spearman") or cor(..., method="kendall") (PR#6641)
- about spearman and kendal correlation coefficient calculation in "cor"
- Bug with Cor(..., method='spearman") and by() (PR#9921)
- cor() function, method="spearman"