spreg.ML_LagRE¶
- class spreg.ML_LagRE(y, x, w, slx_lags=0, slx_vars='all', time_effects=False, epsilon=1e-07, spat_impacts='simple', vm=False, name_y=None, name_x=None, name_w=None, name_ds=None, latex=False)[source]¶
ML estimation of the random effects spatial lag model with all results and diagnostics [Elh03].
- Parameters:
- y
numpy.ndarrayorpandasobject nxt or (nxt)x1 array for dependent variable
- x
numpy.ndarrayorpandasobject nx(txk) or (nxt)xk array for independent (exogenous) variables, excluding the constant
- w
pysalWobject Spatial weights object
- slx_lags
integer Number of spatial lags of X to include in the model specification. If slx_lags>0, the specification becomes of the SLX type.
- slx_vars
either“All” (default)orlistofbooleanstoselectxvariables to be lagged
- time_effectsbool
If True, then include time dummies in the model specification (first omitted). Default: False
- spat_impacts
strorlist - Include average direct impact (ADI), average indirect impact (AII),
and average total impact (ATI) in summary results. Options are ‘simple’, ‘full’, ‘power’, ‘all’ or None. See sputils.spmultiplier for more information.
- epsilon
float tolerance criterion in mimimize_scalar function and inverse_product
- vmbool
if True, include variance-covariance matrix in summary results
- name_y
str Name of dependent variable for use in output
- name_x
listofstrings Names of independent variables for use in output
- name_w
str Name of weights matrix for use in output
- name_ds
str Name of dataset for use in output
- y
- Attributes:
- betas
array (k+2)x1 array of estimated coefficients (rho and phi last)
- rho
float estimate of spatial autoregressive coefficient
- phi
float estimate of weight attached to the cross-sectional component phi^2 = sig2 / (t*sig2_u + sig2)
- u
array (nxt)x1 array of residuals
- predy
array (nxt)x1 array of predicted y values
- n
integer Total number of observations
- t
integer Number of time periods
- k
integer Number of variables for which coefficients are estimated (excluding the phi)
- y
array (nxt)x1 array for dependent variable
- x
array Two dimensional array with nxt rows and one column for each independent (exogenous) variable
- mean_y
float Mean of dependent variable
- std_y
float Standard deviation of dependent variable
- vm
array Variance covariance matrix (k+2 x k+2)
- vm1
array Variance covariance matrix (k+3 x k+3) includes sigma2
- sig2
float Sigma squared used in computations
- logll
float maximized log-likelihood (including constant terms)
- predy_e
array predicted values from reduced form
- e_pred
array prediction errors using reduced form predicted values
- pr2
float Pseudo R squared (squared correlation between y and ypred)
- pr2_e
float Pseudo R squared (squared correlation between y and ypred_e (using reduced form))
- utu
float Sum of squared residuals
- std_err
array 1x(k+1) array of standard errors of the betas
- z_stat
listoftuples z statistic; each tuple contains the pair (statistic, p-value), where each is a float
- name_y
str Name of dependent variable for use in output
- name_x
listofstrings Names of independent variables for use in output
- name_w
str Name of weights matrix for use in output
- name_ds
str Name of dataset for use in output
- title
str Name of the regression method used
- betas
Examples
>>> import libpysal >>> import geopandas as gpd >>> from spreg import ML_LagRE
In this example, we will use data on NCOVR US County Homicides (3085 areas) included in libpysal.
>>> libpysal.examples.load_example('NCOVR') >>> db = gpd.read_file(libpysal.examples.get_path("NAT.shp")) >>> name_ds = "NCOVR.shp"
Extract the HR (homicide rates) data in the 70’s, 80’s and 90’s from the dataframe and make it the dependent variable for the regression. Note that the data can also be passed in the long format instead of wide format (i.e. a vector with n*t rows and a single column for the dependent variable and a matrix of dimension n*txk for the independent variables).
>>> y_name = ['HR70','HR80','HR90'] >>> y = db[y_name]
Extract RD and PS in the same time periods from the dataframe to be used as independent variables in the regression. IMPORTANT: The data must be organized in a way that all time periods of a given variable are side-by-side and in the correct time order. That is: x[:, 0:T] refers to T periods of k1, x[:, T+1:2T] refers to k2, etc. By default a vector of ones will be added to the independent variables passed in.
>>> x_names = ['RD70','RD80','RD90','PS70','PS80','PS90'] >>> x = db[x_names]
Since we are running a spatial model, we need to specify the spatial weights matrix that includes the spatial configuration of the observations. To do that, we can open an already existing gal file or create a new one. In this case, we will create one from
NAT.shp.>>> w = libpysal.weights.Queen.from_dataframe(db, use_index=True) >>> w.transform = 'r' >>> name_w = "NCOVR.gal"
We are all set with the preliminaries, we are good to run the model.
>>> reg = ML_LagRE(y, x, w)
Once we have run the model, we can explore a little bit the output with the command print(reg.summary).
- __init__(y, x, w, slx_lags=0, slx_vars='all', time_effects=False, epsilon=1e-07, spat_impacts='simple', vm=False, name_y=None, name_x=None, name_w=None, name_ds=None, latex=False)[source]¶
Methods
__init__(y, x, w[, slx_lags, slx_vars, ...])lag_c_loglik_sp(rho, n, t, e0, e1, I, Wsp)phi_c_loglik(phi, rho, beta, bigy, bigx, n, ...)Attributes
- lag_c_loglik_sp(rho, n, t, e0, e1, I, Wsp)¶
- property mean_y¶
- phi_c_loglik(phi, rho, beta, bigy, bigx, n, t, W_nt)¶
- property sig2n¶
- property sig2n_k¶
- property std_y¶
- property utu¶
- property vm¶