# [R] S in cor.test(..., method="spearman")

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Wed Sep 13 17:31:12 CEST 2006

```
Dietrich Trenkler said the following on 9/13/2006 9:44 AM:
> Dear HelpeRs,
>
> I have some data:
>
> "ice" <- structure(c(0.386, 0.374, 0.393, 0.425, 0.406, 0.344,
>     0.327, 0.288, 0.269, 0.256, 0.286, 0.298, 0.329, 0.318, 0.381,
>     0.381, 0.47, 0.443, 0.386, 0.342, 0.319, 0.307, 0.284, 0.326,
>     0.309, 0.359, 0.376, 0.416, 0.437, 0.548, 41, 56, 63, 68,
>     69, 65, 61, 47, 32, 24, 28, 26, 32, 40, 55, 63, 72, 72, 67,
>     60, 44, 40, 32, 27, 28, 33, 41, 52, 64, 71), .Dim = as.integer(c(30,
>     2)))
>
> Using
>
>     cor.test(ice[,1],ice[,2],method="spearman")
>
> I get (apart from a warning message due to ties)
>
>         Spearman's rank correlation rho
>
> data:  ice[, 1] and ice[, 2]
> S = 769.4403, p-value = 1.543e-08
> alternative hypothesis: true rho is not equal to 0
> sample estimates:
>      rho
> 0.828823
>
> I wonder what S is. I presume it is
>
> sum((rank(ice[,1])-rank(ice[,2]))^2),
>
> but this delivers  768.5. Is it the way ranks are computed in cor.test?
>
>
>
> D. Trenkler
>

Looking at the code will help. Try

stats:::cor.test.default

This reveals that S is determined by:

x <- ice[, 1]
y <- ice[, 2]
n <- nrow(ice)
r <- cor(rank(x), rank(y))
S <- (n^3 - n) * (1 - r)/6
S
## [1] 769.4403

See ?cor.test as to definition of "S".

HTH,

--sundar

```