Skip to main content

3  The forecaster’s toolbox

In this chapter, we discuss some general tools that are useful for many different forecasting situations. We will describe some benchmark forecasting methods, ways of making the forecasting task simpler using transformations and adjustments, methods for checking whether a forecasting method has adequately utilised the available information, and techniques for computing prediction intervals.

Each of the tools discussed in this chapter will be used repeatedly in subsequent chapters as we develop and explore a range of forecasting methods.

3.1 Some simple forecasting methods

Some forecasting methods are extremely simple and surprisingly effective. We will use the following four forecasting methods as benchmarks throughout this book.

Average method

Here, the forecasts of all future values are equal to the average (or “mean”) of the historical data. If we let the historical data be denoted by y_{1},\dots,y_{T}, then we can write the forecasts as \hat{y}_{T+h|T} = \bar{y} = (y_{1} + \dots + y_{T})/T. The notation \hat{y}_{T+h|T} is a short-hand for the estimate of y_{T+h} based on the data y_1,\dots,y_T.

meanf(y, h)
# y contains the time series
# h is the forecast horizon

Naïve method

For naïve forecasts, we simply set all forecasts to be the value of the last observation. That is, \hat{y}_{T+h|T} = y_{T}. This method works remarkably well for many economic and financial time series.

naive(y, h)
rwf(y, h) # Equivalent alternative

Because a naïve forecast is optimal when data follow a random walk (see Section 8.1), these are also called random walk forecasts.

Seasonal naïve method

A similar method is useful for highly seasonal data. In this case, we set each forecast to be equal to the last observed value from the same season (e.g., the same month of the previous year). Formally, the forecast for time T + h is written as \hat{y}_{T+h|T} = y_{T+h-m(k+1)}, where m = the seasonal period, and k is the integer part of (h - 1)/m (i.e., the number of complete years in the forecast period prior to time T + h). This looks more complicated than it really is. For example, with monthly data, the forecast for all future February values is equal to the last observed February value. With quarterly data, the forecast of all future Q2 values is equal to the last observed Q2 value (where Q2 means the second quarter). Similar rules apply for other months and quarters, and for other seasonal periods.

snaive(y, h)

Drift method

A variation on the naïve method is to allow the forecasts to increase or decrease over time, where the amount of change over time (called the drift) is set to be the average change seen in the historical data. Thus the forecast for time T + h is given by \hat{y}_{T+h|T} = y_{T} + \frac{h}{T - 1}\sum_{t=2}^T (y_{t} - y_{t-1}) = y_{T} + h \left( \frac{y_{T} - y_{1}}{T - 1} \right). This is equivalent to drawing a line between the first and last observations, and extrapolating it into the future.

rwf(y, h, drift = TRUE)

Examples

Figure 3.1 shows the first three methods applied to the quarterly beer production data.

# Set training data from 1992 to 2007
beer2 <- window(ausbeer, start = 1992, end = c(2007, 4))
# Plot some forecasts
autoplot(beer2) +
  autolayer(meanf(beer2, h = 11), series = "Mean", PI = FALSE) +
  autolayer(naive(beer2, h = 11), series = "Naïve", PI = FALSE) +
  autolayer(snaive(beer2, h = 11), series = "Seasonal naïve", PI = FALSE) +
  ggtitle("Forecasts for quarterly beer production") +
  xlab("Year") +
  ylab("Megalitres") +
  guides(colour = guide_legend(title = "Forecast"))
Figure 3.1: Forecasts of Australian quarterly beer production.

In Figure 3.2, the non-seasonal methods are applied to a series of 200 days of the Google daily closing stock price.

autoplot(goog200) +
  autolayer(meanf(goog200, h = 40), series = "Mean", PI = FALSE) +
  autolayer(rwf(goog200, h = 40), series = "Naïve", PI = FALSE) +
  autolayer(rwf(goog200, drift = TRUE, h = 40), series = "Drift", PI = FALSE) +
  ggtitle("Google stock (daily ending 6 Dec 2013)") +
  xlab("Day") +
  ylab("Closing Price (US$)") +
  guides(colour = guide_legend(title = "Forecast"))
Figure 3.2: Forecasts based on 200 days of the Google daily closing stock price.

Sometimes one of these simple methods will be the best forecasting method available; but in many cases, these methods will serve as benchmarks rather than the method of choice. That is, any forecasting methods we develop will be compared to these simple methods to ensure that the new method is better than these simple alternatives. If not, the new method is not worth considering.

3.2 Transformations and adjustments

Adjusting the historical data can often lead to a simpler forecasting task. Here, we deal with four kinds of adjustments: calendar adjustments, population adjustments, inflation adjustments and mathematical transformations. The purpose of these adjustments and transformations is to simplify the patterns in the historical data by removing known sources of variation or by making the pattern more consistent across the whole data set. Simpler patterns usually lead to more accurate forecasts.

Calendar adjustments

Some of the variation seen in seasonal data may be due to simple calendar effects. In such cases, it is usually much easier to remove the variation before fitting a forecasting model. The monthdays() function will compute the number of days in each month or quarter.

For example, if you are studying the monthly milk production on a farm, there will be variation between the months simply because of the different numbers of days in each month, in addition to the seasonal variation across the year.

dframe <- cbind(Monthly = milk, DailyAverage = milk / monthdays(milk))
autoplot(dframe, facet = TRUE) +
  xlab("Years") +
  ylab("Pounds") +
  ggtitle("Milk production per cow")
Figure 3.3: Monthly milk production per cow.

Notice how much simpler the seasonal pattern is in the average daily production plot compared to the total monthly production plot. By looking at the average daily production instead of the total monthly production, we effectively remove the variation due to the different month lengths. Simpler patterns are usually easier to model and lead to more accurate forecasts.

