Skip to content
Advertisement

Why do coefficient of determination, R², implementations produce different results?

When attempting to implement a python function for calculating the coefficient of determination, R², I noticed I got wildly different results depending on whose calculation sequence I used.

The wikipedia page on R² gives a seemingly very clear explanation as to how R² should be calculated. My numpy interpretation of what is being said on the wiki page is the following:

JavaScript

When I try this method with a target vector y and a vector of modeled estimates yhat, this function produces an R² value of -0.00301.

However, the accepted answer from this stackoverflow post discussing how to calculate R², gives the following definition:

JavaScript

Using that method with the same y and yhat vectors as before, I now get an R² of 0.319.

Moreover, in the same stackoverflow post, a lot people of seem to be in favor of calculating the R² with the scipy module like this:

JavaScript

Which in my case produces 0.261.

So my question is: why are R² values produced from seemingly well-accepted sources radically different from each other? And what is the correct way of calculating R² between two vectors?

Advertisement

Answer

Definitions

This is a notation abuse that often leads to misunderstanding. You are comparing two different coefficients:

  • Coefficient of determination (usually noted as R^2) which can be used for any OLS regression not only linear regression (OLS is linear with regards of fit parameters not the function itself);
  • Pearson Correlation Coefficient (usually noted as r or r^2 when squared) which is used for linear regression only.

If you read carefully the introduction of Coefficient of determination on Wikipedia page, you will see that it is discussed there, it starts as follow:

There are several definitions of R2 that are only sometimes equivalent.

MCVE

You can confirm that classical implementation of those score return expected results:

JavaScript

Then your function calcR2_wikipedia (0.9265536406736125) returns the coefficient of determination, it can be confirmed as it returns the same as sklearn.metrics.r2_score:

JavaScript

In the other hand the scipy.stats.linregress returns the correlation coefficient (valid for linear regression only):

JavaScript

Which you can cross confirm by it’s definition:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement