Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • ML evaluator crashes if it calls a built-in maximum likelihood function

    Suppose I want to run a maximum likelihood program that calls another maximum likelihood program. (For example: suppose I have two equations and one of them is essentially an ordered logit, which is rather difficult to program from scratch, as numerical approximations make an lf0 estimator take forever.) So I write something like

    Code:
    program define myeval
      args lnfj Xb
      ologit $ML_y1 `Xb'
      tempvar lnfj_part1
      predict `lnfj_part1', pr
      ...
      replace `lnfj' = log(`lnfj_part1')+...
    end
    This will produce the output:

    Code:
    variable _MLtua1 not found
                   st_data():  3500  invalid Stata variable name
         mopt__st_user_lf1():     -  function returned error
        mopt__calluser_lf2():     -  function returned error
          opt__eval_nr_lf1():     -  function returned error
                 opt__eval():     -  function returned error
        _optimize_evaluate():     -  function returned error
           _mopt__evaluate():     -  function returned error
       _moptimize_evaluate():     -  function returned error
         _moptimize_search():     -  function returned error
               Mopt_search():     -  function returned error
                     <istmt>:     -  function returned error
    r(3500);
    I'm pretty sure the reason is that ml state is stored in global variables, which are overwritten when ologit is called. Is this correct? Does this mean that one can never use an ml estimation command within another ml evaluator, or is there some kind of workaround?

  • #2
    See the help file for ml hold.

    I think the myeval program should be modified as follows:
    Code:
    program define myeval
      args lnfj Xb
      local y1 $ML_y1
      ml hold
      ologit `y1' `Xb'
      ml untold
      tempvar lnfj_part1
      predict `lnfj_part1', pr
      ...
      replace `lnfj' = log(`lnfj_part1')+...
    end

    Comment

    Working...
    X