-
Notifications
You must be signed in to change notification settings - Fork 6
/
ecomplexity_no_normalization.ado
402 lines (342 loc) · 11.9 KB
/
ecomplexity_no_normalization.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
*----------------------------------------------------------------------------------------
cap program drop ecomplexity_no_normalization
program define ecomplexity_no_normalization
*----------------------------------------------------------------------------------------
// net install ecomplexity, from("https://raw.githubusercontent.com/cid-harvard/ecomplexity/master/") force
version 10
syntax varlist [if/], i(varlist) p(varlist) [t(varlist) pop(varlist) rca(real -1) ///
rpop(real -1) knn(real -1) knnt(real -1) cont leaveout im(varlist) asym bi piet]
*----------------------------------------------------------------------
marksample touse
tokenize "`varlist'"
local val = "`1'"
global error_code = 0
local dropped_zero = 0
*----------------------------------------------------------------------
// Drop variables that can confuse the output
*----------------------------------------------------------------------
local errorexistenvar=0
local create_variables M RCA RPOP rca rpop mcp density eci pci coi cog kc0 kp0 diversity ubiquity _merge _fillin id_i id_p piet_c piet_p
foreach var in `create_variables' {
cap drop `var'
*noi di "`var'" _rc
if `errorexistenvar'==0 & _rc==0 {
noi display "________________________________________________________________________________________________"
noi di "Warning!! At least one output variable name was present in the dataset."
noi di "Will delete those variables from memory!!"
noi display "________________________________________________________________________________________________"
local errorexistenvar=1
}
}
if ("`pop'" ~= "" & `rpop' ~= -1) {
local calculate_rpop 1
}
else if `rpop' ~= -1 {
noi di "Warning: You specified an rpop threshold but did not provide pop variable!!!"
exit
}
else {
local calculate_rpop 0
}
if `calculate_rpop' == 1 & `rca' != -1 & "`cont'"~="" {
noi di "When using continous option, please only use just one of RCA or RPOP!!!"
exit
}
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 " "
display "Creates economic complexity variables"
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 (`calculate_rpop' == 1) {
local 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
}
}
}
*------------------------------------------------
*------------------------------------------------
* Checks data
*------------------------------------------------
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''
}
}
*------------------------------------------------
* Loads data into Mata matrices
*------------------------------------------------
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 and Rpop
*------------------------------------------------
// 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')
}
if $error_code == 1 exit
// 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)
}
*------------------------------------------------
//----------------------------------------------------------------------------
// Calculate proximity and density
//----------------------------------------------------------------------------
*noi di "continuous is `cont'"
*noi di "No leaveout is `leaveout'"
if `calculate_rpop' == 0 & "`cont'"~="" {
*noi display " : Continuous"
proxcontinous, levels(RCA)
calculate_density, knn(`knn') knnt(`knnt') `cont' `leaveout' levels(RCA)
}
else if `calculate_rpop' == 1 & "`cont'"~="" {
*noi display " : Continuous"
proxcontinous, levels(RPOP)
calculate_density, knn(`knn') knnt(`knnt') `cont' `leaveout' levels(RPOP)
}
else {
*noi display " : Discrete"
proxdiscrete, `asym'
calculate_density, knn(`knn') knnt(`knnt') `leaveout' `asym'
}
//----------------------------------------------------------------------------
*===================================
ecipci /* calculates eigenvector */
*===================================
*============================================================
coicog /* calculates complexity outlook uindex and gain */
*============================================================
if "`piet'"~="" {
pietronero
}
*------------------------------------------------------------------------------
* Turns matrices into stata dataset shape
*------------------------------------------------------------------------------
if `rca' != -1 {
mata tostata = colshape(RCA,1)
qui mata newvar_row = st_addvar("double", "rca")
qui mata st_store(.,newvar_row,"`touse'",tostata)
}
if `calculate_rpop' == 1 {
mata tostata = colshape(RPOP,1)
qui mata newvar_row = st_addvar("double", "rpop")
qui mata st_store(.,newvar_row,"`touse'",tostata)
}
foreach var in M density kc kp kc0 kp0 coi cog {
mata tostata = colshape(`var',1)
qui mata newvar_row = st_addvar("double", "`var'")
qui mata st_store(.,newvar_row,"`touse'",tostata)
}
if "`piet'"~="" {
foreach var in piet_c piet_p {
mata tostata = colshape(`var',1)
qui mata newvar_row = st_addvar("double", "`var'")
qui mata st_store(.,newvar_row,"`touse'",tostata)
}
}
*------------------------------------------------------------------------------
quietly{
rename kc eci
rename kp pci
rename kc0 diversity
rename kp0 ubiquity
drop id_i
drop id_p
*------------------------------------------------------------------------------
// Normalization: mean of zero and st dev of 1
*------------------------------------------------------------------------------
/*
// ECI
sum eci
replace eci = (eci-r(mean))/r(sd)
replace pci = (pci-r(mean))/r(sd)
replace cog = cog/r(sd)
// OPPVAL
sum coi
replace coi = (coi-r(mean))/r(sd)
*/
*------------------------------------------------------------------------------
*------------------------------------------------------------------------------
// 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
}
}
//============================================================================================================
if `dropped_zero' == 1 append using "`newfile4'"
qui drop if M==.
if `l0'==1 {
cap drop M // If the Mcp matrix was provided by user, there is no need to report it in new dataset
}
*------------------------------------------------------------------------------
* Labeling
*------------------------------------------------------------------------------
cap label var year "Year"
cap label var rca "Revealed Comparative Advantage (RCA)"
cap label var rpop "Revealed per capita Advantage (RPOP)"
cap label var density "Product Density "
cap label var coi "Complexity Outlook Index"
cap label var cog "Opportunity Gain"
cap label var eci "Economic Complexity Index"
cap label var pci "Product Complexity Index"
cap label var diversity "Country Diversity"
cap label var ubiquity "Product Ubiquity"
cap label var piet_c "Country Measure of Pietronero"
cap label var piet_p "Product Measure of Pietronero"
*------------------------------------------------------------------------------
*------------------------------------------------------------------------------
* Showing options used in the calculations
*------------------------------------------------------------------------------
di " "
* Options for RCA and Rpop and M
if `l0' == 1 {
display " : Using M matrix provided by user"
}
if `calculate_rpop' == 0 & `l0' == 0 {
display " : Using RCA with threshold of `rca'"
}
else if `calculate_rpop' == 1 & `rca' == -1 & `l0' == 0 {
display " : Using Rpop with threshold of `rpop'"
}
else if `calculate_rpop' == 1 & `rca' != -1 & `l0' == 0 {
display " : Using combination of RCA>=`rca' and Rpop>=`rpop'"
}
* Options for Proximity matrices
if "`cont'"~="" {
display " : Using Continuous", _c
}
else {
display " : Using Discrete", _c
}
if "`asym'"~="" {
display "and Asymmetric Proximity"
}
else {
display "and Symmetric Proximity"
}
*------------------------------------------------------------------------------
*------------------------------------------------------------------------------
* The End
*------------------------------------------------------------------------------
display "________________________________________________________________________________________________"
end
*------------------------------------------------------------------------------