3
$\begingroup$

I want to fit a model with 5 levels. All levels are hierarchical. That is, level 5 is nested in level 4, level4 is nested in level 3, etc. level 2 is nested in level 1. All levels are factors.

Single level and two level models are working well:

model1 <- lmer(y ~ 1 + (1 | level1), data=data)
model2 <- lmer(y ~ 1 + (1 | level1/level2), data=data)

I wish to fit a model including all levels. What is the appropriate syntax for lmer?

My ideas so far:

model3 <- lmer(y ~ 1 + (1 | level1 / level2 / level3 / level4 / level5), data=data)
model4 <- lmer(y ~ 1 + (1 | level1 / level2) + (1 | level2 / level3) + (1 | level3 / level4)+ (1 | level4 / level5) 

model3 produced this error:

Error: couldn't evaluate grouping factor level5:(level4:(level3:(level2:level1))) within model frame: try adding grouping factor to data frame explicitly if possible

which I can not interpret.

Can you guys hellp me out here?

$\endgroup$

2 Answers 2

1
$\begingroup$

You could manually create the nested grouping variable through combinations of the five categorical variables. That's what lmer does anyway.

$\endgroup$
2
$\begingroup$

See here for an example that might help:

https://rpsychologist.com/r-guide-longitudinal-lme-lmer

$\endgroup$

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.