[Rd] weighted.residuals for glm objects (PR#7961)

henric.nilsson@statisticon.se henric.nilsson at statisticon.se
Tue Jun 21 19:13:22 CEST 2005


Full_Name: Henric Nilsson
Version: 2.2.0 (2005-06-20 r34776)
OS: Windows 2000
Submission from: (NULL) (213.115.23.26)


The help page for `weighted.residuals' states that the function can be used with
both `lm' and `glm' objects. However, it's unclear what's meant by the following
passage

"Weighted residuals are the usual residuals Ri, multiplied by wi^0.5, where wi
are the weights as specified in lm's call."

when it comes to a GLM. What's "...usual residuals..." in this context?

After reading the code it's clear that the function multiplies the deviance
residuals by wi^0.5, but this doesn't seem sensible for a GLM. Consider e.g.

> set.seed(1)
> x <- runif(10)
> y <- x + rnorm(10)
> w <- 0:9
> weighted.residuals(lm(y ~ x, weights = w))
         2          3          4          5          6          7          8 
 0.3845201  1.0361636  1.2689516 -0.9817686  3.7205310  1.3823979 -1.5458433 
         9         10 
-6.2029822  2.6149474 
> weighted.residuals(glm(y ~ x, weights = w))
          2           3           4           5           6           7 
  0.3845201   1.4653567   2.1978886  -1.9635372   8.3193602   3.3861695 
          8           9          10 
 -4.0899170 -17.5446831   7.8448423 

I suggest that the code for `weighted.residuals' is changed to

weighted.residuals <- function (obj, drop0 = TRUE) 
{
    w <- weights(obj)
    r <- residuals(obj, type = "deviance")
    if (is.null(w)) 
        r
    else if (drop0) 
        r[w != 0]
    else r
}

which seems to do the "right thing" for both `lm' and `glm' objects.



More information about the R-devel mailing list