I have a panel dataset of multiple firms (gvkeys) across multiple quarters.
I am trying to get gvkey- and quarter-specific AR(1) predictions for three focal variables (revenue_a, epssurp, marketshare) and save them as variables. In other words, I am trying to predict marketshare of gvkey i at quarter t using marketshare of gvkey i at quarter t-1.
My dataset looks like this:
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input long gvkey float(quarter revenue_a epssurp marketshare) 1045 188 .1980463 . 51.84315 1045 189 .21362144 . 7.200194 1045 190 .238256 . 9.752722 1045 191 .2172393 . 10.783447 1045 192 .1973652 . 9.241472 1045 193 .2025437 . 8.251666 1045 194 .1990759 . 8.9912815 1045 195 .19899364 . 49.23114 1045 196 .19855045 . 25.69328 1045 197 .2192003 . 49.48811 end format %tq quarter
Just so you know, my very slow yet working code based on approach (1) is as follows:
Code:
levelsof gvkey, local(gvkeylev) foreach x of varlist revenue_a epssurp marketshare { foreach i of local gvkeylev { rolling3 _b _se , window(20) step(1) predict p(yhat) saving(`i'.dta, replace) : arima `x', arima(1,0,0), if gvkey==`i' } }
Code:
program myprog, rclass levelsof gvkey, local(gvkeylev) foreach i of local gvkeylev { if gvkey == `i' { arima revenue_a, arima(1,0,0) predict rhat, y gen pred_rev = rhat } end rangerun myprog, interval(quarter -20 -1) use(revenue_a) by(permno) *** To be repeated for other two variables
Please throw me some ideas to accomplish the goal more efficiently! Thank you for your help in advance.
0 Response to Rolling AR Forecast by panel using Rangerun
Post a Comment