Re: [R] R's Spearman

From: Chung-hong Chan <chainsawtiney_at_gmail.com>
Date: Tue, 29 May 2007 11:38:15 +0800

Hi,

You can try with
cor.test(rank(y[1]),rank(y[2]))

On 5/29/07, Raymond Wan <rwan_at_kuicr.kyoto-u.ac.jp> wrote:
>
> Hi all,
>
> I am trying to figure out the formula used by R's Spearman rho (using
> cor(method="spearman")) because I can't seem to get the same value as by
> calculating "by hand". Perhaps I'm using "cor" wrong, but I don't know
> where. Basically, I am running these commands:
>
> > y=read.table(file="tmp",header=TRUE,sep="\t")
> > y
> IQ Hours
> 1 106 7
> 2 86 0
> 3 97 20
> 4 113 12
> 5 120 12
> 6 110 17
> > cor(y[1],y[2],method="spearman")
> Hours
> IQ 0.2319084
>
> [it's an abbreviated example of one I took from Wikipedia]. I
> calculated by hand (apologies if the table looks strange when pasted
> into e-mail):
>
> IQ Hours rank(IQ) rank(hours) diff diff^2
> 1 106 7 3 2 1 1
> 2 86 0 1 1 0 0
> 3 97 20 2 6 -4 16
> 4 113 12 5 3.5 1.5 2.25
> 5 120 12 6 3.5 2.5 6.25
> 6 110 17 4 5 -1 1
> 26.5
>
> rho= 0.242857
>
> where rho = (1 - ((6 * 26.5) / 6 * (6^2 - 1))). I kept modifying the
> table and realized that the difference in result comes from ties. i.e.,
> if I remove the tie in rows 4 and 5, I get the same result from both cor
> and calculating by hand. Perhaps I'm handling ties wrong...does anyone
> know how R does it or perhaps I need to change how I'm using it?
>
> Thank you!
>
> Ray
>
> ______________________________________________
> R-help_at_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
> and provide commented, minimal, self-contained, reproducible code.
>

-- 
"The scientists of today think deeply instead of clearly. One must be
sane to think clearly, but one can think deeply and be quite insane."
Nikola Tesla
http://www.macgrass.com

______________________________________________
R-help_at_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
and provide commented, minimal, self-contained, reproducible code.
Received on Tue 29 May 2007 - 03:42:21 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 29 May 2007 - 06:31:15 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.