I am currently constructing a 5-window and 7-window moving average to smooth out a time-series data I am analysing. As expected, for the first and last 2 observations of the time-series I cannot calculate a moving average as there are no previous year-quarter observations available. Therefore, I would like to make the window smaller for these two observations, i.e. a window of 3 for the 2nd observation, and a window of 1 for the first observation (basically moving average of observation= value observation)
In theory, I understand this, unfortunately I am not that experienced with Stata so I have no clue how to code this. If someone could help me, I would be very grateful.
I am aware that there are commands available in Stata that allow me to do this easier, but I am interested in writing it as a code so I am able to understand better the process behind it and improve my knowledge of Stata.
The command I am using to create the 5-window MA: gen c1 = (l2.earnings_nsa+2 (l1.earnings_nsa+earnings_nsa+f1.earnings_nsa)+f2. earnings_nsa)/8
My data looks as follows:
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input float(yq earnings_nsa) -84 .49 -83 .49 -82 .4833333 -81 .51 -80 .52 -79 .5233333 -78 .5233333 -77 .54333335 -76 .56 -75 .5966667 -74 .6133333 -73 .65 -72 .6866667 -71 .72 -70 .76 -69 .79 -68 .82 -67 .8466667 -66 .87 -65 .8866667 -64 .9 -63 .91 -62 .9133334 -61 .9233333 -60 .93 -59 .9266667 -58 .8866667 -57 .85 -56 .8666667 -55 .9333333 -54 .98 -53 1.01 -52 1.0433333 -51 1.08 -50 1.11 -49 1.1433333 -48 1.16 -47 1.18 -46 1.2233334 -45 1.25 -44 1.26 -43 1.2566667 -42 1.25 -41 1.2433333 -40 1.2733333 -39 1.3 -38 1.32 -37 1.3766667 -36 1.4166666 -35 1.44 -34 1.4533334 -33 1.4833333 -32 1.5066667 -31 1.5166667 -30 1.5233333 -29 1.5766667 -28 1.6066667 -27 1.6233333 -26 1.6366667 -25 1.65 -24 1.66 -23 1.6633333 -22 1.6533333 -21 1.68 -20 1.7033334 -19 1.7266667 -18 1.75 -17 1.78 -16 1.7966666 -15 1.8233334 -14 1.8366667 -13 1.89 -12 1.9133333 -11 1.9233333 -10 1.93 -9 1.9566667 -8 1.9566667 -7 1.9733334 -6 1.9833333 -5 2.0233333 -4 2.0566666 -3 2.0833333 -2 2.0666666 -1 2.0866666 0 2.14 1 2.14 2 2.14 3 2.1566668 4 2.17 5 2.1933334 6 2.1933334 7 2.2366667 8 2.2633333 9 2.27 10 2.2633333 11 2.2966666 12 2.3133333 13 2.3333333 14 2.3333333 15 2.37 end format %tq yq
Rajendra
0 Response to Adjusting the window of a Moving Average
Post a Comment