Two judges rank the performance of six contestants. There are no marks to measure, only an order of merit, and Pearson's coefficient cannot be applied directly. Spearman's rank correlation compares the two rankings.
Ranks and the Formula
When data are ranks or can be ranked, the relation is measured by Spearman's rank correlation coefficient, denoted rho. If d is the difference between the two ranks of each individual and n is the number of individuals, then rho = 1 - (6 × sum of d squared) / (n × (n squared - 1)). Like r, it lies between -1 and +1. A value of +1 means that the rankings are identical, and -1 means that they are exactly opposite.
Definition: Spearman's rank correlation coefficient. A measure of the agreement between two rankings of the same individuals, given by rho = 1 - 6 (sum of d squared) / (n (n squared - 1)).
Ranking the Values
To rank a set of values, the largest value is given rank 1, the next rank 2, and so on, or the smallest is ranked 1. The same rule must be used for both variables. Rank correlation is useful for qualitative data that can be ordered, for data with extreme values, and for small samples.
Tied Ranks
When two or more individuals have the same value, they are given the average of the ranks they would have occupied. For example, if two values tie for the 2nd and 3rd places, each gets 2.5. A correction of m × (m squared - 1) / 12 is added to the sum of d squared for each group of m tied values.
Table 2.4: Procedure for Spearman's rank correlation
| Step | Action | Result |
|---|---|---|
| 1 | Rank both variables | Ranks R1 and R2 |
| 2 | Find d = R1 - R2 | Differences |
| 3 | Square and add the values of d | Sum of d squared |
| 4 | Apply the formula | rho |
Example 2.3: Rank correlation of two tests
Problem: Six students scored 86, 72, 60, 91, 55 and 78 in a test X and 82, 65, 70, 88, 58 and 75 in a test Y. Find the rank correlation.
Solution:
- Rank in X (largest = 1): 2, 4, 5, 1, 6, 3.
- Rank in Y (largest = 1): 2, 5, 4, 1, 6, 3.
- The differences d are 0, -1, 1, 0, 0, 0, and d squared adds up to 2.
- rho = 1 - (6 × 2) / (6 × 35) = 1 - 12 / 210 = 1 - 0.0571.
Result: rho = 0.943, which shows a very strong agreement between the two rankings.
X = [86, 72, 60, 91, 55, 78]
Y = [82, 65, 70, 88, 58, 75]
def rank(v):
s = sorted(v, reverse=True)
return [s.index(a) + 1 for a in v]
d2 = sum((a - b) ** 2 for a, b in zip(rank(X), rank(Y)))
n = len(X)
print(rank(X), rank(Y), d2, round(1 - 6 * d2 / (n * (n * n - 1)), 3))
Output
[2, 4, 5, 1, 6, 3] [2, 5, 4, 1, 6, 3] 2 0.943
Common mistakes
- Ranking one variable from the largest and the other from the smallest.
- Forgetting that the sum of the differences d must be zero, which is a check on the ranks.
- Giving tied values consecutive ranks instead of their average.
Key takeaways
- Spearman's rho works with ranks and lies between -1 and +1.
- rho = 1 - 6 (sum of d squared) / (n (n squared - 1)).
- Tied values receive the average of their ranks.
- The sum of the rank differences is zero.
Practice questions
- What is the value of rho when the two rankings are identical?
- How are tied values ranked?
- Write the formula of Spearman's rank correlation coefficient.
Answers to the practice questions
- +1.
- Each is given the average of the ranks they would have occupied.
- rho = 1 - 6 (sum of d squared) / (n (n squared - 1)).
Sources
- Official BCA syllabus Probability and Statistics, course code 26BCA403DS01, Unit II
