[prev in list] [next in list] [prev in thread] [next in thread]
List: r-help
Subject: Re: [R] a problem 'cor' function
From: Peter Ehlers <ehlers () math ! ucalgary ! ca>
Date: 2006-05-31 22:49:18
Message-ID: 447E1D6E.80903 () math ! ucalgary ! ca
[Download RAW message or body]
Looks like another case of the most F of all FAQs: FAQ 7.31.
See if the following makes sense to you:
pl <- iris[101:150, 3]
all.equal(cor(pl,pl), 1)
[1] TRUE
cor(pl,pl) == 1
[1] FALSE
sprintf("%1.22g", cor(pl, pl))
[1] "0.99999999999999989"
sprintf("%1.22g", pl)
[1] "6" "5.0999999999999996" "5.9000000000000004"
etc
Peter Ehlers
Tao Shi wrote:
> 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
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
--
Peter Ehlers
Chair, Division of Statistics and Actuarial Science
Department of Mathematics and Statistics
University of Calgary, 2500 University Dr. NW ph: 403-220-3936
Calgary, Alberta T2N 1N4, CANADA fax: 403-282-5150
email: ehlers@math.ucalgary.ca
______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic