Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: nl estimation: Error #130 Expression too long

From   "Brian P. Poi" <>
Subject   Re: st: nl estimation: Error #130 Expression too long
Date   Sat, 18 Jun 2011 09:30:43 -0400

On 06/18/2011 12:30 AM, Eugene Bempong Nyantakyi wrote:
I am trying to estimate a nonlinear least square equation but I keep on getting this error  saying "Expression too long" Here is the equation I am trying to estimate

nl (lgexp = log(exp({w_hat = 0.1 }*(z_hat + eta_hat)) - 1) + 1*{xb: ln_distance border n_island n_landlock legalsystem_same common_lang colonial cu fta religion_same eta_hat dum*}) if commcode == 1&  eta_hat != ., vce(cluster pairid)  no constant

dum* is a wildcard for  316 fixed effects. I have two sets of fixed effects exporter fixed effects  : dumexporter1 + dumexporter2 + dumexporter3 + ... + dumexporter158 and importer fixed effects: dumimporter1 + dumimporter2 + dumimporter3 + … + dumimporter158. so I use dum* to include all of them in the linear part of the equation. However when I include the dummies with dum* I get the error code that expression is too long and when I exclude them I get some results but I need to include them in the model. Can anybody help me with the programming. I really need help on this.

Eugene Kwasi


The solution to "expression to long" errors with -nl- is to write a function evaluator program to calculate your nonlinear function rather than using substitutable expressions. Function evaluator programs can be used to calculate arbitrarily long, complicated functions.

Try this example:

clear all
sysuse auto

drop if missing(rep78)

areg mpg gear turn, absorb(rep78)

program nlfe

        version 11
        syntax varlist(min=3 max=3) if, at(name)
        local lhs : word 1 of `varlist'
        local x1  : word 2 of `varlist'
        local x2  : word 3 of `varlist'

	// Compute the first part of the function
        replace `lhs' = `at'[1,1]*`x1' + `at'[1,2]*`x2' `if'

        // Now loop over coefficients on rep78 indicators
        local atcnt = 3                 // First element of `at' for
                                        // rep78 params is the third.
        forvalues i = 1/5 {             // 5 levels of rep78
                replace `lhs' = `lhs' +                         ///
                                `at'[1, `atcnt']*(rep78 == `i') `if'
                local `++atcnt'


nl fe @ mpg gear turn, nparam(7)

-areg- includes a constant term in the model, but for simplicity I did not include one in the -nl- version; all the slope parameters will be identical, as will predicted values, though the R-squared will differ.

One question I have but do not have an answer for is whether there is an incidental parameters problem here. Except for linear regression and Poisson-type models (and maybe some obscure others), controlling for fixed effects by including dummy variables produces inconsistent results in the common case of large-N, fixed-T panel datasets. I do not know whether that is a problem here.

   -- Brian Poi
*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index