# Lm (GLM) gives negative R2

**URL:** <https://discourse.julialang.org/t/lm-glm-gives-negative-r2/92346>\
**Category:** General Usage\
**Created:** [December 31, 2022, 2:05am UTC](https://discourse.julialang.org/t/lm-glm-gives-negative-r2/92346 "2022-12-31T02:05:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jgr](https://avatars.discourse-cdn.com/v4/letter/j/cdc98d/32.png) [@jgr](https://discourse.julialang.org/u/jgr)\
**Post date:** [December 31, 2022, 2:05am UTC](https://discourse.julialang.org/t/lm-glm-gives-negative-r2/92346/1 "2022-12-31T02:05:08Z")

</div>

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))
> 
> r2(reg)

---

<div class="post-metadata">

**Author:** ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)\
**Post date:** [December 31, 2022, 12:37pm UTC](https://discourse.julialang.org/t/lm-glm-gives-negative-r2/92346/2 "2022-12-31T12:37:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jgr](https://avatars.discourse-cdn.com/v4/letter/j/cdc98d/32.png) [@jgr](https://discourse.julialang.org/u/jgr)\
**Post date:** [December 31, 2022, 3:39pm UTC](https://discourse.julialang.org/t/lm-glm-gives-negative-r2/92346/3 "2022-12-31T15:39:48Z")

</div>

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