-
Notifications
You must be signed in to change notification settings - Fork 0
/
monte_carlo_update.do
592 lines (511 loc) · 17.4 KB
/
monte_carlo_update.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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
set more off
clear all
set type double
set seed 20200603
* Set working directory
*cd OAS
*
* This code updates the earlier CEPR analysis of the 2019 Bolivian Elections
* to make use of data provided by the TSE
*
* INPUT is the directory where the OAS RAR file has been unpacked.
* datos_trep_computo_eg2019 should be a subdirectory
* the RAR files within that subdirectory should also be unpacked
global INPUT ../Data/TSE
global DATA Data
global OUTPUT ${DATA}/montecarlo/results
*
* PROBLEM WITH -IMPORT EXCEL-
* any timestamp data imported as strings comes in with a 12 hour clock and no AM/PM indicator
* any timestamp data imported as numeric are fine, but not necessarily formatted usefully
* WHY THIS IS A PROBLEM
* information may be lost if -IMPORT EXCEL- imports as string, say if misisng values are listed as "null"
* SOLUTION
* reach into the underlying XML files and pull data directly as string
*
* CONSEQUENCE
* this fix is probably NOT something Nooruddin did for the OAS, so some timestamps may differ
*
capture: program drop unpackXLSX
program define unpackXLSX
syntax anything(name=xlsxfile), [To(string asis)] [REPLACE]
*
* Note that XLSX files are just other stuff packaged in a zip
*
capture: confirm file `to'
if (_rc~=0 | "`replace'"=="replace") {
! unzip `xlsxfile' -d `to'
}
end
capture: program drop getWorksheet
program define getWorksheet
syntax [anything(name=sheetname)], From(string asis)
*
* This program reads in an Excel worksheet (in XML)
*
if ("`sheetname'"=="") {
local sheetname sheet1
}
*
* First, get any shared strings
*
tempfile ss
* Add newlines after each cell for easier parsing
filefilter `from'/xl/sharedStrings.xml `ss', from("</si>") to("</si>\U") replace
* Read in the data
import delimited using `ss', delim("</si>\n", asstring) enc("utf-8") clear stringcols(_all)
gen sharedString = regexs(1) if regexm(v1,"<si><t[^>]*>([^<]*)")
drop v1
drop if mi(sharedString)
gen stringCode = _n-1
compress
save `ss', replace
*
* Read in the sheet XML
*
tempfile fft xml
* Add newlines after each cell for easier parsing
filefilter `from'/xl/worksheets/`sheetname'.xml `fft', from("\Q/><c r=\Q") to("\Q/>\U<c r=\Q") replace
filefilter `fft' `xml', from("</c>") to("</c>\U") replace
* Read in the data
import delimited using `xml', delim("</c>\n", asstring) enc("utf-8") clear stringcols(_all)
* Parse out the basics
gen cell = regexs(1) if regexm(v1,`"c r="([^"]*)""')
gen row = regexs(1) if regexm(cell,"[A-Z]+([0-9]+)")
gen col = regexs(1) if regexm(cell,"([A-Z]+)[0-9]+")
keep if ~mi(row) & ~mi(col)
gen value = regexs(1) if regexm(v1,"<v>([^<]*)</v>")
gen isSString = regexm(v1,`"t="s""')
gen isIString = regexm(v1,`"t="inlineStr""')
* Handle shared strings
gen stringCode = value if isSString
destring stringCode, force replace
compress
merge n:1 stringCode using `ss', keep(match master)
li if ~mi(stringCode) & _merge==1
replace sharedString = "p21F" if sharedString=="21F"
replace sharedString = "PANBOL" if sharedString=="PAN-BOL"
replace value = sharedString if isSString
* Handle inline strings
gen inlineString = regexs(1) if regexm(v1,"<is><t>([^<]*)</t></is?")
replace inlineString = "p21F" if inlineString=="21F"
replace inlineString = "PANBOL" if inlineString=="PAN-BOL"
replace value = inlineString if isIString
* Reshape into rows
drop v1 cell _merge
drop isSString-inlineString
destring row, force replace
replace col = strlower(col) if length(col)>1
reshape wide value, i(row) j(col) string
drop row
*
* Cleanup
*
* Set variable names from first row
foreach var of varlist value* {
capture: ren `var' `=substr(subinstr(`var'[1]," ","",.),1,32)'
}
drop in 1
compress
end
capture: program drop getTSE
program define getTSE
syntax, [TREP] [COMPUTO] [REPLACE]
local count `trep'`computo'
if ("`count'"=="trepcomputo") {
local count
}
if ("`count'"=="") {
di as error "Must choose one of TREP or COMPUTO"
local count computo
}
if ("`count'"=="trep") {
local upfile ${INPUT}/datos_trep_computo_eg2019/trep/OEA_TREP_LogCompleto_19G_2019.11.03.06.53.xlsx
local sheetname sheet1
}
else {
local upfile ${INPUT}/datos_trep_computo_eg2019/computo/2.RecepcionSobres_final.xlsx
local sheetname sheet1
}
capture: use ${DATA}/`count', clear
if (_rc~=0 | "`replace'"=="replace") {
* Unpack XLSX to a working directory
capture: mkdir ${DATA}
unpackXLSX `upfile', to(${DATA}/`count')
* Read in the data
getWorksheet, from(${DATA}/`count')
*
* Specific cleaning
*
* Detect and decode any timestamps
foreach var of varlist * {
if (regexm("`var'","Date|Fecha") | regexm(`var'[1],"^[0-9][0-9][0-9][0-9][0-9]\.[0-9]+$")) {
destring `var', force replace
replace `var' = round((`var'+td(30dec1899))*86400)*1000
format %tcDDmonCCYY_HH:MM:SS.sss `var'
}
}
* Destring election numbers
destring NumMesa CodVer CC-Ins, force replace
*
* SKIP THIS NEXT STEP. Nooruddin does NOT assign zero to missing vote data.
* That's obviously a bad thing, but he did what he did
/*
foreach var of varlist CC-PANBOL Bla Nul {
replace `var' = 0 if mi(`var')
}
*/
* Remove variable labels (formerly adjusted cell columns)
foreach var of varlist * {
capture: label var `var' ""
}
compress
save ${DATA}/`count', replace
}
end
capture: program drop doDataPrep
program define doDataPrep
syntax anything(name=mcf), [IF(string asis)] [REPLACE]
if ("`if'"=="") {
local if 1
}
capture: use ${DATA}/montecarlo/base/`mcf', clear
if (_rc~=0 | "`replace'"=="replace") {
capture: mkdir ${DATA}/montecarlo
capture: mkdir ${DATA}/montecarlo/base
*
* Fetch the data
*
* TREP
tempfile tf
getTSE, trep
keep if regexm(Ele,"Pres")
keep if regexm(Est,"Ver") | regexm(Est,"Computada")
* filter
keep if `if'
* drop duplicate verification
egen sumVálidos = rowtotal(CC-PANBOL)
drop if sumVálidos==0 & NumMesa==2433
* other data maintenance
order NumMesa
keep NumMesa CC MAS sumV
gen others = sumV-(CC+MAS)
compress
save `tf'
* merge with computo (to get entire list of actas)
getTSE, computo
keep if regexm(Ele,"Pres")
keep if regexm(Est,"Ver") | regexm(Est,"Computada")
keep NumMesa Pa-Reci Ins
compress
merge 1:1 NumMesa using `tf', nogen
* Encode the geographies
gen foreign = Pais~="Bolivia"
egen fcode = group(foreign)
egen pcode = group(fcode Pais), missing
egen dcode = group(pcode Dep), missing
egen pdcode = group(dcode Prov), missing
egen mcode = group(pdcode Muni), missing
egen lcode = group(mcode Loc), missing
egen rcode = group(lcode Reci), missing
egen scode = group(rcode NumMesa), missing
save ${DATA}/montecarlo/base/`mcf', replace
}
end
capture: program drop doBoliviaMCprep
program define doBoliviaMCprep
syntax anything(name=mcf), [IF(passthru)] [REPLACE] [NEWBASE]
if ("`newbase'"=="newbase") {
local newbase replace
}
capture: use ${DATA}/montecarlo/prepped/`mcf', clear
if (_rc~=0 | "`replace'"=="replace") {
capture: mkdir ${DATA}/montecarlo
capture: mkdir ${DATA}/montecarlo/prepped
doDataPrep `mcf', `if' `newbase'
* sample tags one counted acta for each geography
gen ssample = ~mi(CC)
gen scodex = scode if ssample
local lg s
foreach g in r l m pd d p f {
* Tag representative acta for each geography
egen `g'sample = tag(`g'code) if `lg'sample
*
* Key MC variables here:
* codex holds the replacement geography at each level
* rcodex will be the code for the replacement precinct
* scodex will be the code for the replacement acta if necessary
*
bys `g'code: egen `g'codex = total(`g'sample), missing
replace `g'codex = cond(`g'codex,`g'code,.,.)
local lg `g'
}
* generate lists of possible replacement geographies for MC sampling
* - we want to connect these to each geography, so every acta in each
* (country) has a list of possible (departments) from which to draw
* - when we set a replacement (country) for an acta, we will update the
* replacement (department) list as well
local lg f
qui foreach gc in p d pd m l r s {
gen `gc'list = ""
levelsof `lg'code, local(`lg'l) clean
foreach g of local `lg'l {
noi di "`lg'=`g' `gc''s"
levelsof `gc'code if `lg'code==`g' & `gc'sample, local(gl) clean
replace `gc'list = "`gl'" if `lg'code==`g'
}
gen `gc'len = wordcount(`gc'list)
local lg `gc'
}
save ${DATA}/montecarlo/prepped/`mcf', replace
}
end
capture: program drop doMCiter
program define doMCiter
syntax [, Beta Gamma Sheet]
*
* Single Iteration
*
* Assign replacement geographies
* If method is sheet, need to assign all the way down to acta
local gprefs p d pd m l r
if ("`sheet'"=="sheet") {
local gprefs `gprefs' s
}
local lg f
foreach g in `gprefs' {
* Update to available replacement geographies
bys `lg'codex (`lg'sample): replace `g'list = `g'list[_N]
by `lg'codex (`lg'sample): replace `g'len = `g'len[_N]
* Generic geographic indicator for replacement
tostring `g'codex, generate(g_i)
*
* Key MC operation right here:
* randomly pick a replacement geography if codex is missing
*
replace g_i = word(`g'list,runiformint(1,`g'len)) if mi(`g'codex)
destring g_i, replace
bys `g'code (`g'sample): replace `g'codex = g_i[_N]
capture: drop g_i
local lg `g'
}
* Assign replacement parameters and simulate votes
bys `lg'codex (`lg'sample): egen nx = total(cond(~mi(CC),1,.))
bys `lg'codex (`lg'sample): egen Ix = total(cond(~mi(CC),Ins,.))
foreach p in CC MAS others {
bys `lg'codex (`lg'sample): egen V`p'x = total(cond(~mi(CC),`p',.))
}
* Simulate votes
if ("`sheet'"=="sheet") {
foreach p in CC MAS others {
gen `p'sheet = V`p'x*Ins/Ix
}
order CCs MASs otherss, last
}
if ("`gamma'"=="gamma") {
*
* Note that Gamma-Poisson is not quite right in that
* there is no guarantee we wont have more votes than eligible voters
* - Additional robustness test
*
foreach p in CC MAS others {
if (1) {
* Poisson rate per eligible voter is based on an improper
* Bayesian prior
gen par_`p'gamma = rgamma(V`p'x,1/(0+Ix))
replace par_`p'gamma = 0 if mi(par_`p'gamma)
bys rcode: gen `p'gamma = rpoisson(Ins*par_`p'gamma[1])
replace `p'gamma = 0 if mi(`p'gamma)
}
else {
* Poisson rate per acta in te precinct
gen par_`p'gamma = rgamma(0.001+V`p'x,1/(1/1000+nx))
bys rcode: gen `p'gamma = rpoisson(par_`p'gamma[1])
}
}
order CCg MASg othersg, last
}
if ("`beta'"=="beta") {
*
* For Beta-Binomial, we are going to make sure that ordering of the parties
* does not matter by randomly ordering with every iteration
*
* - Primary robustness test
*
local betalast CC MAS others
local betafirst = word(`"`betalast'"',runiformint(1,3))
local betalast: list betalast - betafirst
local betanext = word(`"`betalast'"',runiformint(1,2))
local betalast: list betalast - betanext
macro li _betafirst _betanext _betalast
gen first = "`betafirst'"
gen next = "`betanext'"
gen last = "`betalast'"
* For the first party, the binomial probability of an eligible voter
* is beta(V,I-V) where V is the total votes for the party in the
* representative precinct and I is the eligible voters in the precinct
* - effectively Bayesian with an improper Haldane prior
gen par_`betafirst'beta = rbeta(V`betafirst'x, Ix -V`betafirst'x)
* For the second party, the binomial probability is computed similarly,
* but based on the eligible voters who did NOT vote for the first party
gen par_`betanext'beta = rbeta(V`betanext'x ,(Ix- V`betafirst'x) -V`betanext'x)
* And likewise the third, based on eligible voters who did not vote for the first two
gen par_`betalast'beta = rbeta(V`betalast'x ,(Ix-(V`betafirst'x+V`betanext'x))-V`betalast'x)
* We want the same draw binomial probability draw across all actas
foreach p in CC MAS others {
* clean up where the draw is missing. Should be zero in all cases
replace par_`p'beta = 0 if mi(par_`p'beta)
bys rcode: replace par_`p'beta = par_`p'beta[1]
}
* Now assign the actual votes based on the actual base of eligible voters
* remembering to reduce the base with each party according to the votes
* already assigned
* Also, clean up any missings as we proceed
gen `betafirst'beta = rbinomial(Ins ,par_`betafirst'beta)
replace `betafirst'beta = 0 if mi(`betafirst'beta)
gen `betanext'beta = rbinomial(Ins- `betafirst'beta ,par_`betanext'beta)
replace `betanext'beta = 0 if mi(`betanext'beta)
gen `betalast'beta = rbinomial(Ins-(`betafirst'beta+`betanext'beta),par_`betalast'beta)
replace `betalast'beta = 0 if mi(`betalast'beta)
}
end
capture: program drop doBoliviaMC
program define doBoliviaMC
syntax [anything(name=mcf)], [Beta Gamma Sheet] [REPLACE] [IF(passthru)] [Inner(integer 50)] [Outer(integer 10)]
*
* Monte Carlo projections based on TREP
*
* Options:
* run is an identifier; results will be saved in Summaries`run'
*
* beta (Beta-Binomial based on representative precinct)
* gamma (Gamma-Poisson based on representative precinct)
* sheet (MC sampling of available actas in representative precinct)
* <none of above> (NOT MC) Impute votes per eligible voter based on
* average across smallest geography with counted actas
*
* replace overwrites the run with new simulation;
* otherwise if run has been performed, the program simply loads
* the previous results of the run
*
* inner is the number of MC iterations in each batch to be saved
* outer is the number of MC batches to be saved
* (inner x outer = total number of iterations)
*
* test must imply replace so that the iteration will be performed
* it will break before replacing any saved simulations
if ("`beta'`gamma'`sheet'"=="") {
doDataPrep `mcf', `if'
* Do a simple point estimate
foreach p in CC MASIPSP others {
* Simulated values
gen `p'x = `p'
}
foreach g in r l m pd d p f {
* In each geography, total eligible votes on quick-counted actas
bys `g'code: egen `g'I = total(cond(~mi(CC),Inscritos,.)), missing
foreach p in CC MAS others {
* In each geography, compute party vote rate on quick-counted actas
bys `g'code: egen `g'`p'r = total(`p'/`g'I), missing
by `g'code: replace `p'x = Ins*`g'`p'r if mi(`p'x)
}
}
gen counted = ~mi(CC)
collapse (sum) *x CC MAS others, by(counted)
gen margin = 100*(MAS-CC)/(MAS+CC+others)
gen marginx = 100*(MASx-CCx)/(MASx+CCx+othersx)
li
}
else {
capture: use ${OUTPUT}/`mcf', clear
if (_rc~=0 | "`replace'"=="replace") {
capture: mkdir ${DATA}
capture: mkdir ${DATA}/montecarlo
capture: mkdir ${DATA}/montecarlo/results
*
* Monte Carlo
*
tempfile ti
qui forvalues ii=1/`outer' {
* We might have already done this batch...
capture: use ${OUTPUT}/`mcf'/batches/`ii', clear
if (_rc~=0 | "`replace'"=="replace") {
capture: mkdir ${OUTPUT}/`mcf'
capture: mkdir ${OUTPUT}/`mcf'/batches
tempfile tj
forvalues jj=1/`inner' {
noi di as text "Imputation number: " as result (`ii'-1)*`inner'+`jj'
doBoliviaMCprep `mcf', `if'
doMCiter, `beta' `gamma' `sheet'
local byvars
if ("`beta'"=="beta") {
local byvars by(first next last)
}
collapse (sum) CC* MAS* others*, `byvars'
noi li
if (`jj'>1) {
append using `tj'
}
save `tj', replace
}
* save this batch
save ${OUTPUT}/`mcf'/batches/`ii', replace
}
* Add this batch of to the full batch
if (`ii'>1) {
append using `ti'
}
save `ti', replace
* Calculate the margins and report on the distribution
local s
gen margin`s' = 100*(MAS`s'-CC`s')/(MAS`s'+CC`s'+others`s')
foreach s in `beta' `gamma' `sheet' {
gen margin`s' = 100*(MAS`s'-CC`s')/(MAS`s'+CC`s'+others`s')
}
* Histogram
discard
local otypes CC
unab type: CC*
local type: list type-otypes
local type = substr("`type'",3,.)
sum margin`type', meanonly
hist margin`type', caption(`"`=_N' iterations of imputation type `type'"') ///
xti("MAS-IPSP - CC (% of valid votes)") yti("") ///
freq xlab(`=ceil(10*`r(min)')/10'(0.1)`=floor(10*`r(max)')/10')
}
capture: drop margin*
* Save the full batch of simulations
save ${OUTPUT}/`mcf'/`mcf', replace
}
local otypes CC
unab type: CC*
local type: list type-otypes
local type = substr("`type'",3,.)
* Calculate MAS margins
gen margin = 100*(MAS`type'-CC`type')/(MAS`type'+CC`type'+others`type')
* Histogram
sum margin, meanonly
hist margin, /*caption(`"`=_N' iterations of imputation type `type'"')*/ ///
xti("MAS-IPSP - CC (% of valid votes)") yti("") ///
freq xlab(`=ceil(10*`r(min)')/10'(0.1)`=floor(10*`r(max)')/10')
graph export ${OUTPUT}/`mcf'/`mcf'.png, replace
* Reporting
if ("`type'"=="beta") {
* Check that Beta-Binomial party ordering doesn't matter for beta by
* disaggregating results by ordering
table first next, c(mean margin)
}
codebook margin
}
end
* Options are:
* sheet (MC sampling of available actas in representative precinct)
* beta (Beta-Binomial based on representative precinct)
* gamma (Gamma-Poisson based on representative precinct)
* <empty> (NOT MC) Impute votes per eligible voter based on average across
* smallest geography with counted actas
local type sheet
* May be just about anything here. Used to save simulations in case
* you want to come back to them
local run 2
doBoliviaMC `run', if(dofc(VerificadorDate)<td(21oct2019)) `type' i(50) o(10)