# [R-sig-ME] Understanding residuals in a mixed GAMM

Bob Farmer farmerb at gmail.com
Thu Jun 23 21:41:01 CEST 2011

```Hi, I'm trying to get an appropriate set of residuals to check a
Poisson mixed GAMM's fit, but I've found a few different methods to
use on the gamm4() object that yield inconsistent results.  Below is a
working example of 4 ways to generate residuals using one of the gam
or mer elements in a gamm4() object, and a demonstration that only two
of those approaches are the same.  In Wood (2006), there's a mention
that residuals should be calculated using fitted values off the gam
element that also incorporate the random effects, but it's not clear
to me what's the best way to arrive at this.

Thanks very much.
--Bob Farmer
PhD candidate, Dalhousie University

#example
require(gamm4); require(mgcv); require(lme4)
set.seed(0)
dat <- gamSim(6,n=400,scale=.2,dist="poisson")
b<-gamm4(y~s(x0), random = ~ (1|fac), family=poisson,
data=dat)
r1<-as.vector(residuals(b\$gam))
r2<-as.vector(b\$gam\$residuals)
r3<-as.vector(b\$mer at resid)
r4<-as.vector(log(dat\$y)-predict(b\$gam))

residsDF<-data.frame(
values = c(r1,r2,r3,r4),
group = rep(c("r1","r2","r3","r4"), each = nrow(dat))
)

with(residsDF, boxplot(values ~ group))

assign("differences", with(residsDF,
data.frame(
d1=mean(r1-r2),
d2=mean(r1-r3),
d3=mean(r1-r4),
d4=mean(r2-r3),
d5=mean(r2-r4),
d6=mean(r3-r4)
)))
differences
#so r2 and r3 are equivalent

```