How do I specify which variables are at which levels in a hierarchical linear model? A reviewer has suggested I do a hierarchical linear model for a journal article, but none of the tutorials I could find online: (Example: 1, 2, 3, 4...) were helpful.
I want to construct a hierarchical linear model that has multiple predictor variables on each level. I’m using the lme4 library in R. I want to see if the number of science activities a child completes predicts their score on a science test while controlling for multiple student and classroom-level variables.
The outcome variable is children's score on a science test. Level 1 of the model is the number of science activities children completed (or whether they were in the control group). Level 2 is the student level; I have multiple student variables (e.g., age, race, gender). Level 3 is the classroom level; I also have multiple classroom variables (e.g., science programs teachers already use, years a teacher has been teaching, teacher’s undergraduate major).
The tutorials have me thinking that my model should look something like this:
model1 <- lmer(testscore ~ numsciactivities + (1 | participant) + (1 | classroom), REML = FALSE, data = mydata)

However, that model doesn’t control for any student or classroom variables. So if I were to try doing that, I would expect my model to look something like this:
model2 <- lmer(testscore ~ numsciactivities + age + race_white + race_black + race_asian + gender + (1 | sciprogram) + yrsteaching + (1 | teachermajor), REML = FALSE, data = mydata)

or maybe this:
model3 <- lmer(testscore ~ numsciactivities + (1 + age + race_white + race_black + race_asian + gender | participant) + (1 + sciprogram + yrsteaching + teachermajor | classroom), REML=FALSE, data=mydata)

The problem is that I’m not seeing anywhere where levels 1, 2, and 3 of the model are explicitly specified. How do I correctly specify which variables are at which levels, or do I not need to do so? Also, could I use the control group as the reference category of numsciactivities?

Some notes on your model:

*

*You have coded race multiple times as variable. Don't. It makes far more sense to code it as one variable, Race, then it does to code it as several dichotomous variables.

*Participants are technically nested within classrooms in this setting. So your random effects should at minimum look something like (1|classroom/participant). There is also this thread on random effects structures for nested effects. I also advise looking at the syntax page of the original lme4 package article on how to specify the model.

*You can include as many random slope terms as you want, but keep in mind that the more random effects parameters you include, the harder it will be to converge. See this article on the subject..

*If you need good tutorials on HLM, I suggest a few other articles below as citations. I think they are pretty straight forward and helpful in ways that most HLM books and articles are not.

Citations:

*

*Brown, 2021

*Harrison et al., 2018

*Meteyard & Davies, 2020
Edit
By the way, I looked through the lmer article by Bates and this is the breakdown if you want to look. The g1 here would be your classroom and the g2 would be the participant:

