I am confused about the applicability of linear multi-levels models (also called hierarchical linear models or mixed-effect models) on data with multiple levels. At first, I thought they are made for each other but after trying to use a multi-level model on my own data I got very confused.
By multi-level data I mean that the data is about different levels in an hierarchy. For example, in the popularity data used by Joop Hox there is data about the pupil (popularity, sex) and the teacher (experience and popularity). In my case I want to asses the relation between the student-level and teacher-level predictors on the popularity of the student.
What I do know now about multi-level modeling is that they assess the effects of predictors on the lowest level possibly grouped by higher levels of the hierarchy (at least that is what I assume). For example, you can use multi-level models to check if schools have a different intercept or if the effect of sex is different for schools.
The problem I am trying to solve is how to use data higher up in the hierarchy to predict values lower in the hierarchy. In my real-case example I want to predict the house-prices using data about the districts they are in. Since the number of districts is much lower than the number of houses I want to incorporate this information in the estimation of the variance of the coefficients.
Below is a parallel example with pupil-teacher data used by Joop Hox. In this example, I want to asses the effect of sex on the pupil level and the effect of teacher experience on the teacher level. The model should take into account that there are much less observations (100) of teacher experience than of student sex (2000). Note that there is only one teacher per school; for clarity I rename school to teacher.
Following Joop Hox's notation that would be the following in math:
$$\text{popularity}_{ij} = \beta_{0j} + \beta_{1j} \text{gender}_{ij} + e_{ij}$$
$$\beta_{0j} = \gamma_{00} + \gamma_{01} \text{experience}_{j} + u_{0j}$$
Ordinary linear model
library(foreign)
library(lme4)
library(arm)
library(tidyverse)
library(broom)
df <- read.dta("http://www.ats.ucla.edu/stat/stata/examples/mlm_ma_hox/popular.dta") %>%
rename(teacher = school)
fit_lm <- lm(popular ~ sex + texp, df)
display(fit_lm)
This would underestimate the standard error of the effect of the teacher experience since it assumes there are 2000 observations of teacher experience instead of only 100 (number of teachers).
Stacked linear models
fit_lm_1 <- lm(popular ~ sex + factor(teacher) + 0, df)
teacher_effect <- tidy(fit_lm_1)[3:101,] %>%
mutate(teacher = as.numeric(substr(term, 15,18))) %>%
dplyr::select(teacher, estimate) %>%
bind_rows(data.frame(teacher = 1, estimate = 0))
df_teacher <- df %>%
group_by(teacher) %>%
summarise(texp = first(texp)) %>%
left_join(teacher_effect)
fit_lm_2 <- lm(estimate ~ texp, df_teacher)
display(fit_lm_2)
Takes into account that there is less data about teachers than about pupils. However, does not allow for dependencies between teacher-experience and gender (e.g. think of a private boys/girls school).
Multi-level model with teacher-experience per school
fit_lmer1 <- lmer(popular ~ sex + (texp + 0 | teacher), df)
display(fit_lmer1)
ranef(fit_lmer1)
This would give a separate slope for teacher experience for each teacher. Since there is only a single teacher per teacher (duh!) this is a strange thing to do.
Multi-level model with fixed teacher-experience and intercept per school
fit_lmer2 <- lmer(popular ~ sex + texp + (1 | teacher), df)
display(fit_lmer2)
fixef(fit_lmer2)
This results in a single estimate for teacher experience. According to this blog post lmer
understands that texp
and teacher
are related and that there are only 100 observations of teacher experience.
Fine, you would say? Not totally, I do not want to model the teacher intercept but only the teacher experience, because what could the teacher experience possibly add to an intercept per teacher. Furthermore, I doubt that lmer
really understands that texp
and school
belong together.
In short
I have data on two separate levels and I want to draw valid conclusions about there relationships with a dependent variable. Can I use multi-level modeling for this? How to do this in R with lme4
(or possibly nlme
)