This vignette details the different covariance structures available in clustTMB.

Random Effect Covariance Matrices

Covariance Notation No..of.Parameters Data.requirements
Spatial GMRF gmrf 2 spatial coordinates
AR(1) ar1 2 unit spaced levels
Rank Reduction rr(random = H) JH - (H(H-1))/2
Spatial Rank Reduction rr(spatial = H) 1 + JH - (H(H-1))/2 spatial coordinates

Spatial GMRF

clustTMB fits spatial random effects using a Gaussian Markov Random Field (GMRF). The precision matrix, QQ, of the GMRF is the inverse of a Matern covariance function and takes two parameters: 1) κ\kappa, which is the spatial decay parameter and a scaled function of the spatial range, ϕ=8/κ\phi = \sqrt{8}/\kappa, the distance at which two locations are considered independent; and 2) τ\tau, which is a function of κ\kappa and the marginal spatial variance σ2\sigma^{2}:

τ=12πκσ.\tau = \frac{1}{2\sqrt{\pi}\kappa\sigma}. The precision matrix is approximated following the SPDE-FEM approach [@Lindgren2011], where a constrained Delaunay triangulation network is used to discretize the spatial extent in order to determine a GMRF for a set of irregularly spaced locations, i$.

ωi∼GMRF(Q[κ,τ])\omega_{i} \sim GMRF(Q[\kappa, \tau])

Spatial Example

Prior to fitting a spatial cluster model with clustTMB, users need to set up the constrained Delaunay Triangulation network using the R package, fmesher. This package provides a CRAN distributed collection of mesh functions developed for the package, R-INLA. For guidance on setting up an appropriate mesh, see Triangulation details and examples and Tools for mesh assessment from

In this example, the following mesh specifications were used:

loc <- meuse[, 1:2]
Bnd <- fmesher::fm_nonconvex_hull(as.matrix(loc), convex = 200)
meuse.mesh <- fmesher::fm_mesh_2d(as.matrix(loc),
  max.edge = c(300, 1000),
  boundary = Bnd
)

Coordinates are converted to a spatial point dataframe and read into the clustTMB model, along with the mesh, using the spatial.list argument. The gating formula is specified using the gmrf() command:

Loc <- sf::st_as_sf(loc, coords = c("x", "y"))
mod <- clustTMB(
  response = meuse[, 3:6],
  family = lognormal(link = "identity"),
  gatingformula = ~ gmrf(0 + 1 | loc),
  G = 4, covariance.structure = "VVV",
  spatial.list = list(loc = Loc, mesh = meuse.mesh)
)
## Warning: the 'nobars' function has moved to the reformulas package. Please
## update your imports, or ask an upstream package maintainer to do so.
## intercept removed from gatingformula
##             when random effects specified
## spatial projection is turned off. Need to provide locations in projection.list$grid.df for spatial predictions

Models are optimized with nlminb(), model results can be viewed with nlminb commands:

# Estimated fixed parameters
mod$opt$par
##      betag      betag      betag      betad      betad      betad      betad 
##  0.1652252  0.5709658  0.1645272  2.0157776  4.3160897  5.4259893  6.7095866 
##      betad      betad      betad      betad      betad      betad      betad 
##  1.0064936  3.6030480  5.2113113  6.2155333  0.1259793  3.1475101  4.2016926 
##      betad      betad      betad      betad      betad      theta      theta 
##  5.2523432 -1.4361515  3.1132969  4.2118610  5.1996586 -1.2100819 -2.9055460 
##      theta      theta      theta      theta      theta      theta      theta 
## -1.2794745 -1.2502131 -2.5623943 -3.1154937 -2.2459530 -2.3607821 -1.8075039 
##      theta      theta      theta      theta      theta      theta      theta 
## -4.0486770 -2.6845201 -3.0661880 -2.4648414 -3.3381091 -2.7804404 -2.6686164 
##  ln_kappag 
## -5.9089474
# Minimum negative log likelihood
mod$opt$objective
## [1] 2318.798

Gating Network Examples

When random effects, 𝕦\mathbb{u}, are specified in the gating network, the probability of cluster membership πi,g\pi_{i,g} for observation ii is fit using multinomial regression:

η,g=Xβ,g+𝕦,gℼ,g=exp(η,g)∑g=1Gexp(η,g) \begin{align} \mathbb{\eta}_{,g} &= X\mathbb{\beta}_{,g} + \mathbb{u}_{,g} \\ \mathbb{\pi}_{,g} &= \frac{ exp(\mathbb{\eta}_{,g})}{\sum^{G}_{g=1}exp(\mathbb{\eta}_{,g})} \end{align}