stata里面最ML比较麻烦,不如sas。
下面是一个例子,
capture clear
set more 1 ******************the CES production functions ************* program define nlces if "`1'" == "?" { global S_1 "B0 A D R" global B0 = 1 global A =1 global D =0.3 global R =0.7 exit } replace `1' =$B0+$A*ln($D*labor^$R+(1-$D)*capital^$R) end
use d:\data\dta\ces.dta nl ces lnq
*quietly nl ces lnq
*set more 0
/* NL */
program define nlnexpgr if "`1'" == "?" { /* if query call ... */ global S_1 "B0 B1" /* declare parameters */ global B0=1 /* and initialize them */ global B1=.1 exit } replace `1'=$B0*(1-exp(-$B1*x)) /* otherwise, calculate function */ end
*use sasxmpl1, clear gen y=products gen x=labor nl nexpgr y
/* mL */ program define mlnexpgr version 7 args lnf B1x B0 sigma tempvar res qui gen double `res' = $ML_y1 - `B0'*(1-exp(-`B1x')) qui replace `lnf' = -0.5*ln(2*_pi)-ln(`sigma')-0.5*`res'^2/`sigma'^2 end
ml model lf mlnexpgr (B1: y = x, nocons) (B0:) (sigma:)
ml check ml search ml max
ml model lf mlnexpgr (B1: y = x, nocons) (B0:) (sigma:), robust ml check ml search ml max
*ml model lf mlnexpgr (B1: y = x, nocons) (B0:) (sigma:), cluster(group) *ml max