Dear All,

I am following De Loecker and Warzynski to calculate markups. In my previous posts, I have been able to get some help but at some point, I was stuck in one problem: optimize_init_params() doesnt work!

I provide below the data example I have, the code that I am using, and the error message Stata gives me:

The data:



Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(com marktt_ind) int year float(phi epsilon phi_lag l_lag k_lag l_lag2 k_lag2 l_lagk_lag lk l_lagk lva y_c va_c alpha_l)
 1 12 1995 17.663496  -.14531818  17.60554  5.860786 17.318508 34.348816 299.93073 101.50008 102.48646 101.80706 2.7764525  2.921771  18.57415  578941.9
 1 12 1996 17.689566   -.1132545 17.663496  5.899898 17.370888 34.808792 301.74774 102.48646 102.66798 102.71575 2.8021045  2.915359 18.455437 596292.06
 1 12 1997 17.737486  -.08299164 17.689566  5.897154 17.409752 34.776424 303.09946 102.66798 103.16277 102.78385  2.796608  2.879599 17.807137  620410.1
 1 12 1998 17.870161  -.03027034 17.737486  5.918894   17.4294 35.033302 303.78397 103.16277 104.30627 103.25966  2.790774  2.821044 16.794378  634415.4
 1 12 1999 17.788733   .03749528 17.870161  5.978886  17.44577 35.747074  304.3549 104.30627 104.55385 104.91288  2.847206  2.809711 16.605118  650297.6
 1 12 2000 17.884565  -.03831578 17.788733  5.958425  17.54723 35.502823  307.9053 104.55385 105.81186 105.22836  2.810671  2.848987 17.270275  661007.6
 1 12 2001 17.967432  -.06480103 17.884565  5.991465 17.660433 35.897648  311.8909 105.81186 107.82575 106.17705   2.77633  2.841131  17.13513  719604.9
 1 12 2002 17.909676  -.18381597 17.967432  6.084499 17.721384 37.021133  314.0475 107.82575  105.2437 107.75312  2.769219  2.953035 19.164026  613469.4
 1 12 2003  17.90764  -.08896594 17.909676    5.9428 17.709448 35.316868  313.6245  105.2437  107.1805 105.08236  2.758906 2.8478715 17.251024  762702.9
 1 12 2004 17.953201  -.12208369  17.90764  6.061457 17.682299 36.741264  312.6637  107.1805 108.12317 107.55337  2.730554  2.852638 17.333443    748188
 1 12 2005 17.892786  -.13917208 17.953201   6.09357 17.743814  37.13159   314.843 108.12317 107.93797 107.85824  2.721144  2.860316  17.46705  766008.6
 1 12 2006 17.942837  -.14148639 17.892786  6.098074 17.700336 37.186512  313.3019 107.93797 106.60307   107.666  2.736379  2.877865 17.776285  728916.3
 1 12 2007 17.987103  -.10390857 17.942837  6.037871 17.655739 36.455883  311.7251 106.60307 105.07823 106.29584  2.786732   2.89064 18.004833  681463.3
 1 12 2008 18.108057   -.1910118 17.987103  5.968708 17.604855  35.62547  309.9309 105.07823 104.51495 104.78644   2.77711  2.968122  19.45535 606006.56
 1 12 2009  17.45102   -.4533427 18.108057  5.953243 17.555967 35.441105   308.212 104.51495  102.6487 104.16705  2.656362 3.1097045  22.41442  476689.3
 1 12 2010  17.39038   -.4302138  17.45102  5.866468  17.49753 34.415447  306.1635  102.6487  99.03662 102.34637  2.714753  3.144967 23.218906  350733.5
 1 12 2011 17.479313   -.3458163  17.39038  5.676754 17.445995 32.225536 304.36276  99.03662  95.41161  98.75083  2.848679 3.1944954  24.39786  418116.1
 1 12 2012 17.279308   -.3324071 17.479313  5.484797  17.39565    30.083 302.60867  95.41161  95.24123  95.09793  2.851633   3.18404 24.144094  413602.3
 1 12 2013 17.206064   -.3205327 17.279308  5.493062 17.338459 30.173725 300.62216  95.24123  93.19028  94.98853 2.8774486  3.197981 24.483057    338222
 1 12 2014 17.163809   -.2937959 17.206064  5.389072 17.292456 29.042097 299.02902  93.19028  90.46423  93.00683  2.993029  3.286825  26.75777  346092.9
 2 11 1991 16.864801    .3439448 16.970314   5.53339 14.298125   30.6184  204.4364   79.1171  84.23546   83.7642  2.957535 2.6135905 13.647965  500342.3
 2 11 1992 16.867336    .3416851 16.864801   5.56452 15.137955 30.963886 229.15767  84.23546  84.77357  84.59981  2.954488  2.612803 13.637222  569276.6
 2 11 1993 16.988089    .2198996 16.867336  5.575949 15.203432  31.09121 231.14435  84.77357  83.97953  84.80827  2.950734  2.730834 15.345684 558420.94
 2 11 1994  16.97816    .3379018 16.988089  5.521461 15.209657  30.48653 231.33366  83.97953  85.73739  84.78481 2.9623506  2.624449 13.796967  657649.4
 2 11 1995 17.120811    .3738884  16.97816  5.583496 15.355503  31.17543 235.79147  85.73739   87.3779  86.04663  2.947929  2.574041 13.118726  834368.7
 2 11 1996 17.175308    .3576192 17.120811  5.669881 15.410888  32.14755 237.49545   87.3779  89.25544  89.20128  2.954464  2.596845 13.421323  834693.3
 2 11 1997 17.243544    .3249852 17.175308  5.673323  15.73248 32.186596  247.5109  89.25544   90.8883  90.83337  2.962983 2.6379974 13.985168  817908.9
 2 11 1998 17.168638   .47637135 17.243544  5.676754  16.01061 32.225536 256.33966   90.8883  89.58325  91.14098 3.0594726  2.583101  13.23813  764022.4
 2 11 1999 17.466276   .19378944 17.168638   5.57973 16.055122 31.133387 257.76697  89.58325  89.74376  90.23952  2.976131  2.782341 16.156803  626516.6
 2 11 2000 17.486904    .1302654 17.466276  5.549076 16.172739 30.792246  261.5575  89.74376  92.06766  91.81212  2.954697 2.8244314  16.85136  574889.6
 2 11 2001 17.371695   .09091611 17.486904   5.56452 16.545479 30.963886 273.75287  92.06766  91.55039  91.61359  2.926352 2.8354356  17.03782  470591.1
 2 11 2002 17.538153   .11332463 17.371695  5.560682 16.463879  30.92118  271.0593  91.55039  94.50967  94.84087  2.989271  2.875946 17.742205  633559.6
 3  9 1991 17.686045 -.027189447 17.685211  6.021023 17.647928  36.25272  311.4494 106.25858 105.86242 105.86242  2.732819 2.7600086 15.799978  935891.3
 3  9 1992 17.671646   .14222182 17.686045  6.021023  17.58213  36.25272  309.1313 105.86242 105.61868  105.7041  2.769636  2.627414 13.837938 1111021.1
 4 12 1991 15.022756    .3073085  15.01968 3.2580965  14.90282 10.615192   222.094  48.55482   50.3941  48.27384 4.3042393  3.996931  54.43084  22385.24
 4 12 1992   15.1366    .4556749 15.022756 3.4011974 14.816577 11.568144 219.53096   50.3941  50.23078  50.23078  4.381688  3.926013  50.70443  25348.23
 4 12 1993 15.133115   .38872105   15.1366 3.4011974 14.768559 11.568144  218.1103  50.23078  49.92993  49.92993   4.25475  3.866029  47.75237  27437.33
 4 12 1994  15.34381   .56622404 15.133115 3.4011974 14.680104 11.568144 215.50546  49.92993  52.73893  50.05569  4.217258  3.651034  38.51447 33996.074
 5 13 1991  14.33882   .14315946 14.318871 3.4011974 15.438456 11.568144  238.3459  52.50924   51.9116   51.9116 4.0813046  3.938145  51.32331 18239.602
 5 13 1992 14.425867   .08028135  14.33882 3.4011974 15.262743 11.568144 232.95132   51.9116  53.10882  51.66114 3.9776285  3.897347  49.27157  21289.85
 5 13 1993 14.147803   .26025337 14.425867  3.496508 15.189105 12.225566  230.7089  53.10882  49.79938  52.83147 4.2118893  3.951636  52.02039 17387.271
 5 13 1994 13.998558   .27502674 14.147803  3.295837 15.109783  10.86254 228.30554  49.79938  47.71178  49.48004 4.3498187  4.074792  58.83823  14231.01
 6 13 1991 16.260809  -.42471975  16.21919 4.0943446 14.839644 16.763659 220.21504  60.75862  62.98775  61.11935   3.60359 4.0283093  56.16587 33319.867
 6 13 1992 16.190674   -.3836766 16.260809 4.2195077  14.92775 17.804245  222.8377  62.98775  60.28959  63.48172  3.770783   4.15446  63.71754 31637.805
 6 13 1993 16.109068  -.40775445 16.190674 4.0073333 15.044816  16.05872  226.3465  60.28959  60.91506  60.11831 3.6613054   4.06906  58.50194 32177.703
 6 13 1994 16.068838   -.3437974 16.109068  4.060443 15.002072 16.487196  225.0622  60.91506  60.95181  60.69627  3.686506 4.0303035  56.27799 33962.234
 6 13 1995  16.22614   -.3108323 16.068838 4.0775375  14.94819 16.626312  223.4484  60.95181  60.55251  60.80744 3.7417824 4.0526147  57.54773 38583.008
 6 13 1996 16.159908   -.3321391  16.22614  4.060443 14.912786 16.487196 222.39117  60.55251  60.32162   60.5811 3.7396474 4.0717864  58.66166 35998.133
 6 13 1997  16.52201   -.3992459 16.159908 4.0430512 14.919825 16.346264 222.60117  60.32162 64.415695  60.32117  3.532476  3.931722  50.99471   49431.5
 6 13 1998 16.705597   -.3663772  16.52201  4.317488 14.919716 18.640705  222.5979 64.415695  65.23885 64.278015  3.522991  3.889368  48.87997  50680.11
 6 13 1999   16.3978  -.11567882 16.705597 4.3820267 14.887826  19.20216 221.64737  65.23885  62.63631  65.04882  3.731079  3.846758  46.84094  50147.09
 6 13 2000 16.144672    -.307299   16.3978 4.2195077  14.84446 17.804245   220.358  62.63631  59.18729  63.51788  3.840071 4.1473703  63.26741  29437.58
 6 13 2001 16.062643  -.14592817 16.144672 3.9318256 15.053386 15.459253  226.6044  59.18729  58.38268  58.98281 3.9166164  4.062545  58.12203 32380.344
 6 13 2002 16.104303   -.1866616 16.062643   3.89182  15.00138 15.146264  225.0414  58.38268  59.97431  58.24553 3.8021834  3.988845   53.9925 36518.668
 6 13 2003 16.065002 -.063100174 16.104303 4.0073333  14.96614  16.05872 223.98534  59.97431  60.26117  59.47297  3.801194  3.864294   47.6696  45042.38
 6 13 2004 15.966308  -.04491978 16.065002  4.060443 14.841035 16.487196  220.2563  60.26117  59.77331  60.29439  3.818517 3.8634365  47.62875  43860.78
 6 13 2005 15.850893   .02122972 15.966308 4.0253515 14.849215 16.203455 220.49918  59.77331  59.81426  59.55241  3.737611 3.7163815  41.11535  52677.49
 6 13 2006  15.95958  -.04334816 15.850893 4.0430512 14.794337 16.346264  218.8724  59.81426  60.37286  59.61652  3.462443 3.5057914 33.307793 69961.914
 6 13 2007 16.214342  -.27245182  15.95958 4.0943446 14.745428 16.763659 217.42764  60.37286  60.47477  60.47477  3.686227  3.958679  52.38808   48105.7
 6 13 2008 16.076605  -.27628836 16.214342 4.0943446 14.770316 16.763659 218.16223  60.47477  60.53951  60.05853   3.67999  3.956278  52.26245   47778.8
 6 13 2009 15.069398    .0908123 16.076605 4.1271343 14.668654 17.033237  215.1694  60.53951  56.58737  60.32848   3.76528 3.6744676  39.42766  54259.69
 6 13 2010  15.63076   -.3830249 15.069398  3.871201 14.617522 14.986197 213.67195  56.58737  56.15325  56.46031  3.733954  4.116979  61.37356 32005.066
 6 13 2011 15.673334  -.51269746  15.63076 3.8501475   14.5847 14.823636  212.7135  56.15325  56.50153  56.19424  3.686655 4.1993523  66.64315 27863.145
 6 13 2012 15.249295   -.4021002 15.673334  3.871201 14.595347 14.986197 213.02417  56.50153  54.70491  56.30482 3.7371416 4.1392417  62.75521 28027.807
 6 13 2013  15.25802   -.6352077 15.249295    3.7612 14.544536 14.146626  211.5435  54.70491  52.65004  54.43921  3.782907 4.4181147  82.93977  21643.31
 6 13 2014 14.851965   -.3753061  15.25802  3.637586 14.473894 13.232033  209.4936  52.65004   50.6981  53.21199  3.972618  4.347924   77.3178  21166.89
 7 14 1991 16.853062   .05299169 16.918638  5.476463 17.105768  29.99165  292.6073  93.67912  92.20798   92.7848 2.9481015   2.89511  18.08549  429950.8
 7 14 1992 16.740894     .052176 16.853062  5.442418 16.942467  29.61991 287.04718  92.20798  89.83835  91.76974  2.989162  2.936986 18.858923    402502
 7 14 1993 16.623428   .20358093 16.740894  5.327876 16.861944 28.386263 284.32516  89.83835  87.77859   89.3113  3.105712  2.902131 18.212921  410741.6
 7 14 1994 16.655666    .2300403 16.623428  5.236442 16.763021 27.420326  280.9989  87.77859  86.53831  87.26296  3.115903 2.8858626 17.919018  394877.6
 8 13 1991  16.15451    .3130495 16.176357  5.669881  17.10542  32.14755 292.59537  96.98569  95.57558   96.4793  2.846296  2.533247  12.59433    641836
 8 13 1992 16.157915   .28487125  16.15451  5.616771 17.016108  31.54812  289.5479  95.57558  95.11002  95.42175  2.851641   2.56677 13.023687 611882.75
 8 13 1993  15.88738   .24308966 16.157915  5.598422 16.988718  31.34233 288.61655  95.11002  93.69633  94.93369  2.844345  2.601256 13.480655  578734.4
 8 13 1994 16.042505    .2135471  15.88738  5.525453 16.957222  30.53063 287.54736  93.69633  91.94699   93.4243   2.87401  2.660463 14.302915  505009.4
 9 19 1991  16.42134    .5311236  16.51443  5.627621 16.009825  31.67012 256.31448  90.09723   91.6189   91.6776  2.934493  2.403369 11.060378    718790
 9 19 1992 16.584436    .3750993  16.42134  5.624018  16.29065 31.629576 265.38525   91.6189  90.29236  91.38412 2.9540095   2.57891 13.182762  627915.8
 9 19 1993 16.402678     .320945 16.584436  5.556828 16.248903  30.87834 264.02686  90.29236  88.15438  90.00756  2.974738  2.653793 14.207825    538470
 9 19 1994 16.514917   .26840603 16.402678  5.442418  16.19765  29.61991  262.3639  88.15438  88.59432  87.84346  2.954048  2.685642  14.66761 558444.56
 9 19 1995 16.727755     .198879 16.514917  5.488938 16.140522  30.12844 260.51645  88.59432  89.87144  88.46902  2.900942  2.702063 14.910458  612169.2
 9 19 1996  16.74119    .1691756 16.727755  5.575949 16.117695  31.09121  259.7801  89.87144  90.72715  90.00959  2.890044  2.720868  15.19351  639013.3
 9 19 1997 16.698862   .19144765  16.74119  5.620401  16.14247  31.58891  260.5793  90.72715  90.02193  90.98873 2.9237914  2.732344 15.368864  614118.7
 9 19 1998 17.174212   .10208677 16.698862  5.560682  16.18901  30.92118 262.08408  90.02193  95.09536  89.96577  2.804318  2.702231 14.912965  823176.3
 9 19 1999 17.109404    .0500454 17.174212  5.877736 16.178911 34.547775 261.75717  95.09536  94.45771  94.82333 2.7939355   2.74389 15.547347  805871.6
 9 19 2000  17.16049   .16312754 17.109404  5.855072  16.13263 34.281868 260.26172  94.45771  93.85543  94.08732  2.838111  2.674983 14.512103  851672.7
 9 19 2001  17.05438   .06712701  17.16049  5.840641  16.06937 34.113094 258.22467  93.85543  93.36793  94.69316   2.83193  2.764803 15.875915  772670.6
 9 19 2002 17.037884  -.00194814  17.05438  5.758902 16.212801 33.164948 262.85492  93.36793  91.47625  93.19836   2.84715  2.849098   17.2722    638048
 9 19 2003 17.013098   .04945622 17.037884  5.652489 16.183357 31.950634 261.90106  91.47625    90.082  91.13386  2.890843  2.841387 17.139523 596586.25
 9 19 2004 16.839987   .09076396 17.013098  5.587249 16.122784  31.21735 259.94415    90.082  87.20971  89.67377  2.959271  2.868507 17.610708    468333
 9 19 2005 16.523518   .05706843 16.839987  5.433722 16.049719 29.525335 257.59348  87.20971  84.32843  86.81457  3.000384  2.943316 18.978676  360477.3
 9 19 2006 16.402044 -.034593806 16.523518  5.278115 15.976998 27.858496  255.2645  84.32843  83.87763    83.959  2.944055  2.978649 19.661234  345185.3
 9 19 2007 16.329878  -.06901367 16.402044     5.273 15.907005  27.80453  253.0328  83.87763  81.17746  83.44195   3.00204  3.071053 21.564604 397395.75
 9 19 2008 16.145306  -.16005547 16.329878  5.129899  15.82438  26.31586 250.41096  81.17746   78.7913  80.88337  3.028521 3.1885765 24.253876 209243.53
 9 19 2009 15.674362   -.3187082 16.145306  4.997212  15.76705  24.97213  248.5999   78.7913  77.70444  78.46523  2.959481 3.2781894   26.5277 178776.67
