This page was generated from notebooks/GM_Lag_example.ipynb. Interactive online version: Binder badge


Spatial 2SLS

  • This notebook contains the PySAL/spreg code for Chapter 7 - Spatial 2SLS

    • in: Modern Spatial Econometrics in Practice: A Guide to GeoDa, GeoDaSpace and PySAL.

    • by: Luc Anselin and Sergio J. Rey.

[1]:
%load_ext watermark
%watermark
Last updated: 2021-01-03T11:26:48.607797-05:00

Python implementation: CPython
Python version       : 3.8.6
IPython version      : 7.19.0

Compiler    : Clang 11.0.0
OS          : Darwin
Release     : 20.2.0
Machine     : x86_64
Processor   : i386
CPU cores   : 8
Architecture: 64bit

[2]:
import numpy
import libpysal
import spreg

%watermark -w
%watermark -iv
Watermark: 2.1.0

spreg   : 1.2.0.post1
libpysal: 4.3.0
numpy   : 1.19.4

Data

[3]:
libpysal.examples.explain("baltim")
baltim
======

Baltimore house sales prices and hedonics, 1978.
----------------------------------------------------------------

* baltim.dbf: attribute data. (k=17)
* baltim.shp: Point shapefile. (n=211)
* baltim.shx: spatial index.
* baltim.tri.k12.kwt: kernel weights using a triangular kernel with 12 nearest neighbors in KWT format.
* baltim_k4.gwt: nearest neighbor weights (4nn) in GWT format.
* baltim_q.gal: queen contiguity weights in GAL format.
* baltimore.geojson: spatial weights in geojson format.

Source: Dubin, Robin A. (1992). Spatial autocorrelation and neighborhood quality. Regional Science and Urban Economics 22(3), 433-452.
[4]:
# 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
[5]:
# 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"

Basic Spatial 2SLS

The model to estimate is:

\[y = \rho Wy + X \beta + \epsilon\]

where you use \(WX\) as instruments of \(Wy\).

[6]:
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)
REGRESSION
----------
SUMMARY OF OUTPUT: SPATIAL TWO STAGE LEAST SQUARES
--------------------------------------------------
Data set            :      baltim
Weights matrix      :    baltim_q
Dependent Variable  :       PRICE                Number of Observations:         211
Mean dependent var  :     44.3072                Number of Variables   :          11
S.D. dependent var  :     23.6061                Degrees of Freedom    :         200
Pseudo R-squared    :      0.7278
Spatial Pseudo R-squared:  0.6928

------------------------------------------------------------------------------------
            Variable     Coefficient       Std.Error     z-Statistic     Probability
------------------------------------------------------------------------------------
            CONSTANT      -2.5762742       5.5210355      -0.4666288       0.6407655
               NROOM       0.9440746       1.0609697       0.8898224       0.3735612
               NBATH       5.5981348       1.7376725       3.2216283       0.0012746
               PATIO       5.8424768       2.7435166       2.1295577       0.0332081
              FIREPL       6.4579185       2.4238370       2.6643369       0.0077140
                  AC       5.4871926       2.3450930       2.3398614       0.0192909
                 GAR       4.3565951       1.6955478       2.5694321       0.0101865
                 AGE      -0.0730060       0.0523546      -1.3944510       0.1631814
               LOTSZ       0.0579765       0.0149534       3.8771359       0.0001057
                SQFT       0.0395330       0.1638055       0.2413409       0.8092909
             W_PRICE       0.5823313       0.0721387       8.0723813       0.0000000
------------------------------------------------------------------------------------
Instrumented: W_PRICE
Instruments: W_AC, W_AGE, W_FIREPL, W_GAR, W_LOTSZ, W_NBATH, W_NROOM,
             W_PATIO, W_SQFT
================================ END OF REPORT =====================================

Second order spatial lags

You can also use \([WX, W^2X]\) as instruments of \(Wy\).

[7]:
# using second order spatial lags for the instruments, set w_lags = 2
model2 = spreg.GM_Lag(
    y,
    x,
    w=w,
    w_lags=2,
    name_y=y_name,
    name_x=x_names,
    name_w="baltim_q",
    name_ds="baltim"
)
print(model2.summary)
REGRESSION
----------
SUMMARY OF OUTPUT: SPATIAL TWO STAGE LEAST SQUARES
--------------------------------------------------
Data set            :      baltim
Weights matrix      :    baltim_q
Dependent Variable  :       PRICE                Number of Observations:         211
Mean dependent var  :     44.3072                Number of Variables   :          11
S.D. dependent var  :     23.6061                Degrees of Freedom    :         200
Pseudo R-squared    :      0.7276
Spatial Pseudo R-squared:  0.6915

------------------------------------------------------------------------------------
            Variable     Coefficient       Std.Error     z-Statistic     Probability
------------------------------------------------------------------------------------
            CONSTANT      -2.8867580       5.4563344      -0.5290654       0.5967601
               NROOM       0.9527428       1.0613055       0.8977083       0.3693411
               NBATH       5.5975309       1.7386698       3.2194330       0.0012844
               PATIO       5.7884989       2.7409866       2.1118304       0.0347010
              FIREPL       6.4012808       2.4201110       2.6450360       0.0081682
                  AC       5.4587589       2.3451078       2.3277220       0.0199269
                 GAR       4.3440361       1.6961624       2.5610969       0.0104342
                 AGE      -0.0713188       0.0521742      -1.3669353       0.1716456
               LOTSZ       0.0575329       0.0149111       3.8583943       0.0001141
                SQFT       0.0377524       0.1638248       0.2304438       0.8177470
             W_PRICE       0.5893267       0.0695101       8.4782886       0.0000000
