pyINLA · random effects

fbym2

A non-stationary spatial model with noise: a spatial part whose smoothness differs between sub-regions, plus noise of each area.

Idea

The effect of each area is a spatial part, shared with its neighbours, plus noise of its own. The spatial part is an fbesag field, so its smoothness differs between sub-regions. With the same smoothness everywhere it is the BYM2 model (Riebler et al., 2016), with the same priors, so comparing the two tests whether the map is non-stationary. When the sub-regions do not differ, the estimates are almost the same as BYM2's (see the check).

Model

$$\mathbf v = \frac{1}{\sqrt\tau}\Big(\sqrt{1-\phi}\;\mathbf w + \sqrt{\phi}\;\mathbf u\Big), \qquad \mathbf w \sim \mathcal N(\mathbf 0, I),$$
$$\pi(\mathbf u \mid \mathbf d) \propto \exp\Big(-\frac{1}{2} \sum_{i \sim j} \frac{e^{d_{k(i)}} + e^{d_{k(j)}}}{2}\, (u_i - u_j)^2\Big), \qquad \sum_k d_k = 0 .$$
parametermeaning
$\tau$precision of $v_i$
$\phi$spatial share of the variance
$d_k$log precision of the spatial part in sub-region $k$, relative to the map: $d_k > 0$ smoother, $d_k < 0$ rougher

As in bym2, the latent vector is $(\mathbf v, \mathbf u)$, of length $2n$:

$$Q = \begin{pmatrix} \dfrac{\tau}{1-\phi} I & -\dfrac{\sqrt{\phi\tau}}{1-\phi} I \\[1.5ex] -\dfrac{\sqrt{\phi\tau}}{1-\phi} I & \dfrac{\phi}{1-\phi} I + Q_{\text{fb}}(\mathbf d) \end{pmatrix}.$$

Priors

$\boldsymbol\theta = (\log\tau,\ \text{logit}\,\phi,\ d_1, \dots, d_{P-1})$, reported as Theta1, Theta2, …; $d_P = -(d_1 + \dots + d_{P-1})$.

parameterpriordefaultmeaning
$\tau$PCprec=(1, 0.01)$P(1/\sqrt\tau > 1) = 0.01$
$\phi$PC of BYM2phi=(0.5, 0.5)$P(\phi < 0.5) = 0.5$
$\mathbf d$$\mathcal N\big(\mathbf 0, \sigma_\gamma^2 (I - \tfrac1P J)\big)$sd_gamma=0.2as in fbesag; $\sigma_\gamma \to 0$ gives bym2

Example: rough and smooth regions

Simulated on the 558 microregions of Brazil, 5 expected cases each, with $\boldsymbol\theta = (1,\ \text{logit}\,0.8,\ -2,\ -2,\ 2,\ 2)$. Data: fbym2_simulated.csv (true $v_i$ and $y_i$), brazil_558_microregions.csv, brazil_558_microregions.graph, brazil_558_microregions.geojson.

Map of Brazil: North and Northeast rough, Southeast and South smooth, Centre-West in between
The true $d_k$.
import numpy as np
import pandas as pd
from pyinla import pyinla
from pyinla.gmrfs import fbym2

d      = pd.read_csv("fbym2_simulated.csv")   # idx, region_code, true v, y, E
graph  = "brazil_558_microregions.graph"
region = d["region_code"].to_numpy()          # 1 North, 2 Northeast, 3 Southeast, 4 South, 5 Centre-West
m_regions = fbym2("idx", graph, region, sd_gamma=1)

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

fit_regions = fit(m_regions)
fit_bym2    = fit({"id": "idx", "model": "bym2", "graph": graph})
print(fit_regions.summary_hyperpar[["mean", "0.025quant", "0.975quant"]].round(2))
print(fit_regions.mlik.iloc[1] - fit_bym2.mlik.iloc[1])   # log Bayes factor for the regions
                mean  0.025quant  0.975quant
Theta1 for idx  0.87        0.52        1.22
Theta2 for idx  1.70        0.64        2.75
Theta3 for idx -1.68       -2.29       -1.07
Theta4 for idx -1.81       -2.29       -1.33
Theta5 for idx  1.52        0.66        2.39
Theta6 for idx  1.64        0.56        2.72
56.71719603300926

