-
Notifications
You must be signed in to change notification settings - Fork 6
/
pdensity.ado
executable file
·191 lines (167 loc) · 5.91 KB
/
pdensity.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
*----------------------------------------------------------------------------------------
cap program drop pdensity
program define pdensity
*----------------------------------------------------------------------------------------
syntax varlist [if/], i(varlist) p(varlist) [t(varlist) pop(varlist) rca(real 1) rpop(real -1) knn(real -1) contp contd usevar leaveout im(varlist) asym]
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
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!!"
local errorexistenvar=1
}
}
if "`t'"=="" {
local t_present 0
tempvar t
qui gen byte `t' = 1
}
else {
local t_present 1
}
*----------------------------------------------------------------------
* Sort variables and prepare datasets
*----------------------------------------------------------------------
sort `t' `i' `p'
cap levelsof `t', local(year_levels)
quietly levelsof `t', local(Nt)
global Nnt: word count `Nt'
display " "
display "Calculates density"
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
}
*----------------------------------------------------------------------
if ("`levels'" == "" & "`contd'"~="") {
local levels RCA
*noi di "Set Product density variable to RCA"
}
//============================================================================================================
foreach y of local year_levels{ // starting main loop
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
}
}
*------------------------------------------------
/*
*------------------------------------------------
* Checks data for the running year
*------------------------------------------------
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' // outpu: exp_cp
if $error_code == 1 exit // error checking
*------------------------------------------------
complexity_rca
mata M = (RCA:>`rca')
noi di "RCA threshold is `rca'"
if "`usevar'"~=""{
local levels exp_cp
}
else {
local levels RCA
}
if "`contp'"~="" {
proxcontinous, levels("`levels'")
}
else{
proxdiscrete
}
if "`contd'"~=""{
calculate_density, knn(`knn') cont levels("`levels'") `leaveout'
*calculate_country_density, knn(`knn') cont levels("`levels'") `leaveout'
}
else {
calculate_density, knn(`knn') levels("`levels'") `leaveout'
*calculate_country_density, knn(`knn') levels("`levels'") `leaveout'
}
*------------------------------------------------------------------------------
* Turns matrices into stata dataset shape
*------------------------------------------------------------------------------
*foreach var in density country_density{
foreach var in density {
mata tostata = colshape(`var',1)
qui mata newvar_row = st_addvar("double", "`var'")
qui mata st_store(.,newvar_row,"`touse'",tostata)
}
*------------------------------------------------------------------------------
// saves the results for the year, opens the file were we store the data
*------------------------------------------------------------------------------
drop id_i
drop id_p
quietly {
save "`newfile3'", replace
use "`newfile2'", clear
append using "`newfile3'"
save "`newfile2'", replace
}
} // closing main loop
//============================================================================================================
if `dropped_zero' == 1 append using "`newfile4'"
*=====================================================================================
display "________________________________________________________________________________________________"
*=====================================================================================
end