Announcement

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

  • r(199) error in function evaluator program with nl

    Dear stata list users,

    I want to estimate a non-linear least squares model but seem to be unable to create a function evaluator program.
    The Name of my program is myreg.
    The error I get is the following:

    nlmyreg returned 199
    verify that nlmyreg is a function evaluator program



    I attach a sample of my data that contains my LHS variable, rel_estab_size_c_interp, and the RHS variable, ageq.


    Here is the program :


    nl myreg @ rel_estab_size_c_interp ageq, parameters(gg a2 a3 q delta zeta) initial(gg 1.0008873 a2 0.5 a3 0.3 q 20 delta 0.018 zeta 2.8)


    program nlmyreg
    version 12
    syntax varlist (min=2) if, at(name)
    local rel_estab_size_c_interp: word 1 of `varlist'
    local ageq: word 2 of `varlist'
    // Retrieve parameters
    tempname gg a2 a3 q delta zeta
    scalar `gg' = `at'[1,1]
    scalar `a2' = `at'[1,2]
    scalar `a3' = `at'[1,3]
    scalar `q' = `at'[1,4]
    scalar `delta' = `at'[1,5]
    scalar `zeta' = `at'[1,6]
    // Calculate correction factor
    matrix define z = J(`q',2,0)
    matlist z
    forval s = 0/`q' {
    z[s+1,2] = ( `gg'^s / ( 1 + `a2' * `a3'^s) )^`zeta'
    z[s+1,1] = `delta' * (1-`delta')^s
    }
    generate double `size_avg_estab' = sum(z(:,1).*z(:,2))/sum(z(:,1)) `if'
    // Fill in dependent variable
    replace `rel_estab_size_c_interp' = (((`gg'^`ageq')/(1+`a2'*`a3'^`ageq'))^`zeta'))/`size_avg_estab' `if'
    end



    Thanks in advance for any help.
    Best,
    Eniko
    Attached Files
    Last edited by Eniko Gabor; 15 Oct 2015, 02:27.

  • #2
    At a casual glance I see several bugs here

    You need to focus first on

    Code:
    z[s+1,2] = ( `gg'^s / ( 1 + `a2' * `a3'^s) )^`zeta'
    z[s+1,1] = `delta' * (1-`delta')^s
    which certainly aren't legal Stata commands.

    As s is a local macro defined for the loop, it needs to be referenced as such.

    Those commands need minimally a command name.

    I don't know exactly what the following generate command is trying to do, but the matrix references use the wrong punctuation and the Stata function sum() gives a cumulative sum over observations; it won't give totals of vectors on the fly.

    You seem to be mixing Mata and Stata syntax. In fact I sniff some MATLAB syntax in there too.

    It may not be possible, but at this level finding someone local who knows more Stata than you do and is willing to give you half a hour can be more efficient than debugging programs line by line over the internet.

    Comment


    • #3
      Nick, thank you for your comments.
      OK, I will look at the code lines you suggested.
      Meanwhile I also tried to run the code without the Loop, matrices, local macro s, and it resulted in the same error message.

      nl myreg @ rel_estab_size_c_interp ageq, parameters(gg a2 a3 q delta zeta) initial(gg 1.0008873 a2 0.5 a3 0.3 q 20 delta 0.018 zeta 2.8)

      program nlmyreg
      version 12
      syntax varlist (min=2) if, at(name)
      local rel_estab_size_c_interp: word 1 of `varlist'
      local ageq: word 2 of `varlist'
      // Retrieve parameters
      tempname gg a2 a3 q delta zeta
      scalar `gg' = `at'[1,1]
      scalar `a2' = `at'[1,2]
      scalar `a3' = `at'[1,3]
      scalar `q' = `at'[1,4]
      scalar `delta' = `at'[1,5]
      scalar `zeta' = `at'[1,6]

      // Now fill in dependent variable
      replace `rel_estab_size_c_interp' = (((`gg'^`ageq')/(1+`a2'*`a3'^`ageq'))^`zeta')) `if'
      end

      Comment


      • #4
        Your program requires an at() option but you do not give one.

        Comment


        • #5
          I only Need to add the at() Option if I want to run the program interactively. Running it interactively will only evaluate the program once. Like it is explained here (and if I understand it correctly):

          http://blog.stata.com/2011/12/05/a-t...#disqus_thread

          Still, running it interactively was useful enough to find out that I had to many ")" in the line:

          replace `rel_estab_size_c_interp' = (((`gg'^`ageq')/(1+`a2'*`a3'^`ageq'))^`zeta')) `if'

          Correcting for the brackets, the reduced code below runs (convergence not achieved but that is a separate issue, not relevant here).

          program nlmyreg
          version 12
          syntax varlist (min=2) if, at(name)
          local rel_estab_size_c_interp: word 1 of `varlist'
          local ageq: word 2 of `varlist'
          // Retrieve parameters
          tempname gg a2 a3 q delta zeta
          scalar `gg' = `at'[1,1]
          scalar `a2' = `at'[1,2]
          scalar `a3' = `at'[1,3]
          scalar `q' = `at'[1,4]
          scalar `delta' = `at'[1,5]
          scalar `zeta' = `at'[1,6]

          // Now fill in dependent variable
          replace `rel_estab_size_c_interp' = ((`gg'^`ageq')/(1+`a2'*`a3'^`ageq'))^`zeta' `if'
          end


          nl myreg @ rel_estab_size_c_interp ageq, parameters(gg a2 a3 q delta zeta) initial(gg 1.0008873 a2 0.5 a3 0.3 q 20 delta 0.018 zeta 2.8)




          So, what is left for me is to figure out this part from my original program (already corrected for the local macro s):

          matrix define z = J(`q',2,0)
          matlist z
          forval s = 0/`q' {
          z[`s'+1,2] = ( `gg'^`s' / ( 1 + `a2' * `a3'^`s') )^`zeta'
          z[`s'+1,1] = `delta' * (1-`delta')^`s'
          }
          generate double `size_avg_estab' = sum(z(:,1).*z(:,2))/sum(z(:,1)) `if'

          Comment

          Working...
          X