-
Notifications
You must be signed in to change notification settings - Fork 0
/
robstat.ado
1989 lines (1920 loc) · 63.8 KB
/
robstat.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 1.0.4 21dec2020 Ben Jann
local rc 0
capt findfile lmoremata.mlib
if _rc {
di as error "the {bf:moremata} package is required; type {stata ssc install moremata, replace}"
error 499
}
program robstat, eclass properties(svyb svyj)
version 11
if replay() {
Display `0'
exit
}
local version : di "version " string(_caller()) ":"
Parse_opts `0' // returns lhs, options, statistics, jbtest, jbtest2, jbwald, cluster
`version' _vce_parserun robstat, mark(over) bootopts(`cluster') ///
jkopts(`cluster') wtypes(pw iw fw) noeqlist: ///
`lhs', nose `statistics' `options'
if "`s(exit)'" != "" {
ereturn local cmdline `"robstat `0'"'
if "`jbtest'"!="" {
JBtest, `jbtest2' `jbwald' display
}
exit
}
Estimate `0' // returns diopts
ereturn local cmdline `"robstat `0'"'
if "`jbtest'"!="" {
JBtest, `jbtest2' `jbwald'
}
Display, `diopts'
if `"`e(IFvars)'"'!="" {
local IFnote "(influence functions stored in: `e(IFvars)')"
local i 0
while (1) {
local ++i
local line: piece `i' 80 of "`IFnote'"
if "`line'"=="" continue, break
di as txt "`line'"
}
di
}
end
program Parse_opts
_parse comma lhs 0 : 0
syntax [, nose CLuster(passthru) Statistics(str) swap ///
JBtest JBtest2(str) WALD * ]
if `"`jbtest2'"'!="" local jbtest jbtest
if "`jbtest'"!="" & `"`statistics'"'!="" {
di as err "statistics() not allowed with jbtest"
exit 198
}
if "`jbtest'"!="" & "`swap'"!="" {
di as err "swap not allowed with jbtest"
exit 198
}
if "`wald'"!="" & "`jbtest'"=="" {
di as err "wald only allowed if jbtest is specified"
exit 198
}
if "`jbtest'"!="" JBtest_args `jbtest2' // sets local statistics
c_local lhs `"`lhs'"'
c_local cluster `cluster'
c_local jbtest `jbtest'
if "`jbtest2'"!="" {
c_local jbtest2 jbtest2(`jbtest2')
}
c_local jbwald `wald'
if "`statistics'"!="" {
c_local statistics statistics(`statistics')
}
c_local options `swap' `options'
end
program Display
syntax [, noHEader noTABle Level(passthru) CILog * ]
if "`cilog'"!="" {
if c(stata_version)<15 {
di as err "option cilog only allowed in Stata 15 or newer"
exit 198
}
}
_get_diopts diopts, `options'
if `"`e(cmd)'"'!="robstat" {
di as err "last robstat results not found"
exit 301
}
if `"`level'"'=="" local level level(`e(level)')
if `"`header'"'=="" {
if c(stata_version)>=12 {
nobreak {
Display_ereturn local cmd "total" // mimick header of -total-
capture noisily break {
_coef_table_header
}
Display_ereturn local cmd "robstat"
if _rc exit _rc
}
}
else _coef_table_header
if `"`e(over)'"'!="" {
_svy_summarize_legend
}
else di ""
}
if `"`table'"'=="" {
if "`cilog'"!="" {
mata: st_local("cilog", "cilog" * anyof(st_matrix("e(class)"),2))
}
nobreak {
local depvar `"`e(depvar)'"'
local modified 0
if e(N_stats)==1 {
if e(N_vars)>1 | `"`e(over)'"'!="" {
Display_ereturn local depvar `"`e(statistics)'"'
local modified 1
}
}
capture noisily break {
Display_table "`cilog'" `"`level'"' `"`options'"'
}
if `modified' {
Display_ereturn local depvar `"`depvar'"'
}
if _rc exit _rc
}
}
capt confirm matrix e(jbtest)
if _rc==0 JBtest_display
end
prog Display_ereturn, eclass
ereturn `0'
end
program Display_table
args cilog level options
if c(stata_version)>=15 {
if "`cilog'"!="" {
qui _coef_table, `level'
tempname CI
mata: robstat_cilog()
_coef_table, nopvalue cimat(`CI') `level' `options'
mat drop `CI'
di as txt `"(`cilognote')"'
}
else {
eret di, nopvalue `level' `options'
}
}
else if c(stata_version)>=14 {
eret di, nopvalue `level' `options'
}
else if c(stata_version)>=13 {
quietly update
if r(inst_ado)>=d(26jun2014) {
eret di, nopvalue `level' `options'
}
else {
_coef_table, cionly `level' `options'
}
}
else if c(stata_version) >= 12 {
_coef_table, cionly `level' `options'
}
else {
eret di, `level' `options'
}
end
program JBtest, eclass
syntax [, jbtest2(str) WALD display ]
JBtest_args `jbtest2'
local vce "`e(vce)'"
local wtype "`e(vce)'"
if "`vce'"=="" {
if "`wtype'"!="" & "`wtype'"!="fweight" local wald wald
}
else if "`vce'"!="analytic" local wald wald
local ntests: list sizeof jbtests
local eqs: coleq e(b)
local eqs: list uniq eqs
if "`wald'"!="" {
if e(df_r)<. {
local jbtype F
local jbtitle "Normality Tests (Wald F; df_r = `e(df_r)')"
}
else {
local jbtype chi2
local jbtitle "Normality Tests (Wald chi2)"
}
tempname jbtest tmp
foreach eq of local eqs {
local rown
matrix `tmp' = J(`ntests', 3,.)
local m 0
foreach t of local jbtests {
local ++m
if "`t'"=="jbera" {
local rown `rown' JB
local exp (_b[`eq':skewness]=0) (_b[`eq':kurtosis]=3)
}
else if "`t'"=="moors" {
local rown `rown' MOORS
local exp (_b[`eq':SK25]=0) (_b[`eq':QW25]=1.23)
}
else if "`t'"=="mc" {
local rown `rown' MC
local exp (_b[`eq':MC]=0)
}
else if "`t'"=="lmc" {
local rown `rown' LMC
local exp (_b[`eq':LMC]=0.199)
}
else if "`t'"=="rmc" {
local rown `rown' RMC
local exp (_b[`eq':RMC]=0.199)
}
else if "`t'"=="lr" {
local rown `rown' LR
local exp (_b[`eq':LMC]=0.199) (_b[`eq':RMC]=0.199)
}
else if "`t'"=="mcl" {
local rown `rown' MC-L
local exp (_b[`eq':MC]=0) (_b[`eq':LMC]=0.199)
}
else if "`t'"=="mcr" {
local rown `rown' MC-R
local exp (_b[`eq':MC]=0) (_b[`eq':RMC]=0.199)
}
else if "`t'"=="mclr" {
local rown `rown' MC-LR
local exp (_b[`eq':MC]=0) (_b[`eq':LMC]=0.199) (_b[`eq':RMC]=0.199)
}
qui test `exp'
matrix `tmp'[`m',1] = r(`jbtype'), r(df), r(p)
}
mat rown `tmp' = `rown'
mat roweq `tmp' = `eq'
mat `jbtest' = nullmat(`jbtest') \ `tmp'
}
mat coln `jbtest' = `jbtype' df "Prob>`jbtype'"
}
else {
local nover = e(N_over)
local jbtype chi2
local jbtitle "Normality Tests"
tempname jbtest tmp D V
local k 0
foreach eq of local eqs {
local ++k
local N = el(e(_N), mod(`k'-1,`nover')+1, 1)
local rown
matrix `tmp' = J(`ntests', 3,.)
local m 0
foreach t of local jbtests {
local ++m
if "`t'"=="jbera" {
local rown `rown' JB
local df 2
matrix `D' = (_b[`eq':skewness], _b[`eq':kurtosis]-3)'
matrix `V' = (6, 0) \ (0, 24)
}
else if "`t'"=="moors" {
local rown `rown' MOORS
local df 2
matrix `D' = (_b[`eq':SK25], _b[`eq':QW25]-1.23)'
matrix `V' = (1.84, 0) \ (0, 3.14)
}
else if "`t'"=="mc" {
local rown `rown' MC
local df 1
matrix `D' = (_b[`eq':MC])
matrix `V' = (1.25)
}
else if "`t'"=="lmc" {
local rown `rown' LMC
local df 1
matrix `D' = (_b[`eq':LMC]-0.199)
matrix `V' = (2.62)
}
else if "`t'"=="rmc" {
local rown `rown' RMC
local df 1
matrix `D' = (_b[`eq':RMC]-0.199)
matrix `V' = (2.62)
}
else if "`t'"=="lr" {
local rown `rown' LR
local df 2
matrix `D' = (_b[`eq':LMC]-0.199, _b[`eq':RMC]-0.199)'
matrix `V' = (2.62, -.0123) \ (-.0123, 2.62)
}
else if "`t'"=="mcl" {
local rown `rown' MC-L
local df 2
matrix `D' = (_b[`eq':MC], _b[`eq':LMC]-0.199)'
matrix `V' = (1.25, .323) \ (.323, 2.62)
}
else if "`t'"=="mcr" {
local rown `rown' MC-R
local df 2
matrix `D' = (_b[`eq':MC], _b[`eq':RMC]-0.199)'
matrix `V' = (1.25, .323) \ (.323, 2.62)
}
else if "`t'"=="mclr" {
local rown `rown' MC-LR
local df 3
matrix `D' = (_b[`eq':MC], _b[`eq':LMC]-0.199, _b[`eq':RMC]-0.199)'
matrix `V' = (1.25, .323, -.323) \ ///
(.323, 2.62, -.0123) \ ///
(-.323, -.0123, 2.62)
}
matrix `tmp'[`m',1] = `N'*`D''*invsym(`V')*`D', `df'
matrix `tmp'[`m',3] = chi2tail(`df', `tmp'[`m',1])
}
mat rown `tmp' = `rown'
mat roweq `tmp' = `eq'
mat `jbtest' = nullmat(`jbtest') \ `tmp'
}
mat coln `jbtest' = `jbtype' df "Prob>`jbtype'"
}
ereturn matrix jbtest = `jbtest'
ereturn local jbwald `wald'
ereturn local jbtype `jbtype'
ereturn local jbtitle "`jbtitle'"
if "`display'"!="" JBtest_display
end
program JBtest_args
if strtrim(`"`0'"')=="all" {
local 0 jbera moors mclr mcl mcr mc lr lmc rmc
}
local jbtests
foreach s of local 0 {
local t = lower(`"`s'"')
local l = strlen(`"`t'"')
// jbera
if "`t'"==substr("jbera", 1, max(2,`l')) {
local jbtests `jbtests' jbera
local s1 skewness kurtosis
}
else if "`t'"==substr("moors", 1, max(2,`l')) {
local jbtests `jbtests' moors
local s2 sk25 qw25
}
else if "`t'"=="mc" {
local jbtests `jbtests' mc
local s3 mc
}
else if "`t'"==substr("lmc", 1, max(1,`l')) {
local jbtests `jbtests' lmc
local s4 lmc
}
else if "`t'"==substr("rmc", 1, max(1,`l')) {
local jbtests `jbtests' rmc
local s5 rmc
}
else if "`t'"=="lr" {
local jbtests `jbtests' lr
local s4 lmc
local s5 rmc
}
else if inlist("`t'", "mcl", "mc-l") {
local jbtests `jbtests' mcl
local s3 mc
local s4 lmc
}
else if inlist("`t'", "mcr", "mc-r") {
local jbtests `jbtests' mcr
local s3 mc
local s5 rmc
}
else if inlist("`t'", "mclr", "mc-lr") {
local jbtests `jbtests' mclr
local s3 mc
local s4 lmc
local s5 rmc
}
else {
di as err `"`s' not allowed in jbtest()"'
exit 198
}
}
if "`jbtests'"=="" {
local jbtests jbera moors mclr
local statistics skewness kurtosis sk25 qw25 mc lmc rmc
}
else {
local statistics `s1' `s2' `s3' `s4' `s5'
}
c_local statistics `statistics'
c_local jbtests `jbtests'
end
program JBtest_display
local rspec
local k_eq = e(k_eq)
local njb = rowsof(e(jbtest)) / `k_eq' - 1
forv i = 1/`k_eq' {
forv j = 1/`njb' {
local rspec `rspec'&
}
local rspec `rspec'-
}
matlist e(jbtest), rspec(--`rspec') cspec(&%12s|%10.2f&%5.0g&%10.4f&) ///
title(`e(jbtitle)')
end
program Estimate, eclass
// syntax
syntax varlist(numeric) [if] [in] [pw iw fw/], [ ///
Statistics(str asis) swap ///
over(varname numeric) Total ///
vce(str) CLuster(varname) svy SVY2(str) nose ///
GENerate GENerate2(name) replace ///
/// normality test (just for parsing)
JBtest JBtest2(str) WALD ///
/// optimization options for M-estimation
TOLerance(real 1e-10) ITERate(integer `c(maxiter)') ///
/// kernel density estimation options
Kernel(name) bw(name) Adaptive(int 2) n(int 512) ///
/// display options
Level(cilevel) noHEader noTABle CILog * ///
]
if "`cilog'"!="" {
if c(stata_version)<15 {
di as err "option cilog only allowed in Stata 15 or newer"
exit 198
}
}
if `"`generate2'"'!="" local generate generate
else if "`generate'"!="" local generate2 _IF_
if `"`jbtest'`jbtest2'"'!="" {
JBtest_args `jbtest2' // sets local statistics
}
if `tolerance'<=0 {
di as err "tolerance() must be positive"
exit 198
}
if `iterate'<=0 {
di as err "iterate() must be positive"
exit 198
}
local estopts tolerance(`tolerance') iterate(`iterate')
if "`kernel'"=="" local kernel epan2
if "`bw'"=="" local bw dpi
if `adaptive'<0 {
di as err "adaptive() must be positive"
exit 198
}
if `n'<=2 {
di as err "n() must be > 2"
exit 198
}
local estopts `estopts' kernel(`kernel') bw(`bw') adaptive(`adaptive') n(`n')
// varlist
local varlist: list uniq varlist // remove repeated varnames
local ndepv: list sizeof varlist
// statistics
Parse_stats `statistics' // returns stats, statslbl
local nstats: list sizeof stats
// over(), total
if "`over'"=="" {
if "`total'"!="" {
di as err "total only allowed if over() is specified"
exit 198
}
}
// vce
local vce0 `"`vce'"'
if `"`cluster'"'!="" {
if "`se'"!="" {
di as err "cluster() and nose not both allowed"
exit 198
}
if `"`svy'`svy2'"'!="" {
di as err "cluster() and svy() not both allowed"
exit 198
}
if `"`vce'"'!="" {
di as err "cluster() and vce() not both allowed"
exit 198
}
local vce `"cluster `cluster'"'
local cluster
}
if `"`vce'"'!="" {
if "`se'"!="" {
di as err "vce() and nose not both allowed"
exit 198
}
if `"`svy'`svy2'"'!="" {
di as err "vce() and svy() not both allowed"
exit 198
}
gettoken vce clustvar : vce
if `"`vce'"'=="analytic" & `"`clustvar'"'=="" {
local vce analytic
}
else if `:list sizeof clustvar'==1 & ///
substr("cluster", 1, max(2, strlen(`"`vce'"')))==`"`vce'"' {
local vce cluster
local vcetype Robust
gettoken clustvar : clustvar
local clustopt cluster(`clustvar')
}
else {
di as err "invalid vce()"
exit 198
}
}
else if "`se'"=="" {
local vce analytic
}
// svy
if `"`svy'`svy2'"'!="" {
if "`weight'"!="" {
di as err "weights not allowed with svy; supply weights to {help svyset}"
exit 101
}
if "`se'"!="" {
di as err "svy() and nose not both allowed"
exit 198
}
local svy svy
if `"`svy2'"'!="" {
local svy2 `"subpop(`svy2')"'
}
}
// display options
local levelopt level(`level')
_get_diopts diopts, `options'
c_local diopts `header' `table' `levelopt' `cilog' `diopts'
// sample and weights
marksample touse
markout `touse' `clustvar' `over'
if "`svy'"!="" {
if c(stata_version)>=14 {
tempvar subpop exp
_svy_setup `touse' `subpop' `exp', svy `svy2'
local weight `"`r(wtype)'"'
if "`weight'"=="" {
drop `exp'
local exp
}
}
else {
tempvar subpop
_svy_setup `touse' `subpop', svy `svy2'
local weight `"`r(wtype)'"'
local exp `"`r(wvar)'"'
}
if `"`r(vce)'"'!="linearized" {
di as err "option svy is only allowed if VCE is set to linearized; " ///
`"use the {helpb svy} prefix command for `r(vce)' survey estimation"'
exit 498
}
local svy_posts `"`r(poststrata)'"'
local svy_postw `"`r(postweight)'"'
if `"`svy_posts'"'!="" {
if "`weight'"!="" local wexp `"[`weight' = `exp']"'
tempvar exp
svygen post double `exp' `wexp' if `touse', ///
posts(`svy_posts') postw(`svy_postw')
if "`weight'"=="" local weight pweight
}
}
else local subpop `touse'
if "`over'"!="" {
capt assert ((`over'==floor(`over')) & (`over'>=0)) if `subpop'
if _rc {
di as err "variable in over() must be integer and nonnegative"
exit 452
}
qui levelsof `over' if `subpop', local(overvals)
local N_over: list sizeof overvals
if "`total'"!="" {
local ++N_over
}
local over_labels
foreach overval of local overvals {
local over_labels `"`over_labels' `"`: label (`over') `overval''"'"'
}
local over_labels: list clean over_labels
}
else {
local total total
local N_over 1
}
if "`weight'"!="" {
capt confirm variable `exp'
if _rc {
local wexp `"= `exp'"'
tempvar exp
qui gen double `exp' `wexp'
}
local wgt `"[`weight' = `exp']"'
if "`weight'"=="pweight" {
local swgt `"[aw = `exp']"'
}
else {
local swgt `"`wgt'"'
}
}
else {
local wgt
local swgt
}
if "`svy'"!="" & `"`exp'"'!="" {
su `touse' if `touse', meanonly
}
else {
su `touse' `swgt' if `touse', meanonly
}
local N = r(N)
if `N'==0 error 2000
// check whether variance estimation is supported
if "`se'"=="" {
if "`weight'"=="fweight" {
if "`vce'"=="analytic" & `"`vce0'"'=="" {
di as txt "(variance estimation not supported with fweights)"
local se nose
}
else if "`svy'`vce'"!="" {
di as err "variance estimation not supported with fweights"
exit 498
}
}
}
// generate: check whether variables already exist
if "`se'"=="" & "`generate'"!="" & "`replace'"=="" {
foreach v of local varlist {
foreach overval in `overvals' `total' {
foreach stat of local stats {
Make_IF_Name `generate2' `stat' `v' `ndepv' "`over'" ///
`overval' `stat' `nstats' // returns vname
confirm new variable `vname'
}
}
}
}
// compute statistics
tempname b btmp class cltmp aux auxtmp _N
local uvars
if `nstats'>1 {
local k_eq = `ndepv' * `N_over'
local k_coef = `nstats'
}
else {
if "`over'"!="" {
local k_eq = `ndepv'
local k_coef = `N_over'
}
else {
local k_eq = 1
local k_coef = `ndepv'
}
}
if "`over'"!="" {
mat `_N' = J(`N_over', 1, .)
}
else {
mat `_N' = J(1, 1, .)
}
mat rown `_N' = `overvals' `total'
mat coln `_N' = "N"
foreach v of local varlist {
local k 0
foreach overval in `overvals' `total' {
local ++k
// select obs
if `"`overval'"'=="total" local touse1 (1)
else local touse1 (`over'==`overval')
// equation label
if `k_eq'>1 {
if `ndepv'==1 local eq "`overval'"
else if "`over'"=="" local eq "`v'"
else if `nstats'==1 local eq "`v'"
else local eq "`v'_`overval'"
}
else if "`over'"!="" & `nstats'>1 local eq "`overval'"
else local eq
// count obs
if "`svy'"!="" & `"`exp'"'!="" {
su `touse' if `touse' & `subpop' & `touse1', meanonly
}
else {
su `touse' `swgt' if `touse' & `subpop' & `touse1', meanonly
}
mat `_N'[`k', 1] = r(N)
// compute stats
mat `btmp' = J(1, `nstats', .)
mat coln `btmp' = `statslbl'
mat `cltmp' = `btmp'
if `nstats'==1 {
if "`over'"!="" {
mat coln `btmp' = `overval'
}
else if `ndepv'>1 {
mat coln `btmp' = `v'
}
}
mat coleq `btmp' = `eq'
mat `auxtmp' = `btmp'
local j 0
foreach stat of local stats {
local u
if "`se'"=="" {
tempvar u
qui gen double `u' = 0 if `touse'
local uvars `uvars' `u'
}
local ++j
Estimate_`stat' `wgt' if `touse' & `subpop' & `touse1', ///
v(`v') u(`u') `estopts'
mat `btmp'[1, `j'] = r(b)
mat `cltmp'[1, `j'] = r(class)
mat `auxtmp'[1, `j'] = r(k)
}
mat `b' = nullmat(`b'), `btmp'
mat `class' = nullmat(`class'), `cltmp'
mat `aux' = nullmat(`aux'), `auxtmp'
}
}
// compute standard errors
if "`se'"=="" {
tempname V
if "`svy'"!="" {
//qui svy, `svy2': total `uvars' if `touse'
qui svy, `svy2': mean `uvars' if `touse'
}
else {
//qui total `uvars' `wgt' if `touse', `clustopt'
qui mean `uvars' `wgt' if `touse', `clustopt'
}
matrix `V' = e(V)
local rank = e(rank)
local df_r = e(df_r)
if "`vce'"=="cluster" {
local N_clust = e(N_clust)
}
if "`svy'"!="" {
local svy_scalars
foreach l in N_sub N_strata N_strata_omit singleton census N_pop ///
N_subpop N_psu N_poststrata stages {
if e(`l')<. {
local svy_`l' = e(`l')
local svy_scalars `svy_scalars' `l'
}
}
local svy_macros
foreach l in prefix wtype wvar wexp singleunit strata psu fpc ///
poststrata postweight vce vcetype mse subpop adjust {
local svy_`l' `"`e(`l')'"'
local svy_macros `svy_macros' `l'
}
forv l=1/`svy_stages' {
local svy_su`l' `"`e(su`l')'"'
local svy_fpc`l' `"`e(fpc`l')'"'
local svy_weight`l' `"`e(weight`l')'"'
local svy_strata`l' `"`e(strata`l')'"'
local svy_macros `svy_macros' su`l' fpc`l' weight`l' strata`l'
}
local svy_matrices
foreach l in V_srs V_srssub V_srswr V_srssubwr _N_strata_single ///
_N_strata_certain _N_strata _N_postsum _N_postsize {
capt confirm matrix e(`l')
if _rc==0 {
tempname svy_`l'
mat `svy_`l'' = e(`l')
local svy_matrices `svy_matrices' `l'
}
}
}
}
// post results
if "`se'"=="" {
local coln: colfullnames `b'
mat coln `V' = `coln'
mat rown `V' = `coln'
}
else if "`weight'"=="iweight" { // add empty e(V) for svy
tempname V
mat `V' = `b'' * `b' * 0
}
if "`swap'"!="" { // flip equations and coefficients
mata: _robstat_swap_eq_and_coefs(`k_eq', `k_coef')
}
eret post `b' `V' `wgt', obs(`N') esample(`touse')
eret local cmd "robstat"
eret local depvar "`varlist'"
if "`svy'"!="" eret local title "Survey: Robust Statistics"
else eret local title "Robust Statistics"
eret scalar k_eq = `k_eq'
eret scalar N_stats = `nstats'
eret local statistics "`statslbl'"
eret scalar N_vars = `ndepv'
if "`over'"!="" {
eret local total "`total'"
eret local over_labels `"`over_labels'"'
eret local over_namelist `"`overvals'"'
eret local over "`over'"
}
eret scalar N_over = `N_over'
eret mat _N = `_N'
eret mat class = `class'
eret mat aux = `aux'
if "`se'"=="" {
eret local vcetype "`vcetype'"
eret local vce "`vce'"
eret scalar rank = `rank'
eret scalar df_r = `df_r'
eret scalar level = `level'
if "`vce'"=="cluster" {
eret local clustvar "`clustvar'"
eret scalar N_clust = `N_clust'
}
if "`svy'"!="" {
foreach l of local svy_scalars {
eret scalar `l' = `svy_`l''
}
foreach l of local svy_macros {
eret local `l' `"`svy_`l''"'
}
foreach l of local svy_matrices {
if substr("`l'", 1, 1)=="V" {
mat coln `svy_`l'' = `coln'
mat rown `svy_`l'' = `coln'
}
eret matrix `l' = `svy_`l''
}
}
}
// generate: rename uvars
if "`se'"=="" & "`generate'"!="" {
local IFvars
local i 0
foreach v of local varlist {
foreach overval in `overvals' `total' {
foreach stat of local stats {
Make_IF_Name `generate2' `stat' `v' `ndepv' "`over'" ///
`overval' `stat' `nstats' // returns vname
capt confirm new variable `vname'
if _rc drop `vname'
local ++i
local uvar: word `i' of `uvars'
rename `uvar' `vname'
local IFvars `IFvars' `vname'
}
}
}
eret local IFvars `IFvars'
}
Return_clear // for some reason -return clear- does not delete r(S)
end
program Return_clear, rclass
local x
end
program Make_IF_Name
args vname stat v ndepv over overval stat nstats
local stat: subinstr local stat ":" ""
local uscore
if `ndepv'>1 {
local vname `vname'`v'
local uscore _
}
if "`over'"!="" {
local vname `vname'`uscore'`overval'
local uscore _
}
if `nstats'>1 | "`uscore'"=="" {
local vname `vname'`uscore'`stat'
}
c_local vname `vname'
end
program Parse_stats
local stats
local statslbl
foreach s of local 0 {
local ok = regexm(lower(`"`s'"'), "^([a-z]+)([0-9]*)$")
if `ok' {
local s1 = regexs(1)
local s2 = regexs(2)
}
else {
di as err `"`s' not allowed in statistics()"'
exit 198
}
local l = strlen(`"`s1'"')
// mean
if "`s1'"==substr("mean", 1, max(1,`l')) & `"`s2'"'=="" {
local stats `stats' mean
local statslbl `statslbl' mean
continue
}
// alpha-trimmed mean
if "`s1'"==substr("alpha", 1, max(1,`l')) {
if "`s2'"=="" local s2 5 // default is alpha5
Parse_stats_confirm_num 1 49 `s2' `s' // range is [1,49]
local stats `stats' alpha:`s2'
local statslbl `statslbl' alpha`s2'
continue
}
// median
if "`s1'"==substr("median", 1, max(3,`l')) & `"`s2'"'=="" {
local stats `stats' median
local statslbl `statslbl' median
continue
}
// Hodges-Lehmann
if "`s1'"=="hl" & `"`s2'"'=="" {
local stats `stats' hl
local statslbl `statslbl' HL
continue
}
if "`s1'"=="hlnaive" & `"`s2'"'=="" {
local stats `stats' hlnaive
local statslbl `statslbl' HL(naive)
continue
}
// Huber M
if "`s1'"==substr("huber", 1, max(1,`l')) {
if "`s2'"=="" local s2 95 // default is huber95
Parse_stats_confirm_num 64 99 `s2' `s' // range is [64,99]
local stats `stats' huber:`s2'
local statslbl `statslbl' Huber`s2'
continue
}
// Biweight M
if "`s1'"==substr("biweight", 1, max(2,`l')) {
if "`s2'"=="" local s2 95 // default is biweight95
Parse_stats_confirm_num 1 99 `s2' `s' // range is [1,99]
local stats `stats' biweight:`s2'
local statslbl `statslbl' Biweight`s2'
continue
}
// standard deviation
if "`s1'"=="sd" & `"`s2'"'=="" {
local stats `stats' sd
local statslbl `statslbl' SD
continue
}
// IQR
if "`s1'"=="iqr" & `"`s2'"'=="" {
local stats `stats' iqr
local statslbl `statslbl' IQR
continue
}
// IQRc
if "`s1'"=="iqrc" & `"`s2'"'=="" {
local stats `stats' iqrc
local statslbl `statslbl' IQRc
continue
}
// MAD
if "`s1'"=="mad" & `"`s2'"'=="" {
local stats `stats' mad
local statslbl `statslbl' MAD
continue
}
// MADN
if "`s1'"=="madn" & `"`s2'"'=="" {
local stats `stats' madn
local statslbl `statslbl' MADN
continue
}
// Qn coefficient
if "`s1'"==substr("qn", 1, max(1,`l')) & `"`s2'"'=="" {
local stats `stats' qn
local statslbl `statslbl' Qn
continue
}
if "`s1'"=="qnnaive" & `"`s2'"'=="" {
local stats `stats' qnnaive
local statslbl `statslbl' Qn(naive)
continue
}
// M estimate of scale
if "`s1'"=="s" {
if "`s2'"=="" local s2 50 // default is s50
Parse_stats_confirm_num 1 50 `s2' `s' // range is [1,50]
local stats `stats' s:`s2'
local statslbl `statslbl' S`s2'