-
Notifications
You must be signed in to change notification settings - Fork 2
/
cic.ado
1501 lines (1311 loc) · 60.6 KB
/
cic.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
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
*! cic.ado
*! Changes-in-changes
*!
*! An implimentation of:
*! Athey, S. & G. W. Imbens. "Identification and Inference in Nonlinear Difference-in-Differences Models."
*! Econometrica, 74 (2), March 2006, pp. 431-497.
*! Based on Matlab code by S. Athey & G. W. Imbens, published on S. Athey's website
*!
*! Stata code by Keith Kranker
program cic, properties(mi svyb) eclass byable(onecall)
version 11.2
if replay() {
if ("`e(cmd)'"!="cic") error 301
if _by() error 190
else Replay `0'
exit
}
if _by() local BY `"by `_byvars'`_byrc0':"'
`BY' Estimate `0'
ereturn local cmdline `"cic `0'"'
ereturn local cmd "cic"
ereturn local title "Changes in Changes (CIC) Model"
end
program define Estimate, eclass byable(recall)
version 11.2
// choice of estimator
gettoken estimator 0 : 0
if missing("`estimator'") {
local estimator = "all"
}
cap assert inlist("`estimator'","continuous","dci","bounds","all","check")
if _rc {
di as error "Select one of the following estimators: continuous, dci, bounds, or all."
error 198
}
// parse arguments
syntax varlist(default=none min=3 numeric fv) [if] [in] [fweight iweight aweight] ///
[, at(numlist min=1 >=0 <=100 sort) ///
Vce(passthru) ///
did ///
CONtinuous dci LOWer UPPer all ///
UNTreated ///
ROUnd(real 0) ///
level(passthru) noTABle noHeader noLegend * ] // Reporting options
marksample touse // note that rows are dropped for (1) if/in (2) zero weight (3) missing data (and other reasons, see "help mark")
_get_diopts diopts, `options'
local diopts `diopts' `table' `header' `legend'
_rmcoll `varlist' [`weight'`exp'] if `touse', expand
local varlist `r(varlist)'
// first three variables need to be y, treat, and post
gettoken y varlist : varlist
gettoken treat varlist : varlist
gettoken post varlist : varlist
// default percentiles
if mi(`"`at'"') local at "10 20 30 40 50 60 70 80 90"
// parse vce()
if mi(`"`vce'"') local vce vce(none)
cic_vce_parse, `vce'
local vce = r(vce)
local bsreps = r(bsreps)
local bssize = r(bssize)
local dots = r(dots)
if !missing(r(bsopts)) local bsopts = r(bsopts)
if !missing(r(saving)) local saving = r(saving)
local sepercentile = (r(sepercentile)==1)
if ("`vce'"=="bootstrap" & `bssize'!=0 & !inlist("`wgtvar'","iweight","pweight","aweight")) {
di as err "vce(`vce', size(`size')) is only allowed with iweight , pweight , and aweight. With unweighted samples, you could generate a variable equal to one and use it as an iweight."
error 101
}
// prep to handle weights
if !missing("`weight'") {
tempvar wgtvar
gen `wgtvar'`exp' if `touse'
summ `wgtvar' if `touse', meanonly
local haswgt = 1
}
else local haswgt = 0
if inlist("`weight'","iweight") {
if ("`vce'"=="bootstrap" & `bssize'==0) {
local bssize = r(sum)
di as txt "(When bootstrapping standard errors with iweights, the sub-option vce(bootstrap, size(`=round(`bssize',.001)')) was used by default, where `=round(`bssize',.001)' is the sum of the iweights.)"
}
local wtexp_caller = `", "`wgtvar'", `bssize' "'
local n=round(r(sum))
if ("`vce'"=="delta") {
di as err "vce(delta) is not allowed with `weight's."
error 101
}
}
else if inlist("`weight'","aweight","pweight") {
qui replace `wgtvar' = `wgtvar' / r(mean)
if ("`vce'"!="none" & "`weight'"=="pweight") {
di as err "vce(`vce') is not allowed with `weight's."
error 101
}
qui count if `touse'
local n=r(N)
if ("`vce'"=="bootstrap" & `bssize'==0) {
local bssize = r(N)
di as txt "(When bootstrapping standard errors with aweights and pweights, the weights are normalized to mean=1 and the sub-option vce(bootstrap, size(`bssize')) sub-option was used by default, where `bssize' is the number of observations.)"
}
}
else if inlist("`weight'","fweight") {
// fweight
local wtexp_caller = `", "`wgtvar'", `bssize' "' // fweight
local n=r(sum)
cap assert `wgtvar'==round(`wgtvar')
if _rc error 401
}
else {
// no weights
qui count if `touse'
local n=r(N)
}
// adjust y for covariates (OLS regression)
// always run DID regression if control variables present
local runDID = (("`did'"=="did") | (`: list sizeof varlist'!=0))
// For `untreated' option, flip sign of `treat'
if "`untreated'"=="untreated" {
tempvar untreat
gen `untreat' = (`treat'==0) if `touse'
unab treat_or_untreat : `untreat'
local tot = 0
}
else {
unab treat_or_untreat : `treat'
local tot = 1
}
// implement mata CIC routine
// function has no arguments. Mata will access Stata local macros.
ereturn clear
tempname mata_b mata_V
mata: cic_caller()
// post results to ereturn
di as txt _n "Changes in Changes (CIC) Model"
if (`bsreps'>0 & !mi(`bsreps')) {
// option - save bs reps into a specified file (`saving' contains ", replace" if it was provided)
if !mi(`"`saving'"') copy `bstempfile' `saving'
// post boostrapped estimates with standard errors using bstat
`=cond(`sepercentile',"quietly","")' /// quietly if need to call sepercentile
bstat using `bstempfile', stat( `mata_b' ) `bsopts' `diopts' `level'
if ( `sepercentile' ) {
if (e(level)!=95) {
ereturn display
di as error "-sepercentile- sub-option only works with level(95). Standard errors displayed above are from Stata's default method of producing standard errors."
error 198
}
if `bsreps' < 1000 nois di as error "Warning: More bootstrap repetitions might be needed with the -sepercentile- bootstrapping sub-option.."
tempname ci_se
mata : bs_se( "e(ci_percentile)", "`ci_se'" )
ereturn repost V = `ci_se'
ereturn display
}
}
else if ("`vce'"=="delta") {
// otherwise just use ereturn to get pretty estimates table
di as txt _col(49) "Number of obs =" as res %8.0fc = e(N)
ereturn post `mata_b' `mata_V' [`weight'`exp'], depname(`y') obs(`n') esample(`touse') dof(`=`n'-colsof(`mata_b')') `level'
ereturn display
}
else {
// otherwise just use ereturn to get pretty estimates table
di as txt _col(49) "Number of obs =" as res %8.0fc = e(N)
ereturn post `mata_b' [`weight'`exp'], depname(`y') obs(`n') esample(`touse') dof(`=`n'-colsof(`mata_b')') `level'
ereturn display
}
if (`: list sizeof varlist'!=0 | `runDID') {
ereturn scalar k_eq = 5
ereturn local eqnames continuous discrete_ci dci_lower_bnd dci_upper_bnd did
}
else {
ereturn scalar k_eq = 4
ereturn local eqnames continuous discrete_ci dci_lower_bnd dci_upper_bnd
}
if "`untreated'"=="" ereturn local footnote "Effect of Treatment on the Treated Group"
else ereturn local footnote "Effect of Treatment on the Untreated Group"
di as txt e(footnote)
if `runDID' di as txt "Traditional DID model" as res " [did], [did_model]" as txt =cond(`tot',""," (`untreat' == 1 - `treat')")
if `runDID' di as txt "Quantile DID model" as res " [qdid]"
if inlist("`estimator'","all","continuous","check") di as txt "Continuous CIC model" as res " [continuous]"
if inlist("`estimator'","all","dci") di as txt "Discrete CIC model (under the conditional independence assumption)" as res " [discrete_ci]"
if inlist("`estimator'","all","bounds") di as txt "Lower bound for the discrete CIC model (without conditional independence)" as res " [dci_lower_bnd]"
if inlist("`estimator'","all","bounds") di as txt "Upper bound for the discrete CIC model (without conditional independence)" as res " [dci_upper_bnd]"
ereturn local depvar "`y'"
ereturn local vce "`vce'"
ereturn local estimator "`estimator'"
end // end of cic program definition
// subroutine to replay estimates
program Replay
syntax [, notable noHeader noRULES OR GROUPED *]
_get_diopts diopts, `options'
local diopts `diopts' `table' `header' `legend'
_prefix_display, `diopts' `table' `header' `legend'
di as txt "(" e(footnote) ")"
end
// subroutine to parse the vce() option
// this section is similar in function to the "_vce_parse" command, except that I set default values for reps() and strata()
program define cic_vce_parse, rclass
version 11.2
syntax , vce(string asis)
_parse comma vce 0 : vce
if inlist( "`vce'","bootstra","bootstr","bootst","boots","boot","boo","bo","b") local vce "bootstrap"
if inlist( "`vce'","delt","del","de","d") local vce "delta"
if inlist( "`vce'","non","no","n") local vce "none"
if ("`vce'"!="bootstrap") & !mi("`0'") {
di as error "suboptions are not allowed with vce(`vce')"
error 198
}
return local vce `vce'
if ("`vce'"=="bootstrap") {
syntax [, Reps(integer 200) size(real 0) SAving(string asis) NODots SEPercentile *]
return scalar bsreps = `reps'
return scalar bssize = `size'
return scalar dots = ( "nodots"!="`dots'" )
return scalar sepercentile = ( "sepercentile"=="`sepercentile'" )
return local saving : copy local saving
return local bsopts : copy local options
}
else if ("`vce'"=="delta") {
return scalar bsreps = -1
return scalar dots = 0
return scalar bssize = 0
}
else if ("`vce'"=="none") {
return scalar bsreps = 0
return scalar dots = 0
return scalar bssize = 0
}
else {
di as error "Only vce(delta), vce(bootstrap [, subopts]), and vce(none) allowed."
error 198
}
end
/* * * * * BEGIN MATA BLOCK * * * * */
version 11.2
mata:
// STRUCTURES FOR RETURNING RESULTS
struct cic_result {
real rowvector con, dci, dscrt_low, dscrt_upp, se
}
struct did_result {
pointer(real colvector) Y
real colvector coef
real scalar did
}
// CIC CALLER -- THIS FUNCTION READS STATA DATA INTO MATA AND CALLS THE MAIN CIC ROUTINE
void cic_caller()
{
// Output: Output is returned to Stata through various st_*() functions.
// Inputs:
string rowvector varlist
string scalar y_varname, touse_var, estimator, wgt_var, mata_b, mata_V
real scalar did, tot, bsreps, dots, round, haswgt, bssize
real rowvector at
// Input #1. Name of variables, in the following order:
// - dependent variable, `y'
// - treatment dummy (0/1 for control/treatment groups)
// - time period dummy (0/1 for pre/post periods)
// - (Optional) control variables
varlist = (st_local("y") , st_local("treat_or_untreat") , st_local("post"), tokens(st_local("varlist")))
y_varname = st_local("y")
// Input #2. Name of variable indicating which rows to include, `touse'
touse_var = st_local("touse")
// Input #3. Name of local macro containing quantiles of interest, ranging from 0 to 100 percent (`at')
at = strtoreal(tokens(st_local("at"))) / 100
if (min(at)<0 | max(at)>1) _error( "at() must be between 0% and 100% (inclusive)." )
// Input #4. 0/1 to estimate difference in difference (DID) and quantile DID model. (always estimated if there are control variables)
did = strtoreal(st_local("runDID"))
// Input #5. 0/1 if estimates effect of treatment on the treated (=1) or effect of treatment on the untreated (=0)
// if untreated, you must provide a variable that =0 if treated and =1 if untreated
// setting tot==0 simply flips the sign of the cic() output
tot = strtoreal(st_local("tot"))
// Input #6. String [all|continuous|dci|lower|upper] specifying whether you want all four estimators or just one of them
estimator = st_local("estimator")
// Input #7. How to calculate standard errors:
// - if >1, bootstrapped standard errors and bsreps = number of bootstrap repetitions
// - if 0, no standard errors
// - if -1, standard error for conditional independence based on numerical differentiation
bsreps = strtoreal(st_local("bsreps"))
// Input #8. 0/1 to hide bootstrapping dots (=1 to show dots)
dots = strtoreal(st_local("dots"))
// Input #9. Round y to the nearest ___. (set =0 for no rounding).
round = strtoreal(st_local("round"))
// Input #10. Name of variable with fweight or iweight
haswgt = strtoreal(st_local("haswgt"))
// Input #11 & #12. Name of matrix to leave the coefficients and V-C matrix
mata_b = st_local("mata_b")
mata_V = st_local("mata_V")
// (Optional) Input #12. Name of variable with fweight or iweight (null if no weights)
wgt_var = st_local("wgtvar")
// (Optional) Input #13. Sample Size (This is only used if bootstrapping SEs with weights)
// Set to 0 if using fweights or if there are no weights.
// Provide population size for scaling i/p/aweights.
bssize = strtoreal(st_local("bssize"))
// Read y, treat and post into mata
real colvector y, treat, post
st_view(y =.,.,varlist[1],touse_var) // note that rows with missing data are already dropped by -marksample- in .ado file
st_view(treat=.,.,varlist[2],touse_var)
st_view(post =.,.,varlist[3],touse_var)
if ((uniqrows(treat),uniqrows(post))!=(0,0\1,1)) {
_error( "treat and post must be dummy variables equal to 0 and 1" )
}
real scalar N
N = rows(y)
// read control variables into mata (if need to run DID model)
if (did) {
real matrix rhs
st_view(rhs =.,.,invtokens(varlist[2..length(varlist)]),touse_var) // note that rows with missing data are already dropped by -marksample- in .ado file
}
// read weights into mata (if there are any)
if (haswgt) {
real colvector wgt
st_view(wgt=.,.,wgt_var,touse_var)
}
else wgt=1
// Results will be returned into a structures (defined above)
struct cic_result scalar result
struct did_result scalar didresult
// DID regression
// After this section, upper-case Y is now the dependent variable for
// the cic() function. It is a pointer. It points to (lower case) y
// or a (temporary) variable that is adjusted for covariates and/or rounded.
pointer(real colvector) scalar Y
Y = &y
if (did) {
didresult = did(y, rhs, wgt, round)
swap(Y,didresult.Y)
}
else if (round!=0) Y = &round(y,round)
// Permutation vectors identifying the four treat*post groups
real colvector p00, p01, p10, p11
st_select(p00=.,(1::N),(treat:==0 :& post:==0))
st_select(p01=.,(1::N),(treat:==0 :& post:==1))
st_select(p10=.,(1::N),(treat:==1 :& post:==0))
st_select(p11=.,(1::N),(treat:==1 :& post:==1))
// Number of observations
real scalar N00, N01, N10, N11
N00 = rows(p00);
N01 = rows(p01)
N10 = rows(p10)
N11 = rows(p11)
if (min((N00,N01,N10,N11))<1) _error( "One or more of the four treat*post groups is empty.")
if (min((N00,N01,N10,N11))<2 & bsreps>0) _error( "One or more group has size less than 2. There will be no variation in bootstrap draws.")
// Call the quantile DID routine
if (did) {
real rowvector qdid_result
if (!haswgt) qdid_result=qdid((*Y)[p00],(*Y)[p01],(*Y)[p10],(*Y)[p11],at) // without weights
else qdid_result=qdid((*Y)[p00],(*Y)[p01],(*Y)[p10],(*Y)[p11],at,wgt[p00],wgt[p01],wgt[p10],wgt[p11]) // with weights
}
// Call the main CIC routine
if (!haswgt) result=cic((*Y)[p00],(*Y)[p01],(*Y)[p10],(*Y)[p11],at,estimator) // without weights
else result=cic((*Y)[p00],(*Y)[p01],(*Y)[p10],(*Y)[p11],at,estimator,wgt[p00],wgt[p01],wgt[p10],wgt[p11]) // with weights
// return results into a Stata matrix named `mata_b'
if (did) {
if (tot) st_matrix(mata_b, (didresult.coef', didresult.did, qdid_result, result.con, result.dci, result.dscrt_low, result.dscrt_upp))
else st_matrix(mata_b, (didresult.coef',-didresult.did,-qdid_result,-result.con,-result.dci,-result.dscrt_upp,-result.dscrt_low))
}
else {
if (tot) st_matrix(mata_b, (result.con,result.dci,result.dscrt_low,result.dscrt_upp))
else st_matrix(mata_b, -(result.con,result.dci,result.dscrt_upp,result.dscrt_low))
}
// matrix labels for `mata_b'
string matrix ciclabels, didlabels, qdidlabels
ciclabels=(J(cols(result.con),1,"continuous") \ J(cols(result.dci),1,"discrete_ci") \ J(cols(result.dscrt_low),1,"dci_lower_bnd") \ J(cols(result.dscrt_upp),1,"dci_upper_bnd") )
if (estimator=="all") ciclabels=(ciclabels, J(4,1,strtoname(("mean" , ("q":+strofreal(at*100))))'))
else if (estimator=="continuous") ciclabels=(ciclabels, J(1,1,strtoname(("mean" , ("q":+strofreal(at*100))))'))
else if (estimator=="dci") ciclabels=(ciclabels, J(1,1,strtoname(("mean" , ("q":+strofreal(at*100))))'))
else if (estimator=="bounds") ciclabels=(ciclabels, J(2,1,strtoname(("mean" , ("q":+strofreal(at*100))))'))
else if (estimator=="check") ciclabels=(ciclabels, J(4,1,strtoname(("mean" , ("q":+strofreal(at*100))))'))
if (did) {
if (cols(rhs)==2) didlabels = (J(rows(didresult.coef),1,"did_model"),( ( "0."+varlist[2]+"#0."+varlist[3]) \( "0."+varlist[2]+"#1."+varlist[3]) \( "1."+varlist[2]+"#0."+varlist[3]) \( "1."+varlist[2]+"#1."+varlist[3])))
else didlabels = (J(rows(didresult.coef),1,"did_model"),( ( "0."+varlist[2]+"#0."+varlist[3]) \( "0."+varlist[2]+"#1."+varlist[3]) \( "1."+varlist[2]+"#0."+varlist[3]) \( "1."+varlist[2]+"#1."+varlist[3]) \ varlist[4..length(varlist)]'))
qdidlabels=(J(length(at),1,"qdid"),strtoname("q":+strofreal(at*100))')
ciclabels = ( didlabels \ ( "did", "did" ) \ qdidlabels \ ciclabels )
}
st_matrixcolstripe(mata_b, ciclabels)
st_matrixrowstripe(mata_b, J( rows(st_matrix(mata_b)), 1, ("", y_varname)) )
st_local("cic_coleq" ,invtokens(ciclabels[.,1]'))
st_local("cic_colnames",invtokens(ciclabels[.,2]'))
// return
st_matrix("e(at)",at)
st_numscalar( "e(N_strata)", 4)
if (!haswgt) {
st_numscalar( "e(N)" , N)
st_numscalar( "e(N00)" , N00)
st_numscalar( "e(N01)" , N01)
st_numscalar( "e(N10)" , N10)
st_numscalar( "e(N11)" , N11)
}
else if (bssize) {
st_numscalar( "e(N)" , round(bssize))
st_numscalar( "e(N_obs)" , N)
}
else {
st_numscalar( "e(N)" , round(sum(wgt)))
st_numscalar( "e(N_obs)" , N)
}
st_numscalar( "e(N_support)",rows(uniqrows(*Y)))
// Bootstrapping
if (bsreps>0) {
real scalar b
real colvector bs_wgt
struct did_result scalar bs_didresult
struct cic_result scalar bs_cicresult
real rowvector bs_qdidresult
// pointer to y
// a new pointer is needed for dependent variable since it might be adjusted for covariates with boostrap sample
pointer(real colvector) scalar bs_Y
bs_Y = Y
// empty matrix to store results
real matrix bsdata
bsdata=J(bsreps,cols(st_matrix(mata_b)),.)
// Before loop, extra setup needed for drawing a sample with unequal weights
if (haswgt) {
// weight variables with cumulative sum of the weights from each group
real colvector cumsum00, cumsum01, cumsum10, cumsum11
cumsum00 = quadrunningsum(wgt[p00])
cumsum01 = quadrunningsum(wgt[p01])
cumsum10 = quadrunningsum(wgt[p10])
cumsum11 = quadrunningsum(wgt[p11])
// scalars with population size for each group
real scalar popsize00, popsize01, popsize10, popsize11
if (bssize) {
// With weights (other than frequency weights), use the fraction of bssize in the group (e.g., popsize00 = round(cumsum00[n00]/colsum(wgt)*bssize))
// with iweghts, bssize==sum of the weights by default.
// With pweights or aweights, sumwgt==bssize by default, so the sum of the weights is just the sum of the weights, rounded to the nearest integer
real scalar sumwgt
sumwgt = quadcolsum(wgt)
popsize00 = round(cumsum00[N00]/sumwgt*bssize) // the number of obs. in each group is rounded to the nearest integer
popsize01 = round(cumsum01[N01]/sumwgt*bssize)
popsize10 = round(cumsum10[N10]/sumwgt*bssize)
popsize11 = round(cumsum11[N11]/sumwgt*bssize)
}
else {
// With frequency weights, this is the weighted number of individuals in the group (e.g., popsize00 = cumsum00[n00])
popsize00 = cumsum00[N00]
popsize01 = cumsum01[N01]
popsize10 = cumsum10[N10]
popsize11 = cumsum11[N11]
if (round(popsize00)!=popsize00 | round(popsize01)!=popsize01 | round(popsize10)!=popsize10 | round(popsize11)!=popsize11) "When drawing bootstrap sample with frequency weights, non-integer fweights were found."
}
cumsum00 = cumsum00/cumsum00[N00] // normalize to sum to one within groups
cumsum01 = cumsum01/cumsum01[N01]
cumsum10 = cumsum10/cumsum10[N10]
cumsum11 = cumsum11/cumsum11[N11]
if (min((popsize00,popsize01,popsize10,popsize11))<2) "One or more groups has size <=1. There will be no variation in bootstrap draws."
}
// header for dots
if (dots) {
printf( "{txt}\nBootstrap replications ({res}%g{txt})\n", bsreps)
display( "{txt}{hline 4}{c +}{hline 3} 1 " +
"{hline 3}{c +}{hline 3} 2 " + "{hline 3}{c +}{hline 3} 3 " +
"{hline 3}{c +}{hline 3} 4 " + "{hline 3}{c +}{hline 3} 5 ")
}
// Bootstrapping replications
for(b=1; b<=bsreps; ++b) {
if (haswgt | did) {
// if estimating DID model or have a weighted sample, the bootstrap sample
// is "drawn" by creating a new weight vector.
if (!haswgt) bs_wgt = bs_draw_wgt(p00, p01, p10, p11, N00, N01, N10, N11)
else bs_wgt = bs_draw_wgt(p00, p01, p10, p11, N00, N01, N10, N11, cumsum00, cumsum01, cumsum10, cumsum11, popsize00, popsize01, popsize10, popsize11)
// calculate DID and adjust for covariates w/ bootstrap sample
if (did) {
bs_didresult = did(y, rhs, bs_wgt, round)
swap(bs_Y,bs_didresult.Y)
bs_qdidresult = qdid((*bs_Y)[p00],(*bs_Y)[p01],(*bs_Y)[p10],(*bs_Y)[p11],at,bs_wgt[p00],bs_wgt[p01],bs_wgt[p10],bs_wgt[p11])
}
// call cic() with bootstrap sample
bs_cicresult=cic((*bs_Y)[p00],(*bs_Y)[p01],(*bs_Y)[p10],(*bs_Y)[p11],at,estimator,bs_wgt[p00],bs_wgt[p01],bs_wgt[p10],bs_wgt[p11])
}
else {
// in the simple case of no regression adjustment and no weights, simply
// call cic() with a random draw of dependent variable
bs_cicresult=cic(bs_draw_nowgt((*bs_Y)[p00]),bs_draw_nowgt((*bs_Y)[p01]),bs_draw_nowgt((*bs_Y)[p10]),bs_draw_nowgt((*bs_Y)[p11]),at,estimator)
}
// save estimates into a matrix with one row per bootstrap sample
if (did) {
if (tot==1) bsdata[b,.] = (bs_didresult.coef',bs_didresult.did,bs_qdidresult,bs_cicresult.con,bs_cicresult.dci,bs_cicresult.dscrt_low,bs_cicresult.dscrt_upp)
else bsdata[b,.] = -(bs_didresult.coef',bs_didresult.did,bs_qdidresult,bs_cicresult.con,bs_cicresult.dci,bs_cicresult.dscrt_upp,bs_cicresult.dscrt_low)
}
else {
if (tot==1) bsdata[b,.] = (bs_cicresult.con,bs_cicresult.dci,bs_cicresult.dscrt_low,bs_cicresult.dscrt_upp)
else bsdata[b,.] = -(bs_cicresult.con,bs_cicresult.dci,bs_cicresult.dscrt_upp,bs_cicresult.dscrt_low)
}
// show dots
if (dots) {
if (missing(bsdata[b,.])) printf( "{err}x{txt}")
else printf( ".")
if (!mod(b,50)) printf( " %5.0f\n",b)
else if (b==bsreps & mod(b-1,50)) display("") // end of dots
displayflush()
}
} // end loop through bs iterations
// save bootstrap iterations in a temporary .dta file (named `bstempfile')
stata( "preserve" )
string rowvector bstempfile, bstempvars
// clear data (after preserve) and fill with bsdata matrix
st_dropvar(.)
st_addobs(rows(bsdata))
bstempvars=strtoname("_bs_":+strofreal(1::cols(bsdata)))'
st_store(.,st_addvar("double",bstempvars), bsdata)
// setup file for bstat command
st_global( "_dta[bs_version]" , "3")
if (!haswgt) st_global( "_dta[N]", strofreal(N))
else if (bssize) st_global( "_dta[N]", strofreal(bssize))
else st_global( "_dta[N]", strofreal(round(sum(wgt))))
st_global( "_dta[N_strata]" , "4")
st_global( "_dta[strata]" , (varlist[2] + " " + varlist[1]))
st_global( "_dta[command]" , "cic")
if (did) st_global( "_dta[k_eq]", "6")
else st_global( "_dta[k_eq]", "4")
st_global( "_dta[k_extra]" , "0")
for(b=1; b<=cols(bsdata); ++b) {
if (did) {
if (tot==1) st_global( (bstempvars[1,b]+"[observed]") , strofreal( (didresult.coef',didresult.did,qdid_result,result.con,result.dci,result.dscrt_low,result.dscrt_upp)[1,b]))
else st_global( (bstempvars[1,b]+"[observed]") , strofreal(-(didresult.coef',didresult.did,qdid_result,result.con,result.dci,result.dscrt_upp,result.dscrt_low)[1,b]))
}
else {
if (tot==1) st_global( (bstempvars[1,b]+"[observed]") , strofreal( (result.con,result.dci,result.dscrt_low,result.dscrt_upp)[1,b]))
else st_global( (bstempvars[1,b]+"[observed]") , strofreal(-(result.con,result.dci,result.dscrt_upp,result.dscrt_low)[1,b]))
}
st_global( (bstempvars[1,b]+"[expression]"), ( "["+ciclabels[b,1]+"]_b["+ciclabels[b,2]+"]"))
st_global( (bstempvars[1,b]+"[coleq]") , ciclabels[b,1])
st_global( (bstempvars[1,b]+"[colname]") , ciclabels[b,2])
st_global( (bstempvars[1,b]+"[is_eexp]") , "1" )
}
// save as `bstempfile'
bstempfile=st_tempfilename()
st_local( "bstempfile",bstempfile)
stata(( "qui save " + bstempfile ))
stata( "restore" )
} // done bootstrapping
else if (bsreps==-1) {
// Standard errors calculated via the Delta Method
real matrix V_delta
V_delta = se_cic((*Y)[p00],(*Y)[p01],(*Y)[p10],(*Y)[p11],result)
// turn from a 1x4 vector into a square matrix with the same number of columns as e(b), then post back into Stata
V_delta = (( V_delta[1], J(1,length(at),0), V_delta[2], J(1,length(at),0), V_delta[3], J(1,length(at),0), V_delta[4], J(1,length(at),0)))
if (did) V_delta = (J(1,rows(didlabels)+1+rows(qdidlabels),0),V_delta)
st_matrix(mata_V,diag(V_delta))
st_matrixcolstripe(mata_V, ciclabels)
st_matrixrowstripe(mata_V, ciclabels)
}
} // end of cic_caller; everything is returned to Stata with st_*() commands.
// >>>>>>>>>> check that column names and such are the same as the output in the example in the NOTE (below)
// CIC ROUTINE
struct cic_result scalar cic(real colvector Y00, real colvector Y01, real colvector Y10, real colvector Y11, real rowvector at, string scalar estimator, | real colvector W00, real colvector W01, real colvector W10, real colvector W11 )
{
// Inputs:
// (1)-(4) Four column vectors with dependent variable
// - Y00 is data for the control group in pre-period
// - Y01 is data for the control group in post period
// - Y10 is data for the treatment group in post period
// - Y11 is data for the treatment group in post period
// (5) Vector with k>=1 quantiles of interest, ranging from 0 to 1 (you can set this to missing to skip)
// (6) String = [all|continuous|dci|lower|upper] specifying whether you want all four estimators or just one of them
// (7)-(10) (Optional) Column with fweights or iweights for Y00, Y01, Y10, and Y11 (respectively)
//
// Output: One structure (cic_result) with four row vectors.
// Each vector has (1+k) elements. The first element is the mean, followed by k results (one for each quantile in -at-).
// (1) result.con = CIC ESTIMATOR WITH CONTINUOUS OUTCOMES, EQUATION 9
// (2) result.dci = CIC MODEL WITH DISCRETE OUTCOMES (UNDER THE CONDITIONAL INDEPENDENCE ASSUMPTION), EQUATION 29
// (3) result.dscrt_low = LOWER BOUND ESTIMATE OF DISCRETE CIC MODEL (WITHOUT CONDITIONAL INDEPENDENCE), EQUATION 25
// (4) result.dscrt_upp = UPPER BOUND ESTIMATE OF DISCRETE CIC MODEL (WITHOUT CONDITIONAL INDEPENDENCE), EQUATION 25
// Three of the four vectors will be void if option (6) is not "all"
// Need all or none of args (7)-(10)
if (args()!=6 & args()!=10) _error(( "cic() expected 6 or 10 arguments, but received " + strofreal(args()))) + " arguments"
// Vector with support points for all four groups combined (YS) and the comparison-post group (YS01)
real colvector YS, YS01
YS01 = uniqrows(Y01)
YS = uniqrows(Y00\YS01\Y10\Y11)
if (length(YS)<2) _error("The dependent variable is a constant")
// Vector with CDF functions of the four treat*post groups (F00,F01,F10,F11)
// Sample proportions (w/ or w/out weights)
real colvector f00, f01, f10, f11
f00=(args()==6 ? prob(Y00,YS) : prob(Y00,YS,W00))
f01=(args()==6 ? prob(Y01,YS) : prob(Y01,YS,W01))
f10=(args()==6 ? prob(Y10,YS) : prob(Y10,YS,W10))
f11=(args()==6 ? prob(Y11,YS) : prob(Y11,YS,W11))
// Results will be returned into a structure w/ 4 vectors for continuous, dci, lower, upper
// Each vector has mean estimate in first column, plus one column for each element of "at"
struct cic_result scalar result
if (estimator=="all") {
// Run all four estimators
result = cic_all(f00,f01,f10,f11,YS,YS01,at)
}
else if (estimator=="continuous") {
// Only run CIC estimator with continuous outcomes, equation 9
result.con = cic_con(f00,f01,f10,f11,YS,YS01,at)
}
else if (estimator=="dci") {
// Only run CIC model with discrete outcomes (under the conditional independence assumption), equation 29
result.dci = cic_dci(f00,f01,f10,f11,YS,YS01,at)
}
else if (estimator=="bounds") {
// Only run lower & upper bound estimates for discrete CIC model (without conditional independence), equation 25
result.dscrt_low = cic_lower(f00,f01,f10,f11,YS,YS01,at)
result.dscrt_upp = cic_upper(f00,f01,f10,f11,YS,YS01,at)
}
else if (estimator=="check") {
// Undocumented option for testing purposes
// Run cic_all and also the four separate functions, and check that they give the same results
result = cic_all(f00,f01,f10,f11,YS,YS01,at)
struct cic_result scalar check
check.con = cic_con(f00,f01,f10,f11,YS,YS01,at)
check.dci = cic_dci(f00,f01,f10,f11,YS,YS01,at)
check.dscrt_low = cic_lower(f00,f01,f10,f11,YS,YS01,at)
check.dscrt_upp = cic_upper(f00,f01,f10,f11,YS,YS01,at)
if (result.con==check.con
& result.dci==check.dci
& result.dscrt_low==check.dscrt_low
& result.dscrt_upp==check.dscrt_upp) "Elements are equal"
else {
"result.con"; result.con;
"result.dci"; result.dci;
"result.dscrt_low"; result.dscrt_low;
"result.dscrt_upp"; result.dscrt_upp;
"check.con"; check.con;
"check.dci"; check.dci;
"check.dscrt_low"; check.dscrt_low;
"check.dscrt_upp"; check.dscrt_upp;
_error( "Elements not equal")
}
}
else {
_error("Estimator (argument #6) must be one of the following: all, continuous, dci, lower, or upper}")
}
// DONE. RETURN STRUCTURE W/ FOUR ROW VECTORS.
return(result)
} // end of cic
// ALL FOUR ESTIMATORS
// - CIC ESTIMATOR WITH CONTINUOUS OUTCOMES, EQUATION 9
// - CIC MODEL WITH DISCRETE OUTCOMES (UNDER THE CONDITIONAL INDEPENDENCE ASSUMPTION), EQUATION 29
// - LOWER BOUND ESTIMATE OF DISCRETE CIC MODEL (WITHOUT CONDITIONAL INDEPENDENCE), EQUATION 25
// - UPPER BOUND ESTIMATE OF DISCRETE CIC MODEL (WITHOUT CONDITIONAL INDEPENDENCE), EQUATION 25
// The code in cic_all() is somewhat convoluted because I am calculating all four vectors simultaneously.
// It is faster than, but equivalent to, running cic_con(), cic_dci(), cic_lower(), and cic_upper() sequentially.
struct cic_result cic_all(real vector f00, real vector f01, real vector f10, real vector f11, real vector YS, real vector YS01, real vector at)
{
real colvector F00, F01, F10, F11
// Results will be returned into a structure w/ 4 vectors for con, dci, lower, upper
// Each vector has mean estimate in first column, plus one column for each element of "at"
struct cic_result scalar result
// CDFs (Because of rounding, sum of probabilities might be slightly different than one)
F00=runningsum(f00); F00[length(F00)]=1
F01=runningsum(f01); F01[length(F01)]=1
F10=runningsum(f10); F10[length(F10)]=1
F11=runningsum(f11); F11[length(F11)]=1
// First, estimate the cdf of Y^N_11 using equation (9) in the paper.
// Second, use that to calculate the average effect of the treatment.
// For each y in the support of Y01, fill in FCO(y)=F_10(F^-1_00(F_01(y))).
real vector FCO,FLB,FUB,FDCI,FDCI_weight
real scalar i,F01y,F00invF01y,F00invbF01y,F00F00invF01y,F00F00invbF01y,cdfinv_at_i
FCO=FDCI=FLB=FUB=J(length(YS01),1,0)
for(i=1; i<=length(YS01); ++i) {
F01y=cdf(YS01[i],F01,YS)
F00invF01y=cdfinv(F01y,F00,YS)
F00invbF01y=cdfinv_brckt(F01y,F00,YS)
F00F00invF01y =cdf(F00invF01y,F00,YS)
F00F00invbF01y=cdf(F00invbF01y,F00,YS)
FCO[i]=FUB[i]=cdf(F00invF01y,F10,YS)
FLB[i]=cdf(F00invbF01y,F10,YS)
if ((F00F00invF01y-F00F00invbF01y)>epsilon(1)) FDCI_weight=(F01y-F00F00invbF01y)/(F00F00invF01y-F00F00invbF01y)
else FDCI_weight=0
FDCI[i]=FLB[i]+(FUB[i]-FLB[i])*FDCI_weight
}
FCO[length(FCO)]=FDCI[length(FDCI)]=FLB[length(FLB)]=FUB[length(FUB)]=1 // =1 in last row
// MEAN: CIC ESTIMATOR WITH CONTINUOUS OUTCOMES, EQUATION 9
result.con=( (F11-(0 \ F11[1..(length(YS)-1)]))'*YS - (FCO-(0 \ FCO[1..(length(YS01)-1)]))'*YS01 )
// MEAN: CIC MODEL WITH DISCRETE OUTCOMES (UNDER THE CONDITIONAL INDEPENDENCE ASSUMPTION), EQUATION 29
result.dci=( (F11-(0 \ F11[1..(length(YS)-1)]))'*YS - (FDCI-(0 \ FDCI[1..(length(YS01)-1)]))'*YS01 )
// MEAN: LOWER BOUND ESTIMATE OF DISCRETE CIC MODEL (WITHOUT CONDITIONAL INDEPENDENCE), EQUATION 25
result.dscrt_low =( (F11-(0 \ F11[1..(length(YS)-1)]))'*YS - (FLB-(0 \ FLB[1..(length(YS01)-1)]))'*YS01 )
// MEAN: UPPER BOUND ESTIMATE OF DISCRETE CIC MODEL (WITHOUT CONDITIONAL INDEPENDENCE), EQUATION 25
result.dscrt_upp=( (F11-(0 \ F11[1..(length(YS)-1)]))'*YS - (FUB-(0 \ FUB[1..(length(YS01)-1)]))'*YS01 )
// QUANTILES (each loop adds a cell to the result.* vector for each quantile in at)
if (!missing(at)) {
for(i=1; i<=length(at); ++i) {
cdfinv_at_i = cdfinv(at[i], F11, YS)
// CIC ESTIMATOR WITH CONTINUOUS OUTCOMES, EQUATION 9
result.con = (result.con , ( cdfinv_at_i - cdfinv(at[i], FCO, YS01) ) )
// CIC MODEL WITH DISCRETE OUTCOMES (UNDER THE CONDITIONAL INDEPENDENCE ASSUMPTION), EQUATION 29
result.dci = (result.dci , ( cdfinv_at_i - cdfinv(at[i], FDCI, YS01) ) )
// LOWER BOUND ESTIMATE OF DISCRETE CIC MODEL (WITHOUT CONDITIONAL INDEPENDENCE), EQUATION 25
result.dscrt_low = (result.dscrt_low, ( cdfinv_at_i - cdfinv(at[i], FLB, YS01) ) )
// UPPER BOUND ESTIMATE OF DISCRETE CIC MODEL (WITHOUT CONDITIONAL INDEPENDENCE), EQUATION 25
result.dscrt_upp = (result.dscrt_upp, ( cdfinv_at_i - cdfinv(at[i], FUB, YS01) ) )
}
}
// DONE. RETURN STRUCTURE W/ FOUR ROW VECTORS.
return(result)
} // end of cic_all
// TRADITIONAL DIFFERENCES IN DIFFERENCES REGERSSION (OLS)
struct did_result scalar did(real colvector y, real matrix rhs, real colvector wgt, real scalar round)
{
// Inputs:
// (1) y, the dependent variable
// (2) rhs, matrix of independent variables
// - first column is treat
// - second column is post
// - (optional) remaining columns are covariates
// (3) wgt, a column vector of fweights or iweights (can set to scalar =1 for no weights)
// (4) round, a scalar indicating the nearest unit for rounding Y (=0 for no rounding)
//
// Output: One structure (did_result) with:
// 1. *Y, pointer to adjusted variable (pointing to either a temporary variable or input y itself)
// 2. a vector with coefficients from the DID regression
// Nx4 matrix with dummies indicating group membership to p00, p01, p10, p11 (respectively)
real matrix D
D = ( ((-rhs[.,1]:+1):*(-rhs[.,2]:+1)), ((-rhs[.,1]:+1):*(rhs[.,2])), ((rhs[.,1]):*(-rhs[.,2]:+1)), ((rhs[.,1]):*(rhs[.,2])))
// OLS DID regression
struct did_result scalar didresult
if (cols(rhs)==2) didresult.coef = invsym(quadcross(D,wgt,D))*quadcross(D,wgt,y)
else didresult.coef = invsym(quadcross((D,rhs[.,3..cols(rhs)]),wgt,(D,rhs[.,3..cols(rhs)])))*quadcross((D,rhs[.,3..cols(rhs)]),wgt,y)
// DIFF-IN-DIFF estimate
didresult.did = didresult.coef[4] - didresult.coef[2] - didresult.coef[3] + didresult.coef[1]
// Dependent variable (potentially adjusted for covariates or rounded)
if (cols(rhs)>2) {
// adjust for covariates
// yadj = y - X * _b[X]
// = D * _b[D] + resid (yadj is also rounded if round!=0)
//
// notice that control variables are columns 3 to cols(rhs) of the matrix rhs,
// but are in rows 5 to rows(didresult.coef) of the regression's independent variables
// because the treat/post dummies (the first two variables in rhs) were turned
// into four group dummies in the regression
didresult.Y = &round( y - rhs[.,3..cols(rhs)]*didresult.coef[5..rows(didresult.coef),1] , round)
}
else if (round!=0) {
// no covariaters but need to round y
didresult.Y = &round(y,round)
}
else {
// no covariaters or rounding, just point to input vector
didresult.Y = &y
}
return(didresult)
}
// QUANTILE DID MODEL, EQUATION 22
real rowvector qdid(real colvector Y00, real colvector Y01, real colvector Y10, real colvector Y11, real rowvector at, | real colvector W00, real colvector W01, real colvector W10, real colvector W11 )
{
// Inputs:
// (1)-(4) Four column vectors with dependent variable
// - Y00 is data for the control group in pre-period
// - Y01 is data for the control group in post period
// - Y10 is data for the treatment group in post period
// - Y11 is data for the treatment group in post period
// (5) Vector with k>=1 quantiles of interest, ranging from 0 to 1
// (6)-(9) (Optional) Column with fweights or iweights for Y00, Y01, Y10, and Y11 (respectively)
//
// Output: Vector with (k) elements. (one for each quantile in -at-).
real rowvector qdid; qdid = J(1,length(at),.)
real scalar i
// Need all or none of args (6)-(9)
if (args()>5 & args()!=9) _error(( "Expected 5 or 9 arguements, but received " + strofreal(args())))
if (args()==5) {
// No weights
for(i=1; i<=length(at); ++i) {
qdid[i] = cumdfinv(Y11,at[i])-cumdfinv(Y10:+mean(Y01):-mean(Y00),at[i])
}
}
else {
// With weights
for(i=1; i<=length(at); ++i) {
qdid[i] = cumdfinv(Y11,at[1,i],W11)-cumdfinv(Y10:+mean(Y01,W01):-mean(Y00,W00),at[1,i],W10)
}
}
return(qdid)
}
// SAMPLE PROPORTIONS
real vector prob(real vector Y, real vector YS, |real vector wgt)
{
// given a vector Y and a vector of support points YS
// this function calculates the sample proportions at each of the support points
// wgt is an (optional) set of weights for the vector Y
real scalar n
n = length(YS)
if (args()==3) {
// with weight variable
return(rowsum((abs((YS:-J(n,1,Y'))):<=epsilon(1)):*J(n,1,wgt')):/J(n,1,quadcolsum(wgt)))
}
else {
// without weights
return(rowsum(abs((YS:-J(n,1,Y'))):<=epsilon(1)):/length(Y))
}
}
// CUMULATIVE DISTRIBUTION FUNCTION
real scalar cdf(real scalar y, real vector P, real vector YS)
{
// given a cumulative distrubtion function (P) over the support points (YS),
// returns the empirical cumulative distribution function at a scalar (y)
if (y< YS[1]) return(0)
else if (y>=YS[length(YS)]) return(1)
else return(P[colsum((YS:<=(y+epsilon(y))))])
}
// INVERSE OF CUMULATIVE DISTRIBUTION FUNCTION, EQUATION 8
real scalar cdfinv(real scalar p, real vector P, real vector YS)
{
// given a cumulative distrubtion function (P) over the support points (YS),
// returns the inverse of the empirical cumulative distribution function at probability p (0<p<1)
return(YS[min((length(YS)\select((1::length(YS)),(P:>=(p-epsilon(p))))))])
}
// INVERSE OF CUMULATIVE DISTRIBUTION FUNCTION, ALTERNATIVE FOR discrete OUTCOMES, EQUATION 24
real scalar cdfinv_brckt(real scalar p, real vector P, real vector YS)
{
// given a cumulative distribution function (P) over the support points (YS),
// returns the inverse of the empirical cumulative distribution function at probability p (0<p<1)
// but if equals -oo, it returns min(YS)-100*(1+max(YS)-YS(min)) = 101*min(YS)-100*max(YS)-100
if (p>=(P[1]-epsilon(1))) {
return(YS[max(select((1::length(YS)),(P:<=(p+epsilon(p)))))])
}
else {
return(101*YS[1]-100*YS[length(YS)]-100)
}
}
// EMPIRICAL DISTRIBUTION FUNCTION
real scalar cumdfinv(real colvector X, real scalar p, |real colvector wgt)
{
// given a vector of observations (X),
// returns the empirical distribution of X evaluated at a point (p).
// optionally, the vector X can have weights (wgt)
if (p<=epsilon(p)) return(min(X))
else if (p>=1-epsilon(1)) return(max(X))
else if (args()==2) {
// without weights
real scalar r
r = length(X)*p
return(sort(X,1)[floor(r+1-epsilon(r)),1])
// Note that floor(r+1-epsilon(r)) is smallest integer larger than length(X)*p
// e.g., if length(X)=10, p=0.34 then floor(3.4+1-2.2e-16)=4
// if length(X)=10, p=0.30 then floor(3.0+1-2.2e-16)=3
}
else {
// with weights
real matrix xs, sum_wgt
xs = sort((X,wgt),1)
sum_wgt = runningsum(xs[.,2]) :/ colsum(xs[.,2])
sum_wgt[rows(xs),1]=1 // force sum of weights to 1
// return the observation from fist row where cumulative sum of wgt is larger than p
return(xs[colmin(select((1::rows(xs)),(sum_wgt:>=p) )),1])
}
}
// FOR BOOTSTRAPPING, DRAW RANDOM SAMPLE WITH REPLACEMENT
real colvector bs_draw_nowgt(real colvector x)
{
// Input: Vector we're drawing rows from
// Output: Vector with a simple random sample
// (This function is adequate when x is a vector,
// and when it is a permutatin vector)
return(x[ceil(runiform(rows(x),1):*rows(x)),1])
}
// FOR BOOTSTRAPPING, DRAW RANDOM SAMPLE WITH REPLACEMENT
real colvector bs_draw_wgt(real colvector p00, real colvector p01, real colvector p10, real colvector p11,
real scalar N00, real scalar N01, real scalar N10, real scalar N11,
| real colvector cumsum00, real colvector cumsum01, real colvector cumsum10, real colvector cumsum11,
real scalar popsize00, real scalar popsize01, real scalar popsize10, real scalar popsize11)
{
// Case 1: Unweighted
// Inputs: 1-4. Four (4) permutation vectors identifying the rows of data belonging to each group
// 5-8. Four (4) scalars with the number of observations in each group (e.g., N00=rows(p00))
// Output: A frequency weight vector
//
// Case 2: Freqency or Importance Weights (fweights or iweights)
// Inputs: 1-8. All the inputs provided in Case 1 (unweighted)
// 9-12. Four (4) weight variables with cumulative sum of the weights from each group, normalized to sum to one within groups (e.g., cumsum00 = quadrunningsum(wgt[p00]); cumsum00 = cumsum00/cumsum00[N00])
// 13-16. Four (4) scalars with population size for each group.
// - With frequency weights, this is the weighted number of individuals in the group (e.g., popsize00 = cumsum00[n00])
// - With importance weights, use the fraction of bssize (e.g., popsize00 = round(cumsum00[N00]/colsum(wgt)*bssize))
// Output: A weight vector that replaces the input vector in cic_caller (wgt)
//
// This specialzed program was written to minimize processing time for CIC bootstrapping.