This vignette demonstrates some of the covariance structures
available in the glmmTMB
package. Currently the available
covariance structures are:
Covariance | Notation | no. parameters | Requirement | Parameters |
---|---|---|---|---|
Unstructured (general positive definite) | us |
\(n(n+1)/2\) | See Mappings | |
Heterogeneous Toeplitz | toep |
\(2n-1\) | log-SDs (\(\theta_1-\theta_n\)); correlations \(\rho_k = \theta_{n+k}/\sqrt{1+\theta_{n+k}^2}\), \(k = \textrm{abs}(i-j+1)\) | |
Het. compound symmetry | cs |
\(n+1\) | log-SDs (\(\theta_1-\theta_n\)); correlation \(\rho = \theta_{n+1}/\sqrt{1+\theta_{n+1}^2}\) | |
Het. diagonal | diag |
\(n\) | log-SDs | |
AR(1) | ar1 |
\(2\) | Unit spaced levels | log-SD; \(\rho = \left(\theta_2/\sqrt{1+\theta_2^2}\right)^{d_{ij}}\) |
Ornstein-Uhlenbeck | ou |
\(2\) | Coordinates | log-SD; log-OU rate (\(\rho = \exp(-\exp(\theta_2) d_{ij})\)) |
Spatial exponential | exp |
\(2\) | Coordinates | log-SD; log-scale (\(\rho = \exp(-\exp(-\theta_2) d_{ij})\)) |
Spatial Gaussian | gau |
\(2\) | Coordinates | log-SD; log-scale (\(\rho = \exp(-\exp(-2\theta_2) d_{ij}^2\)) |
Spatial Matèrn | mat |
\(3\) | Coordinates | log-SD, log-range, log-shape (power) |
Reduced rank | rr |
\(nd-d(d-1)/2\) | rank (d) |
The word ‘heterogeneous’ refers to the marginal variances of the model.
Some of the structures require temporal or spatial coordinates. We will show examples of this in a later section.
First, let’s consider a simple time series model. Assume that our measurements \(Y(t)\) are given at discrete times \(t \in \{1,...,n\}\) by
\[Y(t) = \mu + X(t) + \varepsilon(t)\]
where
A simulation experiment is set up using the parameters
Description | Parameter | Value |
---|---|---|
Mean | \(\mu\) | 0 |
Process variance | \(\sigma^2\) | 1 |
Measurement variance | \(\sigma_0^2\) | 1 |
One-step correlation | \(\phi\) | 0.7 |
The following R-code draws a simulation based on these parameter values. For illustration purposes we consider a very short time series.
<- 25 ## Number of time points
n <- MASS::mvrnorm(mu = rep(0,n),
x Sigma = .7 ^ as.matrix(dist(1:n)) ) ## Simulate the process using the MASS package
<- x + rnorm(n) ## Add measurement noise y
In order to fit the model with glmmTMB
we must first
specify a time variable as a factor. The factor levels
correspond to unit spaced time points. It is a common mistake to forget
some factor levels due to missing data or to order the levels
incorrectly. We therefore recommend to construct factors with explicit
levels, using the levels
argument to the
factor
function:
<- factor(1:n, levels=1:n)
times head(levels(times))
## [1] "1" "2" "3" "4" "5" "6"
We also need a grouping variable. In the current case there is only one time-series so the grouping is:
<- factor(rep(1,n)) group
We combine the data into a single data frame (not absolutely required, but good practice):
<- data.frame(y, times, group) dat0
Now fit the model using
glmmTMB(y ~ ar1(times + 0 | group), data=dat0)
This formula notation follows that of the lme4
package.
times + 0
corresponds to
a design matrix \(Z\) linking
observation vector \(y\) (rows) with a
random effects vector \(u\) (columns)
(see Construction of
structured covariance matrices for why we need the
+ 0
)ar1
(this is the only glmmTMB
specific part of
the formula).After running the model, we find the parameter estimates \(\mu\) (intercept), \(\sigma_0^2\) (dispersion), \(\sigma\) (Std. Dev.) and \(\phi\) (First off-diagonal of “Corr”) in the output:
## Formula: y ~ ar1(times + 0 | group)
## Data: dat0
## AIC BIC logLik df.resid
## 77.41777 82.29328 -34.70889 21
## Random-effects (co)variances:
##
## Conditional model:
## Groups Name Std.Dev. Corr
## group times1 1.093e+00 0.47 (ar1)
## Residual 5.898e-05
##
## Number of obs: 25 / Conditional model: group, 1
##
## Dispersion estimate for gaussian family (sigma^2): 3.48e-09
##
## Fixed Effects:
##
## Conditional model:
## (Intercept)
## -0.09745
For those trying to make sense of the internal parameterization, the
internal transformation from the parameter (\(\phi\)) to the AR1 coefficient is \(\phi = \theta_2/\sqrt(1+\theta_2^2)\); the
inverse transformation is \(\theta_2 =
\phi/\sqrt(1-\phi^2)\). (The first element of the
theta
vector is the log-standard-deviation.)
A single time series of 6 time points is not sufficient to identify the parameters. We could either increase the length of the time series or increase the number of groups. We’ll try the latter:
<- function(g, n=6, phi=0.7) {
simGroup <- MASS::mvrnorm(mu = rep(0,n),
x Sigma = phi ^ as.matrix(dist(1:n)) ) ## Simulate the process
<- x + rnorm(n) ## Add measurement noise
y <- factor(1:n)
times <- factor(rep(g,n))
group data.frame(y, times, group)
}simGroup(1)
## y times group
## 1 -1.05644751 1 1
## 2 -1.44135897 2 1
## 3 -0.02632749 3 1
## 4 -0.07130971 4 1
## 5 1.44678696 5 1
## 6 0.85709093 6 1
Generate a dataset with 1000 groups:
<- do.call("rbind", lapply(1:1000, simGroup) ) dat1
And fitting the model on this larger dataset gives estimates close to the true values (AR standard deviation=1, residual (measurement) standard deviation=1, autocorrelation=0.7):
<- glmmTMB(y ~ ar1(times + 0 | group), data=dat1)) (fit.ar1
## Formula: y ~ ar1(times + 0 | group)
## Data: dat1
## AIC BIC logLik df.resid
## 20748.59 20775.39 -10370.30 5996
## Random-effects (co)variances:
##
## Conditional model:
## Groups Name Std.Dev. Corr
## group times1 1.0695 0.70 (ar1)
## Residual 0.9951
##
## Number of obs: 6000 / Conditional model: group, 1000
##
## Dispersion estimate for gaussian family (sigma^2): 0.99
##
## Fixed Effects:
##
## Conditional model:
## (Intercept)
## 0.02553
We can try to fit an unstructured covariance to the previous dataset
dat
. For this case an unstructured covariance has 300
correlation parameters and 25 variance parameters. Adding \(\sigma_0^2 I\) on top would cause a strict
overparameterization, as these would be redundant with the diagonal
elements in the covariance matrix. Hence, when fitting the model with
glmmTMB
, we have to disable the \(\varepsilon\) term (the dispersion) by
setting dispformula=~0
:
<- glmmTMB(y ~ us(times + 0 | group), data=dat1, dispformula=~0)
fit.us $sdr$pdHess ## Converged ? fit.us
## [1] TRUE
The estimated variance and correlation parameters are:
VarCorr(fit.us)
##
## Conditional model:
## Groups Name Std.Dev. Corr
## group times1 1.5313
## times2 1.4346 0.403
## times3 1.4427 0.260 0.340
## times4 1.4712 0.194 0.266 0.399
## times5 1.4539 0.108 0.167 0.254 0.342
## times6 1.4223 0.031 0.096 0.194 0.268 0.358
The estimated correlation is approximately constant along diagonals (apparent Toeplitz structure) and we note that the first off-diagonal is now ca. half the true value (0.7) because the dispersion is effectively included in the estimated covariance matrix (i.e. \(\rho' = \rho \sigma^2_{{\text {AR}}}/(\sigma^2_{{\text {AR}}} + \sigma^2_{{\text {meas}}})\)).
The next natural step would be to reduce the number of parameters by collecting correlation parameters within the same off-diagonal. This amounts to 24 correlation parameters and 25 variance parameters.
We use dispformula = ~0
to suppress the residual
variance (it actually gets set to a small value controlled by the
zerodisp_val
argument of glmmTMBControl()
)1
<- glmmTMB(y ~ toep(times + 0 | group), data=dat1,
fit.toep dispformula=~0)
## Warning in (function (start, objective, gradient = NULL, hessian = NULL, :
## NA/NaN function evaluation
$sdr$pdHess ## Converged ? fit.toep
## [1] TRUE
The estimated variance and correlation parameters are:
<- VarCorr(fit.toep)) (vc.toep
##
## Conditional model:
## Groups Name Std.Dev. Corr
## group times1 1.5227
## times2 1.4329 0.370
## times3 1.4436 0.264 0.370
## times4 1.4705 0.185 0.264 0.370
## times5 1.4638 0.106 0.185 0.264 0.370
## times6 1.4249 0.028 0.106 0.185 0.264 0.370
The diagonal elements are all approximately equal to the true total variance (\(\sigma^2_{{\text {AR}}} + \sigma^2_{{\text {meas}}}\)=2), and the off-diagonal elements are approximately equal to the expected value of 0.7/2=0.35.
<- vc.toep$cond[[1]] ## first term of var-cov for RE of conditional model
vc1 summary(diag(vc1))
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 2.030 2.061 2.113 2.132 2.158 2.319
summary(vc1[row(vc1)!=col(vc1)])
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.0611 0.3821 0.5561 0.5246 0.7696 0.8067
We can get a slightly better estimate of the variance by using REML estimation (however, the estimate of the correlations seems to have gotten slightly worse):
<- update(fit.toep, REML=TRUE) fit.toep.reml
## Warning in finalizeTMB(TMBStruc, obj, fit, h, data.tmb.old): Model convergence
## problem; false convergence (8). See vignette('troubleshooting'),
## help('diagnose')
<- VarCorr(fit.toep.reml)$cond[[1]]
vc1R summary(diag(vc1R))
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 2.031 2.062 2.114 2.133 2.158 2.319
summary(vc1R[row(vc1R)!=col(vc1R)])
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.06185 0.38288 0.55688 0.52532 0.77038 0.80750
The compound symmetry structure collects all off-diagonal elements of the correlation matrix to one common value.
We again use dispformula = ~0
to make the model
parameters identifiable (see the footnote in The Toeplitz structure; a similar,
although slightly simpler, argument applies here).
<- glmmTMB(y ~ cs(times + 0 | group), data=dat1, dispformula=~0)
fit.cs $sdr$pdHess ## Converged ? fit.cs
## [1] TRUE
The estimated variance and correlation parameters are:
VarCorr(fit.cs)
##
## Conditional model:
## Groups Name Std.Dev. Corr
## group times1 1.5554 0.246 (cs)
## times2 1.4302
## times3 1.4220
## times4 1.4478
## times5 1.4543
## times6 1.4491
The models ar1
, toep
, and us
are nested so we can use:
anova(fit.ar1, fit.toep, fit.us)
## Data: dat1
## Models:
## fit.ar1: y ~ ar1(times + 0 | group), zi=~0, disp=~1
## fit.toep: y ~ toep(times + 0 | group), zi=~0, disp=~0
## fit.us: y ~ us(times + 0 | group), zi=~0, disp=~0
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## fit.ar1 4 20749 20775 -10370 20741
## fit.toep 12 20753 20833 -10364 20729 11.8164 8 0.1596
## fit.us 22 20766 20913 -10361 20722 7.0402 10 0.7216
ar1
has the lowest AIC (it’s the simplest model, and
fits the data adequately); we can’t reject the (true in this case!) null
model that an AR1 structure is adequate to describe the data.
The model cs
is a sub-model of toep
:
anova(fit.cs, fit.toep)
## Data: dat1
## Models:
## fit.cs: y ~ cs(times + 0 | group), zi=~0, disp=~0
## fit.toep: y ~ toep(times + 0 | group), zi=~0, disp=~0
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## fit.cs 8 20966 21020 -10475 20950
## fit.toep 12 20753 20833 -10364 20729 221.15 4 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Here we can reject the null hypothesis of compound symmetry (i.e., that all the pairwise correlations are the same).
Coordinate information can be added to a variable using the
glmmTMB
function numFactor
. This is necessary
in order to use those covariance structures that require coordinates.
For example, if we have the numeric coordinates
<- sample(1:2, 10, replace=TRUE)
x <- sample(1:2, 10, replace=TRUE) y
we can generate a factor representing \((x,y)\) coordinates by
<- numFactor(x,y)) (pos
## [1] (1,1) (2,1) (1,1) (1,1) (1,2) (1,1) (1,2) (1,1) (1,1) (1,2)
## Levels: (1,1) (2,1) (1,2)
Numeric coordinates can be recovered from the factor levels:
parseNumLevels(levels(pos))
## [,1] [,2]
## [1,] 1 1
## [2,] 2 1
## [3,] 1 2
In order to try the remaining structures on our test data we
re-interpret the time factor using numFactor
:
$times <- numFactor(dat1$times)
dat1levels(dat1$times)
## [1] "(1)" "(2)" "(3)" "(4)" "(5)" "(6)"
Having the numeric times encoded in the factor levels we can now try the Ornstein–Uhlenbeck covariance structure.
<- glmmTMB(y ~ ou(times + 0 | group), data=dat1)
fit.ou $sdr$pdHess ## Converged ? fit.ou
## [1] TRUE
It should give the exact same results as ar1
in this
case since the times are equidistant:
VarCorr(fit.ou)
##
## Conditional model:
## Groups Name Std.Dev. Corr
## group times(1) 1.06953
## times(2) 1.06953 0.695
## times(3) 1.06953 0.483 0.695
## times(4) 1.06953 0.336 0.483 0.695
## times(5) 1.06953 0.233 0.336 0.483 0.695
## times(6) 1.06953 0.162 0.233 0.336 0.483 0.695
## Residual 0.99511
However, note the differences between ou
and
ar1
:
ou
can handle irregular time points.ou
only allows positive correlation between neighboring
time points.The structures exp
, gau
and
mat
are meant to used for spatial data. They all require a
Euclidean distance matrix which is calculated internally based on the
coordinates. Here, we will try these models on the simulated time series
data.
An example with spatial data is presented in a later section.
<- glmmTMB(y ~ mat(times + 0 | group), data=dat1, dispformula=~0)
fit.mat $sdr$pdHess ## Converged ? fit.mat
## [1] TRUE
VarCorr(fit.mat)
##
## Conditional model:
## Groups Name Std.Dev. Corr
## group times(1) 1.4603
## times(2) 1.4603 0.373
## times(3) 1.4603 0.250 0.373
## times(4) 1.4603 0.178 0.250 0.373
## times(5) 1.4603 0.131 0.178 0.250 0.373
## times(6) 1.4603 0.097 0.131 0.178 0.250 0.373
“Gaussian” refers here to a Gaussian decay in correlation with distance, i.e. \(\rho = \exp(-d x^2)\), not to the conditional distribution (“family”).
<- glmmTMB(y ~ gau(times + 0 | group), data=dat1, dispformula=~0)
fit.gau $sdr$pdHess ## Converged ? fit.gau
## [1] TRUE
VarCorr(fit.gau)
##
## Conditional model:
## Groups Name Std.Dev. Corr
## group times(1) 1.4458
## times(2) 1.4458 0.286
## times(3) 1.4458 0.007 0.286
## times(4) 1.4458 0.000 0.007 0.286
## times(5) 1.4458 0.000 0.000 0.007 0.286
## times(6) 1.4458 0.000 0.000 0.000 0.007 0.286
<- glmmTMB(y ~ exp(times + 0 | group), data=dat1)
fit.exp $sdr$pdHess ## Converged ? fit.exp
## [1] TRUE
VarCorr(fit.exp)
##
## Conditional model:
## Groups Name Std.Dev. Corr
## group times(1) 1.06953
## times(2) 1.06953 0.695
## times(3) 1.06953 0.483 0.695
## times(4) 1.06953 0.336 0.483 0.695
## times(5) 1.06953 0.233 0.336 0.483 0.695
## times(6) 1.06953 0.162 0.233 0.336 0.483 0.695
## Residual 0.99511
Starting out with the built in volcano
dataset we
reshape it to a data.frame
with pixel intensity
z
and pixel position x
and y
:
<- data.frame(z = as.vector(volcano),
d x = as.vector(row(volcano)),
y = as.vector(col(volcano)))
Next, add random normal noise to the pixel intensities and extract a small subset of 100 pixels. This is our spatial dataset:
set.seed(1)
$z <- d$z + rnorm(length(volcano), sd=15)
d<- d[sample(nrow(d), 100), ] d
Display sampled noisy volcano data:
<- array(NA, dim(volcano))
volcano.data cbind(d$x, d$y)] <- d$z
volcano.data[image(volcano.data, main="Spatial data", useRaster=TRUE)
Based on this data, we’ll attempt to re-construct the original image.
As model, it is assumed that the original image
image(volcano)
is a realization of a random field with
correlation decaying exponentially with distance between pixels.
Denoting by \(u(x,y)\) this random field the model for the observations is
\[ z_{i} = \mu + u(x_i,y_i) + \varepsilon_i \]
To fit the model, a numFactor
and a dummy grouping
variable must be added to the dataset:
$pos <- numFactor(d$x, d$y)
d$group <- factor(rep(1, nrow(d))) d
The model is fit by
<- glmmTMB(z ~ 1 + exp(pos + 0 | group), data=d) f
Recall that a standard deviation sd=15
was used to
distort the image. A confidence interval for this parameter is
confint(f, "sigma")
## 2.5 % 97.5 % Estimate
## sigma 10.61016 18.06028 13.84278
The glmmTMB predict
method can predict unseen levels of
the random effects. For instance to predict a 3-by-3 corner of the image
one could construct the new data:
<- data.frame( pos=numFactor(expand.grid(x=1:3,y=1:3)) )
newdata $group <- factor(rep(1, nrow(newdata)))
newdata newdata
## pos group
## 1 (1,1) 1
## 2 (2,1) 1
## 3 (3,1) 1
## 4 (1,2) 1
## 5 (2,2) 1
## 6 (3,2) 1
## 7 (1,3) 1
## 8 (2,3) 1
## 9 (3,3) 1
and predict using
predict(f, newdata, type="response", allow.new.levels=TRUE)
## [1] 114.3405 115.2834 116.2061 114.6033 115.6916 116.7725 114.7828 116.0382
## [9] 117.3101
A specific image column can thus be predicted using the function
<- function(i) {
predict_col <- data.frame( pos = numFactor(expand.grid(1:87,i)))
newdata $group <- factor(rep(1,nrow(newdata)))
newdatapredict(f, newdata=newdata, type="response", allow.new.levels=TRUE)
}
Prediction of the entire image is carried out by (this takes a while…):
<- sapply(1:61, predict_col) pred
Finally plot the re-constructed image by
image(pred, main="Reconstruction")
For various advanced purposes, such as computing likelihood profiles, it is useful to know the details of the parameterization of the models - the scale on which the parameters are defined (e.g. standard deviation, variance, or log-standard deviation for variance parameters) and their order.
For an unstructured matrix of size n
, parameters
1:n
represent the log-standard deviations while the
remaining n(n-1)/2
(i.e. (n+1):(n:(n*(n+1)/2))
) are the elements of the
scaled Cholesky factor of the correlation matrix, filled in
row-wise order (see TMB
documentation). In particular, if \(L\) is the lower-triangular matrix with 1
on the diagonal and the correlation parameters in the lower triangle,
then the correlation matrix is defined as \(\Sigma = D^{-1/2} L L^\top D^{-1/2}\),
where \(D = \textrm{diag}(L L^\top)\).
For a single correlation parameter \(\theta_0\), this works out to \(\rho = \theta_0/\sqrt{1+\theta_0^2}\).
(See calculations here.)
<- VarCorr(fit.us)
vv0 <- vv0$cond$group ## extract 'naked' V-C matrix
vv1 <- nrow(vv1)
n <- getME(fit.us,"theta") ## extract V-C parameters
rpars ## first n parameters are log-std devs:
all.equal(unname(diag(vv1)),exp(rpars[1:n])^2)
## [1] TRUE
## now try correlation parameters:
<- rpars[-(1:n)]
cpars length(cpars)==n*(n-1)/2 ## the expected number
## [1] TRUE
<- diag(n)
cc upper.tri(cc)] <- cpars
cc[<- crossprod(cc)
L <- diag(1/sqrt(diag(L)))
D round(D %*% L %*% D,3)
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1.000 0.403 0.260 0.194 0.108 0.031
## [2,] 0.403 1.000 0.340 0.266 0.167 0.096
## [3,] 0.260 0.340 1.000 0.399 0.254 0.194
## [4,] 0.194 0.266 0.399 1.000 0.342 0.268
## [5,] 0.108 0.167 0.254 0.342 1.000 0.358
## [6,] 0.031 0.096 0.194 0.268 0.358 1.000
round(unname(attr(vv1,"correlation")),3)
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1.000 0.403 0.260 0.194 0.108 0.031
## [2,] 0.403 1.000 0.340 0.266 0.167 0.096
## [3,] 0.260 0.340 1.000 0.399 0.254 0.194
## [4,] 0.194 0.266 0.399 1.000 0.342 0.268
## [5,] 0.108 0.167 0.254 0.342 1.000 0.358
## [6,] 0.031 0.096 0.194 0.268 0.358 1.000
all.equal(c(cov2cor(vv1)),c(fit.us$obj$env$report(fit.us$fit$parfull)$corr[[1]]))
## [1] TRUE
Profiling (experimental/exploratory):
## want $par, not $parfull: do NOT include conditional modes/'b' parameters
<- fit.us$fit$par
ppar length(ppar)
## [1] 22
range(which(names(ppar)=="theta")) ## the last n*(n+1)/2 parameters
## [1] 2 22
## only 1 fixed effect parameter
<- tmbprofile(fit.us$obj,2,trace=FALSE) tt
confint(tt)
plot(tt)
<- fit.cs$fit$par
ppar length(ppar)
## [1] 8
range(which(names(ppar)=="theta")) ## the last n*(n+1)/2 parameters
## [1] 2 8
## only 1 fixed effect parameter, 1 dispersion parameter
<- tmbprofile(fit.cs$obj,3,trace=FALSE) tt2
plot(tt2)
Consider a generalized linear mixed model
\[\begin{equation} g(\boldsymbol{\mu}) = \boldsymbol{X\beta} + \boldsymbol{Zb} \end{equation}\]
where \(g(.)\) is the link function; \(\boldsymbol{\beta}\) is a p-dimensional vector of regression coefficients related to the covariates; \(\boldsymbol{X}\) is an \(n \times p\) model matrix; and \(\boldsymbol{Z}\) is the \(n\times q\) model matrix for the \(q\)-dimensional vector-valued random effects variable \(\boldsymbol{U}\) which is multivariate normal with mean zero and a parameterized \(q \times q\) variance-covariance matrix, \(\boldsymbol{\Sigma}\), i.e., \(\boldsymbol{U} \sim N(\boldsymbol{0}, \boldsymbol{\Sigma})\).
A general latent variable model (GLVM) requires many fewer parameters for the variance-covariance matrix, \(\boldsymbol{\Sigma}\). To a fit a GLVM we add a reduced-rank (rr) covariance structure, so the model becomes \[\begin{align} g(\boldsymbol{\mu}) &= \boldsymbol{X\beta} + \boldsymbol{Z(I_n \otimes \Lambda)b} \\ &= \boldsymbol{X\beta} + \boldsymbol{Zb_{new}} \end{align}\] where \(\otimes\) is the Kronecker product and \(\boldsymbol{\Lambda} = (\boldsymbol{\lambda_1}, \ldots, \boldsymbol{\lambda_d})'\) is the \(q \times d\) matrix of factor loadings (with \(d \ll q\)). The upper triangular elements of \(\boldsymbol{\Lambda}\) are set to be zero to ensure parameter identifiability. Here we assume that the latent variables follow a multivariate standard normal distribution, \(\boldsymbol{b} \sim N(\boldsymbol{0}, \boldsymbol{I})\).
For GLVMs it is important to select initial starting values for the parameters because the observed likelihood may be multimodal, and maximization algorithms can end up in local maxima. Niku et al. (2019) describe methods to enable faster and more reliable fits of latent variable models by carefully choosing starting values of the parameters.
A similar method has been implemented in glmmTMB
. A
generalized linear model is fitted to the data to obtain initial
starting values for the fixed parameters in the model. Residuals from
the fitted GLM are calculated; Dunn-Smyth residuals are calculated for
common families while residuals from the dev.resids()
function are used otherwise. Initial starting values for the latent
variables and their loadings are obtained by fitting a reduced rank
model to the residuals.
One of our main motivations for adding this variance-covariance structure is to enable the analysis of multivariate abundance data, for example to model the abundance of different taxa across multiple sites. Typically an unstructured random effect is assumed to account for correlation between taxa; however the number of parameters required quickly becomes large with increasing numbers of taxa. A GLVM is a flexible and more parsimonious way to account for correlation so that one can fit a joint model across many taxa.
A GLVM can be fit by specifying a reduced rank (rr
)
covariance structure. For example, the code for modeling the mean
abundance against taxa and to account for the correlation between taxa
using two latent variables is as follows
if (require(mvabund)) {
data(spider)
## organize data into long format
<- sort(colSums(spider$abund), decreasing = TRUE)
sppTot <- cbind(spider$abund, spider$x)
tmp $id <- 1:nrow(tmp)
tmp<- reshape(tmp,
spiderDat idvar = "id",
timevar = "Species",
times = colnames(spider$abund),
varying = list(colnames(spider$abund)),
v.names = "abund",
direction = "long")
## fit rank-reduced models with varying dimension
<- lapply(2:10,
fit_list function(d) {
<- glmmTMB(abund ~ Species + rr(Species + 0|id, d = d),
fit.rr data = spiderDat)
})## compare fits via AIC
<- sapply(fit_list, AIC)
aic_vec - min(aic_vec, na.rm = TRUE) aic_vec
The left hand side of the bar taxa + 0
corresponds to a
factor loading matrix that accounts for the correlations among taxa. The
right hand side of the bar splits the above specification independently
among sites. The d
is a non-negative integer (which
defaults to 2).
An option in glmmTMBControl()
has been included to
initialize the starting values for the parameters based on the approach
mentioned above with the default set at
glmmTMBControl(start_method = list(method = NULL, jitter.sd = 0)
:
method = "res"
initializes starting values from the
results of fitting a GLM, and fitting a reduced rank model to the
residuals to obtain starting values for the fixed coefficients, the
latent variables and the factor loadings.jitter.sd
adds variation to the starting values of
latent variables when method = "res"
(default 0).For a reduced rank matrix of rank d
, parameters
1:d
represent the diagonal factor loadings while the
remaining \(nd-d(d-3)/2\),
(i.e. parameters (d+1):(nd-d(d-1)/2
) are the lower diagonal
factor loadings filled in column-wise order. The factor loadings from a
model can be obtained by
fit.rr$obj$env$report(fit.rr$fit$parfull)$fact_load[[1]]
.
An appropriate rank for the model can be determined by standard model
selection approaches such as information criteria (e.g. AIC or BIC)
(Hui et al. 2015).
This section will explain how covariance matrices are constructed
“under the hood”, and in particular why the 0+
term is
generally required in models for temporal and spatial covariances.
Probably the key insight here is that the terms in a random effect
(the f
formula in a random-effects term (f|g)
are expanded using the base-R machinery for regression model formulas.
In the case of an intercept-only random effect (1|g)
, the
model matrix is a column of ones, so we have a \(1 \times 1\) covariance matrix - a single
variance. For a random-slopes model (x|g)
or
(1+x|g)
, where x
is a numeric variable, the
model matrix has two columns, a column of ones and column of observed
values of x
, and the covariance matrix is \(2 \times 2\) (intercept variance, slope
variance, intercept-slope covariance).
Things start to get weird when we have (f|g)
(or
(1+f|g)
) where f
is a factor (representing a
categorical variable). R uses treatment contrasts by default;
if the observed values of f
are
c("c", "s", "v")
2 the corresponding factor will have a
baseline level of "c"
by default, and the model matrix will
be:
model.matrix(~f, data.frame(f=factor(c("c", "s", "v"))))
## (Intercept) fs fv
## 1 1 0 0
## 2 1 1 0
## 3 1 0 1
## attr(,"assign")
## [1] 0 1 1
## attr(,"contrasts")
## attr(,"contrasts")$f
## [1] "contr.treatment"
i.e., an intercept (which corresponds to the predicted mean value for
observations in group c
) followed by dummy variables that
describe contrasts between the predicted mean values for s
and c
(fs
) and between v
and
c
(fv
). The covariance matrix is \(3 \times 3\) and looks like this:
\[ \newcommand{\ssub}[1]{\sigma^2_{\textrm{#1}}} \newcommand{\csub}[2]{\sigma^2_{\textrm{#1}, \textrm{#2}}} \left( \begin{array}{ccc} \ssub{c} & \csub{c}{s-c} & \csub{c}{v-c} \\ \csub{c}{s-c} & \ssub{s-c} & \csub{s-c}{v-c} \\ \csub{c}{v-c} & \csub{s-c}{v-c} & \ssub{v-c} \end{array} \right) \]
This might be OK for some problems, but the parameters of the model will often be more interpretable if we remove the intercept from the formula:
model.matrix(~0+f, data.frame(f=factor(c("c", "s", "v"))))
## fc fs fv
## 1 1 0 0
## 2 0 1 0
## 3 0 0 1
## attr(,"assign")
## [1] 1 1 1
## attr(,"contrasts")
## attr(,"contrasts")$f
## [1] "contr.treatment"
The corresponding covariance matrix is
\[ \left( \begin{array}{ccc} \ssub{c} & \csub{c}{s} & \csub{c}{v} \\ \csub{c}{s} & \ssub{s} & \csub{s}{v} \\ \csub{c}{v} & \csub{s}{v} & \ssub{v} \end{array} \right) \]
This is easier to understand (the elements are the variances of the
intercepts for each group, and the covariances between intercepts of
different groups). If we use an ‘unstructured’ model
(us(f|g)
, or just plain (f|g)
), then this
reparameterization won’t make any difference in the overall model fit.
However, if we use a structured covariance model, then the choice
matters: for example, the two models diag(f|g)
and
diag(0+f|g)
give rise to the covariance matrices
\[ \left( \begin{array}{ccc} \ssub{c} & 0 & 0 \\ 0 & \ssub{s-c} & 0 \\ 0 & 0 & \ssub{v-c} \end{array} \right) \;\; \textrm{vs} \;\; \left( \begin{array}{ccc} \ssub{c} & 0 & 0 \\ 0 & \ssub{s} & 0 \\ 0 & 0 & \ssub{v} \end{array} \right) \]
which cannot be made equivalent by changing parameters.
What does this have to do with temporally/spatially structured covariance matrices? In this case, if two points are separated by a distance \(d_{ij}\) (in space or time), we typically want their correlation to be \(\sigma^2 \rho(d_{ij})\), where \(\rho()\) is a temporal or spatial autocorrelation function (e.g. in the AR1 model, \(\rho(d_{ij}) = \phi^{d_{ij}}\)). So we want to set up a covariance matrix
\[ \sigma^2 \left( \begin{array}{cccc} 1 & \rho(d_{12}) & \rho(d_{13}) & \ldots \\ \rho(d_{12}) & 1 & \rho(d_{23}) & \ldots \\ \rho(d_{13}) & \rho(d_{23}) & 1 & \ldots \\ \vdots & \vdots & \vdots & \ddots \end{array} \right) \]
How glmmTMB
actually does this internally is to
numFactor()
to compute the corresponding pairwise
distancesar1
or
ou
) and the autocorrelation parameters (drawn from the
parameter vector) to specify the autocorrelation functionIn order for this to work, we need the \(i^\textrm{th}\) column of the corresponding
model matrix to correspond to an indicator variable for whether an
observation is at the \(i^\textrm{th}\)
location — not to a contrast between the \(i\textrm{th}\) level and the first level!
So, we want to use e.g. ar1(0 + time|g)
, not
ar1(time|g)
(which is equivalent to
ar1(1+time|g)
).
Why do we do this? Consider the slightly simplified case of a homogeneous Toeplitz structure where all of the variance parameters are identical. The diagonal elements of the covariance matrix are equal to \(\sigma_t^2\), the off-diagonals to \(\sigma_t^2 \cdot \rho(|i-j|)\). If we add a residual variance to the model then the diagonal of the combined covariance matrix becomes \(\sigma_t^2 + \sigma_r^2\) and the off-diagonals become \((\sigma_t^2 + \sigma_r^2) \rho(|i-j|)\). However, by reparameterizing the Toeplitz model to \(\{{\sigma_t^2}' = \sigma_t^2 + \sigma_r^2, \rho'(|i-j|) = \rho(|i-j|) \cdot \frac{\sigma_t^2}{\sigma_t^2 + \sigma_r^2}\}\) — that is, by inflating the variance and deflating the correlation parameters — we can get back to an equivalent Toeplitz model. This implies that the residual variance and the Toeplitz covariance parameters are jointly unidentifiable, which is likely to make problems for the fitting procedure.↩︎
chocolate, strawberry, vanilla↩︎