Just to illustrate my situation, imagine this is my data:
y = rbinom(25, 1, 0.5)
names1 = rep(1:5, each = 5)
names2 = rep(1:5 , 5)
x= rnorm(25)
df = data.frame(cbind(names1, names2, y, x))
That is, it looks like the following:
| names1 | names2 | y | x |
|---|---|---|---|
| 1 | 2 | 0 | -1.10152733 |
| 1 | 3 | 1 | 1.49929490 |
| 1 | 4 | 0 | 0.96740978 |
| 1 | 5 | 1 | -0.52383757 |
| 2 | 1 | 0 | -1.19182503 |
| 2 | 3 | 1 | 1.00492382 |
| 2 | 4 | 1 | 0.39198228 |
| 2 | 5 | 1 | -0.68029102 |
I want to estimate y here using glmer, ie:
glmer(y ~ x + (1|Random), family = binomial, data=df)
For my problem, Random effects structure should be based on names, independent of them appearing on names1 or names 2 column. That is, in the general formulation of the model $y=X\beta + Zb +\u$, the matrix Z here is n*p, where in this exercise n=20 (nrow(df)), and p=5 (length(unique(df$names1)). $Z_{ij}=1$ if the name $j$ appear in row $i$, that is names1=j OR names2=j
My question is how can I use this matrix Z as my random effects grouping:
glmer(y ~ x + (1|Z), family = binomial, data=df)
So far, I have generated Z as:
for (i in 1:length(unique(df$names1))) {
df[paste0("Z", i)] <- ifelse(df$names1 == i | df$names2 == i, 1, 0)
}
df = df[df$names1 != df$names2,]
But i am at lost in how to incorporate Z's in glmer. Any help would be appreciated.
The name for this setup is a multi-membership model.
This can be hacked in
lme4following the recipe here, which I illustrate below. Alternatively, there is an lmerMultiMember package on Github which is supposed to fit these models (but I couldn't get it to work, so I show how to do it the hacky way).set up data
This throws an error for reasons I don't understand. I'll use
M(which is the transpose of theZmatrix) below instead:You can also fit multi-membership models in the
MCMCglmmpackage, if you want to go Bayesian ...