Specifying the random part of a mixed-model in R Suppose ses is a level-1 predictor of students math score, and sector is a level-2 predictor denoting the type of schools (public vs. private).
Given above, are the following models legitimate (syntax-wise)?
(Note: I don’t think I can have sector randomly vary at the school level because sector itself is a school-level predictor, am I right?)
library(lme4)

hsb <- read.csv('https://raw.githubusercontent.com/rnorouzian/e/master/hsb.csv')

m1 <- lmer(math ~ ses+sector + (ses:sector | sch.id), data = hsb)
m2 <- lmer(math ~ ses+sector + (ses*sector | sch.id), data = hsb)

 A: The 2nd model may not be appropriate because it specifies random slopes for sector which is a level 2 variable, but if you have reason to think that it should vary by school, then it could be OK.
The first model may be appropriate if you have reason to believe the cross level interaction should vary by school, AND it is supported by the data. The problem with this model is that it corresponds somewhat to a model with a (fixed) interaction term but omitting the main effects, and that rarely make sense.

Edit:
I just want to expand on my remark above about fitting random slopes for a variable that does not vary within the grouping factor. Statistically speaking, we might have a dataset where such a model converges without warning. What I am trying to say is that I'm not sure whether I can make sense of such a model. I mean, maybe there are some edge cases, or pathological examples where it does make sense, but I would like to think that in real world situations it would not make sense to specify a model with random slopes for a variable that is constant within groups. One way to help think about this is to plot the data with the outcome on the $y$ axis, the variable in question on the $x$ axis and then to plot a seperate line for each group. This will be impossible since each group's data points will occupy the same value for the $x$ variable and the slope will be undefined.
I should probably admit at this point, to utilising way too much sarcasm in some of my answers on this topic. My justification for doing so is to encourage analysts to think about what their models mean.
