📝 Problem
Two judges rank 6 students. Find Spearman's rank correlation.
| Student | A | B | C | D | E | F |
|---|---|---|---|---|---|---|
| Judge 1 | 1 | 3 | 2 | 5 | 6 | 4 |
| Judge 2 | 2 | 1 | 3 | 4 | 5 | 6 |
Unit IV · Lecture 30 of 40 · B.Sc. Data Science, 1st Year, Semester II
Pearson's r requires quantitative data and measures only linear relationships. But what if:
• The data is ordinal (rankings, ratings, preferences)?
• The relationship is monotonic but not linear?
• The data contains outliers that distort Pearson's r?
Spearman's rank correlation (ρ, rho) addresses all three issues.
Two judges rank 5 contestants. Judge A ranks them 1, 2, 3, 4, 5. Judge B ranks them 2, 1, 3, 5, 4. Do the judges agree? We can't use Pearson's r on rankings (they're ordinal, not interval). Instead, we compare the ranks directly using Spearman's ρ. The judges mostly agree — their rank correlation is high.
Charles Edward Spearman was a British psychologist who pioneered the use of factor analysis and rank-based statistics. He developed the rank correlation coefficient in 1904. Interestingly, Spearman's ρ is mathematically equivalent to Pearson's r computed on the ranks rather than the original data values.
ρ = 1 − [6Σdᵢ²] / [n(n² − 1)]
Where:
• dᵢ = difference between the ranks of the i-th pair: dᵢ = R(xᵢ) − R(yᵢ)
• n = number of paired observations
• Σdᵢ² = sum of squared rank differences
Range: −1 ≤ ρ ≤ +1
• ρ = +1: Perfect agreement in ranks
• ρ = −1: Perfect disagreement (one ranking is the reverse of the other)
• ρ = 0: No monotonic relationship between ranks
Two judges rank 6 students. Find Spearman's rank correlation.
| Student | A | B | C | D | E | F |
|---|---|---|---|---|---|---|
| Judge 1 | 1 | 3 | 2 | 5 | 6 | 4 |
| Judge 2 | 2 | 1 | 3 | 4 | 5 | 6 |
| Student | R₁ (Judge 1) | R₂ (Judge 2) | d = R₁−R₂ | d² |
|---|---|---|---|---|
| A | 1 | 2 | −1 | 1 |
| B | 3 | 1 | +2 | 4 |
| C | 2 | 3 | −1 | 1 |
| D | 5 | 4 | +1 | 1 |
| E | 6 | 5 | +1 | 1 |
| F | 4 | 6 | −2 | 4 |
| Σd² = | 12 | |||
n = 6, Σd² = 12
ρ = 1 − 6(12) / [6(36−1)]
= 1 − 72 / [6 × 35]
= 1 − 72/210
= 1 − 0.3429
= 0.6571
Moderate to strong positive agreement between the two judges' rankings. They generally agree on who is better, but differ on some specific placements.
When two or more observations have the same value, they receive the average of the ranks they would have occupied. For example, if the 3rd and 4th values are tied, both get rank (3+4)/2 = 3.5.
Find Spearman's ρ for the following data (note the tie in y):
| x | 10 | 20 | 30 | 40 | 50 |
|---|---|---|---|---|---|
| y | 15 | 25 | 25 | 35 | 45 |
| x | y | R(x) | R(y) | d | d² |
|---|---|---|---|---|---|
| 10 | 15 | 1 | 1 | 0 | 0 |
| 20 | 25 | 2 | 2.5 | −0.5 | 0.25 |
| 30 | 25 | 3 | 2.5 | +0.5 | 0.25 |
| 40 | 35 | 4 | 4 | 0 | 0 |
| 50 | 45 | 5 | 5 | 0 | 0 |
| Σd² = | 0.50 | ||||
Method 1 (Shortcut — approximate with ties):
ρ = 1 − 6(0.50) / [5(25−1)] = 1 − 3/120 = 1 − 0.025 = 0.975
Method 2 (Exact — Pearson's formula on ranks):
When ties exist, use the general formula:
ρ = [ΣR(x)R(y) − n·R̄(x)·R̄(y)] / √{[ΣR(x)² − n·R̄(x)²] · [ΣR(y)² − n·R̄(y)²]}
R̄(x) = (1+2+3+4+5)/5 = 3, R̄(y) = (1+2.5+2.5+4+5)/5 = 3
ΣR(x)R(y) = 1(1)+2(2.5)+3(2.5)+4(4)+5(5) = 1+5+7.5+16+25 = 54.5
ΣR(x)² = 1+4+9+16+25 = 55, ΣR(y)² = 1+6.25+6.25+16+25 = 54.5
ρ = [54.5 − 5(3)(3)] / √{[55−45][54.5−45]} = 9.5 / √{10 × 9.5} = 9.5/√95 = 9.5/9.747 = 0.9747
| Feature | Pearson's r | Spearman's ρ |
|---|---|---|
| Data type | Quantitative (interval/ratio) | Ordinal, ranked, or quantitative |
| Measures | Linear relationship | Monotonic relationship |
| Based on | Actual values | Ranks of values |
| Outlier sensitivity | Highly sensitive | Robust (outliers get extreme ranks but less weight) |
| Range | −1 to +1 | −1 to +1 |
| r = +1 or ρ = +1 means | Perfect linear fit | Perfect rank agreement |
| Non-linear relationships | May give r ≈ 0 | Detects monotonic non-linear patterns |
| When to use | Continuous data, linear relationship, no outliers | Ordinal data, outliers present, monotonic relationship |
Q1: Spearman's ρ measures:
Q2: If two students are tied for ranks 3 and 4, they both receive rank:
Q3: If Σd² = 0 in Spearman's formula, then ρ equals:
Q4: Spearman's ρ is more robust than Pearson's r when the data has:
Q5: Spearman's ρ is mathematically equivalent to Pearson's r computed on:
1. Find Spearman's ρ for: x = 85, 72, 90, 65, 78 and y = 80, 68, 88, 70, 75.
2. Two professors rank 7 students. Their rankings are:
Prof A: 1, 2, 3, 4, 5, 6, 7
Prof B: 2, 1, 4, 3, 6, 5, 7
Find ρ. Do the professors agree?
3. Compute both Pearson's r and Spearman's ρ for: x = 1, 2, 3, 4, 5 and y = 1, 4, 9, 16, 25. Compare the results. Why do they differ?
4. Explain why Spearman's ρ is preferred over Pearson's r when data contains outliers.
5. BONUS: Show that if all ranks are in perfect agreement (R(xᵢ) = R(yᵢ) for all i), then Σd² = 0 and ρ = 1.
Lecture 31: Simple Linear Regression — regression lines, least squares, and the relationship between correlation and regression.