# GLM with random factors for observational design

I would love your help: I have 50 houses where I have counted the number of Mosquito's eggs once a week for 4 months. I have 5 fixed factors (temperature, NDVI, precipitations, etc) and I want to add 2 random factors: the number of the house (because I measured each several times) and time of the year (then the activity of Mosquito's changes a lot in time).

My problem is that I'm not sure about the syntaxis in R, I have tried: Model1= glmer.nb(Eggs~T°+Pp+NDVI+(1|house) + (1|Date), data=HueRC) But I'm not sure about the random slopes or intercepts or if I should make the interaction between house and date.

Another doubt is that some of the differences between the houses in my analysis are due to fixed factors that I use, such as NDVI; and the same happens with Date that might be partially explained by temperature or precipitations. I don't know if that is wrong.

In the picture, the number of eggs in time and the linear model for each house.

• Can you explain what the lines in the graph represent? Are they the line specific to each house? Commented Jan 30, 2020 at 18:44
• Yes, I edited the post! Thank you! Commented Feb 3, 2020 at 17:03

Based on your graph, it appears that the association between time and the number of eggs varies across houses. To test this model, your syntax would be:

Model2 <- glmer.nb(Eggs ~ temp + Pp + NDVI + Date + (Date|house), data=HueRC)


The (Date|House) syntax allows the association between Date and Eggs to vary across Houses. I would suggest that you test this model against a simpler model that forces the association between Date and Eggs to be the same across houses:

Model2a <- glmer.nb(Eggs ~ temp + Pp + NDVI + Date + (1|house), data=HueRC)
anova(Model2a, Model2)


If the likelihood ratio test is significant, then you would favor the more complicated model (Model2) and if it is not significant, then you would favor the more parsionious model (Model2a).

The model in your original post tests something entirely different. It treats Date as a random intercept, meaning that you believe the number of eggs laid on a given date is going to be more highly correlated than the number of eggs laid on different dates.

To give you an analogy from my field of education, we are interested in measuring how much children's learning changes over time, and that would be better represented by the (Date|House) model whereas if we believed that children's learning was systematically influenced by when it was measured, we would use the (1|Date) + (1|House) specification. Note that in the latter model, the Date by House interaction is absorbed into the residual error term along with any other effect specific to $$House_i$$ in $$Date_j$$.