Cross-Sectional Model Comparison: neighbayes vs spreg

Columbus example

This notebook compares equivalent cross-sectional models between neighbayes and spreg on a real example dataset from the PySAL/spreg ecosystem.

The dependent variable is HOVAL, with INC and CRIME used as explanatory variables.

Model mapping used:

  • SLX (neighbayes) vs spreg.OLS(slx_lags=1)

  • SAR (neighbayes) vs spreg.GM_Lag

  • SEM (neighbayes) vs spreg.GM_Error

  • SDM (neighbayes) vs spreg.GM_Lag(slx_lags=1, w_lags=2)

  • SDEM (neighbayes) vs spreg.GM_Error(slx_lags=1)

Dataset: Columbus neighborhood data from libpysal.examples.

import geopandas as gpd
import libpysal
import numpy as np
import pandas as pd
import spreg
from IPython.display import display
from spreg.sputils import _sp_effects, spmultiplier

from neighbayes.models import SAR, SDEM, SDM, SEM, SLX
columbus = gpd.read_file(libpysal.examples.get_path("columbus.shp"))
columbus.plot("HOVAL", scheme="quantiles")
<Axes: xlabel='x', ylabel='y'>
../_images/87eb021619432ca111284f01552906e6d333adaa725f06820e89fe83bac34e71.png
w = libpysal.graph.Graph.build_contiguity(columbus).transform("r")
y = columbus.HOVAL.values
X = columbus[["INC", "CRIME"]]

spreg_results = {
    "SLX": spreg.OLS(y, X, w=w, slx_lags=1, name_y="HOVAL", name_x=["INC", "CRIME"]),
    "SAR": spreg.ML_Lag(y, X, w=w, name_y="HOVAL", name_x=["INC", "CRIME"]),
    "SEM": spreg.ML_Error(y, X, w=w, name_y="HOVAL", name_x=["INC", "CRIME"]),
    "SDM": spreg.ML_Lag(
        y, X, w=w, slx_lags=1, w_lags=2, name_y="HOVAL", name_x=["INC", "CRIME"]
    ),
    "SDEM": spreg.GM_Error(
        y, X, w=w, slx_lags=1, name_y="HOVAL", name_x=["INC", "CRIME"]
    ),
}

spec = "HOVAL ~  1 + INC + CRIME"
sample_kwargs = dict(draws=1000, tune=1000, chains=4, random_seed=42, progressbar=False)
bayes_models = {
    "SLX": SLX(spec, data=columbus, W=w),
    "SAR": SAR(spec, data=columbus, W=w),
    "SEM": SEM(spec, data=columbus, W=w),
    "SDM": SDM(spec, data=columbus, W=w),
    "SDEM": SDEM(spec, data=columbus, W=w),
}
bayes_idata = {name: model.fit(**sample_kwargs) for name, model in bayes_models.items()}
ML_Lag
ML_Error
ML_Lag
GM_Error
/home/runner/micromamba/envs/test/lib/python3.14/site-packages/spreg/diagnostics.py:620: ComplexWarning: Casting complex values to real discards the imaginary part
  ci_result = sqrt(max_eigval / min_eigval)
