I am trying to write a 3 level multilevel linear model in R, using lme4.
I've decided to use the data from this question as a guide: Crossed vs nested random effects: how do they differ and how are they specified correctly in lme4?
library("lme4")
dt <-read.table("http://bayes.acs.unt.edu:8083/BayesContent/class/Jon/R_SC/Module9/lmm.data.txt",
header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE)
> head(dt)
id extro open agree social class school
1 1 63.69356 43.43306 38.02668 75.05811 d IV
2 2 69.48244 46.86979 31.48957 98.12560 a VI
3 3 79.74006 32.27013 40.20866 116.33897 d VI
4 4 62.96674 44.40790 30.50866 90.46888 c IV
5 5 64.24582 36.86337 37.43949 98.51873 d IV
6 6 50.97107 46.25627 38.83196 75.21992 d I
But I would also like to introduce School-level variables.
dfSchool = data.frame(School = sort(unique(dt$school)),SchoolCat1.F = factor(c("S1","S2","S3","S2","S3","S1")),SchoolVar = (rnorm(6)))
> dfSchool
School SchoolCat1.F SchoolVar
1 I S1 -0.08933959
2 II S2 0.72704675
3 III S3 -2.42612923
4 IV S2 -0.78280022
5 V S3 -0.23886568
6 VI S1 -1.53925113
Using the data above as an example, and defining y:= extro, x1:= open, x2:= agree, x:=social, j:= class level, k:= school level, x4:= SchoolCat1.F, x5:= SchoolVar.
I want to write the following model:
how would it be represented?
I believem1 <- lmer(extro ~ open + agree + social + (1 | school/class), data = dt) means that only the intercept is varying.
Therefore m2 <- lmer(extro ~ (open|school/class) + (agree|school/class) + (social|school/class) + (1 | school/class), data = dt) would be a good start.
However, I do not know how to include School-level variables.

My data is different, just a bit hard to share, so I used this one instead. I am trying to predict laptime in a Formula 1 race, my first level is lap, second driver, and third circuit.
So, I noticed you did not specify which level comes first with (1 | School/classID). Isn't it needed?
– Luan Vieira Dec 10 '22 at 22:17(1|driver) + (1|circuit). – Erik Ruzek Dec 10 '22 at 23:01