I have a question about how to tell two different mixed effects models apart. In the simple case both involve fitting a model with a random group effect and a covariate. I fit the model with lme4
in R
. Here is a visualization of the two different scenarios.
library(ggplot2)
library(lme4)
gen_dat2 <- function(group.m,group.v,int, sl,n){
x <- vector()
y <- vector()
g <- vector()
for(i in 1:length(group.m)){
x.t <- rnorm(n,group.m[i],group.v[i])
y.t <- rnorm(n,group.m[i],group.v[i])+int + sl*x.t
x <- c(x,x.t)
y <- c(y,y.t)
g <- c(g,rep(i,n))
}
return(cbind(x,y,g))
}
group.m <- runif(5,1,20)
group.v <- runif(5,1,1.5)
dat2 <- data.frame(gen_dat2(group.m,group.v,1,4,14))
ggplot(dat2,aes(x=x,y=y,colour=as.factor(g),group=g))+geom_point()+stat_smooth(method="lm",se=F)
m2 <- lmer(y~x + (x|g),data=dat2)
Then I can generate and fit the other scenario with similar code:
gen_dat <- function(group.m,group.v,int, sl,n){
x <- vector()
y <- vector()
g <- vector()
for(i in 1:length(group.m)){
x.t <- rnorm(n,0,1)
y.t <- rnorm(n,group.m[i],group.v[i])+int + sl*x.t
x <- c(x,x.t)
y <- c(y,y.t)
g <- c(g,rep(i,n))
}
return(cbind(x,y,g))
}
group.m <- runif(5,1,20)
group.v <- runif(5,1,1.5)
dat1 <- data.frame(gen_dat(group.m,group.v,1,4,14))
ggplot(dat1,aes(x=x,y=y,colour=as.factor(g),group=g))+geom_point()+stat_smooth(method="lm",se=F)
m1 <- lmer(y~x + (x|g),data=dat1)
My central question is how do I tell these two models apart? Am I incorrectly fitting the first one, and I need an extra term in there to model the relationships between groups and the x variable as well as y? Both detect substantial between group variation in the intercept and not much in the slope as I would predict. But I need a way to tell these two apart. Any thoughts would be helpful.
Edits:
This has been helpful in me restating the question. So I want to re-ask the question with an example which I hope will make it clear why I want to be able to tell these two models apart. Let's imagine that Y is the average student test score at a school, and X is spending per student in that school. Our grouping variables are 5 different school districts.
Data in the top figure shows that an increase in spending within a district means that test scores increase. It also shows that between districts there are differences is scores, but that's clearly because some districts spend more student than others.
Data in the second figure show similarly that within a district student scores increase as spending increases. It also shows that between districts there are differences in test scores. However we don't know what is driving those differences, unlike in the first set of data. This is a pretty common situation I've encountered in building models. The former is not.
So what I'm asking is what is the appropriate model that captures the following features from the first dataset:
- Test scores increase as spending per student does
- There is also variance between districts in student test scores
- Part of that difference between districts is because of the underlying relationship between spending and test scores, which also varies with district.
More generally stated, how do you handle a scenario where you're building a hierarchical model where the grouping variable is correlated with one of your continuous independent variables (e.g. the first scenario). I feel like the model I've presented get's at points 1. and 2., but not point 3. So I'm really seeking a way to tease these two scenarios apart.
Normally I might add an extra level of hierarchy if there was another group level explanatory variable. Continuing our example, maybe in the 2nd dataset there are differences between district because in some districts parents have more time to spend on homework with students. So we would add that as a group level predictor in a hierarchical model. But that wouldn't work in the first scenario.
set.seed(0)
) one will not be able to replicate your results/graphs. (In general, code comments would be a plus too.) $\endgroup$