Spearman’s Rank Correlation

Rank the values first, then correlate the ranks.

A pattern that is not a straight line

Five plots are given more and more fertilizer, and the crop rises every time: 1, 5, 7, 8 and 8.5. The rise slows as the fertilizer increases, so the points curve and do not lie on a straight line.

Pearson’s r measures how close the points lie to a straight line. Here r = 0.933, short of 1, although every increase in fertilizer goes with an increase in crop. The relationship is perfect in one sense, the order, and r does not measure order.

fertilizercrop

Each point is higher than the one before it, but the rise gets smaller, so the points bend away from a straight line.

Ranks

The rank of a value is its place in size order: 1 for the smallest, then 2, 3 and so on. The values 12, 19, 15 and 30 have ranks 1, 3, 2 and 4, because in order they run 12, 15, 19, 30.

A rank keeps the order and throws away the gaps. 30 is far above 19, but its rank is only one more.

Correlate the ranks

Spearman’s rank correlation coefficient, rₛ, ranks each measurement separately, then measures how well the two rank columns agree. It is Pearson’s r worked out on the ranks instead of the values.

For the five plots, the fertilizer ranks are 1, 2, 3, 4, 5 and the crop ranks are 1, 2, 3, 4, 5 too, since the crop rises every time. Plotted against each other, the ranks lie exactly on a straight line, and rₛ = 1. Any pattern that rises at every step, curved or not, gives rₛ = 1, and any pattern that falls at every step gives rₛ = −1.

rank of fertilizerrank of crop

The same five plots, each value replaced by its rank. The curve has become the straight line through (1, 1) and (5, 5).

A formula from the differences

Six plants are given 10, 20, 30, 40, 50 and 60 g of fertilizer and grow to 12, 19, 17, 26, 30 and 28 cm. Rank both columns from the smallest. The fertilizer ranks are 1 to 6 in order. The heights in order are 12, 17, 19, 26, 28, 30, so the heights 12, 19, 17, 26, 30 and 28 have ranks 1, 3, 2, 4, 6 and 5.

For each plant, let d be its fertilizer rank minus its height rank: 1 − 1 = 0, 2 − 3 = −1, 3 − 2 = 1, 4 − 4 = 0, 5 − 6 = −1 and 6 − 5 = 1. Squared, they are 0, 1, 1, 0, 1 and 1, so Σd² = 4.

With n pairs, rₛ = 1 − 6Σd² / (n(n² − 1)). Here n = 6, so n(n² − 1) = 6 × 35 = 210, and rₛ = 1 − 24/210 = 1 − 0.114 = 0.886. When the two orders agree exactly, every d is 0 and rₛ = 1.

fertilizerheightrank xrank ydd²A10121100B201923−11C30173211D40264400E503056−11F60286511

The six plants, their ranks and the differences between them. The last column adds to 4.

rank of fertilizerrank of height

The six plants plotted by rank. Two pairs of neighbors are swapped, B with C and E with F, which is why rₛ is a little below 1.

Ties

Equal values share the places they cover, each taking the mean of those places. In 12, 15, 15 and 19, the two 15s cover the 2nd and 3rd places, so each takes (2 + 3)/2 = 2.5, and the ranks are 1, 2.5, 2.5 and 4.

The ranks still add to 1 + 2 + 3 + 4 = 10. Giving both 15s rank 2 leaves the total at 9, and giving each 5 pushes it to 15.

The formula 1 − 6Σd² / (n(n² − 1)) gives exactly Pearson’s r on the ranks only when there are no ties. For the six plants there are none, and Pearson’s r on the two rank columns is 0.886, the same value. With tied ranks the formula is an approximation, usually a close one: in the application below, with one tie among eight cakes, it gives 0.875, and Pearson’s r on the ranks gives 0.874.

What an outlier can do to a rank

Suppose plant E’s height is written down as 80 cm instead of 30 cm. Pearson’s r on the values falls from 0.922 to 0.581, because one point far above the others pulls the best straight line away from the rest.

E was already the tallest plant, so its rank is still 6, and every other rank is unchanged. Σd² is still 4 and rₛ is still 0.886. However large the mistake, the largest value cannot rank higher than n.

An outlier that changes the order does change the ranks. In five points whose crops are 2, 3, 4, 5 and then 0.5, the last crop drops from rank 5 to rank 1 and every other crop moves up a place. Then Σd² = 1 + 1 + 1 + 1 + 16 = 20 and rₛ = 1 − 120/120 = 0, while r = −0.09. Ranks limit how far one value can pull, but they do not ignore it.

fertilizer (g)height (cm)

Plant E recorded at 80 cm. It drags r from 0.922 down to 0.581, but it was already the tallest, so its rank and rₛ = 0.886 are unchanged.

Which coefficient to use

Use Pearson’s r when the question is whether the points follow a straight line, for example before fitting a regression line. Use Spearman’s rₛ when only the order matters: when the relationship rises or falls steadily but curves, when an extreme value would distort r, or when the data are ranks to begin with, such as places in a contest.

Both run from −1 to 1, and both are read the same way: the sign gives the direction and the size gives the strength.

The usual mistakes

Ranking one column from the smallest and the other from the largest. Two judges who agree would then get a negative rₛ. Rank both in the same direction.

Reading the rank from the position in the list. The rank comes from the size of the value, not from where it is printed.

