[R] a problem 'cor' function

Tao Shi shitao at hotmail.com
Wed May 31 23:48:48 CEST 2006


Hi list,

One of my co-workers found this problem with 'cor' in his code and I confirm it too (see below).  He's using R 2.2.1 under Win 2K and I'm using R 2.3.0 under Win XP.

===========================================
> R.Version()
$platform
[1] "i386-pc-mingw32"

$arch
[1] "i386"

$os
[1] "mingw32"

$system
[1] "i386, mingw32"

$status
[1] ""

$major
[1] "2"

$minor
[1] "3.0"

$year
[1] "2006"

$month
[1] "04"

$day
[1] "24"

$`svn rev`
[1] "37909"

$language
[1] "R"

$version.string
[1] "Version 2.3.0 (2006-04-24)"
> data(iris)
> cor(iris[1:4])
             Sepal.Length Sepal.Width Petal.Length Petal.Width
Sepal.Length       1.0000     -0.1176       0.8718      0.8179
Sepal.Width       -0.1176      1.0000      -0.4284     -0.3661
Petal.Length       0.8718     -0.4284       1.0000      0.9629
Petal.Width        0.8179     -0.3661       0.9629      1.0000
> cor(iris[1:4])==1
             Sepal.Length Sepal.Width Petal.Length Petal.Width
Sepal.Length         TRUE       FALSE        FALSE       FALSE
Sepal.Width         FALSE        TRUE        FALSE       FALSE
Petal.Length        FALSE       FALSE         TRUE       FALSE
Petal.Width         FALSE       FALSE        FALSE        TRUE
> cor(iris[1:4], iris[1:4])
             Sepal.Length Sepal.Width Petal.Length Petal.Width
Sepal.Length       1.0000     -0.1176       0.8718      0.8179
Sepal.Width       -0.1176      1.0000      -0.4284     -0.3661
Petal.Length       0.8718     -0.4284       1.0000      0.9629
Petal.Width        0.8179     -0.3661       0.9629      1.0000
> cor(iris[1:4], iris[1:4])==1
             Sepal.Length Sepal.Width Petal.Length Petal.Width
Sepal.Length         TRUE       FALSE        FALSE       FALSE
Sepal.Width         FALSE        TRUE        FALSE       FALSE
Petal.Length        FALSE       FALSE        FALSE       FALSE
Petal.Width         FALSE       FALSE        FALSE        TRUE
===========================================

The two ways of calculating correlation seem to generate the 'same' results, but the second one doesn't appear to be numerically stable (see the 3rd diagonal element of the last matrix).

thanks,

...Tao 

_________________________________________________________________
Join the next generation of Hotmail and you could win the adventure of a lifetime



More information about the R-help mailing list