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)
        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@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
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Sat Aug 26 04:32:23 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sat 26 Aug 2006 - 06:22:52 EST.

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