or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

diagnostics.mdindex.mdml-models.mdols-models.mdpanel-models.mdprobit-models.mdregime-models.mdspatial-error-models.mdsur-models.mdtsls-models.mdutilities.md
tile.json

tessl/pypi-spreg

Spatial econometric regression models for analyzing geographically-related data interactions.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/spreg@1.8.x

To install, run

npx @tessl/cli install tessl/pypi-spreg@1.8.0

index.mddocs/

spreg

A 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.

Package Information

  • Package Name: spreg
  • Language: Python
  • Installation: pip install spreg

Core Imports

import spreg

Common 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_Lag

Basic Usage

import 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)

Architecture

spreg follows a consistent class-based architecture for regression models:

  • Base Classes: Provide core estimation without diagnostics (e.g., BaseOLS, BaseTSLS)
  • Full Classes: Extend base classes with comprehensive diagnostics and output formatting
  • Spatial Models: Handle spatial dependence through error correlation (*_Error) or lag structures (*_Lag)
  • Regime Models: Allow parameters to vary across spatial or categorical regimes
  • Diagnostic Functions: Standalone functions for testing spatial autocorrelation, heteroskedasticity, and model specification

The library integrates seamlessly with the PySAL ecosystem, using libpysal.weights objects for spatial relationships and supporting common NumPy/SciPy data structures.

Capabilities

OLS Regression Models

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): ...

OLS Models

Two-Stage Least Squares

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): ...

Two-Stage Least Squares

Spatial Error Models

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): ...

Spatial Error Models

Maximum Likelihood Models

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): ...

Maximum Likelihood Models

Regime-Based Models

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): ...

Regime Models

Panel Data Models

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): ...

Panel Models

SUR Models

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): ...

SUR Models

Probit Models

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): ...

Probit Models

Diagnostic Functions

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): ...

Diagnostic Functions

Utility Functions

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: ...

Utilities

Common Parameters

Most spreg models share these common parameters:

  • y (array): nx1 dependent variable
  • x (array): nxk independent variables (constant added automatically unless suppressed)
  • w (pysal W object or sparse matrix): Spatial weights matrix
  • regimes (list/Series): Regime identifier for observations
  • vm (boolean): Include variance-covariance matrix in output
  • name_y, name_x, name_w, name_ds (strings): Variable and dataset names for output
  • latex (boolean): Format output for LaTeX
  • hard_bound (boolean): Raise exception if spatial parameters outside [-1,1]

Common Attributes

All spreg regression models provide these standard attributes:

  • betas (array): Estimated coefficients
  • u (array): Residuals
  • predy (array): Predicted values
  • vm (array): Variance-covariance matrix (if requested)
  • n (int): Number of observations
  • k (int): Number of parameters
  • output (DataFrame): Formatted results table
  • summary (string): Comprehensive summary with diagnostics

Spatial models additionally provide:

  • e_filtered (array): Spatially filtered residuals
  • pr2 (float): Pseudo R-squared
  • Spatial parameters (rho for lag, lambda for error)