Giving tied values the same lower place, or adding the places they share. Two values tied for 2nd and 3rd each take 2.5.

Forgetting to square d. The differences always add to 0, so Σd is no use; Σd² is what goes into the formula.

Two judges at a baking contest

In the application below, one judge ranks eight cakes and the other scores them out of 20. The scores are ranked from the highest, to match the first judge ranking from the best, and the two cakes that score 15 share the 4th and 5th places.

Worked example: Two Judges at a Village Baking Contest, One Ranking the Cakes and One Scoring Them with a Tie

Question Two judges assess eight cakes, A to H. The first judge ranks them from 1 (best) to 8: A 3, B 6, C 1, D 5, E 8, F 2, G 4, H 7. The second judge gives each cake a score out of 20: A 17, B 10, C 18, D 15, E 13, F 19, G 15, H 12. (a) Rank the second judge's scores, sharing the rank of any tie between them, and calculate Spearman's rank correlation coefficient from the d2 formula. (b) Test at the 5% level whether there is an association between the two judges' rankings, using the two-tailed critical value 0.7381 for n = 8.

  1. 1.Rank the second judge's scores from the highest, as the first judge ranked from the best: F (19) is 1, C (18) is 2, A (17) is 3, E (13) is 6, H (12) is 7 and B (10) is 8.

    cakerank 1score 2rank 2dd2A3173B6108C1182D515?E8136F2191G415?H7127rank the scores from the highest
    cakerank 1score 2rank 2dd2A3173B6108C1182D515?E8136F2191G415?H7127rank the scores from the highest
    The second judge's scores are ranked from the highest, as the first judge ranked from the best. D and G tie on 15.
  2. 2.D and G both score 15, and they share the 4th and 5th places, so each takes the mean rank 4 + 52 = 4.5.

    cakerank 1score 2rank 2dd2A3173B6108C1182D5154.5E8136F2191G4154.5H7127D and G share places 4 and 5each takes (4 + 5)/2 = 4.5
    cakerank 1score 2rank 2dd2A3173B6108C1182D5154.5E8136F2191G4154.5H7127D and G share places 4 and 5each takes (4 + 5)/2 = 4.5
    D and G share the 4th and 5th places, so each takes rank 4.5.
  3. 3.The differences d between the first and second ranks, from A to H, are 0, −2, −1, 0.5, 2, 1, −0.5 and 0, so ∑ d2 = 0 + 4 + 1 + 0.25 + 4 + 1 + 0.25 + 0 = 10.5.

    cakerank 1score 2rank 2dd2A317300B6108−24C1182−11D5154.50.50.25E813624F219111G4154.5−0.50.25H712700total10.5sum of d2= 10.5
    cakerank 1score 2rank 2dd2A317300B6108−24C1182−11D5154.50.50.25E813624F219111G4154.5−0.50.25H712700total10.5sum of d2= 10.5
    The difference d for each cake, and its square: ∑ d2 = 10.5.
  4. 4.(a) rs = 1 − 6 ∑ d2n(n2 − 1) = 1 − 6 × 10.58 × 63 = 1 − 63504 = 1 − 0.125 = 0.875.

    cakerank 1score 2rank 2dd2A317300B6108−24C1182−11D5154.50.50.25E813624F219111G4154.5−0.50.25H712700total10.5rs = 1 − 6 × 10.5/(8 × 63)= 1 − 63/504 = 0.875
    cakerank 1score 2rank 2dd2A317300B6108−24C1182−11D5154.50.50.25E813624F219111G4154.5−0.50.25H712700total10.5rs = 1 − 6 × 10.5/(8 × 63)= 1 − 63/504 = 0.875
    (a) rs = 1 − 6 × 10.58 × 63 = 0.875.
  5. 5.(b) H0: there is no association between the rankings, ρs = 0; H1: there is an association, ρs ≠ 0. Since 0.875 > 0.7381, reject H0: there is evidence at the 5% level of an association, and as rs is positive the two judges tend to agree. Check: Pearson's r worked out on the two columns of ranks gives 0.874, so with a single tie the formula is a close approximation.

    cakerank 1score 2rank 2dd2A317300B6108−24C1182−11D5154.50.50.25E813624F219111G4154.5−0.50.25H712700total10.50.875 > 0.7381: reject H0the judges tend to agree
    cakerank 1score 2rank 2dd2A317300B6108−24C1182−11D5154.50.50.25E813624F219111G4154.5−0.50.25H712700total10.50.875 > 0.7381: reject H0the judges tend to agree
    (b) 0.875 > 0.7381, so reject H0: there is evidence of an association between the rankings.

Answer: (a) the second judge's ranks are A 3, B 8, C 2, D 4.5, E 6, F 1, G 4.5, H 7; ∑ d2 = 10.5 and rs = 0.875; (b) 0.875 > 0.7381, so reject H0: there is evidence at the 5% level of an association, the judges tending to agree

Common mistakes

  • Giving the two scores of 15 the ranks 4 and 5 in some order, or both 4. Tied values share the places they cover, so each takes 4.5, and the eight ranks still add up to 36.
  • Ranking the scores from the lowest while the first judge ranked from the best. The two columns must be ranked in the same direction, or rs comes out negative although the judges agree.

More correlation and regression problems, worked step by step →

Practice Spearman’s Rank Correlation in the app