If our data are generated by an AR(1) process, e.g. \[y_{t} = \phi y_{t-1} + e_t, ~ e_t \sim \mathcal{N}(0,1), ~ |\phi|<1\] then all values \(y_t,t=1,...n\) will be correlated, however the distribution of \(y_t\) given all earlier observations \[y_t|y_{t-1},...,y_1\] reduces to \(\mathcal{N}(\phi y_{t-1},1)\) as we have conditional independence. Although \(\Sigma=Cov(y)\) is dense (has no zero entries), its inverse, the so-called precision matrix\(Q=\Sigma^{-1}\) is highly sparse (tri-diagonal, see Rue and Held (2005) p 2). This make any evaluation that contains \(\Sigma^{-1}\) extremely cheap, compared to the general case.
In an AR(1) time series, every observation apart from the first and last has two direct neighbours, the observation before and after, given which all other observations are independent. In spatial problems, we can often form neighbours, in various ways:
for irregular, point observations (geostatistical data) one could “define” all observations beyond a certain distance threshold as independent, or alternatively consider the geometric layout by forming the Delauney triangulation and consider points not connected by an edge as independent
for polygon data, a common approach is to consider all polygons that intersect with the polygon at hand as neighbours, and consider all other polygons as independent, conditional on the neighbours
8.1 A CAR model on polygon data
(this section was mostly written by the DeepSeek-V4-Flash LLM; the prompt was “add an example R code chunk demonstrating a CAR model to the GMRF chapter”)
We illustrate this with the German Landkreis polygons used earlier. For each polygon we define its neighbours as the polygons it shares a border with (contiguity). With \(W\) the binary adjacency matrix (\(W_{ij}=1\) if \(i\) and \(j\) are neighbours), the graph Laplacian \(Q = D - W\) (with \(D=\mathrm{diag}(W 1)\)) is the precision matrix of the intrinsic Besag (CAR) model (Besag et al. (1991)): it is sparse, with non-zero entries only for neighbours, and the conditional distribution of the effect at region \(i\) only depends on its neighbours. This is the spatial analogue of the AR(1) precision matrix from the introduction.
library(sf)
Linking to GEOS 3.14.1, GDAL 3.12.2, PROJ 9.7.1; sf_use_s2() is TRUE
library(spdep)
Loading required package: spData
library(dplyr)
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
library(INLA)
Loading required package: Matrix
# polygons and their neighbourslk =read_sf("data/411_landkreis_rsurvstat.gpkg")nb =poly2nb(lk)W =nb2mat(nb, style ="B", zero.policy =TRUE)# the graph Laplacian is the precision matrix Q = D - WD =diag(rowSums(W))Q = D - Wcat("regions:", nrow(W), " neighbours per region: median", median(card(nb)), "\n")
regions: 411 neighbours per region: median 5
cat("density of W:", round(sum(W !=0)/length(W), 4), "\n")
density of W: 0.0126
cat("class of Q:", class(Matrix::Matrix(Q, sparse =TRUE))[1], "\n")
class of Q: dsCMatrix
We aggregate the weekly Covid-19 case counts per Landkreis for a single year, and model them as Poisson counts with a population offset and a spatially structured random effect.
The number of cases \(y_i\) is not directly comparable between regions, because the regions have very different population sizes: a large city will naturally have more cases than a small rural county. We therefore model the rate rather than the raw count. The offset \(E_i\) is the expected count if the rate were 1 (i.e. one case per person per time unit) — here we take \(E_i\) equal to the population of the region. In INLA, passing E = dat$E means the model is
c19 = readr::read_csv("data/Covid.csv.zip", show_col_types =FALSE)c19a = c19 |>filter(lubridate::year(Meldedatum) ==2025) |>group_by(Landkreis_id) |>summarise(total_cases =sum(Faelle_neu),Bevoelkerung =first(Bevoelkerung), .groups ="drop")lk = lk |>left_join(c19a, by ="Landkreis_id")# graph object expected by INLA, built from the adjacency matrixg =inla.matrix2graph(W)dat =data.frame(y = lk$total_cases,E = lk$Bevoelkerung, # offset: expected count at rate 1id =seq_len(nrow(lk)))fit =inla(y ~1+f(id, model ="bym", graph = g, scale.model =TRUE),data = dat, family ="poisson", E = dat$E,control.inla =list(int.strategy ="eb"),control.compute =list(dic =TRUE, waic =TRUE),verbose =FALSE)summary(fit)
Time used:
Pre = 0.301, Running = 1.34, Post = 0.0266, Total = 1.66
Fixed effects:
mean sd 0.025quant 0.5quant 0.975quant mode kld
(Intercept) -6.441 0.013 -6.467 -6.441 -6.416 -6.441 0
Random effects:
Name Model
id BYM model
Model hyperparameters:
mean sd 0.025quant 0.5quant 0.975quant
Precision for id (iid component) 15.87 2.68 11.29 15.63 21.82
Precision for id (spatial component) 8.96 1.76 5.95 8.81 12.85
mode
Precision for id (iid component) 15.13
Precision for id (spatial component) 8.54
Deviance Information Criterion (DIC) ...............: 3856.71
Deviance Information Criterion (DIC, saturated) ....: 813.03
Effective number of parameters .....................: 395.85
Watanabe-Akaike information criterion (WAIC) ...: 3757.04
Effective number of parameters .................: 212.77
Marginal log-Likelihood: -2256.38
is computed
Posterior summaries for the linear predictor and the fitted values are computed
(Posterior marginals needs also 'control.compute=list(return.marginals.predictor=TRUE)')
To see what the offset does, compare this with a model that ignores population and models the raw case counts directly. Without the offset, the intercept is on the raw-count scale, and the spatial effect then absorbs differences in population size, so it can no longer be interpreted as a rate:
# offset-free model: models raw counts, no population correctionfit_n =inla(y ~1+f(id, model ="bym", graph = g, scale.model =TRUE),data = dat, family ="poisson",control.inla =list(int.strategy ="eb"),control.compute =list(dic =TRUE, waic =TRUE),verbose =FALSE)summary(fit_n)
Time used:
Pre = 0.153, Running = 1.08, Post = 0.0158, Total = 1.25
Fixed effects:
mean sd 0.025quant 0.5quant 0.975quant mode kld
(Intercept) 5.569 0.022 5.526 5.569 5.612 5.569 0
Random effects:
Name Model
id BYM model
Model hyperparameters:
mean sd 0.025quant 0.5quant 0.975quant
Precision for id (iid component) 5.25 0.992 3.59 5.15 7.48
Precision for id (spatial component) 2.71 0.571 1.75 2.66 3.99
mode
Precision for id (iid component) 4.95
Precision for id (spatial component) 2.56
Deviance Information Criterion (DIC) ...............: 3862.33
Deviance Information Criterion (DIC, saturated) ....: 818.65
Effective number of parameters .....................: 406.27
Watanabe-Akaike information criterion (WAIC) ...: 3748.02
Effective number of parameters .................: 209.69
Marginal log-Likelihood: -2486.33
is computed
Posterior summaries for the linear predictor and the fitted values are computed
(Posterior marginals needs also 'control.compute=list(return.marginals.predictor=TRUE)')
The two models can be compared with DIC or WAIC. Here the offset model (DIC 3856.7, WAIC 3757) and the offset-free model (DIC 3862.3, WAIC 3748) fit similarly, but only the offset model gives an interpretable incidence rate.
The BYM random effect for region \(i\) is the sum of the structured and unstructured parts, \(u_i + v_i\), stored in the first \(n\) rows of summary.random$id. Adding the intercept and exponentiating gives the fitted incidence rate per person; we multiply by \(10^5\) to express it per 100,000 inhabitants, the convention used for German Covid-19 incidence:
# total BYM effect per region (u + v): first n rows of summary.random$ideff = fit$summary.random$id[seq_len(nrow(lk)), "mean"]# fitted incidence rate per 100,000 inhabitantsintercept = fit$summary.fixed[1, "mean"]lk$rate =exp(intercept + eff) *1e5# observed empirical rate, for comparisonlk$empirical = lk$total_cases / lk$Bevoelkerung *1e5library(ggplot2)# combine both into one faceted map with a shared colour scalelk2 =rbind( lk |>mutate(kind ="Fitted (BYM)", value = rate) |>select(kind, value), lk |>mutate(kind ="Observed", value = empirical) |>select(kind, value))ggplot(lk2) +geom_sf(aes(fill = value), linewidth =0.1) +scale_fill_viridis_c(limits =range(c(lk$rate, lk$empirical))) +facet_wrap(~ kind) +theme_void() +labs(title ="Incidence rate (cases per 100,000)")
The two maps should look similar, because the Poisson mean is \(E_i \exp(\eta_i)\): the BYM model smooths the empirical rates and borrows strength across neighbouring regions, so the fitted map is a regularised version of the observed one.
The agreement can be inspected more directly by plotting the fitted rate against the observed rate for each region. The dashed line is the 1:1 line; points below it are regions where the model shrinks the rate toward the global mean (e.g. small-population regions with noisy rates), and points above it where it smooths extreme values up:
library(ggplot2)ggplot(lk, aes(x = empirical, y = rate)) +geom_abline(slope =1, intercept =0, linetype =2, colour ="grey40") +geom_point(alpha =0.6) +coord_equal() +labs(x ="Observed rate (cases per 100,000)",y ="Fitted rate (cases per 100,000)",title ="Fitted vs observed incidence rate")
NoteExercise
Why is the precision matrix \(Q = D - W\) sparse, and how does that make the CAR model cheap to compute compared to a model with a dense covariance matrix \(\Sigma\)?
8.2 Adding a predictor: socioeconomic deprivation
The German Index of Socioeconomic Deprivation (GISD) of the Robert Koch Institute summarises regional disadvantage in education, employment and income. We use its values for 2021 and therefore switch to the Covid-19 cases of 2021.
GISD reports Berlin as a single district, whereas the RKI case data and our polygons split it into its 12 Bezirke (IDs 11001–11012). We merge these into Berlin (11000), which gives the 400 official districts.
gisd_2021 = readr::read_csv("data/GISD_Release_2025_v1/Bund/GISD_Bund_Kreis.csv",show_col_types =FALSE) |>filter(year ==2021) |>select(Landkreis_id = kreis_id, gisd_score)# Berlin Bezirke -> Berlinberlin_ids =11001:11012to_kreis =function(id) if_else(as.integer(id) %in% berlin_ids, "11000", id)# cases and population: first per Kreis/Bezirk, then Bezirke summed to Berlinc19_400 = c19 |>filter(lubridate::year(Meldedatum) ==2021) |>group_by(Landkreis_id) |>summarise(total_cases =sum(Faelle_neu), Bevoelkerung =first(Bevoelkerung)) |>group_by(Landkreis_id =to_kreis(Landkreis_id)) |>summarise(total_cases =sum(total_cases), Bevoelkerung =sum(Bevoelkerung))# polygons: union the 12 Berlin Bezirke, then join cases and GISDis_berlin =as.integer(lk$Landkreis_id) %in% berlin_idsberlin = lk[is_berlin, ] |>summarise(Landkreis_id ="11000", Name ="Berlin") |>st_cast("MULTIPOLYGON")lk_400 =rbind(lk[!is_berlin, c("Landkreis_id", "Name")], berlin) |>left_join(c19_400, by ="Landkreis_id") |>left_join(gisd_2021, by ="Landkreis_id")nrow(lk_400) # 400
[1] 400
We fit three models to these districts: the BYM model without and with GISD, and for comparison a model with GISD but only an unstructured (iid) random effect.
In the BYM model, GISD has a clearly positive effect: the most deprived district (GISD = 1) is expected to have about 2.2 times the incidence of the least deprived one (GISD = 0). In the iid model, however, the 95% credible interval includes zero.
To see why, we map the BYM effect \(u_i + v_i\) of both models together with the GISD contribution \(\beta \cdot \text{GISD}_i\) (centred, as its mean is absorbed by the intercept). The left map is roughly the sum of the other two.
n =nrow(lk_400)b = fit_gisd$summary.fixed["gisd", "mean"]lk_400$re_bym = fit_bym$summary.random$id$mean[1:n]lk_400$gisd_eff = b * (lk_400$gisd_score -mean(lk_400$gisd_score))lk_400$re_gisd = fit_gisd$summary.random$id$mean[1:n]lk_re =rbind( lk_400 |>mutate(kind ="Spatial effect, BYM", value = re_bym) |>select(kind, value), lk_400 |>mutate(kind ="GISD effect", value = gisd_eff) |>select(kind, value), lk_400 |>mutate(kind ="Spatial effect, BYM + GISD", value = re_gisd) |>select(kind, value))lk_re$kind =factor(lk_re$kind, levels =unique(lk_re$kind))ggplot(lk_re) +geom_sf(aes(fill = value), linewidth =0.1) +scale_fill_gradient2(low ="#2166ac", high ="#b2182b") +facet_wrap(~ kind) +theme_void() +labs(title ="Effects on the log incidence rate", fill ="log rate ratio")
The spatial effect hardly changes when GISD is added. The dominant pattern of 2021, with high incidence in Saxony, Thuringia and south-east Bavaria and low incidence in the north-west, is not related to deprivation, which is why the iid model finds no association. In the BYM model, the CAR effect absorbs this smooth large-scale pattern, so the GISD coefficient is estimated from differences between neighbouring districts: within a neighbourhood, the more deprived district tends to have the higher incidence. This dependence of a fixed effect on the spatial random effect is known as spatial confounding(Hodges and Reich 2010). The GISD coefficient of the BYM model should therefore be read as a local rather than a national association.
NoteExercise
Which other district characteristics could explain the large-scale pattern in the spatial effect? Add one as a second predictor and check whether the spatial effect becomes flatter.
Besag, Julian, Jeremy York, and Annie Mollié. 1991. “Bayesian Image Restoration, with Two Applications in Spatial Statistics.”Annals of the Institute of Statistical Mathematics 43 (1): 1–20.
Hodges, James S, and Brian J Reich. 2010. “Adding Spatially-Correlated Errors Can Mess up the Fixed Effect You Love.”The American Statistician 64 (4): 325–34.
Rue, Havard, and Leonhard Held. 2005. Gaussian Markov Random Fields: Theory and Applications. Chapman; Hall/CRC.