-
Notifications
You must be signed in to change notification settings - Fork 6
/
genproximity.ado
executable file
·301 lines (246 loc) · 7.06 KB
/
genproximity.ado
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
*================================================================================
*================================================================================
cap program drop genproximity
program define genproximity
version 10
syntax varlist [if/], i(varlist) p(varlist) [t(varlist) pop(varlist) rca(real -1) rpop(real -1) knn(real -1) cont im(varlist) asym]
marksample touse
tokenize "`varlist'"
local val = "`1'"
// Drop Variables that can confuse the output
local create_variables M RCA rca rpop mcp density eci pci oppval oppgain _fillin id_i id_p
foreach var in `create_variables' {
cap drop `var'
}
if "`t'"=="" {
local t_present 0
tempvar t
qui gen byte `t' = 1
}
else {
local t_present 1
}
*----------------------------------------------------------------------
* M matrix provided? Detecting binary dataset
*----------------------------------------------------------------------
// if main variable is already in a binary format,
// the program will asume it corresponds to the Mcp Matrix
qui sum `val'
local l1 = r(min)
local l2 = r(max)
if (`l1'==0 & `l2'==1) | "`bi'"~="" {
local l0 = 1
noi di ""
noi di "Binary variable detected"
}
else {
local l0 = 0
}
*----------------------------------------------------------------------
sort `t' `i' `p'
cap levelsof `t', local(year_levels)
quietly levelsof `t', local(Nt)
global Nnt: word count `Nt'
display "Generates proximity matrix"
display "________________________________________________________________________________________________"
if `t_present' == 1 & $Nnt > 1 {
display "Number of periods in sample : $Nnt"
display "Calculations for time period :", _c
}
quietly {
cap tempfile newfile1 newfile2 newfile3 newfile4
save "`newfile1'", replace
drop in 1/l
save "`newfile2'", replace emptyok
save "`newfile4'", replace emptyok
}
foreach y of local year_levels{
if `t_present' == 1 & $Nnt > 1 {
display "`y'", _c
}
cap use "`newfile1'", clear
cap keep if `t'==`y'
*================================================================================
quietly{
count if `touse' == 0
local foundzeroes = r(N)
if `foundzeroes' > 0 {
local dropped_zero = 1
preserve
keep if `touse' == 0
save "`newfile3'", emptyok replace
use "`newfile4'", clear
append using "`newfile3'"
save "`newfile4'", replace
restore
drop if `touse' == 0
}
}
*================================================================================
if ("`pop'" ~= "" & `rpop' ~= -1) {
local calculate_rpop 1
quietly{
tempvar temppop
egen `temppop' = mean(`pop'), by(`i')
replace `pop' = `temppop'
drop `temppop'
count if `pop' == . | `pop' <= 0
local foundzeroes = r(N)
if `foundzeroes' > 0 {
local dropped_zero = 1
preserve
keep if `pop' == . | `pop' <= 0
save "`newfile3'", emptyok replace
use "`newfile4'", clear
append using "`newfile3'"
save "`newfile4'", replace
restore
drop if `pop' == . | `pop' <= 0
}
}
}
else if `rpop' ~= -1 {
noi di "Warning: You specified an rpop threshold but did not provide pop variable!!!"
exit
}
else {
local calculate_rpop 0
}
quietly{
tempvar sum_i sum_p
foreach j in i p {
egen `sum_`j'' = total(`val'), by(``j'')
count if `sum_`j'' == . | `sum_`j'' <= 0
local foundzeroes = r(N)
if `foundzeroes' > 0 {
local dropped_zero = 1
preserve
keep if `sum_`j'' == . | `sum_`j'' <= 0
save "`newfile3'", emptyok replace
use "`newfile4'", clear
append using "`newfile3'"
save "`newfile4'", replace
restore
drop if `sum_`j'' == . | `sum_`j'' <= 0
}
drop `sum_`j''
}
}
load_export_mata `t' `i' `p' `val' `touse'
if $error_code == 1 exit // error checking
*------------------------------------------------
// Inmediate Mcp
if `l0'==1 {
mata M = exp_cp
}
// calculate RCA case
if `calculate_rpop' == 0 & `l0'==0 {
if `rca' == -1 {
local rca 1
}
complexity_rca
mata M = (RCA:>`rca')
}
// calculate Rpop case
else if `calculate_rpop' == 1 & `rca' == -1 & `l0'==0 {
load_population_mata `i' `pop' `touse'
complexity_rpop
mata M = (RPOP:>`rpop')
}
// combination of the two (RCA AND Rpop)
else if `calculate_rpop' == 1 & `rca' != -1 & `l0'==0 {
load_population_mata `i' `pop' `touse'
complexity_rca
complexity_rpop
mata M1 = (RCA:>`rca')
mata M2 = (RPOP:>`rpop')
mata M = M1 + M2
mata M = (M:>0)
}
if "`cont'"~="" {
*noi display " : Continuous"
proxcontinous, levels(RCA)
}
else {
*noi display " : Discrete"
proxdiscrete, `asym'
}
quietly{
egen key=tag(`p')
keep if key==1
keep id_p `p'
rename id_p id_1
rename `p' `p'_1
save temp_id_p_1.dta, replace
rename id_1 id_2
rename `p'_1 `p'_2
save temp_id_p_2.dta, replace
cap drop *
cap set obs ${Np}
// Generate the variables the will store the output of the matrix calculations
gen id_1 = _n
gen id_2 = _n
forval j = 1/2 {
merge 1:1 id_`j' using temp_id_p_`j'.dta
drop _merge id_`j'
erase "temp_id_p_`j'.dta"
}
fillin `p'_1 `p'_2
drop _fillin
if `t_present' == 1 {
gen `t' = `y'
}
sort `p'_1 `p'_2
*gen byte touse = 1
*mata proximity = proximity'
foreach var in proximity {
cap drop `var'
mata tostata = colshape(`var',1)
qui mata newvar_row = st_addvar("double", "`var'")
qui mata st_store(.,newvar_row, tostata)
*mata st_store(.,newvar_row, "`touse'", tostata)
}
*mata tostata = colshape(`var',1)
*qui mata newvar_row = st_addvar("float", "`var'")
*cap mata st_store(.,newvar_row, "`touse'", tostata)
*drop if `p'_1 == `p'_2
// saves the results for the year, opens the file were we store the data
save "`newfile3'", replace
use "`newfile2'", clear
append using "`newfile3'"
save "`newfile2'", replace
}
}
keep `p'_1 `p'_2 proximity // touse
*=====================================================================================
cap label var proximity "Proximity"
*=====================================================================================
di " "
* Options for Proximity matrices
if "`cont'"~="" {
display " : Using Continuous", _c
}
else {
* Options for RCA and Rpop
if `calculate_rpop' == 0 {
display " : Using RCA with threshold of `rca'"
}
else if `calculate_rpop' == 1 & `rca' == -1 & {
display " : Using Rpop with threshold of `rpop'"
}
else if `calculate_rpop' == 1 & `rca' != -1 {
display " : Using combination of RCA>=`rca' and Rpop>=`rpop'"
}
display " : Using Discrete", _c
}
if "`asym'"~="" {
display "and Asymmetric Proximity"
}
else {
display "and Symmetric Proximity"
}
noi di "FINAL"
*=====================================================================================
display "________________________________________________________________________________________________"
*=====================================================================================
end