A similar adjustment can be done for sales data when the number of trading days in each month varies. In this case, the sales per trading day can be modelled instead of the total sales for each month.

Population adjustments

Any data that are affected by population changes can be adjusted to give per-capita data. That is, consider the data per person (or per thousand people, or per million people) rather than the total. For example, if you are studying the number of hospital beds in a particular region over time, the results are much easier to interpret if you remove the effects of population changes by considering the number of beds per thousand people. Then you can see whether there have been real increases in the number of beds, or whether the increases are due entirely to population increases. It is possible for the total number of beds to increase, but the number of beds per thousand people to decrease. This occurs when the population is increasing faster than the number of hospital beds. For most data that are affected by population changes, it is best to use per-capita data rather than the totals.

Inflation adjustments

Data which are affected by the value of money are best adjusted before modelling. For example, the average cost of a new house will have increased over the last few decades due to inflation. A $200,000 house this year is not the same as a $200,000 house twenty years ago. For this reason, financial time series are usually adjusted so that all values are stated in dollar values from a particular year. For example, the house price data may be stated in year 2000 dollars.

To make these adjustments, a price index is used. If z_{t} denotes the price index and y_{t} denotes the original house price in year t, then x_{t} = y_{t}/z_{t} * z_{2000} gives the adjusted house price at year 2000 dollar values. Price indexes are often constructed by government agencies. For consumer goods, a common price index is the Consumer Price Index (or CPI).

Mathematical transformations

If the data show variation that increases or decreases with the level of the series, then a transformation can be useful. For example, a logarithmic transformation is often useful. If we denote the original observations as y_{1},\dots,y_{T} and the transformed observations as w_{1}, \dots, w_{T}, then w_t = \log(y_t). Logarithms are useful because they are interpretable: changes in a log value are relative (or percentage) changes on the original scale. So if log base 10 is used, then an increase of 1 on the log scale corresponds to a multiplication of 10 on the original scale. Another useful feature of log transformations is that they constrain the forecasts to stay positive on the original scale.

Sometimes other transformations are also used (although they are not so interpretable). For example, square roots and cube roots can be used. These are called power transformations because they can be written in the form w_{t} = y_{t}^p.

A useful family of transformations, that includes both logarithms and power transformations, is the family of Box-Cox transformations (Box & Cox, 1964), which depend on the parameter \lambda and are defined as follows: w_t = \begin{cases} \log(y_t) & \text{if $\lambda=0$}; \\ (y_t^\lambda-1)/\lambda & \text{otherwise}. \end{cases}

The logarithm in a Box-Cox transformation is always a natural logarithm (i.e., to base e). So if \lambda = 0, natural logarithms are used, but if \lambda \ne 0, a power transformation is used, followed by some simple scaling.

If \lambda = 1, then w_t = y_t - 1, so the transformed data is shifted downwards but there is no change in the shape of the time series. But for all other values of \lambda, the time series will change shape.

Use the slider below to see the effect of varying \lambda to transform Australian monthly electricity production:

A good value of \lambda is one which makes the size of the seasonal variation about the same across the whole series, as that makes the forecasting model simpler. In this case, \lambda = 0.30 works quite well, although any value of \lambda between 0 and 0.5 would give similar results.

The BoxCox.lambda() function will choose a value of lambda for you.

(lambda <- BoxCox.lambda(elec))
[1] 0.2654
autoplot(BoxCox(elec, lambda))

The BoxCox() command actually implements a slight modification of the Box-Cox transformation, discussed in Bickel & Doksum (1981), which allows for negative values of y_t when \lambda > 0: w_t = \begin{cases} \log(y_t) & \text{if $\lambda=0$}; \\ \text{sign}(y_t)(|y_t|^\lambda-1)/\lambda & \text{otherwise}. \end{cases} For positive values of y_t, this is the same as the original Box-Cox transformation.

Having chosen a transformation, we need to forecast the transformed data. Then, we need to reverse the transformation (or back-transform) to obtain forecasts on the original scale. The reverse Box-Cox transformation is given by y_{t} = \begin{cases} \exp(w_{t}) & \text{if $\lambda=0$};\\ \text{sign}(\lambda w_t + 1)|\lambda w_t+1|^{1/\lambda} & \text{otherwise}. \end{cases} \tag{3.1}

Features of power transformations

  • Choose a simple value of \lambda. It makes explanations easier.
  • The forecasting results are relatively insensitive to the value of \lambda.
  • Often no transformation is needed.
  • Transformations sometimes make little difference to the forecasts but have a large effect on prediction intervals.

Bias adjustments

One issue with using mathematical transformations such as Box-Cox transformations is that the back-transformed point forecast will not be the mean of the forecast distribution. In fact, it will usually be the median of the forecast distribution (assuming that the distribution on the transformed space is symmetric). For many purposes, this is acceptable, but occasionally the mean forecast is required. For example, you may wish to add up sales forecasts from various regions to form a forecast for the whole country. But medians do not add up, whereas means do.

For a Box-Cox transformation, the back-transformed mean is given by y_t = \begin{cases} \exp(w_t)\left[1 + \frac{\sigma_h^2}{2}\right] & \text{if $\lambda=0$;}\\ (\lambda w_t+1)^{1/\lambda}\left[1 + \frac{\sigma_h^2(1-\lambda)}{2(\lambda w_t+1)^{2}}\right] & \text{otherwise;} \end{cases} \tag{3.2} where \sigma_h^2 is the h-step forecast variance. The larger the forecast variance, the bigger the difference between the mean and the median.

The difference between the simple back-transformed forecast given by Equation 3.1 and the mean given by Equation 3.2 is called the bias. When we use the mean, rather than the median, we say the point forecasts have been bias-adjusted.

