This page summarises the method. The derivation and its checks are in the report of the project the package grew out of (to be published).
Why this method
Areal models in space and time attach a latent value to every area-time cell, but surveys usually reach only a small fraction of the cells. The standard tools treat the empty cells in one of two ways. Each makes one of two operations on a Gaussian vector cheap and the other expensive:
- Marginalising, integrating some cells out, is free when the model is specified by its covariance: the observed cells’ covariance is just their rows and columns. This is how Gaussian-process (geostatistical) models work. They carry only the observed locations, and they predict the rest afterwards by kriging. Conditioning, the rest given the observed, then needs dense matrix solves.
- Areal priors (CAR, Leroux, BYM, AR1 and random walks in time) are usually specified by their sparse precision matrix, a Gaussian Markov random field (GMRF). Here conditioning is cheap, needing only sparse blocks of the precision. Marginalising destroys the sparsity: the precision of the observed cells alone is a dense Schur complement. GMRF-based software (INLA, Rue et al. 2009; CARBayes, Lee 2013; ICAR models in Stan, Morris et al. 2019) therefore keeps a latent value for every cell, empty or not, because that is what keeps the algebra sparse.
With hundreds of areas and a few decades of months, the full table holds tens of thousands of cells. That is a heavy load for MCMC, and coarser time steps are often chosen to keep it down. stareal takes the Gaussian-process route for areal models. For a separable space-time prior, the covariance between any two cells is the product of a small spatial and a small temporal matrix. The observed cells’ covariance is therefore cheap, as for a Gaussian process, while the areal prior is kept. The model is fitted on the observed cells alone, and the full surface is rebuilt afterwards, one posterior draw at a time.
This pays off when the data are sparse relative to the space-time grid: fine time steps, many areas, few surveys, as is typical of molecular surveillance. It does not when most cells are observed, when the prior is not separable (for example, temporal correlation that varies by area), or when there are tens of thousands of areas.
The model
Space is a set of m areal units (districts, counties, grid cells) with an adjacency graph; time is a regular grid of T steps (months, quarters, years). An area-time cell is one area in one time step. Surveys report y positives out of k tested. For each observed cell,
y_{at} \sim \text{Binomial}\big(k_{at},\ \text{logit}^{-1}(x_{at})\big), \qquad x_{at} = z_{at}^\top \beta + f_{at} + \epsilon_{at}.
-
z_{at}^\top \beta are the
fixed effects: an intercept, by default a linear trend
in time (
trend(), from -1 at the first step to 1 at the last), and any covariates, which may vary by area, by time or by both. Their prior is Gaussian, \beta \sim N(b_0, B). -
f is a Gaussian
field with separable covariance \Sigma = \Sigma_t \otimes \Sigma_s:
- in space, a prior on the adjacency graph, by default the Leroux conditional autoregressive model (the choices are in the next section);
- in time, a first-order autoregression with unit variance and lag-one correlation \rho_t.
- \epsilon_{at} \sim N(0, \sigma_n^2) is a cell-level nugget: variation between surveys that the smooth field does not explain. It applies to the observed cells; the predicted surface is the field without it.
Surveys in the same cell are pooled into one count, y_{at} of k_{at}. This rests on an assumption: given the cell’s latent value x_{at} (nugget included), the surveys in it are independent binomial samples of one prevalence. A sum of independent binomial counts with a common probability is binomial, so under that assumption pooling loses nothing. The model has no variation between surveys within a cell. Two surveys of different sites in the same district and month are treated as one larger survey. If they differ by more than binomial sampling, through different sites, populations or methods, pooling overstates how much the cell’s data say. Longer time steps and larger areas put more surveys together, and make this more likely.
The hyperparameters \theta = (\sigma,
\rho_s, \rho_t, \sigma_n), and those of any main effects, have
half-normal and Beta priors (areal_priors()). Any of them
can be held at a value instead (fit_areal(fixed = )), for
example \rho_t = 0 for a field with no
memory from one time step to the next.
Spatial priors
W is the adjacency matrix of the
graph, D the diagonal matrix of each
area’s number of neighbours, and R = D -
W the graph Laplacian. The priors that
fit_areal(spatial = ) offers all share the parameters \sigma (the scale) and \rho_s (the strength of spatial dependence).
In every one, \rho_s = 0 makes the
areas independent:
spatial |
The field at one time step | \rho_s |
|---|---|---|
"leroux" (the default; Leroux et al. 2000) |
precision \sigma^{-2}\{\rho_s R + (1 - \rho_s) I\} | 0 to 1 |
"bym2" (Riebler et al. 2016) |
covariance \sigma^2\{\rho_s R_\star^{+} + (1 - \rho_s) I\} | 0 to 1 |
"car" (the proper CAR) |
precision \sigma^{-2}(D - \rho_s W) | 0 to below 1 |
"iid" |
the Leroux prior at \rho_s = 0: covariance \sigma^2 I | fixed at 0 |
"icar" (Besag et al. 1991) |
the Leroux prior at \rho_s = 1: precision \sigma^{-2} R | fixed at 1 |
- Leroux moves between independence and the intrinsic CAR (ICAR) with one parameter, and stays a proper prior for \rho_s < 1.
- BYM2 is the Besag-York-Mollié model reparameterised: a mix of a scaled ICAR and independent noise. R_\star^{+} is the generalised inverse of R, scaled so that the geometric mean of its diagonal is 1. \sigma is then roughly the marginal standard deviation of the field, whatever the graph, and \rho_s the share of its variance that is spatially structured. The name \rho_s is shared with the other priors, but its meaning is not the same as in Leroux.
- The proper CAR shrinks each area towards \rho_s times the mean of its neighbours. Its marginal variances differ between areas: they are larger where there are fewer neighbours.
- ICAR is improper: it says nothing about the field’s mean over the areas. stareal gives that direction zero variance, so the field sums to zero over the areas at every time step. The regional level, and its changes over time, then come from the fixed effects alone: with the default formula, a linear trend.
BYM2 and ICAR need a connected graph (areal_frame()’s
island links make one), and the proper CAR needs every area to have a
neighbour.
All five have the same form. The spatial covariance is \Sigma_s = V \operatorname{diag}(g) V^\top, for a basis V computed once when the frame is built: the eigenvectors of R for Leroux, BYM2, iid and ICAR, and those of D^{-1/2} W D^{-1/2}, rescaled, for the proper CAR. Only the vector g depends on \sigma and \rho_s. So every prior costs the same to fit and to predict, and the rest of this page applies to each.
Main effects and interaction types
fit_areal(main = , interaction = , temporal = ) makes
the field a sum of independent Gaussian components (Knorr-Held
2000):
f_{at} = f^{ST}_{at} + f^{S}_{a} + f^{T}_{t}.
-
The space main effect f^S (
main = "space"or"both") is one value per area, the same at every time step. Its prior is the spatial prior chosen withspatial, with its own scale and dependence, \sigma_\text{space} and \rho_\text{space}. Its covariance over all cells is \mathbf{1}\mathbf{1}^\top \otimes \Sigma_S. -
The time main effect f^T (
main = "time"or"both") is one value per time step, the same in every area, with covariance \Sigma_T \otimes \mathbf{1}\mathbf{1}^\top and scale \sigma_\text{time}.temporalchooses \Sigma_T:-
"ar1": \sigma_\text{time}^2 \rho_\text{time}^{|t - t'|}; -
"rw1"and"rw2": first- and second-order random walks, with precision \sigma_\text{time}^{-2} R_k, R_k = D_k^\top D_k for the matrix D_k of k-th differences.
-
-
The interaction f^{ST} has the separable form of the default
field, scale \sigma, and a type set by
interaction:
interaction |
Time factor | Space factor |
|---|---|---|
"IV" (the default) |
AR1, \rho_t | the spatial prior, \rho_s |
"III" |
independent | the spatial prior, \rho_s |
"II" |
AR1, \rho_t | independent areas |
"I" |
independent | independent areas |
"none" |
no interaction |
Type I is an independent value per cell. On the observed cells it
cannot be told apart from the nugget, so it is fitted with
nugget = FALSE.
Constraints. A random walk is improper: R_k says nothing about the constant (RW1), or
the constant and the linear (RW2). As for the ICAR, stareal
gives those directions zero variance, rather than infinite precision:
\Sigma_T is the generalised inverse
R_k^+, computed as (R_k + N N^\top)^{-1} - N N^\top for an
orthonormal basis N of the null space.
An RW1 then sums to zero over time, and an RW2 also has zero slope. The
intercept and trend() carry the level and the straight
line, and an RW2 the curve about it. An RW1 or an AR1 time effect
overlaps trend() in part: not an error, but the trend
coefficient then means less. R_k^+ is
scaled so that the geometric mean of its diagonal is 1 (Sørbye & Rue
2014), as BYM2’s ICAR part is, so \sigma_\text{time} is roughly the time
effect’s marginal standard deviation whatever the number of time
steps.
Why add main effects. Under BYM2, ICAR and iid, the default field has little or no component shared by the whole region. The regional curve over time is then the fixed effects, by default a straight line on the logit scale (tutorial 4 shows the cost). A time main effect lets the regional curve bend under any spatial prior.
Fitting on the observed cells only
Most area-time cells have no data: in the crt K76T tutorial, 264 of 74,304 unit-months. Because everything except the likelihood is Gaussian, the unobserved cells can be integrated out exactly: the n observed cells’ latent values x_o have the Gaussian prior
x_o \sim N\big(Z_o b_0,\ \Sigma_{oo}\big), \qquad \Sigma_{oo} = \Sigma_t[t_o, t_o] \circ \Sigma_s[a_o, a_o] + Z_o B Z_o^\top + \sigma_n^2 I,
where a_o and t_o are the observed cells’ areas and time steps and \circ is the elementwise product. The fixed effects are integrated out too (the Z_o B Z_o^\top term) and drawn afterwards from their exact conditional distribution. The spatial block \Sigma_s[a_o, a_o] comes from the basis of the previous section, computed once when the frame is built, so a change of \sigma or \rho_s only rescales its eigenvalues. With main effects, their covariances on the observed cells are added: \Sigma_S[a_o, a_o] + \Sigma_T[t_o, t_o]. \Sigma_{oo} is still one n \times n matrix, so the cost per iteration hardly changes.
The MCMC therefore works on n latent values, not m \times T, and its cost per iteration grows as n^3, independently of the size of the full table. This is what makes continental maps over decades, at monthly resolution, feasible.
The sampler
Each iteration updates the latent values, then the hyperparameters.
- Latent values by elliptical slice sampling (Murray, Adams & MacKay, 2010) against a posterior-scaled Gaussian (Nishihara, Murray & Adams, 2014): the Gaussian prior combined with a Gaussian approximation of each cell’s likelihood (pseudo-data), with the approximation divided back out, so the update is exact. Proposing on this ellipse, rather than the prior’s, is what lets heavily surveyed cells mix.
- Hyperparameters by two adaptive Metropolis moves: a centred move (\theta given x_o) and the surrogate-data move of Murray and Adams (2010), in which x_o moves with \theta. The centred move alone gets stuck, because n latent values pin \theta.
- During burn-in the proposals adapt, and the pseudo-data are refitted twice to a Gaussian approximation of the likelihood at the chain’s mean latent values. Both are then fixed, so the draws after burn-in come from an exact MCMC. The refit matters most for rare outcomes, where many cells have no positives.
-
Chains start from dispersed points and run in
parallel;
diagnose()checks R-hat and effective sample sizes of every parameter, the latent value of every observed cell included.
Prediction and aggregation
For each posterior draw of (\theta, \beta, x_o), the full m \times T surface is drawn by conditional simulation (Matheron’s rule): an unconditional draw of the field, corrected by kriging so that it agrees with x_o. This is exact, and needs no matrix larger than n \times n beyond the stored eigenvectors; the work is split into blocks of areas so that memory stays bounded. Main effects are drawn and corrected the same way: one value per area and one per time step for each draw.
Each draw of the surface is then aggregated to any grouping of the
areas (regions, countries) as a weighted mean, and only then summarised.
Group estimates therefore carry the correlation between their areas,
which averaging area-level summaries would lose. The groupings can be
the frame’s, or new ones given to predict(). With
group_draws, the draws of the aggregates are returned too.
Anything derived from them, such as the change between two years or the
difference between two regions, is then computed draw by draw and keeps
the joint uncertainty.
Cross-validation
crossval() holds out folds of observed cells, refits,
and predicts each held-out cell from its exact conditional distribution
given the fitted cells, nugget included. The log predictive density
integrates that Gaussian against the binomial likelihood by adaptive
Gauss-Hermite quadrature, and calibration is checked with a randomised
probability integral transform.
Leave-one-out from one fit
loo()
approximates leave-one-out cross-validation, one observed cell at a
time, without refitting. Plain Pareto-smoothed importance sampling
(PSIS-LOO; Vehtari et al. 2017) reweights the posterior draws by 1/p(y_i \mid x_i). Here that fails. Each
observed cell has its own latent value x_i, which its own surveys pin down, so a few
draws dominate the weights.
The remedy (Vehtari et al. 2016) is to integrate x_i out first. Given the hyperparameters and the other cells’ latent values, x_i is Gaussian. Its mean and variance follow from the precision P = \Sigma_{oo}^{-1} of the observed cells:
x_i \mid x_{-i}, \theta \sim N\!\left(x_i - \frac{\{P(x_o - \mu_o)\}_i}{P_{ii}},\ \frac{1}{P_{ii}}\right),
where \mu_o is the prior mean. One
Cholesky factorisation per draw gives this for every cell at once. The
binomial likelihood is integrated over that Gaussian by quadrature, as
in crossval(). The result is the predictive density of a
new survey of cell i given everything
else. PSIS then works on these densities, which vary much less from draw
to draw. On simulated data, the approximation agrees with exact
leave-one-out (one refit per cell) to well within its standard
error.
compare_models() lines up several fits of the same data
cell by cell. It reports each model’s expected log predictive density,
its difference from the best, the standard error of that difference, and
the effective number of parameters p_\text{loo}. It does not report AIC, BIC or
DIC. AIC needs a number of parameters, which a model with a latent value
per cell does not have. BIC approximates a marginal likelihood, which in
a hierarchical model is driven by the priors. And DIC is unreliable for
latent Gaussian models.
Leaving one cell out keeps its neighbours in, so loo() measures
how well a model fills the gaps between data. For extrapolation, to
areas or countries with no data, crossval() with folds by
region is the test.
Scale
Two things set the cost of a fit:
-
The eigendecomposition of the graph.
areal_frame()does it once. It is a dense m \times m problem, costing O(m^3) in the number of areas m: quick for hundreds of areas, noticeably slower for thousands. Build the frame once and save it withsaveRDS(). - Each MCMC iteration. It costs O(n^3) in the number of observed cells n, because of a dense Cholesky factorisation. It does not depend on the number of areas or time steps.
The sampler is almost entirely dense linear algebra, so an optimised BLAS (Intel MKL, OpenBLAS or Apple Accelerate) makes a large difference over R’s reference BLAS. The method was developed on about 1,300 and 1,800 observed cells. Prediction holds one block of areas at a time, so memory stays bounded however many draws are used.
References
Knorr-Held, L. (2000). Bayesian modelling of inseparable space-time variation in disease risk. Statistics in Medicine 19, 2555–2567.
Sørbye, S. H. & Rue, H. (2014). Scaling intrinsic Gaussian Markov random field priors in spatial modelling. Spatial Statistics 8, 39–51.
Besag, J., York, J. & Mollié, A. (1991). Bayesian image restoration, with two applications in spatial statistics. Annals of the Institute of Statistical Mathematics 43, 1–20.
Riebler, A., Sørbye, S. H., Simpson, D. & Rue, H. (2016). An intuitive Bayesian spatial model for disease mapping that accounts for scaling. Statistical Methods in Medical Research 25, 1145–1165.
Vehtari, A., Mononen, T., Tolvanen, V., Sivula, T. & Winther, O. (2016). Bayesian leave-one-out cross-validation approximations for Gaussian latent variable models. Journal of Machine Learning Research 17(103), 1–38.
Vehtari, A., Gelman, A. & Gabry, J. (2017). Practical Bayesian model evaluation using leave-one-out cross-validation and WAIC. Statistics and Computing 27, 1413–1432.
Leroux, B. G., Lei, X. & Breslow, N. (2000). Estimation of disease rates in small areas: a new mixed model for spatial dependence. In Statistical Models in Epidemiology, the Environment, and Clinical Trials, 179–191.
Murray, I., Adams, R. P. & MacKay, D. J. C. (2010). Elliptical slice sampling. AISTATS.
Murray, I. & Adams, R. P. (2010). Slice sampling covariance hyperparameters of latent Gaussian models. NeurIPS.
Nishihara, R., Murray, I. & Adams, R. P. (2014). Parallel MCMC with generalized elliptical slice sampling. JMLR 15, 2087–2112.
Lee, D. (2013). CARBayes: an R package for Bayesian spatial modeling with conditional autoregressive priors. Journal of Statistical Software 55(13).
Morris, M., Wheeler-Martin, K., Simpson, D., Mooney, S. J., Gelman, A. & DiMaggio, C. (2019). Bayesian hierarchical spatial models: implementing the Besag York Mollié model in Stan. Spatial and Spatio-temporal Epidemiology 31, 100301.
Rue, H., Martino, S. & Chopin, N. (2009). Approximate Bayesian inference for latent Gaussian models by using integrated nested Laplace approximations. Journal of the Royal Statistical Society B 71, 319–392.
Andrieu, C. & Thoms, J. (2008). A tutorial on adaptive MCMC. Statistics and Computing 18, 343–373.
