smooth.construct.spde.smooth.spec: Matern SPDE smooth

View source: R/spde.R

smooth.construct.spde.smooth.specR Documentation

Matern SPDE smooth

Description

A smooth term for a spatially (or temporally) continuous Gaussian random field, usable as s(x, y, bs = "spde", xt = list(mesh = mesh)) in any hmmTMB formula, and so on any transition probability or any parameter of any observation distribution.

Usage

## S3 method for class 'spde.smooth.spec'
smooth.construct(object, data, knots)

## S3 method for class 'spde.smooth'
Predict.matrix(object, data)

Arguments

object, data, knots

As mgcv::smooth.construct(); for Predict.matrix, knots is absent and the rest are as mgcv::Predict.matrix().

Value

A smoothCon object of class spde.smooth, with sparse X and sparse penalties.

The mesh in one dimension

A one-dimensional field needs no mesh: s(x, bs = "spde") builds one by default, as k quadratic B-spline basis functions on evenly spaced knots, so

f <- ~ s(x, bs = "spde")          # and s(x, bs = "spde", k = 40) for a finer one

is the whole of it. The knots are spread over the range of the covariate widened by a fifth on each side, which is the one-dimensional version of the outer extension of a two-dimensional mesh: with the knots stopping at the data, the boundary condition inflates the field's variance at the two ends by a third or more.

k is a resolution, not a smoothing parameter, and this is the one place where a field does not behave like the smooths around it. The range is estimated and is what does the smoothing, so k is not a cap on wiggliness that the penalty then pulls back from: it only has to be large enough to resolve the range the data ask for, knots about a third of a range apart as in two dimensions. That is why the default is k = 15 rather than mgcv's 10 – with the widening, 15 knots put the knot spacing at about half the range the smoother starts from, which is a fifth of the spread of the covariate.

Too small a k does not simply oversmooth. Once the knots are further apart than the range the data want, the weights are near enough independent that the likelihood cannot tell one short range from another, and the range collapses towards zero while the standard deviation grows to compensate. The fitted curve can still look right when that happens, because the basis is then doing the smoothing on its own, but the two parameters are meaningless. So read lambda() before reading the curve: a range far below the knot spacing, or a standard deviation orders of magnitude above the scale of the linear predictor, means raise k and refit.

Passing your own fmesher::fm_mesh_1d() overrides all of this, and is worth doing when the covariate is unevenly spread and you want the knots placed accordingly.

Building a mesh in two dimensions

In two dimensions the mesh is yours to build and pass in, because a good one depends on the domain, the data and the range you expect. fmesher::fm_mesh_2d() takes locations, a maximum triangle edge length inside the domain and in the outer extension, and a cutoff below which nearby points are merged:

mesh <- fmesher::fm_mesh_2d(loc = cbind(data$x, data$y),
                            max.edge = c(0.1, 0.3),
                            cutoff = 0.05, offset = c(0.1, 0.3))
f <- ~ s(x, y, bs = "spde", xt = list(mesh = mesh))

A rule of thumb is max.edge no larger than a third of the range you expect, and an offset about equal to the range; without the outer extension the field's variance is inflated at the boundary.

The field is evaluated at every time step, so missing covariates are replaced by the last non-missing value as elsewhere in hmmTMB. If that is too crude for locations, interpolate them before fitting.

Parameterisation

The precision is

Q(\tau, \kappa) = \tau^2(\kappa^4 C + 2\kappa^2 G_1 + G_2)

with C, G_1, G_2 the finite element matrices from fmesher::fm_fem(). alpha = 2 throughout, so the Matern smoothness is \nu = 2 - d/2: \nu = 1 in two dimensions and \nu = 3/2 in one. The range is \rho = \sqrt{8\nu}/\kappa and the marginal variance is \sigma^2 = \Gamma(\nu) / (\Gamma(2)(4\pi)^{d/2}\kappa^{2\nu}\tau^2), which is 1/(\tau^2\kappa^2 4\pi) in two dimensions and 1/(4\tau^2\kappa^3) in one. Both constants depend on d, so the one- and two-dimensional smoothers do not share them.

The two estimated parameters are \log\sigma and \log\rho, not \log\tau and \log\kappa, and appear as sd and range in lambda(). The change is exact – a linear map of the log parameters, absorbed into the constants in front of the three matrices – and worth making: the parameters mean something on their own, and the likelihood's long ridge along constant marginal variance runs diagonally in (\tau, \kappa) but along an axis in (\sigma, \rho), which the optimiser finds much easier.

Both are estimated freely, with no prior. A single realisation identifies them only loosely, and a field whose range approaches the size of the domain is close to improper: the range then runs off and the standard deviation follows it, with the fitted surface barely changing. The fit is still usable if that happens, but the two parameters are not, and a finer mesh will not help – it is the data that are uninformative.

The field is proper for any positive \kappa, so no identifiability constraint is imposed, as in INLA. It is not centred, and its level is only weakly separated from the intercept when the range is large.

Bandwidth

A model containing this smooth is fitted with the banded forward algorithm, because the exact one makes the Hessian with respect to the field weights dense and so defeats the sparsity the SPDE representation exists to provide. See HMM$update_bw() and HMM$check_bw().

References

Lindgren, F., Rue, H. and Lindstrom, J. (2011). An explicit link between Gaussian fields and Gaussian Markov random fields. JRSS-B 73, 423-498.

Miller, D. L., Glennie, R., & Seaton, A. E. (2020). Understanding the stochastic partial differential equation approach to smoothing. Journal of Agricultural, Biological and Environmental Statistics, 25(1), 1-16.

Fischer, J.-O. (2026). Fast and scalable inference in hidden Markov models with Gaussian fields.

Examples

# A one-dimensional field over a covariate, with a default mesh
if(requireNamespace("fmesher", quietly = TRUE)) {
  d <- data.frame(x = runif(100))
  sm <- mgcv::smoothCon(mgcv::s(x, bs = "spde"), data = d)[[1]]
  c(k = ncol(sm$X), sm$theta.names)
}


hmmTMB documentation built on Nov. 10, 2026, 5:09 p.m.