To see how much difference this bias-adjustment makes, consider the following example, where we forecast average annual price of eggs using the drift method with a log transformation (\lambda = 0). The log transformation is useful in this case to ensure the forecasts and the prediction intervals stay positive.

fc <- rwf(eggs, drift = TRUE, lambda = 0, h = 50, level = 80)
fc2 <- rwf(eggs, drift = TRUE, lambda = 0, h = 50, level = 80, biasadj = TRUE)
autoplot(eggs) +
  autolayer(fc, series = "Simple back transformation") +
  autolayer(fc2, series = "Bias adjusted", PI = FALSE) +
  guides(colour = guide_legend(title = "Forecast"))
Figure 3.4: Forecasts of egg prices using a random walk with drift applied to the logged data.

The blue line in Figure 3.4 shows the forecast medians while the red line shows the forecast means. Notice how the skewed forecast distribution pulls up the point forecast when we use the bias adjustment.

Bias adjustment is not done by default in the forecast package. If you want your forecasts to be means rather than medians, use the argument biasadj=TRUE when you select your Box-Cox transformation parameter.

3.3 Residual diagnostics

Fitted values

Each observation in a time series can be forecast using all previous observations. We call these fitted values and they are denoted by \hat{y}_{t|t-1}, meaning the forecast of y_t based on observations y_{1},\dots,y_{t-1} . We use these so often, we sometimes drop part of the subscript and just write \hat{y}_t instead of \hat{y}_{t|t-1}. Fitted values always involve one-step forecasts.

Actually, fitted values are often not true forecasts because any parameters involved in the forecasting method are estimated using all available observations in the time series, including future observations. For example, if we use the average method, the fitted values are given by \hat{y}_t = \hat{c} where \hat{c} is the average computed over all available observations, including those at times after t. Similarly, for the drift method, the drift parameter is estimated using all available observations. In this case, the fitted values are given by \hat{y}_t = y_{t-1} + \hat{c} where \hat{c} = (y_T - y_1)/(T - 1). In both cases, there is a parameter to be estimated from the data. The “hat” above the c reminds us that this is an estimate. When the estimate of c involves observations after time t, the fitted values are not true forecasts. On the other hand, naïve or seasonal naïve forecasts do not involve any parameters, and so fitted values are true forecasts in such cases.

Residuals

The “residuals” in a time series model are what is left over after fitting a model. For many (but not all) time series models, the residuals are equal to the difference between the observations and the corresponding fitted values: e_{t} = y_{t} - \hat{y}_{t}.

Residuals are useful in checking whether a model has adequately captured the information in the data. A good forecasting method will yield residuals with the following properties:

  1. The residuals are uncorrelated. If there are correlations between residuals, then there is information left in the residuals which should be used in computing forecasts.
  2. The residuals have zero mean. If the residuals have a mean other than zero, then the forecasts are biased.

Any forecasting method that does not satisfy these properties can be improved. However, that does not mean that forecasting methods that satisfy these properties cannot be improved. It is possible to have several different forecasting methods for the same data set, all of which satisfy these properties. Checking these properties is important in order to see whether a method is using all of the available information, but it is not a good way to select a forecasting method.

If either of these properties is not satisfied, then the forecasting method can be modified to give better forecasts. Adjusting for bias is easy: if the residuals have mean m, then simply add m to all forecasts and the bias problem is solved. Fixing the correlation problem is harder, and we will not address it until Chapter 9.

In addition to these essential properties, it is useful (but not necessary) for the residuals to also have the following two properties.

  1. The residuals have constant variance.
  2. The residuals are normally distributed.

These two properties make the calculation of prediction intervals easier (see Section 3.5 for an example). However, a forecasting method that does not satisfy these properties cannot necessarily be improved. Sometimes applying a Box-Cox transformation may assist with these properties, but otherwise there is usually little that you can do to ensure that your residuals have constant variance and a normal distribution. Instead, an alternative approach to obtaining prediction intervals is necessary. Again, we will not address how to do this until later in the book.

Example: Forecasting the Google daily closing stock price

For stock market prices and indexes, the best forecasting method is often the naïve method. That is, each forecast is simply equal to the last observed value, or \hat{y}_{t} = y_{t-1}. Hence, the residuals are simply equal to the difference between consecutive observations: e_{t} = y_{t} - \hat{y}_{t} = y_{t} - y_{t-1}.

The following graph shows the Google daily closing stock price (GOOG). The large jump at day 166 corresponds to 18 October 2013 when the price jumped 12% due to unexpectedly strong third quarter results.

autoplot(goog200) +
  xlab("Day") +
  ylab("Closing Price (US$)") +
  ggtitle("Google Stock (daily ending 6 December 2013)")
Figure 3.5: The daily Google stock price to 6 Dec 2013.

The residuals obtained from forecasting this series using the naïve method are shown in Figure 3.6. The large positive residual is a result of the unexpected price jump at day 166.

res <- residuals(naive(goog200))
autoplot(res) + xlab("Day") + ylab("") + ggtitle("Residuals from naïve method")
Figure 3.6: Residuals from forecasting the Google stock price using the naïve method.
gghistogram(res) + ggtitle("Histogram of residuals")
Figure 3.7: Histogram of the residuals from the naïve method applied to the Google stock price. The right tail seems a little too long for a normal distribution.
ggAcf(res) + ggtitle("ACF of residuals")
Figure 3.8: ACF of the residuals from the naïve method applied to the Google stock price. The lack of correlation suggesting the forecasts are good.

