[R] Bug in cor.test - Spearman

Steve Roberts steve.roberts at man.ac.uk
Fri Nov 7 15:45:08 CET 2003


Greetings.

There seems to be a problem with the P-value computation in the 
cor.test with method="spearman". In R1.8.0  (MS Windows) I 
seem to be getting intermittently nonsense P-values, but the rho's 
are OK. I can get this reproducibly with the toy example attached 
where the first use is OK and subsequent calls with the same data 
give nonsense. (I have also seen the problem without the warning 
about duplicated values.) The toy example behaves correctly under 
1.7.1.

Steve.

---Script---
x<-1:100
y<-rep(c(2,3,4,5),25)
cor.test(x,y,m="p")
cor.test(x,y,m="s")
cor.test(x,y,m="s")
cor.test(x,y,m="k")

---Output--
R : Copyright 2003, The R Development Core Team
Version 1.8.0  (2003-10-08)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for a HTML browser interface to help.
Type 'q()' to quit R.

> x<-1:100
> y<-rep(c(2,3,4,5),25)
> cor.test(x,y,m="p")

        Pearson's product-moment correlation

data:  x and y 
t = 0.3837, df = 98, p-value = 0.702
alternative hypothesis: true correlation is not equal to 0 
95 percent confidence interval:
 -0.1588952  0.2333745 
sample estimates:
       cor 
0.03873177 

> cor.test(x,y,m="s")

        Spearman's rank correlation rho

data:  x and y 
S = 160195, p-value = 0.7015
alternative hypothesis: true rho is not equal to 0 
sample estimates:
       rho 
0.03873177 

Warning message: 
p-values may be incorrect due to ties in: cor.test.default(x, y, m = 
"s") 
> cor.test(x,y,m="s")

        Spearman's rank correlation rho

data:  x and y 
S = 160195, p-value = < 2.2e-16
alternative hypothesis: true rho is not equal to 0 
sample estimates:
       rho 
0.03873177 

Warning message: 
p-values may be incorrect due to ties in: cor.test.default(x, y, m = 
"s") 
> cor.test(x,y,m="k")

        Kendall's rank correlation tau

data:  x and y 
z = 0.5132, p-value = 0.6078
alternative hypothesis: true tau is not equal to 0 
sample estimates:
       tau 
0.03481553 

> 

  Dr Steve Roberts 
  steve.roberts at man.ac.uk

Senior Lecturer in Medical Statistics,
CMMCH NHS Trust and University of Manchester Biostatistics Group,
0161 275 5192 / 0161 276 5785




More information about the R-help mailing list