* Possion model using d2 method * use the data of stata official website * method d2 requires explicit expression for gradient and Hessian clear use http://www.stata-press.com/data/r8/airline list gen lnN=ln(n) * first use canned routine poisson injuries XYZowned lnN * user-defined procedure with method d2 program drop _all program define MLE_d2_poisson version 8 args todo b lnf g negH tempvar t1 mleval `t1' = `b', eq(1) mlsum `lnf'=-exp(`t1')+`t1'*$ML_y1-lngamma($ML_y1+1) if (`todo'==1|`lnf'>=.) exit tempname d1 mlvecsum `lnf' `d1'=-exp(`t1')+$ML_y1, eq(1) matrix `g'=(`d1') if (`todo'==1|`lnf'>=.) exit tempname d11 mlmatsum `lnf' `d11'=exp(`t1'), eq(1) matrix `negH'=(`d11') end ml model d2 MLE_d2_poisson (injuries = XYZowned lnN) ml maximize