I need to run a correlation with Spearman between two variables and then plot the result with a scatterplot in R. I know that normally a positive rho is associated with an ascending trendline in a scatterplot, and a negative with a descending one.
However, I got a positive rho (rho = 0.02 with p = 0.91) and a scatterplot with trendline going down, as shown in the figure below.
The code that I used is given below and the dataset can be found here
cor.test(Finale_from_trials_online$Mean_lat_LG, Finale_from_trials_online$Mean_Lat_cue_1, method = "spearman", exact = FALSE)
plot(Finale_from_trials$Mean_Lat_cue_1, Finale_from_trials$Mean_lat_LG, main="LG and cue 1",
xlab="Mean latency 1", ylab="Mean latency LG", pch=19)
abline(lm(Finale_from_trials$Mean_lat_LG~Finale_from_trials$Mean_Lat_cue_1), col="red") # regression line (y~x)
Did I do anything wrong in the code?
Is the problem concerning the lm
in the abline
argument, since Spearman is just checking for monotonic correlation, and I am plotting a linear trendline?
Or is it just as it is, since the p value is so big and the result is not significant?
Does anybody know why a high p value can cause this?
Spearman's r is calculated using the rank of the values. That is why the outliers with large y values on the left side of the plot are outweighed by the more numerous values with slightly lower y-values than those on the hand side of the plot. But as you have pointed out, the r value is so small, that based on your hypothesis test you cannot say if Spearman's r is truely different from 0.
In contrast, the linear model you have included is sensitive to these outliers. In fact, a bivariate linear model will always have the same sign for beta as Pearson's r, because the sign in both cases come from the covariance. If you do cor.test()
with method = "pearson"
you will see that it is negative. Yet, as Spearman's r is not based on the covariance these sign can differ.