# Extracting random effects from glmer()

I currently am fitting a mixed model on a data set of n=20,000 and my model takes the following form

mask_model=glmer(y~x_1+x_2+(1+x_3||x_4)+(1|x_5),data=fake_data,family="binomial)

So we have a random intercept and slope for each x_4 group, and a random intercept for each x_5 group.

x_3 takes one of 18 values, x_4 takes one of three values, and x_5 takes one of 20,000 values.

I am able to fit mask_model in a few minutes. However when I attempt to extract the random effects from this model via ranef(mask_model) my computer just hangs. This strikes me as strange as I would think when I make the original glmer() it would calculate these random effects. If I exclude the random intercept involving x_5, or if I make x_5 only take one of 5000 values then ranef() seems to work.

Would greatly appreciate any insight into why this might be happening and a potential workaround. When I run getME(mask_model, "b") I get a 8625x1 vector, (I have 8619 id's and 3 provinces). It appears to be that this is the conditional mode of the random effects, but ideally I could somehow extract what the id random effect is for each participant, and the random slope/intercept for each province (if that is possible; unfortunately ranef(mask_model) just hangs in R).

• Please include the output from summary(test_model) ? Also what happens if you use getME(test_model, "b") ? Feb 6, 2021 at 11:32
• Have added this in a comment above. I have described what getME() returns above; is the correct way to interpret this vector by using getME(mask_model,'Zt') to find out which entry of the large vector corresponds to which random effect? Feb 9, 2021 at 19:19
• The output doesn't correspond to the model. Anyway Zt is the transpose of the model matrix for the random effects so Z determines how the random effects are mapped to the response. But the question in the OP was how to extract the (conditional modes of the) random effects themselves and that's what getME(test_model, "b") should do. Feb 9, 2021 at 19:32
• OK I understand now, I see that "b" gives the conditional modes and then rownames(Z_t) give which conditional mode corresponds to which grouping (arranged id first, province slope second, province intercept third). Thank you very much for your help! Feb 9, 2021 at 19:40

You can extract the conditional modes of the random effects using

getME(test_model, "b")


You can extract the model matrix $$Z$$ for the random effects using

getME(mask_model,'Zt')


and transpose it. This matrix determines how the random effects are "mapped" to the response in the mixed model equation:

$$Y = X\beta + Zb + \epsilon$$

You may find this answer to be helpful in understanding how the $$Z$$ model matrix works:
What are the steps to simulate data for a linear model with random slopes and random intercepts

• Great answer and very clear, thank you very much for the help. Feb 10, 2021 at 19:26