Announcement

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

  • Error when using sort in gmm moment function

    I am using Stata's gmm command using a custom moment-evaulator program. The program is quite complicated, and involves sorting the data multiple times (to use various egen commands). This is causing the gmm command to fail with an 'initial values not feasible' error.

    I'm using Stata 18.5, MP—Parallel Edition.

    This much simpler working example illustrates the problem:

    Code:
    set seed 1
    clear all
    set obs 1000
    generate mean_zero = rnormal()
    generate f = floor(runiform()*20)
    generate j = _n
    
    cap program drop moments
    program moments
        syntax varlist [if], at(name)
        local mu = `at'[1,1]
        tempvar r
        bys f: generate `r' = mean_zero - `mu' if _n == 1
        sort j
        replace `varlist' = `r' `if'
    end
    
    matrix start = [1]
    gmm moments, nequations(1) parameters(mean) from(start)
    That code returns an "initial values not feasible r(1400);" error, which makes no sense.

    Removing the `if _n == 1` clause or the `sort j` command resolves the issue -- but for my actual program I can't just avoid the equivalent lines.

    Any help would be appreciated.
    Last edited by Wilbur Townsend; 01 Jul 2024, 02:42.

  • #2
    In case anyone is having similar problems: adding ", sortpreserve" to the program definition and ", stable" to all sort commands seems to have solved the problem. It'd be good to get some insight as to what was going wrong, to ensure I don't do something silly.

    Comment

    Working...
    X