forked from xuyiqing/panelview_stata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpanelview.ado
1363 lines (1204 loc) · 58 KB
/
panelview.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
/*
Version 0.1.1
Jan 23 2022
Hongyu Mou, Yiqing Xu
*/
capture program drop panelview
program define panelview
version 15.1
syntax varlist(min = 1 numeric) [if] [in] , ///
I(varname) T(varname numeric) ///
TYPE(string) ///
[ ///
continuoustreat ///
discreteoutcome ///
bytiming ///
ignoretreat ///
ignoreY ///
MYCOLor(string) ///
PREpost ///
xlabdist(integer 1) ///
ylabdist(integer 1) ///
bygroup ///
style(string) ///
byunit ///
theme(string) ///
lwd(string) ///
leavegap ///
* ///
]
//check if i and t uniquely identify the obs:
isid `i' `t'
//check needed packages
cap which colorpalette.ado
if _rc {
*di as error "colorpalette.ado required: {stata ssc install palettes, replace}"
di as error "colorpalette.ado required: {stata search gr0075} and click to install"
exit 199
}
cap which labmask.ado
if _rc {
di as error "labmask.ado required: {stata ssc install labutil, replace}"
exit 199
}
cap which sencode.ado
if _rc {
di as error "sencode.ado required: {stata ssc install sencode, replace}"
exit 199
}
cap which grc1leg.ado
if _rc {
di as error "grc1leg.ado required: {stata search grc1leg} and click to install"
exit 199
}
//check for bad option combinations:
if "`continuoustreat'" != "" {
if "`bytiming'" != "" {
di as err "option continuoustreat and bytiming may not be combined"
exit 198
}
}
if "`continuoustreat'" != "" {
if "`ignoretreat'" != "" {
di as err ///
"option continuoustreat and ignoretreat may not be combined"
exit 198
}
}
if "`continuoustreat'" != "" {
if ("`type'" == "miss" | "`type'" == "missing") {
di as err ///
"option continuoustreat and type(missing) may not be combined"
exit 198
}
}
if ("`bygroup'" != "") {
if ("`prepost'" == "") {
di as err ///
"option bygroup and prepost should be combined"
exit 198
}
}
if ("`type'" == "outcome") {
if ("`bytiming'" != "") {
di as err ///
"option type(outcome) and bytiming may not be combined"
exit 198
}
}
if ("`type'" == "outcome" | "`type'" == "bivar") {
if ("`leavegap'" != "") {
di as err ///
"option leavegap should be combined with type(treat) or type(missing)"
exit 198
}
}
if ("`type'" != "treat" & "`type'" != "miss" & "`type'" != "missing") {
if ("`ignoreY'" != "") {
di as err ///
"option ignoreY should be combined with type(treat) or type(missing)"
exit 198
}
}
if ("`ignoretreat'" != "") {
if ("`ignoreY'" != "") {
di as err ///
"option ignoreY should not be combined with ignoretreat"
exit 198
}
}
if ("`ignoretreat'" != "") {
if ("`type'" == "miss" | "`type'" == "missing") {
di as err ///
"option type(missing) should not be combined with ignoretreat"
exit 198
}
}
set trace off
//check if "type" is specified correctly:
if ("`type'" != "miss" & "`type'" != "missing" & "`type'" != "treat" & "`type'" != "outcome" & "`type'" != "bivar" &"`type'" != "bivariate") {
di as err ///
"option type can only be one of the following: treat, outcome, bivariate (bivar), and missing (miss)"
exit 198
}
preserve
tempfile backup
qui keep `varlist' `i' `t' //only keep using variables, i.e. include covariates
qui ds `varlist'
tempvar numvar
cap gen `numvar' = `: word count `r(varlist)''
if `numvar' == 1 {
if ("`ignoretreat'" == "" & "`type'" != "miss" & "`type'" != "missing" & "`type'" != "treat" & "`type'" != "outcome") {
di as err "should combine with option ignoretreat, type(missing), type(treat) , or type(outcome) when varlist has only one variable"
exit 198
}
else if ("`type'" == "miss" | "`type'" == "missing") {
tokenize `varlist'
loc outcome `1'
loc treat `1'
}
else if ("`type'" == "treat") {
tokenize `varlist'
loc treat `1'
}
else { //"`type'" == "outcome"
tokenize `varlist'
loc outcome `1'
loc ignoretreat 1
}
}
else if `numvar' == 2 {
if ("`ignoreY'" != "") {
tokenize `varlist'
loc treat `1'
}
else {
if ("`ignoretreat'" != ""|"`type'" == "miss" | "`type'" == "missing") {
tokenize `varlist'
loc outcome `1'
loc treat `1'
*loc covariates `2'
*drop if mi(`covariates')
}
else {
tokenize `varlist'
loc outcome `1'
loc treat `2'
}
}
}
else { //`numvar' >= 3:
if ("`ignoreY'" != "") {
tokenize `varlist'
loc treat `1'
}
else {
if ("`ignoretreat'" != ""|"`type'" == "miss" | "`type'" == "missing") {
tokenize `varlist'
loc outcome `1'
}
else {
tokenize `varlist'
loc outcome `1'
loc treat `2'
}
}
}
marksample touse
if "`leavegap'" == "" {
cap drop if `touse' == 0
}
else if "`leavegap'" != "" {
tempvar countmissvar minrowmiss
qui replace `touse' = . if `touse' == 0
qui egen `countmissvar' = rowmiss(`touse')
qui bysort `i': egen `minrowmiss' = min(`countmissvar')
cap drop if `minrowmiss' != 0
}
quietly count if `touse'
if r(N) == 0 {
error 2000
}
local ids `i'
local tunit `t'
//limit the unit number:
by `i', sort: gen nvals = _n == 1
qui count if nvals
if r(N) > 1000 {
di as err "Please limit your units within 1000 for elegant presentations"
exit 198
}
tempvar nids
cap label list `ids'
if "`r(k)'" != "" { //numeric units indicator with labels:
qui egen `nids' = group(`ids')
*label list `ids'
}
else{ //numeric units indicator without labels or string variable:
capture confirm numeric variable `ids'
if !_rc { //numeric units indicator without labels:
tempvar labelids
qui tostring `ids', gen(`labelids')
labmask `ids', val(`labelids')
*label list `ids'
qui egen `nids' = group(`ids')
}
else { //string variable:
tempvar i_numeric labeli
qui encode `ids',gen(`i_numeric')
*label list `i_numeric'
drop `ids'
rename `i_numeric' `ids'
qui egen `nids' = group(`ids')
}
}
qui levelsof `nids' if `touse'
loc numids = r(r)
// ignore treatment:
tempvar gcontrol
capture confirm variable `treat'
if !_rc {
qui levelsof `treat' if `touse', loc (levstreat)
loc numlevstreat = r(r)
}
if ("`ignoretreat'" != "" | "`type'" == "miss" | "`type'" == "missing") {
cap gen `gcontrol' = 1
}
*if ("`continuoustreat'" != "" & "`type'" == "outcome") {
else if (`numlevstreat' > 2 & "`type'" == "outcome") {
cap gen `gcontrol' = 1
}
else {
*if ("`ignoretreat'" != "" | "`type'" == "miss" | "`type'" == "missing") {
*cap gen `gcontrol' = 1
*}
else {
if (`numlevstreat' == 2) {
tempvar max_treat
cap bys `nids': egen `max_treat' = max(`treat')
cap gen `gcontrol' = 1
qui replace `gcontrol' = 0 if `max_treat' >= 1
}
else {
if (`numlevstreat' == 1) {
di "Only one treatment level"
cap gen `gcontrol' = 1
}
else {
if ("`prepost'" != "") {
di as err "with more than two treatment levels, option prepost may not be combined"
exit 198
}
else {
if (`numlevstreat' >= 5 & "`continuoustreat'" == "") {
di "Too many treatment levels; treat as continuous."
local continuoustreat `"continuoustreat"'
cap gen `gcontrol' = 1
}
if (`numlevstreat' <= 4 & "`continuoustreat'" != "") {
di "Too few treatment levels; consider drop the continuoustreat option."
}
}
}
}
}
}
sort `ids' `tunit'
//sort by time of first treatment
if "`bytiming'" != "" {
cap which sencode.ado
if _rc {
di as error "sencode.ado required: {stata ssc install sencode, replace}"
exit 199
}
tempvar bytime
tempvar min_bytime
tempvar num_trtime
cap gen `bytime' = .
cap replace `bytime' = `tunit' if `treat' > = 1
cap bys `nids': egen `min_bytime' = min(`bytime')
cap bys `nids': egen `num_trtime' = count(`bytime')
drop `bytime'
tempvar nids2
tempvar nids3
decode `ids', generate(`nids2')
sencode `nids2', generate(`nids3') gsort(`min_bytime' -`num_trtime' `ids')
}
else {
tempvar nids2 nidslab
decode `ids', generate(`nids2')
sencode `nids2', generate(`nidslab')
}
qui levelsof `nids' if `touse' , loc (levsnids)
/*
tempvar newtime
egen `newtime' = group(`tunit')
*qui sum `newtime'
*loc maxmintime = r(max) - r(min)
*qui levelsof `newtime'
*loc numsoftime = r(r)
*loc plotcoef = `maxmintime' / (`numsoftime' -1)
tempvar labeltime
qui tostring `tunit', gen(`labeltime')
labmask `newtime', val(`labeltime')
*/
tempvar maxtime mintime timegap inttimegap timegap2 mintimegap id_oneobs
cap bysort `nids': gen `maxtime' = `tunit'[_N]
qui sum `maxtime'
loc maxmaxtime = r(max)
cap bysort `nids': egen `mintime' = min(`tunit')
qui sum `mintime'
loc minmintime = r(min)
loc maxmaxminmingap = `maxmaxtime' - `minmintime'
qui levelsof `tunit'
loc numtime = r(r)
cap bysort `nids': gen `timegap' = (`maxtime'-`mintime')/(`numtime'-1) //possible common difference
qui gen `inttimegap' = int(`timegap')
cap bysort `nids': gen `timegap2' = `tunit'[_n]-`tunit'[_n-1]
cap bysort `nids': egen `mintimegap' = min(`timegap2')
if "`leavegap'" != "" {
if ( `timegap' != `mintimegap' | `inttimegap' != `timegap') { //not arithmetic sequence
tempvar differencetime
cap bysort `nids': gen `differencetime' = `tunit'[_n] - `tunit'[_n-1]
qui sum `differencetime'
loc min_differencetime = r(min)
loc max_differencetime = r(max)
loc divide_differencetime = `max_differencetime' / `min_differencetime'
if (`min_differencetime' != `max_differencetime' & `min_differencetime' != 1 & `divide_differencetime' == int(`divide_differencetime')) { // 两两difference相除是整数倍
qui save `backup', replace
qui keep `nids'
qui duplicates drop `nids', force
qui expand `numtime'
cap bysort `nids': gen `tunit' = `minmintime' + (_n-1) * `min_differencetime'
qui merge 1:1 `nids' `tunit' using `backup'
qui drop _merge
}
else { //commmon difference = 1
qui save `backup', replace
qui keep `nids'
qui duplicates drop `nids', force
qui expand `maxmaxminmingap'
cap bysort `nids': gen `tunit' = `minmintime' + _n
qui merge 1:1 `nids' `tunit' using `backup'
qui drop _merge
}
}
}
if "`leavegap'" == "" {
loc alltimegap = `maxmaxminmingap'/(`numtime'-1)
if (`timegap' != `mintimegap' | `inttimegap' != `timegap') {
di "Time is not evenly distributed (possibly due to missing data). "
}
else {
tempvar tunit_merge
qui gen `tunit_merge' = `tunit'
qui save `backup', replace
qui keep `nids'
qui duplicates drop `nids', force
qui expand `numtime'
cap bysort `nids': gen `tunit_merge' = `minmintime' + (_n-1) * `alltimegap'
qui merge 1:1 `nids' `tunit_merge' using `backup'
tempvar difftime
qui gen `difftime' = (`tunit_merge' == `tunit')
qui sum `difftime'
loc min_difftime = r(min)
loc max_difftime = r(max)
*if (`max_difftime' != `min_difftime') {
*if (`alltimegap' != 1) {
*di "Time is not evenly distributed (possibly due to missing data). "
*}
*}
qui drop `tunit_merge'
qui drop _merge
}
}
tempvar newtime
qui egen `newtime' = group(`tunit')
tempvar labeltime
qui tostring `tunit', gen(`labeltime')
labmask `newtime', val(`labeltime')
//indicate the last period as a different colored dot in outcome plot if only treated in the last period:
tempvar lastchangedot maxtime2 dtreat islastchangedot
cap bysort `nids': gen `maxtime2' = `tunit'[_N-1]
cap gen `lastchangedot' = 0
cap bysort `nids': replace `lastchangedot' = 1 if `treat' == 1 & `tunit' == `maxtime'
cap bysort `nids': gen `dtreat' = `treat'[_N] - `treat'[_N-1] if `lastchangedot' == 1
cap bysort `nids': replace `lastchangedot' = 0 if `dtreat' == 0
cap count if `lastchangedot' == 1
loc islastchangedot = (r(N) != 0)
/*
//paint the period right before treatment:
if ("`ignoretreat'" == "" & "`type'" != "miss" & "`type'" != "missing") {
if ("`type'" == "outcome" & "`discreteoutcome'" == "" ) {
cap destring `labeltime', replace
tempvar L1_labeltime L1_time
cap bys `nids': gen `L1_labeltime' = `labeltime'[_n-1] if `treat' >= 1
cap bys `nids': egen `L1_time' = min(`L1_labeltime')
cap replace `treat' = 1 if `labeltime' == `L1_time'
}
}
*/
tempvar plotvalue varmiss
qui egen `varmiss' = rowmiss(`varlist')
if ("`continuoustreat'" != "" & "`type'" == "outcome") {
cap gen `plotvalue' = 0
}
else {
if ("`ignoretreat'" != ""|"`type'" == "miss" | "`type'" == "missing") {
if "`leavegap'" != "" {
cap gen `plotvalue' = 0 if `varmiss' == 0
}
else {
cap gen `plotvalue' = 0
}
}
else {
if ("`type'" == "outcome" & `numlevstreat' > 2) {
cap gen `plotvalue' = 0
di "The number of treatment level is > 2; we ignore the treatment status."
}
else { //"`type'" == "treat"
if "`leavegap'" != "" {
cap gen `plotvalue' = `treat' if `varmiss' == 0
}
else {
cap gen `plotvalue' = `treat'
}
//remapping continuous treatment to 5 levels to fit color palettes levels:
if "`continuoustreat'" != "" {
qui sum `plotvalue'
loc maxminplotvalue = r(max) - r(min)
qui replace `plotvalue' = int((`plotvalue' - r(min)) * 4 / `maxminplotvalue' )
}
}
}
}
if ("`type'" == "miss" | "`type'" == "missing") {
if ("`ignoretreat'" == "") {
tempvar ignoretreat
cap gen `ignoretreat' = 1
}
}
qui levelsof `plotvalue' if `touse', loc (levsplot)
loc numlevsplot = r(r)
if ("`ignoretreat'" == "") {
if ("`continuoustreat'" == "" | "`type'" != "outcome") {
if `numlevstreat' == 2 {
if "`prepost'" != "" {
foreach x of loc levsplot {
qui replace `plotvalue' = `x' + 1 if `treat' == `x' & `treat' != 0
}
qui replace `plotvalue' = 1 if `treat' == 0 & `gcontrol' == 0 //treated(pre)
//levsplot: 0 <- control; 1 <- pre; 2 <- post
qui levelsof `plotvalue' if `touse', loc (levsplot)
loc numlevsplot = r(r)
}
}
else if `numlevstreat' > 2 {
if ("`type'" == "treat") {
tempvar altlevsplot
qui egen `altlevsplot' = group(`plotvalue') if `touse'
cap replace `altlevsplot' = `altlevsplot' - 1
qui levelsof `altlevsplot' if `touse', loc (levsplot)
loc numlevsplot = r(r)
qui drop `altlevsplot'
}
}
}
}
//deciding color
colorpalette "198 219 239" "eltblue" "66 146 198" "31 120 180" "8 81 156", n(`numlevsplot') nograph
if (`"`mycolor'"' != "") {
colorpalette `mycolor' , n(`numlevsplot') nograph
}
if "`theme'" == "bw" {
colorpalette Greys , n(`numlevsplot') nograph
}
if (`"`mycolor'"' != "Greys" & `"`mycolor'"' != "") {
if ( "`theme'" == "bw") {
di as err " If mycolor is not Greys, mycolor cannot combine with option theme(bw)"
exit 198
}
}
qui return list
tokenize `levsplot'
loc trpreortr `2'
tempvar nopre
if "`trpreortr'" != "" {
cap gen `nopre' = (`trpreortr' == 2)
}
else {
cap gen `nopre' = 0
}
if `nopre' != 1 {
foreach w of loc levsplot {
loc uu = `w' + 1 //012 -> 123
loc col`w' = r(p`uu')
// colorpalette stores #th color in r(p#)
}
}
else {
foreach w of loc levsplot {
if `w' == 0 {
loc uu = `w' + 1
loc col`w' = r(p`uu')
}
else if `w' == 2 {
loc col`w' = r(p`w')
}
}
}
if (`"`xlabel'"'=="") {
qui sum `newtime', mean
local xlabel `"xlabel(`r(min)'(`xlabdist')`r(max)', angle(90) nogrid labsize(tiny) valuelabel noticks)"'
}
if `"`bytiming'"' != "" { // With bytiming:
if (`"`ylabel'"'=="") {
qui sum `nids3', mean
local ylabel `"ylabel(`r(min)'(`ylabdist')`r(max)', angle(0) nogrid labsize(tiny) valuelabel noticks)"'
}
}
else { // Without bytiming:
if (`"`ylabel'"'=="") {
qui sum `nids', mean
local ylabel `"ylabel(`r(min)'(`ylabdist')`r(max)', angle(0) nogrid labsize(tiny) valuelabel noticks)"'
}
}
tempvar bgplotvalue labplotvalue
qui bysort `nids': egen `bgplotvalue' = min(`plotvalue')
qui levelsof `bgplotvalue' if `touse', loc (bglevsplot)
label define `labplotvalue' 0 "Always Under Control" 1 "Treatment Status Changed" 2 "Always Treated"
label val `bgplotvalue' `labplotvalue'
if ("`type'" == "outcome") {
//1. ploting outcome: type(outcome):
if ("`discreteoutcome'" == "" ) {
//1.1. plotting lines of continuous outcome:
di "now display lines of continuous outcome"
loc lines1
foreach w of loc levsplot {
foreach x of loc levsnids {
if (`"`prepost'"' != "" & `w' == 1 ) {
//with prepost: `w' == 1: treated(pre)
if `islastchangedot' == 0 {
loc lines1 `" `lines1' || line `outcome' `tunit' if `nids' == `x' & `gcontrol' == 0 & `touse', lcolor("`col`w''")"'
}
else{
loc lines1 `" `lines1' || line `outcome' `tunit' if `nids' == `x' & `gcontrol' == 0 & `touse', lcolor("`col`w''") || scatter `outcome' `tunit' if `nids' == `x' & `gcontrol' == 0 & `touse' & `lastchangedot' == 1, mcolor("`col`3''") msize(vsmall)"'
}
}
else if (`"`prepost'"' == "" & `w' == 0 ) {
//without prepost: `w' == 0: treated(pre)
if `islastchangedot' == 0 {
loc lines1 `" `lines1' || line `outcome' `tunit' if `nids' == `x' & `gcontrol' == 0 & `touse' , lcolor("`col`w''")"'
}
else{
loc lines1 `" `lines1' || line `outcome' `tunit' if `nids' == `x' & `gcontrol' == 0 & `touse', lcolor("`col`w''") || scatter `outcome' `tunit' if `nids' == `x' & `gcontrol' == 0 & `touse' & `lastchangedot' == 1, mcolor("`col`2''") msize(vsmall)"'
}
}
loc lines1 `" `lines1' || line `outcome' `tunit' if `nids' == `x' & `plotvalue' == `w' & `touse' , lcolor("`col`w''")"'
}
}
if ("`continuoustreat'" != "") {
tw `lines1' legend(region(lstyle(none) fcolor(none)) order(1) label(1 "Observed") size(*0.8) symxsize(3) keygap(1)) yscale(noline) xscale(noline) `options'
}
else { // not continuoustreat:
if ("`ignoretreat'" != "" | `numlevsplot'==1) { // ignore treatment:
tw `lines1' legend(region(lstyle(none) fcolor(none)) order(1) label(1 "Observed") size(*0.8) symxsize(3) keygap(1)) yscale(noline) xscale(noline) `options'
}
else {
if `numlevstreat' > 2 {
tw `lines1' legend(region(lstyle(none) fcolor(none)) order(1) label(1 "Observed") size(*0.8) symxsize(3) keygap(1)) yscale(noline) xscale(noline) `options'
}
else { // not ignore treatment:
if ("`bygroup'" != "" ) { //with bygroup:
if `islastchangedot' == 0 {
tempvar allunits
qui egen `allunits' = max(`nids')
local largestlegend = 3*`allunits' + 1
local midlegend = 2*`allunits'
twoway `lines1' by(`bgplotvalue', note("") cols(1)) legend(region(lstyle(none) fcolor(none)) rows(1) order(1 "Control" `midlegend' "Treated (Pre)" `largestlegend' "Treated (Post)") size(*0.8) symxsize(3) keygap(1)) yscale(noline) xscale(noline) `options'
}
else {
tempvar allunits
qui egen `allunits' = max(`nids')
local largestlegend = `allunits' + 2
local midlegend = `allunits' + 1
twoway `lines1' by(`bgplotvalue', note("") cols(1)) legend(region(lstyle(none) fcolor(none)) rows(1) order(1 "Control" `midlegend' "Treated (Pre)" `largestlegend' "Treated (Post)") size(*0.8) symxsize(3) keygap(1)) yscale(noline) xscale(noline) `options'
}
}
else{ //without bygroup: prepost=on:
if (`"`prepost'"' != "") {
if `islastchangedot' == 0 {
tempvar allunits
qui egen `allunits' = max(`nids')
local largestlegend = 3*`allunits' + 1
local midlegend = 2*`allunits'
tw `lines1' legend(region(lstyle(none) fcolor(none)) rows(1) order(1 "Control" `midlegend' "Treated (Pre)" `largestlegend' "Treated (Post)") size(*0.8) symxsize(3) keygap(1)) yscale(noline) xscale(noline) `options'
}
else {
tempvar allunits
qui egen `allunits' = max(`nids')
local largestlegend = `allunits' + 2
local midlegend = `allunits' + 1
tw `lines1' legend(region(lstyle(none) fcolor(none)) rows(1) order(1 "Control" `midlegend' "Treated (Pre)" `largestlegend' "Treated (Post)") size(*0.8) symxsize(3) keygap(1)) yscale(noline) xscale(noline) `options'
}
}
else { //prepost = off:
if `islastchangedot' == 0 {
tempvar allunits
qui egen `allunits' = max(`nids')
local largestlegend=3*`allunits'
tw `lines1' legend(region(lstyle(none) fcolor(none)) rows(1) order(1 "Control" `largestlegend' "Treated") size(*0.8) symxsize(3) keygap(1)) yscale(noline) xscale(noline) `options'
}
else {
tw `lines1' legend(region(lstyle(none) fcolor(none)) rows(1) order(1 "Control" 2 "Treated") size(*0.8) symxsize(3) keygap(1)) yscale(noline) xscale(noline) `options'
}
}
}
}
}
}
}
else {
//1.2. plotting dots of discrete outcome:
//add some randomness to time units and outcome so that they can scatter around:
di "now display dots of discrete outcome"
tempvar rout rtime
cap gen `rtime' = `tunit' + runiform(-0.2, 0.2)
cap gen `rout' = `outcome' + runiform(-0.2, 0.2)
loc dot1
foreach w of loc levsplot {
loc dot1 `" `dot1' || sc `rout' `rtime' if `plotvalue' == `w' & `touse' , mcolor("`col`w''") msize(small)"'
}
if ("`continuoustreat'" != "") {
tw `dot1' legend(region(lstyle(none) fcolor(none)) row(1) order(1) label(1 "Observed") size(*0.8) symxsize(3) keygap(1)) ytitle("`outcome'") xtitle("`tunit'") `options'
}
else {
if ("`ignoretreat'" != "" | `numlevsplot'==1) { // ignore treatment:
tw `dot1' legend(region(lstyle(none) fcolor(none)) row(1) order(1) label(1 "Observed") size(*0.8) symxsize(3) keygap(1)) ytitle("`outcome'") xtitle("`tunit'") `options'
}
else {
if `numlevstreat' > 2 {
tw `dot1' legend(region(lstyle(none) fcolor(none)) row(1) order(1) label(1 "Observed") size(*0.8) symxsize(3) keygap(1)) ytitle("`outcome'") xtitle("`tunit'") `options'
}
else { // not ignore treatment:
if ("`bygroup'" != "" ) { //with bygroup:
twoway `dot1' by(`bgplotvalue', cols(1) note("")) legend(region(lstyle(none) fcolor(none)) note("") row(1) label(1 "Control") label(2 "Treated (Pre)") label(3 "Treated (Post)") size(*0.8) symxsize(3) keygap(1)) yscale(noline) xscale(noline) ytitle("`outcome'") xtitle("`tunit'") `options'
}
else{ //without bygroup:
if (`"`prepost'"' != "") {
tw `dot1' legend(region(lstyle(none) fcolor(none)) row(1) label(1 "Control") label(2 "Treated (Pre)") label(3 "Treated (Post)") size(*0.8) symxsize(3) keygap(1)) yscale(noline) xscale(noline) ytitle("`outcome'") xtitle("`tunit'") `options'
}
else { //prepost=off:
tw `dot1' legend(region(lstyle(none) fcolor(none)) row(1) label(1 "Control") label(2 "Treated") size(*0.8) symxsize(3) keygap(1)) ytitle("`outcome'") xtitle("`tunit'") `options'
}
}
}
}
}
}
}
else if ("`type'" == "treat" | "`type'" == "miss" | "`type'" == "missing"){
// 2. Heatmap of treatment: type(treat):
if `"`bytiming'"' != "" {
*2.1. With bytiming:
tempvar y0 y1
cap gen `y1'=`nids3'+ 0.5
qui sum `y1'
la val `y1' `:val lab `nids3''
cap gen `y0'=`nids3'- 0.5
qui levelsof `plotvalue' if `touse', loc(levsplot)
qui sum `plotvalue' if `touse', mean
if (`r(min)' < 0) {
tempvar add
cap gen `add' = 0 - `r(min)'
cap replace `plotvalue' = `plotvalue' + `add'
qui levelsof `plotvalue' if `touse', loc(levsplot_color)
colorpalette "198 219 239" "107 174 214" "66 146 198" "31 120 180" "8 81 156" , n(`numlevsplot') nograph
if (`"`mycolor'"' != "") {
colorpalette `mycolor' , n(`numlevsplot') nograph
}
foreach w of loc levsplot_color {
loc uu = `w' + 1
loc col`w' = r(p`uu')
}
foreach w of loc levsplot_color{
loc gcom `"`gcom'||rbar `y1' `y0' `newtime' if (`plotvalue'==`w')&(`touse'), barw(`xdist') col("`col`w''") fi(inten100) lw(none) "'
}
}
else {
foreach w of loc levsplot{
loc gcom `"`gcom'||rbar `y1' `y0' `newtime' if (`plotvalue'==`w')&(`touse'), barw(`xdist') col("`col`w''") fi(inten100) lw(none) "'
}
}
loc sc `"sc `nids3' `newtime' if `touse', mlabpos(0) msy(i)"'
}
else {
*2.2. Without bytiming:
tempvar y0 y1
cap gen `y1'=`nids'+ 0.5
qui sum `y1'
la val `y1' `:val lab `nidslab''
cap gen `y0'=`nids'- 0.5
qui levelsof `plotvalue' if `touse', loc(levsplot)
qui sum `plotvalue' if `touse', mean
if (`r(min)' < 0) {
tempvar add
cap gen `add' = 0 - `r(min)'
cap replace `plotvalue' = `plotvalue' + `add'
qui levelsof `plotvalue' if `touse', loc(levsplot_color)
colorpalette "198 219 239" "107 174 214" "66 146 198" "31 120 180" "8 81 156" , n(`numlevsplot') nograph
if (`"`mycolor'"' != "") {
colorpalette `mycolor' , n(`numlevsplot') nograph
}
foreach w of loc levsplot_color {
loc uu = `w' + 1
loc col`w' = r(p`uu')
}
foreach w of loc levsplot_color{
loc gcom `"`gcom'||rbar `y1' `y0' `newtime' if (`plotvalue'==`w')&(`touse'), barw(`xdist') col("`col`w''") fi(inten100) lw(none) "'
//100% intensity, full color. Line has zero width: it vanishes
}
}
else {
foreach w of loc levsplot{
loc gcom `"`gcom'||rbar `y1' `y0' `newtime' if (`plotvalue'==`w')&(`touse'), barw(`xdist') col("`col`w''") fi(inten100) lw(none) "'
//100% intensity, full color. Line has zero width: it vanishes
}
}
loc sc `"sc `nids' `newtime' if `touse', mlabpos(0) msy(i)"'
}
if ("`ignoretreat'" != "" | `numlevsplot'==1) { // ignore treatment:
local gcom `"`gcom' legend(region(lstyle(none) fcolor(none)) rows(1) order(1) label(1 "Observed") size(*0.6) symxsize(3) keygap(1)) xsize(2) ysize(2) yscale(noline reverse) xscale(noline) aspect(1) xtitle("`tunit'") ytitle("`ids'") `ylabel' `xlabel' "'
}
else { // not ignore treatment:
if `nopre' == 1 {
local gcom `"`gcom' legend(region(lstyle(none) fcolor(none)) rows(1) order(1 2) label(1 "Control") label(2 "Treated") size(*0.6) symxsize(3) keygap(1)) xsize(2) ysize(2) yscale(noline reverse) xscale(noline) aspect(1) xtitle("`tunit'") ytitle("`ids'") `ylabel' `xlabel' "'
}
else {
if (`"`prepost'"' != "") {
local gcom `"`gcom' legend(region(lstyle(none) fcolor(none)) rows(1) order(1 2 3) label(1 "Control") label(2 "Treated (Pre)") label(3 "Treated (Post)") size(*0.6) symxsize(3) keygap(1)) xsize(2) ysize(2) yscale(noline reverse) xscale(noline) aspect(1) xtitle("`tunit'") ytitle("`ids'") `ylabel' `xlabel' "'
}
else {
if "`continuoustreat'" != "" {
qui sum `treat'
loc maxminplotvalue = r(max) - r(min)
loc dismaxminplotvalue = `maxminplotvalue' / 4
loc r_max = r(max) + `dismaxminplotvalue'
tempvar plotvalue1
qui egen `plotvalue1' = cut(`treat'), at(`r(min)' (`dismaxminplotvalue') `r_max')
qui levelsof `plotvalue1' if `touse', loc (levsplot1)
tokenize `levsplot1'
loc contrlev1 `1'
loc contrlev2 `2'
loc contrlev3 `3'
loc contrlev4 `4'
loc contrlev5 `5'
loc contrlev11=round(`1', 0.001)
loc contrlev22=round(`2', 0.001)
loc contrlev33=round(`3', 0.001)
loc contrlev44=round(`4', 0.001)
loc contrlev55=round(`5', 0.001)
if `contrlev55' > `contrlev44' {
local gcom `"`gcom' legend(region(lstyle(none) fcolor(none)) rows(1) order(1 2 3 4 5) label(1 "`contrlev11'") label(2 "`contrlev22'") label(3 "`contrlev33'") label(4 "`contrlev44'") label(5 "`contrlev55'") title("Treatment levels: ", size(*0.45)) size(*0.6) symxsize(3) keygap(1)) xsize(2) ysize(2) yscale(noline reverse) xscale(noline) aspect(1) xtitle("`tunit'") ytitle("`ids'") `ylabel' `xlabel' "'
}
else {
local gcom `"`gcom' legend(region(lstyle(none) fcolor(none)) rows(1) order(1 2 3 4) label(1 "`contrlev11'") label(2 "`contrlev22'") label(3 "`contrlev33'") label(4 "`contrlev44'") title("Treatment level: ", size(*0.45)) size(*0.6) symxsize(3) keygap(1)) xsize(2) ysize(2) yscale(noline reverse) xscale(noline) aspect(1) xtitle("`tunit'") ytitle("`ids'") `ylabel' `xlabel' "'
}
}
else {
if `numlevstreat' > 2 {
tokenize `levsplot'
loc trlev1 `1'
loc trlev2 `2'
loc trlev3 `3'
loc trlev4 `4' // If the number of treatment levels >= 5, need to combine with Continuoustreat
if "`trlev4'" != ""{ //treatment levels = 4:
local gcom `"`gcom' legend(region(lstyle(none) fcolor(none)) rows(2) order(1 2 3 4) label(1 "Treatment level: `trlev1'") label(2 "Treatment level: `trlev2'") label(3 "Treatment level: `trlev3'") label(4 "Treatment level: `trlev4'") size(*0.55) symxsize(3) keygap(0.5) colgap(1)) xsize(2) ysize(2) yscale(noline reverse) xscale(noline) aspect(1) xtitle("`tunit'") ytitle("`ids'") `ylabel' `xlabel' "'
}
else { //treatment levels = 3:
local gcom `"`gcom' legend(region(lstyle(none) fcolor(none)) rows(1) order(1 2 3) label(1 "Treatment level: `trlev1'") label(2 "Treatment level: `trlev2'") label(3 "Treatment level: `trlev3'") size(*0.55) symxsize(3) keygap(0.5) colgap(1)) xsize(2) ysize(2) yscale(noline reverse) xscale(noline) aspect(1) xtitle("`tunit'") ytitle("`ids'") `ylabel' `xlabel' "'
}
}
else { //treatment levels = 2:
local gcom `"`gcom' legend(region(lstyle(none) fcolor(none)) rows(1) order(1 2) label(1 "Control") label(2 "Treated") size(*0.6) symxsize(3) keygap(1)) xsize(2) ysize(2) yscale(noline reverse) xscale(noline) aspect(1) xtitle("`tunit'") ytitle("`ids'") `ylabel' `xlabel' "'
}
}
}
}
}
tw `gcom' plotr(fc(white) margin(zero)) ||`sc' `options'
}
else if ("`type'" == "bivar" | "`type'" == "bivariate"){
if (`"`mycolor'"' != "") {
colorpalette `mycolor' , n(2) nograph
loc y2color = r(p1)
loc y1color = r(p2)
}
else {
loc y1color = "0 0 0"
loc y2color = "144 144 144"
}
if ("`lwd'" == "") {
loc linewide = "medium"
}
else {
loc linewide = "`lwd'"
}
if ("`style'" == "") {
if ("`discreteoutcome'" == "" & "`continuoustreat'" == "") { //continuous Y, discrete D
loc style = "l b"
}
else if ("`discreteoutcome'" != "" & "`continuoustreat'" == "") { //discrete Y, discrete D
loc style = "b b"
}
else if ("`discreteoutcome'" == "" & "`continuoustreat'" != "") { //continuous Y, continuous D
loc style = "l l"
}
else if ("`discreteoutcome'" != "" & "`continuoustreat'" != "") { //discrete Y, continuous D
loc style = "b l"
}
}
else if ("`style'" != "") {
if ("`style'" == "l" | "`style'" == "line") {
loc style = "l l"
}
else if ("`style'" == "b" | "`style'" == "bar") {
loc style = "b b"
}
else if ("`style'" == "c" | "`style'" == "connected") {
loc style = "c c"
}
}
qui sum `newtime', mean
local xlabel `"xlabel(`r(min)'(`xlabdist')`r(max)', valuelabel)"'