These graphs show that the naïve method produces forecasts that appear to account for all available information. The mean of the residuals is close to zero and there is no significant correlation in the residuals series. The time plot of the residuals shows that the variation of the residuals stays much the same across the historical data, apart from the one outlier, and therefore the residual variance can be treated as constant. This can also be seen on the histogram of the residuals. The histogram suggests that the residuals may not be normal — the right tail seems a little too long, even when we ignore the outlier. Consequently, forecasts from this method will probably be quite good, but prediction intervals that are computed assuming a normal distribution may be inaccurate.

Portmanteau tests for autocorrelation

In addition to looking at the ACF plot, we can also do a more formal test for autocorrelation by considering a whole set of r_k values as a group, rather than treating each one separately.

Recall that r_k is the autocorrelation for lag k. When we look at the ACF plot to see whether each spike is within the required limits, we are implicitly carrying out multiple hypothesis tests, each one with a small probability of giving a false positive. When enough of these tests are done, it is likely that at least one will give a false positive, and so we may conclude that the residuals have some remaining autocorrelation, when in fact they do not.

In order to overcome this problem, we test whether the first h autocorrelations are significantly different from what would be expected from a white noise process. A test for a group of autocorrelations is called a portmanteau test, from a French word describing a suitcase or coat rack carrying several items of clothing.

One such test is the Box-Pierce test, based on the following statistic Q = T \sum_{k=1}^\ell r_k^2, where \ell is the maximum lag being considered and T is the number of observations. If each r_k is close to zero, then Q will be small. If some r_k values are large (positive or negative), then Q will be large. We suggest using \ell = 10 for non-seasonal data and \ell = 2m for seasonal data, where m is the period of seasonality. However, the test is not good when \ell is large, so if these values are larger than T/5, then use \ell = T/5

A related (and more accurate) test is the Ljung-Box test, based on Q^* = T(T + 2) \sum_{k=1}^\ell (T - k)^{-1}r_k^2. Again, large values of Q^* suggest that the autocorrelations do not come from a white noise series.

How large is too large? If the autocorrelations did come from a white noise series, then both Q and Q^* would have a \chi^2 distribution with \ell degrees of freedom.1.

In the following code, lag= \ell.

# lag=h and fitdf=K
Box.test(res, lag = 10)

    Box-Pierce test

data:  res
X-squared = 11, df = 10, p-value = 0.4
Box.test(res, lag = 10, type = "Lj")

    Box-Ljung test

data:  res
X-squared = 11, df = 10, p-value = 0.4

For both Q and Q^*, the results are not significant (i.e., the p-values are relatively large). Thus, we can conclude that the residuals are not distinguishable from a white noise series.

All of these methods for checking residuals are conveniently packaged into one R function checkresiduals(), which will produce a time plot, ACF plot and histogram of the residuals (with an overlaid normal distribution for comparison), and do a Ljung-Box test.

checkresiduals(naive(goog200))


    Ljung-Box test

data:  Residuals from Naive method
Q* = 11, df = 10, p-value = 0.4

Model df: 0.   Total lags used: 10

3.4 Evaluating forecast accuracy

Training and test sets

It is important to evaluate forecast accuracy using genuine forecasts. Consequently, the size of the residuals is not a reliable indication of how large true forecast errors are likely to be. The accuracy of forecasts can only be determined by considering how well a model performs on new data that were not used when fitting the model.

When choosing models, it is common practice to separate the available data into two portions, training and test data, where the training data is used to estimate any parameters of a forecasting method and the test data is used to evaluate its accuracy. Because the test data is not used in determining the forecasts, it should provide a reliable indication of how well the model is likely to forecast on new data.

The size of the test set is typically about 20% of the total sample, although this value depends on how long the sample is and how far ahead you want to forecast. The test set should ideally be at least as large as the maximum forecast horizon required. The following points should be noted.

  • A model which fits the training data well will not necessarily forecast well.
  • A perfect fit can always be obtained by using a model with enough parameters.
  • Over-fitting a model to data is just as bad as failing to identify a systematic pattern in the data.

Some references describe the test set as the “hold-out set” because these data are “held out” of the data used for fitting. Other references call the training set the “in-sample data” and the test set the “out-of-sample data”. We prefer to use “training data” and “test data” in this book.

Functions to subset a time series

The window() function introduced in Chapter 2 is useful when extracting a portion of a time series, such as we need when creating training and test sets. In the window() function, we specify the start and/or end of the portion of time series required using time values. For example,

window(ausbeer, start = 1995)

extracts all data from 1995 onward.

Another useful function is subset() which allows for more types of subsetting. A great advantage of this function is that it allows the use of indices to choose a subset. For example,

subset(ausbeer, start = length(ausbeer) - 4 * 5)

extracts the last 5 years of observations from ausbeer. It also allows extracting all values for a specific season. For example,

subset(ausbeer, quarter = 1)

extracts the first quarters for all years.

Finally, head and tail are useful for extracting the first few or last few observations. For example, the last 5 years of ausbeer can also be obtained using

tail(ausbeer, 4 * 5)

Forecast errors

A forecast “error” is the difference between an observed value and its forecast. Here “error” does not mean a mistake, it means the unpredictable part of an observation. It can be written as e_{T+h} = y_{T+h} - \hat{y}_{T+h|T}, where the training data is given by \{y_1,\dots,y_T\} and the test data is given by \{y_{T+1},y_{T+2},\dots\}.

Note that forecast errors are different from residuals in two ways. First, residuals are calculated on the training set while forecast errors are calculated on the test set. Second, residuals are based on one-step forecasts while forecast errors can involve multi-step forecasts.

We can measure forecast accuracy by summarising the forecast errors in different ways.

Scale-dependent errors

The forecast errors are on the same scale as the data. Accuracy measures that are based only on e_{t} are therefore scale-dependent and cannot be used to make comparisons between series that involve different units.

