Recall the globalLandTempHist.txt dataset that consisted of the global land temperature of Earth over the past 300 years. Also recall the Spearman correlation rank coefficient is merely the Pearson’s correlation coefficient of the ranks of two attributes in a dataset. In other words, given a dataset, like the above temperature, one can first compute the ranks of attributes. These ranks are basically a reordering of the indices of each data attribute, such that with the new indices data becomes ordered. For example, the rank of the following dataset,
\[D = [2.5, -3, 1, 5.5, 0, 10] ~,\]is,
\[R = [4, 1, 3, 5, 2, 6] ~,\]Once the ranks of a pair of attributes are computed, one can readily use the original definition of Pearson’s correlation coefficient to compute the Spearman’s rank correlation coefficient of data via the equation,
\[r_{xy} = \frac{ \sum_1^{n} ~ (R_i-\overline{R})(S_i-\overline{S}) }{ \sqrt{\sum_1^{n} ~ (R_i-\overline{R})^2} ~\sqrt{\sum_1^{n} ~ (S_i-\overline{S})^2} }\]where $n$ represents the number of data points and $R$ and $S$ are the corresponding ranks of the data attributes $X$ and $Y$. Given this introduction, we wish to compute the Spearman correlation coefficient between the year
and the temperature anomaly
attribute in the land temperature data mentioned in the above. To do so, we will take the following steps:
- Use an existing package in the language of your choice to compute the rank of the two data attributes. In the case of Python, you can use numpy’s
argsort()
to obtain the attributes’ ranks. - Write a function named
genSpearmanCor(data1,data2)
that returns the Spearman correlation coefficient according to the equation in the above using the ranks. - Now, read the dataset (using Pandas Python library, for example) and make sure to exlude lines of data that contain
nan
values. If you are using Python, you can get help from Pandasdropna()
method to remove rows of data withnan
. - Now, pass the two columns of data to your function to compute the Spearman correlation coefficient. You should obtain a positive correlation indicating that the global land temperature has increased with time over the past 300 years.
- Now, use the Spearman correlation coefficient calculator from an established library in the language of your choice to verify your calculation of the Spearman correlation. Within Python, you can use Numpy’s
corrcoef()
function to compute the correlation. - Compare your answer with what you get from an external package for computing the Spearman’s correlation coefficient. In case of Python, you can use SciPy’s
scipy.stats.spearmanr
function.