5.08e–5.08g - Rank correlation and choosing a coefficient

5.08e–5.08g - Rank correlation and choosing a coefficient

Calculate Spearman's rank correlation coefficient and use it to describe agreement or disagreement between two orderings. You will test for association in a population, choose the correct critical-value column, and decide when ranks or Pearson's straight-line measure answer the question you actually want to ask.

Calculating Spearman's coefficient

Two measurements can rise together perfectly without lying on a straight line. For the pairs (1,1)(1,1), (2,2)(2,2), (3,4)(3,4), (4,8)(4,8), (5,16)(5,16), each increase in the first measurement accompanies an increase in the second, yet Pearson's coefficient is about 0.9330.933, rather than 11. Can a correlation coefficient recognise perfect agreement in order even when the curve bends?

Replace the measurements by their positions in their respective ordered lists. The increasing values of both variables then receive ranks 1,2,3,4,51,2,3,4,5. The distances between the measurements have disappeared, leaving complete agreement between the two orderings.

You already know

Pearson's rr measures closeness to a straight line using the numerical measurements. Hypothesis tests concern a population, with the direction of the alternative chosen from the claim. Failing to reject a null hypothesis is not proof that it is true.

When data consist of positions or ratings with a meaningful order but no meaningful numerical spacing, ranking is useful in its own right. The gap between first and second place need not mean the same thing as the gap between second and third.

Spearman's rank correlation coefficient

Spearman's rank correlation coefficient, rsr_s, measures the agreement between the ranks of paired observations. Rank each variable separately, using a consistent direction, and let did_i be the difference between the two ranks for the same individual. With no tied values, each rank list uses every integer from 11 to nn once.

Use rank 11 for the smallest value of each variable unless the context already supplies ranks, such as first place in a race. The method below assumes there are no ties: two equal values cannot be assigned two distinct ordered positions by guessing. We will use distinct values throughout.

Spearman's rank correlation coefficient

For nn paired observations with no tied ranks,

rs=1−6∑di2n(n2−1)r_s = 1 - \frac{6\sum d_i^2}{n(n^2 - 1)}

Here did_i is the difference between the two ranks in pair ii. We will calculate the coefficient for at most 1010 pairs.

In the formula booklet.

Make a table containing both ranks, their difference and its square. The sign chosen for did_i does not matter after squaring, but the pairing does: each row must still represent the same individual.

Worked example 1: measurements converted to ranks

Question. Six samples have the following paired measurements. Calculate Spearman's rank correlation coefficient.

SampleABCDEF
xx446688101012121414
yy151512121818202017172525

Plan. Rank the two measurement lists from smallest to largest, preserve the sample labels and sum the squared rank differences.

SampleRank of xxRank of yydid_idi2d_i^2
A1122−1-111
B22111111
C3344−1-111
D4455−1-111
E55332244
F66660000
∑di2=1+1+1+1+4+0=8add squared differencesrs=1−6(8)6(62−1)n=6=2735=0.771…simplify\begin{aligned} \sum d_i^2 &= 1 + 1 + 1 + 1 + 4 + 0 = 8 && \text{add squared differences} \\ r_s &= 1 - \frac{6(8)}{6(6^2 - 1)} && n = 6 \\ &= \frac{27}{35} = 0.771\ldots && \text{simplify} \end{aligned}

Why this step? Sample E has the fifth xx rank but the third yy rank; sorting columns independently would erase exactly the disagreement being measured.

Answer. rs=0.771r_s = 0.771, to 33 significant figures. The ranks show positive association, but do not agree perfectly.

Check. Each rank column sums to 1+2+3+4+5+6=211+2+3+4+5+6 = 21, and −1≤27/35≤1-1 \le 27/35 \le 1.

Worked example 2 changes only the gaps between the yy measurements. Their order is preserved, so a rank-based coefficient should stay the same.

Worked example 2: different gaps, the same ordering

Question. Keep the six xx values from example 1, but replace the yy measurements for samples A to F by 30,20,50,70,40,9030,20,50,70,40,90. Calculate Spearman's coefficient.

Plan. Check the new yy order, then use the rank differences rather than differences between the measurements.