The two most commonly used scale-dependent measures are based on the absolute errors or squared errors: \begin{align*} \text{Mean absolute error: MAE} & = \text{mean}(|e_{t}|), \\ \text{Root mean squared error: RMSE} & = \sqrt{\text{mean}(e_{t}^2)}. \end{align*} When comparing forecast methods applied to a single time series, or to several time series with the same units, the MAE is popular as it is easy to both understand and compute. A forecast method that minimises the MAE will lead to forecasts of the median, while minimising the RMSE will lead to forecasts of the mean. Consequently, the RMSE is also widely used, despite being more difficult to interpret.

Percentage errors

The percentage error is given by p_{t} = 100 e_{t}/y_{t}. Percentage errors have the advantage of being unit-free, and so are frequently used to compare forecast performances between data sets. The most commonly used measure is: \text{Mean absolute percentage error: MAPE} = \text{mean}(|p_{t}|). Measures based on percentage errors have the disadvantage of being infinite or undefined if y_{t} = 0 for any t in the period of interest, and having extreme values if any y_{t} is close to zero. Another problem with percentage errors that is often overlooked is that they assume the unit of measurement has a meaningful zero.2 For example, a percentage error makes no sense when measuring the accuracy of temperature forecasts on either the Fahrenheit or Celsius scales, because temperature has an arbitrary zero point.

They also have the disadvantage that they put a heavier penalty on negative errors than on positive errors. This observation led to the use of the so-called “symmetric” MAPE (sMAPE) proposed by Armstrong (1978, p348), which was used in the M3 forecasting competition. It is defined by \text{sMAPE} = \text{mean}\left( 200|y_{t} - \hat{y}_{t}|/(y_{t} + \hat{y}_{t}) \right). However, if y_{t} is close to zero, \hat{y}_{t} is also likely to be close to zero. Thus, the measure still involves division by a number close to zero, making the calculation unstable. Also, the value of sMAPE can be negative, so it is not really a measure of “absolute percentage errors” at all.

Hyndman & Koehler (2006) recommend that the sMAPE not be used. It is included here only because it is widely used, although we will not use it in this book.

Scaled errors

Scaled errors were proposed by Hyndman & Koehler (2006) as an alternative to using percentage errors when comparing forecast accuracy across series with different units. They proposed scaling the errors based on the training MAE from a simple forecast method.

For a non-seasonal time series, a useful way to define a scaled error uses naïve forecasts: q_{j} = \frac{\displaystyle e_{j}} {\displaystyle\frac{1}{T - 1}\sum_{t=2}^T |y_{t} - y_{t-1}|}. Because the numerator and denominator both involve values on the scale of the original data, q_{j} is independent of the scale of the data. A scaled error is less than one if it arises from a better forecast than the average naïve forecast computed on the training data. Conversely, it is greater than one if the forecast is worse than the average naïve forecast computed on the training data.

For seasonal time series, a scaled error can be defined using seasonal naïve forecasts: q_{j} = \frac{\displaystyle e_{j}} {\displaystyle\frac{1}{T - m}\sum_{t=m+1}^T |y_{t} - y_{t-m}|}.

The mean absolute scaled error is simply \text{MASE} = \text{mean}(|q_{j}|).

Examples

beer2 <- window(ausbeer, start = 1992, end = c(2007, 4))
beerfit1 <- meanf(beer2, h = 10)
beerfit2 <- rwf(beer2, h = 10)
beerfit3 <- snaive(beer2, h = 10)
autoplot(window(ausbeer, start = 1992)) +
  autolayer(beerfit1, series = "Mean", PI = FALSE) +
  autolayer(beerfit2, series = "Naïve", PI = FALSE) +
  autolayer(beerfit3, series = "Seasonal naïve", PI = FALSE) +
  xlab("Year") +
  ylab("Megalitres") +
  ggtitle("Forecasts for quarterly beer production") +
  guides(colour = guide_legend(title = "Forecast"))
Figure 3.9: Forecasts of Australian quarterly beer production using data up to the end of 2007.

Figure 3.9 shows three forecast methods applied to the quarterly Australian beer production using data only to the end of 2007. The actual values for the period 2008–2010 are also shown. We compute the forecast accuracy measures for this period.

beer3 <- window(ausbeer, start = 2008)
accuracy(beerfit1, beer3)
accuracy(beerfit2, beer3)
accuracy(beerfit3, beer3)
RMSE MAE MAPE MASE
Mean method 38.45 34.83 8.28 2.44
Naïve method 62.69 57.40 14.18 4.01
Seasonal naïve method 14.31 13.40 3.17 0.94

It is obvious from the graph that the seasonal naïve method is best for these data, although it can still be improved, as we will discover later. Sometimes, different accuracy measures will lead to different results as to which forecast method is best. However, in this case, all of the results point to the seasonal naïve method as the best of these three methods for this data set.

To take a non-seasonal example, consider the Google stock price. The following graph shows the 200 observations ending on 6 Dec 2013, along with forecasts of the next 40 days obtained from three different methods.

googfc1 <- meanf(goog200, h = 40)
googfc2 <- rwf(goog200, h = 40)
googfc3 <- rwf(goog200, drift = TRUE, h = 40)
autoplot(subset(goog, end = 240)) +
  autolayer(googfc1, PI = FALSE, series = "Mean") +
  autolayer(googfc2, PI = FALSE, series = "Naïve") +
  autolayer(googfc3, PI = FALSE, series = "Drift") +
  xlab("Day") +
  ylab("Closing Price (US$)") +
  ggtitle("Google stock price (daily ending 6 Dec 13)") +
  guides(colour = guide_legend(title = "Forecast"))
