Esempio n. 1
0
 def test_model(self):
     # Only spatial lag
     reg = HET.GM_Combo_Het(self.y, self.X, w=self.w, step1c=True)
     betas = np.array([[ 57.7778574 ], [  0.73034922], [ -0.59257362], [ -0.2230231 ], [  0.56636724]])
     np.testing.assert_array_almost_equal(reg.betas,betas,7)
     u = np.array([ 25.65156033])
     np.testing.assert_array_almost_equal(reg.u[0],u,7)
     ef = np.array([ 31.87664403])
     np.testing.assert_array_almost_equal(reg.e_filtered[0],ef,7)
     ep = np.array([ 28.30648145])
     np.testing.assert_array_almost_equal(reg.e_pred[0],ep,7)
     pe = np.array([ 52.16052155])
     np.testing.assert_array_almost_equal(reg.predy_e[0],pe,7)
     predy = np.array([ 54.81544267])
     np.testing.assert_array_almost_equal(reg.predy[0],predy,7)
     n = 49
     self.assertAlmostEqual(reg.n,n)
     k = 4
     self.assertAlmostEqual(reg.k,k)
     y = np.array([ 80.467003])
     np.testing.assert_array_almost_equal(reg.y[0],y,7)
     x = np.array([  1.     ,  19.531  ,  15.72598])
     np.testing.assert_array_almost_equal(reg.x[0].toarray()[0],x,7)
     yend = np.array([ 35.4585005])
     np.testing.assert_array_almost_equal(reg.yend[0],yend,7)
     q = np.array([ 18.594    ,  24.7142675])
     np.testing.assert_array_almost_equal(reg.q[0].toarray()[0],q,7)
     z = np.array([  1.       ,  19.531    ,  15.72598  ,  35.4585005])
     np.testing.assert_array_almost_equal(reg.z[0].toarray()[0],z,7)
     i_s = 'Maximum number of iterations reached.'
     np.testing.assert_string_equal(reg.iter_stop,i_s)
     its = 1
     self.assertAlmostEqual(reg.iteration,its,7)
     my = 38.436224469387746
     self.assertAlmostEqual(reg.mean_y,my)
     stdy = 18.466069465206047
     self.assertAlmostEqual(reg.std_y,stdy)
     vm = np.array([[  4.86218274e+02,  -2.77268729e+00,  -1.59987770e+00,
          -1.01969471e+01,   2.74302006e+00],
        [ -2.77268729e+00,   1.04680972e-01,   2.51172238e-02,
           1.95136385e-03,   3.70052723e-03],
        [ -1.59987770e+00,   2.51172238e-02,   2.15655720e-02,
           7.65868344e-03,  -7.30173070e-03],
        [ -1.01969471e+01,   1.95136385e-03,   7.65868344e-03,
           2.78273684e-01,  -6.89402590e-02],
        [  2.74302006e+00,   3.70052723e-03,  -7.30173070e-03,
          -6.89402590e-02,   7.12034037e-02]])
     np.testing.assert_array_almost_equal(reg.vm,vm,6)
     pr2 = 0.3001582877472412
     self.assertAlmostEqual(reg.pr2,pr2,7)
     pr2_e = 0.35613102283621967
     self.assertAlmostEqual(reg.pr2_e,pr2_e,7)
     std_err = np.array([ 22.05035768,  0.32354439,  0.14685221,  0.52751653,  0.26683966])
     np.testing.assert_array_almost_equal(reg.std_err,std_err,6)
     z_stat = np.array([(2.6202684885795335, 0.00878605635338265), (2.2573385444145524, 0.023986928627746887), (-4.0351698589183433, 5.456281036278686e-05), (-0.42277935292121521, 0.67245625315942159), (2.1225002455741895, 0.033795752094112265)])
     np.testing.assert_array_almost_equal(reg.z_stat,z_stat,6)
     hth = np.array([[  4.90000000e+01,   7.04371999e+02,   1.72131237e+03,
           7.24743592e+02,   1.70735413e+03],
        [  7.04371999e+02,   1.16866734e+04,   2.15575320e+04,
           1.10925200e+04,   2.23848036e+04],
        [  1.72131237e+03,   2.15575320e+04,   7.39058986e+04,
           2.34796298e+04,   6.70145378e+04],
        [  7.24743592e+02,   1.10925200e+04,   2.34796298e+04,
           1.16146226e+04,   2.30304624e+04],
        [  1.70735413e+03,   2.23848036e+04,   6.70145378e+04,
           2.30304624e+04,   6.69879858e+04]])
     np.testing.assert_array_almost_equal(reg.hth,hth,4)
