[R] DIfference between weights options in lm GLm and gls.

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Thu Mar 23 19:15:47 CET 2006


Hi, Spencer,

For your call to gls you actually want:

fit.gls.w <- gls(y~x, data=DF, weights=varFixed(~1/w))

HTH,

--sundar

Spencer Graves wrote:
> 	  In my tests, "gls" did NOT give the same answers as "lm" and "glm",
> and I don't know why;  perhaps someone else will enlighten us both.  I
> got the same answers from "lm" and "glm".  Since you report different
> results, please supply a replicatable example.
> 
> 	  I tried the following:
> set.seed(1)
> DF <- data.frame(x=1:8, xf=rep(c("a", "b"), 4),
>         y=rnorm(8), w=1:8, one=rep(1,8))
> fit.lm.w <- lm(y~x, DF, weights=w)
> fit.glm.w <- glm(y~x, data=DF, weights=w)
> fit.gls.w <- gls(y~x, data=DF,
>                 weights=varFixed(~w))
> 
> 
>>coef(fit.lm.w)
> 
> (Intercept)           x
>  -0.2667521   0.0944190
> 
>>coef(fit.glm.w)
> 
> (Intercept)           x
>  -0.2667521   0.0944190
> 
>>coef(fit.gls.w)
> 
> (Intercept)           x
>  -0.5924727   0.1608727
> 
> 	  I also tried several variants of this.  I know this does not answer
> your questions, but I hope it will contribute to an answer.
> 	
> 	  spencer graves
> 
> Goeland wrote:
> 
> 
>>Dear r-users£¬
>>
>>Can anyone explain exactly the difference between Weights options in lm glm
>>and gls?
>>
>>I try the following codes, but the results are different.
>>
>>
>>
>>
>>>lm1
>>
>>
>>Call:
>>lm(formula = y ~ x)
>>
>>Coefficients:
>>(Intercept)            x
>>     0.1183       7.3075
>>
>>
>>
>>>lm2
>>
>>
>>Call:
>>lm(formula = y ~ x, weights = W)
>>
>>Coefficients:
>>(Intercept)            x
>>    0.04193      7.30660
>>
>>
>>
>>>lm3
>>
>>
>>Call:
>>lm(formula = ys ~ Xs - 1)
>>
>>Coefficients:
>>     Xs      Xsx
>>0.04193  7.30660
>>
>>Here ys= y*sqrt(W), Xs<- sqrt(W)*cbind(1,x)
>>
>>So we can see weights here for lm means the scale for X and y.
>>
>>But for glm and gls I try
>>
>>
>>
>>>glm1
>>
>>
>>Call:  glm(formula = y ~ x)
>>
>>Coefficients:
>>(Intercept)            x
>>     0.1183       7.3075
>>
>>Degrees of Freedom: 1242 Total (i.e. Null);  1241 Residual
>>Null Deviance:      1049000
>>Residual Deviance: 28210        AIC: 7414
>>
>>
>>>glm2
>>
>>
>>Call:  glm(formula = y ~ x, weights = W)
>>
>>Coefficients:
>>(Intercept)            x
>>     0.1955       7.3053
>>
>>Degrees of Freedom: 1242 Total (i.e. Null);  1241 Residual
>>Null Deviance:      1548000
>>Residual Deviance: 44800        AIC: 11670
>>
>>
>>>glm3
>>
>>
>>Call:  glm(formula = y ~ x, weights = 1/W)
>>
>>Coefficients:
>>(Intercept)            x
>>    0.03104      7.31033
>>
>>Degrees of Freedom: 1242 Total (i.e. Null);  1241 Residual
>>Null Deviance:      798900
>>Residual Deviance: 19900        AIC: 5285
>>
>>
>>
>>>glm4
>>
>>
>>Call:  glm(formula = ys ~ Xs - 1)
>>
>>Coefficients:
>>   Xs    Xsx
>>2.687  6.528
>>
>>Degrees of Freedom: 1243 Total (i.e. Null);  1241 Residual
>>Null Deviance:      4490000
>>Residual Deviance: 506700       AIC: 11000
>>
>>With weights, the glm did not give the same results as lm why?
>>
>>Also for gls, I use varFixed here.
>>
>>
>>
>>>gls3
>>
>>Generalized least squares fit by REML
>>  Model: y ~ x
>>  Data: NULL
>>  Log-restricted-likelihood: -3737.392
>>
>>Coefficients:
>>(Intercept)           x
>> 0.03104214  7.31032540
>>
>>Variance function:
>> Structure: fixed weights
>> Formula: ~W
>>Degrees of freedom: 1243 total; 1241 residual
>>Residual standard error: 4.004827
>>
>>
>>>gls4
>>
>>Generalized least squares fit by REML
>>  Model: ys ~ Xs - 1 
>>  Data: NULL
>>  Log-restricted-likelihood: -5500.311
>>
>>Coefficients:
>>      Xs      Xsx
>>2.687205 6.527893
>>
>>Degrees of freedom: 1243 total; 1241 residual
>>Residual standard error: 20.20705
>>
>>We can see the relation between glm and gls with weight as what
>>
>>I think,  but what's the difference between lm wit gls and glm? why?
>>
>>Thanks so much.!
>>
>>Goeland
>>
>>	
>>
>>Goeland
>>goeland at gmail.com
>>2006-03-16
>>
>>
>>
>>------------------------------------------------------------------------
>>
>>______________________________________________
>>R-help at 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
> 
> 
> 
> ------------------------------------------------------------------------
> 
> ______________________________________________
> R-help at 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




More information about the R-help mailing list