Figure 3.10: Forecasts of the Google stock price from 7 Dec 2013.
googtest <- window(goog, start = 201, end = 240)
accuracy(googfc1, googtest)
accuracy(googfc2, googtest)
accuracy(googfc3, googtest)
RMSE MAE MAPE MASE
Mean method 114.21 113.27 20.32 30.28
Naïve method 28.43 24.59 4.36 6.57
Drift method 14.08 11.67 2.07 3.12

Here, the best method is the drift method (regardless of which accuracy measure is used).

Time series cross-validation

A more sophisticated version of training/test sets is time series cross-validation. In this procedure, there are a series of test sets, each consisting of a single observation. The corresponding training set consists only of observations that occurred prior to the observation that forms the test set. Thus, no future observations can be used in constructing the forecast. Since it is not possible to obtain a reliable forecast based on a small training set, the earliest observations are not considered as test sets.

The following diagram illustrates the series of training and test sets, where the blue observations form the training sets, and the red observations form the test sets.

The forecast accuracy is computed by averaging over the test sets. This procedure is sometimes known as “evaluation on a rolling forecasting origin” because the “origin” at which the forecast is based rolls forward in time.

With time series forecasting, one-step forecasts may not be as relevant as multi-step forecasts. In this case, the cross-validation procedure based on a rolling forecasting origin can be modified to allow multi-step errors to be used. Suppose that we are interested in models that produce good 4-step-ahead forecasts. Then the corresponding diagram is shown below.

Time series cross-validation is implemented with the tsCV() function. In the following example, we compare the RMSE obtained via time series cross-validation with the residual RMSE.

e <- tsCV(goog200, rwf, drift = TRUE, h = 1)
sqrt(mean(e^2, na.rm = TRUE))
[1] 6.233
sqrt(mean(residuals(rwf(goog200, drift = TRUE))^2, na.rm = TRUE))
[1] 6.169

As expected, the RMSE from the residuals is smaller, as the corresponding “forecasts” are based on a model fitted to the entire data set, rather than being true forecasts.

A good way to choose the best forecasting model is to find the model with the smallest RMSE computed using time series cross-validation.

Pipe operator

The ugliness of the above R code makes this a good opportunity to introduce some alternative ways of stringing R functions together. In the above code, we are nesting functions within functions within functions, so you have to read the code from the inside out, making it difficult to understand what is being computed. Instead, we can use the pipe operator |> as follows.

goog200 |> tsCV(forecastfunction = rwf, drift = TRUE, h = 1) -> e
e^2 |> mean(na.rm = TRUE) |> sqrt()
[1] 6.233
goog200 |> rwf(drift = TRUE) |> residuals() -> res
res^2 |> mean(na.rm = TRUE) |> sqrt()
[1] 6.169

The left hand side of each pipe is passed as the first argument to the function on the right hand side. This is consistent with the way we read from left to right in English. When using pipes, all other arguments must be named, which also helps readability. When using pipes, it is natural to use the right arrow assignment -> rather than the left arrow. For example, the third line above can be read as “Take the goog200 series, pass it to rwf() with drift=TRUE, compute the resulting residuals, and store them as res”.

We will use the pipe operator whenever it makes the code easier to read. The function on the right hand side of a pipe must always be followed by parentheses, even if it has no arguments. See, for example, the use of sqrt() and residuals() in the code above.

Example: using tsCV()

The goog200 data, plotted in Figure 3.5, includes daily closing stock price of Google Inc from the NASDAQ exchange for 200 consecutive trading days starting on 25 February 2013.

The code below evaluates the forecasting performance of 1- to 8-step-ahead naïve forecasts with tsCV(), using MSE as the forecast error measure. The plot shows that the forecast error increases as the forecast horizon increases, as we would expect.

e <- tsCV(goog200, forecastfunction = naive, h = 8)
# Compute the MSE values and remove missing values
mse <- colMeans(e^2, na.rm = T)
# Plot the MSE values against the forecast horizon
data.frame(h = 1:8, MSE = mse) |>
  ggplot(aes(x = h, y = MSE)) +
  geom_point()

3.5 Prediction intervals

As discussed in Section 1.7, a prediction interval gives an interval within which we expect y_{t} to lie with a specified probability. For example, assuming that the forecast errors are normally distributed, a 95% prediction interval for the h-step forecast is \hat{y}_{T+h|T} \pm 1.96 \hat\sigma_h, where \hat\sigma_h is an estimate of the standard deviation of the h-step forecast distribution.

More generally, a prediction interval can be written as \hat{y}_{T+h|T} \pm c \hat\sigma_h where the multiplier c depends on the coverage probability. In this book we usually calculate 80% intervals and 95% intervals, although any percentage may be used. The following table gives the value of c for a range of coverage probabilities assuming normally distributed forecast errors.

Table 3.1: Multipliers to be used for prediction intervals.
Percentage Multiplier
50 0.67
55 0.76
60 0.84
65 0.93
70 1.04
75 1.15
80 1.28
85 1.44
90 1.64
95 1.96
96 2.05
97 2.17
98 2.33
99 2.58

The value of prediction intervals is that they express the uncertainty in the forecasts. If we only produce point forecasts, there is no way of telling how accurate the forecasts are. However, if we also produce prediction intervals, then it is clear how much uncertainty is associated with each forecast. For this reason, point forecasts can be of almost no value without the accompanying prediction intervals.

One-step prediction intervals

When forecasting one step ahead, the standard deviation of the forecast distribution is almost the same as the standard deviation of the residuals. (In fact, the two standard deviations are identical if there are no parameters to be estimated, as is the case with the naïve method. For forecasting methods involving parameters to be estimated, the standard deviation of the forecast distribution is slightly larger than the residual standard deviation, although this difference is often ignored.)

