Pearson and Spearman Correlations in R

In a previous post we explained the concept of correlation between two variables and specifically discussed for Spearman and Pearson correlations. Here, we will have a practical example using R language.

With the following script we create a scatter-plot derived from two columns of the mtcars dataset depicting the relationship of weight(wt) and miles per gallon(mpg).

Assumptions

Pearson correlation is a parametric correlation and can be used only when x, y come from a normal distribution. In our example, we can test this assumption using the Shapiro-Wilk normality test.

In this test, the null hypothesis is that the data come from a normal distribution. In case when p-value < 0.05 we can reject the null hypothesis and accept the alternative one. Here, for both x and y we accept the null hypothesis and x,y are normally distributed.

Correlations

Calculating the Pearson and Spearman correlations with the following lines, we have:

Both of these metrics indicate strong correlation between weight and mpg variables of the mtcars dataset. Spearman has a slightly higher value since it captures the monotonic relationship and not strictly the linear relationship.

No Comments

Sorry, the comment form is closed at this time.

× How can we help you?