Chapter 8 Time Series (OSCM)
We start by loading the two packages we need for this chapter, forecast
and zoo
:
For this section we will use data on the number of tourists to Australia from 2005 to 2015 (quarterly), taken from the text Forecasting: Principles and Practice. Our data is stored in an object called austourists
. Because we have quarterly data from 2005 to 2015 our data has 44 observations (11 years * 4 quarters):
## [1] 42.20566 24.64917 32.66734 37.25735 45.24246 29.35048 36.34421 41.78208
## [9] 49.27660 31.27540 37.85063 38.83704 51.23690 31.83855 41.32342 42.79900
## [17] 55.70836 33.40714 42.31664 45.15712 59.57608 34.83733 44.84168 46.97125
## [25] 60.01903 38.37118 46.97586 50.73380 61.64687 39.29957 52.67121 54.33232
## [33] 66.83436 40.87119 51.82854 57.49191 65.25147 43.06121 54.76076 59.83447
## [41] 73.25703 47.69662 61.09777 66.05576
8.1 Moving Average
The most simple method for forecasting is the moving average approach, which forecasts a given period by averaging the previous k periods together. We can apply this method with the rollmean()
function from the zoo
package. In the code below, we calculate the moving average for our data with a window of 2. Note that because we cannot calculate a forecast for the first two periods, we add two NA
values to the beginning of the vector to indicate missingness.
## [1] NA NA 33.42742 28.65825 34.96234 41.24991 37.29647 32.84734
## [9] 39.06314 45.52934 40.27600 34.56301 38.34383 45.03697 41.53773 36.58099
## [17] 42.06121 49.25368 44.55775 37.86189 43.73688 52.36660 47.20671 39.83951
## [25] 45.90647 53.49514 49.19510 42.67352 48.85483 56.19033 50.47322 45.98539
## [33] 53.50176 60.58334 53.85277 46.34986 54.66022 61.37169 54.15634 48.91098
## [41] 57.29762 66.54575 60.47683 54.39720 63.57676
Our forecast for the next period Q1 2016 is 63.57676, the last element in the movingAvg
vector. This is the average of the observed values from the previous two periods, 61.09777 and 66.05576, which are stored in the austourists
vector.
8.2 Exponential Smoothing
We can implement exponential smoothing with the ses()
function from the forecast
package. The h
parameter is the number of periods into the future we want to forecast, and alpha
is our desired value for the smoothing parameter.
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 45 61.52621 48.43934 74.61308 41.51157 81.54085
Note that in the output, “Point Forecast” represents the model’s point estimate of the next period. “Lo 80” and “Hi 80” represent an 80% prediction interval around this point estimate, and “Lo 95” and “Hi 95” represent a 95% prediction interval.