11 12 1993 18.044546   .28080943 17.992462  5.777652 18.341461 33.381268  336.4092 105.97058 105.31577  105.4291 2.8322515  2.551442 12.825587  957422.8
11 12 1994 18.442446    .1722128 18.044546  5.771441 18.247742 33.309532  332.9801 105.31577 104.33702 104.85366  2.897744  2.725531 15.264515  815381.8
14 14 1991 17.288708     .181492 17.803917    5.7301 17.288134  32.83404 298.87958  99.06273  98.08169  99.06326    2.9381  2.756608  15.74634 605064.56
14 14 1992 17.072548   .05759528 17.288708  5.673323 17.288225 32.186596 298.88272  98.08169  96.73259  97.70654 2.8785625  2.820967 16.793085  562113.1
14 14 1993 17.004541  .026679995 17.072548  5.616771   17.2221  31.54812  296.6007  96.73259  94.94049  96.37113 2.8579144 2.8312345 16.966393  555857.3
14 14 1994 17.212688   .04635059 17.004541   5.53339 17.157745   30.6184  294.3882  94.94049  93.45595  94.57251  2.969334  2.922983 18.596682  440335.2
14 14 1995 17.539454   .10952644 17.212688   5.46806 17.091244  29.89968 292.11063  93.45595  92.65189  93.08843  3.016487 2.9069605 18.301088 430957.25
end