For example, consider a naïve forecast for the Google stock price data goog200 (shown in Figure 3.5). The last value of the observed series is 531.48, so the forecast of the next value of the GSP is 531.48. The standard deviation of the residuals from the naïve method is 6.21. Hence, a 95% prediction interval for the next value of the GSP is 531.48 \pm 1.96(6.21) = [519.3, 543.6]. Similarly, an 80% prediction interval is given by 531.48 \pm 1.28(6.21) = [523.5, 539.4].

The value of the multiplier (1.96 or 1.28) is taken from Table 3.1.

Multi-step prediction intervals

A common feature of prediction intervals is that they increase in length as the forecast horizon increases. The further ahead we forecast, the more uncertainty is associated with the forecast, and thus the wider the prediction intervals. That is, \sigma_h usually increases with h (although there are some non-linear forecasting methods that do not have this property).

To produce a prediction interval, it is necessary to have an estimate of \sigma_h. As already noted, for one-step forecasts (h = 1), the residual standard deviation provides a good estimate of the forecast standard deviation \sigma_1. For multi-step forecasts, a more complicated method of calculation is required. These calculations assume that the residuals are uncorrelated.

Benchmark methods

For the four benchmark methods, it is possible to mathematically derive the forecast standard deviation under the assumption of uncorrelated residuals. If \hat{\sigma}_h denotes the standard deviation of the h-step forecast distribution, and \hat{\sigma} is the residual standard deviation, then we can use the following expressions.

Mean forecasts: \hat\sigma_h = \hat\sigma\sqrt{1 + 1/T}

Naïve forecasts: \hat\sigma_h = \hat\sigma\sqrt{h}

Seasonal naïve forecasts \hat\sigma_h = \hat\sigma\sqrt{k + 1}, where k is the integer part of (h - 1)/m and m is the seasonal period.

Drift forecasts: \hat\sigma_h = \hat\sigma\sqrt{h(1 + h/T)}.

Note that when h = 1 and T is large, these all give the same approximate value \hat\sigma.

Prediction intervals will be computed for you when using any of the benchmark forecasting methods. For example, here is the output when using the naïve method for the Google stock price.

naive(goog200)
    Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
201          531.5 523.5 539.4 519.3 543.6
202          531.5 520.2 542.7 514.3 548.7
203          531.5 517.7 545.3 510.4 552.6
204          531.5 515.6 547.4 507.1 555.8
205          531.5 513.7 549.3 504.3 558.7
206          531.5 512.0 551.0 501.7 561.3
207          531.5 510.4 552.5 499.3 563.7
208          531.5 509.0 554.0 497.1 565.9
209          531.5 507.6 555.3 495.0 568.0
210          531.5 506.3 556.6 493.0 570.0

When plotted, the prediction intervals are shown as shaded region, with the strength of colour indicating the probability associated with the interval.

autoplot(naive(goog200))

Prediction intervals from bootstrapped residuals

When a normal distribution for the forecast errors is an unreasonable assumption, one alternative is to use bootstrapping, which only assumes that the forecast errors are uncorrelated.

A forecast error is defined as e_t = y_t - \hat{y}_{t|t-1}. We can re-write this as y_t = \hat{y}_{t|t-1} + e_t. So we can simulate the next observation of a time series using y_{T+1} = \hat{y}_{T+1|T} + e_{T+1} where \hat{y}_{T+1|T} is the one-step forecast and e_{T+1} is the unknown future error. Assuming future errors will be similar to past errors, we can replace e_{T+1} by sampling from the collection of errors we have seen in the past (i.e., the residuals). Adding the new simulated observation to our data set, we can repeat the process to obtain y_{T+2} = \hat{y}_{T+2|T+1} + e_{T+2} where e_{T+2} is another draw from the collection of residuals. Continuing in this way, we can simulate an entire set of future values for our time series.

Doing this repeatedly, we obtain many possible futures. Then we can compute prediction intervals by calculating percentiles for each forecast horizon. The result is called a bootstrapped prediction interval. The name “bootstrap” is a reference to pulling ourselves up by our bootstraps, because the process allows us to measure future uncertainty by only using the historical data.

To generate such intervals, we can simply add the bootstrap argument to our forecasting functions. For example:

naive(goog200, bootstrap = TRUE)
    Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
201          531.5 525.7 537.8 522.9 542.9
202          531.5 523.2 539.5 519.4 547.0
203          531.5 520.9 541.2 516.7 552.3
204          531.5 519.0 543.0 514.0 560.3
205          531.5 517.5 544.6 511.8 582.1
206          531.5 516.1 545.9 509.5 582.4
207          531.5 514.8 547.3 508.0 583.5
208          531.5 513.5 548.9 505.8 584.9
209          531.5 512.3 549.8 503.9 586.6
210          531.5 510.7 551.4 502.1 587.5

In this case, they are similar (but not identical) to the prediction intervals based on the normal distribution.

Prediction intervals with transformations

If a transformation has been used, then the prediction interval should be computed on the transformed scale, and the end points back-transformed to give a prediction interval on the original scale. This approach preserves the probability coverage of the prediction interval, although it will no longer be symmetric around the point forecast.

The back-transformation of prediction intervals is done automatically using the functions in the forecast package in R, provided you have used the lambda argument when computing the forecasts.

3.6 The forecast package in R

This book uses the facilities in the forecast package in R (which is loaded automatically whenever you load the fpp2 package). This appendix briefly summarises some of the features of the package. Please refer to the help files for individual functions to learn more, and to see some examples of their use.

Functions that output a forecast object:

Many functions, including meanf(), naive(), snaive() and rwf(), produce output in the form of a forecast object (i.e., an object of class forecast). This allows other functions (such as autoplot()) to work consistently across a range of forecasting models.