/home/runner/micromamba/envs/test/lib/python3.14/site-packages/spreg/ml_error.py:184: RuntimeWarning: Method 'bounded' does not support relative tolerance in x; defaulting to absolute tolerance.
  res = minimize_scalar(
Initializing NUTS using jitter+adapt_diag...
Multiprocess sampling (4 chains in 2 jobs)
NUTS: [beta, sigma2]
Sampling 4 chains for 1_000 tune and 1_000 draw iterations (4_000 + 4_000 draws total) took 12 seconds.
bayes_models["SAR"].summary()
mean sd eti89_lb eti89_ub ess_bulk ess_tail r_hat mcse_mean mcse_sd
rho 0.216 0.163 -0.053 0.48 3826 2817 1.00 0.0027 0.0025
sigma 15.02 1.54 13 18 3462 3732 1.00 0.026 0.02
sigma2 228 48 160 310 3462 3732 1.00 0.81 0.74
Intercept 37.746332 13.970024 15.082874 59.44563 3807.210162 3461.829975 1.00199 0.226666 0.165988
INC 0.572275 0.527626 -0.239062 1.436265 3768.085045 3904.523227 0.999947 0.008605 0.006025
CRIME -0.452957 0.177478 -0.731685 -0.169956 3855.789234 3636.886571 1.000372 0.002859 0.002009

Sampler adequacy for \(\rho\)

Before trusting the SAR posterior summary above, verify that the spatial parameter \(\rho\) has been sampled efficiently. Wolf, Anselin & Arribas-Bel (2018) [Wolf et al., 2018] show that \(\rho\) tends to mix slowly, so a chain that converges in mean can still under-cover the tails by 10–12 %.

from neighbayes.diagnostics import spatial_mcmc_diagnostic

spatial_mcmc_diagnostic(bayes_models["SAR"], emit_warnings=False).to_frame()
ess_bulk ess_tail r_hat mcse_mean yield_pct hpdi_drift_pct adequate
parameter
rho 3826.525331 2817.417701 1.000484 0.002652 95.663133 2.279794 True
print(spreg_results["SAR"].summary)
REGRESSION RESULTS
------------------

SUMMARY OF OUTPUT: MAXIMUM LIKELIHOOD SPATIAL LAG (METHOD = FULL)
------------------------------------------------------------------------------------
Data set            :     unknown
Weights matrix      :     unknown
Dependent Variable  :       HOVAL                Number of Observations:          49
Mean dependent var  :     38.4362                Number of Variables   :           4
S.D. dependent var  :     18.4661                Degrees of Freedom    :          45
Pseudo R-squared    :      0.3826
Spatial Pseudo R-squared:  0.3271
Log likelihood      :   -200.4394
Sigma-square ML     :    206.3220                Akaike info criterion :     408.879
S.E of regression   :     14.3639                Schwarz criterion     :     416.446

------------------------------------------------------------------------------------
            Variable     Coefficient       Std.Error     z-Statistic     Probability
------------------------------------------------------------------------------------
            CONSTANT        37.26813        13.94565         2.67238         0.00753
                 INC         0.56316         0.51316         1.09742         0.27246
               CRIME        -0.45102         0.17770        -2.53815         0.01114
             W_HOVAL         0.23063         0.15496         1.48836         0.13666
------------------------------------------------------------------------------------

SPATIAL LAG MODEL IMPACTS
Impacts computed using the 'simple' method.
            Variable         Direct        Indirect          Total
                 INC         0.5632          0.1688          0.7320
               CRIME        -0.4510         -0.1352         -0.5862
================================ END OF REPORT =====================================
def extract_spreg_params(model):
    names = list(getattr(model, "name_x", []))
    betas = np.asarray(model.betas).flatten()
    if len(betas) == len(names) + 1 and type(model).__name__ == "GM_Lag":
        names = names + ["rho"]
    if len(names) != len(betas):
        names = [f"param_{i}" for i in range(len(betas))]
    return pd.Series(betas, index=names, dtype=float)


def extract_neighbayes_params(model_name, model, idata):
    out = {}
    beta_mean = idata.posterior["beta"].mean(("chain", "draw")).values
    beta_names = (
        model._beta_names()
        if model_name in {"SLX", "SDM", "SDEM"}
        else list(model._feature_names)
    )
    for name, val in zip(beta_names, beta_mean):
        out[name] = float(val)
    if model_name in {"SAR", "SDM"}:
        out["rho"] = float(idata.posterior["rho"].mean())
    if model_name in {"SEM", "SDEM"}:
        out["lambda"] = float(idata.posterior["lam"].mean())
    harmonized = {}
    for k, v in out.items():
        key = k.replace("W*", "W_")
        if key.lower() == "intercept":
            key = "CONSTANT"
        harmonized[key] = v
    return pd.Series(harmonized, dtype=float)


def bayes_effects_df(model):
    eff = model.impacts()
    # impacts() returns a DataFrame indexed by feature names
    return pd.DataFrame(
        {
            "feature": eff.index.tolist(),
            "direct_neighbayes": eff["direct"].values,
            "indirect_neighbayes": eff["indirect"].values,
            "total_neighbayes": eff["total"].values,
        }
    )


def spreg_effects_df(model_name, sp_model):
    if model_name == "SEM":
        # SEM has no spatial multiplier on the systematic component,
        # so direct = coefficient, indirect = 0, total = coefficient.
        # neighbayes excludes the intercept from effects, so we
        # only compare non-constant covariates here.
        sp_params = extract_spreg_params(sp_model)
        features = [f for f in ["INC", "CRIME"] if f in sp_params.index]
        return pd.DataFrame(
            {
                "feature": features,
                "direct_spreg": [float(sp_params[f]) for f in features],
                "indirect_spreg": [0.0 for _ in features],
                "total_spreg": [float(sp_params[f]) for f in features],
            }
        )

    output = sp_model.output.copy()
    output["regime"] = "global"

    def classify_var(var_name):
        if var_name == "CONSTANT":
            return "o"
        if var_name in {"lambda", "W_HOVAL"}:
            return "rho"
        if var_name.startswith("W_"):
            return "wx"
        return "x"

    output["var_type"] = output["var_names"].map(classify_var)
    sp_model.output = output

    vars_x = output.query("var_type == 'x'")
    rho = (
        float(np.squeeze(getattr(sp_model, "rho", 0.0)))
        if hasattr(sp_model, "rho")
        else 0.0
    )
    multipliers = spmultiplier(w, rho)
    slx_lags = getattr(sp_model, "slx_lags", 0) or (
        1 if model_name in {"SLX", "SDEM"} else 0
    )
    slx_vars = getattr(sp_model, "slx_vars", None)
    if slx_vars is None:
        slx_vars = "All"

    total_spreg, direct_spreg, indirect_spreg = _sp_effects(
        sp_model,
        vars_x,
        multipliers,
        slx_lags=slx_lags,
        slx_vars=slx_vars,
    )

    return pd.DataFrame(
        {
            "feature": vars_x["var_names"].tolist(),
            "direct_spreg": np.asarray(direct_spreg).flatten(),
            "indirect_spreg": np.asarray(indirect_spreg).flatten(),
            "total_spreg": np.asarray(total_spreg).flatten(),
        }
    )


coef_rows = []
effect_tables = {}

for model_name in ["SLX", "SAR", "SEM", "SDM", "SDEM"]:
    sp = extract_spreg_params(spreg_results[model_name])
    by = extract_neighbayes_params(
        model_name, bayes_models[model_name], bayes_idata[model_name]
    )

    common = sorted(set(sp.index).intersection(set(by.index)))
    for param in common:
        coef_rows.append(
            {
                "model": model_name,
                "parameter": param,
                "neighbayes_posterior_mean": by[param],
                "spreg_estimate": sp[param],
                "difference": by[param] - sp[param],
            }
        )

    beff = bayes_effects_df(bayes_models[model_name]).copy()
    seff = spreg_effects_df(model_name, spreg_results[model_name]).copy()
    merged = beff.merge(seff, on="feature", how="inner")
    if not merged.empty:
        merged["direct_difference"] = (
            merged["direct_neighbayes"] - merged["direct_spreg"]
        )
        merged["indirect_difference"] = (
            merged["indirect_neighbayes"] - merged["indirect_spreg"]
        )
        merged["total_difference"] = merged["total_neighbayes"] - merged["total_spreg"]
        merged.insert(0, "model", model_name)
    effect_tables[model_name] = merged

comparison = (
    pd.DataFrame(coef_rows).sort_values(["model", "parameter"]).reset_index(drop=True)
)
impacts_comparison = pd.concat(
    [tbl for tbl in effect_tables.values() if not tbl.empty],
    ignore_index=True,
)

Coefficient comparison

display(comparison)
model parameter neighbayes_posterior_mean spreg_estimate difference
0 SAR CONSTANT 37.746332 37.268135 0.478197
1 SAR CRIME -0.452957 -0.451020 -0.001937
2 SAR INC 0.572275 0.563156 0.009119
3 SDEM CONSTANT 32.094234 29.460538 2.633696
4 SDEM CRIME -0.582942 -0.575430 -0.007512
5 SDEM INC 0.832056 0.820920 0.011136
6 SDEM W_CRIME 0.244207 0.300693 -0.056486
7 SDEM W_INC 0.446880 0.478086 -0.031206
8 SDEM lambda 0.424356 0.358330 0.066026
9 SDM CONSTANT 23.607108 17.045361 6.561747
10 SDM CRIME -0.608804 -0.608430 -0.000375
11 SDM INC 0.761806 0.803686 -0.041880
12 SDM W_CRIME 0.399984 0.475451 -0.075468
13 SDM W_INC -0.066965 0.042213 -0.109178
14 SEM CONSTANT 47.220736 48.008252 -0.787516
15 SEM CRIME -0.555613 -0.559458 0.003845
16 SEM INC 0.756577 0.711532 0.045045
17 SEM lambda 0.420059 0.390499 0.029560
18 SLX CONSTANT 30.727055 27.727885 2.999170
19 SLX CRIME -0.592507 -0.590675 -0.001831
20 SLX INC 0.709345 0.742442 -0.033097
21 SLX W_CRIME 0.352868 0.390156 -0.037288
22 SLX W_INC 0.406128 0.486179 -0.080051

Direct/Indirect/Total effects comparison

display(impacts_comparison)
model feature direct_neighbayes indirect_neighbayes total_neighbayes direct_spreg indirect_spreg total_spreg direct_difference indirect_difference total_difference
0 SLX INC 0.709345 0.406128 1.115473 0.742442 0.486179 1.228621 -0.033097 -0.080051 -0.113148
1 SLX CRIME -0.592507 0.352868 -0.239639 -0.590675 0.390156 -0.200519 -0.001831 -0.037288 -0.039120
2 SAR INC 0.583379 0.160840 0.744219 0.563156 0.168814 0.731970 0.020224 -0.007975 0.012249
3 SAR CRIME -0.462567 -0.137754 -0.600321 -0.451020 -0.135200 -0.586219 -0.011547 -0.002555 -0.014102
4 SEM INC 0.756577 0.000000 0.756577 0.711532 0.000000 0.711532 0.045045 0.000000 0.045045
5 SEM CRIME -0.555613 0.000000 -0.555613 -0.559458 0.000000 -0.559458 0.003845 0.000000 0.003845
6 SDM INC 0.771364 0.214341 0.985705 0.803686 0.523510 1.327196 -0.032322 -0.309169 -0.341491
7 SDM CRIME -0.593718 0.269749 -0.323969 -0.608430 0.399790 -0.208640 0.014712 -0.130040 -0.115328
8 SDEM INC 0.832056 0.446880 1.278936 0.820920 0.478086 1.299005 0.011136 -0.031206 -0.020069
9 SDEM CRIME -0.582942 0.244207 -0.338735 -0.575430 0.300693 -0.274737 -0.007512 -0.056486 -0.063998

neighbayes uses Bayesian inference while spreg reports frequentist point estimates, so exact equality is not expected. The coefficient and effects tables are intended to verify directional and numerical agreement under matched model formulas with HOVAL as the dependent variable.

For effects, this notebook now uses spreg’s own spatial-impact utilities: spreg.sputils.spmultiplier and spreg.sputils._sp_effects. SEM is the one exception, since it has no spatial multiplier on the systematic component and therefore uses direct = coefficient, indirect = 0, total = coefficient.

Note on intercept exclusion: neighbayes excludes the intercept from impacts for all model types, since the intercept has no meaningful spatial impact interpretation. This is consistent with spreg’s _sp_effects, which also excludes the constant (CONSTANT) from its impact calculations.

Example: Synthetic Data on a 30x30 Grid

This example demonstrates how to generate synthetic spatial data on a 30x30 regular grid, construct a spatial weights matrix, and fit a cross-sectional spatial regression model using the neighbayes package.

from neighbayes import dgp
from neighbayes.models import SAR

# Set random seed for reproducibility
np.random.seed(42)

# True parameters
beta_true = np.array([1.0, -1.0])
rho_true = 0.5
sigma_true = 1.0

gdf = dgp.simulate_sar(
    n_side=40,
    beta=beta_true,
    rho=rho_true,
    sigma=sigma_true,
    create_gdf=True,
    contiguity="queen",
)

w = libpysal.graph.Graph.build_contiguity(gdf, rook=False).transform("r")

# Fit SAR model
model = SAR("y ~ X_1", W=w, data=gdf)
res = model.fit()

model.summary()
/home/runner/micromamba/envs/test/lib/python3.14/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  from .autonotebook import tqdm as notebook_tqdm
Gibbs sampling (sar): 4 chains for 1,000 tune and 2,000 draw iterations (4 x 3,000 = 12,000 draws total)
/home/runner/micromamba/envs/test/lib/python3.14/site-packages/rich/live.py:260: UserWarning: install "ipywidgets" 
for Jupyter support
  warnings.warn('install "ipywidgets" for Jupyter support')

Sampling took 2s (6,128 draws/s)
mean sd eti89_lb eti89_ub ess_bulk ess_tail r_hat mcse_mean mcse_sd
rho 0.4782 0.0286 0.43 0.52 7506 5822 1.00 0.00033 0.00033
sigma 1.0149 0.0181 0.99 1 7687 7484 1.00 0.00021 0.00015
sigma2 1.0303 0.0368 0.97 1.1 7687 7484 1.00 0.00042 0.0003
Intercept 1.069649 0.065774 0.964162 1.175777 7625.257089 6685.721707 1.000563 0.000754 0.000697
X_1 -1.02811 0.026606 -1.070692 -0.985458 7943.495409 7861.139564 1.000354 0.000299 0.000217
model.impacts()
direct direct_ci_lower direct_ci_upper direct_pvalue indirect indirect_ci_lower indirect_ci_upper indirect_pvalue total total_ci_lower total_ci_upper total_pvalue
variable
X_1 -1.069625 -1.124501 -1.014448 0.0 -0.906286 -1.121235 -0.717517 0.0 -1.975911 -2.215307 -1.76013 0.0
sim_spreg = spreg.GM_Lag(
    gdf["y"],
    gdf["X_1"],
    w=w,
)
GM_Lag
print(sim_spreg.summary)
REGRESSION RESULTS
------------------

SUMMARY OF OUTPUT: SPATIAL TWO STAGE LEAST SQUARES
------------------------------------------------------------------------------------
Data set            :     unknown
Weights matrix      :     unknown
Dependent Variable  :           y                Number of Observations:        1600
Mean dependent var  :      2.1306                Number of Variables   :           3
S.D. dependent var  :      1.5047                Degrees of Freedom    :        1597
Pseudo R-squared    :      0.5404
Spatial Pseudo R-squared:  0.4769

------------------------------------------------------------------------------------
            Variable     Coefficient       Std.Error     z-Statistic     Probability
------------------------------------------------------------------------------------
            CONSTANT         1.22752         0.11385        10.78147         0.00000
                 X_1        -1.03456         0.02719       -38.04315         0.00000
                 W_y         0.40360         0.05233         7.71321         0.00000
------------------------------------------------------------------------------------
Instrumented: W_y
Instruments: W_X_1

DIAGNOSTICS FOR SPATIAL DEPENDENCE
TEST                              DF         VALUE           PROB
Anselin-Kelejian Test             1          2.403           0.1211

SPATIAL LAG MODEL IMPACTS
Impacts computed using the 'simple' method.
            Variable         Direct        Indirect          Total
                 X_1        -1.0346         -0.7001         -1.7347
================================ END OF REPORT =====================================