Spatial econometric regression models for analyzing geographically-related data interactions.
npx @tessl/cli install tessl/pypi-spreg@1.8.0A comprehensive Python library for spatial econometric regression analysis. spreg provides methods for analyzing processes where observations interact with one another across geographic space, offering simultaneous autoregressive spatial regression models, diagnostic tools, and regime-based analysis capabilities.
pip install spregimport spregCommon specific imports:
from spreg import OLS, TSLS, GM_Error_Het, GM_Error_Hom
from spreg import GM_Combo_Het, GM_Combo_Hom
from spreg import ML_Error, ML_Lagimport numpy as np
import spreg
from libpysal import weights
# Prepare data
n = 100
y = np.random.randn(n, 1)
x = np.random.randn(n, 2)
w = weights.lat2W(10, 10) # 10x10 spatial weights
# Basic OLS with spatial diagnostics
ols_model = spreg.OLS(y, x, w=w, spat_diag=True, name_y='y', name_x=['x1', 'x2'])
print(ols_model.summary)
# Spatial error model with heteroskedasticity
spatial_model = spreg.GM_Error_Het(y, x, w=w.sparse, name_y='y', name_x=['x1', 'x2'])
print(spatial_model.summary)
# Two-stage least squares for endogenous variables
yend = np.random.randn(n, 1) # endogenous variable
q = np.random.randn(n, 1) # instrument
tsls_model = spreg.TSLS(y, x, yend, q, name_y='y', name_x=['x1', 'x2'],
name_yend=['yend'], name_q=['instrument'])
print(tsls_model.summary)spreg follows a consistent class-based architecture for regression models:
BaseOLS, BaseTSLS)*_Error) or lag structures (*_Lag)The library integrates seamlessly with the PySAL ecosystem, using libpysal.weights objects for spatial relationships and supporting common NumPy/SciPy data structures.
Ordinary least squares with extensive spatial and non-spatial diagnostic capabilities, supporting robust standard errors and spatial lag of X (SLX) specifications.
class OLS:
def __init__(self, y, x, w=None, robust=None, gwk=None, sig2n_k=False,
nonspat_diag=True, spat_diag=False, moran=False,
white_test=False, vif=False, slx_lags=0, slx_vars='All',
regimes=None, vm=False, constant_regi='one', cols2regi='all',
regime_err_sep=False, cores=False, name_y=None, name_x=None,
name_w=None, name_ds=None, latex=False): ...Two-stage least squares estimation for handling endogenous variables, with spatial diagnostic capabilities and regime-based analysis.
class TSLS:
def __init__(self, y, x, yend, q, h=None, robust=None, gwk=None, sig2n_k=False,
nonspat_diag=True, spat_diag=False, slx_lags=0, slx_vars='All',
regimes=None, vm=False, name_y=None, name_x=None, name_yend=None,
name_q=None, name_h=None, name_w=None, name_ds=None, latex=False): ...GMM estimation of spatial error models with options for heteroskedasticity, homoskedasticity, and combined spatial lag-error specifications (SARAR models).
class GM_Error_Het:
def __init__(self, y, x, w, max_iter=1, epsilon=0.0000001, step1c=False,
inv_method='power_exp', hard_bound=False, vm=False, name_y=None,
name_x=None, name_w=None, name_ds=None, latex=False): ...
class GM_Error_Hom:
def __init__(self, y, x, w, hard_bound=False, vm=False, name_y=None,
name_x=None, name_w=None, name_ds=None, latex=False): ...
class GM_Combo_Het:
def __init__(self, y, x, yend, q, w, w_lags=1, lag_q=True, max_iter=1,
epsilon=0.0000001, step1c=False, inv_method='power_exp',
hard_bound=False, vm=False, name_y=None, name_x=None,
name_yend=None, name_q=None, name_w=None, name_ds=None,
latex=False): ...Full information maximum likelihood estimation for spatial lag and error models with analytical derivatives and concentrated log-likelihood functions.
class ML_Error:
def __init__(self, y, x, w, epsilon=0.0000001, hard_bound=False, vm=False,
name_y=None, name_x=None, name_w=None, name_ds=None, latex=False): ...
class ML_Lag:
def __init__(self, y, x, w, epsilon=0.0000001, hard_bound=False, vm=False,
name_y=None, name_x=None, name_w=None, name_ds=None, latex=False): ...Spatial regression models allowing parameters to vary across regimes, with options for separate or joint estimation and extensive regime-specific diagnostics.
class GM_Error_Het_Regimes:
def __init__(self, y, x, regimes, w, constant_regi='many', cols2regi='all',
regime_err_sep=False, regime_lag_sep=False, cores=False,
max_iter=1, epsilon=0.0000001, step1c=False,
inv_method='power_exp', hard_bound=False, vm=False,
name_y=None, name_x=None, name_regimes=None, name_w=None,
name_ds=None, latex=False): ...Fixed effects and random effects panel data models with spatial error and lag specifications for analyzing spatially-correlated panel datasets.
class Panel_FE_Error:
def __init__(self, y, x, w, epsilon=0.0000001, hard_bound=False, vm=False,
name_y=None, name_x=None, name_w=None, name_ds=None,
latex=False): ...
class Panel_RE_Error:
def __init__(self, y, x, w, epsilon=0.0000001, hard_bound=False, vm=False,
name_y=None, name_x=None, name_w=None, name_ds=None,
latex=False): ...Seemingly unrelated regressions with spatial error and lag structures for simultaneous equation systems with cross-equation correlation.
class SUR:
def __init__(self, bigy, bigX, df_name=None, sur_constant=True, name_bigy=None,
name_bigX=None, name_ds=None, vm=False, latex=False): ...Spatial probit regression for binary choice models with spatial dependence, supporting various spatial structures and diagnostic tests.
class Probit:
def __init__(self, y, x, w=None, optim='newton', scalem='phimean', maxiter=100,
vm=False, name_y=None, name_x=None, name_w=None, name_ds=None,
latex=False, hard_bound=False): ...Comprehensive diagnostic testing for spatial autocorrelation, heteroskedasticity, normality, multicollinearity, and model specification in spatial regression contexts.
class LMtests:
def __init__(self, ols, w, tests=["all"]): ...
class MoranRes:
def __init__(self, ols, w, z=False): ...
class AKtest:
def __init__(self, iv, w, case='nosp'): ...
def jarque_bera(reg): ...
def breusch_pagan(reg): ...
def white(reg): ...Core utilities for spatial operations, matrix computations, GMM optimization, and data generation for simulation studies.
def get_lags(w, x, w_lags): ...
def get_spFilter(w, lamb, sf): ...
def optim_moments(moments_in, vcX, all_par, start, hard_bound): ...
def set_endog(y, x, w, yend, q, w_lags, lag_q, slx_lags, slx_vars): ...
class RegressionPropsY: ...
class RegressionPropsVM: ...Most spreg models share these common parameters:
y (array): nx1 dependent variablex (array): nxk independent variables (constant added automatically unless suppressed)w (pysal W object or sparse matrix): Spatial weights matrixregimes (list/Series): Regime identifier for observationsvm (boolean): Include variance-covariance matrix in outputname_y, name_x, name_w, name_ds (strings): Variable and dataset names for outputlatex (boolean): Format output for LaTeXhard_bound (boolean): Raise exception if spatial parameters outside [-1,1]All spreg regression models provide these standard attributes:
betas (array): Estimated coefficientsu (array): Residualspredy (array): Predicted valuesvm (array): Variance-covariance matrix (if requested)n (int): Number of observationsk (int): Number of parametersoutput (DataFrame): Formatted results tablesummary (string): Comprehensive summary with diagnosticsSpatial models additionally provide:
e_filtered (array): Spatially filtered residualspr2 (float): Pseudo R-squaredrho for lag, lambda for error)