Here is how I simulate random effects. I'll demonstrate for linear regression, but extending it to a different GLM should be straight forward.
Let's start with a random intercept model. The model is usually written as
$$ y = XB + Z\gamma $$
Where $Z$ is an indicator for the group and $\gamma_i$ is normally distributed with mean 0 and some variance. Simulation of this model is as follows...
groups <- 1:5
N <- 250
g <- factor(sample(groups, replace = TRUE, size = N), levels = groups)
x <- rnorm(N)
X <- model.matrix(~ x)
Z <- model.matrix(~ g - 1)
beta <- c(10, 2)
gamma <- rnorm(length(groups), 0, 0.25)
y = X %*% beta + Z%*% gamma + rnorm(N, 0, 0.3)
Let's fit a mixed model and see if we recover some of these estimates
library(lme4)
model = lmer(y ~ x + (1|g), data = d)
summary(model)
linear mixed model fit by REML ['lmerMod']
Formula: y ~ x + (1 | g)
Data: d
REML criterion at convergence: 136.2
Scaled residuals:
Min 1Q Median 3Q Max
-2.85114 -0.65429 -0.00888 0.65268 2.63459
Random effects:
Groups Name Variance Std.Dev.
g (Intercept) 0.05771 0.2402
Residual 0.09173 0.3029
Number of obs: 250, groups: g, 5
Fixed effects:
Estimate Std. Error t value
(Intercept) 9.95696 0.10914 91.23
x 2.00198 0.01993 100.45
Correlation of Fixed Effects:
(Intr)
x -0.008
Fixed effects look good, and the group standard deviation (0.25) is estimated pretty accurately, and so is the residual standard deviation.
Random slope models are similar. Under the assumption each slope comes from a normal distribution, then we can write the slope as
$$ y = Bx + \beta_i x$$
Here $B$ is the population mean and $\beta_i$ is the effect of group i. Here is a simulation
library(tidyverse)
groups <- 1:5
N <- 250
g <- sample(groups, replace = TRUE, size = N)
x <- rnorm(N)
X <- model.matrix(~ x)
B <- c(10, 2)
beta <- rnorm(length(groups), 0, 2)
y = X %*% B + x*beta[g] + rnorm(N, 0, 0.3)
and a model ...
library(lme4)
d = tibble(y, x, g)
model = lmer(y ~ x + (x|g), data = d)
summary(model)
Linear mixed model fit by REML ['lmerMod']
Formula: y ~ x + (x | g)
Data: d
REML criterion at convergence: 158.9
Scaled residuals:
Min 1Q Median 3Q Max
-2.95141 -0.65904 0.02218 0.61932 2.66614
Random effects:
Groups Name Variance Std.Dev. Corr
g (Intercept) 2.021e-05 0.004496
x 3.416e+00 1.848314 1.00
Residual 9.416e-02 0.306856
Number of obs: 250, groups: g, 5
Fixed effects:
Estimate Std. Error t value
(Intercept) 10.00883 0.01984 504.47
x 2.05913 0.82682 2.49
Correlation of Fixed Effects:
(Intr)
x 0.099
Here are the coefficients of the 5 groups
coef(model)
$g
(Intercept) x
1 10.00135 -1.015180
2 10.01335 3.919787
3 10.00934 2.270760
4 10.01081 2.873636
5 10.00928 2.246626
and compare them to the true values
B[2] + beta
-0.9406479 3.9195119 2.2976457 2.8536623 2.3539863