# Re: [R] Strange R squared, possible error

From: Berwin A Turlach <berwin_at_maths.uwa.edu.au>
Date: Fri, 18 Mar 2011 09:05:16 +0800

G'day Gabor,

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

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

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Fri 18 Mar 2011 - 01:07:55 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 18 Mar 2011 - 01:40:23 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.