[R] residuals of glm

Ben Bolker bbolker at gmail.com
Mon Sep 29 23:51:14 CEST 2014


Yuan Jian <jayuan2008 <at> yahoo.com> writes:

> 
> Hello,
> I found the residuals of glm is not the same as calculated manually.
>  >y=c(12,14,33,50,67,74,123,141,165,204,253,246,240)
> > t=1:13
> > m1=glm(y~t+I(t^2),family=poisson(link="log"))
> > coefficients(m1)[1]+coefficients(m1)[2]*log(t)+
> coefficients(m1)[3]*log(t^2)

 [snip]

> > log(y)-m1$residuals
>        1        2        3        4        5        6        7        8 
> 2.434906 2.890062 3.369962 3.775366 4.146170 4.456127 4.745377 4.982733 
>        9       10       11       12       13 
> 5.174013 5.326826 5.429551 5.499618 5.521138 
> 
> i hope the last two sentences have the same result. 
> could anyone help me out?
> thanks
> Jay

  I'm afraid you may be misunderstanding the way GLMs work.
A log link does *not* mean that the predictors get log-transformed,
nor does it mean that the observed response gets log-transformed; rather, it
means that the predicted response (i.e., the linear predictor based on
the underlying linear model) gets exponentiated.  You will have
better luck (1) summing coefficients * (untransformed) predictor variables;
(2) exponentiating the result; (3) comparing the differences
between the predicted and observed values to residuals(m1,"response")
(see ?residuals.glm).



More information about the R-help mailing list