# How to build a Generalized Linear Mixed Model with repeated measures in R

I am trying to analyze a dataset where I have individuals from 3 different species (essentially 3 'treatments'), and I score each individual on a cognitive test once per day, for 10 days. Essentially, I am interested in knowing if there are significant differences between species (treatments) in their ability to learn this cognitive task. 'Score' is my response variable, and consists of counts, so I'm thinking this represents a Poisson distribution. I am confused about how to incorporate the fact that 'Day' is a repeated measure within individuals, while 'Species' is a between-individual factor with 3 levels. I'm also unsure whether any of my variables are nested, and if I need to incorporate that into my syntax. I think I need to use the 'glmer' function in R, from the 'lme4' package. So far, I've come up with this:

m1<-glmer(Score~Species*Day + (1|Subject), family = poisson, data=myData)

As far as I can tell, this model considers 'Treatment' and 'Day' as fixed effects, as well as the interaction between them. The model also considers 'Subject' as a random effect (which is what I think the '1|x' notation does). However, I don't think this accounts for any nesting of the data, and again, I'm not sure if that is important here. Also, I'm not sure if 'Day' also needs to be incorporated as a random effect.

I also have sex information about all of my subjects, and I would potentially be interested in knowing if there are sex differences in the performance on the cognitive task, although this is not my primary focus. I assume sex could also be considered as a random effect?

Any and all criticisms welcome!

'Score' is my response variable, and consists of counts, so I'm thinking this represents a Poisson distribution.

That's a reasonable first guess, although you should definitely check for overdispersion (after running the model)

However, I don't think this accounts for any nesting of the data, and again, I'm not sure if that is important here. Also, I'm not sure if 'Day' also needs to be incorporated as a random effect.

I think you're fine. The fact that individuals are nested within species gets take care of automatically. You might want (1) to include variation among individuals in their learning rate, i.e. use (Day|Subject); (2) allow for temporal trends other than (log-)linear, using polynomials (poly()) or splines (splines::ns()) or generalized additive mixed models (gamm4 package).

I also have sex information about all of my subjects ... I assume sex could also be considered as a random effect?

You should probably include sex as a fixed effect, e.g.

Score~Species*Sex*Day + (Day|Subject)

• Thanks for the help, Ben. I tried including the random effects you suggested - Score~SpeciesSexDay + (Day|Subject), and I ended up with the error: Error: number of observations (=155) < number of random effects (=162) for term (Day | Subject); the random-effects parameters are probably unidentifiable... any idea what this means? – auzzie599 Sep 16 '16 at 19:53
• Is Day a factor or a numeric (continuous) variable? My advice is predicated on the latter ... – Ben Bolker Sep 17 '16 at 2:15