Introduction
The current version of lme4 offers four covariance
classes/structures: Covariance.us (unstructured),
Covariance.diag (diagonal), Covariance.cs
(compound symmetry), and Covariance.ar1 (autoregressive
order 1). The syntax for use is similar to the glmmTMB
package (see covariance
structures in glmmTMB), although the results are slightly
different.
This vignette provides a detailed mathematical exposition of the new
machinery, along with brief notes on the appropriate syntax. In a
separate document, we provide comparisons of results between
glmmTMB and lme4 (the computations are too
slow to satisfy CRAN timing requirements; if we tried to store and
re-load saved fits, they would be too big to satisfy CRAN package size
requirements).
Background
For exact details of this structure, refer to the lmer
vignette
(Bates et al. 2015). We provide a quick
summary in this vignette.
In matrix notation, a linear mixed model can be represented as: Where represents an unknown vector of random effects, with .
We create the relative co-factor
which is a
block diagonal matrix that depends on the variance-component parameter
vector
.
Let
be the scale parameter of the variance of a linear mixed model. In
lme4, the variance-covariance matrix is constructed by:
For generalized linear mixed models, instead represents the unscaled Cholesky factor; that is, the scaling term is omitted from the equation above.
The major difference between the four covariance classes
(Covariance.us (unstructured), Covariance.diag
(diagonal), Covariance.cs (compound symmetry), and
Covariance.ar1 (autoregressive order 1)) is in the
construction of the the relative Cholesky factor
.
Covariance Structures
Suppose that a particular random-effects term has
varying effects (i.e., the number of columns of the model matrix
constructed from the left-hand side f of the random-effects
formula (f|g). If the grouping variable (g)
has
levels and
.
In general the covariance matrix for the term,
,
is a
block diagonal matrix composed of homogeneous
blocks; the (relative) Cholesky factor
has a similar structure. In what follows we will describe the
construction of these template matrices, for either the
covariance matrix or the Cholesky factor.
The unstructured covariance, which is the default in
lme4, of size
has the following form:
To specify unstructured (general positive-semidefinite) covariances
for a particular term, specify either (f|g) (as in
lme4 < 2.0) or us(f|g).
The next three covariance structures can either be heterogeneous or
homogeneous. If we have a homogeneous covariance structure
(hom = TRUE), then we assume
.
The diagonal covariance has the following form: By default, we assume a heterogeneous diagonal covariance structure.
To specify a diagonal covariance structure, use
diag(f|g) (or diag(f|g, hom = FALSE)). Unlike
the unstructured covariance matrix, which is invariant to the
parameterization of the terms in the varying effects model
f, the diagonal covariance matrix (and the others described
below) depend on the contrasts assigned to factor variables and on the
inclusion of an intercept term. For example, suppose that f
represents a factor variable with
levels, and that the standard treatment contrasts are used. The template
matrices will be
.
- If the intercept is not included (i.e. the term is specified as
(0+f|g)or(f-1|g)), then the variances will represent variances of observations in each level of the factor around the population mean; - if the intercept is included (the term is specified as
(f|g)or(1+f|g)), will represent the among-cluster variance of observations from the first (baseline) level off; $^2_2, will represent variances of differences in expected values between observations from levels 2 to and those in the baseline level.
If a different, non-default contrast is used, then the variances
describe variation in the corresponding parameters. For example, with
successive-difference contrasts (MASS::contr.sdif())
would estimate among-cluster variance for observations at the first
level,
would estimate variance in the difference between level 2 and level 1,
and so forth.
Similar points apply to the compound symmetric and autoregressive covariance structures below.
The compound symmetric covariance has the following form: By default, we assume a heterogeneous compound symmetric covariance structure.
Use cs(f|g) or cs(f|g, hom = TRUE) to
specify a compound symmetric structure.
The AR1 (auto-regressive order 1) covariance has the following form: Unlike the diagonal and compound symmetric structures, by default we assume a homogeneous ar1 covariance structure.
Use ar1(f|g) or ar1(f|g, het = TRUE) to
specify an AR1 structure.
Construction of the Relative Co-factor
For the unstructured covariance matrix, lme4 estimates
the following parameters in par:
to construct the relative co-factor
(this procedure is the same as in pre-2.0 versions of
lme4):
The definition of the parameter vectors differs for the other
covariance structures. In the diagonal covariance matrix case,
(or par) only contains the standard deviations. The
relative co-factor
is:
For the compound symmetry covariance structure, the parameter vector
par contains the
standard deviations
and the common correlation
.
In contrast to glmmTMB, the correlation is estimated on its
original scale (bounded between -1 and 1), rather than on an
unconstrained, transformed scale.
The relative co-factor is a lower triangular matrix. Consider the form:
Its elements are constructed as follows. First, define the sequence recursively:
Then the elements of
are given by:
Users can extract the values
via the getTheta() function of the
Covariance.cs object, in which theta will be a
vector in the column-wise elements of
.
The setup is similar for the autoregressive order 1 (AR1) covariance structure. Again, the parameter vector contains the standard deviations and the autocorrelation parameter . The relative co-factor is a lower triangular matrix whose form is similar to the compound symmetric case.
The elements
are given by:
Again, these values can be extracted
using getTheta() function of the
Covariance.ar1 object, in which theta will be
still be a vector in the column-wise elements of
.
Extracting model components
This section illustrates how to extract par,
theta, Lambda, as described in the previous
section, as well as the variance covariance matrices of a model, from a
merMod object.
We’ll fit the standard sleepstudy example, except that
we will use a model with a compound symmetric covariance structure.
Because this model has only two varying effects (intercept and slope
with respect to day) per subject, and hence the covariance matrix is
,
there is no difference in overall model fit between the
compound-symmetric and the unstructured covariance matrices. However,
the models are parameterized differently, so this example will highlight
the differences between par and theta.
library(lme4)
#> Loading required package: Matrix
fm1.cs <- lmer(Reaction ~ Days + cs(1 + Days | Subject), sleepstudy)Extracting the covariance structure:
print(fm1.cs_cov <- getReCovs(fm1.cs))
#> [[1]]
#> An object of class "Covariance.cs"
#> Slot "hom":
#> [1] FALSE
#>
#> Slot "nc":
#> [1] 2
#>
#> Slot "par":
#> [1] 0.96679232 0.23140420 0.06561148The result is a list with only one element as we only have one
random-effects term (cs(1 + Days | Subject)). To see the
values of par and theta for this object, we
can call:
getME(fm1.cs, "par")
#> Subject.(Intercept) Subject.Days Subject.rho
#> 0.96679232 0.23140420 0.06561148
getME(fm1.cs, "theta")
#> Subject.(Intercept) Subject.Days.(Intercept) Subject.Days
#> 0.96679232 0.01518277 0.23090558The matrix is large, so we’ll view it instead of printing:

To most users, the most crucial information is simply the
variance-covariance matrices. Extract these via
VarCorr.merMod() (the list has one element per
random-effect term in the model — in this case, only one):
vc_mat <- VarCorr(fm1.cs)
vc_mat$Subject
#> (Intercept) Days
#> (Intercept) 612.160273 9.613533
#> Days 9.613533 35.070443
#> attr(,"class")
#> [1] "vcmat_cs" "matrix" "array"
#> attr(,"stddev")
#> (Intercept) Days
#> 24.74187 5.92203
#> attr(,"correlation")
#> (Intercept) Days
#> (Intercept) 1.00000000 0.06561148
#> Days 0.06561148 1.00000000For detailed comparisons with glmmTMB, see
browseURL(system.file("extra_docs", "covariance_structure_comparison.html", package = "lme4")).