def sur_joinrho(n_eq, bigK, bSUR, varb): """Test on joint significance of spatial autoregressive coefficient in SUR Parameters ---------- n_eq : integer, number of equations bigK : n_eq x 1 array with number of variables by equation (includes constant term, exogenous and endogeneous and spatial lag) bSUR : dictionary with regression coefficients by equation, with the spatial autoregressive term as last varb : variance-covariance matrix for regression coefficients Returns ------- : tuple with test statistic, degrees of freedom, p-value """ bb = sur_dict2mat(bSUR) R = np.zeros((n_eq, varb.shape[0])) q = np.zeros((n_eq, 1)) kc = -1 for i in range(n_eq): kc = kc + bigK[i] R[i, kc] = 1 w, p = wald_test(bb, R, q, varb) return (w, n_eq, p)
def sur_chow(n_eq, bigK, bSUR, varb): """test on constancy of regression coefficients across equations in a SUR specification Note: requires a previous check on constancy of number of coefficients across equations; no other checks are carried out, so it is possible that the results are meaningless if the variables are not listed in the same order in each equation. Parameters ---------- n_eq : integer, number of equations bigK : array with the number of variables by equation (includes constant) bSUR : dictionary with the SUR regression coefficients by equation varb : array with the variance-covariance matrix for the SUR regression coefficients Returns ------- test : a list with for each coefficient (in order) a tuple with the value of the test statistic, the degrees of freedom, and the p-value """ kr = bigK[0][0] test = [] bb = sur_dict2mat(bSUR) kf = 0 nr = n_eq df = n_eq - 1 for i in range(kr): Ri = buildR1var(i, kr, kf, 0, nr) tt, p = wald_test(bb, Ri, np.zeros((df, 1)), varb) test.append((tt, df, p)) return test
def sur_chow(n_eq,bigK,bSUR,varb): """test on constancy of regression coefficients across equations in a SUR specification Note: requires a previous check on constancy of number of coefficients across equations; no other checks are carried out, so it is possible that the results are meaningless if the variables are not listed in the same order in each equation. Parameters ---------- n_eq : integer, number of equations bigK : array with the number of variables by equation (includes constant) bSUR : dictionary with the SUR regression coefficients by equation varb : array with the variance-covariance matrix for the SUR regression coefficients Returns ------- test : a list with for each coefficient (in order) a tuple with the value of the test statistic, the degrees of freedom, and the p-value """ kr = bigK[0][0] test = [] bb = sur_dict2mat(bSUR) kf = 0 nr = n_eq df = n_eq - 1 for i in range(kr): Ri = buildR1var(i,kr,kf,0,nr) tt,p = wald_test(bb,Ri,np.zeros((df,1)),varb) test.append((tt,df,p)) return test
def sur_joinrho(n_eq,bigK,bSUR,varb): """Test on joint significance of spatial autoregressive coefficient in SUR Parameters ---------- n_eq : integer, number of equations bigK : n_eq x 1 array with number of variables by equation (includes constant term, exogenous and endogeneous and spatial lag) bSUR : dictionary with regression coefficients by equation, with the spatial autoregressive term as last varb : variance-covariance matrix for regression coefficients Returns ------- : tuple with test statistic, degrees of freedom, p-value """ bb = sur_dict2mat(bSUR) R = np.zeros((n_eq,varb.shape[0])) q = np.zeros((n_eq,1)) kc = -1 for i in range(n_eq): kc = kc + bigK[i] R[i,kc] = 1 w,p = wald_test(bb,R,q,varb) return (w,n_eq,p)
def __init__(self,bigy,bigX,w,nonspat_diag=True,spat_diag=False,vm=False,\ epsilon=0.0000001,\ name_bigy=None,name_bigX=None,name_ds=None,name_w=None): #need checks on match between bigy, bigX dimensions # moved init here BaseSURerrorML.__init__(self,bigy=bigy,bigX=bigX,w=w,epsilon=epsilon) # check on variable names for listing results self.name_ds = USER.set_name_ds(name_ds) self.name_w = USER.set_name_w(name_w, w) #initialize names - should be generated by sur_stack if name_bigy: self.name_bigy = name_bigy else: # need to construct y names self.name_bigy = {} for r in range(self.n_eq): yn = 'dep_var_' + str(r) self.name_bigy[r] = yn if name_bigX: self.name_bigX = name_bigX else: # need to construct x names self.name_bigX = {} for r in range(self.n_eq): k = self.bigX[r].shape[1] - 1 name_x = ['var_' + str(i + 1) + "_" + str(r+1) for i in range(k)] ct = 'Constant_' + str(r+1) # NOTE: constant always included in X name_x.insert(0, ct) self.name_bigX[r] = name_x #inference self.sur_inf = sur_setp(self.bSUR,self.varb) # adjust concentrated log lik for constant const = -self.n2 * (self.n_eq * (1.0 + np.log(2.0*np.pi))) self.errllik = const + self.clikerr self.surerrllik = const + self.cliksurerr # LR test on off-diagonal sigma if nonspat_diag: M = self.n_eq * (self.n_eq - 1)/2.0 likrodiag = 2.0 * (self.surerrllik - self.errllik) plik1 = stats.chisqprob(likrodiag, M) self.lrtest = (likrodiag,int(M),plik1) else: self.lrtest = None # LR test on spatial autoregressive coefficients if spat_diag: liklambda = 2.0 * (self.surerrllik - self.llik) plik2 = stats.chisqprob(liklambda, self.n_eq) self.likrlambda = (liklambda,self.n_eq,plik2) else: self.likrlambda = None # asymptotic variance for spatial coefficient if vm: self.vm = surerrvm(self.n,self.n_eq,w,self.lamsur,self.sig) vlam = self.vm[:self.n_eq,:self.n_eq] self.lamsetp = lam_setp(self.lamsur,vlam) # test on constancy of lambdas R = buildR(kr=1,kf=0,nr=self.n_eq) w,p = wald_test(self.lamsur,R,np.zeros((R.shape[0],1)),vlam) self.lamtest = (w,R.shape[0],p) if spat_diag: # test on joint significance of lambdas Rj = np.identity(self.n_eq) wj,pj = wald_test(self.lamsur,Rj,np.zeros((Rj.shape[0],1)),vlam) self.joinlam = (wj,Rj.shape[0],pj) else: self.joinlam = None else: self.vm = None self.lamsetp = None self.lamtest = None self.joinlam = None # test on constancy of regression coefficients across equations if check_k(self.bigK): # only for equal number of variables self.surchow = sur_chow(self.n_eq,self.bigK,self.bSUR,self.varb) else: self.surchow = None # listing of results self.title = "SEEMINGLY UNRELATED REGRESSIONS (SUR) - SPATIAL ERROR MODEL" SUMMARY.SUR(reg=self, nonspat_diag=nonspat_diag, spat_diag=spat_diag, lambd=True)