Announcement

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

  • ml for nonlinear function


    I have this log-likelihood function from a censored maximisation problem
    Click image for larger version

Name:	Capture_stata.PNG
Views:	1
Size:	31.8 KB
ID:	1708289


    with data on y_i, x_i, and z_i and want to have parameter estimates of \sigma, \lambda, \gamma_0, and \gamma_1. I wrote the following evaluator

    Code:
    cap program drop censored_likelihood
    program define censored_likelihood
        version 17.0
        args lnf sigma lambda gamma0 gamma1 y x z
        tempvar m epsilon
        local y "$ML_y1"
        gen double `m' = (1 + (`lambda' - 1) * `x' * (`gamma0' - exp(-`gamma1' * `z')))
        gen double `epsilon' = `y' - `m'
        qui replace `epsilon' = 0 if `y' == 0
        
        gen double Iy0 = (`y' == 0')
        gen double Iy010 = (`y' > 0 & `y' < 10)
        gen double Iy10 = (`y' == 10)
        
        
        replace `lnf' = -0.5 * ln(2*c(pi)*`sigma'^2) - (`y'-`m')^2/(2*`sigma'^2)  ///
                    + Iy0   * ln(normal(-`m'/`sigma')) ///
                    + Iy010 * ln(normal((10-`m')/`sigma') - normal(-`m'/`sigma')) ///
                    + Iy10  * ln(1 - normal((10-`m')/`sigma'))
        mlsum `lnf'
        
    end
    and then I don't get how to initialize it. How can I give the non-linear function and parameters to the ml model function?

    Here is example data

    Code:
    set seed 12553
    clear
    set obs 1000
    gen y = runiformint(0,10)
    gen x = runiformint(0,1)
    gen z = abs(rnormal() * 50)
    Last edited by Henry Strawforrd; 03 Apr 2023, 13:59.

  • #2
    Originally posted by Henry Strawforrd View Post
    I have this log-likelihood function . . . and want to have parameter estimates of \sigma, \lambda, \gamma_0, and \gamma_1. I wrote the following evaluator . . . and then I don't get how to initialize it. How can I give the non-linear function and parameters to the ml model function?
    Those are handled by ml model as constants in their own equations, separate from that of the predictors.

    Take a look at Example 2 on pp. 1601–2 of the entry for ml — Maximum likelihood estimation in the user's manual, particularly in the bottom half of p. 1601 where alternative ways in which to specify these constant parameters are shown.

    You can see another example in the StataCorp FAQ "How do I estimate a nonlinear model using ml?" here.

    Comment


    • #3
      Thanks! I wrote it like this now and at least it does something. Is the y = x z correct or will that give y = a +bx + cz?

      Code:
      ml model lf censored_likelihood (b1: y = x z, nocons) (lambda:) (gamma0:) (gamma1:) (sigma:)
      ml check
      ml maximize
      Last edited by Henry Strawforrd; 04 Apr 2023, 04:00.

      Comment


      • #4
        Originally posted by Henry Strawforrd View Post
        Is the y = x z correct or will that give y = a +bx + cz?
        The latter.

        And it will feed the linear predictor to your sigma parameter. You need to rearrange your evaluator command's argument list to match the order in which ml model will feed things to it.

        You might want to read through the user's manual's lf examples again, as well as the example in the company's FAQ, working through them slowly until you've got the gist of things.

        I also recommend the book that's mentioned in the help file. I really didn't feel comfortable that I could program these things until I'd gone through the book.

        Comment


        • #5
          Thanks! I am looking through both while trying to do this, but being a bit overwhelmed by the quantity and not finding very related passages to my problem.

          Comment

          Working...
          X