Analysis of Times Series with Stata |
1. Computing the trend with moving averages |
generate date = tq(2012q1) + _n -1 format %tq dateThis will generate a new varaible with an year/quarter indicator for each case of the time series. If we look at the data window, we see it as follows:
Now we have to tell Stata about the frequency of the time series. For this, we declare the "Sales" variable as a times series according to the "date" variable we have just created, with:
tsset dateWe can first get the time series plot of "Sales". We do it with the command:
tsline SalesThe graph we obtain is:
As it can be seen in the time plot, we can observe an increasing trend and a strong seasonality. The series is below the trend during the first and fourth quarters, and above the trend during the second and third quarters. To identify the trend, we start with the method of moving averages. Considering that this is a quarterly series, and therefore the frequency is even (4 quarters within a year), we will have to use a centered moving average of order 4, so that the moving average gets centered with the original series. We first compute a moving average without centering and we put it at the second case of each group of 4 consecutive cases used to compute the mean:
tssmooth ma MA4= Sales, window(1 1 2) replace MA4 = . in 1 replace MA4 = . in 15 replace MA4 = . in 16The first commands computes the moving average. At the "window" option it is set how the moving average should be computed. The first number in the brackets says how many cases before the current case will be used, the second one tells to include the current case in the mean, and the third one how many cases after the current case are to be used. Therefore, (1 1 2) tells Stata to compute an average using 4 cases, 1 before, the actual case, and 2 after, all consecutive. The three other commands eliminate the first case and the last two cases, since there is not enough information to compute a complete moving average of order4. Now the data window looks as follows:
If the order of the moving average was uneven, we would stop here. Considering it is even, we have to perform a second step, that is, centering the moving average. To compute the centered moving average we now compute the moving average of order 2 using the moving average of order 4 we have already computed. We can do it with the command:
tssmooth ma MA4C = MA4, window(1 1 0) replace MA4C = . in 2 replace MA4C = . in 15The first command works as before, it is telling Stata to compute the moving average, using MA4 which are the moving averages of order 4 we computed previously, and including only the previous and the current case (1 1 0). Line 2 and 3 eliminate the cases when there is not enough information to compute the moving averages. The window looks now as follows:
We already have the trend of the series. To confirm that it is really the trend, we can now plot the original series together with the trend. We can do it with the command:
tsline Sales MA4CThe graph is: