Why am I getting different results for Pearson and Spearman correlation, although I am already comparing ranks?

a <- c(1,2,3,1,2,3)
b <- c(1,2,2,1,2,3)
cor(a, b, method="pearson")
# [1] 0.8911328
cor(a, b, method="spearman")
# [1] 0.9036961

Formula to use when there are tied ranks seems identical to formula for pearson correlation.

As far as I understand, spearman correlation is just pearson correlation on the ranks. Right?

R function documentation

  • 2
    $\begingroup$ your inputs are not ranks in the usual meaning of the term? $\endgroup$ – mdewey Jun 4 '18 at 14:57
  • $\begingroup$ What would be the corresponding ranks if they are not? $\endgroup$ – PalimPalim Jun 4 '18 at 14:57
  • 1
    $\begingroup$ Try cor(rank(a), rank(b), method = ...) $\endgroup$ – mdewey Jun 4 '18 at 15:02
  • $\begingroup$ Consider the data in a as if it represented positions in a race among six people: two people tied for each position. There are various ways to assign ranks with ties, but typically the ranks that would be given to the top two places--namely 1 and 2--would be averaged, giving a rank of 1.5 to the first two finishers. Similarly, the next two would have equal ranks of 3.5 and the last two would have ranks of 5.5. $\endgroup$ – whuber Jun 4 '18 at 15:03
  • $\begingroup$ thanks, i tried it with the rank function and I saw how R assigns ranks differently from what I expected. $\endgroup$ – PalimPalim Jun 4 '18 at 15:07

As pointed out by @mdewey I am not already using ranks. If I use the correct ranks results are identical as expected.

a <- c(1,2,3,3)
b <- c(1,2,2,1)
cor(a, b, method="spearman")
# [1] 0.2357023
# [1] 1.0 2.0 3.5 3.5
# [1] 1.5 3.5 3.5 1.5
cor(rank(a), rank(b), method="spearman")
# [1] 0.2357023

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.