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


Spatial Panel Models with Fixed Effects

[1]:
%load_ext watermark
%watermark
Last updated: 2021-01-04T11:36:50.162976-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

Open data on NCOVR US County Homicides (3085 areas).

  • First, extract the HR (homicide rates) data in the 70’s, 80’s and 90’s as the dependent variable.

  • Data can also be passed in the long format instead of wide format.

    • i.e. a vector with \(n \times t\) rows and a single column for the dependent variable, and

    • a matrix of dimension \(n \times (t \ast k)\) for the independent variables

  • Then, extract RD and PS as independent variables in the regression.

[3]:
# Open data on NCOVR US County Homicides (3085 areas).
nat = libpysal.examples.load_example("NCOVR")
db = libpysal.io.open(nat.get_path("NAT.dbf"), "r")

# Create spatial weight matrix
nat_shp = libpysal.examples.get_path("NAT.shp")
w = libpysal.weights.Queen.from_shapefile(nat_shp)
w.transform = 'r'

# Define dependent variable
name_y = ["HR70", "HR80", "HR90"]
y = numpy.array([db.by_col(name) for name in name_y]).T

# Define independent variables
name_x = ["RD70", "RD80", "RD90", "PS70", "PS80", "PS90"]
x = numpy.array([db.by_col(name) for name in name_x]).T

Spatial Lag model

Let’s estimate a spatial lag panel model with fixed effects:

\[y = \rho Wy + X\beta + \mu_i + e\]
[4]:
fe_lag = spreg.Panel_FE_Lag(
    y, x, w, name_y=name_y, name_x=name_x, name_ds="NAT"
)
[5]:
print(fe_lag.summary)
REGRESSION
----------
SUMMARY OF OUTPUT: MAXIMUM LIKELIHOOD SPATIAL LAG PANEL - FIXED EFFECTS
-----------------------------------------------------------------------
Data set            :         NAT
Weights matrix      :     unknown
Dependent Variable  :          HR                Number of Observations:        9255
Mean dependent var  :      0.0000                Number of Variables   :           3
S.D. dependent var  :      3.9228                Degrees of Freedom    :        9252
Pseudo R-squared    :      0.0319
Spatial Pseudo R-squared:  0.0079
Sigma-square ML     :      14.935                Log likelihood        :  -67936.533
S.E of regression   :       3.865                Akaike info criterion :  135879.066
                                                 Schwarz criterion     :  135900.465

------------------------------------------------------------------------------------
            Variable     Coefficient       Std.Error     z-Statistic     Probability
------------------------------------------------------------------------------------
                  RD       0.8005886       0.1614474       4.9588189       0.0000007
                  PS      -2.6003523       0.4935486      -5.2686851       0.0000001
                W_HR       0.1903043       0.0159991      11.8947008       0.0000000
------------------------------------------------------------------------------------
Warning: Assuming panel is in wide format.
y[:, 0] refers to T0, y[:, 1] refers to T1, etc.
x[:, 0:T] refers to T periods of k1, x[:, T+1:2T] refers to k2, etc.
================================ END OF REPORT =====================================
[6]:
numpy.around(fe_lag.betas, decimals=4)
[6]:
array([[ 0.8006],
       [-2.6004],
       [ 0.1903]])

Spatial Error model

Now, let’s estimate a spatial error panel model with fixed effects:

\[y = X\beta + \mu_i + v\]

where

\[v = \lambda W v + e\]
[7]:
fe_error = spreg.Panel_FE_Error(
    y, x, w, name_y=name_y, name_x=name_x, name_ds="NAT"
)
[8]:
print(fe_error.summary)
REGRESSION
----------
SUMMARY OF OUTPUT: MAXIMUM LIKELIHOOD SPATIAL ERROR PANEL - FIXED EFFECTS
-------------------------------------------------------------------------
Data set            :         NAT
Weights matrix      :     unknown
Dependent Variable  :          HR                Number of Observations:        9255
Mean dependent var  :      0.0000                Number of Variables   :           2
S.D. dependent var  :      3.9228                Degrees of Freedom    :        9253
Pseudo R-squared    :      0.0084
Sigma-square ML     :      14.923                Log likelihood        :  -67934.005
S.E of regression   :       3.863                Akaike info criterion :  135872.010
                                                 Schwarz criterion     :  135886.276

------------------------------------------------------------------------------------
            Variable     Coefficient       Std.Error     z-Statistic     Probability
------------------------------------------------------------------------------------
                  RD       0.8697923       0.1718029       5.0627323       0.0000004
                  PS      -2.9660674       0.5444784      -5.4475397       0.0000001
              lambda       0.1943460       0.0160253      12.1274222       0.0000000
------------------------------------------------------------------------------------
Warning: Assuming panel is in wide format.
y[:, 0] refers to T0, y[:, 1] refers to T1, etc.
x[:, 0:T] refers to T periods of k1, x[:, T+1:2T] refers to k2, etc.
================================ END OF REPORT =====================================
[9]:
numpy.around(fe_error.betas, decimals=4)
[9]:
array([[ 0.8698],
       [-2.9661],
       [ 0.1943]])