spreg.TSLS_Regimes¶
- class spreg.TSLS_Regimes(y, x, yend, q, regimes, w=None, robust=None, gwk=None, slx_lags=0, slx_vars='all', sig2n_k=True, spat_diag=False, vm=False, constant_regi='many', cols2regi='all', regime_err_sep=False, name_y=None, name_x=None, cores=False, name_yend=None, name_q=None, name_regimes=None, name_w=None, name_gwk=None, name_ds=None, summ=True, latex=False)[source]¶
Two stage least squares (2SLS) with regimes.
- Parameters:
- y
numpy.ndarrayorpandas.Series nx1 array for dependent variable
- x
numpy.ndarrayorpandasobject Two dimensional array with n rows and one column for each independent (exogenous) variable, excluding the constant
- yend
numpy.ndarrayorpandasobject Two dimensional array with n rows and one column for each endogenous variable
- q
numpy.ndarrayorpandasobject Two dimensional array with n rows and one column for each external exogenous variable to use as instruments (note: this should not contain any variables from x)
- regimes
listorpandas.Series List of n values with the mapping of each observation to a regime. Assumed to be aligned with ‘x’.
- constant_regi: string
Switcher controlling the constant term setup. It may take the following values:
‘one’: a vector of ones is appended to x and held constant across regimes.
‘many’: a vector of ones is appended to x and considered different per regime (default).
- cols2regi
list, ‘all’ Argument indicating whether each column of x should be considered as different per regime or held constant across regimes (False). If a list, k booleans indicating for each variable the option (True if one per regime, False to be held constant). If ‘all’ (default), all the variables vary by regime.
- regime_err_sep: boolean
If True, a separate regression is run for each regime.
- robust
str If ‘white’, then a White consistent estimator of the variance-covariance matrix is given. If ‘hac’, then a HAC consistent estimator of the variance-covariance matrix is given. If ‘ogmm’, then Optimal GMM is used to estimate betas and the variance-covariance matrix. Default set to None.
- gwk
pysalWobject Kernel spatial weights needed for HAC estimation. Note: matrix must have ones along the main diagonal.
- 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. Note: WX is computed using the complete weights matrix
- slx_vars
either“all” (default)orlistofbooleanstoselectxvariables to be lagged
- sig2n_kbool
If True, then use n-k to estimate sigma^2. If False, use n.
- vmbool
If True, include variance-covariance matrix in summary
- coresbool
Specifies if multiprocessing is to be used Default: no multiprocessing, cores = False Note: Multiprocessing may not work on all platforms.
- name_y
str Name of dependent variable for use in output
- name_x
listofstrings Names of independent variables for use in output
- name_yend
listofstrings Names of endogenous variables for use in output
- name_q
listofstrings Names of instruments for use in output
- name_regimes
str Name of regimes variable for use in output
- name_w
str Name of weights matrix for use in output
- name_gwk
str Name of kernel weights matrix for use in output
- name_ds
str Name of dataset for use in output
- latexbool
Specifies if summary is to be printed in latex format
- y
- Attributes:
- output
dataframe regression results pandas dataframe
- summary
str Summary of regression results and diagnostics (note: use in conjunction with the print command)
- betas
array kx1 array of estimated coefficients
- u
array nx1 array of residuals
- predy
array nx1 array of predicted y values
- n
integer Number of observations
- y
array nx1 array for dependent variable
- x
array Two dimensional array with n rows and one column for each independent (exogenous) variable, including the constant Only available in dictionary ‘multi’ when multiple regressions (see ‘multi’ below for details)
- yend
array Two dimensional array with n rows and one column for each endogenous variable Only available in dictionary ‘multi’ when multiple regressions (see ‘multi’ below for details)
- q
array Two dimensional array with n rows and one column for each external exogenous variable used as instruments Only available in dictionary ‘multi’ when multiple regressions (see ‘multi’ below for details)
- vm
array Variance covariance matrix (kxk)
- regimes
list List of n values with the mapping of each observation to a regime. Assumed to be aligned with ‘x’.
- constant_regi: [False, ‘one’, ‘many’]
Ignored if regimes=False. Constant option for regimes. Switcher controlling the constant term setup. It may take the following values:
‘one’: a vector of ones is appended to x and held constant across regimes.
‘many’: a vector of ones is appended to x and considered different per regime (default).
- cols2regi
list, ‘all’ Ignored if regimes=False. Argument indicating whether each column of x should be considered as different per regime or held constant across regimes (False). If a list, k booleans indicating for each variable the option (True if one per regime, False to be held constant). If ‘all’, all the variables vary by regime.
- regime_err_sep: boolean
If True, a separate regression is run for each regime.
- kr
int Number of variables/columns to be “regimized” or subject to change by regime. These will result in one parameter estimate by regime for each variable (i.e. nr parameters per variable)
- kf
int Number of variables/columns to be considered fixed or global across regimes and hence only obtain one parameter estimate
- nr
int Number of different regimes in the ‘regimes’ list
- name_y
str Name of dependent variable for use in output
- name_x
listofstrings Names of independent variables for use in output
- name_yend
listofstrings Names of endogenous variables for use in output
- name_q
listofstrings Names of instruments for use in output
- name_regimes
str Name of regimes variable for use in output
- name_w
str Name of weights matrix for use in output
- name_gwk
str Name of kernel weights matrix for use in output
- name_ds
str Name of dataset for use in output
- multi
dictionary Only available when multiple regressions are estimated, i.e. when regime_err_sep=True and no variable is fixed across regimes. Contains all attributes of each individual regression
- output
Examples
We first need to import the needed modules, namely numpy to convert the data we read into arrays that
spregunderstands andpysalto perform all the analysis.>>> import numpy as np >>> import libpysal >>> from libpysal.examples import load_example >>> from libpysal.weights import Rook >>> import geopandas as gpd
Open data on NCOVR US County Homicides (3085 areas) using geopandas.
>>> nat = load_example('Natregimes') >>> db = gpd.read_file(nat.get_path('natregimes.shp'))
Select the HR90 column (homicide rates in 1990) as the dependent variable for the regression.
>>> y = db['HR90']
Select UE90 (unemployment rate) and PS90 (population structure) variables as independent variables in the regression.
>>> x = db[['PS90','UE90']]
In this case we consider RD90 (resource deprivation) as an endogenous regressor. We tell the model that this is so by passing it in a different parameter from the exogenous variables (x).
>>> yd = db['RD90']
Because we have endogenous variables, to obtain a correct estimate of the model, we need to instrument for RD90. We use FP89 (families below poverty) for this and hence put it in the instruments parameter, ‘q’.
>>> q = db[['FP89']]
The different regimes in this data are given according to the North and South dummy (SOUTH).
>>> r_var = db['SOUTH']
Since we want to perform tests for spatial dependence, we need to specify the spatial weights matrix that includes the spatial configuration of the observations into the error component of the model. To do that, we can open an already existing gal file or create a new one. In this case, we will create one from our geopandas dataframe.
>>> w = Rook.from_dataframe(db, use_index=True)
Unless there is a good reason not to do it, the weights have to be row-standardized so every row of the matrix sums to one. Among other things, this allows to interpret the spatial lag of a variable as the average value of the neighboring observations. In PySAL, this can be easily performed in the following way:
>>> w.transform = 'r'
We can now run the regression and then have a summary of the output by typing: model.summary Alternatively, we can just check the betas and standard errors of the parameters:
>>> from spreg import TSLS_Regimes >>> tslsr = TSLS_Regimes(y, x, yd, q, r_var, w=w, spat_diag=True, name_regimes='SOUTH', name_ds='NAT', name_w='NAT.shp')
>>> tslsr.betas array([[ 3.66973562], [ 1.06950466], [ 0.14680946], [ 9.55873243], [ 1.94666348], [-0.30810214], [ 2.45864196], [ 3.68718119]])
>>> np.sqrt(tslsr.vm.diagonal()) array([0.46522151, 0.12074672, 0.05661795, 0.41893265, 0.16721773, 0.06631022, 0.27538921, 0.21745974])
>>> print(tslsr.summary) REGRESSION RESULTS ------------------ SUMMARY OF OUTPUT: TWO STAGE LEAST SQUARES - REGIMES ---------------------------------------------------- Data set : NAT Weights matrix : NAT.shp Dependent Variable : HR90 Number of Observations: 3085 Mean dependent var : 6.1829 Number of Variables : 8 S.D. dependent var : 6.6414 Degrees of Freedom : 3077 Pseudo R-squared : 0.4246 ------------------------------------------------------------------------------------ Variable Coefficient Std.Error z-Statistic Probability ------------------------------------------------------------------------------------ 0_CONSTANT 3.66974 0.46522 7.88815 0.00000 0_PS90 1.06950 0.12075 8.85742 0.00000 0_UE90 0.14681 0.05662 2.59298 0.00951 0_RD90 2.45864 0.27539 8.92788 0.00000 1_CONSTANT 9.55873 0.41893 22.81687 0.00000 1_PS90 1.94666 0.16722 11.64149 0.00000 1_UE90 -0.30810 0.06631 -4.64637 0.00000 1_RD90 3.68718 0.21746 16.95570 0.00000 ------------------------------------------------------------------------------------ Instrumented: 0_RD90, 1_RD90 Instruments: 0_FP89, 1_FP89 Regimes variable: SOUTH REGIMES DIAGNOSTICS - CHOW TEST VARIABLE DF VALUE PROB CONSTANT 1 88.485 0.0000 PS90 1 18.086 0.0000 UE90 1 27.220 0.0000 RD90 1 12.258 0.0005 Global test 4 195.994 0.0000 DIAGNOSTICS FOR SPATIAL DEPENDENCE TEST DF VALUE PROB Anselin-Kelejian Test 1 104.255 0.0000 ================================ END OF REPORT =====================================
- __init__(y, x, yend, q, regimes, w=None, robust=None, gwk=None, slx_lags=0, slx_vars='all', sig2n_k=True, spat_diag=False, vm=False, constant_regi='many', cols2regi='all', regime_err_sep=False, name_y=None, name_x=None, cores=False, name_yend=None, name_q=None, name_regimes=None, name_w=None, name_gwk=None, name_ds=None, summ=True, latex=False)[source]¶
Methods
__init__(y, x, yend, q, regimes[, w, ...])Attributes
- property mean_y¶
- property pfora1a2¶
- property sig2n¶
- property sig2n_k¶
- property std_y¶
- property utu¶
- property vm¶