# How to nest in a generalized linear model in R?

I have 108 counts taken from 36 field points. I took three counts from each point at different times, also, the 36 field points are divided in a altitude factor of three levels (12 points at each level). I have also six numerical independent variables measured at each count. I want to make a generalized linear model to see which of my independent variables contribute significantly to the counts observed (time is a factor of three levels).

I know that if I had only factors I would use a split-plot design to see differences among levels, where I know how to nest, but I have six numerical independent variables. Transform data to do a lm is not working, that´s why I am trying to use a glm().

My question is, how can I nest my data in a glm() function?

Sounds to me like you should use a mixed effects model. Function lmer() in package lme4 also provides a family argument.

• Yes, i agree, and I've deleted my own answer. I was confused because the OP sounded like the original model was fit with lm(), which turns out not to have been the case. – Peter Ellis Jun 12 '12 at 19:50

In SPSS you can nest with GLM, this is also possible with R - which I discovered 10 minutes ago.

fit1 <- glm(pay~gender/year, data=Dat, family=Gamma(link = "log"))


Here I nest gender within year. However as Roland pointed out, a mixed effects model is the best choice. It is more flexible and has lower type 1 error. Using lme4 package you can use a mixed model:

library(lme4)
fm1 <- lmer(log(pay) ~ gender+(1 | year), Dat, na.action=na.omit, REML=FALSE)