pyINLA · random effects

fbesag

A non-stationary spatial model: neighbours can be more alike in one part of the map than in another.

Idea

Each area is pulled towards its neighbours. How strongly is set by a precision $\tau_k$ that differs between sub-regions: high where the map is smooth, low where it is rough. With one sub-region the precision is the same everywhere (the Besag model), so comparing the two tests whether the map is non-stationary. When the sub-regions do not differ, the estimates are the same as with one precision (see the check).

Sub-regions are chosen before seeing the data. They need not be contiguous, and the graph can be a time line.

Model

$$\pi(\mathbf x \mid \tau_1, \dots, \tau_P) \propto \exp\Big(-\frac{1}{2} \sum_{i \sim j} w_{ij}\, (x_i - x_j)^2\Big), \qquad w_{ij} = \frac{\tau_{k(i)} + \tau_{k(j)}}{2},$$
$$x_i \mid \mathbf x_{-i} \sim \mathcal N\Big(\frac{\sum_{j \sim i} w_{ij}\, x_j}{\sum_{j \sim i} w_{ij}},\ \frac{1}{\sum_{j \sim i} w_{ij}}\Big),$$

where $k(i)$ is the sub-region of area $i$. The structure is scaled so that, with all $\tau_k = 1$, the geometric mean of the marginal variances is 1 (Sørbye and Rue, 2014), with a sum-to-zero constraint per connected part of two or more areas. pyinla reports $\theta_k = \log\tau_k$ as Theta1, …, ThetaP.

Prior

$$\theta_k = \bar\theta + \gamma_k, \qquad \pi(\boldsymbol\theta) \propto \exp\Big(-\lambda e^{-\bar\theta/2} - \frac{\bar\theta}{2}\Big)\, \exp\Big(-\frac{1}{2\sigma_\gamma^2} \sum_k \gamma_k^2\Big), \qquad \sum_k \gamma_k = 0 .$$

A PC prior (Simpson et al., 2017) on the common level $\bar\theta$, and a normal prior on the deviations $\gamma_k$. As $\sigma_\gamma \to 0$, all $\tau_k$ become equal.

argumentdefaultmeaning
param{'p1': 1, 'p2': 1e-5}$P(\text{common sd} > 1) = 10^{-5}$
sd_gamma0.2with five sub-regions, each $\tau_k$ within a factor of about 1.4 of the common level (two sd); with 1, about 6

Example: dengue in Brazil

Dengue cases in April in the 558 microregions of Brazil (Lowe et al., 2021), with the five regions as sub-regions. Data: brazil_558_microregions.csv, brazil_558_microregions.graph, brazil_558_microregions.geojson.

Map of the 558 microregions of Brazil coloured by region
The five regions.
Map of the ratio of observed to expected dengue cases in April
Observed / expected, April.
import numpy as np
import pandas as pd
from pyinla import pyinla
from pyinla.gmrfs import fbesag

areas  = pd.read_csv("brazil_558_microregions.csv")   # idx, region, Y and E of April
graph  = "brazil_558_microregions.graph"
region = areas["region_code"].to_numpy()             # 1 North, 2 Northeast, 3 Southeast, 4 South, 5 Centre-West

m_besag   = fbesag("idx", graph, np.ones(558, int))  # one precision: besag
m_regions = fbesag("idx", graph, region)             # one precision per region

def fit(m):
    return pyinla(model={"response": "Y", "fixed": ["1"], "random": [m]},
                  family="poisson", data=areas, E=areas["E"].to_numpy())

fit_besag, fit_regions = fit(m_besag), fit(m_regions)
print(fit_regions.summary_hyperpar[["mean", "0.025quant", "0.975quant"]].round(2))
print(fit_regions.mlik.iloc[1] - fit_besag.mlik.iloc[1])   # log Bayes factor for the regions
                mean  0.025quant  0.975quant
Theta1 for idx -1.06       -1.38       -0.75
Theta2 for idx -1.08       -1.28       -0.88
Theta3 for idx -1.66       -1.87       -1.46
Theta4 for idx -1.12       -1.47       -0.77
Theta5 for idx -1.02       -1.35       -0.70
10.60895681735974
Posterior means and 95 percent intervals of the log precision of each region in April, with the single besag value
$\theta_k$ with 95% intervals. Grey: besag.

The Southeast is rougher than the other regions.

