Announcement

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

  • -gmm- expression too long

    Hi Statalist,

    I am trying to estimate a conditional logit model. Since one of my regressors is endogenous, I am trying to estimate it with -gmm- instead of the -clogit- command. I encountered a problem that the expression for the moment condition is too long. I have tried both the interactive version and the moment-evaluator program version, and Stata reported seemingly the same error. I wonder if someone knows a way to go around this problem.

    Interactive version: my moment conditions are not in the form of E{z_i u_i(b)} = 0, but are in the more general form of E{h_i(z_i;b)} = 0. Specifically, they take the form E{\sum_{j}[(Y_{ij}-(exp(X_{ij}\beta)/ \sum_{k}(exp(X_{ik}\beta))))*X_{ij}]}=0. Therefore, I cannot use the instrument() option, but need to write out the entire moment conditions, h_i(z_i;b), instead of just of the residual term, u_i(b). When I do so, Stata reports the error message “could not evaluate equation 1”. I suspect that, as one person mentioned in a previous thread for a similar situation, the macro that contains my moment condition is too long and contains too many characters (51474 characters). The reason this person offers for that similar situation is that there is a limit to how long an expression passed to -generate- or -replace- can be.

    Moment-evaluator program version: following the person's suggestion in the same thread, I also tried the moment-evaluator program version. Similar to the interactive version, I need to write out the entire moment conditions, h_i(z_i;b), instead of just of the residual term, u_i(b). When I do so, Stata reports the error message "expression too long". I suspect that the error message is given for the same reason as in the interactive version.

    Below please see my code for the interactive version:
    [CODE]
    global den "exp({b1}*a1)"
    foreach j of numlist 1(1)58 {
    if `j'!=1 {
    global den "$den+exp({b1}*a`j')"
    }
    }

    global mc `"(y1-(exp({b1}*a1)/($den)))*a1"'
    foreach l of numlist 1(1)58 {
    if `l'!=1 {
    global mc "$mc+(y`l'-(exp({b1}*a`l')/($den)))*a`l'"
    }
    }
    [CODE]
    And the moment-evaluator program version:
    Code:
     gmm gmm_api, nequations(1) parameters(b1) from(b1 -0.0004) winitial(identity)
    With ado file:
    Code:
    program gmm_api
        version 14
        syntax varlist if, at(name)
        quietly {
            *macro drop _all
            *running the do file does not display anything; have to copy it to the command window
            global den "exp(`at'[1,1]*a1)"
            foreach j of numlist 1(1)58 {
                if `j'!=1 {
                    global den "$den+exp(`at'[1,1]*a`j')"
                }
            }
            di "$den"
    
            global mc `"(y1-(exp(`at'[1,1]*a1)/($den)))*a1"'
            foreach l of numlist 1(1)58 {
                if `l'!=1 {
                    global mc "$mc+(y`l'-(exp(`at'[1,1]*a`l')/($den)))*a`l'"
                }
            }
            di "$mc"
            replace `varlist'=($mc) `if'
    
        }
    end
    The data:
    a1 y1 a2 y2 a3 y3 a58 y58
    100.7805 1 109.6537 0 125.322 0
    112.7694 1 114.6556 0 139.0111 0
    112.1117 1 98.22129 0 54 0 117.3743 0
    97.55307 1 93.82123 0 53 0 86.10336 0
    103.3631 1 83.72346 0 93.53911 0
    I tried to write my variable names as succinctly as possible, but the whole expression for the moment condition is still too long. I wonder if someone knows a way I can overcome the limit on the length of the expression for my moment condition. Thanks in advance.
    Last edited by Wenbo Li; 13 Feb 2018, 09:42.

  • #2
    You didn't get a quick response. You'll increase your chances of a useful answer by following the FAQ on asking questions - provide Stata code in code delimiters, readable Stata output, and sample data using dataex. Simplify your code to the minimum needed to generate the problem. Without being able to replicate your problem it is hard to help you.

    While I'm not the one to help with this kind of problem, my glance at your expressions suggests they don't look very long. I would suspect that something odd is happening in the interpretation of your code. Have you tried running it with set trace on? What happens of you lower the range of the foreach loops?

    Comment

    Working...
    X