[R] R.squared in Weighted Least Square using the Lm Function
Prof Brian Ripley
ripley at stats.ox.ac.uk
Fri Aug 25 19:17:04 CEST 2006
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)
rss <- sum(r^2)
} else {
mss <- if (attr(z$terms, "intercept")) {
m <- sum(w * f/sum(w))
sum(w * (f - m)^2)
}
else sum(w * f^2)
rss <- sum(w * r^2)
r <- sqrt(w) * r
}
ans$r.squared <- mss/(mss + rss)
That's the great thing about R: you can answer your own question by
reading the code for yourself.
> 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 at 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
More information about the R-help
mailing list