Announcement

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

  • Loop through group, run regression and store residual in STATA

    Hello, below is the snippet of my dataset.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str6 gvkey str4 fyear str2 sic_2 float(y x_1 x_2 x_3) str7 sic_year float resid
    "001004" "2000" "50"   .06220395   .0013495313 1.1798345    .3178605 "50_2000" .
    "001004" "2001" "50"  -.04746714   .0014247977  .9100482   .04614065 "50_2001" .
    "001004" "2002" "50"   .04890601    .001408056  .8537565 -.064236924 "50_2002" .
    "001004" "2003" "50"   .02122277   .0014564075  .9495165  -.13965492 "50_2003" .
    "001004" "2004" "50"  .071815275   .0014098566 1.0543585  -.21068333 "50_2004" .
    "001004" "2005" "50"  -.05528591    .001365691 1.2254127   -.2238163 "50_2005" .
    "001004" "2006" "50"   -.0216986   .0010216393  1.084132   -.3307557 "50_2006" .
    "001004" "2007" "50"  .015853763   .0009366514 1.2971864 -.036582794 "50_2007" .
    "001004" "2008" "50"    .0473205    .000734209  1.045496   .05273456 "50_2008" .
    "001004" "2009" "50"   .11118314    .000725947    .98159  -.30753365 "50_2009" .
    "001004" "2010" "50"   .07234841   .0006662039 1.1830329  -.19900575 "50_2010" .
    "001004" "2011" "50"   .05530053   .0005869485 1.2176235   -.0543526 "50_2011" .
    "001004" "2012" "50"  .074192055   .0004554454  .9869957   .06016433 "50_2012" .
    "001004" "2013" "50"   .06542187   .0004679676  .9523141   .20623332 "50_2013" .
    "001004" "2014" "50" -.019549897   .0004546488  .7248465  -.03105251 "50_2014" .
    "001004" "2015" "50"   .02118812    .000660066 1.0974257  -.06930693 "50_2015" .
    "001004" "2016" "50"  .015116843   .0006934332 1.2257125   .01338326 "50_2016" .
    "001004" "2017" "50"   .04274982   .0006648494 1.1623563   -.2017818 "50_2017" .
    "001004" "2018" "50"   .04420542   .0006558668 1.3457073    -.024595 "50_2018" .
    "001010" "2000" "37"  .010411405  .00028063086 .12454397  .021692766 "37_2000" .
    "001010" "2001" "37"   .03009619  .00026353935 .09658717  .014020292 "37_2001" .
    "001010" "2002" "37"   .04439849  .00026859337  .0841503  -.54411644 "37_2002" .
    "001013" "2000" "36"   .13237439    .000597897 1.9658254    .5291986 "36_2000" .
    "001013" "2001" "36"   .02392646  .00025185745  .6051631     .341292 "36_2001" .
    "001013" "2002" "36"  .024202904    .000400048  .4191303   .10981318 "36_2002" .
    "001013" "2003" "36"  .033997554    .000873973   .675756   -.0097011 "36_2003" .
    "001013" "2004" "36"  .002390315   .0007710695  .6047498  -.29678464 "36_2004" .
    "001013" "2005" "36"   .04103354    .000700231  .8187101  -.07891604 "36_2005" .
    "001013" "2006" "36"   .05674267   .0006514658   .835114  -.02625407 "36_2006" .
    "001013" "2007" "36"   .08799802   .0006205784  .8205287  -.08328162 "36_2007" .
    "001013" "2008" "36"   .09950136  .00056663645  .8252493    .2604828 "36_2008" .
    "001013" "2009" "36"   .04159292  .00052056223 .51884437   -.0832379 "36_2009" .
    "001019" "2000" "73"   .15912886    .034082003 1.4486214  -.08677278 "73_2000" .
    "001019" "2008" "73"    .2008777    .029256875  2.393417   .10924517 "73_2008" .
    "001019" "2009" "73"   .20647435    .029863227 2.3315117    .1684286 "73_2009" .
    "001019" "2010" "73"   .02536875     .03060163 2.2165678  .027939286 "73_2010" .
    "001019" "2011" "73"   .05960198     .03384553  2.420632    -.079537 "73_2011" .
    "001019" "2012" "73"    .0902501    .032950014  2.434018  -.06408778 "73_2012" .
    "001019" "2013" "73"   .11570624     .02844303 2.1564083  -.11411343 "73_2013" .
    "001019" "2014" "73"   .05207752      .0279236 2.2290573   .09695074 "73_2014" .
    "001019" "2015" "73"   .14463449    .027105413 2.0696337   .03591467 "73_2015" .
    "001019" "2016" "73"   .02311432    .030574495 2.2940044   .05533984 "73_2016" .
    "001019" "2017" "73"    .0929701     .03047201 2.2311606  -.13779444 "73_2017" .
    "001019" "2018" "73"   .01968181    .032803018  2.550172  -.17536493 "73_2018" .
    "001021" "2000" "38"   .11132561     .07150007 1.8137423    .0940941 "38_2000" .
    "001021" "2001" "38"   .08804273     .08614749  2.071933    .3414886 "38_2001" .
    "001021" "2002" "38"   .13317892     .11580776 2.3262305   .23659526 "38_2002" .
    "001021" "2003" "38"    .0588535     .12738854 2.2985988   -.2278981 "38_2003" .
    "001021" "2004" "38"    .1457644     .16545334  3.281436  -.54632694 "38_2004" .
    "001021" "2005" "38"    .1801441      .1601281 3.7045636  -.29831865 "38_2005" .
    "001021" "2006" "38"   .11455905     .12265424 3.0661106   -.4565191 "38_2006" .
    "001021" "2007" "38"  -.10766334     .06973015 2.0026498   -.3876996 "38_2007" .
    "001034" "2000" "28"  .028548626   .0008618713  .7763685   -.0639474 "28_2000" .
    "001034" "2001" "28"  .074131526   .0006209502  .6054203   -.1588217 "28_2001" .
    "001034" "2002" "28"   .06786588   .0004184086  .5149615   -.0278338 "28_2002" .
    "001034" "2003" "28"   .06835228   .0004353649  .5647923  -.01837022 "28_2003" .
    "001034" "2004" "28"   .07992296   .0004293194  .5750648   .33738625 "28_2004" .
    "001034" "2005" "28"   .12340344  .00049904134 .27627778  -.05000943 "28_2005" .
    "001034" "2006" "28"  .026446624   .0006159976 .40275645  -.04225559 "28_2006" .
    "001037" "2000" "36"  -.03715847     .21857923 3.4740984  -2.8002186 "36_2000" .
    "001038" "2000" "78"   .03655604   .0008411809 1.0218674  -.10658182 "78_2000" .
    "001038" "2001" "78"   .09610662    .000954869 1.2809626   -.4297522 "78_2001" .
    "001038" "2002" "78"   .10064886    .000781757 1.4005723  .006840373 "78_2002" .
    "001045" "2000" "45"   .12890786  .00004102732  .8083614   .03036022 "45_2000" .
    "001045" "2001" "45"  .019494144  .00003814901  .7234197   .06347995 "45_2001" .
    "001045" "2002" "45" -.033829663  .00003044974  .5267501 -.004293414 "45_2002" .
    "001045" "2003" "45"   .01985661 .000033039283 .57620513  -.03981234 "45_2003" .
    "001045" "2004" "45"   .02444596 .000034094784  .6356972  -.07047392 "45_2004" .
    "001045" "2005" "45"   .03558892 .000034754805  .7198415 -.064331144 "45_2005" .
    "001045" "2006" "45"   .06573996 .000033904053  .7649771  -.01129005 "45_2006" .
    "001045" "2007" "45"  .066392176 .000034311204  .7855893 -.029850746 "45_2007" .
    "001045" "2008" "45"  -.04879073 .000035000525  .8318225   .13471702 "45_2008" .
    "001045" "2009" "45"   .03694141  .00003972194   .791142  -.08949354 "45_2009" .
    "001045" "2010" "45"   .04878528  .00003931127  .8715308 -.072804466 "45_2010" .
    "001045" "2011" "45"   .02710459  .00003985969  .9575096 -.033203125 "45_2011" .
    "001045" "2012" "45"   .05363133  .00004193224 1.0422257  -.07786816 "45_2012" .
    "001045" "2013" "45"  .028711187  .00004253509 1.1361973   -.6779243 "45_2013" .
    "001045" "2014" "45"   .07285113 .000023652963  1.008799   .03926392 "45_2014" .
    "001045" "2015" "45"   .14276576 .000022846176  .9364648  .018505404 "45_2015" .
    "001045" "2016" "45"   .13475163 .000020654756  .8299081  -.04186719 "45_2016" .
    "001045" "2017" "45"   .09252252  .00001950306  .8231657  -.04552015 "45_2017" .
    "001045" "2018" "45"  .068740755  .00001945677  .8666239 -.023873454 "45_2018" .
    "001050" "2000" "35"    .0515145    .019579817 1.7586005 -.023045445 "35_2000" .
    "001050" "2001" "35"    .0783956    .017890368  1.627916    .2167776 "35_2001" .
    "001050" "2002" "35"   .06979068     .01885725 1.4874034     .202112 "35_2002" .
    "001050" "2003" "35"  .034128156     .02142383 1.4602267  -.02585856 "35_2003" .
    "001050" "2004" "35"   .04573067    .024298975 1.6855227  -.29535404 "35_2004" .
    "001050" "2005" "35"   .05952902     .02301973 1.8765912   -1.239336 "35_2005" .
    "001050" "2006" "35"   -.0967366     .02331002 3.1552215  -2.3448484 "35_2006" .
    "001050" "2007" "35"   .06263848     .01582579 3.7341425   .28586125 "35_2007" .
    "001050" "2008" "35"   .05259232    .010358937  2.257109     .817372 "35_2008" .
    "001050" "2009" "35"   .12408242    .008332153 1.1580442 -.013473092 "35_2009" .
    "001050" "2010" "35"   .03790234     .01290073 1.8138683  .018190028 "35_2010" .
    "001050" "2011" "35"   .11683224    .013370593 1.8610796   .05535425 "35_2011" .
    "001050" "2012" "35"   .21209906     .01260319  1.702086   -.7847375 "35_2012" .
    "001050" "2013" "35"    .2569604     .01062654 2.0967972   -.7002891 "35_2013" .
    "001050" "2014" "35"   .04666089    .002869144  .7552075   -.2989792 "35_2014" .
    "001050" "2015" "35"  .030497266    .002413331   .886711  -.11967468 "35_2015" .
    "001050" "2016" "35"    .1162271   .0016699537  .6963891   .12016987 "35_2016" .
    "001050" "2017" "35"  .013175997    .002005479  .6919925  .015466254 "35_2017" .
    end

    For each group of industry-year (sic_2 and fyear), I'd like to run a regression (y = x_1 + x_2 + x_3 + e) and store the residual to the variable "resid"

    My take on this is first creating a variable that identifies the industry-year (sic_year) then loop through each group of sic_year, run the regression and get the residual:

    Code:
    levelsof sic_year, local(ind_year)
    
    foreach i of local ind_year{
            display "industry year `i'"
            regress y x_1 x_2 x_3 if sic_year == `i'
            replace resid = _rss if sic_year == `i'
    }
    But I keep getting the error message "10_2000 invalid name". I'm not sure what's the issue here. Any ideas / suggestions are greatly appreciated! Thank you

  • #2
    One issue is that sic_year is a string variable, so in your -if- condition, you need to use quotes: ... if sic_year == "`i'"

    Second, after running the regression, you will need to use predict to generate the residuals. So something like this (warning: untested code because your extract only had one observation per sic_year, so that no regression could be run):

    Code:
    levelsof sic_year, local(ind_year)
    
    foreach i of local ind_year{
            display "industry year `i'"
            regress y x_1 x_2 x_3 if sic_year == "`i'"
            predict r, resid
            replace resid = r if sic_year == "`i'"
            drop r
    }

    Comment


    • #3
      Originally posted by Hemanshu Kumar View Post
      One issue is that sic_year is a string variable, so in your -if- condition, you need to use quotes: ... if sic_year == "`i'"

      Second, after running the regression, you will need to use predict to generate the residuals. So something like this (warning: untested code because your extract only had one observation per sic_year, so that no regression could be run):

      Code:
      levelsof sic_year, local(ind_year)
      
      foreach i of local ind_year{
      display "industry year `i'"
      regress y x_1 x_2 x_3 if sic_year == "`i'"
      predict r, resid
      replace resid = r if sic_year == "`i'"
      drop r
      }
      Your solution works perfectly. Thank you so much!

      Comment

      Working...
      X