Suppose I have a dataset with repeated measurements on q clusters. I want to fit an LMM with two random intercepts, on the same cluster, with a non-diagonal covariance structure on the random effects (if it were diagonal I assume the model would be un-identifiable).
That is:
$$y_{ij} = x_{ij}^\top\beta + b_{1j} + b_{2j} + \varepsilon_{ij},$$
where $y_{ij}$ is the $i$-th observation in the $j$-th cluster ($j = 1, ..., q; ~i = 1, ... n_j$). Again if $b_{1j}$ and $b_{2j}$ are independent I assume the model is un-identifiable but I want them to be correlated. Let $b$ be the vector of random effects of length $2q,$ then $b$ distributes Gaussian with mean $0 $ and a $2q \times 2q$ covariance:
q <- 3
rho <- 0.5
sig2b1 <- 1
sig2b2 <- 1
D <- matrix(c(sig2b1 ,rho, rho, sig2b2),nrow = 2) %x% diag(q); D
[1,] 1.0 0.0 0.0 0.5 0.0 0.0
[2,] 0.0 1.0 0.0 0.0 0.5 0.0
[3,] 0.0 0.0 1.0 0.0 0.0 0.5
[4,] 0.5 0.0 0.0 1.0 0.0 0.0
[5,] 0.0 0.5 0.0 0.0 1.0 0.0
[6,] 0.0 0.0 0.5 0.0 0.0 1.0
(Here $q = 3$, on the diagonal $\sigma^2_{b_1} = \sigma^2_{b_2} = 1 $ and we have three variance components to estimate not including the residuals variance $\sigma^2_e$)
Now the model is a standard LMM in vectorized form:
$$y = X\beta + Zb + \varepsilon,$$
Let's get some data:
library(mvtnorm)
b <- t(rmvnorm(n = 1, mean = rep(0, 2 * q), sigma = D))
# can do: plot(b[1:q], b[(q+1):(2*q)]) to see the correlation
z <- rep(letters[1:q], times = c(3,3,2) * 10)
n <- length(z)
Z <- model.matrix(~0 + z)
Z <- cbind(Z, Z) # Notice Z is concatenated to itself, Z is of order n X 2q
x <- rnorm(n)
y <- 1 + 2 * x + Z %*% b + rnorm(n)
Using lme4
with two random intercepts, is surprisingly (sometimes) not bad:
library(lme4)
mod <- lmer(y ~ x + (1|z) + (1|z))
# can do: plot(b[1:q], ranef(mod)$z[,1])
# and: plot(b[(q + 1):(2 * q)], ranef(mod)$z[,2])
# and: VarCorr(mod)
But I want to fit the model with the D
covariance structure, where there is some correlation between the random intercepts of each cluster $j.$