# How to center level-2 variables in unbalanced two-level models?

Let's say I have a sample of 200 students and they are clustered in 10 different classrooms. However, the sample is unbalanced such that some schools have more than 20 students and others have fewer than 20. I am trying to explain a level-1 (i.e., student) variable student_outcome using a level-2 (i.e., classroom) predictor teacher_quality, and I would like to center teacher_quality. Should I subtract the mean of teacher_quality across all 200 students or should I subtract the mean of teacher_quality across all 10 classrooms? Are both of these approaches called "grand mean centering" or do they have different names?

Based on the following simulation, it appears that the choice between these two centering techniques only influences the fixed intercept and the correlation of the fixed effects. It does not influence the random effects, the fixed slope, or the residuals. I'm still not sure how this influences the interpretation of the intercept and fixed effect correlation and which would be preferred (if either).

set.seed(123)
library(lme4)

# Set parameters
n_classrooms <- 10
n_studentsper <- 30
n_students <- n_classrooms * n_studentsper
n_sample <- 200

# Simulate variables
teacher_quality <- rnorm(n_classrooms)
student_outcome <- rnorm(n_students, mean = rep(teacher_quality, each = n_studentsper))

# Simulate population
my_population <-
data.frame(
student = 1:n_students,
classroom = rep(1:n_classrooms, each = n_studentsper),
student_outcome = student_outcome,
teacher_quality = rep(teacher_quality, each = n_studentsper)
)

# Simulate an unbalanced sample
index <- sample(1:n_students, n_sample, replace = FALSE)
my_sample <- my_population[index, ]

# Look at sample
#>     student classroom student_outcome teacher_quality
#> 130     130         5   -1.3146054258      0.12928774
#> 120     120         4   -0.0007996947      0.07050839
#> 200     200         7    0.4068880809      0.46091621
#> 21       21         1   -0.1340114251     -0.56047565
#> 199     199         7    2.1118236733      0.46091621
#> 87       87         3    3.7460413072      1.55870831

# Count students per classroom
margin.table(with(my_sample, table(student, classroom)), 2)
#> classroom
#>  1  2  3  4  5  6  7  8  9 10
#> 21 16 22 18 21 20 25 20 17 20

# Calculate mean of teacher_quality across students
(m1 <- mean(my_sample\$teacher_quality))
#>  0.113781

# Calculate mean of teacher_quality across classrooms
(m2 <- mean(teacher_quality))
#>  0.07462564

# Apply both types of centering
centering <-
data.frame(
teacher_quality_c1 = my_sample$$teacher_quality - m1, teacher_quality_c2 = my_sample$$teacher_quality - m2
)
my_sample_c <- cbind(my_sample, centering)

# Estimate model without centering
fit0 <- lme4::lmer(
student_outcome ~ 1 + teacher_quality + (1 | classroom),
data = my_sample
)
summary(fit0)
#> Linear mixed model fit by REML ['lmerMod']
#> Formula: student_outcome ~ 1 + teacher_quality + (1 | classroom)
#>    Data: my_sample
#>
#> REML criterion at convergence: 535.6
#>
#> Scaled residuals:
#>     Min      1Q  Median      3Q     Max
#> -2.1494 -0.7218 -0.0360  0.6304  2.4438
#>
#> Random effects:
#>  Groups    Name        Variance Std.Dev.
#>  classroom (Intercept) 0.003084 0.05553
#>  Residual              0.828378 0.91015
#> Number of obs: 200, groups:  classroom, 10
#>
#> Fixed effects:
#>                 Estimate Std. Error t value
#> (Intercept)     -0.01918    0.06723  -0.285
#> teacher_quality  0.99788    0.07309  13.653
#>
#> Correlation of Fixed Effects:
#>             (Intr)
#> teachr_qlty -0.121

# Estimate model with m1 centering
fit1 <- lme4::lmer(
student_outcome ~ 1 + teacher_quality_c1 + (1 | classroom),
data = my_sample_c
)
summary(fit1)
#> Linear mixed model fit by REML ['lmerMod']
#> Formula: student_outcome ~ 1 + teacher_quality_c1 + (1 | classroom)
#>    Data: my_sample_c
#>
#> REML criterion at convergence: 535.6
#>
#> Scaled residuals:
#>     Min      1Q  Median      3Q     Max
#> -2.1494 -0.7218 -0.0360  0.6304  2.4438
#>
#> Random effects:
#>  Groups    Name        Variance Std.Dev.
#>  classroom (Intercept) 0.003084 0.05553
#>  Residual              0.828378 0.91015
#> Number of obs: 200, groups:  classroom, 10
#>
#> Fixed effects:
#>                    Estimate Std. Error t value
#> (Intercept)         0.09436    0.06674   1.414
#> teacher_quality_c1  0.99788    0.07309  13.653
#>
#> Correlation of Fixed Effects:
#>             (Intr)
#> tchr_qlty_1 0.003

# Estimate model with m2 centering
fit2 <- lme4::lmer(
student_outcome ~ 1 + teacher_quality_c2 + (1 | classroom),
data = my_sample_c
)
summary(fit2)
#> Linear mixed model fit by REML ['lmerMod']
#> Formula: student_outcome ~ 1 + teacher_quality_c2 + (1 | classroom)
#>    Data: my_sample_c
#>
#> REML criterion at convergence: 535.6
#>
#> Scaled residuals:
#>     Min      1Q  Median      3Q     Max
#> -2.1494 -0.7218 -0.0360  0.6304  2.4438
#>
#> Random effects:
#>  Groups    Name        Variance Std.Dev.
#>  classroom (Intercept) 0.003084 0.05553
#>  Residual              0.828378 0.91015
#> Number of obs: 200, groups:  classroom, 10
#>
#> Fixed effects:
#>                    Estimate Std. Error t value
#> (Intercept)         0.05529    0.06680   0.828
#> teacher_quality_c2  0.99788    0.07309  13.653
#>
#> Correlation of Fixed Effects:
#>             (Intr)
#> tchr_qlty_2 -0.040
$$$$

• Interesting, Jeffrey. Is your teacher_quality variable unique to students within classrooms or is it the same for all students? In other words, is it something like a student's rating of their teacher's quality? Mar 12 '20 at 15:59
• It is the same for all students. This is meant to be an example of a classroom-level (i.e., level 2) variable. Mar 12 '20 at 17:06
• Then I would calculate the mean based on teacher_quality across classrooms (m2`). To me this is a conceptual question. The teacher level is the level at which it was measured and thus the level at which it makes sense to think about a mean. Mar 12 '20 at 19:20
• About the question within the answer, the interpretation of the intercept is always the predicted value when all variables are zero. Thus, when you dont center, the intercept is the predicted student score when teacher quality is zero. When you center across students, the intercept is the predicted student score when teacher quality is 0.114. When you center across classrooms, the intercept is the predicted student score when teacher quality is 0.075. Apr 12 at 3:13
• I dont know much about the fixed effect correlation, but I guess, in this case, as you only have one predictor, it is the correlation between this predictor and the intercept. As the intercept changes between the models, so it does their corretion with the predictor. Apr 12 at 3:15