Announcement

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

  • using constraints in ML

    Dear Statalisters,

    I want to impose constraints in a self written ML model. I've looked how to do this on the internet but can't get it to work.

    This is what I tried::

    constraint define 1 [eq1:]x2=1

    ml model d0 cloglog_d0_hs (dead1=x2 delta) ///
    (dead2=x1 x2) /logitp1 /logitp2 /logitp3 /mu1 /mu2, constraint(1)

    ml init b1 b2 0 0 0 -0.5 -0.5, copy

    ml maximize, trace difficult search(norescale)


    no error messages but also no contraints imposed. Any suggestions?

    Regards,

    Marcel




















  • #2
    Without your evaluator and a working dataset, it is hard to tell you what to fix, but I
    imagine it is how you defined your constraint. It probably should be:

    Code:
    constraint define 1 [eq1]:x2=1
    Here is a working example using a simple d0 evaluator for liner regression:

    Code:
    program myreg_d0
            args todo b lnl
    
            tempname xb lnsigma
            mleval `xb' = `b', eq(1)
            mleval `lnsigma' = `b', eq(2)
    
            mlsum `lnl' = lnnormalden($ML_y1 - `xb', exp(`lnsigma'))
    end
    
    sysuse auto
    
    constraint 1 [xb]: turn = -.75
    
    ml model d0 myreg_d0 (xb: mpg = turn trunk) /lnsigma, constraint(1)
    ml init /lnsigma = 5.8
    ml init xb:_cons = 21.5
    ml max
    Here is the log from running the above code:

    Code:
    . program myreg_d0
      1.         args todo b lnl
      2. 
    .         tempname xb lnsigma
      3.         mleval `xb' = `b', eq(1)
      4.         mleval `lnsigma' = `b', eq(2)
      5. 
    .         mlsum `lnl' = lnnormalden($ML_y1 - `xb', exp(`lnsigma'))
      6. end
    
    . 
    . sysuse auto
    (1978 Automobile Data)
    
    . 
    . constraint 1 [xb]: turn = -.75
    
    . 
    . ml model d0 myreg_d0 (xb: mpg = turn trunk) /lnsigma, constraint(1)
    
    . ml init /lnsigma = 5.8
    
    . ml init xb:_cons = 21.5
    
    . ml max
    
    initial:       log likelihood = -497.21266
    rescale:       log likelihood = -298.76197
    rescale eq:    log likelihood = -242.60861
    Iteration 0:   log likelihood = -1985.0117  (not concave)
    Iteration 1:   log likelihood = -313.18718  (not concave)
    Iteration 2:   log likelihood = -244.96237  (not concave)
    Iteration 3:   log likelihood = -237.48694  (not concave)
    Iteration 4:   log likelihood = -232.63018  (not concave)
    Iteration 5:   log likelihood = -227.93703  
    Iteration 6:   log likelihood = -205.83078  
    Iteration 7:   log likelihood = -204.67864  
    Iteration 8:   log likelihood = -204.67749  
    Iteration 9:   log likelihood = -204.67749  
    
                                                      Number of obs   =         74
                                                      Wald chi2(1)    =       9.42
    Log likelihood = -204.67749                       Prob > chi2     =     0.0021
    
     ( 1)  [xb]turn = -.75
    ------------------------------------------------------------------------------
             mpg |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
    xb           |
            turn |       -.75  (constrained)
           trunk |  -.3229896   .1052305    -3.07   0.002    -.5292375   -.1167417
           _cons |   55.47707    1.51509    36.62   0.000     52.50755    58.44659
    -------------+----------------------------------------------------------------
    lnsigma      |
           _cons |   1.346974   .0821995    16.39   0.000     1.185866    1.508082
    ------------------------------------------------------------------------------

    Comment


    • #3
      As a sidelight, I often find that the coeflegend option can help you to get constraints right.

      Code:
      sysuse auto, clear
      logit foreign price mpg, coefl
      constraint 1 _b[mpg] = .25
      logit foreign price mpg, constraint(1)
      sysuse nhanes2f, clear
      mlogit health i.female, b(1) coefl
      constraint 1 _b[fair:1.female] = .40
      mlogit health i.female, b(1) constraint(1)
      -------------------------------------------
      Richard Williams, Notre Dame Dept of Sociology
      StataNow Version: 19.5 MP (2 processor)

      EMAIL: [email protected]
      WWW: https://academicweb.nd.edu/~rwilliam/

      Comment

      Working...
      X