-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimqi.ado
2296 lines (2141 loc) · 83 KB
/
simqi.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 2.1 January 5, 2003
* (C) Copyright 1998-2003 Michael Tomz, Jason Wittenberg, Gary King
* This file is part of the program Clarify. All Rights Reserved.
* Simulate quantities of interest
program define simqi
version 6.0
* future: print-out x values for first differences (a listx for fd's)
* MARK THE ORIGINAL DATASET (WE WILL REVERT TO IT)
tempvar tokeep
qui gen `tokeep' = 1
* DID USER RUN ESTSIMP BEFORE CALLING THIS ROUTINE?
local ecmd `e(cmd)'
gettoken estsimp modname : ecmd /* parse the command name */
local modname = trim("`modname'") /* trim leading spaces */
if "`estsimp'" ~= "estsimp" {
di in r _n "Must run -estsimp- before -simqi-"
exit 198
}
* DID USER RUN SETX, THEREBY CREATING THE MATRIX mrt_xc?
capture mat list mrt_xc
if _rc == 0 {
local xccols : colnames(mrt_xc)
if "`xccols'" == "`e(rhsvars)'" { local xcfound yes }
}
if "`xcfound'" ~= "yes" {
di in r _newline "X-values missing or invalid. Please rerun -setx-."
exit 198
}
* ARE SIMULATED PARAMETERS (b1,b2,b3...) & DEPVAR STILL IN MEMORY?
local nsims : word count `e(allsims)'
tokenize "`e(allsims)'"
local i 1
while `i' <= `nsims' {
capture confirm variable ``i''
if _rc {
di in r _n "Can't find the simulated" _c
di in r " variable ``1''. Rerun -estsimp-"
exit 198
}
local i = `i' + 1
}
tsunab var : `e(depvar)'
* STRIP FD & CHANGEX FROM LIST OF OPTIONS THAT USER HAS SPECIFIED
local opts `"`0'"' /* list of all specified options */
while "`opts'" ~= "" {
if substr(trim("`opts'"),1,3) == "fd(" {
local opts : subinstr local opts "fd" ""
gettoken opt opts : opts, match(p)
local fd `fd' `opt' /* build a list of fd requests */
}
else if substr(trim("`opts'"),1,8) == "changex(" {
local opts : subinstr local opts "changex" ""
gettoken changex opts : opts, match(p)
}
else {
gettoken opt opts : opts
local rest `rest' `opt'
}
}
if "`fd'" ~= "" & "`changex'" == "" {
di in r _n "If you request a first difference, you must specify "
di in r "the values of X by using the changex() option"
exit 198
}
if "`fd'" == "" & "`changex'" ~= "" {
di in r _n "If you include the changex() option, you must request "
di in r "a first-difference by wrapping one of the options in fd()."
exit 198
}
tokenize "`changex'", parse(",")
if "`1'" == "," | "`2'" == "," {
di in r _n "The changex option cannot contain a comma."
exit 198
}
tokenize "`changex'", parse(";")
if "`1'" == ";" | "`2'" == ";" {
di in r _n "The changex option cannot contain a semicolon."
exit 198
}
* APPLY HIGH-LEVEL PARSING TO REST OF OPTIONS
local 0 `rest' /* reassign to 0 for hi-level parse */
syntax [, PV GENPV(str) EV GENEV(str) PR /*
*/ PRVAL(numlist ascending) GENPR(str) /*
*/ Level(real $S_level) LISTX /*
*/ MSIMS(numlist integer >0 max=1) TFUNC(string)]
tempvar sample /* mark the original sample */
mark `sample'
* CHECK level
if int(`level') ~= `level' {
di in r _n "Confidence level must be an integer."
exit 198
}
if `level' <= 0 | `level' >= 100 {
di in r _n "Confidence level must be between 1 and 99, inclusive"
exit 198
}
* CHECK tfunc: THE TRANSFORMATION FUNCTION THAT THE USER SELECTED
* Note: user can only choose squared, sqrt, logiti, exp, or ln
if "`tfunc'" ~= "" {
if "`tfunc'" == "squared" { local tfuncOK yes }
else if "`tfunc'" == "sqrt" { local tfuncOK yes }
else if "`tfunc'" == "logiti" { local tfuncOK yes }
else if "`tfunc'" == "exp" { local tfuncOK yes }
else if "`tfunc'" == "ln" { local tfuncOK yes }
else {
di in r _n "tfunc(`tfunc') invalid"
exit 198
}
}
* PARSE fd COMMANDS
if "`fd'" ~= "" { _parsefd ,`fd' } /* parse 1st difference args */
local fdev `r(fdev)' /* save fdev */
local fdgenev `r(fdgenev)' /* save fdgenev */
local fdpr `r(fdpr)'
local fdprval `r(fdprval)'
local fdgenpr `r(fdgenpr)'
* CHECK ALL *gen OPTIONS: ARE THE SPECIFIED VARIABLES UNIQUE?
local allgens `genpv' `genev' `fdgenev'
if "`allgens'" ~= "" {
local ngens : word count `allgens' /* number of vars to generate */
tempvar genlist
qui gen str1 `genlist' = "" in 1/`ngens'
tokenize "`allgens'"
local i 1
while `i' <= `ngens' {
confirm new variable ``i''
qui replace `genlist' = "``i''" in `i'
local i = `i' + 1
}
qui tab `genlist'
if `r(r)' < `ngens' {
di in r _n "Error: duplicate variables to generate"
exit 198
}
}
* PARSE changex COMMANDS
local nrhsvar : word count `e(rhsvars)' /* # rhs vars */
local allfds `changex'
local nx 0 /* # sets of x */
while "`changex'" ~= "" {
local nx = `nx' + 1 /* next set */
gettoken changes changex : changex, parse("&") /* changes in */
while "`changes'" ~= "" { /* this set */
gettoken vars changes : changes, match(paren) /* variable(s) */
gettoken fun1 changes : changes, match(paren1) /* function 1 */
gettoken fun2 changes : changes, match(paren2) /* function 2 */
while "`vars'" ~= "" {
gettoken var vars : vars /* get varname */
if "`paren1'" == "" {local slist`nx' `slist`nx'' `var' `fun1'}
else {local slist`nx' `slist`nx'' `var' (`fun1')}
if "`paren2'" == "" {local elist`nx' `elist`nx'' `var' `fun2'}
else {local elist`nx' `elist`nx'' `var' (`fun2')}
}
}
gettoken amper changex : changex, parse("&") /* strip-off & */
}
* CREATE X-VECTORS (STARTING AND ENDING VECTORS) FOR changex SCENARIOS
* Note: these vectors do *not* yet contain a constant term. We'll add
local i 1
while `i' <= `nx' {
tempname xcS`i' xcE`i' /* xcStart, xcEnd vectors */
setx `slist`i'' $mrt_seto keepmrt /* ?does mrt_seto always have , */
matrix `xcS`i'' = r(mrt_xc)
setx `elist`i'' $mrt_seto keepmrt
matrix `xcE`i'' = r(mrt_xc)
local i = `i' + 1
}
* ACTIVATE DEFAULT OPTIONS FOR EACH MODEL (MUST MODIFY FOR NEW MODEL)
* Note: this is model-specific and needs to change when we add models
local mainops `pv' `genpv' `ev' `genev' `pr' `prval' `genpr'/* not fd's */
if "`mainops'" == "" & `nx' == 0 {
if "`modname'" == "regress" { local ev ev }
else if "`modname'" == "logit" { local pr pr }
else if "`modname'" == "probit" { local pr pr }
else if "`modname'" == "ologit" { local pr pr }
else if "`modname'" == "oprobit" { local pr pr }
else if "`modname'" == "mlogit" { local pr pr }
else if "`modname'" == "poisson" { local ev ev }
else if "`modname'" == "nbreg" { local ev ev }
else if "`modname'" == "sureg" { local ev ev }
else if "`modname'" == "weibull" { local ev ev }
}
* reconstruct
local mainops `pv' `genpv' `ev' `genev' `pr' `prval' `genpr'
* MODEL-SPECIFIC CHECKS
* Note: no reason to check pv option; it's allowed for all models
local modabbr = substr("`modname'",1,6)
CK`modabbr' "`genpv'" "`ev'" "`genev'" "`fdev'" "`fdgenev'" /*
*/ "`pr'" "`prval'" "`genpr'" "`fdpr'" "`fdprval'" "`fdgenpr'" /*
*/ "`nx'" "`msims'" "`tfunc'"
if `e(k_cat)' > 0 { /* if model has categorical d.v. */
tempname catdi fdcatdi
matrix `catdi' = r(catdi)
matrix `fdcatdi' = r(fdcatdi)
local prval `r(prval)' /* overwrite prval */
local fdprval `r(fdprval)' /* overwrite fdprval */
}
* OUTPUT FOR MAIN OPERATIONS
if "`mainops'" ~= "" {
if "`listx'" ~= "" { List_XC }
QI`modabbr', `pv' genpv("`genpv'") `ev' genev("`genev'") /*
*/ `pr' prval("`prval'") genpr("`genpr'") /*
*/ catdi("`catdi'") level(`level') `listx' /*
*/ msims("`msims'") tfunc("`tfunc'") xfsetx(mrt_xc)
}
* OUTPUT FOR FIRST DIFFERENCES
* If there are any first differences, build list of names for temporary
* variables. We will pass the lists to the QOI program
* We also build labels for the dependent variable
if `nx' > 0 {
* Expected values
if "`fdev'" ~= "" | "`fdgenev'" ~= "" {
local minev `e(k_eq)'
local i 1
while `i' <= `minev' {
* list of names
tempvar evE_`i' evS_`i' fdev_`i'
local evE `evE' `evE_`i''
local evS `evS' `evS_`i''
* labels for dependent variable
local depvar : word `i' of `e(depvar)'
if `e(versn)' > 6 { local depvar = abbrev("`depvar'",8) }
if "`tfunc'" == "" { local elab_`i' (`depvar') }
else { local elab_`i' [`tfunc'(`depvar')] }
local i = `i' + 1
}
}
else { local minev 0 }
* Probabilities
if "`fdprval'" ~= "" | "`fdgenpr'" ~= "" {
* how many probability values do we need to calculate?
if "`fdprval'" ~= "" { local vals `fdprval' }
else {
tempname ecat
mat `ecat' = e(cat)
local nvals : word count `fdgenpr'
local i 1
while `i' <= `nvals' {
local val = `ecat'[1,`i']
local vals `vals' `val'
local i = `i' + 1
}
}
local minpr : word count `vals'
* make lists of minpr temporary variables
local i 1
while `i' <= `minpr' {
tempvar prE`i' prS`i' fdpr`i'
local prE `prE' `prE`i''
local prS `prS' `prS`i''
local value : word `i' of `vals'
* FIX ME - ABBREVIATE DEPVAR
local depvar `e(depvar)'
if `e(versn)' > 6 { local depvar = abbrev("`depvar'",8) }
local prlab`i' dPr(`depvar' = `value')
local i = `i' + 1
}
}
else { local minpr 0 }
* Note: we do not need list of names or labels for predicted values,
* because we don't allow first differences of predicted values
* Create flag to indicate if we should print first differences
local printfd `fdev' `fdprval'
local fdgen `fdgenev' `fdgenpr'
}
local n 1
while `n' <= `nx' {
* simulate and return E(Y)|xcE as temporary variables
QI`modabbr', `fdev' fdgenev("`fdgenev'") `fdpr' /*
*/ fdprval("`fdprval'") fdgenpr("`fdgenpr'") /*
*/ fdcatdi("`fdcatdi'") msims("`msims'") tfunc("`tfunc'") /*
*/ xfsetx(`xcE`n'') evnames("`evE'") prnames("`prE'")
* simulate and return E(Y)|xcS as temporary variables
QI`modabbr', `fdev' fdgenev("`fdgenev'") `fdpr' /*
*/ fdprval("`fdprval'") fdgenpr("`fdgenpr'") /*
*/ fdcatdi("`fdcatdi'") msims("`msims'") tfunc("`tfunc'") /*
*/ xfsetx(`xcS`n'') evnames("`evS'") prnames("`prS'")
if "`printfd'" ~= "" {
* Print header for first differences (e.g. x1 min sqrt(10))
gettoken fd allfds : allfds, parse("&")
di _n(1) "First Difference: `fd'"
_dihead1 `level'
gettoken fd allfds : allfds, parse("&")
}
* first diffs of expected values
* (loop through equations)
local i 1
while `i' <= `minev' {
qui gen `fdev_`i'' = `evE_`i'' - `evS_`i''
if "`printfd'" ~= "" {
_sumdisp `fdev_`i'' `level' "dE`elab_`i''" printed
}
if "`fdgenev'" ~= "" {
gettoken newvar fdgenev : fdgenev
_genvar `fdev_`i'' `newvar' "fd`n': dE`elab_`i''"
}
drop `evE_`i'' `evS_`i'' `fdev_`i''
local i = `i' + 1
}
* first diffs of probabilities
* (loop through the requested probability values)
local i 1
while `i' <= `minpr' {
qui gen `fdpr`i'' = `prE`i'' - `prS`i''
if "`printfd'" ~= "" {
_sumdisp `fdpr`i'' `level' "`prlab`i''" printed
}
if "`fdgenpr'" ~= "" {
gettoken newvar fdgenpr : fdgenpr
_genvar `fdpr`i'' `newvar' "fd`n': `prlab`i''"
}
drop `prE`i'' `prS`i'' `fdpr`i''
local i = `i' + 1
}
local n = `n' + 1
}
if "`fdgen'" ~= "" { _newgens "`fdgen'" }
* REVERT TO ORIGINAL DATASET
qui keep if `tokeep' == 1
end
***************************** PARSING ROUTINES *****************************
program define _parsefd, rclass
* Parses first difference options
version 6.0
syntax [, PV GENPV(str) EV GENEV(str) PR PRVAL(numlist ascend) GENPR(str)]
if "`pv'" ~= "" {
di in r _n "fd(pv) invalid. -simqi- does not simulate first " _c
di in r "differences" _n "for predicted values."
exit 198
}
if "`genpv'" ~= "" {
di in r _n "fd(genpv(`genpv')) invalid. -simqi- does not generate " _c
di in r "first differences" _n "for predicted values."
exit 198
}
return local fdev `ev'
return local fdgenev `genev'
return local fdpr `pr'
return local fdprval `prval'
return local fdgenpr `genpr'
end
***** MODEL-SPECIFIC CHECKING PROGRAMS (MUST MODIFY FOR NEW MODEL) ******
program define CKregres, rclass
version 6.0
args genpv ev genev fdev fdgenev pr prval genpr fdpr fdprval /*
*/ fdgenpr nx msims tfunc
if "`pr'`prval'`genpr'`fdpr'`fdprval'`fdgenpr'" ~= "" {
di in r _n "Probabilities are not allowed with regression."
exit 198
}
if "`msims'" ~= "" & "`tfunc'" == "" {
di in g _n "Note: msims are not necessary with regress, unless you " /*
*/ " have specified a tfunc()." _n " msims(`msims') ignored." _n
}
ck_genn 1 genpv "`genpv'" /* name only 1 in genpv */
ck_genn 1 genev "`genev'" /* name only 1 in genev */
ck_genn `nx' fd(genev()) "`fdgenev'" /* name only nx in fdgenev */
end
program define CKsureg, rclass
version 6.0
args genpv ev genev fdev fdgenev pr prval genpr fdpr fdprval /*
*/ fdgenpr nx msims tfunc
if "`pr'`prval'`genpr'`fdpr'`fdprval'`fdgenpr'" ~= "" {
di in r _n "Probabilities are not allowed with sureg"
exit 198
}
if "`msims'" ~= "" & "`tfunc'" == "" {
di in g _n "Note: msims are not necessary with sureg, unless you "/*
*/ " have specified a tfunc()." _n " msims(`msims') ignored." _n
}
ck_genn `e(k_eq)' genpv "`genpv'" /* name only `e(k_eq)' vars */
ck_genn `e(k_eq)' genev "`genev'" /* name only `e(k_eq)' vars */
ck_genn `nx'*`e(k_eq)' fd(genev()) "`fdgenev'"
end
program define CKlogit, rclass
* output
* matrix r(catdi): row vector containing 1 if probability for category
* i should be displayed on screen, 0 if not. e.g. catdi=(0,0,1,0)
* indicates that 3rd cat should be displayed, others suppressed
* matrix r(fdcatdi): same, except applies to first differences
version 6.0
args genpv ev genev fdev fdgenev pr prval genpr fdpr fdprval /*
*/ fdgenpr nx msims tfunc
if "`ev'`genev'`fdev'`fdgenev'" ~= "" {
di in y _n "For models with binary dependent variables, simulated " /*
*/ "expected values are" _n "equivalent to simulated probabilities."/*
*/ " Please use pr, prval(#), and/or" _n "genpr(varname) in place "/*
*/ "of ev or genev(varname)."
exit 198
}
if "`tfunc'" ~= "" {
di in r _n "The tfunc() option does not work with the logit model."
exit 198
}
if "`msims'" ~= "" {
di in g _n "Note: msims are not necessary to compute probabilities "
*/ "for this model." _n "msims(`msims') ignored." _n
}
* Check genpv
ck_genn 1 genpv "`genpv'" /* name only 1 var in genpv */
* Check pr, prval, genpr options
* Check that #s in prval correspond with values of the dep variable
* Create a vector of 1s and 0s indicating which outcomes to display
* Calculate max# of new variables that can be generated with genpr()
* [cannot exceed the # of outcomes that will be displayed]
* Make sure user has not listed more than that # of vars in genpr()
tempname catdi fdcatdi
ck_pr "`pr'" "`prval'"
matrix `catdi' = r(catdi)
return matrix catdi `catdi'
local prval `r(prval)'
return local prval `prval'
ck_genn `r(oktogen)' genpr "`genpr'" /* name only oktogen vars in genpr*/
* Check fdpr, fdprval, fdgenpr options
* Check that #s in prval correspond with values of the dep variable
* Create a vector of 1s and 0s indicating which outcomes to display
* Calculate max# of new variables that can be generated with fdgenpr()
* [cannot exceed `nx' * #outcomes that will be displayed]
* Make sure user has not listed more than that # of vars in fdgenpr()
ck_pr "`fdpr'" "`fdprval'"
matrix `fdcatdi' = r(catdi)
return matrix fdcatdi `fdcatdi'
local fdprval `r(prval)'
return local fdprval `fdprval'
ck_genn `nx'*`r(oktogen)' fd(genpr()) "`fdgenpr'"
end
program define CKprobit, rclass
* see comments for CKlogit
* output
* matrix r(catdi): row vector containing 1 if probability for category
* i should be displayed on screen, 0 if not. e.g. catdi=(0,0,1,0)
* indicates that 3rd cat should be displayed, others suppressed
* matrix r(fdcatdi): same, except applies to first differences
version 6.0
args genpv ev genev fdev fdgenev pr prval genpr fdpr fdprval /*
*/ fdgenpr nx msims tfunc
if "`ev'`genev'`fdev'`fdgenev'" ~= "" {
di in y _n "For models with binary dependent variables, simulated " /*
*/ "expected values are" _n "equivalent to simulated probabilities."/*
*/ " Please use pr, prval(#), and/or" _n "genpr(varname) in place "/*
*/ "of ev or genev(varname)."
exit 198
}
if "`tfunc'" ~= "" {
di in r _n "The tfunc() option does not work with the probit model."
exit 198
}
if "`msims'" ~= "" {
di in g _n "Note: msims are not necessary to compute probabilities "
*/ "for this model." _n "msims(`msims') ignored." _n
}
ck_genn 1 genpv "`genpv'" /* name only 1 var in genpv */
tempname catdi fdcatdi
ck_pr "`pr'" "`prval'"
matrix `catdi' = r(catdi)
return matrix catdi `catdi'
local prval `r(prval)'
return local prval `prval'
ck_genn `r(oktogen)' genpr "`genpr'" /* name only oktogen vars in genpr*/
ck_pr "`fdpr'" "`fdprval'"
matrix `fdcatdi' = r(catdi)
return matrix fdcatdi `fdcatdi'
local fdprval `r(prval)'
return local fdprval `fdprval'
ck_genn `nx'*`r(oktogen)' fd(genpr()) "`fdgenpr'"
end
program define CKologit, rclass
* see comments for CKlogit
* output
* matrix r(catdi): row vector containing 1 if probability for category
* i should be displayed on screen, 0 if not. e.g. catdi=(0,0,1,0)
* indicates that 3rd cat should be displayed, others suppressed
* matrix r(fdcatdi): same, except applies to first differences
version 6.0
args genpv ev genev fdev fdgenev pr prval genpr fdpr fdprval /*
*/ fdgenpr nx msims tfunc
if "`ev'`genev'`fdev'`fdgenev'" ~= "" {
di in y _n "For models with ordered dependent variables, please use " /*
*/ "pr, prval(#), and/or" _n "genpr(varname) in place " /*
*/ "of ev or genev(varname)."
exit 198
}
if "`tfunc'" ~= "" {
di in r _n "The tfunc() option does not work with the ologit model."
exit 198
}
if "`msims'" ~= "" {
di in g _n "Note: msims are not necessary to compute probabilities "
*/ "for this model." _n "msims(`msims') ignored." _n
}
ck_genn 1 genpv "`genpv'"
tempname catdi fdcatdi
ck_pr "`pr'" "`prval'"
matrix `catdi' = r(catdi)
return matrix catdi `catdi'
local prval `r(prval)'
return local prval `prval'
ck_genn `r(oktogen)' genpr "`genpr'" /* name only 2 vars in genev */
ck_pr "`fdpr'" "`fdprval'"
matrix `fdcatdi' = r(catdi)
return matrix fdcatdi `fdcatdi'
local fdprval `r(prval)'
return local fdprval `fdprval'
ck_genn `nx'*`r(oktogen)' fd(genpr()) "`fdgenpr'"
end
program define CKoprobi, rclass
* see comments for CKlogit
* output
* matrix r(catdi): row vector containing 1 if probability for category
* i should be displayed on screen, 0 if not. e.g. catdi=(0,0,1,0)
* indicates that 3rd cat should be displayed, others suppressed
* matrix r(fdcatdi): same, except applies to first differences
version 6.0
args genpv ev genev fdev fdgenev pr prval genpr fdpr fdprval /*
*/ fdgenpr nx msims tfunc
if "`ev'`genev'`fdev'`fdgenev'" ~= "" {
di in y _n "For models with ordered dependent variables, please use " /*
*/ "pr, prval(#), and/or" _n "genpr(varname) in place " /*
*/ "of ev or genev(varname)."
exit 198
}
if "`tfunc'" ~= "" {
di in r _n "The tfunc() option does not work with the oprobit model."
exit 198
}
if "`msims'" ~= "" {
di in g _n "Note: msims are not necessary to compute probabilities "
*/ "for this model." _n "msims(`msims') ignored." _n
}
ck_genn 1 genpv "`genpv'"
tempname catdi fdcatdi
ck_pr "`pr'" "`prval'"
matrix `catdi' = r(catdi)
return matrix catdi `catdi'
local prval `r(prval)'
return local prval `prval'
ck_genn `r(oktogen)' genpr "`genpr'" /* name only 2 vars in genev */
ck_pr "`fdpr'" "`fdprval'"
matrix `fdcatdi' = r(catdi)
return matrix fdcatdi `fdcatdi'
local fdprval `r(prval)'
return local fdprval `fdprval'
ck_genn `nx'*`r(oktogen)' fd(genpr()) "`fdgenpr'"
end
program define CKmlogit, rclass
* see comments for CKlogit
* output
* matrix r(catdi): row vector containing 1 if probability for category
* i should be displayed on screen, 0 if not. e.g. catdi=(0,0,1,0)
* indicates that 3rd cat should be displayed, others suppressed
* matrix r(fdcatdi): same, except applies to first differences
version 6.0
args genpv ev genev fdev fdgenev pr prval genpr fdpr fdprval /*
*/ fdgenpr nx msims tfunc
if "`ev'`genev'`fdev'`fdgenev'" ~= "" {
di in y _n "For models with nominal dependent variables, please use " /*
*/ "pr, prval(#), and/or" _n "genpr(varname) in place " /*
*/ "of ev or genev(varname)."
exit 198
}
if "`tfunc'" ~= "" {
di in r _n "The tfunc() option does not work with the mlogit model."
exit 198
}
if "`msims'" ~= "" {
di in g _n "Note: msims are not necessary to compute probabilities "
*/ "for this model." _n "msims(`msims') ignored." _n
}
ck_genn 1 genpv "`genpv'"
tempname catdi fdcatdi
ck_pr "`pr'" "`prval'"
matrix `catdi' = r(catdi)
return matrix catdi `catdi'
local prval `r(prval)'
return local prval `prval'
ck_genn `r(oktogen)' genpr "`genpr'" /* name only 2 vars in genev */
ck_pr "`fdpr'" "`fdprval'"
matrix `fdcatdi' = r(catdi)
return matrix fdcatdi `fdcatdi'
local fdprval `r(prval)'
return local fdprval `fdprval'
ck_genn `nx'*`r(oktogen)' fd(genpr()) "`fdgenpr'"
end
program define CKpoisso, rclass
version 6.0
args genpv ev genev fdev fdgenev pr prval genpr fdpr fdprval /*
*/ fdgenpr nx msims tfunc
if "`tfunc'" ~= "" {
di in r _n "The tfunc() option does not work with the poisson model."
exit 198
}
if "`msims'" ~= "" {
di in g _n "Note: msims are not necessary to compute probabilities "
*/ "for this model." _n "msims(`msims') ignored." _n
}
if "`pr'`fdpr'" ~= "" {
di in r "The pr option is for models with a finite number of outcomes." _n /*
*/ "The poisson model has a potentially infinite number of outcomes." _n /*
*/ "Please use the prval() option to specify the particular outcomes" /*
*/ _n "for which you would like probabilities to be calculated."
exit 198
}
if "`genpr'" ~= "" & "`prval'" == "" {
di in r _n "For this model, you must specify prval() when using genpr()."
exit 198
}
if "`fdgenpr'" ~= "" & "`fdprval'" == "" {
di in r _n "For this model, you must specify fd(prval()) when using " /*
*/ fd(genpr())."
exit 198
}
* check genpv option
ck_genn 1 genpv "`genpv'"
* check prval and genpr options
* confirm that values in prval are nonnegative integers
* Make sure user has not listed too many vars in genpr()
ck_nnint, optname(prval) intlist("`prval'")
local n : word count `prval'
ck_genn `n' genpr "`genpr'" /* name max of n vars in genpr */
* check fdprval and fdgenpr options
* confirm that values in fdprval are nonnegative integers
* Make sure user has not listed too many vars in fdgenpr()
ck_nnint, optname(fdprval) intlist("`fdprval'")
local n : word count `fdprval'
ck_genn `nx'*`n' fdgenpr "`fdgenpr'" /* name max of n vars in fdgenpr */
end
program define CKnbreg, rclass
version 6.0
args genpv ev genev fdev fdgenev pr prval genpr fdpr fdprval /*
*/ fdgenpr nx msims tfunc
if "`tfunc'" ~= "" {
di in r _n "The tfunc() option does not work with the nbreg model."
exit 198
}
if "`msims'" ~= "" {
di in g _n "Note: msims are not necessary to compute probabilities "
*/ "for this model." _n "msims(`msims') ignored." _n
}
if "`pr'`fdpr'" ~= "" {
di in r "The pr option is for models with a finite number of outcomes." _n /*
*/ "The poisson model has a potentially infinite number of outcomes." _n /*
*/ "Please use the prval() option to specify the particular outcomes" /*
*/ _n "for which you would like probabilities to be calculated."
exit 198
}
if "`genpr'" ~= "" & "`prval'" == "" {
di in r _n "For this model, you must specify prval() when using genpr()."
exit 198
}
if "`fdgenpr'" ~= "" & "`fdprval'" == "" {
di in r _n "For this model, you must specify fd(prval()) when using " /*
*/ fd(genpr())."
exit 198
}
* check genpv option
ck_genn 1 genpv "`genpv'"
* check prval and genpr options
* confirm that values in prval are nonnegative integers
* Make sure user has not listed too many vars in genpr()
ck_nnint, optname(prval) intlist("`prval'")
local n : word count `prval'
ck_genn `n' genpr "`genpr'" /* name max of n vars in genpr */
* check fdprval and fdgenpr options
* confirm that values in fdprval are nonnegative integers
* Make sure user has not listed too many vars in fdgenpr()
ck_nnint, optname(fdprval) intlist("`fdprval'")
local n : word count `fdprval'
ck_genn `nx'*`n' fdgenpr "`fdgenpr'" /* name max of n vars in fdgenpr */
end
program define CKweibul, rclass
version 6.0
args genpv ev genev fdev fdgenev pr prval genpr fdpr fdprval /*
*/ fdgenpr nx msims tfunc
if "`tfunc'" ~= "" {
di in r _n "The tfunc() option does not work with the weibull model."
exit 198
}
if "`pr'`prval'`genpr'`fdpr'`fdprval'`fdgenpr'" ~= "" {
di in r _n "Probabilities are not allowed with weibull."
exit 198
}
if "`msims'" ~= "" {
di in g _n "Note: msims are not necessary to compute probabilities "
*/ "for this model." _n "msims(`msims') ignored." _n
}
* check genpv option
ck_genn 1 genpv "`genpv'"
ck_genn 1 genev "`genev'" /* name only 1 in genev */
ck_genn `nx' fd(genev()) "`fdgenev'" /* name only nx in fdgenev */
end
***************************** CHECKING UTILITIES ***************************
program define ck_nnint
* checks for nonnegative integers
version 6.0
capture syntax [, OPTNAME(str) INTLIST(numlist integer >=0)]
if _rc {
di in r _n "Numbers in `optname' option must be nonnegative integers."
exit 198
}
end
program define ck_pr, rclass
* args
* pr, equals "pr" if Clarify should find probs for all outcomes, otw ""
* prval, a list of #s that should correspond to values of the depvar
* reads
* e(k_cat), the # of categories in dependent variable
* e(cat), a vector containing the numeric values for the cats
* Note: e(k_cat) and e(cat) refer to cats actually used in estimation
* output
* local r(oktogen): max# of vars that can be generated with genpr()
* matrix r(catdi): row vector containing 1 if probability for category
* i should be displayed on screen, 0 if not. e.g. catdi=(0,0,1,0)
* indicates that 3rd cat should be displayed, others suppressed
version 6.0
args pr prval
tempname catdi allcats
matrix `allcats' = e(cat) /* valid categories */
if "`pr'" == "" { /* pr was not specified */
matrix `catdi' = J(1,`e(k_cat)',0) /* cats to display */
local vals `prval'
while "`vals'" ~= "" {
gettoken val vals : vals
local found 0
local i 1
while `i' <= `e(k_cat)' {
local c = `allcats'[1,`i']
if `val' == `c' {
local found 1
matrix `catdi'[1,`i'] = 1
local i = `e(k_cat)' + 1 /* exit the loop */
}
else { local i = `i' + 1 } /* inspect next # in allcats */
}
if ~`found' {
di in r _n "Error: `val' is not a valid outcome for `e(depvar)'"
exit 198
}
}
}
else { /* pr was specified */
if "`prval'" ~= "" {
di in g _n "Note: You specified both the pr and the prval() options."
di in g "pr takes precedence over prval(), so -simqi- will display"
di in g "Pr(Y=j) for all j, rather than the values listed in prval()."
}
matrix `catdi' = J(1,`e(k_cat)',1) /* display pr's for all outcomes */
local prval /* erase prval */
local i 1 /* rebuild prval to contain all outcome values */
while `i' <= `e(k_cat)' {
local c = `allcats'[1,`i']
local prval `prval' `c'
local i = `i' + 1
}
}
if "`prval'" == "" { return local oktogen = `e(k_cat)' }
else {
local words : word count `prval'
return local oktogen = `words'
}
return matrix catdi `catdi'
return local prval `prval'
end
program define ck_genn
* Make sure only maxnum variables are listed
* maxnum = maximum number of variables that we allow
* optname = name of the option
* vtogen = list of names of variables to generate
version 6.0
args maxnum optname vtogen /* option name and vars to generate */
local ngen : word count `vtogen'
if `ngen' > `maxnum' {
di in r _n "Error: You listed too many variables in the " /*
*/ "`optname' option."
exit 198
}
end
************************** CALCULATION UTILITIES ****************************
program define prep_XC, rclass
* Arguments
* master = chosen values for all x's, even if not used in this equ
* i = number of the equation
* Output
* xc, a matrix
version 6.0
args master i
tempname xc tmp
local rhsvars `e(rhs_`i')' /* rhsvars for equation i */
while "`rhsvars'" ~= "" { /* pluck off chosen values*/
gettoken rhsvar rhsvars : rhsvars
matrix `tmp' = `master'["r1","`rhsvar'"]
mat `xc' = nullmat(`xc'),`tmp'
}
if `e(cons_`i')' == 1 { mat `xc' = `xc',1 } /* add constant */
matrix colnames `xc' = `e(msn_`i')' /* rename columns */
return matrix xc `xc'
end
*! version 1.3 April 24, 1999 Michael Tomz
* Cholesky decomposition of an arbitrary matrix
* Input: V, the original matrix
* k, the dimension of the matrix
* A, the name of a new matrix to be created
* Output: A, the lower-triangle cholesky of V
program define _chol
version 6.0
args V k A
capt matrix `A' = cholesky(`V') /* square root of VC matrix */
if _rc == 506 { /* If VC ~ pos definite, ... */
tempname eye transf varianc vcd tmp
mat `eye' = I(`k') /* identity matrix */
mat `transf' = J(1,`k',0) /* initialize transf matrix */
local i 1
while `i' <= `k' {
scalar `varianc' = `V'[`i',`i'] /* variance of parameter `i' */
if `varianc' ~= 0 { /* if has variance, add row */
mat `tmp' = `eye'[`i',1...] /* of `eye' to transf matrix */
mat `transf' = `transf' \ `tmp'
}
local i = `i' + 1
}
mat `transf' = `transf'[2...,1...] /* drop 1st row of transf mat*/
mat `vcd' = `transf'*`V'*`transf'' /* decomposed VC (no 0 rows) */
mat `A' = cholesky(`vcd') /* square root of decomp VC */
mat `A' = `transf''*`A'*`transf' /* rebuild full sq root mat */
}
else if _rc { matrix `A' = cholesky(`V') } /* redisplay error message */
end
program define _expand
version 6.0
args msims
capture set obs `msims'
if _rc {
di in r _n "Not enough memory for msims(`msims')."
di in r "Please increase memory or reduce msims()."
exit 198
}
end
*! Version 1.0.0 August 3, 1998 Michael Tomz
*Random draws from Poisson
*Reference: William H. Press, et al (1992). Numerical Recipies in C:
* The Art of Scientific Computing. Cambridge U Press, pp. 206-8
program define rnd_pois
version 5.0
local mean `1'
local out `2'
if `mean' < 12 {
tempvar g em t nostop
qui g `g' = exp(-`mean')
qui g `em' = -1 if `g' ~= .
qui g `t' = 1 if `g' ~= .
qui g `nostop' = 1 if `g' ~= .
local repeat 1
while `repeat' > 0 {
qui replace `em' = `em' + 1 if `nostop' == 1
qui replace `t' = `t' * uniform() if `nostop' == 1
qui replace `nostop' = 0 if `t' <= `g'
qui count if `nostop' == 1
local repeat = _result(1)
}
}
else {
tempvar sq alxm g part1 part2 y em t
g `sq' = sqrt(2*`mean')
g `alxm' = ln(`mean')
g `g' = `mean'*`alxm'-lngamma(`mean'+1)
g `part1' = 1 if `sq' ~= .
qui g `part2' = .
qui g `y' = .
qui g `em' = .
qui g `t' = .
local rep1 1
while `rep1' > 0 {
qui replace `part2' = `part1'
qui count if `part2'
local rep2 = _result(1)
while `rep2' > 0 {
qui replace `y' = tan(_pi*uniform()) if `part2'==1
qui replace `em' = `sq'*`y' + `mean' if `part2'==1
qui replace `part2' = 0 if `em' >= 0
qui count if `part2'
local rep2 = _result(1)
}
qui replace `em' = int(`em') if `part1'==1
qui replace `t'=.9*(1+`y'^2)*exp(`em'*`alxm'-lngamma(`em'+1)-`g') /*
*/ if `part1'==1
qui replace `part1' = 0 if uniform() <= `t'
qui count if `part1'
local rep1 = _result(1)
}
}
qui gen `out' = `em'
end
*! Version 1.0.0 August 3, 1998 Michael Tomz
* Random draws from Gamma
* References: For Ahrens/Dieter algorithm, see Brian D. Ripley (1987).
* Stochastic Simulation. John Wiley & Sons, p. 88
* For Best 1978 algorithm, see Luc Devroye (1986). Non-Uniform
* Random Variate Generation. NY: Springer-Verlag, p. 410
program define rnd_gam
version 5.0
local alpha `1' /* shape parameter */
local beta `2' /* scale parameter */
local out `3' /* output variable */
summarize `alpha', meanonly
* If minimum alpha is less than 1, use Ahrens/Dieter 1974 algorithm
if _result(5) < 1 {
tempvar cond1 cond2 cond3 cond4 nostop u0 u1 x
qui g `cond1' = 1 if `alpha' ~= .
qui g `cond2' = 1 if `alpha' ~= .
qui g `cond3' = 1 if `alpha' ~= .
qui g `cond4' = 1 if `alpha' ~= .
qui g `u0' = .
qui g `u1' = .
qui g `x' = .
local repeat = 1
while `repeat' > 0 {
**If condition 1 is true ( if value has not been accepted )
qui replace `u0'=uniform() if `cond1'==1
qui replace `u1'=uniform() if `cond1'==1
* Evaluate condition 2
qui replace `cond2'=0 if `cond1'==1 & `u0'<=exp(1)/(`alpha'+exp(1))
**If condition 2 is still true
qui replace `x'=-ln((`alpha'+exp(1))*(1-`u0')/(`alpha'*exp(1))) /*
*/ if `cond1'==1 & `cond2'==1
* Evaluate condition 4
qui replace `cond4'=0 if `cond1'==1 & `cond2'==1 & /*
*/ `u1'<=`x'^(`alpha'-1)
* If condition 4 is now false, done!
qui replace `cond1'=0 if `cond1'==1 & `cond4'==0
* But if condition 4 is still true, start again
qui replace `cond3'=1 if `cond1'==1 & `cond4'==1
**If condition 2 is now false
qui replace `x'=((`alpha'+exp(1))*`u0'/exp(1))^(1/`alpha') /*
*/ if `cond1'==1 & `cond2'==0
* Evaluate condition 3
qui replace `cond3'=0 if `cond1'==1 & `cond2'==0 & `u1'<=exp(-`x')
* If condition 3 is now false, done!
qui replace `cond1'=0 if `cond1'==1 & `cond3'==0
* But if condition 3 is still true, start again
qui replace `cond2'=1 if `cond1'==1 & `cond3'==1
qui replace `cond4'=1 if `cond1'==1 & `cond3'==1
**Are we done yet?
qui count if `cond1' == 1
local repeat = _result(1)
}
}
* Otherwise use Best 1978 algorithm
else {
tempvar b c nostop u v w x y z
qui gen `b' = `alpha' - 1