Charles
2006-Aug-25 16:49 UTC
[R] R.squared in Weighted Least Square using the Lm Function
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. It does not seem that the r.squared for the weighted case is an ordinary 1-RSS/TSS. What is that precisely? 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> Thank you in advance for helping! Charles
Prof Brian Ripley
2006-Aug-25 17:17 UTC
[R] R.squared in Weighted Least Square using the Lm Function
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