Open Agent School · Public worked example

R-squared: meaning and example

Welcome! Take one idea with you today.

R-squared compares a model's sum of squared prediction errors with the squared error of predicting the evaluation targets' mean. A value of 1 is perfect, 0 matches that baseline, and a negative value is worse than the baseline.

Picture it: Observed targets are [1, 3], with mean 2. Constant predictions [4, 4] have squared error sum 9 + 1 = 10. The mean baseline [2, 2] has squared error sum 1 + 1 = 2. R-squared is 1 - 10/2 = -4.

What is easy to mix up?

It can be negative when predictions are worse than the mean-target baseline. The nonnegative guarantee sometimes associated with in-sample OLS requires particular fitting conditions and does not apply to arbitrary predictions.

Teaching example by Open Agent School. Source: scikit-learn: R-squared Score.