I want to acknowledge that this is a trivial edge case. As far as I know, the only case where this will occour is when fitting an n degree polynomial(with a constant term) to constant data. For example:

x = [1,2,3], y = [1,1,1]

when using least squares to fit a line to this data, it would trivially be the horizontal line y = 1. This seems to generate a case where: $$ SSE = \sum_1^n{(y_i - \hat{y_i})^2} = 0\\ SST = \sum_1^n{(y_i - \bar{y})^2} = 0\\ $$ I am calculating R^2 as: $$ R^2 = 1-\frac{SSE}{SST} $$ How should this be handled. Is it undefined, 0, 1? I can think of arguments for all.

  1. Undefined: Divide by 0
  2. [1]: Intuitively, the line y = 1 perfectly fits the data suggesting that an R^2 of 1.
  3. [0]: If SSE = SST then R^2 = 1-1 = 0. Furthermore from an high level standpoint, R^2 measures how good a fit is when compared to fitting the data with a horizontal line(the mean). From some other googling, it seems that a horizontal fit is treated as R^2 = 0 regardless. I found the following post, however it only addresses the case where SSE = 0 not SST = 0 and I am struggling to justify this convention. (Why is R squared zero when the best-fit line is horizontal?)

Opinions? Are there other problems that hit this edge case?

  • $\begingroup$ That $R^2$ may be viewed as the magnitude of the response projected onto the column space relative to the magnitude of the response is some small argument for 2. $\endgroup$ Nov 10, 2022 at 18:52
  • 1
    $\begingroup$ Data must vary (Y is a variable), otherwise it is a degenerate case (given that you request a constant term) and can be regarded an exception. $\endgroup$
    – ttnphns
    Nov 11, 2022 at 1:32
  • $\begingroup$ FWIW, R seems to lean towards 1: summary(lm(c(1,1,1)~I(1:3))) $\endgroup$ Nov 11, 2022 at 11:29
  • $\begingroup$ That's the issue i'm having. Based on what @ttnphns said above this seems to be a case where r^2 is meaningless, but there doesn't seem to be a convention as to how to handle it. in MATLAB, fit(x,y,n) produces a NaN, in SciPy scipy.stats.linregress(x,y) produces a 0 $\endgroup$
    – Ben
    Nov 11, 2022 at 13:59
  • $\begingroup$ ... and in some other software that produces an error message "Y is constant and is removed from the analysis" $\endgroup$
    – ttnphns
    Nov 11, 2022 at 14:41

2 Answers 2


You should just continue to think of R2 as undefined in this situation. Thinking of it as 1 or zero obscures what's really go on here.

R2 answers the question of "what percent of the variance in Y is being explained by the model?" But in this case Y has no variance. So there is nothing to be explained. The question is ill formed.

Asking what R2 "really" is in this situation is analogous to asking "what percentage of this circle is green?" when the area of the circle is zero. Someone could say "It's 100% - no part of the circle is NOT green!" and someone else could say "zero percent - no part of the circle IS green." But the correct answer is: "there is no circle in the first place, so the question can't be answered."


$R^2$ in this situation is UNINTERESTING. (This is not to say, however, that the question is not worth asking, so this statement is not a criticism of the OP.)

The point of regression is that we want tight estimates of some variable ($y$) of interest. When we just have $y$, we might have more variability than is desired. Consequently, we measure some other variables (features) that influence $y$. This way, we can use those features to explain some, preferably all, of the variability in $y$.

When there is no variability in $y$, there is no regression worth doing, and any statistics derived from such a regression are worthless. This situation is uninteresting from a statistical standpoint.

  • 2
    $\begingroup$ And undefined, mathematically speaking. $\endgroup$
    – Tim
    Mar 20, 2023 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.