[R] Weighted least squares

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Jan 18 08:37:22 CET 2005


On Mon, 17 Jan 2005, Ming Hsu wrote:

> I would like to run a weighted least squares with the the weighting 
> matrix W.

This is generalized not weighted least squares if W really is a matrix and 
not a vector of case-by-case weights.

> I ran the following two regressions,
>
> (W^-1)Y = Xb + e
> Y = WXb+ We

If W is a diagonal matrix, the weights for the second are W^(-2) and you 
used W below.

> In both cases, E[bhat] = b.
>
> I used the following commands in R
>
> lm1 <- lm(Y/W ~ X)
> lm2 <- lm(Y ~ W:X, weights = W)
>
> where
>
> Y <- rnorm(10,1)
> X <- Y + rnorm(10,1)
> W <- 1:10

That W is not a matrix!

> In lm2, I believe W is applied to the error term, resulting in WLS.  However
> the estimated coefficients in lm1 and lm2 are really different.  I tried glm
> as well, but the result was the same as lm.
>
> Any advice would be appreciated,

Please do check that you supply an example that agrees with your words.

Use lm.gls in MASS or gls in nlme for generalized least squares, if that 
is what you meant.

-- 
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