the code:


Code:
clear mata
mata:
void GMM_AXT(todo,betas,crit,g,H)
{
    PHI=st_data(.,("phi"))
    PHI_LAG=st_data(.,("phi_lag"))
    Z=st_data(.,("const","l_lag","k"))
    X=st_data(.,("const","l","k"))
    X_lag=st_data(.,("const","l_lag","k_lag"))
    Y=st_data(.,("y"))
    C=st_data(.,("const"))
 
    OMEGA=PHI-X*betas'
    OMEGA_lag=PHI_LAG-X_lag*betas'
    OMEGA_lag_pol=(C,OMEGA_lag)
    g_b = invsym(OMEGA_lag_pol'OMEGA_lag_pol)*OMEGA_lag_pol'OMEGA
    XI=OMEGA-OMEGA_lag_pol*g_b
    crit=(Z'XI)'(Z'XI)
}
 
void GMM_AXT_TRL(todo,betas,crit,g,H)
{
    PHI=st_data(.,("phi"))
    PHI_LAG=st_data(.,("phi_lag"))
    Z=st_data(.,("const","l_lag","k","l_lag2","k2","l_lagk"))
    X=st_data(.,("const","l","k","l2","k2","lk"))
    X_lag=st_data(.,("const","l_lag","k_lag","l_lag2","k_lag2","l_lagk_lag"))
    Y=st_data(.,("y"))
    C=st_data(.,("const"))
 
    OMEGA=PHI-X*betas'
    OMEGA_lag=PHI_LAG-X_lag*betas'
    OMEGA_lag_pol=(C,OMEGA_lag)
    g_b = invsym(OMEGA_lag_pol'OMEGA_lag_pol)*OMEGA_lag_pol'OMEGA
    XI=OMEGA-OMEGA_lag_pol*g_b
    crit=(Z'XI)'(Z'XI)
}
 
void AXT()
    {
S=optimize_init()
optimize_init_evaluator(S, & GMM_AXT())
optimize_init_evaluatortype(S,"d0")
optimize_init_technique(S, "nm")
optimize_init_nmsimplexdeltas(S, 0.1)
optimize_init_which(S,"min")
optimize_init_params(S,("OLS"))
p=optimize(S)
p
st_matrix("beta_axt",p)
}
 
 
void AXT_TRLOG()
    {
S=optimize_init()
optimize_init_evaluator(S, & GMM_AXT_TRL())
optimize_init_evaluatortype(S,"d0")
optimize_init_technique(S, "nm")
optimize_init_nmsimplexdeltas(S, 0.1)
optimize_init_which(S,"min")
optimize_init_params(S,(0,0,0,0,0,0))
p=optimize(S)
p
st_matrix("beta_axttrlog",p)
}
end
 
 
cap program drop axt
program axt, rclass
preserve
sort Id year
mata AXT()
end
 
 
cap program drop axt_trlog
program axt_trlog, rclass
preserve
sort Id year
mata AXT_TRLOG()
end
 
 
reg y l k i.year
gen beta_lols=_b[l]
gen beta_kols=_b[k]
gen Mar_ols=_b[l]/alpha_l
 
 
mata AXT()
gen beta_c1=beta_axt[1,1]
gen beta_l1=beta_axt[1,2]
gen beta_k1=beta_axt[1,3]
gen Markup_axt1=beta_l1/alpha_l
gen omega_axt1=phi-beta_l1*l-beta_k1*k
 
 
mata AXT_TRLOG()
gen betal_tl1=beta_axt_trlog[1,2]
gen betal_tl2=beta_axt_trlog[1,4]
gen betak_tl1=beta_axt_trlog[1,3]
gen betak_tl2=beta_axt_trlog[1,5]
gen betalk_tl=beta_axt_trlog[1,6]
gen betal_tl=betal_tl1+2*betal_tl2*l+betalk_tl*k
gen Markup_axt_tltra=betal_tl/alpha_l
 
 
gen mar_1=Markup_axt1
gen mar_3=Markup_axt_tltra
*-------------------------------------------------------------------------------------------------=
the error:

Code:
  
 mata AXT()  
   optimize_init_params():  3253  <tmp>[1,1] found where re  
 > al required  
                    AXT():     -  function returned error  
                  <istmt>:     -  function returned error  
 r(3253);  
    
 end of do-file  
    
 r(3253);  
 .
Do I have to change something in optimize_init_params()? I actually do not know how to solve this problem, and any help is appreciated!

Best regards
Ali