Objects of class forecast contain information about the forecasting method, the data used, the point forecasts obtained, prediction intervals, residuals and fitted values. There are several functions designed to work with these objects including autoplot(), summary() and print().

The following list shows all the functions that produce forecast objects.

  • meanf()
  • naive(), snaive()
  • rwf()
  • croston()
  • stlf()
  • ses()
  • holt(), hw()
  • splinef()
  • thetaf()
  • forecast()

forecast() function

So far we have used functions which produce a forecast object directly. But a more common approach, which we will focus on in the rest of the book, will be to fit a model to the data, and then use the forecast() function to produce forecasts from that model.

The forecast() function works with many different types of inputs. It generally takes a time series or time series model as its main argument, and produces forecasts appropriately. It always returns objects of class forecast.

If the first argument is of class ts, it returns forecasts from the automatic ETS algorithm discussed in Chapter 7.

Here is a simple example, applying forecast() to the ausbeer data:

forecast(ausbeer, h = 4)
        Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
2010 Q3          404.6 385.9 423.3 376.0 433.3
2010 Q4          480.4 457.5 503.3 445.4 515.4
2011 Q1          417.0 396.5 437.6 385.6 448.4
2011 Q2          383.1 363.5 402.7 353.1 413.1

That works quite well if you have no idea what sort of model to use. But by the end of this book, you should not need to use forecast() in this “blind” fashion. Instead, you will fit a model appropriate to the data, and then use forecast() to produce forecasts from that model.

3.7 Exercises

  1. For the following series, find an appropriate Box-Cox transformation in order to stabilise the variance.

    • usnetelec
    • usgdp
    • mcopper
    • enplanements
  2. Why is a Box-Cox transformation unhelpful for the cangas data?

  3. What Box-Cox transformation would you select for your retail data (from Exercise 3 in Section 2.10)?

  4. For each of the following series, make a graph of the data. If transforming seems appropriate, do so and describe the effect. dole, usdeaths, bricksq.

  5. Calculate the residuals from a seasonal naïve forecast applied to the quarterly Australian beer production data from 1992. The following code will help.

    beer <- window(ausbeer, start = 1992)
    fc <- snaive(beer)
    autoplot(fc)
    res <- residuals(fc)
    autoplot(res)

    Test if the residuals are white noise and normally distributed.

    checkresiduals(fc)

    What do you conclude?

  6. Repeat the exercise for the WWWusage and bricksq data. Use whichever of naive() or snaive() is more appropriate in each case.

  7. Are the following statements true or false? Explain your answer.

    1. Good forecast methods should have normally distributed residuals.
    2. A model with small residuals will give good forecasts.
    3. The best measure of forecast accuracy is MAPE.
    4. If your model doesn’t forecast well, you should make it more complicated.
    5. Always choose the model with the best forecast accuracy as measured on the test set.
  8. For your retail time series (from Exercise 3 in Section 2.10):

    1. Split the data into two parts using

      myts.train <- window(myts, end = c(2010, 12))
      myts.test <- window(myts, start = 2011)
    2. Check that your data have been split appropriately by producing the following plot.

      autoplot(myts) +
        autolayer(myts.train, series = "Training") +
        autolayer(myts.test, series = "Test")
    3. Calculate forecasts using snaive applied to myts.train.

      fc <- snaive(myts.train, h = length(myts.test))
    4. Compare the accuracy of your forecasts against the actual values stored in myts.test.

      accuracy(fc, myts.test)
    5. Check the residuals.

      checkresiduals(fc)

      Do the residuals appear to be uncorrelated and normally distributed?

    6. How sensitive are the accuracy measures to the training/test split?

  9. visnights contains quarterly visitor nights (in millions) from 1998 to 2016 for twenty regions of Australia.

    1. Use window() to create three training sets for visnights[,"QLDMetro"], omitting the last 1, 2 and 3 years; call these train1, train2, and train3, respectively. For example train1 <- window(visnights[, "QLDMetro"], end = c(2015, 4)).

    2. Compute one year of forecasts for each training set using the snaive() method. Call these fc1, fc2 and fc3, respectively.

    3. Use accuracy() to compare the MAPE over the three test sets. Comment on these.

  10. Use the Dow Jones index (data set dowjones) to do the following:

    1. Produce a time plot of the series.
    2. Produce forecasts using the drift method and plot them.
    3. Show that the forecasts are identical to extending the line drawn between the first and last observations.
    4. Try using some of the other benchmark functions to forecast the same data set. Which do you think is best? Why?
  11. Consider the daily closing IBM stock prices (data set ibmclose).

    1. Produce some plots of the data in order to become familiar with it.
    2. Split the data into a training set of 300 observations and a test set of 69 observations.
    3. Try using various benchmark methods to forecast the training set and compare the results on the test set. Which method did best?
    4. Check the residuals of your preferred method. Do they resemble white noise?
  12. Consider the sales of new one-family houses in the USA, Jan 1973 – Nov 1995 (data set hsales).

    1. Produce some plots of the data in order to become familiar with it.
    2. Split the hsales data set into a training set and a test set, where the test set is the last two years of data.
    3. Try using various benchmark methods to forecast the training set and compare the results on the test set. Which method did best?
    4. Check the residuals of your preferred method. Do they resemble white noise?

3.8 Further reading

  • Ord et al. (2017) provides further discussion of simple benchmark forecasting methods.
  • A review of forecast evaluation methods is given in Hyndman & Koehler (2006), looking at the strengths and weaknesses of different approaches. This is the paper that introduced the MASE as a general-purpose forecast accuracy measure.

  1. For the ARIMA models discussed in chapters 8 and 9, the degrees of freedom is adjusted to give better results.↩︎

  2. That is, a percentage is valid on a ratio scale, but not on an interval scale. Only ratio scale variables have meaningful zeros.↩︎