Announcement

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

  • nonlinear nl estimation - error #130 occurred in evaluating expression expression too long

    Hello I am faced with this problem - I have too many parameters apparently.

    Say I want to use nl command ( to do a simple linear estimation with 1000 variables):
    If I put
    nl(y= {Xb: var1-var1000}) for example
    I will also get this error:

    error #130 occurred in evaluating expression
    expression too long

    I have a model with many dummy variables.
    How did you fix this? I am guessing the limit should be removed somehow?

    Thanks

    Simeon

  • #2
    It is difficult, no impossible, for nl to work out what you want. Your syntax does not match any of the allowed syntaxes, but seems to be some sort of wild guess at what you imagine the syntax might be. Stata is confused and giving you an error message that doesn't make much sense, but that can happen when your syntax is very wrong.

    In any case, why are you trying to use nl when you have a "simple linear estimation with 1000 variables"? That's a job for regress, although I doubt that anything is simple in practice when using 1000 predictors.
    Last edited by Nick Cox; 07 May 2014, 04:49.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      It is difficult, no impossible, for nl to work out what you want. Your syntax does not match any of the allowed syntaxes, but seems to be some sort of wild guess at what you imagine the syntax might be. Stata is confused and giving you an error message that doesn't make much sense, but that can happen when your syntax is very wrong.

      In any case, why are you trying to use nl when you have a "simple linear estimation with 1000 variables"? That's a job for regress, although I doubt that anything is simple in practice when using 1000 predictors.
      Thanks a lot for this response I really appreciate your willingness to help.

      nl(y= {Xb: var1-var1000}) should be a shortcut for nl(y= {a1}*var1 + {a2}*var2 + ..... {a1000}*var1000)
      Also nl(y= {Xb: var1-var100}) works fine but if the number of parameters/variables is more then say around 200 I get the error.

      Anyways my model is mostly linear - I just have a nonlinear term added. It is Y = XB + aR^b*Z^c + e. So I have the nonlinear b and c to estimate, and a whole bunch of mostly dummy variables added.

      Comment


      • #4
        You're right: that is legal syntax and so my interpretation in my first paragraph was wrong, and sorry about that.

        You didn't explain about the nonlinear terms in your first posting, so my second paragraph turns out to be irrelevant.

        But in addition to the problem you have experienced, I don't see a way to add nonlinear terms to your model using the same syntax.

        Comment


        • #5
          Hi Zues,
          I see that in your model, the last section aR^b*Z^c has a very close resemblance to a standard CobbDouglas function. If that is the case, and unless your model specifically requires the non linearity, you could re specify and linearize your model:
          ln y=A+XB+b*lnR+c*lnZ+e
          So the dummy variables and other control variables can be assumed to affect the outcome via the "a" parameter, where a=exp(XB+A).
          Now, if the dummies are created based on a single category (aka are mutually exclusive as in one dummy state), you can explore estimating the model either using panel data or the areg command.
          Hope this helps
          Fernando

          Comment


          • #6
            Thanks a lot for these quick responses I was not expecting this.

            I was not sure whether I should give all the details about my regression I just had the question of why there was this limit to the number of parameters.

            Anyways the regression is panel - there is no crazy nonlinear function or anything. So I have Y = XB + aR^b*Z^c + u_i + e_it. There are a few hundred groups, but my dummies are not for the DVLS to remove the u_i. I've concluded to use first differences for that. The dummies are indicator and mutually exclusive for something else.

            So I have the interaction R*Z (raised to the parameter powers) , also 5 regular variables call them x1..x5, and the dummies d1..d1000.

            The original model is mostly log linear. except the R*Z term

            It is: Y = exp(b1*d1 + ... b1000*d1000) * X1^a1*...*X5^a5 * exp(aR^b*Z^c)

            Then taking logs I can estimate. Doing a Box - Cox transformation on the R and Z below does not change the results but smooths out the calculation and makes it much easier for Stata.

            nl(lnY= {a1}*(R^{a3}-1)/{a3}*(Z^{a2}-1)/{a2} + {Xb: lnx1 lnx2 lnx3 lnx4 lnx5 d1-d100}), initial(a1 1 a2 1 a3 1)

            So the above works and the interaction must be added before the {XB: .. } term - ( for some reason if I add the interaction variables after the {XB: .. } they don't work.)

            but with more dummies:

            nl(lnY= {a1}*(R^{a3}-1)/{a3}*(Z^{a2}-1)/{a2} + {Xb: lnx1 lnx2 lnx3 lnx4 lnx5 d1-d200}), initial(a1 1 a2 1 a3 1)

            Does not work

            Comment


            • #7
              I see the problem. So first, My first advice will not be appropriate as you want to maintain the aR^b*Z^c non linear.
              Now for the estimation itself, I think you might be running into your current Stata restrictions set up. For instance, I think Stata uses a matrix of size 200 as the default maximum. Thus larger number of parameters cannot be estimated. So, after this is said. you should try the following:
              set matsize 2000
              and then run your model.
              Perhaps that will work.
              Fernando

              Comment


              • #8
                It still doesn't work.
                These matsize is more or less the size of the X matrix using linear regression. But perhaps there are some other options for nonlinear regression.
                Last edited by sladmin; 13 May 2014, 09:05. Reason: Edited for language.

                Comment


                • #9
                  Originally posted by Nick Cox View Post
                  You're right: that is legal syntax and so my interpretation in my first paragraph was wrong, and sorry about that.

                  You didn't explain about the nonlinear terms in your first posting, so my second paragraph turns out to be irrelevant.

                  But in addition to the problem you have experienced, I don't see a way to add nonlinear terms to your model using the same syntax.
                  I also tried this with gmm (nonlinear IV) and again there appears to be this same limit of about 160 parameters or so.

                  Comment


                  • #10

                    Simeon wants to fit a nonlinear model with 1000 covariates, which
                    results in an expression that is too long to be processed by -nl-
                    in the interactive form.

                    We will look into expanding the allowed size of expressions. Such
                    a change could not be made soon.

                    Simeon may want to create a function evaluator program, which
                    allows us to break the expression into smaller parts. In the following
                    example, I will use a function evaluator program for the
                    model

                    y = x1+... + x100 + z^k + b0 + epsilon
                    Code:
                    clear
                    program drop _all
                    set more off
                    set seed 1357
                    
                    *create covariates and a dependent variable
                    set obs 2000
                    generate y = 1
                    forvalues i = 1(1)100{
                      quietly  generate x`i' = rnormal()
                      quietly replace y = y + x`i'
                    }
                    generate z = abs(rnormal())
                    replace y = y + z^2
                    replace y = y + rnormal()
                    
                    *create the evaulator program
                    
                    program nlmyeval
                      version 13
                      syntax varlist [if], at(name)
                      local dvar: word 1 of `varlist'
                      tempvar my_y
                      gen double `my_y' = 0 `if'
                      forvalues i = 1(1)100{
                            tempname b
                            scalar `b' = `at'[1,`i']
                            replace  `my_y' = `my_y' + `b'*x`i'  `if'
                      }
                      tempname b0 k
                      scalar `k' = `at'[1,101]
                      scalar `b0' = `at'[1,102]
                      replace `my_y' = `my_y' + z^`k' `if'
                      replace `my_y' = `my_y' + `b0' `if'
                      replace `dvar' = `my_y' `if'
                    end
                    
                    *call -nl- with the evaluator program
                    mat M = J(1,102,1)
                    nl myeval @ y, nparameters(102) initial(M)
                    Simeon can read more about function evaluation programs in the
                    entry for -nl- in the [R] Manual.

                    --Isabel

                    Comment


                    • #11
                      Thanks a lot of this response Isabel. I will let you know if I have any questions.

                      Comment


                      • #12
                        Hi!

                        I hope someone will see this very late response to this question since I'm currently trying to create a function evaluator program that will solve a similar model as discussed above. The reason that I need to use a function evaluator program is I have many variables (450+) and normal nl command gives "expression too long", same as for Simeon above. Isabels answer has helped me alot in developing my program but I need to use weights as well as normalize a few variables to 1.

                        Can anyone help me with this?

                        Thanks,

                        Susanna

                        Comment

                        Working...
                        X