-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpenlogit.ado
1070 lines (950 loc) · 27.9 KB
/
penlogit.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
/*
pllf2 is an adapted code of the -pllf- command by Royston
*/
*! 1.0.1 Discacciati A, Orsini N 21feb2014
*! 1.0.2 Discacciati A, Orsini N 01sep2014
*! 1.1.0 Discacciati A, Orsini N 19jul2017
capture program drop penlogit
program penlogit, eclass properties(mi) byable(onecall)
version 12
if _by() {
local BY `"by `_byvars'`_byrc0':"'
}
`version' `BY' _vce_parserun penlogit, mark(OFFset CLuster) : `0'
if "`s(exit)'" != "" {
version 12: ereturn local cmdline `"penlogit `0'"'
exit
}
if replay() {
if ("`e(cmd)'"!="penlogit") error 301
Replay `0'
}
else `version' `BY' Estimate `0'
end
capture program drop Estimate
program Estimate, eclass byable(recall)
version 12
syntax varlist [if] [in] [fweight], ///
[ ///
NPrior(string) ///
LFPrior(string) ///
ppl(string) ///
BINomial(string) ///
Level(integer $S_level) ///
or ///
NOList ///
nppl(integer 100) ///
NOCONstant ///
]
local cmdline : copy local 0
marksample touse
gettoken depv indepv : varlist
noi _rmcoll `indepv' if `touse' , forcedrop `=cond("`noconstant'"=="noconstant","noconstant"," ")'
local indepv `r(varlist)'
local nr_profile : word count `ppl'
*Normal priors
local nprior_elements : word count `nprior'
if mod(`nprior_elements', 3) != 0 {
di as err "incorrect number of elements in nprior()"
exit 198
}
local npriors = `nprior_elements' / 3
local npriornames ""
local priorm ""
local priorv ""
tokenize "`nprior'"
forv j = 1/3 {
forv k =`j'(3)`nprior_elements' {
if (`j'==1) {
confirm numeric variable ``k''
local npriornames "`npriornames' ``k''"
}
if (`j'==2) {
local priorm "`priorm' `=``k'''"
}
if (`j'==3) {
local priorv "`priorv' `=``k'''"
}
}
}
unab npriornames : `npriornames' , min(`npriors') max(`npriors')
foreach v of local npriornames {
if (`: list v in indepv' != 1) {
di as err "prior `v' is not among [indepvars]"
exit 198
}
}
*
*Generalized Log-f priors
local lfprior_elements: word count `lfprior'
if mod(`lfprior_elements', 5) != 0 {
di as err "incorrect number of elements in lfprior()"
exit 198
}
local lfpriors = `lfprior_elements' / 5
local lfpriornames ""
local mm ""
local d0 ""
local d1 ""
local ss ""
tokenize "`lfprior'"
forv j = 1/5 {
forv k =`j'(5)`lfprior_elements' {
if (`j'==1) {
confirm numeric variable ``k''
local lfpriornames "`lfpriornames' ``k''"
}
if (`j'==2) {
local mm "`mm' `=``k'''"
}
if (`j'==3) {
local d1 "`d1' `=``k'''"
}
if (`j'==4) {
local d0 "`d0' `=``k'''"
}
if (`j'==5) {
local ss "`ss' `=``k'''"
}
}
}
unab lfpriornames : `lfpriornames', min(`lfpriors') max(`lfpriors')
foreach v of local lfpriornames {
if (`: list v in indepv' != 1) {
di as err "prior `v' is not among [indepvars]"
exit 198
}
}
*
*Check for repeated priors
local names "`lfpriornames' `npriornames'"
local dup: list dups names
if "`dup'" != "" {
local rep: list uniq dup
di as err "prior(s) for `dup' specified more than once"
exit 198
}
*
*Check penalized profile likelihood variables
if "`ppl'" != "" {
local bign = c(N)
capture unab ppl : `ppl' , min(`nr_profile') max(`nr_profile')
foreach v of local ppl {
if (`: list v in indepv' != 1) {
di as err "var `v' (option ppl) is not among [indepvars]"
exit 198
}
}
}
*
tempvar constant H n tag
tempname A prior_v H prior_logrr lb ub s lfc lfn lfexpoff nc nexpoff retnorm retlogf subjn
gen `constant' = 1
gen `H' = 0
if "`binomial'" == "" {
gen `n' = 1
}
else {
confirm variable `binomial'
gen `n' = `binomial'
}
if "`binomial'" != "" {
capture assert `n' >= `depv' if `touse' & !missing(`depv'), fast
if _rc != 0 {
di as err "`depv' > `binomial' in some cases"
exit 499
}
}
else {
_rmcoll `depv' if `touse', logit
}
tempvar wgt
if "`weight'" == "" {
gen `wgt' = 1
}
else {
local weights [`weight'`exp']
gen `wgt' `exp'
}
gen `tag' = 0
* Normal priors
if `npriors' != 0 {
mat `retnorm' = J(2,`npriors',.)
mat rownames `retnorm' = m v
mat colnames `retnorm' = `npriornames'
}
local c = 1
qui foreach v of local npriornames {
qui set obs `=_N+1'
local prior_logrr : word `c' of `priorm'
local prior_v : word `c' of `priorv'
mat `retnorm'[1,`c'] = `prior_logrr'
mat `retnorm'[2,`c'] = `prior_v'
scalar `A' = 900
scalar `s' = (`prior_v'/(2/scalar(`A')))^0.5
scalar `H' = - (`prior_logrr'/scalar(`s'))
replace `depv' = `A' in l
replace `v' = `=1/`s'' in l
replace `H' = scalar(`H') in l
replace `constant' = 0 in l
replace `n' = 2*scalar(`A') in l
replace `tag' = 1 in l
if "`nolist'" == "" {
scalar `lb' = `=exp(`prior_logrr')'*(invF(`=2*scalar(`A')', /*
*/ `=2*scalar(`A')', `=(1-(`level'/100))/2'))^`s'
scalar `ub' = `=exp(`prior_logrr')'*(invF(`=2*scalar(`A')', /*
*/ `=2*scalar(`A')', `=((1-(`level'/100))/2)+(`level'/100)'))^`s'
local normal`c' `: di " exact prior median OR (`level'% PL): " %4.2fc `=exp(`prior_logrr')*(invF(2*scalar(`A'), /*
*/ 2*scalar(`A'), 0.5))^`s'' /*
*/ " (" %4.2f scalar(`lb') ", " %4.2f scalar(`ub') ")"'
get_df_invF `=invF(`=2*scalar(`A')', `=2*scalar(`A')', 0.975)^`s''
scalar `subjn' = r(df)/2
local normalz`c' `: di "cases=" %-5.2f scalar(`subjn') " noncases=" %-5.2f scalar(`subjn') " exp(offset)=" /*
*/ %-5.0g `=exp(scalar(`H'))' '
}
local `c++'
}
*
* Generalized Log-f priors
if `lfpriors' != 0 {
mat `retlogf' = J(4,`lfpriors',.)
mat rownames `retlogf' = m df1 df2 s
mat colnames `retlogf' = `lfpriornames'
}
local c = 1
qui foreach v of local lfpriornames {
qui set obs `=_N+1'
local mmx : word `c' of `mm'
local d1x : word `c' of `d1'
local d0x : word `c' of `d0'
local ssx : word `c' of `ss'
mat `retlogf'[1,`c'] = `mmx'
mat `retlogf'[2,`c'] = `d1x'
mat `retlogf'[3,`c'] = `d0x'
mat `retlogf'[4,`c'] = `ssx'
scalar `H' = ln(`d1x' / `d0x') - (`mmx' / `ssx')
replace `depv' = `=(`d1x'/2)' in l
replace `v' = `=1/`ssx'' in l
replace `H' = scalar(`H') in l
replace `constant' = 0 in l
replace `n' = `=(`d1x'+`d0x')/2' in l
replace `tag' = 1 in l
if "`nolist'" == "" {
scalar `lb' = `=exp(`mmx')'*(invF(`d1x', /*
*/ `d0x', `=(1-(`level'/100))/2'))^`ssx'
scalar `ub' = `=exp(`mmx')'*(invF(`d1x', /*
*/ `d0x', `=((1-(`level'/100))/2)+(`level'/100)'))^`ssx'
local logf`c' `: di " exact prior median OR (`level'% PL): " %4.2fc `=exp(`mmx')*(invF(`d1x', /*
*/ `d0x', 0.5))^`ssx'' /*
*/ " (" %4.2f scalar(`lb') ", " %4.2f scalar(`ub') ")"'
scalar `lfc' = `d1x'/(2 * `ssx'^2)
scalar `lfn' = `d0x'/(2 * `ssx'^2)
scalar `lfexpoff' = exp(scalar(`H'))
local logfz`c' `: di "cases=" %-5.2f scalar(`lfc') " noncases=" %-5.2f scalar(`lfn') " exp(offset)=" /*
*/ %-5.0g `=exp(scalar(`H'))' '
}
local `c++'
}
*
qui foreach v of local indepv {
replace `v' = 0 if `tag' == 1 & `v' == .
}
qui replace `wgt' = 1 if `tag'
local c = 1
if "`ppl'" != "" {
local bign = c(N)
foreach x of local ppl {
qui pllf2 glm `depv' `indepv' `=cond("`noconstant'"=="noconstant"," ","`constant'")' /*
*/ if `touse' [fw = `wgt'], /*
*/ fam(binomial `n') offzet(`H') nocons /*
*/ profile(`x') nodots nograph level(`level') n(`nppl')
tempname lb_`x' ub_`x'
scalar `lb_`x'' = r(l_llci)
scalar `ub_`x'' = r(l_ulci)
local `c++'
}
qui drop if _n > `bign'
}
tempname coefs VCE ilog
qui glm `depv' `indepv' `=cond("`noconstant'"=="noconstant"," ","`constant'")' if `touse' [fw = `wgt'], /*
*/ offset(`H') nocons nolog fam(binomial `n')
local ll = e(ll)
local ic = e(ic)
local k = e(k)
local converged = e(converged)
local nobs = e(N)-`npriors'-`lfpriors'
local nobsda = e(N)
local conams "`indepv' `=cond("`noconstant'"=="noconstant", "", "_cons")'"
mat `coefs' = e(b)
mat `VCE' = e(V)
mat `ilog' = e(ilog)
mat colnames `coefs' = `conams'
mat rownames `VCE' = `conams'
mat colnames `VCE' = `conams'
mat coleq `coefs' = ""
mat roweq `VCE' = ""
mat coleq `VCE' = ""
ereturn post `coefs' `VCE', obs(`nobs') depn(`depv')
ereturn matrix ilog = `ilog'
if `npriors' != 0 {
ereturn matrix nprior = `retnorm'
}
if `lfpriors' != 0 {
ereturn matrix lfprior = `retlogf'
}
ereturn scalar N = `nobs'
ereturn scalar N_da = `nobsda'
ereturn scalar pll = `ll'
ereturn scalar ic = `ic'
ereturn scalar k = `k'
ereturn scalar converged = `converged'
ereturn local cons = "`noconstant'"
ereturn local m = cond("`binomial'"!="", "`binomial'", "1")
ereturn local varfunc = "glim_v2"
ereturn local link = "glim_l02"
ereturn local opt = "moptimize"
ereturn local predict "glim_p"
if "`weight'" != "" {
ereturn local wexp = "`exp'"
ereturn local wtype = "fweight"
}
ereturn local indepvars "`indepv'"
ereturn local cmd "penlogit"
ereturn local cmdline `"penlogit `cmdline'"'
if `=`nprior_elements' + `lfprior_elements'' == 0 {
local title "Logistic regression"
local lltitle "Log likelihood"
}
else {
local title "Penalized logistic regression"
local lltitle "Penalized log likelihood"
}
di _n in gr "`title'" _col(55) "No. of obs = " in ye %10.0g e(N) _n
if "`nolist'" == "" {
local c = 1
foreach v of local npriornames {
di in gr "Normal prior for `=abbrev("`v'", 12)': " in y "`normal`c''"
di in gr "Data approx. equivalent to prior: " in y "`normalz`c''" _n
local `c++'
}
local c = 1
foreach v of local lfpriornames {
di in gr "Log-F prior for `=abbrev("`v'", 21)': " in y "`logf`c''"
di in gr "Data approx. equivalent to prior: " in y "`logfz`c''" _n
local `c++'
}
}
di in gr "`lltitle' = " in ye `ll' _n
Replay, level(`level') `or'
local c = 1
if "`ppl'" != "" {
tempname plm
mat define `plm' = J(`nr_profile', 2, .)
mat colnames `plm' = lb ub
mat rownames `plm' = `ppl'
local ytitle "`depv'"
if "`or'" == "" local tt "Coef."
else local tt "Odds Ratio"
di as txt "{hline 13}{c TT}{hline 24}"
di as txt %12s abbrev("`ytitle'",12) _col(14)"{c |}" /*
*/ %1s " [`level'% PL Conf. Interval]"
di as txt "{hline 13}{c +}{hline 24}"
foreach x of local ppl {
mat `plm'[`c',1] = `lb_`x''
mat `plm'[`c',2] = `ub_`x''
if "`or'" == "" di in gr %12s abbrev("`x'",12) _col(14) "{c |}" /*
*/ as res _col(18) %9.0g `lb_`x'' _col(30) %9.0g `ub_`x''
else di in gr %12s abbrev("`x'",12) _col(14) "{c |}" /*
*/ as res _col(18) %9.0g exp(`lb_`x'') _col(30) %9.0g exp(`ub_`x'')
local `c++'
}
di as txt "{hline 13}{c BT}{hline 24}"
ereturn matrix ppl = `plm'
}
qui drop if `tag' == 1
ereturn repost, esample(`touse')
end
capture program drop Replay
program Replay
syntax [, Level(cilevel) or ]
if "`or'"== "" ereturn display, level(`level')
else ereturn display, level(`level') eform("Odds Ratio")
end
*------- pllf2 -------*
capture program drop pllf2
program define pllf2, rclass sortpreserve
version 9.0
/*
Currently supported commands include at least the following:
clogit cnreg cox ereg fit glm gnbreg heckman logistic logit ///
mlogit nbreg ologit oprobit poisson probit regress reg3 ///
streg stcox stpm weibull
*/
gettoken cmd 0 : 0
if "`cmd'"=="stpm" local eqxb [xb]
else if ("`cmd'"=="fit") | ("`cmd'"=="reg") | (substr("`cmd'",1,4)=="regr") local cmd regress
syntax anything [if] [in] [aweight fweight pweight iweight], ///
[DEViance FORMula(string) gen(string) DIFFerence LEVel(cilevel) ///
PLaceholder(string) PROfile(string) range(string) ///
MAXCost(int -1) n(integer 100) noci noDOTs nograph noCONStant gropt(string asis) ///
LEVLINe(string asis) CILINes(string asis) offzet(string) *]
if `maxcost'<0 local maxcost = int(`n'/2)
if "`placeholder'"!="" {
if wordcount("`placeholder'")!=1 {
di as err "invalid placeholder()"
exit 198
}
}
else local placeholder X
if "`formula'"!="" {
if "`profile'"!="" {
di as txt "[profile() ignored]"
local profile
}
if "`range'"=="" {
di as err "range() required"
exit 198
}
// Check for `placeholder' in `anything'
local result: subinstr local anything "`placeholder'" "", count(local nat)
if `nat'==0 {
di as err `"`placeholder' not found in regression_cmd_stuff ( `anything' )"'
exit 198
}
}
else if "`profile'"!="" {
local varlist "`anything'"
// Disentangle profile; extract eq from it, if present
tokenize `profile', parse("[]")
if "`5'"!="" {
di as err "syntax error in profile(`profile'), invalid equation name"
exit 198
}
if "`2'"!="" {
// Rudimentary check that user has entered the eq correctly
if "`1'"!="[" | "`3'"!="]" {
di as err "syntax error in profile(`profile'), invalid equation name"
exit 198
}
local profile `4'
local eq [`2']
constraint free
local cuse `r(free)'
}
if "`profile'"!="_cons"{
unab profile: `profile'
}
else {
if "`eq'"=="" {
di as err "profile log likelihood for _cons not supported"
exit 198
}
}
}
else {
di as err "must supply either formula() or profile()"
exit 198
}
/*
if "`gen'"=="" {
local gen1 _beta
local gen2 _pll
}
else gettoken gen1 gen2: gen
if "`gen2'"=="" local gen2 _pll
*/
if "`range'"!="" {
gettoken from to: range
confirm num `from'
confirm num `to'
if `from' > `to' {
local temp `from'
local from `to'
local to `temp'
local temp
}
}
if "`weight'" != "" local wt [`weight'`exp']
if "`profile'" != "" { // ------------ begin linear profiling --------
// Fit model and get level% ci. Program terminates if invalid cmd attempted.
if "`eq'"=="" & "`constant'"!="noconstant" {
qui _rmcoll `varlist' `profile' // strips `profile' if already mentioned in `varlist'
local tempvl `r(varlist)'
}
else local tempvl `varlist'
capture noisily `cmd' `tempvl' `if' `in' `wt', `options' `constant' offset(`offzet')
local ytitle `e(depvar)'
quietly {
// Check that alleged parameter exists. One or both of `eqxb' or `eq' will always be null.
capture local b0 = `eq'`eqxb'_b[`profile']
if "`b0'"=="" local b0 .
if "`cmd'"=="regress" local z = invttail(e(df_r), (100-`level')/200)
else local z = -invnorm((100-`level')/200)
local nobs = e(N)
local ll0 = e(ll)
capture local se = `eq'`eqxb'_se[`profile']
if _rc==0 {
local llci = `b0'-`z'*`se'
local ulci = `b0'+`z'*`se'
}
else {
local se .
if "`range'"=="" {
noi di as err "could not select range for `profile' (could not estimate MLE). try supplying range()"
exit 198
}
}
if "`range'"=="" {
// previously used default range as Wald-based `level' CI; now using +/-(z*1.2)SE for this.
local from = `b0'-`z'*1.2*`se'
local to = `b0'+`z'*1.2*`se'
}
// If no equation specified, unabbreviate varlist and remove `profile' from it
if "`eq'"=="" {
unab varlist: `varlist'
local varlist: list varlist - profile
}
if "`cmd'"=="regress" {
constraint free
local cuse `r(free)'
// For regress, need to identify yvar; otherwise, not required
gettoken yvar varlist: varlist
}
if `n'>_N {
di as txt "[Increasing the dataset size to `n'] " _cont
set obs `n'
}
tempvar X Y order // values of X = regression coefficient, Y = profile likelihood for `profile'
gen `X' = .
gen `Y' = .
gen long `order' = _n
local stepsize = (`to'-`from')/(`n'-1)
forvalues i=1/`n' {
local b = `from'+(`i'-1)*`stepsize'
if "`eq'"!="" {
// Use constrained regression
constraint define `cuse' `eq'`profile'=`b'
`cmd' `varlist' `if' `in' `wt', `options' constraint(`cuse') `constant'
}
else {
if `i'==1 {
tempvar offset
gen `offset' = .
}
if "`cmd'"=="regress" {
replace `offset' = `yvar'-`b'*`profile'
regress `offset' `varlist' `if' `in' `wt', `options'
}
else {
replace `offset' = `b'*`profile'+`offzet'
`cmd' `varlist' `if' `in' `wt', `options' offset(`offset') `constant'
}
}
sort `order'
replace `X' = `b' in `i'
replace `Y' = e(ll) in `i'
if "`dots'"!="nodots" noi di "." _c
}
local cost 0 // "cost": number of extra evaluations of pll needed to find likelihood based CI
local left_limit .
local right_limit .
if "`ci'"!="noci" {
/*
Search for likelihood based CI bounds: VERY crude!
Left limit first.
*/
local target = `ll0'-`z'^2/2 // pll value for computing likelihood based CI
if `Y'[1]<`target' {
/*
First evaluated pll is below target pll on left of mle.
Bracket target from already known values of pll and interpolate.
*/
forvalues i=2/`n' {
if `Y'[`i']>=`target' {
local left_limit = `X'[`i'-1]+`stepsize'*(`target'-`Y'[`i'-1])/(`Y'[`i']-`Y'[`i'-1])
continue, break // exit forvalues loop
}
}
}
else {
/*
Search for left ll-based confidence limit to the left of first value of b.
Requires new evaluations of ll - no more than `maxcost' allowed.
*/
local Yold = `Y'[1]
local bold `from'
local i 1
while missing(`left_limit') & `i'<=`maxcost' {
local b = `from'-`i'*`stepsize'
// evaluate pll
if "`eq'"!="" {
// Use constrained regression
constraint define `cuse' `eq'`profile'=`b'
`cmd' `varlist' `if' `in' `wt', `options' constraint(`cuse') `constant'
}
else {
if "`cmd'"=="regress" {
replace `offset' = `yvar'-`b'*`profile'
regress `offset' `varlist' `if' `in' `wt', `options'
}
else {
replace `offset' = `b'*`profile'+`offzet'
`cmd' `varlist' `if' `in' `wt', `options' offset(`offset') `constant'
}
}
local Ynew = e(ll)
if `Ynew'<`target' { // now bracketing target
local left_limit = `bold'-`stepsize'*(`target'-`Yold')/(`Ynew'-`Yold')
}
else {
local Yold `Ynew'
local bold `b'
local ++i
}
if "`dots'"!="nodots" noi di "." _c
}
local cost `i'
if missing(`left_limit') noi di as txt _n "[note: failed to find left-hand confidence limit]"
}
/*
Now right limit
*/
if `Y'[`n']>`target' { // search for right_limit to the right of last value of b
local Yold = `Y'[`n']
local bold `to'
local i 1
while missing(`right_limit') & `i'<=`maxcost' {
local b = `to'+`i'*`stepsize'
// evaluate pll
if "`eq'"!="" {
// Use constrained regression
constraint define `cuse' `eq'`profile'=`b'
`cmd' `varlist' `if' `in' `wt', `options' constraint(`cuse') `constant'
}
else {
if "`cmd'"=="regress" {
replace `offset' = `yvar'-`b'*`profile'
regress `offset' `varlist' `if' `in' `wt', `options'
}
else {
replace `offset' = `b'*`profile'+`offzet'
`cmd' `varlist' `if' `in' `wt', `options' offset(`offset') `constant'
}
}
local Ynew = e(ll)
if `Ynew'<`target' { // now bracketing target
local right_limit = `bold'+`stepsize'*(`target'-`Yold')/(`Ynew'-`Yold')
}
else {
local Yold `Ynew'
local bold `b'
local ++i
}
if "`dots'"!="nodots" noi di "." _c
}
local cost = `cost'+`i'
}
else { // pll is below target on right of mle, bracket target and interpolate
local n1 = `n'-1
forvalues i=`n1'(-1)1 {
if `Y'[`i']>=`target' {
local right_limit = `X'[`i']+`stepsize'*(`target'-`Y'[`i'])/(`Y'[`i'+1]-`Y'[`i'])
continue, break // exit forvalues loop
}
}
}
if missing(`right_limit') noi di as txt _n "[note: failed to find right-hand confidence limit]"
}
lab var `X' "`eq'_b[`profile']"
}
}
else { // --------------- begin non-linear profiling ---------------
tempvar xx
qui gen `xx' = .
// Trial fit of model with central value of param. Program terminates if invalid cmd attempted.
local A = (`to'-`from')/2
parsat `"`anything'"' `if' `in', formula(`formula') var(`xx') value(`A') placeholder(`placeholder')
cap `cmd' `r(result)' `if' `in' `wt', `options' `constant'
local rc = _rc
if `rc' error `rc'
local ytitle `e(depvar)'
cap local ll = e(ll)
if _rc | ("`ll'"==".") {
di as err "valid log likelihood not returned in e(ll)"
exit 198
}
quietly {
if "`cmd'"=="regress" local z = invttail(e(df_r), (100-`level')/200)
else local z = -invnorm((100-`level')/200)
local nobs = e(N)
replace `touse' = e(sample)
if `n'>_N {
di as txt "[Increasing the dataset size to `n'] " _cont
set obs `n'
}
tempvar X Y order // values of X = regression coefficient, Y = profile likelihood for `profile'
gen `X' = .
gen `Y' = .
gen long `order' = _n
local stepsize = (`to'-`from')/(`n'-1)
local y1 .
local y2 .
local y3 .
local b0 .
local ll0 .
local done 0
forvalues i=1/`n' {
local A = `from'+(`i'-1)*`stepsize'
// Substitute A in `formula' and fit model
parsat `"`anything'"' `if' `in', formula(`formula') var(`xx') value(`A') placeholder(`placeholder')
cap `cmd' `r(result)' `if' `in' `wt', `options' `constant'
if _rc {
noi di as err "model fit failed at parameter = " `A'
exit 198
}
local y3 = e(ll)
if !missing(`y1') & !missing(`y2') {
if `y1'<`y2' & `y2'>`y3' {
// Solve quadratic y = a + b*x + c*x^2 through 3 points to get MLE b0 and ll at MLE ll0
local c = (2*(`y3'-`y2')-(`y3'-`y1'))/(2*`stepsize'^2)
local b = (`y3'-`y2')/`stepsize'-`c'*(2*`A'-`stepsize')
local a = `y3'-`A'*(`b'+`c'*`A')
local b0 = -`b'/(2*`c')
local ll0 = `a'+`b0'*(`b'+`c'*`b0')
local done 1
}
}
local y1 `y2'
local y2 `y3'
sort `order'
replace `X' = `A' in `i'
replace `Y' = e(ll) in `i'
if "`dots'"!="nodots" noi di "." _c
}
if !`done' {
// MLE not straddled. Attempt quadratic solution using terminals of range and a midpoint. Maintain equal spacing.
if mod(`n',2)==0 { // even number of evaluations
local mid = `n'/2
local last `n'-1
}
else { // odd number of evaluations
local mid = (`n'+1)/2
local last `n'
}
local A = `X'[`last']
local s = `X'[`mid']-`X'[1] // stepsize for this exercise
local y1 = `Y'[1]
local y2 = `Y'[`mid']
local y3 = `Y'[`last']
local c = (2*(`y3'-`y2')-(`y3'-`y1'))/(2*`s'^2)
local b = (`y3'-`y2')/`s'-`c'*(2*`A'-`s')
local a = `y3'-`A'*(`b'+`c'*`A')
local b0 = -`b'/(2*`c')
local ll0 = `a'+`b0'*(`b'+`c'*`b0')
noi di as txt _n "Note: range does not include MLE of parameter - estimate may be inaccurate."
}
local cost 0 // "cost": number of extra evaluations of pll needed to find likelihood based CI
local left_limit .
local right_limit .
if "`ci'"!="noci" {
/*
Search for likelihood based CI bounds: VERY crude!
Left limit first.
*/
local target = `ll0'-`z'^2/2 // pll value for computing likelihood based CI
if `Y'[1]<`target' {
/*
First evaluated pll is below target pll on left of mle.
Bracket target from already known values of pll and interpolate.
*/
forvalues i=2/`n' {
if `Y'[`i']>=`target' {
local left_limit = `X'[`i'-1]+`stepsize'*(`target'-`Y'[`i'-1])/(`Y'[`i']-`Y'[`i'-1])
continue, break // exit forvalues loop
}
}
}
else {
/*
Search for left ll-based confidence limit to the left of first value of b.
Requires new evaluations of ll - no more than `maxcost' allowed.
*/
local Yold = `Y'[1]
local bold `from'
local i 1
while missing(`left_limit') & `i'<=`maxcost' {
local A = `from'-`i'*`stepsize'
// evaluate pll
parsat `"`anything'"' `if' `in', formula(`formula') var(`xx') value(`A') placeholder(`placeholder')
cap `cmd' `r(result)' `if' `in' `wt', `options' `constant'
if _rc {
noi di as err "model fit failed at parameter = " `A'
exit 198
}
local Ynew = e(ll)
if `Ynew'<`target' { // now bracketing target
local left_limit = `bold'-`stepsize'*(`target'-`Yold')/(`Ynew'-`Yold')
}
else {
local Yold `Ynew'
local bold `A'
local ++i
}
if "`dots'"!="nodots" noi di "." _c
}
local cost `i'
if missing(`left_limit') noi di as txt _n "[note: failed to find left-hand confidence limit]"
}
/*
Now right limit
*/
if `Y'[`n']>`target' { // search for right_limit to the right of last value of b
local Yold = `Y'[`n']
local bold `to'
local i 1
while missing(`right_limit') & `i'<=`maxcost' {
local A = `to'+`i'*`stepsize'
// evaluate pll
parsat `"`anything'"' `if' `in', formula(`formula') var(`xx') value(`A') placeholder(`placeholder')
cap `cmd' `r(result)' `if' `in' `wt', `options' `constant'
if _rc {
noi di as err "model fit failed at parameter = " `A'
exit 198
}
local Ynew = e(ll)
if `Ynew'<`target' { // now bracketing target
local right_limit = `bold'+`stepsize'*(`target'-`Yold')/(`Ynew'-`Yold')
}
else {
local Yold `Ynew'
local bold `A'
local ++i
}
if "`dots'"!="nodots" noi di "." _c
}
local cost = `cost'+`i'
}
else { // pll is below target on right of mle, bracket target and interpolate
local n1 = `n'-1
forvalues i=`n1'(-1)1 {
if `Y'[`i']>=`target' {
local right_limit = `X'[`i']+`stepsize'*(`target'-`Y'[`i'])/(`Y'[`i'+1]-`Y'[`i'])
continue, break // exit forvalues loop
}
}
}
if missing(`right_limit') noi di as txt _n "[note: failed to find right-hand confidence limit]"
}
local se .
local llci .
local ulci .
lab var `X' "`placeholder' in `formula'"
}
}
// Pseudo-SE
*local pse = (`right_limit'-`left_limit')/(2*`z')
*capture drop `gen1'
*capture drop `gen2'
*rename `X' `gen1'
*rename `Y' `gen2'
*lab var `Y' "profile log likelihood function"
local ll_limit = `ll0'-`z'^2/2
local limit `ll_limit'
if "`difference'"!="" {
// compute difference, subtract ll0
qui replace `Y' = `Y'-`ll0'
*lab var `gen2' "profile log likelihood difference function"
local limit = -`z'^2/2
}
if "`deviance'"!="" {
qui replace `Y' = -2*`Y'
if "`difference'"!="" lab var `Y' "profile deviance difference function"
else lab var `Y' "profile deviance function"
local limit = -2*`limit'
}
/*
local asym = 100*((`right_limit'-`b0')-(`b0'-`left_limit'))/(`right_limit'-`left_limit')
if "`graph'"!="nograph" {
local Asym: display %4.1f `asym'
// Extract title if present - default involves asymmetry
_get_gropts , graphopts(`gropt') getallowed(title)
local gropt `s(graphopts)'
if `"`s(title)'"'=="" local title title("Asymmetry = `Asym'%")
else if `"`s(title)'"'==`""""' local title
else local title title(`s(title)')
if !missing(`left_limit') local lll `left_limit'
if !missing(`right_limit') local rrr `right_limit'
if ("`lll'`rrr'"!="") ///
local xl xline(`lll' `rrr', lstyle(ci) `cilines')
graph twoway line `gen2' `gen1', `gropt' `title' ///
`xl' yline(`limit', lstyle(refline) `levline')
}
if "`dots'"!="nodots" di
local tt "Coef."
di as txt _n "{hline 13}{c TT}{hline 47}"
di as txt %12s abbrev("`ytitle'",12) _col(14)"{c |}" ///
%10s "Coef." " Std. Err. [`level'% PLL Conf. Int.]"
di as txt "{hline 13}{c +}{hline 47}"
if "`eq'"!="" di as res %-12s abbrev("`eq'",12) _col(14) as txt "{c |}"
if "`formula'"!="" di as txt %12s "`placeholder'" _cont
else di as txt %12s abbrev("`profile'",12) _cont
di _col(14) "{c |}" as res ///
_col(16) %9.0g `b0' ///
_col(28) %9.0g `pse' ///
_col(41) %9.0g `left_limit' ///
_col(53) %9.0g `right_limit'
di as txt "{hline 13}{c BT}{hline 47}"
di as txt "Note: Std. Err. is pseudo standard error, derived from PLL CI"