Announcement

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

  • Estimate a non linear least square using Mata

    Dear all:

    I have the next issue, I want to estimate a non lineal least square, for that use the next "program"

    Code:
    program nlequation1, rclass
    
        version 13
        syntax varlist(min=2 max=2) if
        local y : word 1 of `varlist'
        local x : word 2 of `varlist'
      
        return local eq "`y' = {b0} + (`x')^{b1}"
        return local title "Non Linear Least Square Equation"
        
    end
    
    nl equation1: clarity quality
    nl equation1: clarity quality , initial(b0  b1 )
    but now i would like to do the same but with Mata, using Gauss Newton Method. i donĀ“t know how to start programming.

    Kind regards

  • #2
    see this post

    Comment


    • #3
      Thanks , I read the post and I have some question. First how can write "`y' = {b0} + (`x')^{b1}" , because the example of the post uses exp(Xb) who is linear in Xb, but in my case bo is linear and b1 is non linear. Second, what is todo in the code.

      Thanks again.

      Comment


      • #4
        todo is for how the gradient and the Hessian are computed (numerically or analytically).

        0= g and H are both computed numerically (lazy, Stata does it for you)
        1=g analytically, H numerically (require some work)
        2 = g and H are both computed analytically and provided by the user (may require a lot of work and debugging :-))

        See the help optimize() for more details or this magnificent book

        You can modify the evaluator in this way. It wil work only with one covariate though, but generalizing the model to more than one covariate shouldn't be too hard. Computing the gradient and the Hessian will require a bit of work.

        Code:
        void MYNLExp(real scalar todo, real vector b,   ///
                real vector y, real matrix X,           ///
                val, grad, hess)
        {
                real vector  r, f
                real scalar b0, b1
                real matrix  df
         
                b0 = b[1,1]
                b1 = b[1,2]
        
                f   = exp(b0 :+ X:^b1)
                r   = y - f
                val = -(r:^2)
         
                ...
        }

        Comment

        Working...
        X