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 fpp 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 input. 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)
#>         Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
#> 2010 Q3            405   386   423   376   433
#> 2011 Q4            480   458   503   446   515
#> 2011 Q1            417   397   437   386   448
#> 2011 Q2            383   364   402   354   412
#> 2012 Q3            403   381   425   369   437
#> 2012 Q4            478   451   506   436   521
#> 2012 Q1            415   390   441   377   454
#> 2012 Q2            382   357   406   345   419

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.