# Re: [R] R.squared in Weighted Least Square using the Lm Function

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Sat 26 Aug 2006 - 03:17:04 EST

On Fri, 25 Aug 2006, Charles wrote:

> Hello all,
> I am using the function lm to do my weighted least
> square regression.
>
> model<-lm(Y~X1+X2, weight=w)
>
> What I am confused is the r.squared.

What r.squared? There is no r.squared in that object, but it is calculated by the summary method.

> It does not seem that the r.squared for the weighted
> case is an ordinary 1-RSS/TSS.
> What is that precisely?

Precisely that, with weights in the SS. The code is

```    r <- z\$residuals
f <- z\$fitted
w <- z\$weights
if (is.null(w)) {
mss <- if (attr(z\$terms, "intercept"))
sum((f - mean(f))^2)
else sum(f^2)
} else {
mss <- if (attr(z\$terms, "intercept")) {
m <- sum(w * f/sum(w))
sum(w * (f - m)^2)
}
else sum(w * f^2)
r <- sqrt(w) * r
```

}

> Is the r.squared measure comparable to that obtained
> by the ordinary least square?
>
> <I also notice that
> model\$res is the unweighted residual while
> summary(model)\$res is the weighted residual>

Yes, as documented with added emphasis in ?summary.lm .

```--
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help