I am trying to calculate the random effect predictions from a linear mixed model by hand, and using notation provided by Wood in Generalized Additive Models: an introduction with R (pg 294 / pg 307 of pdf), I am getting confused over what each parameters represents.
Below is a summary from Wood.
Define a linear mixed model
$$ Y = X\beta + Zb + \epsilon $$
where b $\sim$ N(0, $\psi$), and $\epsilon \sim$ N(0, $\sigma^{2}$)
If b and y are random variables with joint normal distribution
\begin{align*} \begin{bmatrix} b\\ y \end{bmatrix} &\sim N \begin{bmatrix} \begin{bmatrix} 0\\ X\beta \end{bmatrix}\!\!,& \begin{bmatrix} \psi & \Sigma_{by} \\ \Sigma_{yb}& \Sigma_{\theta}\sigma^{2} \end{bmatrix} \end{bmatrix}\\ \end{align*}
The RE predictions are calculated by
\begin{align*} E[b \mid y] &= \Sigma_{by} \Sigma_{yy}^{-1} (y - x\beta)\\ &= \Sigma_{by}\Sigma_{\theta}^{-1}(y - x \beta) / \sigma^{2}\\ &= \psi z^{T}\Sigma_{\theta}^{-1} (y - x \beta) / \sigma^{2} \end{align*}
where $\Sigma_{\theta} = Z\psi Z^{T} /\sigma^{2} + I_{n}$
Using an random intercept model example from lme4
R package I get output
library(lme4)
m = lmer(angle ~ temp + (1 | replicate), data=cake)
summary(m)
% Linear mixed model fit by REML ['lmerMod']
% Formula: angle ~ temp + (1 | replicate)
% Data: cake
%
% REML criterion at convergence: 1671.7
%
% Scaled residuals:
% Min 1Q Median 3Q Max
% -2.83605 -0.56741 -0.02306 0.54519 2.95841
%
% Random effects:
% Groups Name Variance Std.Dev.
% replicate (Intercept) 39.19 6.260
% Residual 23.51 4.849
% Number of obs: 270, groups: replicate, 15
%
% Fixed effects:
% Estimate Std. Error t value
% (Intercept) 0.51587 3.82650 0.135
% temp 0.15803 0.01728 9.146
%
% Correlation of Fixed Effects:
% (Intr)
% temp -0.903
So from this, I think $\psi$ = 23.51, $(y-X\beta)$ can be estimated from cake$angle - predict(m, re.form=NA)
,
and sigma
from the square of the population level residuals.
th = 23.51
zt = getME(m, "Zt")
res = cake$angle - predict(m, re.form=NA)
sig = sum(res^2) / (length(res)-1)
Multiplying these together gives
th * zt %*% res / sig
[,1]
1 103.524878
2 94.532914
3 33.934892
4 8.131864
---
which is not correct when compared to
> ranef(m)
$replicate
(Intercept)
1 14.2365633
2 13.0000038
3 4.6666680
4 1.1182799
---
Why?