Skip to Main Content

Using ml commands to maximize a user specified likelihood function | Stata Code Fragments

* This code produces an ordered logit model for a y variable with 3 levels.

* First write what Stata calls the evaluator 
program myologit

version 9
args lnf xb a1 a2
* The contribution to the likelihood at each level of y        
quietly    replace `lnf' = ln(1/(1+exp(-`a1' + `xb')))                          if $ML_y1 == 1
quietly    replace `lnf' = ln(1/(1+exp(-`a2'+ `xb')) - 1/(1+exp(-`a1' + `xb'))) if $ML_y1 == 2
quietly    replace `lnf' = ln(1 - 1/(1+exp(-`a2'+ `xb')))                       if $ML_y1 == 3

end

use https://stats.idre.ucla.edu/stat/stata/notes/hsb2, clear

* specify the method (lf) and the name of your evaluator (myologit)
* followed by the equation(s) in parantheses and then the cutpoints.
ml model lf myologit (xb: ses = female math write, nocons ) /a1 /a2
ml check
ml search
ml maximize 

* You can check a simplified version of your model against the results
* from Stata, before writing a program to run a more complicated model.
ologit ses female math write