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


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.

I receive the following output from summary(mask_model)


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).

  • $\begingroup$ Please include the output from summary(test_model) ? Also what happens if you use getME(test_model, "b") ? $\endgroup$ Feb 6, 2021 at 11:32
  • $\begingroup$ 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? $\endgroup$ Feb 9, 2021 at 19:19
  • $\begingroup$ 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. $\endgroup$ Feb 9, 2021 at 19:32
  • $\begingroup$ 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! $\endgroup$ Feb 9, 2021 at 19:40

1 Answer 1


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


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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.