. program define mycox1_d0
1.
. version 10.0
2.
. args todo b lnf
3.
. local t "$ML_y1" // $ML_y1 is -t
4.
. local d "$ML_y2" // $ML_y2 is d
5.
. tempvar xb B A sumd last L
6.
. mleval 'xb' = 'b'
7.
. // data assumed already sorted by 'negt' and 'd'
8.
. local byby "by 'negt' 'd'"
9.
. local wxb "$ML_w*'xb'"
10.
. quietly {
11.
. gen double 'B' = sum($ML_w*exp('xb'))
12.
. 'byby': gen double 'A' = cond(_n==_N, sum('wxb'),.) if 'd'==1
13.
. 'byby': gen 'sumd' = cond(_n==_N, sum($ML_w*'d'),.)
14.
. 'byby': gen byte 'last' = (_n==_N & 'd' == 1)
15.
. gen double 'L' = 'A' - 'sumd'*ln('B') if 'last'
16.
. mlsum 'lnf' = 'L' if 'last', noweight
17.
. }
18.
. end
然后导入data,就用stata自带的data做个测试,
sysuse cancer, clear
gen drug2 = drug==2
gen drug3 = drug==3
ml model d0 mycox_d0 ( studytime died = drug2 drug3 age, nocons)
ml maximize
跑不出来,提示我
invalid syntax
r(198);
然后我用
ml check
Test 1: Calling mycox1_d0 to check if it computes log likelihood and
does not alter coefficient vector...
FAILED; mycox1_d0 returned error 198.
Here is a trace of its execution:
------------------------------------------------------------------------------
-> mycox1_d0 0 ML_b ML_f __000003 __000004 __000001
- `begin'
= capture noisily version 10: mycox1_d0 0 ML_b ML_f __000003 __000004 __000001
-------------------------------------------------------------------------- begin mycox1_d0 ---
- version 10.0
- args todo b lnf
- local t "$ML_y1" // $ML_y1 is -t
= local t "studytime" // studytime is -t
invalid syntax
---------------------------------------------------------------------------- end mycox1_d0 ---
- `end'
= set trace off
------------------------------------------------------------------------------
Fix mycox1_d0.
r(198);
我看不懂问题在哪儿?希望知道的朋友指教。谢谢了。