[R] R-squared value for linear regression passing through origin using lm()

Thomas Lumley tlumley at u.washington.edu
Fri Oct 19 17:58:37 CEST 2007


On Fri, 19 Oct 2007, Ralf Goertz wrote:
=
> Thanks to Thomas Lumley there is another convincing example. But still
> I've got a problem with it:
>
>> x<-c(2,3,4);y<-c(2,3,3)
>
>> 1-2*var(residuals(lm(y~x+1)))/sum((y-mean(y))^2)
>
> [1] 0.75
>
> That's okay, but neither
>
>> 1-3*var(residuals(lm(y~x+0)))/sum((y-0)^2)
> [1] 0.97076
>
> nor
>
>> 1-2*var(residuals(lm(y~x+0)))/sum((y-0)^2)
> [1] 0.9805066
>
> give the result of summary(lm(y~x+0)), which is 0.9796.
>

You want

R> 1-sum(residuals(lm(y~x+0))^2)/sum((y-0)^2)
[1] 0.9796238

It isn't var(residuals)*(n-1) but sum(residuals^2) that you want.  They are not the same in a zero-intercept model, because the residuals need not have mean zero.

         -thomas


Thomas Lumley			Assoc. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle



More information about the R-help mailing list