Hi Statalist colleagues,
I am trying to run a rolling regression with regression window 2006-2010, 2007-2011, 2008-2012, ... 2016-2020=> 11 windows in total.
Usually when I had annual data, I could just use stata command rolling, window().
But the problem is that my dataset is not a annual data, but looks as follows with irregular number of data in each year.
In this case,
1. I want to regress sp500 on chg_pay but with rolling window.
2. I want to then plot the time series of coefficients for the 11 windows as a line plot.
Could you please let me know how this can be done without writing codes for each window separately?
What I have been doing is as follows:
Thanks much!
I am trying to run a rolling regression with regression window 2006-2010, 2007-2011, 2008-2012, ... 2016-2020=> 11 windows in total.
Usually when I had annual data, I could just use stata command rolling, window().
But the problem is that my dataset is not a annual data, but looks as follows with irregular number of data in each year.
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input float date double(chg_pay sp500ret) 16806 0 .002699353764334944 16807 -3.06 .9642661927006246 16813 0 -.6267879872783877 16820 0 .5570752695287728 16827 0 .7259659913377181 16834 0 -.8795154476552325 16835 -1.86 -.5284534048121459 16841 0 -.14297327046401964 16848 0 .7407178393196157 16855 0 -.351535179790019 16862 0 -.16139690217960023 16869 0 -.4862526350060681 16870 .89 .7346771008208686 16876 0 .17956659475244496 16883 0 -.25766760374676956 16890 0 -.19727134079343678 16897 0 -.16791975171757123 16898 .83 -1.0336369214625885 16904 0 .07735547418905586 16911 0 .11973841298658083 16918 0 .3579838662962631 16925 0 .3403068087995287 16926 -3.33 1.0314307163522995 16932 0 -1.262520562446534 16939 0 -.6645362921561926 16946 0 1.1495829417599523 16953 0 1.2329523359992711 16954 -4.27 .19502276200795698 16960 0 .14709459466515362 16967 0 2.125841051352162 16974 0 -.5043920367085564 16981 0 2.1610077482290935 16988 0 .2754370921263627 16989 -1.33 -.6754211553848921 16995 0 -1.2944842425878456 17002 0 -.8475443658016668 17009 0 -.3945381797991865 17016 0 .16549140816095598 17017 -.96 -.06988812790753585 17023 0 .48091770998057726 17030 0 .16310202500695592 17037 0 .23962460327804358 17044 0 -.03308182471537524 17045 .15 .553265095135469 17051 0 -.4761053916451097 17058 0 -.1304631441617743 17065 0 -.5172298374870543 17072 0 .19346662334887021 17079 0 .25006625790326975 17080 -3.62 -.2685599897141011 17086 0 .9549206042762659 17093 0 .07702558203199761 17100 0 .49725826937532247 17107 0 -.007332058951659004 17108 -1.35 -.2157402280056564 17114 0 -.5120184856426069 17121 0 .23123454381466235 17127 0 .24223333011963444 17135 0 .08351569993028107 17142 0 -.3943097671421092 17143 1.16 .18307521128408943 17149 0 .8764006329381457 17156 0 -.3442699506921887 17163 0 -.14355526877846136 17170 0 .12276079457049782 17171 2.03 -.6087524804457067 17177 0 .6351502020245148 17184 0 -.29478393312007967 17191 0 -1.12592255158096 17198 0 .5595287287383011 17199 -1.39 .16965360372751537 17205 0 -.11576327838745959 17219 0 -.062332570010925625 17226 0 -.2579759755564126 17233 0 .7193847085361194 17234 .07 .06818373557433421 17240 0 .37272142996862545 17247 0 -.03524710561841893 17254 0 .37823843877755614 17261 0 .3344615129269535 17268 0 .6217702105595135 17275 0 -.11833738602358146 17282 0 -.06852223261658574 17289 0 .46165343950139714 17290 -.49 .21729574543345453 17296 0 -1.3793620770045867 17303 0 -.08351759251497004 17310 0 -.9582420409548487 17317 0 .027849212081831887 17318 .89 .373462463868135 17324 0 -1.75270669093065 17331 0 .48806865473609573 17338 0 .6402575656946352 17345 0 -.0400368612295976 17352 0 . 17353 .41 .35911318175458895 17359 0 1.906700336800049 17366 0 .4490394080175486 17373 0 -2.3334415236812123 17380 0 .4597997281687194 end format %td date
1. I want to regress sp500 on chg_pay but with rolling window.
2. I want to then plot the time series of coefficients for the 11 windows as a line plot.
Could you please let me know how this can be done without writing codes for each window separately?
What I have been doing is as follows:
Code:
**** 2006-2010 keep if year(date)>=2006 & year(date)<=2010 reg sp500 chg_pay . . . keep if year(date)>=2007 & year(date)<=2011 reg sp500 chg_pay
Comment