-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcoefplot.ado
More file actions
3929 lines (3748 loc) · 124 KB
/
Copy pathcoefplot.ado
File metadata and controls
3929 lines (3748 loc) · 124 KB
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 1.8.8 22aug2025 Ben Jann
program coefplot
version 11
if _N==0 {
_coefplot `macval(0)'
exit
}
parse_nodrop `macval(0)' // returns nodrop
if "`nodrop'"!="" {
_coefplot `macval(0)'
exit
}
if c(stata_version)<16 {
preserve
qui keep in 1 if 0
_coefplot `macval(0)'
exit
}
tempname tmpframe
frame put in 1 if 0, into(`tmpframe')
frame `tmpframe' {
_coefplot `macval(0)'
}
end
program parse_nodrop
while (`"`macval(0)'"'!="") {
gettoken subgraph 0 : 0, parse("|") bind
}
_parse comma subgraph 0 : subgraph
syntax [, GENerate GENerate2(passthru) NODROP * ]
if `"`generate'`generate2'"'!="" local nodrop nodrop
c_local nodrop `nodrop'
end
program _coefplot
nobreak {
capt mata: mata drop COEFPLOT_STRUCT
capt n break __coefplot `macval(0)'
local rc = _rc
capt mata: mata drop COEFPLOT_STRUCT
exit `rc'
}
end
program __coefplot, rclass
// get subgraphs and parse global options
parse_subgraphs `macval(0)' // returns n_subgr, subgr_#, opts
parse_globalopts `macval(opts)' // returns expanded global opts and twopts,
// subgropts0, plotopts0, modelopts0, twplotopts0
// backup current estimates, initialize struct
tempname ecurrent
_est hold `ecurrent', restore estsystem nullok
mata: COEFPLOT_STRUCT = coefplot_struct_init()
// dryrun across subgraphs and plots to collect options
local i 0
local N_plots 0
forv j = 1/`n_subgr' {
if "`recycle'"=="" local i 0
local firstplot_`j' = `i'+1
parse_plots `j' `macval(subgr_`j')' // returns n_plots_j, plot_j_#, opts
if ("`recycle'"=="" & `n_subgr'>1) {
combine_plotopts, `macval(opts)' _opts0(`macval(plotopts1prev)')
local plotopts1prev `macval(plotopts)'
local opts `macval(plotopts)' `macval(options)'
}
merge_subgropts, `macval(opts)' _opts0(`subgropts0' `plotopts0' `macval(modelopts0)')
// returns subgropts, plotopts1, modelopts1, twplotopts1
parse_subgropts `j', `subgropts'
forv k = 1/`n_plots_`j'' {
local ++i
local twplotopts1_`i' `twplotopts1_`i'' `twplotopts1'
parse_models `j' `k' `macval(plot_`j'_`k')'
// returns n_models_j_k, model_j_k_#, opts
if `n_models_`j'_`k''==1 & `"`model_`j'_`k'_1'"'=="_skip" {
if `"`opts'"'!="" {
di as err "options not allowed with _skip"
exit 198
}
continue
}
if (`i'>`N_plots') { // get p#() from twoplotopts0
parse_get_popt_i `i', `macval(twplotopts0)' // returns twplotopts0, plotopts
if `"`macval(plotopts)'"'!="" {
merge_plotopts, `macval(plotopts)' // to isolate the modelopts
local popt_mopts_`i' `macval(modelopts2)'
merge_plotopts, `macval(opts)' ///
_opts0(`macval(modelopts2)' `plotopts' `_opts0' `options')
// returns plotopts, modelopts2, options, _opts0
local opts `macval(modelopts2)' `plotopts' `_opts0' `options'
}
}
else {
if `"`macval(popt_mopts_`i')'"'!="" { // add modelopts from p#()
merge_plotopts, `macval(opts)' _opts0(`macval(popt_mopts_`i')')
// returns plotopts, modelopts2, options, _opts0
local opts `macval(modelopts2)' `plotopts' `_opts0' `options'
}
}
if ("`recycle'"=="" & `n_subgr'>1) {
combine_plotopts, `macval(opts)' _opts0(`macval(plotopts2_`i')')
local plotopts2_`i' `macval(plotopts)'
local opts `macval(plotopts)' `macval(options)'
}
merge_plotopts, `macval(opts)' _opts0(`macval(plotopts1)' `macval(modelopts1)')
// returns plotopts, modelopts2, options, _opts0
if `"`_opts0'"'!="" error 198
local modelopts_`j'_`k' `macval(modelopts2)'
local twplotopts_`i' `twplotopts_`i'' `options'
local plotopts_`i' `plotopts'
}
local lastplot_`j' `i'
local N_plots = max(`N_plots', `i')
}
// expand plotopts
local customoffset 0
forv i = 1/`N_plots' {
parse_plotopts `i', `plotopts_`i''
if `"`offset_`i''"'!="" local customoffset 1
}
// parse cismooth
forv i = 1/`N_plots' {
local cis_`i' = `"`cismooth_`i''"'!=""
if `cis_`i'' {
if `"`cismooth_`i''"'=="cismooth" local cismooth_`i'
parse_cismooth `i', `cismooth_`i'' // returns cis_levels_i,
// cis_n_i, cis_lwidth_i, cis_intens_i, cis_color_i, cis_pstyle_i
}
else local cis_n_`i' = 0
}
// parse models and collect results
local i 0
forv j = 1/`n_subgr' {
if "`recycle'"=="" local i 0
forv k = 1/`n_plots_`j'' {
local ++i
if `n_models_`j'_`k''==1 & `"`macval(model_`j'_`k'_1)'"'=="_skip" {
continue
}
forv l = 1/`n_models_`j'_`k'' {
parse_model `macval(model_`j'_`k'_`l')' // returns model, matrix, opts
if `"`matrix'"'=="" {
if `"`model'"'=="." {
_est unhold `ecurrent'
}
else {
qui est restore `model'
}
}
collect_coefs `"`model'"' ///
"`matrix'" /// matrix mode?
"`atmode'" /// whether at() is used; will be replaced
`i' /// plot number
`j' /// subgraph number
"`cis_levels_`i''" ///
, `macval(opts)' _opts0(`macval(modelopts_`j'_`k')')
// returns equation, atmode, n_ci
// may reset mlabel_# and mlabvposition_#
local n_ci = `n_ci' - `cis_n_`i''
if `"`matrix'"'=="" & `"`model'"'=="." {
_est hold `ecurrent', restore estsystem nullok
}
if "`n_ci_`i''"=="" local n_ci_`i' 0
local n_ci_`i' = max(`n_ci_`i'', `n_ci')
mata: coefplot_add_label(COEFPLOT_STRUCT, "by", `j', "model", 0)
if `"`equation'"'!="" {
if `"`model'"'=="." local model `"`equation'"'
else local model `"`model'=`equation'"'
}
mata: coefplot_add_label(COEFPLOT_STRUCT, "plot", `i', "model", 0)
}
}
}
forv i = 1/`N_plots' { // expand ciopts
if `n_ci_`i''>0 {
parse_ciopts_nocilwincr `i', `ciopts_`i'' `cirecast_`i'' // returns nocilwincr_#
parse_ciopts `i' `n_ci_`i'' `ciopts_`i'' `cirecast_`i''
}
}
mata: coefplot_set_r(COEFPLOT_STRUCT) // returns r, N_ci, N_aux, mlbllen
local mlbllen = max(1, min(c(maxstrvarlen), `mlbllen'))
if `r'==0 {
di as txt "(nothing to plot)"
exit
}
// cleanup and and arrange
if `"`horizontal'`vertical'"'=="" {
if `atmode' local vertical vertical
else local horizontal horizontal
}
if `"`horizontal'"'!="" {
local xaxis y
local yaxis x
local offdir "-"
local reverse yscale(reverse)
local plotregion plotregion(margin(t=0 b=0))
}
else { // vertical
local xaxis x
local yaxis y
local offdir "+"
local plotregion plotregion(margin(l=0 r=0))
}
if `atmode' {
if "`bycoefs'"!="" {
di as err "at() and bycoefs not both allowed"
exit 198
}
local grid `"`grid'`gridopts'"'
foreach opt in order coeflabels eqlabels relocate headings ///
groups grid {
if `"``opt''"'!="" {
di as err "at() and `opt'() not both allowed"
exit 198
}
}
mata: coefplot_add_eq_and_grp(COEFPLOT_STRUCT)
local reverse
local plotregion
local meqs 0
}
else {
if `"`eqstrict'"'!="" local meqs 1
else {
mata: coefplot_multiple_eqs(COEFPLOT_STRUCT) // returns meqs
}
mata: coefplot_arrange(COEFPLOT_STRUCT) // updates local r
mata: coefplot_coeflbls(COEFPLOT_STRUCT)
coeflbls "`labels'" `"`clinteract'"'
if "`bycoefs'"!="" {
mata: coefplot_bycoefs(COEFPLOT_STRUCT) // returns n_subgr
local meqs 0
}
mata: coefplot_catvals(COEFPLOT_STRUCT)
// modifies C.at; sets C.eq, C.grp; returns groups
mata: coefplot_headings(COEFPLOT_STRUCT)
// modifies C.at; returns hlbls
}
// save results to variables
if `"`generate'`nodrop'"'=="" {
qui set obs `r' // (all obs have been dropped)
}
else if (_N < `r') {
if `"`generate'"'!="" {
di as txt "need to create additional observations; " _c
di as txt "press break to abort"
more
set obs `r'
}
else {
preserve
qui set obs `r'
}
}
tempname by plot at mlbl mlpos b V se t df pval eq grp
qui gen `by' = .
qui gen `plot' = .
qui gen `at' = .
qui gen str`mlbllen' `mlbl' = ""
qui gen `mlpos' = .
qui gen `b' = .
qui gen `V' = .
qui gen `se' = .
qui gen `t' = .
qui gen `df' = .
qui gen `pval' = .
if `"`format'"'!="" {
format `format' `b' `V' `se' `t' `pval'
}
qui gen `eq' = .
qui gen `grp' = .
forv i = 1/`N_ci' {
tempname ll`i' ul`i'
qui gen `ll`i'' = .
qui gen `ul`i'' = .
if `"`format'"'!="" {
format `format' `ll`i'' `ul`i''
}
}
forv i = 1/`N_aux' {
tempname aux`i'
qui gen `aux`i'' = .
if `"`format'"'!="" {
format `format' `aux`i''
}
}
if `"`generate'"'!="" {
preserve
local returnvars
local i 0
foreach v in by plot at mlbl mlpos b V se t df pval {
local ++i
local varl: word `i' of ///
"subgraph ID" ///
"plot ID" ///
"plot position (category axis)" ///
"marker label" ///
"marker label position" ///
"coefficient" ///
"variance" ///
"standard error" ///
"t or z statistic" ///
"degrees of freedom" ///
"p-value"
if "`replace'"!="" {
capt confirm new variable `generate'`v', exact
if _rc {
drop `generate'`v'
}
}
rename ``v'' `generate'`v'
lab var `generate'`v' `"`varl'"'
local `v' `generate'`v'
local returnvars `returnvars' `generate'`v'
}
forv i = 1/`N_ci' {
foreach v in ll ul {
if "`v'"=="ll" local varl "CI`i': lower limit"
else local varl "CI`i': upper limit"
if "`replace'"!="" {
capt confirm new variable `generate'`v'`i', exact
if _rc {
drop `generate'`v'`i'
}
}
rename ``v'`i'' `generate'`v'`i'
lab var `generate'`v'`i' `"`varl'"'
local `v'`i' `generate'`v'`i'
local returnvars `returnvars' `generate'`v'`i'
}
}
forv i = 1/`N_aux' {
local varl "Auxiliary variable `i'"
if "`replace'"!="" {
capt confirm new variable `generate'aux`i', exact
if _rc {
drop `generate'aux`i'
}
}
rename `aux`i'' `generate'aux`i'
lab var `generate'aux`i' `"`varl'"'
local aux`i' `generate'aux`i'
local returnvars `returnvars' `generate'aux`i'
}
}
mata: coefplot_put(COEFPLOT_STRUCT)
mata: coefplot_apply_transform(COEFPLOT_STRUCT)
qui compress `at' `df' `plot' `by' `eq' `grp' `mlpos' // not really needed
// get labels
set_by_and_plot_labels `plot' `by'
if `"`plotlabels'"'!="" {
set_labels "`plot'" "`N_plots'" `"`plotlabels'"'
}
if `"`pltrunc'`plwrap'"'!="" {
truncwrap_vlabels "`plot'" "`N_plots'" "`pltrunc'" ///
"`plwrap'" "`plbreak'"
}
if "`bycoefs'"=="" {
if `"`bylabels'"'!="" {
set_labels "`by'" "`n_subgr'" `"`bylabels'"'
}
if `"`bltrunc'`blwrap'"'!="" {
truncwrap_vlabels "`by'" "`n_subgr'" "`bltrunc'" ///
"`blwrap'" "`blbreak'"
}
}
if `atmode'==0 {
if "`grid'"=="" & "`xaxis'"=="y" {
if `N_plots'>1 & `"`offsets'"'=="" local grid between
else local grid within
}
get_axis_labels `at' `eq' `grp' "`grid'" `"`groups'"'
// => returns xlabels, xgrid, xrange, eqlabels, groups
if `meqs'==0 | "`noeqlabels'"!="" local eqlabels
if `"`cltrunc'`clwrap'"'!="" {
if "`bycoefs'"=="" {
truncwrap_labels xlabels "`cltrunc'" "`clwrap'" ///
"`clbreak'" `"`xlabels'"'
}
else {
truncwrap_vlabels "`by'" "`n_subgr'" "`cltrunc'" ///
"`clwrap'" "`clbreak'"
}
}
if "`bycoefs'"!="" {
if `"`bylabels'"'!="" {
reset_xlabels `"`bylabels'"' `"`xlabels'"'
}
if `"`bltrunc'`blwrap'"'!="" {
truncwrap_labels xlabels "`bltrunc'" "`blwrap'" ///
"`blbreak'" `"`xlabels'"'
}
}
if `"`clangle'"'=="" local clangle angle(horizontal)
local xlabel `xaxis'label(`xlabels', nogrid `clangle' `clopts')
local xrange `xaxis'scale(range(`xrange'))
if !inlist("`grid'", "", "none") {
local xtick `xaxis'tick(`xgrid', notick tlstyle(none) grid `gridopts')
// note: tlstyle(none) is required to prevent by() from drawing
// the ticks
}
else local xtick
if "`eqashead'"!="" {
merge_eqlabels_hlbls `"`eqlabels'"' `"`hlbls'"'
// => returns hlbls and clears eqlabels
}
if `"`eqtrunc'`eqwrap'"'!="" {
if `"`eqlabels'"'!="" {
truncwrap_labels eqlabels "`eqtrunc'" "`eqwrap'" ///
"`eqbreak'" `"`eqlabels'"'
}
}
if `"`gtrunc'`gwrap'"'!="" {
if `"`groups'"'!="" {
truncwrap_labels groups "`gtrunc'" "`gwrap'" ///
"`gbreak'" `"`groups'"'
}
}
if `"`htrunc'`hwrap'"'!="" {
if `"`hlbls'"'!="" {
truncwrap_labels hlbls "`htrunc'" "`hwrap'" ///
"`hbreak'" `"`hlbls'"'
}
}
}
// compute offsets
if `customoffset' {
forv i = 1/`N_plots' {
if "`offset_`i''"!="" {
qui replace `at' = `at' `offdir' `offset_`i'' if `plot'==`i'
}
}
}
else if `atmode'==0 & `"`offsets'"'=="" & `N_plots'>1 {
capt mata: coefplot_at_unique(COEFPLOT_STRUCT) // error if not true
if _rc==1 exit _rc
if _rc {
if "`recycle'"=="" | `n_subgr'==1 {
qui replace `at' = `at' - 0.5 + `plot'/(`N_plots'+1)
}
else {
forv j=1/`n_subgr' {
qui replace `at' = `at' - 0.5 + ///
(`plot'-`firstplot_`j''+1) / ///
(`lastplot_`j''-`firstplot_`j''+2) if `by'==`j'
}
}
}
}
// inject tempvars
forv i=1/`N_plots' {
foreach opt in ifopt weightopt mlabel mlabvposition {
if `"``opt'_`i''"'!="" {
mata: coefplot_inject_temvars("`opt'_`i'", `N_ci', `N_aux')
}
}
}
// handle string expressions in mlabel()
forv i=1/`N_plots' {
if `"`mlabel_`i''"'!="" {
if `"`mlabel_`i''"'=="mlabel(`mlbl')" continue
parse_mlabel_exp, `mlabel_`i'' // returns mlblexp
capt confirm variable `mlblexp'
if _rc==0 continue
capt replace `mlbl' = `mlblexp' if `plot'==`i'
if _rc {
di as err "invalid expression in mlabel()"
exit 198
}
local mlabel_`i' mlabel(`mlbl')
}
}
// compile plot
local addaxis 1
local eqaxis 2
local axisalt alt
if (`"`eqlabels'"'!="" & `"`groups'"'!="") | (`"`addplotbelow'"'!="") local axisalt
if `"`groups'"'!="" {
local ++eqaxis
local addaxis `addaxis' 2
local groupsopts `xaxis'scale(axis(2) `axisalt' noline) ///
`xaxis'title("", axis(2)) ///
`xaxis'label(`groups', axis(2) noticks tlstyle(none) `gopts')
}
if `"`eqlabels'"'!="" {
local addaxis `addaxis' `eqaxis'
local eqaxisopts `xaxis'scale(axis(`eqaxis') `axisalt' noline) ///
`xaxis'title("", axis(`eqaxis')) ///
`xaxis'label(`eqlabels', axis(`eqaxis') noticks ///
tlstyle(none) `eqopts')
}
local axisalt
if "`addaxis'"!="1" {
local addaxis `xaxis'axis(`addaxis')
if `"`addplotbelow'"'!="" {
if (`"`eqlabels'"'!="")+(`"`groups'"'!="")==1 {
local axisalt `xaxis'scale(alt)
}
}
}
else local addaxis
if `"`hlbls'"'!="" {
local hlblsopts ///
`xaxis'label(`hlbls', custom add tlstyle(none) `hopts')
}
local j 0
if `"`addplot'"'!="" {
mata: coefplot_inject_temvars("addplot", `N_ci', `N_aux')
if `"`addplotbelow'"'!="" {
capt two `addplot' ||, nodraw
if _rc==0 local j `.Graph.last_style'
capt confirm integer number `j'
if _rc local j 0
}
}
local plots
local legendlbls
local legendorder
forv i=1/`N_plots' {
local key
if "`n_ci_`i''"=="" {
continue // plot does not exist (this can happen if _skip is
// specified together with norecycle)
}
local n_ci = `n_ci_`i'' + `cis_n_`i''
if `n_ci'==0 & `"`cionly_`i''"'!="" {
continue // can happen if noci and cionly is specified
}
local axis
if `"`axis_`i''"'!="" {
local axis `yaxis'axis(`axis_`i'')
}
local ciplots
if (`n_ci')>0 {
get_pstyle_id `=mod(`i'-1,`pcycle')+1', `pstyle_`i'' // returns pstyle_id
forv k = 1/`n_ci' {
local lw
if `k'>`cis_n_`i'' {
local l = `k' - `cis_n_`i''
local ciopts `ciopts_`i'_`l''
parse_ciopts_recast_pstyle, `ciopts'
// returns cirecast, cipstyle, ciopts
if "`nocilwincr_`i''"=="" {
local lw = string(1 + log10(`l')/log10(2))
local lw lwidth(*`lw')
}
local ciplotcmd rspike `ll`k'' `ul`k'' `at'
if (substr(`"`cirecast'"',1,2)=="pc") { // paired coordinates
local ciplotcmd `cirecast' `ll`k'' `at' `ul`k'' `at'
if `"`cirecast'"'=="pcrarrow" {
local ciplotcmd pcarrow `ul`k'' `at' `ll`k'' `at'
}
}
else if `"`cirecast'"'!="" {
local ciopts `ciopts' recast(`cirecast')
}
}
else { // cismooth
local l 0
local cirecast
local cipstyle `cis_pstyle_`i''
local lw: word `k' of `cis_lwidth_`i''
local lw lwidth(*`lw')
local lcinten: word `k' of `cis_intens_`i''
local ciopts lcolor("`cis_color_`i''*`lcinten'") `cipstyle'
local ciplotcmd rspike `ll`k'' `ul`k'' `at'
}
if `"`cipstyle'"'!="" local pstyle
else {
set_pstyle `pstyle_id' `"`cirecast'"' // returns pstyle
}
local ciplots `ciplots' ///
(`ciplotcmd' if `plot'==`i'`ifopt_`i'', `addaxis' ///
`pstyle' `lw' `axis' `ciopts' `horizontal')
}
}
if "`citop_`i''"=="" & `n_ci'>0 {
local plots `plots' `ciplots'
local j = `j' + `cis_n_`i''
if inrange(`key_`i'', 1, `n_ci_`i'') {
local key = `j' + `key_`i''
}
local j = `j' + `n_ci_`i''
}
if `"`cionly_`i''"'=="" {
if `"`pstyle_`i''"'!="" local pstyle `pstyle_`i''
else {
set_pstyle `=mod(`i'-1,`pcycle')+1' `"`recast_`i''"' // returns pstyle
}
if `"`recast_`i''"'!="" local recast recast(`recast_`i'')
else local recast
if `"`horizontal'"'=="" | inlist(`"`recast_`i''"', ///
"area", "bar", "spike", "dropline", "dot") {
local plots `plots' ///
(scatter `b' `at' ///
if `plot'==`i'`ifopt_`i''`weightopt_`i'', ///
`addaxis' `pstyle' `twplotopts0' `twplotopts1_`i'' ///
`axis' `recast' `mlabel_`i'' `mlabvposition_`i'' ///
`twplotopts_`i'' `horizontal')
}
else {
local plots `plots' ///
(scatter `at' `b' ///
if `plot'==`i'`ifopt_`i''`weightopt_`i'', ///
`addaxis' `pstyle' `twplotopts0' `twplotopts1_`i'' ///
`axis' `recast' `mlabel_`i'' `mlabvposition_`i'' ///
`twplotopts_`i'')
}
local ++j
if `key_`i''==0 {
local key `j'
}
}
local plotlab `"`: lab `plot' `i''"'
gettoken trash : plotlab, qed(hasquotes)
if `hasquotes'==0 {
local plotlab `"`"`plotlab'"'"'
}
if "`citop_`i''"!="" & `n_ci'>0 {
local plots `plots' `ciplots'
local j = `j' + `cis_n_`i''
if inrange(`key_`i'', 1, `n_ci_`i'') {
local key = `j' + `key_`i''
}
local j = `j' + `n_ci_`i''
}
if "`key'"!="" {
local legendlbls `legendlbls' label(`key' `plotlab')
local legendorder `legendorder' `key'
}
}
if `"`legendorder'"'!="" {
local legendorder all order(`legendorder')
if `N_plots'==1 {
if `n_subgr'==1 & `"`legend'"'=="" {
local legendorder `legendorder' off
}
}
}
else local legendorder off
if `n_subgr'>1 {
local byopt `by', note("")
if (`N_plots'==1 & `"`bylegend'"'=="") | `"`legendorder'"'=="off" {
local byopt `byopt' legend(off)
}
local byopt by(`byopt' `bylegend' `byopts')
}
else local byopt
if `"`plots'"'=="" {
di as txt "(nothing to plot)"
exit
}
if `"`addplot'"'!="" {
if `"`addplotbelow'"'!="" {
local plots `addplot' || `plots' ||
}
else {
local plots `plots' || `addplot' ||
}
}
local plots two `plots', `axisalt' `groupsopts' `eqaxisopts' ///
`xlabel' `hlblsopts' `xtick' `xrange' `reverse' yti("") xti("") ///
legend(`legendlbls' `legendorder') `legend' `plotregion' `byopt' `twopts'
`plots'
// return
if `"`generate'"'!="" {
restore, not
di as txt _n "Generated variables:" _c
describe `returnvars'
}
return local graph `plots'
return local labels `"`xlabels'"'
return local eqlabels `"`eqlabels'"'
return local groups `"`groups'"'
return local headings `"`hlbls'"'
return local legend `"`legendlbls' `legendorder'"'
return scalar n_plots = `N_plots'
return scalar n_subgr = `n_subgr'
return scalar n_ci = `N_ci'
end
program parse_subgraphs // input: "subgr || subgr ..., opts"
local i 0
local empty 1
while (`"`macval(0)'"'!="") {
gettoken subgraph 0 : 0, parse("|") bind
if `"`macval(subgraph)'"'=="|" {
gettoken subgraph 0 : 0, parse("|") bind
if `"`macval(subgraph)'"'!="|" error 198 // require "||"
if `empty' {
local ++i
c_local subgr_`i' "." // use active model
}
else local empty 1
continue
}
if `"`0'"'=="" { // get opts if last
_parse comma subgraph opts : subgraph
}
if `"`macval(subgraph)'"'!="" { // skip last if empty
local empty 0
local ++i
c_local subgr_`i' `"`macval(subgraph)'"'
}
}
if `i'==0 { // check if empty
local i 1
c_local subgr_1 "." // use active model
}
c_local n_subgr `i'
c_local opts `macval(opts)'
end
program parse_globalopts
syntax [, ///
/// globalopts
HORizontal ///
VERTical ///
sort SORT2(str) ///
orderby(str) ///
order(str asis) ///
BYCoefs ///
noRECycle ///
grid(str) ///
noOFFsets ///
format(str) ///
noLABels ///
COEFLabels(str asis) ///
NOEQLABels ///
EQLabels(str asis) ///
eqstrict ///
HEADings(str asis) ///
GROUPs(str asis) ///
PLOTLabels(str asis) ///
bylabels(str asis) ///
GENerate GENerate2(name) ///
RELOCate(str asis) ///
replace ///
addplot(str asis) ///
NODROP ///
LEGend(passthru) ///
BYOPts(str asis) ///
Bname(passthru) /// so that b() is not b1title()
rename(passthru) ///
EQREName(passthru) ///
PCYCle(int 15) ///
/// twoway options not captured by _get_gropts, gettwoway
play(passthru) ///
XOVERHANGs ///
YOVERHANGs ///
fxsize(passthru) ///
fysize(passthru) ///
* ///
]
_get_gropts, graphopts(`options') gettwoway
local twopts `s(twowayopts)' `play' `xoverhangs' `yoverhangs' `fxsize' `fysize'
local opts0 `bname' `macval(rename)' `macval(eqrename)' `s(graphopts)'
if `"`sort'`sort2'"'!="" {
parse_sort `sort2' // returns local sort
}
if `"`orderby'"'!="" {
parse_orderby `orderby', `recycle' // returns local orderby
}
if `"`generate'"'!="" & `"`generate2'"'=="" {
local generate "__"
}
else local generate `"`generate2'"'
if `"`grid'"'!="" {
parse_grid, `grid' // returns local grid, gridopts
}
if `"`coeflabels'"'!="" {
parse_coeflabels `coeflabels'
// returns coeflabels, cltrunc, clwrap, clbreak, clinteract, clangle, clopts
}
if `"`clinteract'"'=="" {
local clinteract `"" # ""'
}
parse_eqlabels "`noeqlabels'" `eqlabels'
// returns eqlabels, eqashead, eqxlab
// if eqashead=="": also eqgap, eqwrap, eqtrunc, eqbreak, eqopts
// if eqashead!="": also hoff, hgap, hwrap, htrunc, hbreak, hopts
if `"`headings'"'!="" {
if `"`eqashead'"'!="" {
di as err "eqlabels(, asheadings) and headings() not both allowed"
exit 198
}
parse_headings `headings' // returns headings, hoff, hgap, hopts
}
else if `"`hgap'"'=="" local hgap 0
if `"`eqashead'"'!="" {
if "`bycoefs'"!="" {
di as err "eqlabels(, asheadings) and bycoefs not both allowed"
exit 198
}
}
if `"`groups'"'!="" {
parse_groups `groups' // returns groups, ggap, gwrap, gtrunc, gbreak, gopts
}
else local ggap 0
if `"`plotlabels'"'!="" {
parse_plotlabels `plotlabels' // returns plotlabels, plwrap, pltrunc, plbreak
}
if `"`bylabels'"'!="" {
parse_bylabels `bylabels' // returns bylabels, blwrap, bltrunc, blbreak
}
if `"`format'"'!="" {
confirm numeric format `format'
}
if `"`horizontal'"'!="" {
if `"`vertical'"'!="" {
di as err "horizontal and vertical not both allowed"
exit 198
}
}
if `"`addplot'"'!="" {
parse_addplot `addplot' // returns addplot, addplotbelow
}
parse_byopts, `byopts' // returns bylegend, byopts
foreach opt in ///
horizontal ///
vertical ///
sort ///
orderby ///
order ///
bycoefs ///
recycle ///
grid gridopts ///
offsets ///
format ///
labels ///
coeflabels cltrunc clwrap clbreak clinteract clangle clopts ///
noeqlabels ///
eqlabels eqashead eqxlab eqgap eqtrunc eqwrap eqbreak eqopts ///
eqstrict ///
headings hxlab hoff hgap htrunc hwrap hbreak hopts ///
groups ggap gtrunc gwrap gbreak gopts ///
plotlabels plwrap pltrunc plbreak ///
bylabels blwrap bltrunc blbreak ///
relocate ///
generate ///
replace ///
addplot addplotbelow ///
nodrop ///
legend ///
bylegend ///
byopts ///
{
c_local `opt' `"``opt''"'
}
c_local pcycle `pcycle'
c_local twopts `twopts'
merge_subgropts, `macval(opts0)'
c_local subgropts0 `subgropts'
c_local plotopts0 `plotopts1'
c_local modelopts0 `macval(modelopts1)'
c_local twplotopts0 `twplotopts1'
end
program parse_sort
syntax [anything] [, Descending by(str) ]
if `"`anything'"'=="" {
local subgr .
local plot .
}
else {
gettoken subgr rest : anything, parse(":")
if `"`rest'"'=="" { // sort(#)
local plot `"`subgr'"'
local subgr .
}
else if `"`subgr'"'==":" { // sort(:#)
local subgr .
gettoken plot rest : rest
}
else { // sort(#:#)
gettoken colon rest : rest, parse(":")
if `"`colon'"'!=":" {
di as err "sort(): invalid syntax"
exit 198
}
gettoken plot rest : rest
}
if `"`rest'"'!="" {
di as err "sort(): invalid syntax"
exit 198
}
foreach t in subgr plot {
if `"``t''"'=="" local `t' .
else if `"``t''"'!="." {
capt confirm integer number ``t''
if _rc==0 {
local rc = (``t''<=0)
}
else local rc 1
if `rc' {
di as err "sort(): invalid syntax"
exit 198
}
}
}
}
capt parse_sort_by, `by'
if _rc {
di as err `"sort(): '`by'' not allowed in by()"'
exit 198
}
local descending = ("`descending'"!="")
c_local sort `"`subgr' `plot' `descending' "`by'""'
end
program parse_sort_by
syntax [, b v se t tabs df p ll ul aux * ]
local by `b' `v' `se' `t' `tabs' `df' `p' `ll' `ul' `aux'
if `: list sizeof by'>1 exit 198
if inlist("`by'","ll", "ul", "aux") { // by(ll/ul/aux #)
if `"`options'"'=="" local by `by' 1
else {
capt confirm integer number `options'
if _rc==0 {
local rc = (`options'<=0)
}
else local rc 1
if `rc' exit 198
local by `by' `options'
}
}
else if `"`options'"'!="" exit 198
if `"`by'"'=="" local by b
c_local by `by'
end
program parse_orderby
syntax [anything] [, norecycle ]
gettoken subgr rest : anything, parse(":")
if `"`rest'"'=="" { // orderby(#)
local plot `"`subgr'"'
local subgr 1
}
else if `"`subgr'"'==":" { // ordeby(:#)
local subgr 1
gettoken plot rest : rest
}
else { // orderby(#:#)
gettoken colon rest : rest, parse(":")
if `"`colon'"'!=":" {
di as err "orderby(): invalid syntax"
exit 198
}
if "`recycle'"!="" {
di as err "orderby(): subgraph not allowed with norecycle"
exit 198
}
gettoken plot rest : rest
}
if `"`rest'"'!="" {
di as err "orderby(): invalid syntax"
exit 198
}
foreach t in subgr plot {
if `"``t''"'=="" local `t' 1
else {
capt confirm integer number ``t''
if _rc==0 {
local rc = (``t''<=0)
}
else local rc 1
if `rc' {
di as err "orderby(): invalid syntax"
exit 198
}
}
}
if "`recycle'"!="" local subgr .
c_local orderby `subgr' `plot'
end
program parse_grid
syntax [, Between Within None * ]
if ("`between'"!="") + ("`within'"!="") + ("`none'"!="") > 1 {
di as err "grid(): only one of between, within, and none allowed"
exit 198
}
c_local grid `between' `within' `none'
c_local gridopts `options'
end
program parse_coeflabels
mata: coefplot_parsecomma("coeflabels", "0", "0")
syntax [, Truncate(numlist integer max=1 >0) ///
Wrap(numlist integer max=1 >0) noBreak ///