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


Spatial Panel Models with Fixed Effects

[1]:
import numpy
import libpysal
import spreg

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.

[2]:
# 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\]
[ ]:
fe_lag = spreg.Panel_FE_Lag(y, x, w, name_y=name_y, name_x=name_x, name_ds="NAT")
[4]:
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 =====================================
[5]:
numpy.around(fe_lag.betas, decimals=4)
[5]:
array([[ 0.8006],
       [-2.6004],
       [ 0.1903]])

Data can also be in ‘long’ format:

[6]:
y_long = y.reshape((y.shape[0]*y.shape[1],1), order='F')
x_long = x.reshape((x.shape[0]*3,2), order='F')

fe_lag_long = spreg.Panel_FE_Lag(y_long, x_long, w, name_y=name_y, name_x=name_x, name_ds="NAT")
print(fe_lag_long.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 long format.
y[0:N] refers to T0, y[N+1:2N] refers to T1, etc.
x[0:N] refers to T0, x[N+1:2N] refers to T1, etc.
================================ END OF REPORT =====================================

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.5444783      -5.4475397       0.0000001
              lambda       0.1943460       0.0160253      12.1274197       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]])