Dear Stata User,

To learn more about Industrial Organizations and STATA programming, I have been trying to replicate the BLP (1995) code, but I am having issues on the estimation part, that I hope someone may help me with.

I have trace where the problem comes up.

Code:
 Source |       SS           df       MS      Number of obs   =     2,217
-------------+----------------------------------   F(5, 2211)      =    279.64
       Model |  1639.14273         5  327.828545   Prob > F        =    0.0000
    Residual |  2592.05292     2,211  1.17234415   R-squared       =    0.3874
-------------+----------------------------------   Adj R-squared   =    0.3860
       Total |  4231.19565     2,216  1.90938432   Root MSE        =    1.0827

------------------------------------------------------------------------------
logit_depvar |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
        hpwt |  -.1205578    .277234    -0.43   0.664    -.6642241    .4231085
         air |  -.0347453   .0728063    -0.48   0.633    -.1775211    .1080305
         mpd |   .2631698   .0431176     6.10   0.000     .1786145     .347725
       space |   2.341023   .1251805    18.70   0.000      2.09554    2.586507
       price |  -.0887309   .0040258   -22.04   0.000    -.0966257   -.0808362
       _cons |  -10.06885   .2528788   -39.82   0.000    -10.56476   -9.572949
------------------------------------------------------------------------------
option expression() required
    The moment expression is incorrectly specified.
r(198);
The interesting part is that when run line per line of program table_3 Colum 1, I get by without any errors for the OLS estimation, but after GMM, the same error occurs again.

Code:
program main
    prepare_data
    table_3
end


program prepare_data
    insheet using "C:\Users\Max\Desktop\Artigos\Cars\BLP_Pyton\trunk\analysis\Transparent Identification (BLP Replication)\external\data\blp_1999_data.csv", clear comma names double
    drop v19
    save "C:\Users\Max\Desktop\Artigos\Cars\Stata_Tutorial_IndustrialOrganizations\blp_data.dta", replace
end


