spreg.ML_Error_Regimes¶
-
class
spreg.
ML_Error_Regimes
(y, x, regimes, w=None, constant_regi='many', cols2regi='all', method='full', epsilon=1e-07, regime_err_sep=False, regime_lag_sep=False, cores=False, vm=False, name_y=None, name_x=None, name_w=None, name_ds=None, name_regimes=None)[source]¶ ML estimation of the spatial error model with regimes (note no consistency checks, diagnostics or constants added); [Ans88]
- Parameters
- yarray
nx1 array for dependent variable
- xarray
Two dimensional array with n rows and one column for each independent (exogenous) variable, excluding the constant
- regimeslist
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).
- cols2regilist, ‘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.
- wSparse matrix
Spatial weights sparse matrix
- methodstring
if ‘full’, brute force calculation (full matrix expressions) if ‘ord’, Ord eigenvalue computation if ‘LU’, LU sparse matrix decomposition
- epsilonfloat
tolerance criterion in mimimize_scalar function and inverse_product
- regime_err_sep: boolean
If True, a separate regression is run for each regime.
- regime_lag_sep: boolean
Always False, kept for consistency in function call, ignored.
- coresboolean
Specifies if multiprocessing is to be used Default: no multiprocessing, cores = False Note: Multiprocessing may not work on all platforms.
- vmboolean
if True, include variance-covariance matrix in summary results
- name_ystring
Name of dependent variable for use in output
- name_xlist of strings
Names of independent variables for use in output
- name_wstring
Name of weights matrix for use in output
- name_dsstring
Name of dataset for use in output
- name_regimesstring
Name of regimes variable for use in output
Examples
Open data baltim.dbf using pysal and create the variables matrices and weights matrix.
>>> import numpy as np >>> import libpysal >>> from libpysal.examples import load_example >>> from libpysal.weights import Queen >>> from spreg import ML_Error_Regimes >>> import geopandas as gpd >>> np.set_printoptions(suppress=True) #prevent scientific format >>> baltimore = load_example('Baltimore') >>> db = libpysal.io.open(baltimore.get_path("baltim.dbf"),'r') >>> df = gpd.read_file(baltimore.get_path("baltim.shp")) >>> ds_name = "baltim.dbf" >>> y_name = "PRICE" >>> y = np.array(db.by_col(y_name)).T >>> y.shape = (len(y),1) >>> x_names = ["NROOM","AGE","SQFT"] >>> x = np.array([db.by_col(var) for var in x_names]).T >>> w = Queen.from_dataframe(df) >>> w_name = "baltim_q.gal" >>> w.transform = 'r'
Since in this example we are interested in checking whether the results vary by regimes, we use CITCOU to define whether the location is in the city or outside the city (in the county):
>>> regimes = db.by_col("CITCOU")
Now we can run the regression with all parameters:
>>> mlerr = ML_Error_Regimes(y,x,regimes,w=w,name_y=y_name,name_x=x_names, name_w=w_name,name_ds=ds_name,name_regimes="CITCOU") >>> np.around(mlerr.betas, decimals=4) array([[-2.076 ], [ 4.8615], [-0.0295], [ 0.3355], [32.3457], [ 2.8708], [-0.2401], [ 0.799 ], [ 0.6 ]]) >>> "{0:.6f}".format(mlerr.lam) '0.599951' >>> "{0:.6f}".format(mlerr.mean_y) '44.307180' >>> "{0:.6f}".format(mlerr.std_y) '23.606077' >>> np.around(mlerr.vm1, decimals=4) array([[ 0.0053, -0.3643], [ -0.3643, 465.3559]]) >>> np.around(np.diag(mlerr.vm), decimals=4) array([58.7121, 2.5036, 0.0074, 0.0659, 81.9796, 3.2676, 0.0124, 0.0514, 0.0053]) >>> np.around(mlerr.sig2, decimals=4) array([[215.554]]) >>> "{0:.6f}".format(mlerr.logll) '-872.860883' >>> "{0:.6f}".format(mlerr.aic) '1761.721765' >>> "{0:.6f}".format(mlerr.schwarz) '1788.536630' >>> mlerr.title 'MAXIMUM LIKELIHOOD SPATIAL ERROR - REGIMES (METHOD = full)'
- Attributes
- summarystring
Summary of regression results and diagnostics (note: use in conjunction with the print command)
- betasarray
(k+1)x1 array of estimated coefficients (lambda last)
- lamfloat
estimate of spatial autoregressive coefficient Only available in dictionary ‘multi’ when multiple regressions (see ‘multi’ below for details)
- uarray
nx1 array of residuals
- e_filteredarray
nx1 array of spatially filtered residuals
- predyarray
nx1 array of predicted y values
- ninteger
Number of observations
- kinteger
Number of variables for which coefficients are estimated (including the constant, excluding the rho) Only available in dictionary ‘multi’ when multiple regressions (see ‘multi’ below for details)
- yarray
nx1 array for dependent variable
- xarray
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)
- methodstring
log Jacobian method. if ‘full’: brute force (full matrix computations) if ‘ord’, Ord eigenvalue computation if ‘LU’, LU sparse matrix decomposition
- epsilonfloat
tolerance criterion used in minimize_scalar function and inverse_product
- mean_yfloat
Mean of dependent variable
- std_yfloat
Standard deviation of dependent variable
- vmarray
Variance covariance matrix (k+1 x k+1), all coefficients
- vm1array
variance covariance matrix for lambda, sigma (2 x 2) Only available in dictionary ‘multi’ when multiple regressions (see ‘multi’ below for details)
- sig2float
Sigma squared used in computations Only available in dictionary ‘multi’ when multiple regressions (see ‘multi’ below for details)
- logllfloat
maximized log-likelihood (including constant terms) Only available in dictionary ‘multi’ when multiple regressions (see ‘multi’ below for details)
- pr2float
Pseudo R squared (squared correlation between y and ypred) Only available in dictionary ‘multi’ when multiple regressions (see ‘multi’ below for details)
- std_errarray
1xk array of standard errors of the betas Only available in dictionary ‘multi’ when multiple regressions (see ‘multi’ below for details)
- z_statlist of tuples
z statistic; each tuple contains the pair (statistic, p-value), where each is a float Only available in dictionary ‘multi’ when multiple regressions (see ‘multi’ below for details)
- name_ystring
Name of dependent variable for use in output
- name_xlist of strings
Names of independent variables for use in output
- name_wstring
Name of weights matrix for use in output
- name_dsstring
Name of dataset for use in output
- name_regimesstring
Name of regimes variable for use in output
- titlestring
Name of the regression method used Only available in dictionary ‘multi’ when multiple regressions (see ‘multi’ below for details)
- regimeslist
List of n values with the mapping of each observation to a regime. Assumed to be aligned with ‘x’.
- constant_regi: string
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).
- cols2regilist, ‘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_lag_sep: boolean
If True, the spatial parameter for spatial lag is also computed according to different regimes. If False (default), the spatial parameter is fixed accross regimes.
- krint
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)
- kfint
Number of variables/columns to be considered fixed or global across regimes and hence only obtain one parameter estimate
- nrint
Number of different regimes in the ‘regimes’ list
- multidictionary
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
-
__init__
(y, x, regimes, w=None, constant_regi='many', cols2regi='all', method='full', epsilon=1e-07, regime_err_sep=False, regime_lag_sep=False, cores=False, vm=False, name_y=None, name_x=None, name_w=None, name_ds=None, name_regimes=None)[source]¶ Initialize self. See help(type(self)) for accurate signature.
Methods
__init__
(y, x, regimes[, w, constant_regi, …])Initialize self.
get_x_lag
(w, regimes_att)Attributes
-
get_x_lag
(w, regimes_att)¶
-
property
mean_y
¶
-
property
sig2n
¶
-
property
sig2n_k
¶
-
property
std_y
¶
-
property
utu
¶
-
property
vm
¶