-
Notifications
You must be signed in to change notification settings - Fork 1
/
lnmor.ado
1940 lines (1838 loc) · 57.4 KB
/
lnmor.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.1.5 16jan2023 Ben Jann
program lnmor, properties(or)
version 15
if replay() {
if "`e(cmd)'"!="lnmor" {
if `"`e(cmd)'"'=="mi estimate" & `"`e(cmd_mi)'"'=="lnmor" {
mi estimate `0'
exit
}
error 301
}
Replay `0'
exit
}
local version : di "version " string(_caller()) ":"
_parse_opts `0' // returns 00, diopts, post, prefix, cmdline; may update 0
// application with prefix command
if "`prefix'"!="" {
tempname ecurrent
_estimates hold `ecurrent', restore
if "`prefix'"=="mi" {
// mi: using display routine of mi estimate
`version' `00'
}
else {
// svy/bootstrap/jackknife: using own display routine
if "`prefix'"=="svyr" {
nobreak {
capt noisily break {
`version' `00'
}
if _rc {
local rc = _rc
capt mata mata drop _LNMOR_TMP_IFs
exit `rc'
}
}
_ereturn_svy
}
else if "`prefix'"=="svyb" {
`version' `00'
_ereturn_svy
}
else { // bootstrap/jackknife
`version' `00'
}
Replay, `diopts'
Describe_newvars `e(ifgenerate)'
}
_ereturn_cmdline `0'
_ereturn_est_cmdline `cmdline'
_estimates unhold `ecurrent', not
exit
}
// application without prefix command
Estimate `00'
_return_cmdline `0'
_return_est_cmdline `cmdline'
if "`post'"!="" {
_postrtoe, all
Replay, `diopts'
Describe_newvars `e(ifgenerate)'
exit
}
Replay_from_r, `diopts'
Describe_newvars `r(ifgenerate)'
end
program _ereturn_cmdline, eclass
eret local cmdline `"lnmor `0'"'
end
program _ereturn_est_cmdline, eclass
eret local est_cmdline `"`0'"'
end
program _return_cmdline, rclass
return add
return local cmdline `"lnmor `0'"'
end
program _return_est_cmdline, rclass
return add
return local est_cmdline `"`0'"'
end
program _parse_opts
// check for _lnmor() and run estimation command if needed
_parse comma lhs 0 : 0
syntax [, _lnmor(str asis) _prefix(str asis) * ]
local has_lnmor = `"`_lnmor'"'!=""
if `has_lnmor' {
// run estimation command
local estcmd `lhs'
if `"`options'"'!="" local estcmd `estcmd', `options'
if `"`_prefix'"'!="" local estcmd `_prefix': `estcmd'
`estcmd'
// update syntax
local 0 `_lnmor'
c_local 0 `0' // update returned cmdline
_parse comma lhs 0 : 0
}
// parse options
syntax [, vce(passthru) post or noHEADer noTABle NOSE /*
*/ IFGENerate(passthru) RIFgenerate(passthru) * ]
_get_diopts diopts options, `options'
local diopts `or' `header' `table' `diopts'
// multiple imputation
if `"`e(mi)'"'=="mi" {
_parse_miopts, `options'
local miopts cmdok `miopts' `post' `diopts'
if `"`e(cmd)'"'=="mi estimate" local cmd_mi cmd_mi
else local cmd_mi cmd
if `"`e(`cmd_mi')'"'=="lnmor" {
// results in memory are from lnmor
local cmdline `"`e(est_cmdline)'"'
}
else {
_check_source_model `e(`cmd_mi')'
local cmdline `"`e(cmdline_mi)'"'
}
c_local cmdline `"`cmdline'"'
if `"`ifgenerate'`rifgenerate'"'!="" {
di as err "{bf:ifgenerate()} not allowed after {bf:mi estimate}"
exit 198
}
if `"`nose'"'!="" {
di as err "{bf:nose} not allowed after {bf:mi estimate}"
exit 198
}
_parse_opts_vce, `vce' // returns prefix if bootstrap or jackknife
if "`prefix'"!="" {
di as err "{bf:vce(`prefix')} not allowed after {bf:mi estimate}"
exit 198
}
_on_colon_parse `cmdline' // remove -mi estimate-
if `"`e(prefix)'"'!="" {
// move prefix (e.g. svy) into option
_on_colon_parse `s(after)'
local _prefix _prefix(`s(before)')
}
_parse_estcmd 0 `s(after)' // returns estcmd
c_local 00 mi estimate, `miopts':/*
*/ lnmor `estcmd' _lnmor(`lhs', post `options') `_prefix'
c_local prefix "mi"
exit
}
// confirm that e() contains valid model and collect command line
if !`has_lnmor' & `"`e(cmd)'"'=="lnmor" {
// results in memory are from lnmor
local cmdline `"`e(est_cmdline)'"'
}
else {
_check_source_model `e(cmd)'
local cmdline `"`e(cmdline)'"'
}
c_local cmdline `"`cmdline'"'
// svy
if `"`e(prefix)'"'=="svy" {
if `"`vce'"'!="" {
di as err "{bf:vce()} not allowed after {bf:svy}"
exit 198
}
local vcetype `"`e(vce)'"'
if `"`vcetype'"'=="linearized" local svytype "svyr"
else local svytype "svyb"
_on_colon_parse `cmdline'
_parse_svy `s(before)' // returns svy
_parse_estcmd 0 `s(after)' // returns estcmd
if "`svytype'"=="svyr" {
// linearized VCE
if `"`nose'"'!="" {
di as err "{bf:nose} not allowed after {bf:svy `vcetype'}"
exit 198
}
c_local 00 `svy' noheader notable:/*
*/ _lnmor_`svytype' estimate `estcmd'/*
*/ _lnmor(`lhs', post saveifuninmata/*
*/ `ifgenerate' `rifgenerate' `options')
}
else {
// replication-based VCE
if `"`ifgenerate'`rifgenerate'"'!="" {
di as err `"{bf:ifgenerate()} not allowed after {bf:svy `vcetype'}"'
exit 198
}
c_local 00 `svy' noheader notable:/*
*/ _lnmor_`svytype' `estcmd'/*
*/ _lnmor(`lhs', post nose `options')
}
c_local diopts `diopts'
c_local prefix "`svytype'"
exit
}
// check for vce(bootstrap) or vce(jackknife)
_parse_opts_vce, `vce' // returns prefix vcelevel
if "`prefix'"=="" {
if `"`e(cmd)'"'=="lnmor" {
// results in memory are from lnmor; must refit the original model
di as txt `"(refitting {bf:`e(est_cmd)'} model)"'
quietly `cmdline'
}
c_local 00 `lhs', `vce' `nose' `ifgenerate' `rifgenerate' `options'
c_local diopts `diopts'
c_local post `post'
c_local prefix ""
exit
}
// bootstrap / jackknife
if `"`ifgenerate'`rifgenerate'"'!="" {
di as err "{bf:ifgenerate()} not allowed with replication based VCE"
exit 198
}
if `"`e(wtype)'"'=="fweight" {
di as err "{bf:vce(`prefix')} not allowed with {bf:fweight}s"
exit 198
}
_parse_diopts_level, `diopts' // returns level diopts
if `"`vcelevel'"'!="" local level `vcelevel' // vcelevel takes precedence
if "`prefix'"=="jackknife" local ropts jkopts
else local ropts bootopts
_parse_estcmd 1 `cmdline' // returns estcmd
c_local 00 _vce_parserun lnmor, noeqlist wtypes(pw iw)/*
*/ `ropts'(noheader notable force):/*
*/ `estcmd' `vce' `level' _lnmor(`lhs', post nose `options')
c_local diopts `level' `diopts'
c_local prefix "`prefix'"
end
program _parse_svy
_parse comma lhs 0 : 0
syntax [, * ]
c_local svy `lhs', `options'
end
program _ereturn_svy, eclass
eret local cmdname ""
eret local command ""
eret local predict ""
end
program _parse_miopts
syntax [, MIopts(str) * ]
c_local miopts `miopts'
c_local options `options'
end
program _parse_estcmd // remove or; possibly remove vce, cluster, robust, level
gettoken remove 0 : 0
if `remove' local rmopts or vce(passthru) CLuster(passthru) Robust Level(passthru)
else local rmopts or
_parse comma lhs 0 : 0
syntax [, `rmopts' * ]
c_local estcmd `lhs', `options'
end
program _parse_opts_vce
syntax [, vce(str) ]
_parse comma v 0 : vce
syntax [, Level(passthru) * ]
local l = strlen(`"`v'"')
if `"`v'"'==substr("bootstrap",1,max(4,`l')) local prefix bootstrap
else if `"`v'"'==substr("jackknife",1,max(4,`l')) local prefix jackknife
c_local prefix `prefix'
c_local vcelevel `level'
end
program _parse_diopts_level
syntax [, Level(passthru) * ]
c_local level `level'
c_local diopts `options'
end
program _check_source_model
if !inlist(`"`0'"',"logit","probit") {
di as err "last estimates not found; " /*
*/ "{bf:lnmor} only allowed after {bf:logit} or {bf:probit}"
exit 301
}
end
program _postrtoe, eclass
syntax [, all ]
// b V depvar N wtype wexp
if "`all'"!="" {
tempname touse
qui gen byte `touse' = e(sample)
}
tempname b
matrix `b' = r(b)
capt confirm matrix r(V)
if _rc==1 exit _rc
if _rc==0 {
tempname V
matrix `V' = r(V)
}
eret post `b' `V' [`r(wtype)'`r(wexp)'], depname(`r(depvar)') /*
*/ obs(`r(N)') esample(`touse')
// scalars
local rscalars: r(scalars)
local drop N
local rscalars: list rscalars - drop
foreach r of local rscalars {
eret scalar `r' = r(`r')
}
// macros
local rmacros: r(macros)
local drop depvar wtype wexp
local rmacros: list rmacros - drop
foreach r of local rmacros {
eret local `r' `"`r(`r')'"'
}
// matrices
local rmatrices: r(matrices)
local drop b V
local rmatrices: list rmatrices - drop
foreach r of local rmatrices {
matrix `b' = r(`r')
eret matrix `r' = `b'
}
end
program Replay, rclass
syntax [, or noHEADer noTABle eform(passthru) * ]
if "`or'"!="" & `"`eform'"'=="" local eform eform(Odds Ratio)
if "`header'"=="" {
if `"`eform'"'!="" local title "Marginal odds ratio"
else local title "Marginal log odds ratio"
local hflex 1
if c(stata_version)<17 local hflex 0
else if d(`c(born_date)')<d(13jul2021) local hflex 0
local w1 17
local c1 49
local c2 = `c1' + `w1' + 1
local w2 10
local c3 = `c2' + 2
if `hflex' local headopts head2left(`w1') head2right(`w2')
else local headopts
_coef_table_header, nomodeltest title(`title') `headopts'
if `hflex' {
// if _coef_table_header used more space than allocated
local offset1 = max(0, `s(head2_left)' - `w1')
local offset2 = max(0, `s(head2_right)' - `w2')
local c1 = `c1' - `offset1' - `offset2'
local c2 = `c2' - `offset2'
}
if `"`e(subsample)'"'!="" {
di as txt _col(`c1') "Subsample n. obs" _col(`c2') "=" /*
*/ _col(`c3') as res %`w2'.0gc e(N_subsmp)
}
di as txt _col(`c1') "Command" _col(`c2') "=" /*
*/ _col(`c3') as res %`w2's e(est_cmd)
if `"`e(dxtype)'"'!="" {
di as txt _col(`c1') "Type of dx()" _col(`c2') "=" /*
*/ _col(`c3') as res %`w2's e(dxtype)
}
if `"`e(delta)'"'!="" {
di as txt _col(`c1') "Value of delta()" _col(`c2') "=" /*
*/ _col(`c3') as res %`w2'.0g e(delta)
di as txt _col(`c1') "Centered" _col(`c2') "=" _col(`c3') /*
*/ as res %`w2's cond(`"`e(centered)'"'!="","yes","no")
di as txt _col(`c1') "Normalized" _col(`c2') "=" _col(`c3') /*
*/ as res %`w2's cond(`"`e(normalize)'"'!="","yes","no")
}
if `"`e(atvars)'"'!="" {
di as txt "Evaluated at:"
local atvars `"`e(atvars)'"'
local K = e(k_eq)
forv i = 1/`K' {
di as res %5s "`i'" as txt ": " _c
local j 0
foreach v of local atvars {
if `j' di as txt ", " _c
local ++j
di as txt "`v' = " as res el(e(at), `i',`j') _c
}
di ""
}
}
di ""
}
if "`table'"=="" {
eret di, `eform' `options' /* note: eform does not seem to work if e(V)
is missing */
return add
local dxtype `"`e(dxtype)'"'
if `"`dxtype'"'=="" exit
di as txt "Terms affected by dx():" _c
forv i = 1/`e(nterms)' {
if e(dx`i')==1 di as res " `e(term`i')'" _c
}
di ""
if `"`dxtype'"'=="levels" {
di as txt "Levels of dx(): " as res `"`e(dxlevels)'"'
}
}
end
program Replay_from_r, rclass
tempname ecurrent
_estimates hold `ecurrent', restore
_postrtoe
return add
Replay `0'
return add
end
program Describe_newvars
if `"`0'"'=="" exit
tempname rcurrent
_return hold `rcurrent'
describe `0'
_return restore `rcurrent'
end
program Estimate, rclass
syntax varlist(numeric fv) [, nowarn CONStant NOTBAL TBAL /*
*/ kmax(numlist int max=1 >1 missingokay) /*
*/ dx DX2(passthru) delta DELTA2(passthru) CENTERed NORMalize /*
*/ EPSilon /* undocumented
*/ at(passthru) atmax(int 50) /*
*/ SUBSAMPle(str) /*
*/ noDOTs NOSE saveifuninmata /*
*/ IFGENerate(str) RIFgenerate(str) IFScaling(str) replace ]
if "`kmax'"=="" local kmax 100
if "`warn'"!="" local warn qui
if "`notbal'"!="" local tbal notbal
// ifgen
if `"`rifgenerate'"'!="" {
if `"`ifgenerate'"'!="" {
di as err "{bf:ifgenerate()} and {bf:rifgenerate()} not both allowed"
exit 198
}
local ifgenerate `"`rifgenerate'"'
local iftype "RIF"
}
else if `"`ifgenerate'"'!="" local iftype "IF"
capt _parse_ifscaling, `ifscaling'
if _rc==1 exit _rc
if _rc {
di as err "{bf:ifscaling()}: invalid specification"
exit 198
}
// parse dx() option
if "`epsilon'"!="" {
local delta delta
local delta2 = 2*exp(log(c(epsdouble))/3)
local centered centered
local normalize normalize
}
else if `"`delta2'"'!="" {
local delta delta
_parse_delta, `delta2' // returns delta2
}
if "`delta'"!="" {
local dx dx
if `"`delta2'"'=="" local delta2 1
}
else {
local centered
local normalize
}
if `"`dx2'"'!="" local dx dx
if "`dx'"!="" {
_parse_dx, `dx2' // returns dxtype dxlevels
}
if "`delta2'"=="0" {
local npred 1
local centered
local normalize
}
else if "`delta2'"!="" local npred 2
else local npred 1
// collect some info on original model
local est_N = e(N)
local est_cmd `"`e(cmd)'"'
local clustvar `"`e(clustvar)'"'
if `"`clustvar'"'!="" {
local N_clust = e(N_clust)
local vceopt vce(cluster `clustvar')
}
_collect_model_info // returns mvars mnames mfv mcons mk
// preserve model and select sample
tempname ecurrent
_estimates hold `ecurrent', restore copy
preserve
qui keep if e(sample)
if `"`subsample'"'!="" {
tempvar subuse
_set_subsamp `subuse' `subsample'
}
else local subuse
// weights
tempname sum_w0 sum_w
local weight `"`e(wtype)'"'
local exp `"`e(wexp)'"'
if "`weight'"!="" {
local wvar = substr(`"`exp'"', 3, .) // strip "= "
capt confirm var `wvar'
if _rc==1 exit _rc
if _rc {
tempname wvar
qui gen double `wvar' `exp'
}
local awgt "[aw=`wvar']"
su `wvar', meanonly
local N0 = cond("`weight'"=="fweight", r(sum), r(N))
scalar `sum_w0' = r(sum)
}
else {
qui count
local N0 = r(N)
scalar `sum_w0' = r(N)
}
if `N0'!=`est_N' {
di as error "something is wrong; inconsistent estimation sample"
exit 498
}
if "`subuse'"!="" {
if "`weight'"!="" {
su `wvar' if `subuse', meanonly
local N = cond("`weight'"=="fweight", r(sum), r(N))
scalar `sum_w' = r(sum)
}
else {
qui count if `subuse'
local N = r(N)
scalar `sum_w' = r(N)
}
}
else {
local N `N0'
scalar `sum_w' = `sum_w0'
}
if `N'==0 error 2000
// process varlist
fvexpand `varlist' // (use full sample)
_collect_fvinfo `r(varlist)' // returns names nterms term# type# name# lvls# bn#
local tmp: list dups names
if `"`tmp'"'!="" {
di as error "inconsistent varlist; duplicate variables not allowed"
exit 198
}
local tmp: list names - mnames
if `"`tmp'"'!="" {
gettoken tmp : tmp
di as error "{bf:`tmp'} not found in list of covariates"
exit 111
}
local ndx 0
forv j = 1/`nterms' {
local mfv`j': list name`j' in mfv
if "`type`j''"=="factor" {
if !`mfv`j'' {
`warn' di as txt "Warning: {bf:`name`j''} is a continuous" /*
*/ " variable in the original model"
}
continue
}
if `mfv`j'' {
`warn' di as txt "Warning: {bf:`name`j''} is a factor" /*
*/ " variable in the original model"
continue
}
if "`type`j''"!="variable" continue
if "`dx'"=="" continue
local dxtype`j' "`dxtype'"
local ++ndx
}
if `ndx'>1 {
if "`dxtype'"=="levels" & `"`dxlevels'"'=="" {
di as err "{bf:dx(levels)} not allowed with multiple continuous terms"
exit 198
}
}
// constant
if "`constant'"!="" {
if `nterms'>1 {
di as err "{bf:constant} not allowed with multiple terms"
exit 198
}
if "`dxtype1'"!="" {
di as err "{bf:constant} cannot be combined with {bf:dx()}"
exit 198
}
}
// at
tempname AT
_parse_at `AT', `at' atmax(`atmax') mnames(`mnames') subuse(`subuse')
/* returns AT K at */
local hasat = `"`at'"'!=""
if `hasat' {
local tmp: list at & names
if `"`tmp'"'!="" {
di as error "{it:varlist} and {bf:at()} must be distinct"
exit 198
}
}
// determine treatment levels
forv j = 1/`nterms' {
tempname levels`j'
mata: _get_levels("`j'", "`subuse'")
/* fills in levels#; returns k# cname# */
}
// prepare VCE
if "`saveifuninmata'"!="" local nose
local vce = "`nose'"==""
if `vce' {
// tempvars for outcome IFs
forv k = 1 / `K' {
forv j = 1/`nterms' {
if "`dxtype`j''"=="levels" {
mata: _mktmpnames("IF`j'_`k'", `k`j'')
}
else if "`dxtype`j''"!="" {
tempvar IF`j'_`k'
}
else {
local tmp: list sizeof term`j'
if "`constant'"!="" & !`bn`j'' local ++tmp
mata: _mktmpnames("IF`j'_`k'", `tmp')
}
local IFs `IFs' `IF`j'_`k''
}
}
foreach v of local IFs {
qui gen double `v' = 0
}
// parse ifgenerate()
if `"`ifgenerate'"'!="" {
// expand stub*; confirm (new) varnames
_parse_ifgenerate `"`ifgenerate'"' `:list sizeof IFs' `replace'
}
// obtain model IFs
mata: _mktmpnames("mIFs", `mk')
_get_model_IF `mcons' "`mvars'" "`mIFs'"
}
else local ifgenerate
// compute marginal ORs
if "`dots'"=="" {
di as txt _n "Enumerating predictions: " _c
local lsize = min(78,c(linesize))
local lpos 26
}
if `hasat' tempname B
local k 0
tempname b
tempvar name0
forv k = 1 / `K' {
if `hasat' {
if "`dots'"=="" _progress_info "`k':" `lpos' `lsize'
local j 0
foreach v of local at {
local ++j
qui replace `v' = `AT'[`k', `j']
}
}
forv j = 1/`nterms' {
if "`cname`j''"!="`name`j''" {
rename `name`j'' `name0'
rename `cname`j'' `name`j''
}
if "`dots'"=="" {
_progress_info "`name`j''" `lpos' `lsize'
}
local opts subuse(`subuse') /*
*/ name(`name`j'') levels(`levels`j'') k(`k`j'') /*
*/ wvar(`wvar') sum_w(`sum_w') ifs(`IF`j'_`k'') mifs(`mIFs') /*
*/ mvars(`mvars') mcons(`mcons') estcmd(`est_cmd') /*
*/ lsize(`lsize') lpos(`lpos') `dots'
if "`dxtype`j''"=="" {
// fractional logit
Estimate_fl, term(`term`j'') type(`type`j'') bn(`bn`j'') /*
*/ mfv(`mfv`j'') lvls(`lvls`j'') tbal(`tbal') `constant' /*
*/ `opts'
}
else if "`delta2'"=="" {
// derivatives
Estimate_dx, type(`dxtype`j'') `opts'
}
else {
// discrete change effects
Estimate_dc, type(`dxtype`j'') delta(`delta2') `centered' /*
*/ `normalize' `opts'
}
matrix `b' = nullmat(`b'), r(b)
if "`cname`j''"!="`name`j''" {
rename `name`j'' `cname`j''
rename `name0' `name`j''
}
}
if `hasat' {
matrix coleq `b' = "`k'"
matrix `B' = nullmat(`B'), `b'
matrix drop `b'
}
}
if `hasat' local b `B'
if "`dots'"=="" {
_progress_info "done" `lpos' `lsize'
di ""
}
// returns
_ms_build_info `b' `awgt'
tempname V
matrix `V' = `b'' * `b' * 0
if "`saveifuninmata'"!="" {
local rank = colsof(`V')
mata: _lnmor_restore(1)
mata: *crexternal("_LNMOR_TMP_IFs") = st_data(., st_local("IFs"))
mata: st_replacematrix(st_local("V"), I(`rank'))
}
else if `vce' {
qui total `IFs' [`weight'`exp'], `vceopt'
if "`weight'"=="iweight" {
local N_tot = el(e(_N),1,1)
}
else local N_tot = e(N)
if `N0'!=`N_tot' {
di as error "something is wrong; inconsistent VCE sample"
exit 498
}
mata: st_replacematrix(st_local("V"), st_matrix("e(V)"))
local rank = e(rank)
if `"`clustvar'"'!="" {
local N_clust = e(N_clust)
local evce `"`e(vce)'"'
local vcetype `"`e(vcetype)'"'
}
else {
local evce robust
local vcetype Robust
}
mata: _lnmor_restore("`ifgenerate'"!="")
}
else {
restore
_estimates unhold `ecurrent'
}
// returns
if `ndx' {
if "`dxtype'"=="levels" {
tempname levels
matrix `levels' = `b' * .
}
}
return matrix b = `b'
return matrix V = `V'
return scalar k_eq = `K'
return scalar k_eform = `K'
return local cmd "lnmor"
return local est_cmd `"`est_cmd'"'
return local title "Marginal (log) odds ratio"
return scalar N = `N0'
return scalar sum_w = `sum_w0'
if "`subuse'"!="" {
return scalar N_subsmp = `N'
return scalar sum_w_subsmp = `sum_w'
return local subsample `"`subsample'"'
}
return local depvar `"`e(depvar)'"'
return local wtype `"`weight'"'
return local wexp `"`exp'"'
return local tbal "`tbal'"
return scalar nterms = `nterms'
if `ndx' {
return local dxtype "`dxtype'"
return local dxlevels "`dxlevels'"
if "`delta2'"!="" {
return scalar delta = `delta2'
return local centered "`centered'"
return local normalize "`normalize'"
}
if "`dxtype'"=="levels" {
local i1 1
forv k = 1 / `K' {
forv j = 1/`nterms' {
if "`dxtype`j''"!="" {
matrix `levels'[1,`i1'] = `levels`j''[1...,1]'
local i1 = `i1' + rowsof(`levels`j'')
}
else {
local i1 = `i1' + `:list sizeof term`j''
}
}
}
return matrix levels = `levels'
}
}
forv j = 1/`nterms' {
return local name`j' "`name`j''"
return local term`j' "`term`j''"
return local type`j' "`type`j''"
return scalar k`j' = `k`j''
return matrix levels`j' = `levels`j''
return scalar dx`j' = "`dxtype`j''"!=""
}
if `hasat' {
return local atvars "`at'"
return matrix at = `AT'
}
if `vce' {
return local clustvar `"`clustvar'"'
if `"`clustvar'"'!="" {
return scalar N_clust = `N_clust'
return scalar df_r = `N_clust' - 1
}
else {
return scalar df_r = `N0' - 1
}
return local vce `"`evce'"'
return local vcetype `"`vcetype'"'
return scalar rank = `rank'
}
else {
return scalar df_r = `N0' - 1
}
if "`ifgenerate'"!="" {
tempname b
mat `b' = return(b)
local coln: coln `b'
local j 0
foreach v of local ifgenerate {
local ++j
gettoken IF IFs : IFs
if "`IF'"=="" continue, break
if "`iftype'"=="RIF" {
qui replace `IF' = `IF' + `b'[1,`j']/`sum_w0'
}
if "`ifscaling'"=="mean" {
qui replace `IF' = `IF' * `sum_w0'
}
gettoken nm coln : coln
lab var `IF' "`iftype' of `nm'"
capt confirm variable `v', exact
if _rc==1 exit _rc
if _rc==0 drop `v'
rename `IF' `v'
}
return local ifgenerate "`ifgenerate'"
return local iftype "`iftype'"
return local ifscaling "`ifscaling'"
}
end
program _parse_ifscaling
syntax [, Mean Total ]
local ifscaling `mean' `total'
if `: list sizeof ifscaling'>1 exit 198
if `"`ifscaling'"'=="" local ifscaling total
c_local ifscaling `ifscaling'
end
program _parse_delta
syntax [, delta2(str) ]
if `"`delta2'"'!="" {
capt confirm number `delta2'
if _rc==1 exit _rc
if _rc {
di as err "delta() invalid -- invalid number"
exit 198
}
}
c_local delta2 `delta2'
end
program _parse_dx
syntax [, dx2(str) ]
if `"`dx2'"'=="" local dx2 "average"
if `:list sizeof dx2'==1 {
if `"`dx2'"'=="." local dx2 "observed"
_parse_dx_type, `dx2' // returns dxtype
}
if "`dxtype'"=="" {
capt numlist `"`dx2'"', sort
if _rc==1 exit _rc
if _rc {
di as err "{bf:dx()}: invalid specification"
exit 198
}
local dxtype "levels"
local dxlevels "`r(numlist)'"
}
c_local dxtype `dxtype'
c_local dxlevels: list uniq dxlevels
end
program _parse_dx_type
capt syntax [, ATMean AVErage OBServed LEVels ]
if _rc==1 exit _rc
if _rc exit
c_local dxtype `atmean' `average' `observed' `levels'
end
program _set_subsamp
gettoken subuse 0 : 0
syntax [varname(numeric default=none)] [if]
mark `subuse' `if'
if "`varlist'"!="" {
markout `subuse' `varlist'
qui replace `subuse' = 0 if `varlist'==0
}
end
program _progress_info
args s lpos lsize
local l = strlen("`s'")
if (`lpos'+`l'-1)>`lsize' {
local l0 = `lsize' - `lpos' + 1
local s0 = substr("`s'", 1, `l0')
di as txt "`s0'"
local lpos 1
local s = substr("`s'", `l0'+1, .)
local l = `l' - `l0'
}
di as txt "`s'" _c
local lpos = `lpos' + `l'
c_local lpos `lpos'
end
program _collect_model_info
_ms_lf_info
c_local mk `r(k1)'
c_local mcons `r(cons1)'
c_local mvars `r(varlist1)'
foreach t in `r(varlist1)' {
_ms_parse_parts `t'
if inlist(r(type), "interaction", "product") {
forv j = 1/`r(k_names)' {
local names `names' `r(name`j')'
if !inlist(r(op`j'),"c","co") {
local fv `fv' `r(name`j')'
}
}
}
else {
if r(type)=="factor" {
local fv `fv' `r(name)'
}
local names `names' `r(name)'
}
}
c_local mnames: list uniq names
c_local mfv: list uniq fv
end
program _collect_fvinfo
local J 0
local bn 1
local next 1
foreach t of local 0 {
_ms_parse_parts `t'
local Type `r(type)'
if "`Type'"=="factor" {
local Name `r(name)'
}
else if "`Type'"=="variable" {
local Name `r(name)'
if "`type'"=="interaction" local Type "interaction"
}
else if "`Type'"=="interaction" {
_collect_fvinfo_interaction `t' // returns Name or error
if "`type'"=="variable" local type "interaction"
}
else {
di as err "`Type' terms not allowed in {it:varlist}"
exit 198
}
if "`Name'"!="`name'" local next 1
else if "`Type'"!="`type'" local next 1
else local next 0
if `next' {
if `J' {
_collect_fvinfo_bn "`type'" "`term'" `bn'
c_local term`J' "`term'"
c_local type`J' "`type'"
c_local name`J' "`name'"
c_local lvls`J' "`lvls'"
c_local bn`J' `bn'
local names `names' `name'
}
local term
local type
local name
local lvls