{"id":28271,"library":"spreg","title":"spreg","description":"PySAL Spatial Econometric Regression in Python. Provides spatial regression models (OLS, ML, GM, Panel) and diagnostics. Current version: 1.9.0. Released approximately every 3-6 months.","status":"active","version":"1.9.0","language":"python","source_language":"en","source_url":"https://github.com/pysal/spreg","tags":["spatial econometrics","pysal","regression","spatial statistics"],"install":[{"cmd":"pip install spreg","lang":"bash","label":"Install from PyPI"}],"dependencies":[{"reason":"Provides spatial weights (W) and other spatial utilities","package":"libpysal","optional":false},{"reason":"Data I/O and DataFrame support","package":"pandas","optional":false},{"reason":"Numerical computation","package":"numpy","optional":false},{"reason":"Sparse matrix operations and optimization","package":"scipy","optional":false}],"imports":[{"note":"Top-level import is the correct pattern","wrong":"from spreg.ols import OLS","symbol":"OLS","correct":"from spreg import OLS"},{"note":"GM_Lag is a top-level class, not in a submodule","wrong":"from spreg.GM import GM_Lag","symbol":"GM_Lag","correct":"from spreg import GM_Lag"},{"note":"ML classes are at top-level","wrong":"from spreg.ML import ML_Error","symbol":"ML_Error","correct":"from spreg import ML_Error"},{"note":"Added in v1.8.0","symbol":"spsearch","correct":"from spreg import spsearch"}],"quickstart":{"code":"import numpy as np\nimport libpysal\nfrom spreg import OLS\n\n# Generate random data\nnp.random.seed(123)\nn = 100\nx = np.random.randn(n, 2)\ny = 1 + 2*x[:,0] + 3*x[:,1] + np.random.randn(n)*0.5\n\n# Create spatial weights (Queen contiguity from random points)\npts = np.random.rand(n, 2)\nw = libpysal.weights.DistanceBand.from_array(pts, threshold=0.5, binary=True)\nw.transform = 'r'\n\n# Fit OLS\nm1 = OLS(y, x, w=w, spat_diag=True)\nprint(m1.summary)","lang":"python","description":"Basic OLS with spatial diagnostics using random data and distance-based weights."},"warnings":[{"fix":"Ensure your weights are passed as libpysal W objects or accept automatic conversion. If you need the old behavior, convert to W yourself.","message":"In v1.8+, spreg automatically converts sparse and full weights matrices to libpysal W objects. If you relied on passing raw scipy sparse matrices, they will be converted, which may change behavior.","severity":"breaking","affected_versions":">=1.8.0"},{"fix":"Replace check_spat_diag(y, x, w) with OLS(y, x, w, spat_diag=True) or equivalent.","message":"check_spat_diag was deprecated in v1.6.1 and removed in later versions. Use spat_diag=True in model constructors instead.","severity":"deprecated","affected_versions":">=1.6.1"},{"fix":"Convert regimes to a pandas Categorical: regimes = pd.Categorical(regimes). Then pass to the model.","message":"When using regime models (e.g., OLS_Regimes), the regimes argument must be a pandas Categorical or a list of strings with length equal to number of observations. Mixing types can cause silent errors.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-05-09T00:00:00.000Z","next_check":"2026-08-07T00:00:00.000Z","problems":[{"fix":"Use: from spreg import OLS","cause":"Incorrect import path for OLS class.","error":"ImportError: cannot import name 'OLS' from 'spreg.ols'"},{"fix":"Convert your weights using libpysal.weights.W(weights_matrix) or use libpysal.weights to read shapefiles.","cause":"Passed a raw scipy sparse matrix or numpy array instead of a libpysal W object.","error":"ValueError: The weights matrix must be a W object."},{"fix":"Ensure regimes is either a pandas Categorical or a list of strings of length n. Example: regimes = pd.Categorical(['a','b']*50)","cause":"Regimes argument is not properly formatted.","error":"TypeError: regimes must be a pandas Categorical or a list of strings with length equal to the number of observations."}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}