------------------------------------------------------------------------------------
Instrumented: W_PRICE
Instruments: W2_AC, W2_AGE, W2_FIREPL, W2_GAR, W2_LOTSZ, W2_NBATH, W2_NROOM,
             W2_PATIO, W2_SQFT, W_AC, W_AGE, W_FIREPL, W_GAR, W_LOTSZ,
             W_NBATH, W_NROOM, W_PATIO, W_SQFT
================================ END OF REPORT =====================================

Spatial Diagnostics

[8]:
model = spreg.GM_Lag(
    y,
    x,
    w=w,
    spat_diag=True,
    name_y=y_name,
    name_x=x_names,
    name_w="baltim_q",
    name_ds="baltim"
)
print(model.summary)
REGRESSION
----------
SUMMARY OF OUTPUT: SPATIAL TWO STAGE LEAST SQUARES
--------------------------------------------------
Data set            :      baltim
Weights matrix      :    baltim_q
Dependent Variable  :       PRICE                Number of Observations:         211
Mean dependent var  :     44.3072                Number of Variables   :          11
S.D. dependent var  :     23.6061                Degrees of Freedom    :         200
Pseudo R-squared    :      0.7278
Spatial Pseudo R-squared:  0.6928

------------------------------------------------------------------------------------
            Variable     Coefficient       Std.Error     z-Statistic     Probability
------------------------------------------------------------------------------------
            CONSTANT      -2.5762742       5.5210355      -0.4666288       0.6407655
               NROOM       0.9440746       1.0609697       0.8898224       0.3735612
               NBATH       5.5981348       1.7376725       3.2216283       0.0012746
               PATIO       5.8424768       2.7435166       2.1295577       0.0332081
              FIREPL       6.4579185       2.4238370       2.6643369       0.0077140
                  AC       5.4871926       2.3450930       2.3398614       0.0192909
                 GAR       4.3565951       1.6955478       2.5694321       0.0101865
                 AGE      -0.0730060       0.0523546      -1.3944510       0.1631814
               LOTSZ       0.0579765       0.0149534       3.8771359       0.0001057
                SQFT       0.0395330       0.1638055       0.2413409       0.8092909
             W_PRICE       0.5823313       0.0721387       8.0723813       0.0000000
------------------------------------------------------------------------------------
Instrumented: W_PRICE
Instruments: W_AC, W_AGE, W_FIREPL, W_GAR, W_LOTSZ, W_NBATH, W_NROOM,
             W_PATIO, W_SQFT

DIAGNOSTICS FOR SPATIAL DEPENDENCE
TEST                           MI/DF       VALUE           PROB
Anselin-Kelejian Test             1           5.234          0.0221
================================ END OF REPORT =====================================

White Standard Errors

[9]:
model = spreg.GM_Lag(
    y,
    x,
    w=w,
    robust="white",
    spat_diag=True,
    name_y=y_name,
    name_x=x_names,
    name_w="baltim_q",
    name_ds="baltim"
)
print(model.summary)
REGRESSION
----------
SUMMARY OF OUTPUT: SPATIAL TWO STAGE LEAST SQUARES
--------------------------------------------------
Data set            :      baltim
Weights matrix      :    baltim_q
Dependent Variable  :       PRICE                Number of Observations:         211
Mean dependent var  :     44.3072                Number of Variables   :          11
S.D. dependent var  :     23.6061                Degrees of Freedom    :         200
Pseudo R-squared    :      0.7278
Spatial Pseudo R-squared:  0.6928

White Standard Errors
------------------------------------------------------------------------------------
            Variable     Coefficient       Std.Error     z-Statistic     Probability
------------------------------------------------------------------------------------
            CONSTANT      -2.5762742       7.0147591      -0.3672648       0.7134215
               NROOM       0.9440746       1.4002856       0.6742015       0.5001832
               NBATH       5.5981348       2.1605285       2.5910951       0.0095671
               PATIO       5.8424768       2.9445656       1.9841558       0.0472385
              FIREPL       6.4579185       2.4500195       2.6358641       0.0083923
                  AC       5.4871926       2.6021469       2.1087175       0.0349690
                 GAR       4.3565951       2.2070747       1.9739228       0.0483905
                 AGE      -0.0730060       0.0976079      -0.7479516       0.4544894
               LOTSZ       0.0579765       0.0237454       2.4415887       0.0146228
                SQFT       0.0395330       0.2355809       0.1678105       0.8667323
             W_PRICE       0.5823313       0.1325884       4.3920220       0.0000112
------------------------------------------------------------------------------------
Instrumented: W_PRICE
Instruments: W_AC, W_AGE, W_FIREPL, W_GAR, W_LOTSZ, W_NBATH, W_NROOM,
             W_PATIO, W_SQFT

DIAGNOSTICS FOR SPATIAL DEPENDENCE
TEST                           MI/DF       VALUE           PROB
Anselin-Kelejian Test             1           5.234          0.0221
================================ END OF REPORT =====================================