Lm (GLM) gives negative R2

Hi, I’m using lm to estimate R2 for a regression. The data is not real data, and y and x are pretty much the same. I would expect a R2 close to 1, however, I got -Inf or other negative values. I’m not sure why. A replicable simple example is below:

reg = lm([ones(100) ones(100)], log.(ones(100).*10))


R^2 may be defined as 1-RSS/TSS, where TSS is the sum of squared deviations from the mean of the dependent variable. In your case, the dependent variable has no variation, so the sum of deviations from the mean is zero. I think you must always get -Inf, I don’t see how you could get another negative value. However, the regressor matrix is also singular in your example, and I’m not sure how the GLM package deals with that, so perhaps I’m wrong about that.


I see, thanks. I will check the code of GLM.