-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDO_spatial_cross_sections.do
206 lines (145 loc) · 6.57 KB
/
DO_spatial_cross_sections.do
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
* SPATIAL ECONOMETRICS with cross-sectional data (Before and after Stata 15)
* Clean your environment
clear all
macro drop _all
set more off
cls
version 15
* Change working directory
* cd "/Users/carlos/Github/QuaRCS-lab/tutorial-spatial-cross-section-columbus-crime"
* Manually download the data (A shapefile that includes the variables) and store it in a data folder
* Source: https://geodacenter.github.io/data-and-lab/columbus/
* Install packages
* ssc install spmap
* ssc install shp2dta
* ssc install sppack
* ssc install spregcs
* Learn more about the following commands
*help spatwmat
*help spreg
*help spmat
*help spregcs
*help spatreg
* Import and unzip data
copy "https://github.com/quarcs-lab/data-open/raw/master/Columbus/columbus/columbus.zip" columbus.zip, replace
unzipfile columbus.zip
* Use the shp2dta package to convert a shape file into 2 stata datasets (dBase and coordinates)
shp2dta using "columbus.shp", database("columbusDbase.dta") coordinates("columbusCoor.dta") genid(id) replace
* Load and describe the stata Dbase file
use "columbusDbase.dta", clear
describe
* Plot a map
*spmap CRIME using "columbusCoor.dta", id(id) legend(size(small) position(11)) clmethod(custom) clbreaks(0 15 30 45 60 75) fcolor(Blues) title("Property crimes per thousand households") note("Columbus, Ohio 1980 neighorhood data" "Source: Anselin (1988)")
*graph save "mapCrime.gph", replace
*graph export "mapCrime.png", replace
* Generate spatial weights matrices with spmat package
spmat contiguity Wqueen using "columbusCoor.dta", id(id)
spmat contiguity WqueenS using "columbusCoor.dta", id(id) normalize(row)
* Summarize spatial weights matrices
spmat summarize Wqueen
spmat summarize Wqueen, links
spmat summarize WqueenS
spmat summarize WqueenS, links
* Export matrix from an spmat object to a Mata object
spmat getmatrix Wqueen mataWqueen
spmat getmatrix WqueenS mataWqueenS
* Display Mata matrices
mata
mataWqueen
mataWqueenS
end
*** Export W matrix (created with spmat) to stata file (that is, .dta)
* Export weight matrix to .txt file (with no id column)
spmat export Wqueen using "Wqueen_fromStata_spmat.txt", noid replace
spmat export WqueenS using "WqueenS_fromStata_spmat.txt", noid replace
* Import .txt weight matrix and save it at .dta (stata) data file
import delimited "Wqueen_fromStata_spmat.txt", delimiter(space) rowrange(2) clear
save "Wqueen_fromStata_spmat.dta", replace
import delimited "WqueenS_fromStata_spmat.txt", delimiter(space) rowrange(2) clear
save "WqueenS_fromStata_spmat.dta", replace
* [IMPORTANT] Import .dta weight matrix with spatwmat package
spatwmat using "Wqueen_fromStata_spmat.dta", name(Wqueen_fromStata_spatwmat)
matrix list Wqueen_fromStata_spatwmat
* [IMPORTANT] Import .dta weight matrix with spatwmat package, standardize it, and store eigen values
spatwmat using "Wqueen_fromStata_spmat.dta", name(WqueenS_fromStata_spatwmat) eigenval(eWqueenS_fromStata_spatwmat) standardize
matrix list WqueenS_fromStata_spatwmat
* Import .dta weights matrix with spmatrix (official function from Stata15)
use "Wqueen_fromStata_spmat.dta", clear
gen id = _n
order id, first
spset id
spmatrix fromdata WqueenS_fromStata15 = v*, normalize(row) replace
spmatrix summarize WqueenS_fromStata15
* Global Moran's I of the dependent variable
use "columbusDbase.dta", clear
spatgsa CRIME, w(WqueenS_fromStata_spatwmat) moran
* (0) Fit OLS model: No spatial lags
use "columbusDbase.dta", clear
spset id
regress CRIME INC HOVAL
estat moran, errorlag(WqueenS_fromStata15)
* LM Spatial diagnostics of regression residuals
reg CRIME INC HOVAL
spatdiag, weights(WqueenS_fromStata_spatwmat)
* (1) SAR/SLM: Spatial lag model (using 3 alternative packages)
* using spregress (official function from Stata 15)
use "Wqueen_fromStata_spmat.dta", clear
gen id = _n
order id, first
spset id
spmatrix fromdata WqueenS_fromStata15 = v*, normalize(row) replace
spmatrix summarize WqueenS_fromStata15
use "columbusDbase.dta", clear
spset id
spregress CRIME INC HOVAL, ml dvarlag(WqueenS_fromStata15) vce(robust)
estat ic
estat impact
* using the spregcs package (BE CAREFUL!! it requires a W created with spmat)
spregcs CRIME INC HOVAL, wmfile("Wqueen_fromStata_spmat.dta") model(sar) mfx(lin)
* using the spatreg package (BE CAREFUL!! it requires a W created with spatwmat)
spatreg CRIME INC HOVAL, weights(WqueenS_fromStata_spatwmat) eigenval(eWqueenS_fromStata_spatwmat) model(lag)
* (2) SEM: Spatial error model (using 3 alternative packages)
* using spregress (official function from Stata 15)
use "Wqueen_fromStata_spmat.dta", clear
gen id = _n
order id, first
spset id
spmatrix fromdata WqueenS_fromStata15 = v*, normalize(row) replace
spmatrix summarize WqueenS_fromStata15
use "columbusDbase.dta", clear
spset id
spregress CRIME INC HOVAL, ml errorlag(WqueenS_fromStata15) vce(robust)
estat ic
estat impact
* using the spregcs package (BE CAREFUL!! it requires a W created with spmat)
spregcs CRIME INC HOVAL, wmfile("Wqueen_fromStata_spmat.dta") model(sem) mfx(lin)
* using the spatreg package (BE CAREFUL!! it requires a W created with spatwmat)
spatreg CRIME INC HOVAL, weights(WqueenS_fromStata_spatwmat) eigenval(eWqueenS_fromStata_spatwmat) model(error)
* (3) Fit SLX model: spatial lag of the independent variables
use "Wqueen_fromStata_spmat.dta", clear
gen id = _n
order id, first
spset id
spmatrix fromdata WqueenS_fromStata15 = v*, normalize(row) replace
spmatrix summarize WqueenS_fromStata15
use "columbusDbase.dta", clear
spset id
spregress CRIME INC HOVAL, ml ivarlag(WqueenS_fromStata15: INC HOVAL) vce(robust)
estat ic
estat impact
* (4) Fit SAC model: spatial lag of the dependent and error term
spregress CRIME INC HOVAL, ml dvarlag(WqueenS_fromStata15) ivarlag(WqueenS_fromStata15: INC HOVAL) vce(robust)
estat ic
estat impact
* (5) Fit SDM model: spatial lag of the dependent and independent variables
spregress CRIME INC HOVAL, ml dvarlag(WqueenS_fromStata15) errorlag(WqueenS_fromStata15) vce(robust)
estat ic
estat impact
* (6) Fit SDEM model: spatial lag of the independent variables and error term
spregress CRIME INC HOVAL, ml ivarlag(WqueenS_fromStata15: INC HOVAL) errorlag(WqueenS_fromStata15) vce(robust)
estat ic
estat impact
* (7) Fit GNS model: spatial lag of the dependent, independent, and error terms
spregress CRIME INC HOVAL, ml dvarlag(WqueenS_fromStata15) ivarlag(WqueenS_fromStata15: INC HOVAL) errorlag(WqueenS_fromStata15) vce(robust)
estat ic
estat impact