[R] extracting residuals from glm object
Marc Schwartz
marc_schwartz at me.com
Tue May 12 14:13:35 CEST 2009
On May 12, 2009, at 3:50 AM, utkarshsinghal wrote:
> Hi All,
>
> Can anybody explain why the following three ways of extracting
> residuals from a glm object are giving me different outputs:
>
> > idv = runif(1000,0,1)
> > dv = rbinom(1000,1,0.5)
> > d = data.frame(idv,dv)
> > fit = glm(dv~idv, data=d, family=binomial)
>
> > head(residuals(fit))
> 1 2 3 4 5 6
> 1.216862 -1.161059 -1.156795 1.204759 -1.141068 1.201437
>
> > head(fit$residuals)
> 1 2 3 4 5 6
> 2.096724 -1.962126 -1.952454 2.066224 -1.917492 2.057981
>
> > head(d$dv-fit$fitted.values)
> 1 2 3 4 5 6
> 0.5230655 -0.4903489 -0.4878241 0.5160253 -0.4784855 0.5140869
set.seed(1)
idv <- runif(1000, 0, 1)
d <- data.frame(idv, dv)
fit <- glm(dv ~ idv, data = d, family = binomial)
> head(fit$residuals)
1 2 3 4 5 6
-1.957016 -1.960477 -1.967029 -1.978074 -1.954949 -1.977749
> head(residuals(fit, type = "working"))
1 2 3 4 5 6
-1.957016 -1.960477 -1.967029 -1.978074 -1.954949 -1.977749
> head(d$dv - fit$fitted.values)
1 2 3 4 5 6
-0.4890179 -0.4899201 -0.4916190 -0.4944577 -0.4884778 -0.4943746
> head(residuals(fit, type = "response"))
1 2 3 4 5 6
-0.4890179 -0.4899201 -0.4916190 -0.4944577 -0.4884778 -0.4943746
See ?glm and ?residuals.glm and read the information there regarding
the type of residuals stored in the glm model object as opposed to the
multiple types of residuals that can be returned by residuals.glm().
See the references in ?residuals.glm for more information as per the
Details section therein.
HTH,
Marc Schwartz
More information about the R-help
mailing list