I have to set up an LMM with just one independent variable and there is both monthly and yearly variation. I aim to get fixed effects coefficients (slope and intercept) for each month and year given as a random effect.

Can anyone give suggestions on how to code this? The below code just gives one intercept and a different slope in fixed effects:

summary(lmer(Y ∼ X+(1+month)+(1|year),data = dataset,REML="TRUE"))

Or should I input each month as a different dataset:

summary(lmer(Y ∼ X+(1|year),data = June,REML="TRUE"))
  • $\begingroup$ First, if you use a subset of data from just June, you are not including data for the other months. Also, what is the rationale for using month/year as random effects? $\endgroup$ Commented Feb 1, 2023 at 5:44

1 Answer 1


If you want the X effect on Y and random intercept of both year and month, the code would go

m1<-lmer(Y ∼ X+(1|month)+(1|year),data = dataset,REML="TRUE")

If you want random intercepts of month and year and random slopes of X for month and year, the code would go

m2<-lmer(Y ∼ X+(X|month)+(X|year),data = dataset,REML="TRUE")

However, m1 and m2 give the code assuming year and month are crossed random effects and I'm not sure whether months should actually be nested within years, in which case the codes would go

m3<-lmer(Y ∼ X+(1|year:month),data = dataset,REML="TRUE")
m4<-lmer(Y ∼ X+(X|year:month),data = dataset,REML="TRUE")

I think the crossed vs. nested might depend on the exact nature of your data. My intuition is that if you gathered your data from same individuals across years, then your random effects would be nested. However, if the actual observational units were different, I think they would be crossed, but I admit I'm not sure. Here is a good explanation of crossed vs. nested random effects.

I've also seen it recommended in a roughly comparable case that month would be entered as a fixed effect, which would go

m5<-lmer(Y ∼ X+factor(month)+(X|year),data = dataset,REML="TRUE")

but in the example case there were only 5 months so this advice may not apply as you have 12.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.