[R] Strange R squared, possible error

Berwin A Turlach berwin at maths.uwa.edu.au
Fri Mar 18 02:05:16 CET 2011


G'day Gabor,

On Thu, 17 Mar 2011 20:38:21 -0400
Gabor Grothendieck <ggrothendieck at gmail.com> wrote:

> > Or am I missing something?

O.k., because the residuals don't add to zero, there may be a non-zero
correlation between residuals and fitted values, which messes up the
equation at the variance level.

> Try it on an example to convince yourself:
> 
> > fm <- lm(demand ~ Time, BOD)
> > var(fitted(fm)) + var(resid(fm)) - var(BOD$demand)
> [1] 3.552714e-15
> >
> > fm0 <- lm(demand ~ Time - 1, BOD)
> > var(fitted(fm0)) + var(resid(fm0)) - var(BOD$demand)
> [1] 59.28969

But, and this is of course the geometry of least squares:

R> sum(fitted(fm)^2) + sum(resid(fm)^2) - sum(BOD$demand^2)
[1] 0
R> sum(fitted(fm0)^2) + sum(resid(fm0)^2) - sum(BOD$demand^2)
[1] 2.273737e-13

and the reason why the formula changes if there is no (explicit)
intercept term in the model.

Cheers,

	Berwin



More information about the R-help mailing list