def autospace(y,
              x,
              w,
              gwk,
              opvalue=0.01,
              combo=False,
              name_y=None,
              name_x=None,
              name_w=None,
              name_gwk=None,
              name_ds=None):
    """
    Runs automatic spatial regression using decision tree
    
    Accounts for both heteroskedasticity and spatial autocorrelation
    
    No endogenous variables
    
    Parameters
    ----------
    y            : array
                   nx1 array for dependent variable
    x            : array
                   Two dimensional array with n rows and one column for each
                   independent (exogenous) variable, excluding the constant
    w            : pysal W object
                   Spatial weights object 
    gwk          : pysal W object
                   Kernel spatial weights needed for HAC estimation. Note:
                   matrix must have ones along the main diagonal.
    opvalue      : real
                   p-value to be used in tests; default: opvalue = 0.01
    combo        : boolean
                   flag for use of combo model rather than HAC for lag-error
                   model; default: combo = False
                   
    Returns
    -------
    results      : a dictionary with
                   results['final model']: one of
                        No Space - Homoskedastic
                        No Space - Heteroskedastic
                        Spatial Lag - Homoskedastic
                        Spatial Lag - Heteroskedastic
                        Spatial Error - Homoskedastic
                        Spatial Error - Heteroskedastic
                        Spatial Lag with Spatial Error - HAC
                        Spatial Lag with Spatial Error - Homoskedastic
                        Spatial Lag with Spatial Error - Heteroskedastic
                        Robust Tests not Significant - Check Model
                   results['heteroskedasticity']: True or False
                   results['spatial lag']: True or False
                   results['spatial error']: True or False
                   results['regression1']: regression object with base model (OLS)
                   results['regression2']: regression object with final model
    """
    results = {}
    results['spatial error'] = False
    results['spatial lag'] = False
    r1 = ols.OLS(y,
                 x,
                 w=w,
                 gwk=gwk,
                 spat_diag=True,
                 name_y=name_y,
                 name_x=name_x,
                 name_w=name_w,
                 name_gwk=name_gwk,
                 name_ds=name_ds)
    results['regression1'] = r1
    Het = r1.koenker_bassett['pvalue']
    if Het < opvalue:
        Hetflag = True
    else:
        Hetflag = False
    results['heteroskedasticity'] = Hetflag
    model = lmChoice(r1, opvalue)
    if model == "MIXED":
        if not combo:
            r2 = twosls_sp.GM_Lag(y,
                                  x,
                                  w=w,
                                  gwk=gwk,
                                  robust='hac',
                                  name_y=name_y,
                                  name_x=name_x,
                                  name_w=name_w,
                                  name_gwk=name_gwk,
                                  name_ds=name_ds)
            results['final model'] = "Spatial Lag with Spatial Error - HAC"
        elif Hetflag:
            r2 = error_sp_het.GM_Combo_Het(y,
                                           x,
                                           w=w,
                                           name_y=name_y,
                                           name_x=name_x,
                                           name_w=name_w,
                                           name_ds=name_ds)
            results[
                'final model'] = "Spatial Lag with Spatial Error - Heteroskedastic"
        else:
            r2 = error_sp_hom.GM_Combo_Hom(y,
                                           x,
                                           w=w,
                                           name_y=name_y,
                                           name_x=name_x,
                                           name_w=name_w,
                                           name_ds=name_ds)
            results[
                'final model'] = "Spatial Lag with Spatial Error - Homoskedastic"
    elif model == "ERROR":
        results['spatial error'] = True
        if Hetflag:
            r2 = error_sp_het.GM_Error_Het(y,
                                           x,
                                           w,
                                           name_y=name_y,
                                           name_x=name_x,
                                           name_w=name_w,
                                           name_ds=name_ds)
            results['final model'] = "Spatial Error - Heteroskedastic"
        else:
            r2 = error_sp_hom.GM_Error_Hom(y,
                                           x,
                                           w,
                                           name_y=name_y,
                                           name_x=name_x,
                                           name_w=name_w,
                                           name_ds=name_ds)
            results['final model'] = "Spatial Error - Homoskedastic"
    elif model == "LAG":
        results['spatial lag'] = True
        if Hetflag:
            r2 = twosls_sp.GM_Lag(y,
                                  x,
                                  w=w,
                                  robust='white',
                                  name_y=name_y,
                                  name_x=name_x,
                                  name_w=name_w,
                                  name_ds=name_ds)
            results['final model'] = "Spatial Lag - Heteroskedastic"
        else:
            r2 = twosls_sp.GM_Lag(y,
                                  x,
                                  w=w,
                                  name_y=name_y,
                                  name_x=name_x,
                                  name_w=name_w,
                                  name_ds=name_ds)
            results['final model'] = "Spatial Lag - Homoskedastic"
    else:
        if Hetflag:
            r2 = ols.OLS(y,
                         x,
                         robust='white',
                         name_y=name_y,
                         name_x=name_x,
                         name_ds=name_ds)
            results['final model'] = "No Space - Heteroskedastic"
        else:
            r2 = r1
            results['final model'] = "No Space - Homoskedastic"
    results['regression2'] = r2
    return results