Recall the globalLandTempHist.txt dataset that consisted of the global land temperature of Earth over the past 300 years. Also recall the equation for the Pearson’s correlation coefficient, between two attributes of a dataset.
\[r_{xy} = \frac{ \sum_1^{n} ~ (x_i-\overline{x})(y_i-\overline{y}) }{ \sqrt{\sum_1^{n} ~ (x_i-\overline{x})^2} ~\sqrt{\sum_1^{n} ~ (y_i-\overline{y})^2} }\]where $n$ represents the number of data points. We wish to compute the Pearson 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:
- Write a function named
getPearsonCor(data1,data2)
that return the Pearson correlation coefficient according to the equation in the above. - 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 Pearson 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 Pearson correlation coefficient calculator from an established library in the language of your choice to verify your calculation of the Pearson correlation. Within Python, you can use Numpy’s
corrcoef()
function to compute the correlation.