import pysal as ps import numpy as np db = ps.open('../workshop_data/phx.csv') len(db) #How many observations db.header #Names of the columns db[0, :] #First row db[0:5, 0] #First six elements of the first column pdens = db.by_col('pop_dens') pdens[0:5] #First six elements of the population density column min(pdens) #Minimum value max(pdens) #Maximum value np.mean(pdens) #Average value np.var(pdens) #Variance y = np.array(db.by_col('l_pct_err')).reshape((len(db), 1)) x_names = ['hsu', 'pop_dens', 'white_rt', 'black_rt', 'hisp_rt', \ 'fem_nh_rt', 'renter_rt', 'vac_hsu_rt'] x = np.array([db.by_col(var) for var in x_names]).T w = ps.open('../workshop_data/phx_knn06.gwt').read() w.transform = 'R' from pysal.spreg import OLS model = OLS(y, x, w=w, name_x=x_names, spat_diag=True) print model.summary from pysal.spreg import OLS model = OLS(y, x, w=w, name_x=x_names, spat_diag=False, nonspat_diag=False, robust='white') print model.summary from pysal.spreg.user_output import summary_spat_diag model = OLS(y, x, w=w, name_x=x_names, spat_diag=True) spd = summary_spat_diag(model, None, None) print spd from pysal.spreg import GM_Error error_kp98 = GM_Error(y, x, w, name_x=x_names) print error_kp98.summary from pysal.spreg import GM_Error_Hom error_hom = GM_Error_Hom(y, x, w, name_x=x_names) print error_hom.summary from pysal.spreg import GM_Error_Het error_het = GM_Error_Het(y, x, w, name_x=x_names) print error_het.summary from pysal.spreg import GM_Lag lag_model = GM_Lag(y, x, w=w, name_x=x_names, spat_diag=True) print lag_model.summary lag_model2lags = GM_Lag(y, x, w=w, name_x=x_names, w_lags=2) print lag_model2lags.summary from pysal import lag_spatial from pysal.spreg import TSLS wy = lag_spatial(w, y) #Get the spatial lag of y wx = lag_spatial(w, x) #Get the lag of x to use as instruments iv_model = TSLS(y, x, yend=wy, q=wx, name_x=x_names) print iv_model.summary from pysal.spreg import GM_Combo_Het sarar_het = GM_Combo_Het(y, x, w=w, name_x=x_names) print sarar_het.summary lag_white = GM_Lag(y, x, w=w, name_x=x_names, robust='white') print lag_white.summary wk = ps.open('../workshop_data/phx_k_triangular.kwt', dataFormat='gwt').read() lag_hac = GM_Lag(y, x, w=w, name_x=x_names, robust='hac', gwk=wk) print lag_hac.summary #Pull out the X variables without including population density x_names = ['hsu', 'white_rt', 'black_rt', 'hisp_rt', \ 'fem_nh_rt', 'renter_rt', 'vac_hsu_rt'] x = np.array([db.by_col(var) for var in x_names]).T #Create inverse of land area area = np.array(db.by_col('ALAND10')).reshape((x.shape[0], 1)) inv_area = 1. / area #Create the matrix for the instruments q_names = ['inv_area'] q = inv_area #Create the matrix for the endogenous variable yend_names = ['pop_dens'] yend = np.array([db.by_col(var) for var in yend_names]).T #Non-spatial model (w only required for spatial diagnostics model = ps.spreg.TSLS(y, x, w=w, yend=yend, q=q, \ name_x=x_names, name_yend=yend_names, name_q=q_names) #KP98-99 Error model model = ps.spreg.GM_Endog_Error(y, x, w=w, yend=yend, q=q, \ name_x=x_names, name_yend=yend_names, name_q=q_names) #Drukker et al. error model (Hom) model = ps.spreg.GM_Endog_Error_Hom(y, x, w=w, yend=yend, q=q, \ name_x=x_names, name_yend=yend_names, name_q=q_names) #Arraiz. et al. error model (Het) model = ps.spreg.GM_Endog_Error_Het(y, x, w=w, yend=yend, q=q, \ name_x=x_names, name_yend=yend_names, name_q=q_names) #Lag model model = ps.spreg.GM_Lag(y, x, w=w, yend=yend, q=q, \ name_x=x_names, name_yend=yend_names, name_q=q_names) #Combo model model = ps.spreg.GM_Combo_Het(y, x, w=w, yend=yend, q=q, \ name_x=x_names, name_yend=yend_names, name_q=q_names) help(ps.spreg.OLS) #Import the LM tests method from pysal.spreg import LMtests #Specify the files for the weights we want to try as a list w_files = ['../workshop_data/phx_knn06.gwt', \ '../workshop_data/phx_knn10.gwt', \ '../workshop_data/phx_rook.gal', \ '../workshop_data/phx_queen.gal'] #Run the OLS model = ps.spreg.OLS(y, x, spat_diag=False, nonspat_diag=False) #Setup the loop over the weights files for w_file in w_files: print 'File: ', w_file w = ps.open(w_file).read() lms = LMtests(model, w) print '\tLM error: %.4f\t(%.4f)'%lms.lme print '\tLM lag: %.4f\t(%.4f)'%lms.lml print '\tSARMA: %.4f\t(%.4f)'%lms.sarma print '\tRobust LM error: %.4f\t(%.4f)'%lms.rlme print '\tRobust LM lag: %.4f\t(%.4f)'%lms.rlml