-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathviolinplot.ado
More file actions
2454 lines (2364 loc) · 81.6 KB
/
Copy pathviolinplot.ado
File metadata and controls
2454 lines (2364 loc) · 81.6 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.2.7 12jun2025 Ben Jann
program violinplot, rclass
version 15
// syntax
syntax [anything] [if] [in] [pw iw fw] [, TABle ///
VERTical HORizontal ///
Left Right ///
OVERLay ///
ASOver Over(str) ATOver swap noSTack ///
SPlit(str) ///
by(str) ///
DScale(str) ABSolute tight ltight rtight ///
RAnge(str) n(numlist int >=1 max=1) PDFopts(str) ///
qdef(passthru) ///
cw ///
LABels(str asis) OLABels(str asis) SLABels(str asis) ///
BYLABels(str asis) noLABel ///
key(str) order(str) bind ///
OFFset(numlist) DOFFset(numlist) ///
NODensity ///
NOLine Line Line2(str) LColors(str asis) ///
NOFill Fill Fill2(str) FColors(str asis) ///
NOWhiskers Whiskers Whiskers2(str) WColors(str asis) ///
NOBox Box Box2(str) BColors(str asis) ///
NOMEDian MEDian MEDian2(str) MEDColors(str asis) ///
NOMEAN MEAN MEAN2(str) MEANColors(str asis) ///
NORAG RAG RAG2(str) RAGColors(str asis) ///
OUTsides OUTsides2(str) ///
Colors(str asis) ///
gap(real 0.5) pad(numlist max=2 >=0) ///
PSTYles(numlist int >0) ///
BYOPTs(str) ///
addplot(str asis) * ]
// outsides() -> rag()
if `"`outsides2'"'!="" local outsides outsides
if "`outsides'"!="" {
if `"`rag'`rag2'"'!="" {
di as err "only on of rag() and outsides() allowed"
exit 198
}
local rag2 outsides msymbol(o) `outsides2'
local outsides
local outsides2
}
// - split()
_parse_split `split'
if "`split'"!="" {
if "`stack'"!="" {
di as err "split() and nostack not both allowed"
exit 198
}
if "`overlay'"!="" {
di as err "split() and overlay not both allowed"
exit 198
}
}
// - orientation
if "`split'"!="" | ("`left'"!="" & "`right'"!="") {
local left
local right
}
if "`vertical'"!="" & "`horizontal'"!="" {
di as err "vertical and horizontal not both allowed"
exit 198
}
if "`horizontal'"=="" {
if "`split'"!="" local vertical vertical
else if "`overlay'"!="" & "`left'`right'"=="" local vertical vertical
else if "`nodensity'"!="" local vertical vertical
}
// - by() and over()
if `"`by'"'!="" _parse_overby by `by'
if `"`over'"'!="" {
if "`asover'"!="" {
di as err "asover and over() not both allowed"
exit 198
}
_parse_overby over `over'
if "`atover'"!="" {
if "`swap'"!="" {
di as err "atover and swap not both allowed"
exit 198
}
if "`over_missing'"!="" {
di as err "missing suboption in over() not allowed with atover"
exit 198
}
if "`over_total'"!="" {
di as err "total suboption in over() not allowed with atover"
exit 198
}
if "`over_sort'"!="" {
di as err "sort suboption in over() not allowed with atover"
exit 198
}
}
}
else {
if "`atover'"!="" {
di as err "atover requires over()"
exit 198
}
if "`asover'"!="" & "`swap'"!="" {
di as err "asover and swap not both allowed"
exit 198
}
}
// - nodensity
if "`nodensity'"!="" {
local noline noline
local line
local line2
local nofill nofill
local fill
local fill2
}
// - fill option; default is nofill (unless noline is specified)
if "`nofill'"=="" & "`split'"!="" local fill fill
else if `"`fill2'"'!="" local fill fill
if "`fill'"!="" {
if "`nofill'"!="" {
di as err "fill and nofill not both allowed"
exit 198
}
}
_parse_fill, `fill2' // fill_select, fill_line ... fill2
// - line option; default is line
if "`noline'"=="" local line line
else if `"`line2'"'!="" local line line
if "`line'"!="" {
if "`noline'"!="" {
di as err "line and noline not both allowed"
exit 198
}
}
else if "`nofill'"=="" local fill fill // set fill on if noline
// - whiskers option: default is whiskers unless overlay has been specified
if "`nowhiskers'"=="" {
if "`nodensity'"!="" local whiskers whiskers
else if "`overlay'`split'"=="" local whiskers whiskers
}
if `"`whiskers2'"'!="" local whiskers whiskers
if "`whiskers'"!="" {
if "`nowhiskers'"!="" {
di as err "whiskers and nowhiskers not both allowed"
exit 198
}
_parse_whisk, `whiskers2' // whisk_stat, whisk_cap ... whiskers2
}
// - box option: default is box unless overlay has been specified
if "`nobox'"=="" {
if "`nodensity'"!="" local box box
else if "`overlay'"=="" local box box
}
if `"`box2'"'!="" local box box
if "`box'"!="" {
if "`nobox'"!="" {
di as err "box and nobox not both allowed"
exit 198
}
_parse_box, `box2' // box_type, box_limits, box_stat ... box2
if "`box_type'"=="" {
if "`nodensity'"!="" local box_type bar
else if "`split'"!="" local box_type lines
else local box_type spike
}
}
if "`box_BARW'"!="" {
if "`split'"!="" local box_BARW = `box_BARW'/2
local box_barw barwidth(`box_BARW')
}
else {
if "`nodensity'"!="" local box_BARW 0.7
else local box_BARW = 1/6
if "`split'"!="" local box_BARW = `box_BARW'/2
}
if "`whiskers'"!="" {
if "`box_type'"=="bar" & "`whisk_nocap'"=="" local whisk_cap cap
if "`whisk_capw'"=="" {
if "`box_type'"=="bar" local whisk_capw = `box_BARW' * .6
else if "`split'"!="" local whisk_capw 0.05
else local whisk_capw 0.1
}
else if "`split'"!="" local whisk_capw = `whisk_capw'/2
}
// - median() and mean(): default is median and nomean
if "`nomedian'"=="" local median median
foreach el in median mean {
if `"``el'2'"'!="" local `el' `el'
if "``el''"!="" {
if "`no`el''"!="" {
di as err "`el' and no`el' not both allowed"
exit 198
}
_parse_med `el', ``el'2' // `el'_type, `el'_stat ... `el'2
if "``el'_type'"=="" {
if "`el'"=="median" {
if "`box_type'"=="bar" local `el'_type bline
else if "`split'"!="" local `el'_type line
else local `el'_type marker
}
else local `el'_type marker
}
if "``el'_type'"=="bline" {
if "``el'_BARW'"=="" local `el'_BARW = `box_BARW'
else if "`split'"!="" local `el'_BARW = ``el'_BARW'/2
}
else if "``el'_BARW'"!="" local `el'_barw barwidth(``el'_BARW')
}
}
// - rag option: default is norag
if `"`rag2'"'!="" local rag rag
if "`rag'"!="" {
if "`norag'"!="" {
di as err "rag and norag not both allowed"
exit 198
}
_parse_rag, `rag2' // rag_offset, rag_spread ... rag2
if "`left'"!="" | "`split'"!="" {
if "`rag_right'"=="" local rag_left left
}
else if "`right'"!="" {
if "`rag_left'"=="" local rag_right right
}
if "`rag_left'"!="" & "`rag_right'"!="" {
local rag_left
local rag_right
}
}
// - list of summary statistics
if `"`mean_stat'"'=="" local stats mean
else local stats `"`mean_stat'"'
if `"`median_stat'"'=="" local stats `"`stats' median"'
else local stats `"`stats' `median_stat'"'
if `"`box_stat'"'=="" local stats `"`stats' p25 p75"'
else local stats `"`stats' `box_stat'"'
// - list of plot types
local plotlist `fill'
if "`box_type'"=="fill" local plotlist `plotlist' `box'
local plotlist `plotlist' `rag' `whiskers'
if "`box_type'"!="fill" local plotlist `plotlist' `box'
if "`median_type'"=="line" local plotlist `plotlist' `median'
if "`mean_type'"=="line" local plotlist `plotlist' `mean'
local plotlist `plotlist' `line'
if "`median_type'"!="line" local plotlist `plotlist' `median'
if "`mean_type'"!="line" local plotlist `plotlist' `mean'
// - activate (suppressed) line if plotlist is empty
if "`plotlist'"=="" {
local line line
local plotlist line
local lineomit 1
}
else local lineomit 0
// - reorder plot types
if `"`order'"'!="" {
_parse_order `"`order'"' "`plotlist'" // returns plotlist
}
// - determine legend key
_parse_key, `key'
if "`key'"!="" {
if !`: list key in plotlist' {
di as err "key(): `key' not available"
exit 198
}
}
else {
if "`line'"!="" local key "line"
else if "`fill'"!="" local key "fill"
else if "`box'"!="" local key "box"
else gettoken key : plotlist
}
// - further option
if "`n'"!="" local nopt n(`n')
_parse_pdfopts, `pdfopts' // extracts -exact- option
_parse_range `range' // returns range
_parse_dscale `dscale' // returns dscale and dstype
local dscale = `dscale' * 0.5
if "`tight'"=="" local tight `ltight' `rtight'
if "`pad'"=="" {
if `dscale'>=. local pad 0
else local pad 0.5
}
local hasaddplot = `"`addplot'"'!=""
// determine whether density estimation is needed
if "`line'`fill'"=="" {
local domit 1
if "`box_type'"=="fill" local domit 0
else if "`box_type'"=="lines" local domit 0
else if "`median_type'"=="line" local domit 0
else if "`mean_type'"=="line" local domit 0
else if "`rag_spread'"!="" {
if `rag_spread2'>=. local domit 0
}
else if "`rag_stack'"!="" {
if `rag_stack'!=1 local domit 0
}
}
else local domit 0
if `domit' {
local dstype
local doffset
}
// parse varlist
gettoken varlist : anything, match(haspar)
if "`haspar'"!="" {
_parse_vlists `anything' // returns varlist, k_var, varlist_#, k_var_#
}
else {
_parse_vlist `anything' // returns varlist, k_var
local k_grp 1
local k_var_1 `k_var'
local varlist_1 `varlist'
}
if `k_grp'>1 {
if "`asover'"=="" & "`by'"!="" {
di as err "by() not allowed with multiple variable groups" /*
*/ " (unless asover is specified)"
exit 198
}
}
if "`atover'"!="" {
if `k_var'>1 & "`overlay'"=="" {
di as err "atover not allowed with multiple variables" /*
*/ " unless overlay is specified"
exit 198
}
else if `k_var'==1 & "`overlay'"!="" {
di as err "overlay not allowed if atover is specified with" /*
*/ " single variable"
exit 198
}
}
if "`over_sort'"!="" {
if "`over_sort2'"=="" local over_sort2 1
if `over_sort2'>`k_var_1' { // support for first variable group only
di as err "over(, sort()): variable #`over_sort2' not found"
exit 499
}
}
// preserve current estimates
tempname ecurrent
_estimates hold `ecurrent', restore nullok
// sample and weights
if "`cw'"!="" marksample touse
else marksample touse, novarlist
if "`over'"!="" & "`over_missing'"=="" markout `touse' `by', strok
if "`by'"!="" & "`by_missing'"=="" markout `touse' `by', strok
if "`split'"!="" markout `touse' `split', strok
if "`weight'"!="" local wgt `"[`weight'`exp']"'
else local wgt
// collect levels of split
if "`split'"!="" {
local splitstr = substr("`: type `split''",1,3)=="str"
if `splitstr' {
qui levelsof `split' if `touse'
}
else {
local splitfmt: format `split'
tempname SLEV
qui levelsof `split' if `touse', matrow(`SLEV')
}
local k_split = r(r)
if `k_split'!=2 {
di as err "split() must identify exactly two groups"
exit 499
}
local splitlevels `"`r(levels)'"'
}
else {
local splitstr 0
local k_split 1
local splitlevels ""
}
// collect levels of over and by
if "`over'"!="" {
local overstr = substr("`: type `over''",1,3)=="str"
if `overstr' {
qui levelsof `over' if `touse', `over_missing'
}
else {
local overfmt: format `over'
tempname OLEV
qui levelsof `over' if `touse', `over_missing' matrow(`OLEV')
}
local k0_over = r(r)
local k_over = `k0_over' + ("`over_total'"!="")
local overlevels `"`r(levels)'"'
if "`atover'"!="" & `overstr' {
di as err "atover not allowed with string variable"
exit 198
}
}
else {
local overstr 0
local k0_over 0
local k_over 1
local overlevels ""
}
if "`by'"!="" {
local bystr = substr("`: type `by''",1,3)=="str"
if `bystr' {
qui levelsof `by' if `touse', `by_missing'
}
else {
local byfmt: format `by'
tempname BYLEV
qui levelsof `by' if `touse', `by_missing' matrow(`BYLEV')
}
local k0_by = r(r)
local k_by = `k0_by' + ("`by_total'"!="")
local bylevels `"`r(levels)'"'
}
else {
local bystr 0
local k0_by 0
local k_by 1
local bylevels ""
}
// assign results layers
// - subgraphs
if "`by'"!="" local BY by
else if "`asover'"=="" & `k_grp'>1 local BY grp
else local BY
if "`BY'"=="" local By by
else local By `BY'
// - over
if "`asover'"!="" local OVER grp
else local OVER over
// - clusters and plots within clusters
if `k_var'==1 & `k_`OVER''>1 local swap "swap"
else if `k_var'>1 & `k_`OVER''==1 local swap
if "`swap'"!="" {
local CLUS var
local PLOT `OVER'
}
else {
local CLUS `OVER'
local PLOT var
}
local stack = "`stack'"=="" // 0 if nostack is specified
if "`overlay'"!="" | `k_`CLUS''==1 local stack 0
// - physical plots
if "`split'"!="" local PID split
else local PID `PLOT'
// prepare pstyles
if "`pstyles'"=="" {
if "`split'"!="" local pstyles 2
else if "`overlay'"!="" local pstyles `k_`PLOT''
else if `stack' local pstyles 1
else if `k_`CLUS''==1 local pstyles 1
else local pstyles `k_`PLOT''
if `pstyles'!=1 {
numlist "1/`=min(`pstyles', 15)'"
local pstyles `r(numlist)'
}
}
// collect p#[el]() options
forv p = 1/`k_`PID'' {
_parse_popts "" `p' , `options' // must type space before ","
foreach el of local plotlist {
_parse_popts `el' `p' , `options' // must type space before ","
}
}
// generate colors
if `"`colors'"'!="" {
_parse comma lhs rhs : colors
if `"`rhs'"'=="" local rhs ","
colorpalette `lhs' `rhs' nograph n(`k_`PID'')
if r(n)!=`k_`PID'' {
// wrong number of colors, e.g. because select() was applied
colorpalette `r(p)', nograph n(`k_`PID'') class(`r(pclass)')
}
local colors `"`r(p)'"'
}
foreach opt in l f w b med mean rag {
if `"``opt'colors'"'=="" {
local `opt'colors `"`colors'"'
continue
}
_parse comma lhs rhs : `opt'colors
if `"`rhs'"'=="" local rhs ","
colorpalette `lhs' `rhs' nograph n(`k_`PID'')
if r(n)!=`k_`PID'' {
// wrong number of colors, e.g. because select() was applied
colorpalette `r(p)', nograph n(`k_`PID'') class(`r(pclass)')
}
local `opt'colors `"`r(p)'"'
}
// collect labels
// - variable labels
local varlbls
local ii 0
forv g = 1/`k_grp' {
local varlbls_`g'
local space
forv i = 1/`k_var_`g'' {
gettoken lab labels : labels
if `"`lab'"'=="" {
local v: word `i' of `varlist_`g''
if "`label'"=="" local lab: var lab `v'
if `"`lab'"'=="" local lab "`v'"
}
local varlbls_`g' `"`varlbls_`g''`space'`"`lab'"'"'
if `i'>`ii' {
local varlbls `"`varlbls'`space'`"`lab'"'"'
local ++ii
}
local space " "
}
}
// - variable groups (use label/name of first variable in group)
local grplbls
local space
if "`BY'"=="grp" local glabels `"`bylabels'"'
else local glabels `"`olabels'"'
forv g = 1/`k_grp' {
gettoken lab glabels : glabels
if `"`lab'"'=="" {
local v: word 1 of `varlist_`g''
if "`label'"=="" local lab: var lab `v'
if `"`lab'"'=="" local lab "`v'"
}
local grplbls `"`grplbls'`space'`"`lab'"'"'
local space " "
}
// - over categories and by categories
local overlabels: copy local olabels
foreach tmp in by over {
local `tmp'lbls
local space
foreach lev of local `tmp'levels {
gettoken lab `tmp'labels : `tmp'labels
if `"`lab'"'=="" {
if ``tmp'str' local lab `"`lev'"'
else if `"`lev'"'=="." { // sysmis
local lab `"``tmp'_missing2'"'
if `"`lab'"'=="" {
if "`label'"!="" local lab "."
else local lab "(missing)"
}
}
else {
if "`label'"=="" {
local lab: lab (``tmp'') `lev', strict
}
if `"`lab'"'=="" {
local lab `: di ``tmp'fmt' `lev''
}
}
}
local `tmp'lbls `"``tmp'lbls'`space'`"`lab'"'"'
local space " "
}
if `k_`tmp''>`k0_`tmp'' {
gettoken lab `tmp'labels : `tmp'labels
if `"`lab'"'=="" local lab `"``tmp'_total2'"'
if `"`lab'"'=="" local lab "(total)"
local `tmp'lbls `"``tmp'lbls'`space'`"`lab'"'"'
}
}
// - split labels
local splitlbls
local space
foreach lev of local splitlevels {
gettoken lab slabels : slabels
if `"`lab'"'=="" {
if `splitstr' local lab `"`lev'"'
else {
if "`label'"=="" {
local lab: lab (`split') `lev', strict
}
if `"`lab'"'=="" {
local lab `: di `splitfmt' `lev''
}
}
}
local splitlbls `"`splitlbls'`space'`"`lab'"'"'
local space " "
}
// prepare tempvars
local ids tag byid overid splitid grpid varid
local vres pos avg med blo bup wlo wup dup dlo at
if "`rag'"!="" {
local ids `ids' rtag
local vres `vres' xrag
if "`weight'"=="" & "`rag_unique'"=="" local rag_noweight noweight
if "`rag_noweight'"=="" local vres `vres' wrag wvar
}
tempvar `ids' `vres'
foreach v of local ids {
qui gen byte ``v'' = .
}
foreach v of local vres {
qui gen double ``v'' = .
}
tempvar TOUSE
if "`by'`over'`split'"!="" {
qui gen byte `TOUSE' = .
}
if "`wvar'"!="" {
if "`weight'"!="" qui replace `wvar' `exp'
else qui replace `wvar' = 1
local ragwgt [aw=`wrag']
}
// fill in byid for existing observations so that addplot() will
// will work with by()
if `hasaddplot' & "`by'"!="" {
local j 0
foreach lev of local bylevels {
local ++j
if `bystr' local lev `"`"`lev'"'"'
qui replace `byid' = `j' if `by'==`lev'
}
}
// compute results
tempname TMP N NOBS S STATS BWIDTH
local preserve preserve
if `hasaddplot' local r0 = _N // (append results)
else local r0 0
local r1 `r0'
local eq 0
local eqs
local bylvls: copy local bylevels
forv j = 1/`k_by' {
gettoken bylev bylvls : bylvls
local olvls: copy local overlevels
forv o = 1/`k_over' {
gettoken olev olvls : olvls
local slvls: copy local splitlevels
forv s = 1/`k_split' {
local ++eq
gettoken slev slvls : slvls
_makeiff `touse' `TOUSE' /* returns iff, tousej, eqlbl
*/ `j' `k0_by' "`by'" `bystr' `"`bylev'"' "`BYLEV'"/*
*/ `o' `k0_over' "`over'" `overstr' `"`olev'"' "`OLEV'"/*
*/ `s' "`split'" `splitstr' `"`slev'"' "`SLEV'"
local eq`eq' `"`eqlbl'"'
local EQ`eq' `"`eqLBL'"'
forv g = 1/`k_grp' {
forv i = 1/`k_var_`g'' {
if !`: list sizeof RANGE' local RANGE: copy local range
gettoken pdf_l RANGE : RANGE
gettoken pdf_u RANGE : RANGE
local eqs `eqs' `eq'
local xvar: word `i' of `varlist_`g''
su `xvar' `iff', meanonly
matrix `N' = (r(N), 0)
matrix rown `N' = `xvar'
if (`N'[1,1]==0) { // no observations
matrix `NOBS' = nullmat(`NOBS') \ `N'
matrix `TMP' = .
mat rown `TMP' = `xvar'
matrix `BWIDTH' = nullmat(`BWIDTH') \ `TMP'
matrix `TMP' = J(1,6,.)
mat rown `TMP' = `xvar'
matrix `STATS' = nullmat(`STATS') \ `TMP'
continue
}
if `domit' local x_is_cons 1
else local x_is_cons = r(max)==r(min)
// summary stats
Fit_stats `"`stats'"' "`xvar'" `"`wgt'"' `"`iff'"'/*
*/ `"`qdef'"' "`box_limits'" `"`whisk_stat'"'/*
*/ `"`pdf_l'"' `"`pdf_u'"'
matrix `S' = r(stats)
matrix `STATS' = nullmat(`STATS') \ `S'[1,1..6]
matrix `N'[1,2] = r(sum_w)
matrix `NOBS' = nullmat(`NOBS') \ `N'
// density estimate
if !`x_is_cons' {
Fit_PDF "`xvar'" `"`wgt'"' `"`iff'"' "`tight'"/*
*/ "`S'" "`exact'" "`nopt'" `"`pdfopts'"'
local n: colsof e(b)
matrix `BWIDTH' = nullmat(`BWIDTH') \ e(bwidth)'
}
else { // skip density estimate if x is constant
local n 1
matrix `TMP' = .
mat rown `TMP' = `xvar'
matrix `BWIDTH' = nullmat(`BWIDTH') \ `TMP'
}
local a = `r1' + 1
local b = `r1' + `n' + 1 // insert empty row at end
if `b' > _N {
`preserve'
local preserve
_addobs `b' `touse' `tousej'
}
if !`x_is_cons' {
if "`absolute'"!="" {
mata: st_store((`a',`b'-1), "`dup'", ///
st_numscalar("e(W)") * st_matrix("e(b)")')
}
else {
mata: st_store((`a',`b'-1), "`dup'", ///
st_matrix("e(b)")')
}
mata: st_store((`a',`b'-1), "`at'", ///
st_matrix("e(at)")')
}
// tag start and end of density estimate
qui replace `tag' = 1 in `a' // first row
qui replace `tag' = 2 in `b' // last row (missing)
// store summary stats in data
qui replace `avg' = `S'[1,1] in `a'
qui replace `med' = `S'[1,2] in `a'
qui replace `blo' = `S'[1,3] in `a'
qui replace `bup' = `S'[1,4] in `a'
qui replace `wlo' = `S'[1,5] in `a'
qui replace `wup' = `S'[1,6] in `a'
// rag: copy data
if "`rag'"!="" {
qui replace `rtag' = 1 in `a' // first row
mata: _copyrag(`a', `b', "`rag_outsides'", /*
*/ "`rag_unique'"!="", "`wvar'", "`S'")
}
// ids
qui replace `varid' = `i' in `a'/`b'
qui replace `grpid' = `g' in `a'/`b'
qui replace `splitid' = `s' in `a'/`b'
qui replace `overid' = `o' in `a'/`b'
qui replace `byid' = `j' in `a'/`b'
local r1 `b'
}
}
}
}
}
local neq `eq'
if `"`whisk_stat'"'!="" local coln `stats' `whisk_stat'
else local coln `stats' whisk_l whisk_u
if !`domit' {
mat `STATS' = `BWIDTH', `STATS'
local coln bwidth `coln'
}
if !inlist("`weight'","","fweight") {
mat `STATS' = `NOBS', `STATS'
local coln N sum_w `coln'
}
else {
mat `STATS' = `NOBS'[1...,1], `STATS'
local coln N `coln'
}
mat coln `STATS' = `coln'
if "`by'`over'`split'"!="" {
mat roweq `STATS' = `eqs'
}
// check whether there have been any valid observations at all
if `r1'==`r0' error 2000
// sort results
if "`over_sort'"!="" {
tempname sorttag
qui gen byte `sorttag' = `tag'==1 & `varid'==`over_sort2'/*
*/ & `grpid'==1 & `byid'==1 in `=`r0'+1'/`r1'
mata: _over_sort((`r0'+1, `r1'))
}
// determine plot positions on categorical axis
local in "in `=`r0'+1'/`r1'"
if "`atover'"!="" {
if `k_`CLUS''==1 {
local i 0
foreach ii of local overlevels {
local ++i
qui replace `pos' = `ii' if ``PLOT'id'==`i' `in'
}
local plotpos `"`overlevels'"'
}
else {
local i 0
foreach ii of local overlevels {
local ++i
qui replace `pos' = `ii' if ``CLUS'id'==`i' `in'
}
local cluspos `"`overlevels'"'
}
}
else if "`overlay'"!="" {
if "`vertical'"!="" {
qui replace `pos' = ``CLUS'id'-1 `in'
numlist "0/`=`k_`CLUS''-1'"
}
else {
qui replace `pos' = -(``CLUS'id'-1) `in'
numlist "0(-1)`=-(`k_`CLUS''-1)'"
}
local cluspos `r(numlist)'
}
else {
if `k_`CLUS''==1 {
if "`vertical'"!="" {
qui replace `pos' = ``PLOT'id'-1 `in'
numlist "0/`=`k_`PLOT''-1'"
}
else {
qui replace `pos' = -(``PLOT'id'-1) `in'
numlist "0(-1)`=-(`k_`PLOT''-1)'"
}
local plotpos `r(numlist)'
}
else {
local cluspos
local plotpos
if "`vertical'"!="" local step 1
else local step -1
local ii 0
forv c = 1/`k_`CLUS'' {
if "`CLUS'"=="grp" local ktmp `k_var_`c''
else local ktmp `k_`PLOT''
local ii0 = `ii'
forv i = 1/`ktmp' {
qui replace `pos' = `ii' if ``CLUS'id'==`c' &/*
*/ ``PLOT'id'==`i' `in'
local plotpos `plotpos' `ii'
local ii = `ii' + `step'
}
local cluspos `cluspos' `= (`ii0' + `ii' - `step') / 2'
local ii = `ii' + `step'*`gap'
}
}
}
// determine (minimum) range of categorical axis
if "`pad'"!="0" {
su `pos', meanonly
gettoken pad0 pad1 : pad
gettoken pad1 : pad1
if "`pad1'"=="" local pad1 `pad0'
local csrange "`=r(min) - `pad0'' `=r(max) + `pad1''"
}
// shift and rescale PDFs
local in "in `=`r0'+1'/`r1'"
if "`dstype'"=="individual" {
forv j = 1/`k_`By'' {
forv c = 1/`k_`CLUS'' {
forv i = 1/`k_`PLOT'' {
_dscale `dup' `dlo' `pos' if ``By'id'==`j' & /*
*/ ``CLUS'id'==`c' & ``PLOT'id'==`i' `in'/*
*/, dscale(`dscale')
}
}
}
}
else if "`dstype'"=="plot" {
forv j = 1/`k_`By'' {
forv i = 1/`k_`PLOT'' {
_dscale `dup' `dlo' `pos' if ``By'id'==`j' & /*
*/ ``PLOT'id'==`i' `in', dscale(`dscale')
}
}
}
else if "`dstype'"=="group" {
forv j = 1/`k_`By'' {
forv c = 1/`k_`CLUS'' {
_dscale `dup' `dlo' `pos' if ``By'id'==`j' & /*
*/ ``CLUS'id'==`c' `in', dscale(`dscale')
}
}
}
else if "`dstype'"=="subgraph" {
forv j = 1/`k_`By'' {
_dscale `dup' `dlo' `pos' if ``By'id'==`j' `in',/*
*/ dscale(`dscale')
}
}
else if !`domit' {
_dscale `dup' `dlo' `pos' `in', dscale(`dscale')
}
// add offsets
local in "in `=`r0'+1'/`r1'"
// - determine default offset
if "`split'"!="" {
if "`split_offset'"!="" local offset `split_offset' // old syntax
if "`offset'"=="" {
if "`box_type'"=="bar" local offset = `box_BARW'/2
else { // add offset equal to 1% of range of axis
_get_split_offset `cluspos' `plotpos' // returns offset
}
}
}
if "`rag'"!="" & "`rag_offset'"=="" local rag_offset `offset'
// - elements of box plot
if !inlist("`offset'", "", "0") {
if `: list sizeof offset'>1 {
tempname POS
qui gen double `POS' = .
local tmp `offset'
forv p = 1/`k_`PID'' {
if "`tmp'"=="" local tmp `offset'
gettoken num tmp : tmp
if "`vertical'"=="" local num = -`num'
qui replace `POS' = `pos' + `num'*cond(`splitid'==1,-1,1)/*
*/ if ``PID'id'==`p' `in'
}
}
else {
if "`vertical'"=="" local offset = -`offset'
tempname POS
qui gen double `POS' = `pos' + `offset'*cond(`splitid'==1,-1,1) `in'
}
}
else local POS `pos'
// - rag
if "`rag'"!="" {
if !inlist("`rag_offset'", "", "0") {
if `: list sizeof rag_offset'>1 {
tempname RPOS
qui gen double `RPOS' = .
local tmp `rag_offset'
forv p = 1/`k_`PID'' {
if "`tmp'"=="" local tmp `rag_offset'
gettoken num tmp : tmp
if "`vertical'"=="" local num = -`num'
qui replace `RPOS' = `pos' + `num'*cond(`splitid'==1,-1,1)/*
*/ if ``PID'id'==`p' `in'
}
}
else {
if "`vertical'"=="" local rag_offset = -`rag_offset'
tempname RPOS
qui gen double `RPOS' = `pos' + `rag_offset'*cond(`splitid'==1,-1,1) `in'
}
}
else if "`rag_spread'`rag_stack'"!="" {
tempname RPOS
qui gen double `RPOS' = `pos' `in'
}
else local RPOS `pos'
if "`rag_spread'`rag_stack'"!="" {
local rag_dir = "`rag_left'`rag_right'"!=""
if `rag_dir' {
if "`rag_left'"!="" & "`vertical'"!="" local rag_dir -1
else if "`rag_right'"!="" & "`vertical'"=="" local rag_dir -1
}
if "`rag_spread'"!="" {
mata: _rag_spread(`r0'+1, `r1', `rag_spread', `rag_spread2',/*
*/ `rag_dir')
}
else {
mata: _rag_stack(`r0'+1, `r1', `rag_stack', `rag_stack2',/*
*/ `rag_dir')
}
}
}
// - density
if !inlist("`doffset'", "", "0") {
if `: list sizeof doffset'>1 {
tempname Pos
qui gen double `Pos' = .
local tmp `doffset'
forv p = 1/`k_`PID'' {
if "`tmp'"=="" local tmp `doffset'
gettoken num tmp : tmp
if "`vertical'"=="" local num = -`num'
qui replace `Pos' = `pos' + `num'*cond(`splitid'==1,-1,1) /*
*/ if ``PID'id'==`p' `in'
qui replace `dup' = `dup' + `num'*cond(`splitid'==1,-1,1)/*
*/ if ``PID'id'==`p' `in'
qui replace `dlo' = `dlo' + `num'*cond(`splitid'==1,-1,1)/*
*/ if ``PID'id'==`p' `in'
}
}
else {
if "`vertical'"=="" local doffset = -`doffset'
tempname Pos
qui gen double `Pos' = `pos' + `doffset'*cond(`splitid'==1,-1,1) `in'
qui replace `dup' = `dup' + `doffset'*cond(`splitid'==1,-1,1) `in'
qui replace `dlo' = `dlo' + `doffset'*cond(`splitid'==1,-1,1) `in'
}
}
else local Pos `pos'
// compute points on density curve used by some plot types
local pdftmp
if "`box_type'"=="fill" local pdftmp box_dlo box_dup box_at
else if "`box_type'"=="lines" local pdftmp box_dlo box_dup box_at
if "`median_type'"=="line" local pdftmp `pdftmp' med_dlo med_dup
if "`mean_type'"=="line" local pdftmp `pdftmp' avg_dlo avg_dup
if "`pdftmp'"!="" {
tempvar `pdftmp'