Statistics
Coefficient of determination
stats.r-squaredAlso called r squared, r2, r2 score, coefficient of determination
where
One minus the ratio of the residual sum of squares to the total sum of squares about the mean of the observed values. It is 1 for a perfect fit and negative for fits worse than the mean.
Notation
observed values
y ∈ ℝn
predicted values
ŷ ∈ ℝn
number of observations
Size, at least 2
Result: the coefficient of determination
scalar
Worked examples
Each result is solved exactly and confirmed by two independent computations.
Given
Result
≈ 0.8
Given
Result
≈ -1.5
Given
Result
≈ 0.75
Common mistakes
MSE over the sample variance
The residual term divides by n and the total term by n − 1, so 1 − R2 comes out (n − 1)/n times its true value.
Fix: Use plain sums of squares, or the same divisor in both terms.
Total sum of squares taken about the predictions
The denominator uses the spread of the predicted values instead of the observed values.
Fix: Compute the total sum of squares from the observed values and their mean.
Source
Murphy 2022
Murphy, Kevin P. Probabilistic Machine Learning: An Introduction. MIT Press, 2022.