Spatial Regression (spreg)¶
Description:
- spreg, short for "spatial regression", is a Python package to estimate simultaneous autoregressive spatial regression models. This is part of the refactoring of PySal.
Resources:
- [GitHub] https://github.com/pysal/spreg?tab=readme-ov-file
- [References] https://github.com/pysal/spreg/blob/main/docs/api.rst
- [Examples] https://github.com/pysal/spreg/tree/main/notebooks
- [Modern Spatial Econometrics in Practice: A Guide to GeoDa, GeoDaSpace and PySAL] https://sergerey.org/giasp16/pdfs/anselin_rey_weights.pdf
Example 1¶
Basic Spatial 2SLS
In [ ]:
Copied!
import numpy
import libpysal
import spreg
libpysal.examples.explain("baltim")
# Read Baltimore data
db = libpysal.io.open(libpysal.examples.get_path("baltim.dbf"), "r")
ds_name = "baltim.dbf"
# Read dependent variable
y_name = "PRICE"
y = numpy.array(db.by_col(y_name)).T
y = y[:, numpy.newaxis]
# Read exogenous variables
x_names = ["NROOM", "NBATH", "PATIO", "FIREPL", "AC", "GAR", "AGE", "LOTSZ", "SQFT"]
x = numpy.array([db.by_col(var) for var in x_names]).T
# Read spatial data
ww = libpysal.io.open(libpysal.examples.get_path("baltim_q.gal"))
w = ww.read()
ww.close()
w_name = "baltim_q.gal"
w.transform = "r"
model = spreg.GM_Lag(
y,
x,
w=w,
name_y=y_name,
name_x=x_names,
name_w="baltim_q",
name_ds="baltim"
)
print(model.summary)
import numpy
import libpysal
import spreg
libpysal.examples.explain("baltim")
# Read Baltimore data
db = libpysal.io.open(libpysal.examples.get_path("baltim.dbf"), "r")
ds_name = "baltim.dbf"
# Read dependent variable
y_name = "PRICE"
y = numpy.array(db.by_col(y_name)).T
y = y[:, numpy.newaxis]
# Read exogenous variables
x_names = ["NROOM", "NBATH", "PATIO", "FIREPL", "AC", "GAR", "AGE", "LOTSZ", "SQFT"]
x = numpy.array([db.by_col(var) for var in x_names]).T
# Read spatial data
ww = libpysal.io.open(libpysal.examples.get_path("baltim_q.gal"))
w = ww.read()
ww.close()
w_name = "baltim_q.gal"
w.transform = "r"
model = spreg.GM_Lag(
y,
x,
w=w,
name_y=y_name,
name_x=x_names,
name_w="baltim_q",
name_ds="baltim"
)
print(model.summary)