5.4 Computing the Coefficient \((r_s)\) Spearman correlation

Sample Spearman correlation coefficient \((r_s)\) is computed by substituting the ranks for the raw measurements in Pearson’s coefficient. \begin {align*} r &=\frac {S_{XY}}{\sqrt {S_{XX}S_{YY}}}\hspace {0.3cm}\text {Pearson}\\\\ &=\frac {\sum (X_i-\overline {X})(Y_i-\overline {Y})}{\Bigg [\sum (X_i-\overline {X})^2\sum (Y_i-\overline {Y})^2\Bigg ]^\frac {1}{2}}\\\\ &=\frac {\sum XY-\dfrac {\sum X\sum Y}{n}}{\Bigg \{\Bigg [\sum X^2-\dfrac {\Big (\sum X\Big )^2}{n}\Bigg ]\Bigg [\sum Y^2-\dfrac {\Big (\sum Y\Big )^2}{n}\Bigg ]\Bigg \}^\frac {1}{2}} \end {align*}

Since sum of integers from 1 through \(n\) is \(\dfrac {n(n+1)}{2}\) and sum of the squares of an \(n\) ranks is \[\frac {n(n+1)(2n+1)}{6}\] thus \begin {align*} r_s &=\frac {\sum R(X_i)R(Y_i)-\dfrac {n(n+1)^2}{4}}{\Bigg \{\Bigg [\sum R(X_i)^2-\dfrac {n(n+1)^2}{4}\Bigg ]\Bigg [\sum R(Y_i)^2-\dfrac {n(n+1)^2}{4}\Bigg ]\Bigg \}^\frac {1}{2}}\\\\ &=\frac {12\Big [\sum R(X_i)R(Y_i)-\dfrac {n(n+1)^2}{4}\Big ]}{n^3-n}\\\\ &=\frac {12\sum R(X_i)R(Y_i)}{n^3-n}-\frac {3(n+1)}{n-1}\\ \end {align*}

Alternatively, the difference \(d_i\), for each pair of ranks may be obtained. The following equation is used \[r_s=1-\frac {6\sum d_i^2}{n^3-n}\]

Note 5.3. This last formula is valid only when there are no ties. It is the one almost everybody remembers, and it is the one most often misapplied.

The derivation above replaced \(\sum R(X_i)^2\) and \(\sum R(Y_i)^2\) by \(\dfrac {n(n+1)(2n+1)}{6}\). That substitution is the sum of the squares of the integers \(1, 2, \ldots , n\), and it holds only if the ranks actually are those integers. Average ranks such as \(3.5,\ 3.5\) in place of \(3,\ 4\) have the same sum but a smaller sum of squares, so the substitution fails and every line that follows from it fails with it.

With ties, either use the corrected expression given below, or — equivalently and more simply — compute Pearson’s correlation coefficient directly on the average ranks. The two agree exactly, and the second needs nothing memorised beyond Pearson’s formula.

Using sum of the ranks for each pair \(\displaystyle {r_s=\frac {6\sum S^2_i}{n^3-n}-\frac {7n+5}{n-1}}\hspace {0.2cm}\) where \(S_i=R(X_i)+R(Y_i).\)

Note 5.4. The sign and size of \(r_s\) are read as follows.

(i).
\(r_s=0\): no correlation. The ranks of one variable carry no information about the ranks of the other.
(ii).
\(r_s>0\): \(R(Y_i)\) tends to increase as \(R(X_i)\) increases.
(iii).
\(r_s<0\): \(R(Y_i)\) tends to decrease as \(R(X_i)\) increases.

Like Pearson’s coefficient, \(r_s\) lies in \([-1,1]\), and the extremes are attained exactly when the two rankings are identical \((r_s=1)\) or exactly reversed \((r_s=-1)\).

Questions on this section

Stuck on something here? Ask below and it stays attached to this topic.