Announcement

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

  • #16
    Hi Joro
    Just one point on the Example that you posted.
    This code
    nl (y = {a} * x^max(3, {c}))

    only constrains that the max(3, {c}) can have a value of 3 or above. It make any constraints on {c}.
    Also, when {c} is less than 3, the function doesn't change with respect to {c}, which may be why Stata is assuming {c} is constrained to its initial value of 0.

    Fernando

    Comment


    • #17
      Nick Cox Joro Kolev



      Thank you for all your comments, they have been very helpful to me. Regarding this question, today I tried to estimate the same nonlinear equation by maximum likelihood. In fact, the I wanted the nl estimation to be sure that the maximum likelihood performs correctly. Â The code I use is the following:




      Code:
      capture program drop mymodel_gf0
      
      program mymodel_gf0
      
      
      
      
          args todo b lnfj
      
          tempvar xb
      
          quietly generate double `xb' =`b’[1,1]*((x)^(`b'[1,2]-1)) 
      
          quietly replace `lnfj' = ln(normalden((y - `b')/`b'[1,3])) - ln(`b'[1,3])
      
      end
      
      
      
      
      ml model gf0 mymodel_gf0 (y = x) /sigma
      
      
      
      
      ml maximize






      However I can't get the program to run. Is it because stata assumes this is a linear equation? How can I correct this?







      Thank you very much for your help!




      Viridiana

      Comment


      • #18
        That's really a new question which should start a new thread. More crucially, you don't supply a reproducible example. However, the code looks very confused to me.

        If local macro b contains the name of a matrix you supply, then you can refer to particular elements but you can't in the same code subtract the matrix from your outcome variable.

        Further, I don't get any sense that this code would iterate as every time you call it within ml he result would be the same.

        I think you're confusing nl programming and ml programming. There is scope for initializing parameters in ml, but the mechanism is different.

        But, but, but: The biggest deal of all here is the idea. I don't see why maximum likelihood should give a different answer here any way if you're assuming normal errors.

        Comment


        • #19
          Here is the same model with the data Nick generated estimated by maximum likelihood. The recently introduced command -mlexp- makes estimation of maximum likelihood models a lot easier:

          Code:
          . nl (y = {a} * x^{c}), nolog
          (obs = 100)
          
          
                Source |      SS            df       MS
          -------------+----------------------------------    Number of obs =        100
                 Model |  237913.27          2  118956.634    R-squared     =     0.9996
              Residual |  94.467737         98  .963956498    Adj R-squared =     0.9996
          -------------+----------------------------------    Root MSE      =   .9818129
                 Total |  238007.74        100  2380.07735    Res. dev.     =   278.0965
          
          ------------------------------------------------------------------------------
                     y |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
          -------------+----------------------------------------------------------------
                    /a |   3.016182   .0613011    49.20   0.000     2.894532    3.137831
                    /c |   .6979471   .0048235   144.70   0.000     .6883749    .7075192
          ------------------------------------------------------------------------------
          
          . mlexp (ln(normalden(y, {b}*x^{c}, {sigma}))), nolog
          initial:       log likelihood =     -<inf>  (could not be evaluated)
          feasible:      log likelihood = -16142.302
          rescale:       log likelihood = -16142.302
          rescale eq:    log likelihood = -392.11126
          
          Maximum likelihood estimation
          
          Log likelihood = -139.04826                     Number of obs     =        100
          
          ------------------------------------------------------------------------------
                       |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
          -------------+----------------------------------------------------------------
                    /b |   3.016181   .0609311    49.50   0.000     2.896759    3.135604
                    /c |   .6979471   .0047947   145.57   0.000     .6885497    .7073444
                /sigma |   .9719452   .0687269    14.14   0.000     .8372429    1.106647
          ------------------------------------------------------------------------------
          The results are the same as from the NLS.

          Comment


          • #20
            Thank you, Fernando ! You resolved the mystery of what Stata is doing, and why Stata thinks that c is a constant.

            Unfortunately the two methods of imposing the inequality constraint that c>1 do not lead to the same results. Here are the results with Nick's data:

            Code:
            . nl (y = {a} * x^max(1,{c=3})), nolog
            (obs = 100)
            
            
                  Source |      SS            df       MS
            -------------+----------------------------------    Number of obs =        100
                   Model |  36030.414          0           .    R-squared     =     0.9219
                Residual |  3052.1515         99  30.8298129    Adj R-squared =     0.9219
            -------------+----------------------------------    Root MSE      =    5.55246
                   Total |  39082.566         99  394.773392    Res. dev.     =   625.6309
            
            ------------------------------------------------------------------------------
                       y |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
            -------------+----------------------------------------------------------------
                      /a |   .8333161   .0095456    87.30   0.000     .8143756    .8522566
                      /c |  -.0433347          .        .       .            .           .
            ------------------------------------------------------------------------------
              Parameter c taken as constant term in model & ANOVA table
            
            . nl (y = {a} * x^(1+exp({lnc}))), nolog
            (obs = 100)
            
            
                  Source |      SS            df       MS
            -------------+----------------------------------    Number of obs =        100
                   Model |  35808.757          1  35808.7575    R-squared     =     0.9162
                Residual |  3273.8083         98  33.4062071    Adj R-squared =     0.9154
            -------------+----------------------------------    Root MSE      =    5.77981
                   Total |  39082.566         99  394.773392    Res. dev.     =   632.6416
            
            ------------------------------------------------------------------------------
                       y |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
            -------------+----------------------------------------------------------------
                      /a |   .8589113   .0389093    22.07   0.000     .7816971    .9361254
                    /lnc |  -25.45258   4.80e+08    -0.00   1.000    -9.52e+08    9.52e+08
            ------------------------------------------------------------------------------
              Parameter lnc taken as constant term in model & ANOVA table
            
            . nlcom 1+exp( _b[lnc:_cons])
            
                   _nl_1:  1+exp( _b[lnc:_cons])
            
            ------------------------------------------------------------------------------
                       y |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
            -------------+----------------------------------------------------------------
                   _nl_1 |          1          .        .       .            .           .
            ------------------------------------------------------------------------------
            
            .
            Do you see me doing anything wrong? Or these are numerical problems?

            Originally posted by FernandoRios View Post
            Hi Joro
            Just one point on the Example that you posted.
            This code
            nl (y = {a} * x^max(3, {c}))

            only constrains that the max(3, {c}) can have a value of 3 or above. It make any constraints on {c}.
            Also, when {c} is less than 3, the function doesn't change with respect to {c}, which may be why Stata is assuming {c} is constrained to its initial value of 0.

            Fernando

            Comment


            • #21
              Hi Joro
              I would say that the difference comes from numerical differences.
              The way I see it, for example, is that neither is imposing an specific constraint on {c}. But on the power of "x".
              In the first example, (with a minor variable change)
              x^{d}
              {d}=max(1,{c=3})
              the constrain is that one is imposing the restriction that {d} >=1. if the true solution was that d<1, (based on how the data was created), it implies that a solution is for c<=1.
              Because the initial value being used is c=3, NL algorithm is looking for the closest numerical solution towards c<1. And It seems that the last solution was to use c=-.04.

              In the second case, the restriction is more explicit, since for any c> -infinity. then d>>1
              In this case, the closest numerical solution is c=-25. And at that value d=1 (as you show)

              I would also say that at the boundaries of the solution, neither function is continuous, which is why both approaches give slightly different results.
              F

              Comment

              Working...
              X