{"id":489,"date":"2015-11-07T20:09:47","date_gmt":"2015-11-08T01:09:47","guid":{"rendered":"http:\/\/csclub.uwaterloo.ca\/~k55chen\/?p=489"},"modified":"2017-09-20T15:53:48","modified_gmt":"2017-09-20T19:53:48","slug":"stata-a-loop-of-cross-sectional-regressions-for-calculating-abnormal-accruals","status":"publish","type":"post","link":"https:\/\/www.kaichen.work\/?p=489","title":{"rendered":"A loop of cross-sectional regressions for calculating abnormal accruals in Stata"},"content":{"rendered":"<p>I write a loop of cross-sectional regressions for calculating abnormal accruals. This program can be easily modified and replaced with\u00a0Jones, modified Jones, or Dechow and Dichev model.<\/p>\n<p>I add detailed comments in the program to help you prepare the input file.<\/p>\n<pre class=\"lang:default decode:true EnlighterJSRAW  \">set more off\r\ncd \"D:\\Dropbox\"\r\n\r\nuse funda_full_regdata, clear\r\n\/\/This is the input file. Download required Compustat data and then\r\n\/\/compute all dependent variable and independent variable (e.g., total\r\n\/\/accruals scaled by total assets (ta), delta sales scaled by total\r\n\/\/asset (dsale), PP&amp;E scaled by total assets (ppe).\r\n\r\nkeep if fyear&gt;=2003 &amp; fyear&lt;=2014\r\n\r\n* Remove obs without required data\r\nforeach v in ta dsale ppe {\r\n  drop if `v'==.\r\n}\r\n\r\n* Handle outliers for each industry year\r\nge trunc=0\r\nlocal vlist \"ta dsale ppe\"\r\negen groupid=group(fyear sic2)\r\nsum groupid, meanonly\r\nlocal num=r(max)\r\nforvalues i=1\/`num' {\r\n  foreach v in `vlist' {\r\n    _pctile `v' if groupid==`i', p(1 99)\r\n    replace trunc=1 if groupid==`i' &amp; (`v'&lt;r(r1) | `v'&gt;r(r2)) \/\/drop top and bottom 1%. You can do winsorize as well\r\n  }\r\n}\r\ndrop if trunc==1\r\ndrop trunc groupid\r\n\r\n* Require at least 20 obs for each estimation\r\nbysort groupid: egen obsnum=count(datadate)\r\nkeep if obsnum&gt;=20\r\ndrop groupid\r\negen groupid=group(fyear sic2)\r\nsum groupid, meanonly\r\nlocal num=r(max)\r\n\r\n* Run cross-sectional regressions\r\nge da=.\r\nge regobs=.\r\nge df=.\r\nge r2a=.\r\n\r\nforeach x in dsale ppe _cons {\r\n  ge `x'_b=.\r\n  ge `x'_se=.\r\n}\r\n\r\nforvalues i=1\/`num' {\r\n  regress ta dsale ppe if groupid==`i'\r\n  predict resid if groupid==`i', residual\r\n  replace da=resid if groupid==`i'\r\n  drop resid\r\n  replace regobs=e(N) if groupid==`i'\r\n  replace df=e(df_r) if groupid==`i'\r\n  replace r2a=e(r2_a) if groupid==`i'\r\n  foreach x in dsale _cons {\r\n    replace `x'_b=_b[`x'] if groupid==`i'\r\n    replace `x'_se=_se[`x'] if groupid==`i'\r\n  }\r\n}\r\n\r\nsave da, replace\r\n\/\/This is the output file. It includes:\r\n\/\/DA - discretionary accruals (residuals)\r\n\/\/Regression coefficients such as dsale_b, ppe_b\r\n\/\/Standard error for each coefficient such as dsale_se, ppe_se\r\n\/\/Some regression stats such as number of obs (regobs), (degree of\r\n\/\/freedom (df), and adjusted R squared (r2a)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I write a loop of cross-sectional regressions for calculating abnormal accruals. This program can be easily modified and replaced with\u00a0Jones, modified Jones, or Dechow and Dichev model. I add detailed comments in the program to help you prepare the input &hellip; <a href=\"https:\/\/www.kaichen.work\/?p=489\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"_links":{"self":[{"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/posts\/489"}],"collection":[{"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=489"}],"version-history":[{"count":9,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/posts\/489\/revisions"}],"predecessor-version":[{"id":898,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/posts\/489\/revisions\/898"}],"wp:attachment":[{"href":"https:\/\/www.kaichen.work\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=489"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=489"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}