ADDRESS AND STATISTICAL CHECKS

Checking R squared when actual values have no spread

Predictions that match every actual value look like a perfect fit, yet R squared still cannot be computed when the actual values never vary. This guide shows why the denominator fails and how to tell that case from a real perfect fit.

A perfect match that still returns an error

Enter actual values 5, 5, 5 and predicted values 5, 5, 5 into the R squared calculator and the page refuses to give a number. That surprises people, because every prediction is right. The refusal comes from the formula, not from the quality of the fit, and it applies to any constant actual list, including 0, 0, 0 or 12, 12, 12, whatever predictions sit beside it. R squared is written as one minus the sum of squared prediction errors divided by the sum of squared deviations of the actual values from their own mean. The top of that fraction is zero here because the predictions are exact. The bottom is zero as well, because five minus the mean of five is zero in every row. The result is zero divided by zero, and no single value is the correct answer to that.

The two sums behind the ratio

The calculator uses R² = 1 - sum((actual - predicted)²) / sum((actual - meanActual)²). The first sum is often called the residual sum of squares and the second the total sum of squares. The total sum measures how much the actual values spread around their average, which is the spread a model is supposed to explain. Dividing by it turns the leftover error into a share of that spread. When the actual values are all equal, there is no spread to explain. A list of 5, 5, 5 has a total sum of squares of 0 + 0 + 0 = 0, so the share has no meaning, whatever the residual sum happens to be.

The control case with real spread

Compare actual values 4, 5, 6 with predictions 4, 5, 6. The mean of the actual list is 5, and the deviations are -1, 0 and 1, so the total sum of squares is 1 + 0 + 1 = 2. The residual sum is 0, because each prediction matches. The ratio is 0 / 2 = 0, and R² = 1 - 0 = 1. The calculator reports a clean 1. The only structural difference from the constant case is the denominator: 2 instead of 0. Row-by-row matching is identical in both examples, which shows that a perfect prediction by itself does not decide whether R squared exists.

Why the page does not substitute a number

Some tools return 1 for this situation, some return 0, and some return a missing value. Each choice can be defended in one sense and criticized in another, since the data contain no information about how well a model tracks variation that does not exist. This implementation rejects the input and names the problem, a constant actual list. That keeps a downstream spreadsheet from carrying an invented value forward as though it were a measured score. If you export results from several groups into one report, the rejection also tells you which group needs a different summary.

Imperfect predictions and a negative R squared

Once the actual values vary, the number behaves as expected. Take actual values 2, 4, 6 and predictions 3, 4, 5. The mean of the actuals is 4, so the total sum of squares is 4 + 0 + 4 = 8. The prediction errors are -1, 0 and 1, which gives a residual sum of 2. R² = 1 - 2/8 = 0.75. Now predict 6, 5, 4 for actuals 4, 5, 6. The residual sum is 4 + 0 + 4 = 8 against a total of 2, so R² = 1 - 8/2 = -3. A negative value means the predictions fit worse than simply using the mean of the actual values for every row.

Nearly constant data is fragile

The undefined case sits at the edge of a very sensitive region. Take actual values 5, 5, 5, 5, 6 and predict 5 for every row. The mean of the actuals is 5.2, the deviations are -0.2 four times and 0.8, and the total sum of squares is 0.16 + 0.64 = 0.8. The only error is the last row, which contributes 1 to the residual sum. R² = 1 - 1/0.8 = -0.25. One changed value moved the result from undefined to a negative number. When the actual values barely vary, a small error is large relative to the denominator, so R squared swings widely even though the predictions are close in absolute terms.

Where constant actual lists come from

A constant list rarely appears on purpose. It shows up when a filter keeps a single category, when a sensor reading is stuck at one value, when a group has been split down to a handful of identical rows, or when a column was copied with the wrong header. Before fitting anything, look at the minimum and maximum of the actual column. If they are equal, the question you are asking cannot be answered with R squared. Error measures that compare predictions to actual values directly, such as the mean absolute error or the root mean squared error, still work, because they do not divide by the spread of the actual values.

What this worksheet does and does not show

The two controls here are abstract arithmetic checks. They show how a denominator of zero is handled and how a nonconstant list behaves. They do not say whether a model suits a data set, whether a high R squared is enough for a decision, or whether the predictions came from a sound method. R squared also depends on the data set it is computed on, so the same model can score differently on training rows and on new rows. When you see the constant-actual rejection in your own work, treat it as information about the data rather than a fault of the calculator.

Reporting a group with no spread

Suppose you compute R squared separately for several groups, such as one fitted line per store, per sensor or per class. If one group has the same actual value in every row, leave its R squared blank in the report and write a short reason next to it, for example that all actual values equal 5. A blank with a reason is easier to audit than a zero or a one, because a reader who sees a 1 will assume the model tracked real variation. You can still show that group's largest and smallest prediction error, which carry information in the original units. If the group is large and constant, ask whether the measurement is saturated, capped by the instrument, or recorded with too coarse a resolution to show change.

Reproduce the checks

Constant perfect predictions

Open the R Squared Calculator and enter:

Actual
5,5,5
Predicted
5,5,5

Expected rejection:

constant actual list

Zero actual variation leaves this formula undefined.

Nonconstant perfect predictions

Open the R Squared Calculator and enter:

Actual
4,5,6
Predicted
4,5,6

Expected output:

R squared

1

The actual list has positive variation.

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.

Sources

Related tasks