Announcement

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

  • how to loop two parameters for self-build maximum likelihood program?

    Dear all,

    I'd like loop both d and theta (i.e., d=0.1(0.05)0.2, and theta=1(0.1)2) for the following program myest. I tried "forvalues after program" and "program within forvalues". Neither works.
    Would you please help to figure it out? Thank you.


    *****code as below
    capture program drop myest
    program define myest
    version 14
    args lnf Xb
    tempvar d theta
    quietly generate double `d'=0.1
    quietly generate double `theta'=1
    quietly replace `lnf' = ln(normal((ln(300/((1-exp(-1*`d'-0.1))/(`d'+0.1)*exp(-1*`d'*2)))-`Xb')/`theta')) if $ML_y1==2
    quietly replace `lnf' = ln(1-normal((ln(300/((1-exp(-1*`d'-0.1))/(`d'+0.1)*exp(-1*`d'*19)))-`Xb')/`theta')) if $ML_y1==20
    quietly replace `lnf' = ln(normal((ln(300/exp(-1*$ML_y1*`d')*((1-exp(-1*`d'-0.1))/(`d'+0.1)))-`Xb')/`theta')-normal((ln(300/exp(-1*($ML_y1-1)*`d')*((1-exp(-1*`d'-0.1))/(`d'+0.1)))-`Xb')/`theta')) if ($ML_y1<20)&($ML_y1>2)
    end

    qui ml model lf myest (y= x1 x2)
    qui ml check
    qui ml maximize
    display e(ll)

  • #2
    Code:
    forv d = 0.1(0.05)0.2 {
         forv theta = 1(0.1)2)
         *do stuff with `d' and `theta'
    }
    }

    Comment


    • #3
      Perhaps it’s better to ask…what is the goal?
      are d and theta added parameters for maximization?
      or do you want to see sensitive to them
      that may help giving a good answer

      Comment


      • #4
        Originally posted by George Ford View Post
        Code:
        forv d = 0.1(0.05)0.2 {
        forv theta = 1(0.1)2)
        *do stuff with `d' and `theta'
        }
        }

        Thank you George for your prompt response. I tried similar coding as below, but the STATA report r(100) "varlist required".

        capture program drop myest
        program define myest
        version 14
        args lnf Xb
        tempvar dd theta
        quietly replace `lnf' = ln(normal((ln(300/((1-exp(-1*`dd'-0.1))/(`dd'+0.1)*exp(-1*`dd'*2)))-`Xb')/`theta')) if $ML_y1==2
        quietly replace `lnf' = ln(1-normal((ln(300/((1-exp(-1*`dd'-0.1))/(`dd'+0.1)*exp(-1*`dd'*19)))-`Xb')/`theta')) if $ML_y1==20
        quietly replace `lnf' = ln(normal((ln(300/exp(-1*$ML_y1*`dd')*((1-exp(-1*`dd'-0.1))/(`dd'+0.1)))-`Xb')/`theta')-normal((ln(300/exp(-1*($ML_y1-1)*`dd')*((1-exp(-1*`dd'-0.1))/(`dd'+0.1)))-`Xb')/`theta')) if ($ML_y1<20)&($ML_y1>2)
        end

        forvalues dd = 0.1(0.05)0.2 {
        forvalues theta = 1(0.1)2 {
        myest
        ml model lf myest (y1= x1 x2)
        ml maximize
        display `dd'
        display `theta'
        display e(ll)
        }
        }



        Comment


        • #5
          Originally posted by FernandoRios View Post
          Perhaps it’s better to ask…what is the goal?
          are d and theta added parameters for maximization?
          or do you want to see sensitive to them
          that may help giving a good answer
          Thank you Fernando. You made a great point.
          The goal is to estimate d, theta, and b(the coeffcients) together from maximizing the log-likelihood function. However, by Program myest, I only can estimate b(the coeffcients) given the value of d and theta. Therefore, as a second best way, I'd explore the space of d and theta (d=0.1(0.05)0.2, theta=1(0.1)2), and check which combination of d and theta has the highest log-likelihood value.

          Comment


          • #6
            I see
            In that case, my suggestion is
            1) modify your code so all parameters (d theta and b) are estimated in the model.
            2) use constraints to change the values for d and theta

            F

            Comment


            • #7
              I can't figure out what you're up to, but there are some issues I see.

              First, the tempvar line is resetting `dd' and `theta' as tempvars (so you lose the loop values).

              Second, you need to send `dd' and `theta' as arguments.


              Code:
              capture program drop myest
              program define myest
              version 14
              args dd theta
              di `dd' _col(20) `theta'
              end
              
              forvalues dd = 0.1(0.05)0.2 {
                   forvalues theta = 1(0.1)2 {
                        myest `dd' `theta'
              }
              }
              Third, I'm not sure what you're doing with myest in the -ml model- line; you're calling a program. Where does `Inf' fit into this?


              Comment

              Working...
              X