program table_3
    use "C:\Users\Max\Desktop\Artigos\Cars\Stata_Tutorial_IndustrialOrganizations\blp_data.dta", clear
    cap matrix drop TABLE
    
    * Column 1 (OLS)
    reg logit_depvar hpwt air mpd space price
    compute_inelastic_demand, price_coef_name(price)                   option expression() required
    local ols_inelastic_lower = round(r(ci_lower), 0.001)                         
    local ols_inelastic_upper = round(r(ci_upper), 0.001)                                      
    matrix TABLE = _b[_cons]        \ _se[_cons]  \ ///
                   _b[hpwt]         \ _se[hpwt]   \ ///
                   _b[air]          \ _se[air]    \ ///
                   _b[mpd]          \ _se[mpd]    \ ///
                   .                \ .           \ ///
                   _b[space]        \ _se[space]  \ ///
                   .                \ .           \ ///
                   _b[price]        \ _se[price]  \ r(num_inelastic)
    scalar ols_r2 = e(r2)
    
    * Column 2 (IV)
    * First construct the instruments, following BLP (1995, p. 861)
    egen ncar_firm = count(1), by(year firm_id)
    local five_dmd_variables "const hpwt air mpd space"
    foreach var of local five_dmd_variables{ 
        * Reproduces IV construction from BLP (1995) code
        gen own_`var' = `var' * ncar_firm
        egen all_`var' = total(`var'), by(year)
    }
    local indep_var "const hpwt air mpd space price" ///  GMM estimation
    local inst_var "const hpwt air mpd space own_* all_*" 
    gmm (logit_depvar - {xb:`indep_var'}), ///
        instruments(`inst_var') ///                                                                     
        winitial(identity) center                                                                   
    compute_2sls_se, indep_var(`indep_var') inst_var(`inst_var')         option expression() required
    compute_inelastic_demand, price_coef_name(xb_price:_cons)
    local iv_inelastic_lower = round(r(ci_lower), 0.001)
    local iv_inelastic_upper = round(r(ci_upper), 0.001)
    matrix TABLE = nullmat(TABLE) ,                            ///
                   (_b[xb_const:_cons] \ _se[xb_const:_cons] \ ///
                   _b[xb_hpwt:_cons]   \ _se[xb_hpwt:_cons]  \ ///
                   _b[xb_air:_cons]    \ _se[xb_air:_cons]   \ ///
                   _b[xb_mpd:_cons]    \ _se[xb_mpd:_cons]   \ ///
                   .                   \ .                   \ ///
                   _b[xb_space:_cons]  \ _se[xb_space:_cons] \ ///
                   .                   \ .                   \ ///
                   _b[xb_price:_cons]  \ _se[xb_price:_cons] \ r(num_inelastic))
    
    * Column 3 (OLS w/ logs)
    foreach var of varlist price hpwt space mpg {
        replace `var' = log(`var')
    }
    replace trend = year    // transform trend to get the same intercept coefficient
    
    reg price hpwt air mpg space trend
    matrix TABLE = nullmat(TABLE) ,          ///
                   (_b[_cons] \ _se[_cons] \ ///
                   _b[hpwt]   \ _se[hpwt]  \ ///
                   _b[air]    \ _se[air]   \ ///
                   .          \ .          \ ///
                   _b[mpg]    \ _se[mpg]   \ ///
                   _b[space]  \ _se[space] \ ///
                   _b[trend]  \ _se[trend] \ . \ . \ .)
    matrix_to_txt, saving("C:\Users\Max\Desktop\Artigos\Cars\Stata_Tutorial_IndustrialOrganizations\tables.txt") mat(TABLE) format(%20.3f) ///
        title(<tab:ols_logit>) append
    file open f using ../output/tables.txt, write append
    file write f "(`ols_inelastic_lower'-`ols_inelastic_upper')" _tab
    file write f "(`iv_inelastic_lower'-`iv_inelastic_upper')" _tab "." _n
    file close f
    matrix TABLE = ols_r2 , ., e(r2)
    matrix_to_txt, saving("C:\Users\Max\Desktop\Artigos\Cars\Stata_Tutorial_IndustrialOrganizations\tables.txt") mat(TABLE) format(%20.3f) title() append
end

* Replicates standard errors from BLP (1995)
program compute_2sls_se, eclass
    syntax, indep_var(varlist) inst_var(varlist)
    preserve
    
    predict resid
    * local inst_var const hpwt air mpd space own_* all_*
    foreach instrument of varlist `inst_var' {
        gen mom_`instrument' = `instrument' * resid
    }
    quietly corr mom_*, covariance
    matrix VCOV_MOM = r(C)
    
    mkmat `indep_var', matrix(X)
    mkmat `inst_var', matrix(Z)
    matrix JACOBIAN = Z' * X / _N
    matrix VCOV_PARAM = ///
        inv(JACOBIAN' * JACOBIAN) * JACOBIAN' * VCOV_MOM * JACOBIAN * inv(JACOBIAN' * JACOBIAN) / _N
    ereturn repost V = VCOV_PARAM
    
    restore
end

program compute_inelastic_demand, rclass
    syntax, price_coef_name(string)
    * Rounds inputs to ensure match with BLP (1995) table
    scalar price_coef = round(_b[`price_coef_name'], 0.001)
    scalar price_coef_se = round(_se[`price_coef_name'], 0.001)
    
    gen price_elasticity = -price_coef * price * (1-share)
    quietly count if price_elasticity < 1
    return scalar num_inelastic = `r(N)'
    
    gen price_elasticity_ci_upper = -(price_coef + 2*price_coef_se) * price * (1-share)
    quietly count if price_elasticity_ci_upper < 1
    return scalar ci_upper = `r(N)'
    
    gen price_elasticity_ci_lower = -(price_coef - 2*price_coef_se) * price * (1-share)
    quietly count if price_elasticity_ci_lower < 1
    return scalar ci_lower = `r(N)'
    
    drop price_elasticity*
end
Here is a sample of the data I am using:

Code:
clear
input double(logit_depvar hpwt) byte air double(mpd space price)
 -6.725221995827 .528996865204 0 1.888145604396 1.1502  4.935802469136
 -7.175606516457 .494324423288 0 1.935989010989  1.278  5.516049382716
 -7.852502562348 .467613439557 0 1.716799450549 1.4592  7.108641975309
 -7.424867073844  .42654028436 0 1.687870879121 1.6068   6.83950617284
 -7.590731362432 .452488687783 0 1.504285714286 1.6458  8.928395061728
 -5.761440843009 .450870646766 0 1.726813186813 1.6224  7.153086419753
 -5.800458924364 .564001961746 0 1.727925824176  1.768  9.856790123457
 -7.494032269991 .731367541212 0 2.194120879121  1.768 11.269135802469
 -5.893490833865 .719013923762 0 2.056153846154  1.816 12.135802469136
  -7.37054142125 .728323699422 0 1.978269230769  1.744  12.97037037037
 -8.837259359519 .732484076433 0 1.918186813187  1.808              15
 -5.780288130463 .729386892178 0 1.912623626374  1.808 16.044444444444
 -7.402376987159 .780748663102 0 2.018324175824  1.776  18.22962962963
  -7.87526574482 .716510903427 0 1.892596153846  1.832 19.167901234568
  -5.38771548063 .419384902144 0 2.211923076923  1.122   5.16049382716
 -5.513151785684 .487231182796 0 1.802472527473  1.387  5.938271604938
 -5.159829472982 .451713395639 0 1.682307692308 1.5352   6.60987654321
 -6.180926607269 .468648998061 0           1.62   1.41  7.212345679012
 -5.992941074536 .702408256881 0 1.604423076923 1.5732  8.434567901235
 -7.742032900857 .388531618435 0         1.4175  1.736  7.644444444444
 -7.087644340445 .388531618435 0         1.4175  1.736  7.982716049383
 -4.616703146464 .385638297872 0 1.406373626374  1.736  8.372839506173
 -6.038658871342 .631188118812 0 1.463118131868  1.736 10.207407407407
 -7.802465286548 .843222985634 0 1.872568681319 1.2627 13.661728395062
 -6.738130217816 .659314313114 0 1.528763736264    1.8 10.345679012346
 -8.898149315437  .77528349919 0  1.65782967033    1.8 11.572839506173
 -7.857978882178 .772779700115 0 1.893708791209    1.8 12.123456790123
 -7.629367819706  .49504950495 0 2.330975274725 1.0168  4.925925925926
 -5.637141076827 .431034482759 0 1.949340659341  1.379  6.049382716049
 -7.595590772216 .413907284768 0 1.654491758242 1.4784  6.733333333333
 -6.161708963165 .446841294299 0 1.614436813187 1.6146  6.856790123457
 -9.585797856799  .45101088647 0 1.544340659341 1.5862  6.683950617284
 -6.701175396079 .386151797603 0 1.378557692308  1.768  8.143209876543
 -8.070101066651 .679012345679 0  1.55657967033  1.768 10.427160493827
 -5.181236055153 .384812724474 0 2.266442307692  1.141  4.738271604938
 -5.463168183523 .384172109105 0 2.130700549451 1.3277  5.518518518519
  -5.38139545377 .464297150176 0 1.711236263736 1.5939  6.597530864198
 -6.163448235385 .497768623412 0 1.732376373626  1.425  7.187654320988
 -7.450346159145 .380952380952 0 1.458667582418  1.736  8.118518518519
 -5.578863554699 .376850605653 0 1.443090659341  1.736  8.874074074074
 -5.203481508363  .60286360211 0 1.447541208791  1.736  9.706172839506
 -7.324715240714  .82023239918 0 2.050590659341  1.677 13.074074074074
  -7.39708396913 .719637223975 0 2.079519230769    1.8 18.318518518519
 -7.579640177217 .729562262642 0 1.970480769231  1.736  21.76049382716
 -6.841955237469 .382848392037 0 2.127362637363 1.3419  5.637037037037
 -6.924573150893 .449194547708 0 1.648928571429  1.638  6.859259259259
 -7.066129412843 .720504353047 0  1.65782967033 1.4972  8.120987654321
 -7.532588465879 .595681310499 0 1.533214285714    1.8  9.525925925926
 -6.654976390355 .598251265532 0 1.630013736264    1.8 11.046913580247
 -5.449077174338 .445879458795 0  1.86478021978 1.6016  7.404938271605
 -6.244589705636 .578313253012 0 1.730151098901  1.768   9.83950617284
 -6.245083863989 .716364450414 0 1.862554945055 1.7933  11.98024691358
 -7.318705638377  .77399380805 0 1.920412087912   1.76 13.474074074074
 -5.468062504112 .440917107584 0 1.852541208791 1.3608  5.906172839506
 -8.017784479964 .415282392027 0 1.667843406593 1.4025  6.553086419753
 -6.229720910417 .446291166513 0 1.602197802198 1.6195  6.750617283951
 -7.620248469214 .391363022942 0         1.4175  1.728  7.767901234568
 -7.106055733275 .620181025813 0 1.746840659341 1.4235  6.143209876543
 -5.881441826009 .449055435119 0  1.65782967033 1.5939  7.222222222222
 -7.009599786801 .458280657396 0 1.604423076923 1.4208  7.523456790123
 -5.896781323347 .613195977434 0 1.469793956044  1.768  9.308641975309
 -7.275763014923 .664609541894 0 1.534326923077    1.8 10.395061728395
 -6.718326740432 .776598498576 0 1.733489010989 1.6401 11.251851851852
-10.054804973755 .465766185375 0 2.201909340659 1.1136  7.641975308642
 -8.044587548282 .487908358082 0  1.98271978022 1.2627  8.876543209877
 -7.226972850754 .373878364905 0 2.332087912088  1.092  6.037037037037
 -7.000540728132 .428481411468 0  2.79717032967   .906  4.286419753086
 -6.320242054469  .43935052531 0 2.470054945055   .992  5.234567901235
-10.058397075003 .277777777778 0 2.789381868132  .7896   3.83950617284
 -9.092117377617 .375939849624 0 2.438901098901 1.0368  5.138271604938
 -8.722849008341        .24375 0 2.836112637363  .8586  3.444444444444
 -9.875475937897 .436724565757 0 2.542376373626 1.0106   5.41975308642
 -9.978877371848 .409722222222 0 1.858104395604  1.295 13.116049382716
 -9.180665140251 .398514015535 0 1.820274725275  1.295  13.38024691358
 -9.082277013601 .525259284041 0 1.530989010989  1.295 15.748148148148
 -8.312732485058 .622621930128 0 1.650041208791  1.183 18.441975308642
-10.187085015326 .333531864205 0 2.612472527473  1.053  4.698765432099
 -9.703643207457 .364356435644 0 2.087307692308 1.1682  8.587654320988
   -8.6207644385 .430816016219 0 2.280906593407 1.0205  8.876543209877
  -9.30211179366 .634495084897 0 1.952678571429 1.0496 16.037037037037
 -8.978212908876 .293654955427 0 2.553502747253  1.002  4.555555555556
-10.122375976667 .349397590361 0 2.122912087912 1.1352  8.333333333333
 -6.217754549513 .450127877238 0 2.584656593407   .948  4.920987654321
  -6.75767044042 .497695852535 0 2.388832417582 1.0354  5.308641975309
 -6.617200108078 .467532467532 0 2.298708791209 1.1008   5.62962962963
 -9.605431006932 .387858347386 0 1.773543956044 1.2395  7.034567901235
 -8.108781000785  .42731277533 0 2.335425824176 1.0332  6.414814814815
    -5.104170658  .31914893617 0 2.692582417582  .9699  4.555555555556
 -6.590614088581 .240629338269 0 2.480068681319 1.1008  6.417283950617
 -7.765050850697 .328293736501 0 2.060604395604 1.1946  7.404938271605
  -8.59832323573 .376114773168 0 2.182994505495 1.2627  8.320987654321
 -8.703890107146 .441330998249 0 2.082857142857 1.2648 10.234567901235
 -6.382279292207 .400962309543 0 2.253263157895 1.1502  4.782296650718
  -6.99530621771 .371609067261 0 2.161789473684  1.278  5.418660287081
 -7.725352136363 .347705146036 0 1.964947368421 1.4592  6.715311004785
 -7.397157080116 .315357931252 0 2.130526315789 1.6068   6.66028708134
 -7.590750652679 .424088210348 0 1.660421052632 1.6536  9.294258373206
 -5.869501152436 .357057843371 0 1.511052631579  1.768  9.468899521531
  -7.54825092196 .510667271902 0 1.420736842105  1.768 10.784688995215
 -5.868270574208 .498338870432 0 1.326947368421  1.816 11.698564593301
end

If anyone can guide me through it, I appreciate it!!

Max