\[ \begin{split} \underbrace{Y_i}_{\text{response}} & \sim \overbrace{\text{Distr}}^{\substack{\text{conditional} \\ \text{distribution}}}(\underbrace{g^{-1}(\eta_i)}_{\substack{\text{inverse} \\ \text{link} \\ \text{function}}},\underbrace{\phi}_{\substack{\text{scale} \\ \text{parameter}}}) \\ \underbrace{\boldsymbol \eta}_{\substack{\text{linear} \\ \text{predictor}}} & = \underbrace{\boldsymbol X\boldsymbol \beta}_{\substack{\text{fixed} \\ \text{effects}}} + \underbrace{\boldsymbol Z\boldsymbol b}_{\substack{\text{random} \\ \text{effects}}} \\ \underbrace{\boldsymbol b}_{\substack{\text{conditional} \\ \text{modes}}} & \sim \text{MVN}(\boldsymbol 0, \underbrace{\Sigma(\boldsymbol \theta)}_{\substack{\text{variance-} \\ \text{covariance} \\ \text{matrix}}}) \end{split} \]
model.matrix()
, terms()
21
); -1
or
+0
to suppress+0
):
= pairwise products of predictor
variablespoly()
,
splines::ns()
, etc.library(Matrix)
library(reformulas)
terms(~ mpg * disp, data = mtcars)
model.matrix(hp~ mpg * disp, data = mtcars)
( f | g )
(f
= varying
term, g
= cluster/grouping variable)g
can be expanded into separate terms
((f | g1/g2)
→ (f | g1 + g1:g2)
);
*
, +
may be expanded
(reformulas
package)f
= any set of terms that vary within levels
of g
nlme::lme
, MCMCglmm
)lme4
,
glmmTMB
)brms
,
MCMCglmm
, glmmTMB
) as (e.g.)
cs( f | g )
MCMCglmm
):
e.g. ~us(f):g
g
) with model matrix for
f
f <- factor(rep(1:3, each = 2))
Ji <- t(as(f, "sparseMatrix"))
Xi <- cbind(1, rep.int(c(-1, 1), 3L))
Zi <- t(Matrix::KhatriRao(t(Ji), t(Xi)))
lme4
exampledd <- expand.grid(f1 = factor(1:3), f2 = factor(1:2), g1 = factor(1:5), g2 = factor(1:6))
dd$y <- 1
form <- y ~ 1 + (f1 | g1) + (f2 | g2)
rt <- reformulas::mkReTrms(
bar = findbars(form),
fr = model.frame(subbars(form), data = dd))
Sigma <- rt$Lambdat %*% t(rt$Lambdat)
image(Sigma)