CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-spreg

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

Overall
score

87%

Overview
Eval results
Files

diagnostics.mddocs/

Diagnostic Functions

Comprehensive diagnostic testing for spatial autocorrelation, heteroskedasticity, normality, multicollinearity, and model specification in spatial regression contexts.

Capabilities

Spatial Diagnostic Tests

class LMtests:
    def __init__(self, ols, w, tests=["all"]):
        """
        Lagrange Multiplier tests for spatial dependence.
        
        Parameters:
        - ols: OLS regression object
        - w: Spatial weights object
        - tests: list of strings specifying which tests to run
            * 'all': runs all tests (default)
            * 'lme': LM error test  
            * 'rlme': Robust LM error test
            * 'lml': LM lag test
            * 'rlml': Robust LM lag test
            * 'sarma': LM SARMA test
            * 'lmwx': LM test for WX
            * 'rlmwx': Robust LM WX test
            * 'lmspdurbin': Joint test for SDM
            * 'rlmdurlag': Robust LM Lag - SDM
        
        Attributes:
        - lme: tuple (statistic, p-value) for LM error test
        - lml: tuple (statistic, p-value) for LM lag test
        - rlme: tuple (statistic, p-value) for Robust LM error test
        - rlml: tuple (statistic, p-value) for Robust LM lag test
        - sarma: tuple (statistic, p-value) for SARMA test
        - lmwx: tuple (statistic, p-value) for LM WX test
        - rlmwx: tuple (statistic, p-value) for Robust LM WX test
        - lmspdurbin: tuple (statistic, p-value) for Joint SDM test
        - rlmdurlag: tuple (statistic, p-value) for Robust LM Lag - SDM test
        """

class MoranRes:
    def __init__(self, ols, w, z=False):
        """
        Moran's I test for spatial autocorrelation in residuals.
        
        Parameters:
        - ols: OLS regression object
        - w: Spatial weights object
        - z: boolean, if True computes eI, vI and zI attributes
        
        Attributes:
        - I: float, Moran's I statistic
        - eI: float, Moran's I expectation (if z=True)
        - vI: float, Moran's I variance (if z=True)  
        - zI: float, Moran's I standardized value (if z=True)
        """

class AKtest:
    def __init__(self, iv, w, case='nosp'):
        """
        Anselin-Kelejian test for spatial dependence in IV residuals.
        
        Parameters:
        - iv: TSLS regression object
        - w: Spatial weights object
        - case: string, 'nosp' for no spatial endogenous regressors, 'gen' for general case
        
        Attributes:
        - mi: float, Moran's I statistic for IV residuals
        - ak: float, Square of corrected Moran's I
        - p: float, P-value of the test
        """

General Diagnostic Tests

def jarque_bera(reg):
    """Jarque-Bera test for normality of residuals."""

def breusch_pagan(reg):
    """Breusch-Pagan test for heteroskedasticity."""

def white(reg):
    """White test for heteroskedasticity."""

def koenker_bassett(reg):
    """Koenker-Bassett test for heteroskedasticity."""

def vif(reg):
    """Variance Inflation Factor for multicollinearity detection."""

def f_stat(reg, z_stat=False):
    """F-statistic or Wald statistic for overall model significance."""

def t_stat(reg, z_stat=False):
    """t-statistics or z-statistics for individual coefficients."""

Usage Examples

Spatial Diagnostic Testing

import numpy as np
import spreg
from libpysal import weights

# OLS model for testing
n = 100
x = np.random.randn(n, 2)
y = np.random.randn(n, 1)
w = weights.KNN.from_array(np.random.randn(n, 2), k=5)

ols_model = spreg.OLS(y, x, nonspat_diag=False, spat_diag=False)

# Manual spatial diagnostic testing
lm_tests = spreg.LMtests(ols_model, w)

print("LM Error test:", lm_tests.lme)
print("LM Lag test:", lm_tests.lml)
print("Robust LM Error:", lm_tests.rlme)
print("Robust LM Lag:", lm_tests.rlml)
print("SARMA test:", lm_tests.sarma)

# Decision rule for spatial dependence
if lm_tests.lme[1] < 0.05 and lm_tests.lml[1] < 0.05:
    if lm_tests.rlme[1] < lm_tests.rlml[1]:
        print("Spatial error model recommended")
    else:
        print("Spatial lag model recommended")

# Moran's I test on residuals
moran_test = spreg.MoranRes(ols_model, w, z=True)
print(f"Moran's I: {moran_test.I}, Z-score: {moran_test.zI}")

# For TSLS models, use AKtest
tsls_model = spreg.TSLS(y, x[:,:1], x[:,1:2], x[:,1:2])  # Simple IV example
ak_test = spreg.AKtest(tsls_model, w)
print(f"AK test statistic: {ak_test.ak}, p-value: {ak_test.p}")

Install with Tessl CLI

npx tessl i tessl/pypi-spreg

docs

diagnostics.md

index.md

ml-models.md

ols-models.md

panel-models.md

probit-models.md

regime-models.md

spatial-error-models.md

sur-models.md

tsls-models.md

utilities.md

tile.json