-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpcn_compare_rp.ado
313 lines (249 loc) · 9.83 KB
/
pcn_compare_rp.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
/*==================================================
project: graph pcn compare results
Author: David L. Vargas
E-email: [email protected]
url:
Dependencies: The World Bank
----------------------------------------------------
Creation Date: 15 Jun 2020 - 12:41:59
Modification Date:
Do-file version: 01
References:
Output:
==================================================*/
/*==================================================
0: Program set up
==================================================*/
program define pcn_compare_rp, rclass
syntax [anything(name=subcmd id="subcommand")], ///
[ ///
server(string) ///
VARiables(string) ///
DIRSave(string) ///
SDLevel(string) ///
TOLerance(integer 3) /// decimal places
COUNtry(string) ///
REGion(string) ///
year(string) ///
level(string) ///
FILLgaps ///
wb ///
]
version 15
//========================================================
// Start
//========================================================
qui{
/*================================================
1: Check options definition and declare macros
==================================================*/
// Check for sepscatter packg
cap which sepscatter
if (_rc) noi ssc install sepscatter
cap which confirmdir
if (_rc) noi ssc install confirmdir
// relevant macros
if ("`variables'"=="") loc variables = "headcount"
else loc variables = lower("`variables'")
if ("`server'" == "") loc server "AR"
else loc server = lower("`server'")
if ("`sdlevel'" == "") loc sdlevel = 2
cap loc sdlevel = real("`sdlevel'")
if (_rc){
noi di as err "The SD level must be a real number" char(10)
noi di "SD level set to default"
}
if ("`dirsave'" != "") {
if (lower("`dirsave'") == "wd") loc dirsave "`c(pwd)'"
else confirmdir "`dirsave'"
}
else{
noi dis as err "No saving directory specified. STOP" char(10) as text "Either provide a directory or set dirsave(wd) to use working directory."
err
}
if ("`level'"!=""){
loc level = lower("`level'")
if !inlist("`level'","country","region"){
noi dis as err "Level must be either country or region" char(10)
noi di "level set to default region"
loc level = "region"
}
if ("`level'"=="region"){
loc levcall "regioncode"
}
else{
loc level "country"
loc levcall "countrycode"
}
}
else{
loc level "region"
loc levcall "regioncode"
}
/*================================================
2: Get comparison data and graphs
==================================================*/
pcn_compare, mainv(`variables') server(`server') check("main") ///
country(`country') region(`region') year(`year') ///
`fillgaps' `aggregate'
pcn_compare_gr, variables(`variables') dirsave(`dirsave') ///
sdlevel(`sdlevel') level(`level')
/*================================================
3: Report
==================================================*/
putdocx begin
// Add a title
putdocx paragraph, style(Title)
putdocx text ("Report: Current vs Testing Povcalnet")
putdocx paragraph
putdocx text ("This report compares the currently publish povcalnet to the unpublish version on the testing server. This file was generated by the PCN package, with the following user given conditions:"), linebreak(1)
putdocx text ("- variables: `variables'"), linebreak(1)
putdocx text ("- server: `server'"), linebreak(1)
putdocx text ("- SD level: `sdlevel'"), linebreak(1)
putdocx text ("- Decimal points tolerance: `tolerance'"), linebreak(1)
if ("`year'"!=""){
putdocx text ("- Years: `year'"), linebreak(1)
}
putdocx text ("- level: `level'"), linebreak(1)
if ("`region'"!=""){
putdocx text ("- region: `region'"), linebreak(1)
}
if ("`country'"!=""){
putdocx text ("- country: `country'"), linebreak(1)
}
// Overview
putdocx paragraph, style(Heading1)
putdocx text ("Overview of changes")
putdocx paragraph
putdocx text ("The year-country points, have the following status:"), linebreak(1)
preserve
contract status
rename _freq occurrences
putdocx table mytable = data(status occurrences), varnames width(50%)
restore
putdocx sectionbreak
// by variable
levelsof `levcall', local(levs)
putdocx paragraph
putdocx text ("Analysing by variable we have the following"), linebreak(1)
foreach var of local variables{
putdocx paragraph, style(Heading1)
putdocx text ("Changes on `var'")
putdocx paragraph
putdocx text ("Regarding the `var':"), linebreak(2)
putdocx text ("The differences across servers have the following stats:"), linebreak(1)
loc vars "d_`var'"
forv x = 1/`sdlevel' {
loc vars "`vars' ht_`x'sd_`var'"
}
local nvars: word count `vars'
local nrows=`nvars'+2
putdocx table table2 = (`nrows',7), border(all, nil) width(100%) layout(autofitcontents) note(Note: The differences are computed as current - testing values.)
putdocx table table2(1,1)=("Summary Statistics"), bold font("Calibri", 12) halign(center) colspan(7) linebreak
putdocx table table2(2,1) = ("Variable"), bold font("Calibri", 11) halign(center)
putdocx table table2(2,2) = ("Variable Definition"), bold font("Calibri", 11) halign(center)
putdocx table table2(2,3) = ("Obs."), bold font("Calibri", 11) halign(center)
putdocx table table2(2,4) = ("Mean"), bold font("Calibri", 11) halign(center)
putdocx table table2(2,5) = ("Std. Dev."), bold font("Calibri", 11) halign(center)
putdocx table table2(2,6) = ("Min."), bold font("Calibri", 11) halign(center)
putdocx table table2(2,7) = ("Max."), bold font("Calibri", 11) halign(center)
local i=3
foreach vh of local vars {
local lab: variable label `vh'
quietly summarize `vh'
putdocx table table2(`i',1) = ("`vh'")
putdocx table table2(`i',2) = ("`lab'")
putdocx table table2(`i',3) = (r(N))
putdocx table table2(`i',4) = (r(mean)), nformat(%5.2f)
putdocx table table2(`i',5) = (r(sd)), nformat(%5.2f)
putdocx table table2(`i',6) = (r(min))
putdocx table table2(`i',7) = (r(max))
local i=`i'+1
}
putdocx paragraph
putdocx text ("The differences beyond the `sdlevel' SD from the mean are distributed as follows:"), linebreak(1)
putdocx image hist_d_`var'.png
putdocx paragraph
putdocx text ("Differencing by `level':"), linebreak(1)
putdocx image histR_d_`var'.png
// freq table by level
putdocx paragraph
putdocx text ("By `level' and level of SD from the mean we have:"), linebreak(1)
loc vars ""
forv x = 1/`sdlevel' {
loc vars "`vars' ht_`x'sd_`var'"
}
local nreg: word count `levs'
local nrows=`nreg'+2
loc j = 3
foreach vh of local vars {
local lab: variable label `vh'
putdocx table table`j' = (`nrows',4), border(all, nil) width(100%) layout(autofitcontents) note("Note: The variable is a dummy, with value of one if the absolute difference is higher than the mean plus the selected number of standard deviations.")
putdocx table table`j'(1,1)=("`lab' by `level'"), bold font("Calibri", 12) halign(center) colspan(7) linebreak
putdocx table table`j'(2,1) = ("`level'"), bold font("Calibri", 11) halign(center)
putdocx table table`j'(2,2) = ("1"), bold font("Calibri", 11) halign(center)
putdocx table table`j'(2,3) = ("0"), bold font("Calibri", 11) halign(center)
putdocx table table`j'(2,4) = ("Total"), bold font("Calibri", 11) halign(center)
local i=3
foreach rg of local levs {
qui count if `levcall' == "`rg'" & `vh' == 1
loc uno = r(N)
qui count if `levcall' == "`rg'" & `vh' == 0
loc zero = r(N)
qui count if `levcall' == "`rg'"
loc tot = r(N)
putdocx table table`j'(`i',1) = ("`rg'")
putdocx table table`j'(`i',2) = ("`uno'")
putdocx table table`j'(`i',3) = ("`zero'")
putdocx table table`j'(`i',4) = ("`tot'")
local i=`i'+1
}
loc j = `j' + 1
}
// Difference scatter
putdocx paragraph
putdocx text ("Scatter plot of differences:"), linebreak(1)
putdocx image sc_`var'.png
putdocx paragraph
putdocx text ("The following countries have differences in headcount beyond the tolerance level of `SDLevel' deviation of the mean:"), linebreak(1)
// problematic observations by level table by level
tempvar obsid
*countrycode year povertyline coveragetype datatype
*egen `obsid' = concat(countrycode year coveragetype datatype), p(-)
egen `obsid' = concat(countrycode year), p(-)
loc vars ""
forv x = 1/`sdlevel' {
loc vars "`vars' ht_`x'sd_`var'"
}
local nreg: word count `levs'
local nrows=`nreg'+2
foreach vh of local vars {
local lab: variable label `vh'
putdocx table table`j' = (`nrows',2), border(all, nil) width(100%) layout(autofitcontents) note("Note: The variable is a dummy, with value of one if the absolute difference is higher than the mean plus the selected number of standard deviations.")
putdocx table table`j'(1,1)=("countries `lab' by `level''"), bold font("Calibri", 12) halign(center) colspan(7) linebreak
putdocx table table`j'(2,1) = ("observation"), bold font("Calibri", 11) halign(center)
local i=3
foreach rg of local levs {
qui levelsof `obsid' if `levcall' == "`rg'" & `vh' == 1, clean s(", ") local(obsi)
putdocx table table`j'(`i',1) = ("`rg'")
putdocx table table`j'(`i',2) = ("`obsi'")
local i=`i'+1
}
loc j = `j' + 1
}
putdocx sectionbreak
}
putdocx save "`dirsave'/pcn_report.docx", replace
noi di as result "Graphs and report (pcn_report.docx) saved at `dirsave'."
}
end
exit
/* End of do-file */
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
Notes:
1.
2.
3.
Version Control:
cd "~\OneDrive - WBG\Desktop"
pcn_compare_rp, dirs(wd) var(headcount)