spatial_diagnostics

spatial_diagnostics(
    data,
    y,
    x,
    year=None,
    *,
    w=None,
    fixed_effects=None,
    alpha=0.05,
    gdf=None,
    ref=None,
)

Tests for spatial dependence left in a least-squares regression, and the spatial model they point to.

Parameters

Name Type Description Default
data A dataset id, or a panel with geo_id (and year), e.g. from load or combine required
y str The outcome column required
x The regressors: a column or a list of columns required
year The year to test (default: the last with data for y and every regressor) None
w Spatial weights from weights (default: queen contiguity among the units with data) None
fixed_effects str | None A column or a coarser scale, added as dummies None
alpha float Significance level of the decision rule 0.05
gdf Boundaries to use instead of the project’s (a GeoDataFrame with geo_id) None

Returns

Name Type Description
result object A result with .df (one row per test: Moran’s I of the residuals, the Lagrange multiplier tests for a lag and for an error model, their robust forms and the joint test, with statistic, df and p), .gt (the same as a table), .recommendation ("ols", "lag" or "error"), .reasoning, .moran_i_resid, .ols_model (the fitted spreg model) and .interpret().

Example

!pip install -q geo4bolivia
import geo4bolivia as geo

res = geo.spatial_diagnostics("sdg", "index_sdg", "urbanization")
print(res.interpret())
res.gt

Moran’s I on the OLS residuals is 0.47 (statistically significant at the 1% level) under queen contiguity (1 island(s) attached to nearest neighbor), row-standardized, n=343 — the residuals cluster in space, so the non-spatial model leaves spatial structure on the table.

At α = 0.05, the tests that reject no-spatial-dependence are: LM lag, LM error, robust LM lag, robust LM error. The robust forms are the decisive ones — each tests one dependence channel while allowing for the other.

Recommendation: error — Both robust LM tests reject at alpha = 0.05 (robust LM lag = 4.50, robust LM error = 23.08), so the larger robust statistic wins and points to the error model. With both channels active, consider durbin (SDM) - it nests both the lag and the spatially-lagged-X channels and lets the data separate them.

These are associations, not causal effects. A causal reading needs a research design.

Spatial dependence diagnostics for Sustainable Development Index, IDS (0-100)
OLS residual tests | W: queen contiguity (1 island(s) attached to nearest neighbor), row-standardized, n=343
Statistic df p-value
Moran's I (residuals) 0.4700 -- 0.0000
LM lag 168.5305 1 0.0000
LM error 187.1010 1 0.0000
Robust LM lag 4.5047 1 0.0338
Robust LM error 23.0751 1 0.0000
LM SARMA 191.6057 2 0.0000
Anselin-Florax recommendation: error