Regional deviations

from pyinla import hyperpar_sample
import matplotlib.pyplot as plt

np.random.seed(1)
th = hyperpar_sample(4000, fit_regions, intern=True).to_numpy()   # log tau, logit phi, d_1, ..., d_4
dk = np.column_stack([th[:, 2:6], -th[:, 2:6].sum(axis=1)])       # d_5 = -(d_1 + ... + d_4)
region_names = ["North", "Northeast", "Southeast", "South", "Centre-West"]
q = pd.DataFrame(np.quantile(dk, [0.025, 0.5, 0.975], axis=0),
                 index=["2.5%", "50%", "97.5%"], columns=region_names)
print(q.round(2))

plt.errorbar(q.loc["50%"], region_names, fmt="o",
             xerr=[q.loc["50%"] - q.loc["2.5%"], q.loc["97.5%"] - q.loc["50%"]])
plt.axvline(0)
       North  Northeast  Southeast  South  Centre-West
2.5%   -2.30      -2.28       0.67   0.58        -0.40
50%    -1.68      -1.81       1.53   1.63         0.33
97.5%  -1.06      -1.32       2.37   2.72         1.11
Posterior medians and 95 percent intervals of d_k for each region, with the true values
$d_k$, median and 95% interval, and the truth.

Estimates

import geopandas as gpd

# error of the posterior mean of v, by region (v: the first 558 entries)
def rmse(fit):
    v_hat = fit.summary_random["idx"]["mean"].to_numpy()[:558]
    return np.sqrt(pd.Series((v_hat - d["v"]) ** 2).groupby(region).mean()).set_axis(region_names)
print(pd.DataFrame({"bym2": rmse(fit_bym2), "fbym2": rmse(fit_regions)}).T.round(2))

map_df = gpd.read_file("brazil_558_microregions.geojson").sort_values("idx").reset_index(drop=True)
map_df["truth"] = d["v"].to_numpy()
map_df["bym2"]  = fit_bym2.summary_random["idx"]["mean"].to_numpy()[:558]
map_df["fbym2"] = fit_regions.summary_random["idx"]["mean"].to_numpy()[:558]
map_df.plot("fbym2", cmap="RdBu_r", vmin=-2.5, vmax=2.5, linewidth=0, legend=True)
       North  Northeast  Southeast  South  Centre-West
bym2    0.72       0.59       0.34   0.35         0.36
fbym2   0.66       0.50       0.25   0.28         0.33
Maps of the true v and the posterior means from bym2 and fbym2
Posterior mean of $v_i$.

The two estimates differ by 0.11 to 0.16 on average, too little to see on this colour scale. With one spatial precision, bym2 smooths the North more than fbym2 and the Southeast and South less; its error is higher in every region.

Repeated: 20 data sets

20 simulated data sets for each truth (with the R package gmrfs): the example above, and no difference between the regions.

RMSE of the posterior mean of v by region for bym2 and fbym2, averaged over 20 simulated data sets
Error of the posterior mean of $v_i$, by region.
truthmodelNorthNortheastSoutheastSouthCentre-West
regions differRMSEbym20.5270.4950.3280.3450.331
fbym20.4850.4420.2500.2580.303
coveragebym20.8860.8890.9770.9730.968
fbym20.9280.9460.9590.9530.937
no difference, all $d_k = 0$RMSEbym20.3160.3200.3200.3010.302
fbym20.3180.3210.3210.3030.306
coveragebym20.9490.9450.9450.9410.957
fbym20.9430.9490.9420.9430.945

When the regions differ, fbym2 has the lower error in 16 to 20 of the 20 data sets in every region, and the log BF favours it in all 20 (38 to 74). When they do not, the two are almost the same (error at most 0.004 higher), and the log BF has median $-2.3$ and is at most 1.4.

Testing for non-stationarity

$$\log \text{BF} = \log \pi(\mathbf y \mid \text{fbym2}) - \log \pi(\mathbf y \mid \text{bym2})$$

If you change the priors of fbym2, give bym2 the same hyper.

Beyond $\text{logit}\,\phi = 12$, the bym2 prior is flat (improper) and fbym2's is the exact PC tail. If the posterior of $\phi$ reaches 1, compare with fbym2 with all id_part equal to 1 instead of bym2.

References