Precision of each area

$$\tau_i = \frac{n_i\, \tau_{k(i)} + \sum_{j \sim i} \tau_{k(j)}}{2\, n_i}$$
import geopandas as gpd

tau   = np.exp(fit_regions.summary_hyperpar["mean"].to_numpy())[region - 1]   # tau of each area's region
lines = open(graph).read().split("\n")[1:559]
nbs   = [np.array(line.split()[2:], dtype=int) - 1 for line in lines]          # the neighbours, 0-based
n_i   = np.array([len(nb) for nb in nbs])

map_df = gpd.read_file("brazil_558_microregions.geojson").sort_values("idx").reset_index(drop=True)
map_df["log_tau"] = np.log((n_i * tau + np.array([tau[nb].sum() for nb in nbs])) / (2 * n_i))
map_df.plot("log_tau", linewidth=0, legend=True)
Map of the log precision of each microregion in April
$\log\tau_i$. It changes over a band of areas at the borders of the Southeast.

Maps

from matplotlib.colors import ListedColormap

x_regions = fit_regions.summary_random["idx"]["mean"].to_numpy()
x_besag   = fit_besag.summary_random["idx"]["mean"].to_numpy()
# observed over expected, relative to the whole country
map_df["smr"]  = (areas["Y"] / areas["E"]) / (areas["Y"].sum() / areas["E"].sum())
map_df["rr"]   = np.exp(x_regions)
map_df["diff"] = x_regions - x_besag

brks = [0, 0.2, 0.5, 1, 2, 5, np.inf]
cols = ListedColormap(["#2c7bb6", "#abd9e9", "#e0f3f8", "#fee090", "#fdae61", "#d7191c"])
map_df.assign(rr=pd.cut(map_df["rr"], brks, right=False)).plot("rr", cmap=cols, linewidth=0, legend=True)
lim = map_df["diff"].abs().max()
map_df.plot("diff", cmap="RdBu_r", vmin=-lim, vmax=lim, linewidth=0, legend=True)

# the largest difference in each region
region_names = ["North", "Northeast", "Southeast", "South", "Centre-West"]
print(map_df["diff"].abs().groupby(region).max().set_axis(region_names).round(2).to_string())
North          0.22
Northeast      0.18
Southeast      0.31
South          0.29
Centre-West    0.13
Map of the fbesag posterior relative risk of dengue in April
fbesag: $\exp$ of the posterior mean of $x_i$.
Map of the difference between the fbesag and besag posterior means of the spatial effect
fbesag minus besag, posterior mean of $x_i$.

Testing for non-stationarity

$$\log \text{BF} = \log \pi(\mathbf y \mid \text{fbesag, } P \text{ sub-regions}) - \log \pi(\mathbf y \mid \text{fbesag, one})$$

Compare with fbesag with one sub-region, not besag, whose default prior is different.

$\sigma_\gamma = 0.2$$\sigma_\gamma = 1$
log BF, April10.610.4

fbesag has no noise term, so a rough region can also be noise. fbym2, which has one, also finds the Southeast rougher in April ($d = -0.48$, 95% interval $-0.69$ to $-0.26$; log BF 10.0).

Simulated check

20 data sets per setting on the same map, with the expected counts of April (simulated and fitted with the R package gmrfs, which gives the same fits):

truth$\sigma_\gamma$log BF, median (range)log BF > 0$\theta$ Southeast, meaninterval covers truth
stationary, all $-1.3$0.2−0.9 (−1.7 to 0.6)4 of 20−1.2919 of 20
1−5.8 (−7.0 to −2.8)0 of 20−1.3119 of 20
Southeast $-1.8$, others $-1.0$0.213.1 (3.9 to 19.3)20 of 20−1.608 of 20
113.4 (0.7 to 21.1)20 of 20−1.7517 of 20

The default $\sigma_\gamma$ pulls the Southeast towards the others; $\sigma_\gamma = 1$ recovers it.

With no difference between the regions (stationary truth), the estimates and intervals of $x_i$ are the same as with one precision:

modelRMSE, SoutheastRMSE, other regionscoverage, Southeastcoverage, other regions
one precision0.1700.2040.9470.949
fbesag, $\sigma_\gamma = 0.2$0.1700.2030.9480.949
fbesag, $\sigma_\gamma = 1$0.1700.2030.9490.949

References