Order in y:B,A,E,C,D,Fsmallest to largestRanks in sample order:2,1,4,5,3,6restore A to F∑di2=(−1)2+12+(−1)2+(−1)2+22+02=8same rank differencesrs=1−6(8)6(35)=2735substitute ranks\begin{aligned} \text{Order in }y &: B, A, E, C, D, F && \text{smallest to largest} \\ \text{Ranks in sample order} &: 2,1,4,5,3,6 && \text{restore A to F} \\ \sum d_i^2 &= (-1)^2+1^2+(-1)^2+(-1)^2+2^2+0^2 = 8 && \text{same rank differences} \\ r_s &= 1 - \frac{6(8)}{6(35)} = \frac{27}{35} && \text{substitute ranks} \end{aligned}

Why this step? Spearman's coefficient deliberately discards the size of the measurement gaps. The larger gap from 7070 to 9090 cannot matter when the order has not changed.

Answer. rs=0.771r_s = 0.771, to 33 significant figures, unchanged.

Check. The rank columns are identical to those in example 1, so every di2d_i^2 is identical as well.

The formula depends on squared differences, not a squared total difference.

Watch out

Wrong: replace ∑di2\sum d_i^2 by (∑di)2(\sum d_i)^2. In example 1 the differences sum to zero, but their squares sum to 88. Right: square each row's difference before adding.

Now begin with two lists of ranks already supplied.

What rank correlation does and does not say

Rank correlation describes an ordering relationship, so it can recognise an increasing curve as well as an increasing straight line.

Monotonic association

A monotonic relationship keeps the same direction of change: as one variable increases, the other consistently increases, or consistently decreases. With distinct values, perfect agreement between the two increasing orders gives rs=1r_s = 1; perfectly reversed orders give rs=−1r_s = -1.

The opening data lie on the increasing curve y=2x−1y = 2^{x-1}. Their two increasing rank lists are identical, so every did_i is zero and rs=1r_s = 1. That answers the opening question: perfect agreement of order can be recognised without insisting on a straight line. Pearson's rr and Spearman's rsr_s are measuring different features of the same points.

[DIAGRAM: asset_name: Straightness versus ordering; asset_slug: h245_5_08_rank_shapes; caption: An increasing curve has perfect rank agreement, while an exact U-shaped relationship changes direction and is not summarised well by a single rank coefficient; description: Left panel plots the five points (1,1), (2,2), (3,4), (4,8), (5,16), with a smooth increasing exponential guide and labels giving x ranks and y ranks both 1 to 5, rs = 1 and r approximately 0.933. Right panel plots x values -3, -1, 0, 2, 4 and y values 9, 1, 0, 4, 16 with a smooth parabola guide, labels the falling and rising sections, and states that the association changes direction. All values are distinct within each variable, so no tied ranks occur.]
An increasing curve has perfect rank agreement, while an exact U-shaped relationship changes direction and is not summarised well by a single rank coefficient

If the relationship changes direction, neither a single increasing nor a single decreasing ordering captures it. A coefficient near zero need not mean that the variables are unrelated. A scatter diagram remains useful even after the ranks have been calculated.

There is another surprise when the sample is tiny. With three distinct pairs, fix one rank list as 1,2,31,2,3. The other list has only six possible orders:

Other rank list∑di2\sum d_i^2rsr_s
1,2,31,2,30011
1,3,21,3,2221/21/2
2,1,32,1,3221/21/2
2,3,12,3,166−1/2-1/2
3,1,23,1,266−1/2-1/2
3,2,13,2,188−1-1

The denominator is 3(32−1)=243(3^2-1) = 24, so these six permutations produce only four coefficient values. Listing all permutations matters if you are asked to show that no other values are possible. The range from −1-1 to 11 does not mean every value in that interval is attainable for a fixed sample size.

When ranking, decide what the two directions mean in context. Giving rank 11 to the best performer in both races measures agreement about performance. Reversing both rank lists keeps agreement unchanged. Reversing only one turns agreement into disagreement; it does not provide an alternative answer to the same question. A positive rank coefficient alone also establishes no causal mechanism.

Key idea

Spearman's coefficient measures agreement of order, not straightness, equality of the measurements or causation. Keep the meaning of each ordering visible.

Check a proposed shortcut before accepting its result.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account