2
$\begingroup$

Let's say I predict that there will be sex differences in test scores depending on school. I also have a predictor at Level 1, which is age. School is a random factor.

y  age   school   sex  rank
12  18     X       m    A
45  22     X       f    B
57  21     Y       m    A 
88  23     Y       f    B

with this model:

y ~ age + sex + (1|school)

how does R know that sex is an individual characteristic and not a group characteristic? I read that in mixed-models it is important to determine which predictors are level 1 or level 2, but in this case I don't know how it is possible to make that distinction.

Conversely, what if my model is now:

y ~ age + rank + (1|school)

I predict that A-ranked schools will have better scores than B-ranked schools. Rank is a predictor at Level 2, so how does R know this? Or are sex and rank treated in the same way, even though they belong to different levels?

$\endgroup$

1 Answer 1

1
$\begingroup$

I am not sure how lme4 internally recognizes and deals with group predictor variables, but the logic of it is exactly what you pointed out. Individual variables vary within groups whereas group variables are constant within a group but differ between groups. The key to all this is telling lmer what your grouping structure is. If you had indicated a different variable as your group, e.g., school district, then the school variables would vary within districts and would be considered "lower level" predictors.

You can also calculate the school means of individual variables and include those means as predictors in your lmer model:

library(dplyr) 
df <- df %>% group_by(school) %>% mutate(mn_sex = mean(sex), mn_age = mean(age)) %>% ungroup()
m <- lmer(y ~ age + mn_age + sex + mn_sex + (1|school), data=df)

This models the within-school (age, sex) and between-school (mn_age, mn_sex) associations explicitly so that you can see the relations between these predictors and the outcome at the different levels represented in your model. In this formulation, the mn_ variable coefficients represent a test of whether the within- and between-school associations with the outcome are equivalent (non-significant mn_ coefficient) or different (significant mn_ coefficient) and by how much (subtract mn_ coefficient from the uncentered coefficient of th same variable: mn_age - age).

With regards to your question about rank, note that the way you represented your data in the first table rank varies within schools and thus is not a group level variable. Perhaps you made a typo? At any rate, lmer will appropriately model any variable that does not vary within groups as explaining the group-level variation in the outcome. As a test, run two models and compare the school variance estimates. You will see that in m2, below, only the variance of the School term decreases:

m1 <- lmer(y ~ age + (1|school), data=df)
m2 <- lmer(y ~ age + rank + (1|school), data=df) 
$\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.