slm: spatial regression¶
OLS, spatial lag (SLM) and spatial error (SEM) models by maximum likelihood with spreg, reported as APA tables, with the diagnostics for spatial dependence and the direct, indirect and total effects of the spatial lag model.
from apatools.slm import slm, slm_report, slm_tests, slm_effects
results, metrics = slm(
data,
"y",
["x1", "x2"],
w, # w: libpysal weights, rows in the order of the data
model=["ols", "slm", "sem"],
ols_model_spat_diag=True,
pred_metrics=True,
pred_exclude_neighbors=True,
)
print(slm_report(results, metrics))
print(slm_tests(results, w, format_pval=True, add_stars=True, decimal=3))
print(slm_effects(results, w, decimal=3, add_stars=True, format_pval=True))
Arguments of the spreg model constructor are passed as <model>_model_<argument> with the prefix ols_, slm_ or
sem_ (for example ols_model_spat_diag, or slm_model_method="LU" for large samples). Rows dropped because of
missing values are removed from w automatically.
Design choices and caveats¶
Fit statistics.
R² of the ML models is spreg’s pseudo R² (the squared correlation of observed and predicted y); OLS has its usual R².
AIC and BIC count the spatial parameter in both spatial models. spreg’s own criteria of the spatial lag model count
rho, those of the spatial error model leavelambdaout of the parameter count, which flatters the error model in comparisons. apatools adds it (2 to the AIC andlog nto the BIC), so that OLS, SLM and SEM are comparable. Like spreg, none of them counts the error variance (see the table below for R).Leave-one-out prediction metrics (
pred_metrics): every observation is held out in turn, the model and the weights are refitted without it and the held-out value is predicted:x·βfor OLS and SEM,ρ·(Wy)ᵢ + x·βfor SLM (the observed neighbour values enter through the weights, so SLM is not strictly comparable with the other two). One fit per observation and model.
Caveat: plain leave-one-out is optimistic for spatial data. The neighbours of the held-out observation stay in the training sample and are correlated with it, so
R²predoverestimates how well the model predicts a new, unseen region.pred_exclude_neighbors=Truegives a more honest (slightly pessimistic) estimate; it may create islands in the training weights, which is reported with a warning.
slm_tests. One table of diagnostics, to check assumptions and the need for a spatial term, not the quality of the
fit.
OLS: Moran’s I of the residuals (a z test), the Lagrange multiplier tests for a missing spatial lag (
LM-lag) and for spatial error dependence (LM-error), their robust versions andLM-SARMA(Anselin et al., 1996), and Jarque–Bera, Breusch–Pagan, Koenker–Bassett and White tests of the residuals. The spatial tests needw. The customary reading (Anselin and Rey, 2014): if both LM tests are insignificant, OLS is fine; otherwise fit the model of the more significant one, and if both are significant, decide by the robust versions. The function does not make that decision.Breusch–Pagan and Koenker–Bassett regress on the regressors of the model, as R’s
bptestand statsmodels do. spreg’s default is to use their squares; apatools overrides that default (and says so here), so that the numbers equal the ones of the other software. White’s test is spreg’s.SLM and SEM: the z test and the likelihood-ratio test (against the OLS model among the results) of
rho/lambda, Jarque–Bera of the residuals, and Moran’s I of the residuals with a permutation p-value (n_perm,seed; a significant value means that spatial dependence is left in the model). The residuals are those of the model:y - ρWy - Xbfor the lag model and the filtered ones,(I - λW)(y - Xb), for the error model. The permutation test treats the residuals as exchangeable, an approximation for model residuals.
slm_effects: direct, indirect and total effects. In the spatial lag model the coefficient of a regressor is not
its effect: a change in one location spills over to its neighbours through ρWy. LeSage and Pace (2009) summarize the
effect matrix \((I - \rho W)^{-1}\beta_k\) by three averages: the direct effect (the diagonal: the effect on the
location itself, including the feedback through its neighbours), the total effect (the row sums: a unit change of the
regressor in all locations) and the indirect effect, the spillover (total minus direct).
The averages are series in the traces of the powers of
W, so no inverse ofI - ρWis needed;traces="exact"takes them from the eigenvalues (cost n³, the default up to 2000 units),traces="mc"estimates them with random vectors (Hutchinson) for largen.Two ways to the standard errors.
inference="simulation"(default) drawsn_drawstimes from the estimated joint normal distribution of the coefficients andρ(LeSage and Pace) and takes the standard deviation and the percentiles of the effects of the draws (the interval can be asymmetric);inference="analytic"is the delta method with the gradient of the same series (the interval is estimate ± z · se).When the two disagree. The delta method treats the effects as linear in the estimates. That holds when
ρis estimated precisely; with few units and a largeρit can understate the standard errors of the indirect and total effects badly (in a 64-unit example with ρ = 0.6: 0.44 against 1.48 by simulation with 100,000 draws and 1.43 in R’simpacts). The direct effect is much less sensitive. Prefer the simulation then.The simulation is noisy in the same situation, because a few draws of
ρnear its bound give huge effects: the standard error of that indirect effect was 1.03 with 5000 draws and 1.48 with 100,000 (R’s own simulation behaves the same way: 1.18 and 1.43). Raisen_drawsand check that the result is stable with anotherseed. Draws with|ρ|at or above the bound given by the spectral radius ofWare replaced.The covariance matrix is spreg’s, which includes the covariance between the coefficients and
ρ.OLS has no spillovers, and in the spatial error model the coefficients already are the direct and total effects: those models are skipped with a warning. The effects are averages; they do not say how the effect differs between locations.
Where it differs from R¶
what |
apatools / spreg |
R ( |
difference |
|---|---|---|---|
ML estimates, standard errors, log-likelihood |
spreg |
|
agree: coefficients and standard errors within 1e-4, log-likelihood within 1e-8 |
AIC, BIC |
do not count the error variance (spatial parameter counted) |
count it |
R’s AIC is 2 higher (BIC: |
Moran’s I of the OLS residuals |
z test, two-sided p-value |
|
our p-value is twice R’s when I is above its expectation |
LM tests, SARMA |
spreg |
|
agree (1e-7) |
Breusch–Pagan, Koenker–Bassett |
regress on the regressors |
|
agree (1e-7); spreg’s own default differs (squares) |
White |
spreg |
|
agree (1e-7) |
direct, indirect, total effects |
series in the traces |
|
point estimates agree (1e-6); standard errors of the simulation within the noise of the simulations |
Checked against¶
tests/test_r_slm.py compares with spatialreg, spdep and lmtest on two simulated data sets (rook and queen
lattices), tests/test_effects.py with the dense matrix inverse, spreg’s multipliers, finite differences of the effects
(the gradients of the delta method) and the simulation (the standard errors), and the point estimates of the effects
with spatialreg::impacts.
Literature¶
The references cited in the docstrings of the functions of this module (they are collected from the docstrings when the documentation is built; the software that does the work is on the software page).
Anselin, L. (1988). Spatial econometrics: Methods and models. Kluwer Academic. https://doi.org/10.1007/978-94-015-7799-1
Anselin, L., & Rey, S. J. (2014). Modern Spatial Econometrics in Practice: A Guide to GeoDa, GeoDaSpace and PySAL. GeoDa Press.
Anselin, L., Bera, A. K., Florax, R., & Yoon, M. J. (1996). Simple diagnostic tests for spatial dependence. Regional Science and Urban Economics, 26(1), 77-104. https://doi.org/10.1016/0166-0462(95)02111-6
Bera, A. K., & Yoon, M. J. (1993). Specification testing with locally misspecified alternatives. Econometric Theory, 9(4), 649-658. https://doi.org/10.1017/S0266466600008021
Breusch, T. S., & Pagan, A. R. (1979). A simple test for heteroscedasticity and random coefficient variation. Econometrica, 47(5), 1287-1294. https://doi.org/10.2307/1911963
Burridge, P. (1980). On the Cliff-Ord test for spatial correlation. Journal of the Royal Statistical Society Series B: Statistical Methodology, 42(1), 107-108. https://doi.org/10.1111/j.2517-6161.1980.tb01108.x
Cliff, A., & Ord, K. (1972). Testing for spatial autocorrelation among regression residuals. Geographical Analysis, 4(3), 267-284. https://doi.org/10.1111/j.1538-4632.1972.tb00475.x
Elhorst, J. P. (2014). Spatial econometrics: From cross-sectional data to spatial panels. Springer. https://doi.org/10.1007/978-3-642-40340-8 https://doi.org/10.1007/978-3-642-40340-8
Jarque, C. M., & Bera, A. K. (1980). Efficient tests for normality, homoscedasticity and serial independence of regression residuals. Economics Letters, 6(3), 255-259. https://doi.org/10.1016/0165-1765(80)90024-5
Koenker, R., & Bassett, G. (1982). Robust tests for heteroscedasticity based on regression quantiles. Econometrica, 50(1), 43-61. https://doi.org/10.2307/1912528
LeSage, J., & Pace, R. K. (2009). Introduction to spatial econometrics. CRC Press. https://doi.org/10.1201/9781420064254 https://doi.org/10.1201/9781420064254
Roberts, D. R., Bahn, V., Ciuti, S., Boyce, M. S., Elith, J., Guillera-Arroita, G., Hauenstein, S., Lahoz-Monfort, J. J., Schröder, B., Thuiller, W., Warton, D. I., Wintle, B. A., Hartig, F., & Dormann, C. F. (2017). Cross-validation strategies for data with temporal, spatial, hierarchical, or phylogenetic structure. Ecography, 40(8), 913-929. https://doi.org/10.1111/ecog.02881
White, H. (1980). A heteroskedasticity-consistent covariance matrix estimator and a direct test for heteroskedasticity. Econometrica, 48(4), 817-838. https://doi.org/10.2307/1912934