How to use lmer for three-way interactions I am quite new to R, so this is maybe a "basic" question. I have a dataset with multiple imputed data (repeated measures). I want to do a mixed model and test a three-way interaction with lmer. However, I can't figure out how to set up this model correctly:
Sleep problems (sleep) should be the dependent variable.
The individual should be included as a random effect.
Time, treatment group and chronotype group should be included as covariates as follows:
main effect of time and chronotype group,
the two-way interactions treatment group × time and time × chronotype group,
and the three-way interaction treatment group × time × chronotype group.
All analyses should be adjusted for age and sex.
If it was a standard mixed model, I would have used lmer like:
lmer(sleep~ time*chronotype_group  + factor(treatment_group) + factor(sex) + age + (1|id)))

Would be grateful for help to adjust the model over to the wanted specifications stated above.

Three-way interactions require a lot of data for the model to converge and to detect the effect. But in case you're confident that your data is sufficiently powered, the code would go
model<-lmer(sleep ~ (1|id)+age+sex+factor(treatment_group)*chronotype_group*time, data=data)

if you want a linear effect of time. If you want time as factor, use factor(time) instead. This gives you all main effects, all 2-way interactions and the 3-way interaction while adjusting for age and sex. You need to include treatment group as a main effect if you want to add interactions involving this term, and all potential 2-way interactions if you want to include the 3-way interaction.
