Announcement

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

  • Regression within loops

    Dear Stata Users,
    I need to calculate the following regression:
    Code:
    reg bm_w cum_ret_w b1cum_ret_w b2cum_ret_w b3cum_ret_w b4cum_ret_w b5cum_ret_w b6cum_ret_w
    Where “bm_w” is dependent variable (book-to-market ratio) and “cum_ret_w” is cumulative return; “b1,2,3,4,5,6cum_ret_w” are lagged cumulative returns. What I need is to add to this equation firm and year fixed effects and run the regression each year. Then I need to save for each year obtained coefficients of firm fixed effects. What I did is the following, but I do not think that it is true.
    Code:
    gen b10=.
     
    sum fyear
    scalar minyr=r(min)
    scalar maxyr=r(max)
    local m=minyr
    local n=maxyr
     
    forvalues i=`m'(1)`n'{
                qui xi: reg bm_w cum_ret_w b1cum_ret_w b2cum_ret_w b3cum_ret_w b4cum_ret_w b5cum_ret_w b6cum_ret_w i.fyear i.gvkey1 if fyear==`i', vce(cluster gvkey1)
                  qui replace b10=_b[gvkey1] if fyear==`i'
                                               di `i'
    }
    I upload a sample of data. Please, help me with this issue.
    Best regards,
    Alberto

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double(gvkey1 fyear) int sic1 float(cum_ret_w bm_w b1cum_ret_w b2cum_ret_w b3cum_ret_w b4cum_ret_w b5cum_ret_w b6cum_ret_w)
    1004 1995 5080    .6296296   .5781373           .           .           .           .           .           .
    1004 1997 5080    .4903846   .4107591    .6296296           .           .           .           .           .
    1004 1998 5080   -.4752747   .6029035    .4903846    .6296296           .           .           .           .
    1004 2000 5080   -.2862191   .9021367   -.4752747    .4903846    .6296296           .           .           .
    1004 2001 5080   -.4068466   .8509081   -.2862191   -.4752747    .4903846    .6296296           .           .
    1004 2002 5080   -.4011628   2.058111   -.4068466   -.2862191   -.4752747    .4903846    .6296296           .
    1004 2004 5080   .02022468    .602174   -.4011628   -.4068466   -.2862191   -.4752747    .4903846    .6296296
    1004 2005 5080   1.0557941   .4789298   .02022468   -.4011628   -.4068466   -.2862191   -.4752747    .4903846
    1004 2006 5080    .2249266   .4030713   1.0557941   .02022468   -.4011628   -.4068466   -.2862191   -.4752747
    1004 2007 5080    .2766028   .7829043    .2249266   1.0557941   .02022468   -.4011628   -.4068466   -.2862191
    1004 2008 5080   -.3771989   1.149232    .2766028    .2249266   1.0557941   .02022468   -.4011628   -.4068466
    1004 2009 5080    .2668137   .9595225   -.3771989    .2766028    .2249266   1.0557941   .02022468   -.4011628
    1004 2010 5080   .18558477   .7956493    .2668137   -.3771989    .2766028    .2249266   1.0557941   .02022468
    1004 2011 5080   -.2844345  1.7845465   .18558477    .2668137   -.3771989    .2766028    .2249266   1.0557941
    1004 2012 5080   -.1184521  1.1639197   -.2844345   .18558477    .2668137   -.3771989    .2766028    .2249266
    1004 2013 5080    .4859416  1.0409775   -.1184521   -.2844345   .18558477    .2668137   -.3771989    .2766028
    1004 2014 5080   .04240154   .8076297    .4859416   -.1184521   -.2844345   .18558477    .2668137   -.3771989
    1013 1996 3661  -.19417475  .13855562   .04240154    .4859416   -.1184521   -.2844345   .18558477    .2668137
    1013 1997 3661   .16376306   .1697689  -.19417475   .04240154    .4859416   -.1184521   -.2844345   .18558477
    1013 1998 3661    .8229508  .29470924   .16376306  -.19417475   .04240154    .4859416   -.1184521   -.2844345
    1013 1999 3661     .822606  .17434515    .8229508   .16376306  -.19417475   .04240154    .4859416   -.1184521
    1013 2000 3661   -.7251185  .17690077     .822606    .8229508   .16376306  -.19417475   .04240154    .4859416
    1013 2003 3661   .29130438   .3028035   -.7251185     .822606    .8229508   .16376306  -.19417475   .04240154
    1013 2004 3661   -.2377702    .368258   .29130438   -.7251185     .822606    .8229508   .16376306  -.19417475
    1013 2005 3661       2.875    .380683   -.2377702   .29130438   -.7251185     .822606    .8229508   .16376306
    1013 2007 3661 -.036555093   .4581832       2.875   -.2377702   .29130438   -.7251185     .822606    .8229508
    1034 1996 2834   -.4583333   .5844623 -.036555093       2.875   -.2377702   .29130438   -.7251185     .822606
    1034 1997 2834    .6571429   .4326353   -.4583333 -.036555093       2.875   -.2377702   .29130438   -.7251185
    1034 1998 2834    .7173252  .28056055    .6571429   -.4583333 -.036555093       2.875   -.2377702   .29130438
    1034 1999 2834  -.11191336   .3864849    .7173252    .6571429   -.4583333 -.036555093       2.875   -.2377702
    1034 2000 2834    .2904412   .4805555  -.11191336    .7173252    .6571429   -.4583333 -.036555093       2.875
    1034 2002 2834   -.5374757  1.6351016    .2904412  -.11191336    .7173252    .6571429   -.4583333 -.036555093
    1034 2003 2834   .21597093  1.0823281   -.5374757    .2904412  -.11191336    .7173252    .6571429   -.4583333
    1034 2004 2834   -.2196132   .9869798   .21597093   -.5374757    .2904412  -.11191336    .7173252    .6571429
    1034 2005 2834    .8943521   .5954725   -.2196132   .21597093   -.5374757    .2904412  -.11191336    .7173252
    1034 2006 2834  -.27952167   .6970336    .8943521   -.2196132   .21597093   -.5374757    .2904412  -.11191336
    1034 2007 2834  -.26860252   .8285203  -.27952167    .8943521   -.2196132   .21597093   -.5374757    .2904412
    1038 1997 7830    .3587786  .28671938  -.26860252  -.27952167    .8943521   -.2196132   .21597093   -.5374757
    1038 1998 7830    -.240991   .3199931    .3587786  -.26860252  -.27952167    .8943521   -.2196132   .21597093
    1038 2003 7830    .8436364   .4959008    -.240991    .3587786  -.26860252  -.27952167    .8943521   -.2196132
    1045 2013 4512           . -.41429055    .8436364    -.240991    .3587786  -.26860252  -.27952167    .8943521
    1045 2014 4512    .5985097  .05402936           .    .8436364    -.240991    .3587786  -.26860252  -.27952167
    1045 2015 4512  -.13712312   .2130214    .5985097           .    .8436364    -.240991    .3587786  -.26860252
    1050 1999 3564   -.3220339   .4429039  -.13712312    .5985097           .    .8436364    -.240991    .3587786
    1050 2000 3564   -.3888889    .651834   -.3220339  -.13712312    .5985097           .    .8436364    -.240991
    1050 2003 3564  -.12698413   .4873977   -.3888889   -.3220339  -.13712312    .5985097           .    .8436364
    1050 2004 3564    .8297873  .21087435  -.12698413   -.3888889   -.3220339  -.13712312    .5985097           .
    1050 2006 3564    .2121622  .14485474    .8297873  -.12698413   -.3888889   -.3220339  -.13712312    .5985097
    1050 2007 3564  -.20377088   .2520334    .2121622    .8297873  -.12698413   -.3888889   -.3220339  -.13712312
    1050 2008 3564   -.7403433  1.2842144  -.20377088    .2121622    .8297873  -.12698413   -.3888889   -.3220339
    1050 2009 3564    .6808512    .562706   -.7403433  -.20377088    .2121622    .8297873  -.12698413   -.3888889
    1050 2012 3564   .56446534   .3673891    .6808512   -.7403433  -.20377088    .2121622    .8297873  -.12698413
    1050 2013 3564    .4603978   .4123246   .56446534    .6808512   -.7403433  -.20377088    .2121622    .8297873
    1050 2014 3564           0   .4439706    .4603978   .56446534    .6808512   -.7403433  -.20377088    .2121622
    1050 2015 3564   -.4402333   .9406149           0    .4603978   .56446534    .6808512   -.7403433  -.20377088
    1055 1995 3571   -.4333333  .52395946   -.4402333           0    .4603978   .56446534    .6808512   -.7403433
    1056 1996 3825   .26666668    .406091   -.4333333   -.4402333           0    .4603978   .56446534    .6808512
    1056 1998 3825    .6805556   .4827653   .26666668   -.4333333   -.4402333           0    .4603978   .56446534
    1056 1999 3825   -.4134276  .28058764    .6805556   .26666668   -.4333333   -.4402333           0    .4603978
    1056 2000 3825   1.4276316  .14607291   -.4134276    .6805556   .26666668   -.4333333   -.4402333           0
    1056 2001 3825  -.27192307   .4071935   1.4276316   -.4134276    .6805556   .26666668   -.4333333   -.4402333
    1056 2003 3825      .46375   .5596957  -.27192307   1.4276316   -.4134276    .6805556   .26666668   -.4333333
    1056 2004 3825  -.17663045   .4026596      .46375  -.27192307   1.4276316   -.4134276    .6805556   .26666668
    1056 2005 3825   .11746363   .7083759  -.17663045      .46375  -.27192307   1.4276316   -.4134276    .6805556
    1062 1999 6799   .12222222  1.1767538   .11746363  -.17663045      .46375  -.27192307   1.4276316   -.4134276
    1062 2001 6799   .20918977          .   .12222222   .11746363  -.17663045      .46375  -.27192307   1.4276316
    1062 2008 6799   -.3640898          .   .20918977   .12222222   .11746363  -.17663045      .46375  -.27192307
    1062 2009 6799    .5446749  1.1258447   -.3640898   .20918977   .12222222   .11746363  -.17663045      .46375
    1072 1996 3670   -.1809524   .3984589    .5446749   -.3640898   .20918977   .12222222   .11746363  -.17663045
    1072 1997 3670   -.2195767   .4794515   -.1809524    .5446749   -.3640898   .20918977   .12222222   .11746363
    1072 1998 3670  -.16871166   .6018789   -.2195767   -.1809524    .5446749   -.3640898   .20918977   .12222222
    1072 1999 3670    2.221774  .14846854  -.16871166   -.2195767   -.1809524    .5446749   -.3640898   .20918977
    1072 2000 3670   -.7247899   .4991231    2.221774  -.16871166   -.2195767   -.1809524    .5446749   -.3640898
    1072 2001 3670   .07913996   .4035332   -.7247899    2.221774  -.16871166   -.2195767   -.1809524    .5446749
    1072 2002 3670  -.50654584   .9356121   .07913996   -.7247899    2.221774  -.16871166   -.2195767   -.1809524
    1072 2003 3670    .8886364   .4842205  -.50654584   .07913996   -.7247899    2.221774  -.16871166   -.2195767
    1072 2005 3670    .2482758   .4750666    .8886364  -.50654584   .07913996   -.7247899    2.221774  -.16871166
    1072 2006 3670  -.11010835    .626677    .2482758    .8886364  -.50654584   .07913996   -.7247899    2.221774
    1072 2007 3670 -.071280256   .8348034  -.11010835    .2482758    .8886364  -.50654584   .07913996   -.7247899
    1072 2008 3670    -.390637  1.0792886 -.071280256  -.11010835    .2482758    .8886364  -.50654584   .07913996
    1072 2009 3670    .3892544   .7457429    -.390637 -.071280256  -.11010835    .2482758    .8886364  -.50654584
    1072 2010 3670    .2988216   .8039275    .3892544    -.390637 -.071280256  -.11010835    .2482758    .8886364
    1072 2011 3670   -.1862245   .9430141    .2988216    .3892544    -.390637 -.071280256  -.11010835    .2482758
    1072 2012 3670  -.18085107   .9831554   -.1862245    .2988216    .3892544    -.390637 -.071280256  -.11010835
    1072 2013 3670   .22300263   .9235651  -.18085107   -.1862245    .2988216    .3892544    -.390637 -.071280256
    1072 2014 3670   .08359133   .8882915   .22300263  -.18085107   -.1862245    .2988216    .3892544    -.390637
    1073 1996 7373           .  .08713468   .08359133   .22300263  -.18085107   -.1862245    .2988216    .3892544
    1075 1996 4911   .07627118   .7877945           .   .08359133   .22300263  -.18085107   -.1862245    .2988216
    1075 1997 4911    .3399209   .6116627   .07627118           .   .08359133   .22300263  -.18085107   -.1862245
    1075 1998 4911     .059375   .6283538    .3399209   .07627118           .   .08359133   .22300263  -.18085107
    1075 1999 4911   -.2359375   .8508248     .059375    .3399209   .07627118           .   .08359133   .22300263
    1075 2000 4911    .5425101   .5898113   -.2359375     .059375    .3399209   .07627118           .   .08359133
    1075 2001 4911 -.030576784   .7040493    .5425101   -.2359375     .059375    .3399209   .07627118           .
    1075 2002 4911  -.14475666   .8634696 -.030576784    .5425101   -.2359375     .059375    .3399209   .07627118
    1075 2003 4911   .28475124    .774572  -.14475666 -.030576784    .5425101   -.2359375     .059375    .3399209
    1075 2004 4911   .13146496   .7237033   .28475124  -.14475666 -.030576784    .5425101   -.2359375     .059375
    1075 2005 4911  -.00839334   .8361715   .13146496   .28475124  -.14475666 -.030576784    .5425101   -.2359375
    1075 2006 4911    .1896268   .6801202  -.00839334   .13146496   .28475124  -.14475666 -.030576784    .5425101
    1075 2007 4911  -.13076451   .8287032    .1896268  -.00839334   .13146496   .28475124  -.14475666 -.030576784
    1075 2008 4911  -.16109656  1.0630713  -.13076451    .1896268  -.00839334   .13146496   .28475124  -.14475666
    end

  • #2
    "What I did is the following, but I do not think that it is true." - this is not very helpful. What exactly is wrong? Also, remove all the quietly statements when you are debugging - why would you not want to see what is happening when you are debugging?

    Also, it often helps to run the model without the loop to make sure it works. When I put 1995 in for the year and ran the reg statement outside the loop, it notes that there are no usable observations. When you start with year==1995, the program won't run because you don't have values for the lagged variables. You'll need to change your loop beginning to make sure you only use years for which all your variables are defined.

    You don't need the xi - that is not needed in recent versions of Stata.

    The program doesn't run as specified partially because gvkey1 is treated a factor variable so it makes a bunch of dummies. There is no single parameter labelled gvkey1.

    Also, instead of creating all these lag variables, you might use the factor variable notation. Look it up.

    Comment

    Working...
    X