spreg.MoranRes¶
-
class
spreg.
MoranRes
(ols, w, z=False)[source]¶ Moran’s I for spatial autocorrelation in residuals from OLS regression
- Parameters
- olsOLS
OLS regression object
- wW
Spatial weights instance
- zboolean
If set to True computes attributes eI, vI and zI. Due to computational burden of vI, defaults to False.
Examples
>>> import numpy as np >>> import libpysal >>> from spreg import OLS >>> import spreg
Open the csv file to access the data for analysis
>>> csv = libpysal.io.open(libpysal.examples.get_path('columbus.dbf'),'r')
Pull out from the csv the files we need (‘HOVAL’ as dependent as well as ‘INC’ and ‘CRIME’ as independent) and directly transform them into nx1 and nx2 arrays, respectively
>>> y = np.array([csv.by_col('HOVAL')]).T >>> x = np.array([csv.by_col('INC'), csv.by_col('CRIME')]).T
Create the weights object from existing .gal file
>>> w = libpysal.io.open(libpysal.examples.get_path('columbus.gal'), 'r').read()
Row-standardize the weight object (not required although desirable in some cases)
>>> w.transform='r'
Run an OLS regression
>>> ols = OLS(y, x)
Run Moran’s I test for residual spatial autocorrelation in an OLS model. This computes the traditional statistic applying a correction in the expectation and variance to account for the fact it comes from residuals instead of an independent variable
>>> m = spreg.MoranRes(ols, w, z=True)
Value of the Moran’s I statistic:
>>> print(round(m.I,4)) 0.1713
Value of the Moran’s I expectation:
>>> print(round(m.eI,4)) -0.0345
Value of the Moran’s I variance:
>>> print(round(m.vI,4)) 0.0081
Value of the Moran’s I standardized value. This is distributed as a standard Normal(0, 1)
>>> print(round(m.zI,4)) 2.2827
P-value of the standardized Moran’s I value (z):
>>> print(round(m.p_norm,4)) 0.0224
- Attributes
- Ifloat
Moran’s I statistic
- eIfloat
Moran’s I expectation
- vIfloat
Moran’s I variance
- zIfloat
Moran’s I standardized value
Methods
__init__
(ols, w[, z])Initialize self.