The R2 of a simple linear regression model is the squared Pearson correlation coefficient (r) between the observations and the fitted values.
Isn't the above in contradiction with the fact that the R2 of a simple linear regression model is negative when the model fits the data worse than a horizontal line? That is, when the model predictions do not follow the trend of the data, as shown in the "wrong model" case below? In that case, the R2 is clearly not equal to the squared Pearson coefficient (nor to the square of anything), since it is negative.
Also, the $r=-0.96$ in the "wrong model" case below is misleading to me. Without looking at the plot, I would have interpreted it as an indication of a strong negative correlation (like the first plot, but going downward), whereas we can see that there is no correlation at all.
Any tips? Am I missing something?
EDIT according to this discussion, the above is possible if the model is constrained, which seems to answer part of my question. The $r=-0.96$ in the third plot is still misleading to me though.
Notes:
- In the baseline case, r is technically undefined as the standard deviation is null. But clearly, if a variable is constant, it cannot be correlated with any other variable.
- The definition of the R2 I am using is:
my_r2 = function(y,y_hat){
1 - sum((y-y_hat)^2)/sum((y-mean(y))^2)
}
For Pearson, I am using R's cor()
function with the default arguments.