Understanding glm$residuals and resid(glm)

独自空忆成欢 提交于 2019-11-28 16:45:40
Ian Fellows

Calling resid(model) will default to the deviance residuals, whereas model$resid will give you the working residuals. Because of the link function, there is no single definition of what a model residual is. There are the deviance, working, partial, Pearson, and response residuals. Because these only rely on the mean structure (not the variance), the residuals for the quasipoisson and poisson have the same form. You can take a look at the residuals.glm function for details, but here is an example:

counts <- c(18,17,15,20,10,20,25,13,12)
outcome <- gl(3,1,9)
treatment <- gl(3,3)
glm.D93 <- glm(counts ~ outcome + treatment, family=quasipoisson())
glm.D93$resid


#working
resid(glm.D93,type="working")
(counts - glm.D93$fitted.values)/exp(glm.D93$linear)

#deviance
resid(glm.D93,type="dev")
fit <- exp(glm.D93$linear)
poisson.dev <- function (y, mu) 
    sqrt(2 * (y * log(ifelse(y == 0, 1, y/mu)) - (y - mu)))
poisson.dev(counts,fit) * ifelse(counts > fit,1,-1)

#response
resid(glm.D93,type="resp")
counts - fit

#pearson
resid(glm.D93,type="pear")
(counts - fit)/sqrt(fit)

I don't know enough about poisson and quasi-poisson distributions to answer your question in the depth asked for (i.e. an exact equation that will transform the variables into the residuals using the model), but if any of the confusion is due to what residual types are being used and why the two commands give a different answer, this could help:

resid() defaults to a "deviance" type in R. However, glm() assigns different residuals to the $residuals vector.

If you're using the quasi-poisson family, glm() will assign residuals of the working type, whereas, resid() gives the deviance type as default.

To try this out, you can use:

resid(glm,type="working")

and

glm$residuals

and that should give you the same answer (at least, it did on a sample dataset I used).

According to R, working residuals are: "the residuals in the final iteration of the IWLS fit"

If you look up the book: "Generalized Linear models and extensions" (by Hardin and Hilbe) on googlebooks, you can access section 4.5 which explains the various types of residuals.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!