Spearman's rank correlation test with improved accuracy
This function is a modified copy of the part of the function cor.test(), which evaluates Spearman's rank correlation test. There are two modifications described below, which improve the accuracy of the p-value.
spearman.test(x, y, alternative = c("two.sided", "less", "greater"), method = "spearman", exact = NULL, conf.level = 0.95, ...)
- x, y, alternative, exact, conf.level, ...
- have exactly
the same meaning as in
cor.test. See the corresponding help page.
- defaults to
"spearman"and this should not be changed.
In cases, where the correlation is positive, AS 89 is called with S+2 instead
of S+1, which is used in
cor.test. Since AS 89 assumes an even input,
this increases accuracy.
cor.test uses AS 89 only if
n <= 1290<="" code=""> and less
accurate approximation using Student's t distribution otherwise.
The code of
cor.test contains a comment
n*(n^2 - 1) does not overflow
at the line with the test
n <= 1290<="" code="">, which possibly tries to explain the reason
for this restriction. However, this comment is probably related to older versions
of the code. Currently, =>
n is used in the C code after conversion to
double and no integer overflow occurs for
n > 1290.
Hence, the function
spearman.test uses AS 89 in all cases, unless the
user explicitly specifies
- A list with class
"htest"with the same structure as the value of the function
cor.test(method="spearman"). Except of the p-value, also the contents is identical.
- Spearman correlation coefficient
- Spearman's rho
x <- 1:10 y <- c(5:1,6,10:7) out1 <- cor.test(x,y,method="spearman") out2 <- spearman.test(x,y) # out1$p.value = 0.05169460 # out2$p.value = 0.05444507 # the correct p-value in this case is 0.05443067 c(out1$p.value, out2$p.value) out2$p.value <- out1$p.value # except of the p-value, the output is identical identical(out1,out2) # for negative correlation, there is no difference out3 <- cor.test(x,-y,method="spearman") out4 <- spearman.test(x,-y) identical(out3,out4)