I have written a Stata maximum likelihood routine to mimic stpm2, a user contributed routine to estimate survival models with restricted cubic splines. My routine reproduces results from stpm2. I have rewritten my routine with Mata using ml model lf as a shell. When I use results from stpm2 as starting values, and set ml maximize, difficult iterate(0) search(off), I get the correct likelihood, suggesting to me that my likelihood function is written correctly. But if I use these starting values and allow the program to iterate, I cannot achieve convergence. Nor can I achieve convergence if I use different starting values. Any suggestions for a next step?
mata:
void my_po_lf(transmorphic scalar ML, real rowvector b, real colvector lnfj)
{
real vector event
real vector XB, first_sum, second_sum, survivor, hazard
real vector parmindex, parms
real matrix DER
event = moptimize_util_depvar(ML,1)
XB = moptimize_util_xb(ML,b,1)
first_sum = moptimize_util_xb(ML,b,2)
parmindex = moptimize_util_eq_indices(ML, 2)
parms = b[parmindex[1,2]..parmindex[2,2]]
parms = parms[1,1..4]'
DER = st_data(.,"drcs1 drcs2 drcs3 drcs4")
second_sum = DER * parms
survivor = (1 :+ exp(first_sum) :* exp(XB)) :^ (-1)
hazard = survivor :* (exp(first_sum) :* exp(XB)) :* second_sum
lnfj = ((event :== 1) :* ln(hazard)) :+ ln(survivor)
}
end
ml maximize, difficult
mata:
void my_po_lf(transmorphic scalar ML, real rowvector b, real colvector lnfj)
{
real vector event
real vector XB, first_sum, second_sum, survivor, hazard
real vector parmindex, parms
real matrix DER
event = moptimize_util_depvar(ML,1)
XB = moptimize_util_xb(ML,b,1)
first_sum = moptimize_util_xb(ML,b,2)
parmindex = moptimize_util_eq_indices(ML, 2)
parms = b[parmindex[1,2]..parmindex[2,2]]
parms = parms[1,1..4]'
DER = st_data(.,"drcs1 drcs2 drcs3 drcs4")
second_sum = DER * parms
survivor = (1 :+ exp(first_sum) :* exp(XB)) :^ (-1)
hazard = survivor :* (exp(first_sum) :* exp(XB)) :* second_sum
lnfj = ((event :== 1) :* ln(hazard)) :+ ln(survivor)
}
end
ml maximize, difficult
Comment