Checking a negative prediction R squared
Compute R squared from actual and predicted lists to see why it equals zero for mean-only predictions, one for perfect predictions, and goes negative for worse ones.
The definition used here
This R Squared Calculator takes two lists, the actual values and the predicted values, and computes R squared as 1 minus the sum of squared prediction errors divided by the sum of squared deviations of the actual values from their mean. The numerator of the fraction measures how far the predictions are from the actual values. The denominator measures how far the actual values are from their own mean, which is the total variation. The ratio compares the predictions with a baseline that always predicts the average. Subtracting the ratio from 1 gives the proportion of the total variation the predictions account for. The lists must have the same length, with each prediction paired to the actual value in the same position. Swapping the roles of the two lists changes the denominator, so the order of entry matters.
The baseline sum for actual values 1, 2, 3
The mean of 1, 2 and 3 is 2. The deviations from the mean are minus 1, 0 and 1, and their squares are 1, 0 and 1, so the total sum of squares is 2. This number is the denominator for every case on this page, since the actual list does not change. It represents the error of a forecaster who ignores everything and predicts 2 each time. Any prediction list is then judged by whether its squared errors fall below or above 2. Because the denominator depends only on the actual values, two different prediction lists for the same actual list can be compared directly by their scores, and the one with the smaller squared error always has the higher R squared.
Perfect predictions and mean-only predictions
If the predictions equal the actual values, 1, 2, 3, every error is zero, the error sum is 0, and R squared is 1 minus 0 divided by 2, which is 1. If every prediction is the mean, 2, 2, 2, the errors are minus 1, 0 and 1, the squared errors sum to 2, and R squared is 1 minus 2 divided by 2, which is 0. The calculator returns 1 and 0 for these lists. Zero marks the point where the predictions are exactly as good, by this measure, as always predicting the average.
Worse than the baseline
Now take predictions 3, 2, 1, the actual list reversed. The errors are minus 2, 0 and 2, so the squared errors are 4, 0 and 4, summing to 8. R squared is 1 minus 8 divided by 2, which is 1 minus 4, or minus 3. The calculator returns minus 3. The predictions are four times worse than the baseline in squared error, and R squared reports that as a negative number. A negative value therefore means the predictions do worse than a constant guess equal to the mean of the actual values.
Intermediate examples
Predictions of 1, 2, 4 have errors 0, 0 and minus 1, with squared error sum 1, so R squared is 1 minus 1 divided by 2, which is 0.5. Predictions of 1.5, 2, 2.5 have errors of 0.5, 0 and minus 0.5, with squared errors summing to 0.5, so R squared is 0.75. Predictions of 2, 3, 4, each too high by 1, have squared errors summing to 3, so R squared is 1 minus 1.5, which is minus 0.5. The consistent bias of one unit is enough to push the score below zero, because the baseline total is only 2, and a bias of that size is large relative to the spread of the actual values.
Why a score below zero is possible
Many people first meet R squared as the square of a correlation coefficient, which cannot be negative. That identity holds for the fit of a least-squares line to the data it was fitted on, where the intercept guarantees the model does at least as well as the mean. When predictions come from somewhere else, such as a model applied to new data, nothing guarantees this. The error-ratio definition then permits negative values, and scikit-learn's documentation for its r2_score function states that the score can be negative because a model can be arbitrarily worse.
Not the same as squaring Pearson correlation
The actual list 1, 2, 3 and the predicted list 3, 2, 1 have a Pearson correlation of minus 1, and its square is 1. Yet this calculator returns minus 3 for them. The predictions are perfectly anticorrelated with the actual values, which is a strong linear relationship in the wrong direction, but R squared in the error-ratio form measures closeness to the actual values, not strength of association. A prediction that is a perfect linear function of the actual values, such as 10, 20, 30, can have a correlation of 1 and a large negative R squared, since its errors are large.
Using the score responsibly
R squared compares predictions with a mean baseline on one set of actual values. It says nothing about how a model will do on other data, whether the errors are acceptable for a purpose, or whether the relationship is causal. A high score on a short list can occur by chance, and a score near zero is not always a failure if the target is hard to predict. The lists here have three entries, which is far too few to say anything about a model, and they were chosen so the arithmetic fits in a line. Report R squared together with the error measure in the original units, and with the number of observations behind it.
Reading the three controls together
The three examples on this page span the useful landmarks: 1 for a perfect match, 0 for the mean baseline and minus 3 for a prediction that does harm relative to the baseline. When you see an R squared in a report, locating it relative to these landmarks tells you what kind of statement it is. A value of 0.8 means the squared error is 20 percent of the baseline. A negative value means the baseline would have done better. These are descriptions of the supplied lists only. A model that was fitted on one data set and evaluated on another is the usual place a negative score appears in practice, and it is a useful warning that the fit does not transfer.
Reproduce the checks
Worse than the mean baseline
Open the R Squared Calculator and enter:
- Actual
1,2,3- Predicted
3,2,1
Expected output:
R squared
-3Squared prediction error is four times the baseline sum.
Mean-only predictions
Open the R Squared Calculator and enter:
- Actual
1,2,3- Predicted
2,2,2
Expected output:
R squared
0The error equals the baseline sum.
Perfect predictions
Open the R Squared Calculator and enter:
- Actual
1,2,3- Predicted
1,2,3
Expected output:
R squared
1The squared prediction error is zero.
References and implementation scope
The examples describe the behavior of the linked Computandum tools. A successful local check does not establish that another application accepts the same format or applies the same limits.