How to Calculate Spearman Rank Correlation by Hand

Calculate Spearman Rank Correlation Hand
Image by Author | ChatGPT
 

The Spearman rank correlation coefficient measures the strength and direction of association between two ranked variables. Unlike Pearson correlation, which measures linear relationships, Spearman correlation evaluates monotonic relationships, making it useful when data doesn’t follow a normal distribution or contains outliers. It’s also useful for analyzing non-linear relationships, as long as they follow a monotonic pattern where one variable consistently increases or decreases as the other changes.

The Spearman correlation coefficient ranges from -1 to 1, where -1 indicates a perfect negative monotonic relationship, 0 indicates no monotonic relationship, and 1 indicates a perfect positive monotonic relationship.

This article provides a step-by-step example of calculating Spearman rank correlation by hand using a dataset of student study hours and exam scores.

The Spearman Rank Correlation Formula

The Spearman rank correlation coefficient is calculated using the following formula:

calculate spearman correlation 

Where:

    • rs = Spearman rank correlation coefficient
  • d = difference between ranks for each pair of observations
  • Σd² = sum of squared differences
  • n = number of observations
  • 6 = a constant 

The number 6 in the formula is a mathematical constant that arises from the derivation of the Spearman correlation coefficient. It’s related to the variance of ranks in a perfectly ordered sequence and ensures the correlation coefficient ranges from -1 to 1.

Sample Dataset

We’ll work with the following data showing hours studied and corresponding exam scores for 8 students:

Student Hours Studied (X) Exam Score (Y)
1 2 65
2 4 70
3 6 75
4 8 85
5 10 90
6 12 88
7 14 95
8 16 92

Step 1: Rank the X Values

First, we assign ranks to the X values (hours studied) from lowest to highest:

Hours Studied (X) Rank of X
2 1
4 2
6 3
8 4
10 5
12 6
14 7
16 8

Since there are no tied values, each observation receives a unique rank. When tied values occur, assign the average of the ranks that would have been assigned to those values. For example, if two values tie for ranks 3 and 4, both receive rank 3.5.

Step 2: Rank the Y Values

Next, we assign ranks to the Y values (exam scores) from lowest to highest:

Exam Score (Y) Rank of Y
65 1
70 2
75 3
85 4
88 5
90 6
92 7
95 8

Again, no tied values exist, so ranking is straightforward.

Step 3: Calculate the Difference in Ranks

Now we calculate the difference between each pair of ranks (d) and square these differences (d²):

Student Rank X Rank Y d = Rank X – Rank Y d²
1 1 1 0 0
2 2 2 0 0
3 3 3 0 0
4 4 4 0 0
5 5 6 -1 1
6 6 5 1 1
7 7 8 -1 1
8 8 7 1 1

Step 4: Sum the Squared Differences

We sum all the squared differences:

 
Σd² = 0 + 0 + 0 + 0 + 1 + 1 + 1 + 1 = 4

Step 5: Apply the Spearman Correlation Formula

Let’s apply the formula we introduced above: 

calculate spearman correlation

Where n is the number of observations.

Substituting our values:

 
rs = 1 - (6 × 4) / (8 × (8² - 1))
rs = 1 - 24 / (8 × 63)
rs = 1 - 24 / 504
rs = 1 - 0.048
rs = 0.952

This gives us a Spearman correlation coefficient of 0.952.

Interpretation

The Spearman correlation coefficient of 0.952 indicates a very strong positive monotonic relationship between study hours and exam scores. This means that as study hours increase, exam scores tend to increase in a consistent pattern, though not necessarily in a perfectly linear fashion.

The high positive correlation suggests that students who study more hours generally achieve higher exam scores, making study time a strong predictor of academic performance in this dataset.

Conclusion

Calculating Spearman rank correlation by hand involves ranking both variables, finding the differences between ranks, and applying a straightforward formula. This non-parametric measure is particularly valuable when dealing with ordinal data or when the relationship between variables is monotonic but not necessarily linear. The method provides robust results even when data contains outliers or doesn’t meet the assumptions required for Pearson correlation.

Additional Resources

How to Calculate Spearman Rank Correlation in Python
How to Calculate Spearman Rank Correlation in R
When to Use Spearman’s Rank Correlation (2 Scenarios)

Leave a Reply

Your email address will not be published. Required fields are marked *