The end result of running a correlation is to get the correlation coefficient (r), which is a number between -1 and 1. There are two things we look for in a correlation coefficient:
Direction: this is denoted by whether the coefficient is negative or positive. A negative number does not mean it is bad, and a positive number does not mean it is good. It is simply saying that if it is:
Strength: this is how close to -1 or 1 it is. The closer to -1 or 1, the stronger the correlation between the two variables. The inverse is also true: the closer to 0 the number is, the weaker the correlation is.
Absolute value of r Strength of relationship
r < 0.25 No relationship
0.25 < r < 0.5 Weak relationship
0.5 < r < 0.75 Moderate relationship
r > 0.75 Strong relationship
Now that we understand what the correlation coefficient (r) represents, letβs calculate it! We can use the cor command for this.
cor(examData$studying_hours, examData$exam_score) # Study time vs performance
cor(examData$anxiety_score, examData$exam_score) # Anxiety vs performance
cor(examData$studying_hours, examData$anxiety_score) # Study time vs anxiety
[1] 0.3900419
[1] -0.4449023
[1] -0.7122237
Success! We have three different correlation coefficients. Let us look at all three:
Now, we can also utilize the cor.test command, which not only will give us the correlation coefficient, but also the p-value, so we can identify if the correlation is statistically significant or not.
cor.test(examData$studying_hours, examData$exam_score) # Study time vs performance
cor.test(examData$anxiety_score, examData$exam_score) # Anxiety vs performance
cor.test(examData$studying_hours, examData$anxiety_score) # Study time vs anxiety
Pearson's product-moment correlation
data: examData$studying_hours and examData$exam_score
t = 4.1933, df = 98, p-value = 6.034e-05
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.2096883 0.5447277
sample estimates:
cor
0.3900419
Pearson's product-moment correlation
data: examData$anxiety_score and examData$exam_score
t = -4.9178, df = 98, p-value = 3.524e-06
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
-0.5897813 -0.2722777
sample estimates:
cor
-0.4449023
Pearson's product-moment correlation
data: examData$studying_hours and examData$anxiety_score
t = -10.044, df = 98, p-value < 2.2e-16
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
-0.7971286 -0.5996997
sample estimates:
cor
-0.7122237
Turns out all three are statistically significant! Thereβs something just as important as the r value and p value: the method used to calculate it. By default cor and cor.test use Pearsonβs method. Going back to visualizing our data, we can only use Pearsonβs method if our data is linear.