Slide 1

Slide 1 text

Rob J Hyndman Making forecasting easier forecast v7 for

Slide 2

Slide 2 text

Outline 1 Motivation and history 2 Automatic forecasting in R 3 ggplot2 graphics 4 Bias adjustment Making forecasting easier Motivation and history 2

Slide 3

Slide 3 text

Motivation Making forecasting easier Motivation and history 3

Slide 4

Slide 4 text

Motivation Making forecasting easier Motivation and history 3

Slide 5

Slide 5 text

Motivation Making forecasting easier Motivation and history 3

Slide 6

Slide 6 text

Motivation Making forecasting easier Motivation and history 3

Slide 7

Slide 7 text

Motivation Making forecasting easier Motivation and history 3

Slide 8

Slide 8 text

Motivation 1 Common in business to have thousands of products that need forecasting at least monthly. 2 Forecasts are often required by people who are untrained in time series analysis. Specifications Automatic forecasting algorithms must: ¯ determine an appropriate time series model; ¯ estimate the parameters; ¯ compute the forecasts with prediction intervals. Making forecasting easier Motivation and history 4

Slide 9

Slide 9 text

Motivation 1 Common in business to have thousands of products that need forecasting at least monthly. 2 Forecasts are often required by people who are untrained in time series analysis. Specifications Automatic forecasting algorithms must: ¯ determine an appropriate time series model; ¯ estimate the parameters; ¯ compute the forecasts with prediction intervals. Making forecasting easier Motivation and history 4

Slide 10

Slide 10 text

Forecast package history Pre 2003 Collection of functions used for consulting projects July/August 2003 ets and thetaf added August 2006 v1.0 available on CRAN May 2007 auto.arima added July 2008 JSS paper (Hyndman & Khandakar) September 2009 v2.0. Unbundled. May 2010 arfima added Feb/March 2011 tslm, stlf, naive, snaive added August 2011 v3.0. Box Cox transformations added December 2011 tbats added April 2012 Package moved to github November 2012 v4.0. nnetar added June 2013 Major speed-up of ets January 2014 v5.0. tsoutliers and tsclean added May 2015 v6.0. Added several new plots December 2015 264,000 package downloads in one month! February 2016 v7.0. Added ggplot2 graphics & bias adjustment Making forecasting easier Motivation and history 5

Slide 11

Slide 11 text

Outline 1 Motivation and history 2 Automatic forecasting in R 3 ggplot2 graphics 4 Bias adjustment Making forecasting easier Automatic forecasting in R 6

Slide 12

Slide 12 text

Automatic methods in forecast package Making forecasting easier Automatic forecasting in R 7 Automatic model selection auto.arima + forecast ets + forecast tbats + forecast bats + forecast arfima + forecast ar + forecast nnetar + forecast stlm + forecast Automatic forecasting forecast.ts stlf thetaf dshw, hw, holt, ses splinef rwf, naive croston All produce an object of class “forecast”

Slide 13

Slide 13 text

ets algorithm in R Based on Hyndman, Koehler, Snyder & Grose (IJF 2002): For each model, optimize parameters and initial values of underlying state space model using MLE. Select best method using AICc. Produce forecasts and prediction intervals using best method. Making forecasting easier Automatic forecasting in R 8

Slide 14

Slide 14 text

ets algorithm in R Based on Hyndman, Koehler, Snyder & Grose (IJF 2002): For each model, optimize parameters and initial values of underlying state space model using MLE. Select best method using AICc. Produce forecasts and prediction intervals using best method. Making forecasting easier Automatic forecasting in R 8

Slide 15

Slide 15 text

ets algorithm in R Based on Hyndman, Koehler, Snyder & Grose (IJF 2002): For each model, optimize parameters and initial values of underlying state space model using MLE. Select best method using AICc. Produce forecasts and prediction intervals using best method. Making forecasting easier Automatic forecasting in R 8

Slide 16

Slide 16 text

Exponential smoothing Making forecasting easier Automatic forecasting in R 9 Forecasts from ETS(M,A,N) 1960 1970 1980 1990 2000 2010 300 400 500 600

Slide 17

Slide 17 text

Exponential smoothing fit <- ets(livestock) fcast <- forecast(fit) plot(fcast) Making forecasting easier Automatic forecasting in R 10 Forecasts from ETS(M,A,N) 1960 1970 1980 1990 2000 2010 300 400 500 600

Slide 18

Slide 18 text

Exponential smoothing Making forecasting easier Automatic forecasting in R 11 Forecasts from ETS(M,Ad,M) 1995 2000 2005 2010 0.4 0.6 0.8 1.0 1.2 1.4

Slide 19

Slide 19 text

Exponential smoothing fit <- ets(h02) fcast <- forecast(fit) plot(fcast) Making forecasting easier Automatic forecasting in R 12 Forecasts from ETS(M,Ad,M) 1995 2000 2005 2010 0.4 0.6 0.8 1.0 1.2 1.4

Slide 20

Slide 20 text

auto.arima algorithm in R Based on Hyndman and Khandakar (JSS 2008): Select no. differences via unit root tests. Use stepwise search to traverse model space, starting with a simple model and considering nearby variants. For each model, optimize parameters using MLE. Select best method using AICc. Produce forecasts and prediction intervals using best method. Making forecasting easier Automatic forecasting in R 13

Slide 21

Slide 21 text

auto.arima algorithm in R Based on Hyndman and Khandakar (JSS 2008): Select no. differences via unit root tests. Use stepwise search to traverse model space, starting with a simple model and considering nearby variants. For each model, optimize parameters using MLE. Select best method using AICc. Produce forecasts and prediction intervals using best method. Making forecasting easier Automatic forecasting in R 13

Slide 22

Slide 22 text

auto.arima algorithm in R Based on Hyndman and Khandakar (JSS 2008): Select no. differences via unit root tests. Use stepwise search to traverse model space, starting with a simple model and considering nearby variants. For each model, optimize parameters using MLE. Select best method using AICc. Produce forecasts and prediction intervals using best method. Making forecasting easier Automatic forecasting in R 13

Slide 23

Slide 23 text

auto.arima algorithm in R Based on Hyndman and Khandakar (JSS 2008): Select no. differences via unit root tests. Use stepwise search to traverse model space, starting with a simple model and considering nearby variants. For each model, optimize parameters using MLE. Select best method using AICc. Produce forecasts and prediction intervals using best method. Making forecasting easier Automatic forecasting in R 13

Slide 24

Slide 24 text

auto.arima algorithm in R Based on Hyndman and Khandakar (JSS 2008): Select no. differences via unit root tests. Use stepwise search to traverse model space, starting with a simple model and considering nearby variants. For each model, optimize parameters using MLE. Select best method using AICc. Produce forecasts and prediction intervals using best method. Making forecasting easier Automatic forecasting in R 13

Slide 25

Slide 25 text

Auto ARIMA Making forecasting easier Automatic forecasting in R 14 Forecasts from ARIMA(0,1,0) with drift 1960 1970 1980 1990 2000 2010 250 300 350 400 450 500 550

Slide 26

Slide 26 text

Auto ARIMA fit <- auto.arima(livestock) fcast <- forecast(fit) plot(fcast) Making forecasting easier Automatic forecasting in R 15 Forecasts from ARIMA(0,1,0) with drift 1960 1970 1980 1990 2000 2010 250 300 350 400 450 500 550

Slide 27

Slide 27 text

Auto ARIMA Making forecasting easier Automatic forecasting in R 16 Forecasts from ARIMA(3,1,3)(0,1,1)[12] 1995 2000 2005 2010 0.4 0.6 0.8 1.0 1.2 1.4

Slide 28

Slide 28 text

Auto ARIMA fit <- auto.arima(h02) fcast <- forecast(fit) plot(fcast) Making forecasting easier Automatic forecasting in R 17 Forecasts from ARIMA(3,1,3)(0,1,1)[12] 1995 2000 2005 2010 0.4 0.6 0.8 1.0 1.2 1.4

Slide 29

Slide 29 text

TBATS model TBATS Trigonometric terms for seasonality Box-Cox transformations for heterogeneity ARMA errors for short-term dynamics Trend (possibly damped) Seasonal (including multiple and non-integer periods) Automatic algorithm described in De Livera, Hyndman and Snyder (JASA 2011). Making forecasting easier Automatic forecasting in R 18

Slide 30

Slide 30 text

Examples fit <- tbats(gas) fcast <- forecast(fit) plot(fcast) Making forecasting easier Automatic forecasting in R 19 Forecasts from TBATS(1, {0,0}, 1, {<52.1785714285714,9>}) 1995 2000 2005 7 8 9 10

Slide 31

Slide 31 text

Examples fit <- tbats(callcentre) fcast <- forecast(fit) plot(fcast) Making forecasting easier Automatic forecasting in R 20 Forecasts from TBATS(0.607, {0,0}, −, {<169,5>, <845,4>}) 2005 2010 2015 2020 2025 2030 2035 0 100 200 300 400 500

Slide 32

Slide 32 text

Outline 1 Motivation and history 2 Automatic forecasting in R 3 ggplot2 graphics 4 Bias adjustment Making forecasting easier ggplot2 graphics 21

Slide 33

Slide 33 text

ggplot2 graphics Making forecasting easier ggplot2 graphics 22 Internet Usage per Minute Minutes Internet users 0 20 40 60 80 100 100 150 200 plot(WWWusage, xlab="Minutes", ylab="Internet users", main="Internet Usage per Minute")

Slide 34

Slide 34 text

ggplot2 graphics Making forecasting easier ggplot2 graphics 23 80 120 160 200 0 25 50 75 100 Minutes Internet users Internet Usage per Minute autoplot(WWWusage, xlab="Minutes", ylab="Internet users", main="Internet Usage per Minute")

Slide 35

Slide 35 text

ggplot2 graphics Making forecasting easier ggplot2 graphics 24 100 150 200 250 300 0 30 60 90 Time y level 80 95 Forecasts from ETS(A,Ad,N) autoplot(forecast(WWWusage))

Slide 36

Slide 36 text

ggplot2 graphics Making forecasting easier ggplot2 graphics 25 100 150 200 250 300 0 30 60 90 Time y level 80 95 Forecasts from ETS(A,Ad,N) library(magrittr) WWWusage %>% forecast %>% autoplot

Slide 37

Slide 37 text

ggplot2 graphics Making forecasting easier ggplot2 graphics 26 100 150 200 250 0 30 60 90 Minute Number of users Forecasts from ARIMA model WWWusage %>% auto.arima %>% forecast(level=c(50,80,95)) -> fc autoplot(WWWusage) + geom_forecast(fc, color=’#ffcccc’, show.legend=FALSE) + ggtitle("Forecasts from ARIMA model") + labs(x="Minute", y="Number of users")

Slide 38

Slide 38 text

ggplot2 graphics autoplot methods ts forecast acf stl Arima ets . . . Other ggplot2 graphics ggseasonplot ggmonthplot ggtsdisplay Making forecasting easier ggplot2 graphics 27

Slide 39

Slide 39 text

Outline 1 Motivation and history 2 Automatic forecasting in R 3 ggplot2 graphics 4 Bias adjustment Making forecasting easier Bias adjustment 28

Slide 40

Slide 40 text

Box-Cox transformations wt = fλ (yt ) = log(yt ), λ = 0; (yλ t − 1)/λ, λ = 0. λ = 1: (No substantive transformation) λ = 1 2 : (Square root plus linear transformation) λ = 0: (Natural logarithm) λ = −1: (Inverse plus 1) Most modelling and forecasting functions in the forecast package have a lambda argument allowing Box-Cox transformations. Making forecasting easier Bias adjustment 29

Slide 41

Slide 41 text

Box-Cox transformations wt = fλ (yt ) = log(yt ), λ = 0; (yλ t − 1)/λ, λ = 0. λ = 1: (No substantive transformation) λ = 1 2 : (Square root plus linear transformation) λ = 0: (Natural logarithm) λ = −1: (Inverse plus 1) Most modelling and forecasting functions in the forecast package have a lambda argument allowing Box-Cox transformations. Making forecasting easier Bias adjustment 29

Slide 42

Slide 42 text

Box-Cox transformations wt = fλ (yt ) = log(yt ), λ = 0; (yλ t − 1)/λ, λ = 0. λ = 1: (No substantive transformation) λ = 1 2 : (Square root plus linear transformation) λ = 0: (Natural logarithm) λ = −1: (Inverse plus 1) Most modelling and forecasting functions in the forecast package have a lambda argument allowing Box-Cox transformations. Making forecasting easier Bias adjustment 29

Slide 43

Slide 43 text

Box-Cox transformations wt = fλ (yt ) = log(yt ), λ = 0; (yλ t − 1)/λ, λ = 0. λ = 1: (No substantive transformation) λ = 1 2 : (Square root plus linear transformation) λ = 0: (Natural logarithm) λ = −1: (Inverse plus 1) Most modelling and forecasting functions in the forecast package have a lambda argument allowing Box-Cox transformations. Making forecasting easier Bias adjustment 29

Slide 44

Slide 44 text

Box-Cox transformations wt = fλ (yt ) = log(yt ), λ = 0; (yλ t − 1)/λ, λ = 0. λ = 1: (No substantive transformation) λ = 1 2 : (Square root plus linear transformation) λ = 0: (Natural logarithm) λ = −1: (Inverse plus 1) Most modelling and forecasting functions in the forecast package have a lambda argument allowing Box-Cox transformations. Making forecasting easier Bias adjustment 29

Slide 45

Slide 45 text

Box-Cox transformations wt = fλ (yt ) = log(yt ), λ = 0; (yλ t − 1)/λ, λ = 0. λ = 1: (No substantive transformation) λ = 1 2 : (Square root plus linear transformation) λ = 0: (Natural logarithm) λ = −1: (Inverse plus 1) Most modelling and forecasting functions in the forecast package have a lambda argument allowing Box-Cox transformations. Making forecasting easier Bias adjustment 29

Slide 46

Slide 46 text

Box-Cox transformations wt = fλ (yt ) = log(yt ), λ = 0; (yλ t − 1)/λ, λ = 0. λ = 1: (No substantive transformation) λ = 1 2 : (Square root plus linear transformation) λ = 0: (Natural logarithm) λ = −1: (Inverse plus 1) Most modelling and forecasting functions in the forecast package have a lambda argument allowing Box-Cox transformations. Making forecasting easier Bias adjustment 29

Slide 47

Slide 47 text

Box-Cox transformations Making forecasting easier Bias adjustment 30

Slide 48

Slide 48 text

Back-transformation We must reverse the transformation (or back-transform) to obtain forecasts on the original scale. The reverse Box-Cox transformations are given by yt = f−1 λ (wt ) = exp(wt ), λ = 0; (λwt + 1)1/λ, λ = 0. fit <- auto.arima(elec, lambda=1/3) fc <- forecast(fit) plot(fc, include=120) Making forecasting easier Bias adjustment 31

Slide 49

Slide 49 text

Back-transformation Making forecasting easier Bias adjustment 32 Forecasts from ARIMA(0,1,1)(0,1,1)[12] 1986 1988 1990 1992 1994 1996 1998 10000 12000 14000 16000

Slide 50

Slide 50 text

Bias and Box-Cox If the forecast is the mean on the transformed scale, it is not the mean on the original scale. If the forecast is the median on the transformed scale, it is the median on the original scale. Quantiles are preserved because the transformation is monotonically increasing. If E(W) = µ and Var(W) = σ2, then Bias E(Y)−eµ ≈ (λµ + 1)1/λ 1 + σ2(1−λ) 2(λµ+1)2 − eµ if λ = 0; 1 2 eµσ2 if λ = 0. Making forecasting easier Bias adjustment 33

Slide 51

Slide 51 text

Bias and Box-Cox If the forecast is the mean on the transformed scale, it is not the mean on the original scale. If the forecast is the median on the transformed scale, it is the median on the original scale. Quantiles are preserved because the transformation is monotonically increasing. If E(W) = µ and Var(W) = σ2, then Bias E(Y)−eµ ≈ (λµ + 1)1/λ 1 + σ2(1−λ) 2(λµ+1)2 − eµ if λ = 0; 1 2 eµσ2 if λ = 0. Making forecasting easier Bias adjustment 33

Slide 52

Slide 52 text

Bias and Box-Cox If the forecast is the mean on the transformed scale, it is not the mean on the original scale. If the forecast is the median on the transformed scale, it is the median on the original scale. Quantiles are preserved because the transformation is monotonically increasing. If E(W) = µ and Var(W) = σ2, then Bias E(Y)−eµ ≈ (λµ + 1)1/λ 1 + σ2(1−λ) 2(λµ+1)2 − eµ if λ = 0; 1 2 eµσ2 if λ = 0. Making forecasting easier Bias adjustment 33

Slide 53

Slide 53 text

Bias and Box-Cox If the forecast is the mean on the transformed scale, it is not the mean on the original scale. If the forecast is the median on the transformed scale, it is the median on the original scale. Quantiles are preserved because the transformation is monotonically increasing. If E(W) = µ and Var(W) = σ2, then Bias E(Y)−eµ ≈ (λµ + 1)1/λ 1 + σ2(1−λ) 2(λµ+1)2 − eµ if λ = 0; 1 2 eµσ2 if λ = 0. Making forecasting easier Bias adjustment 33

Slide 54

Slide 54 text

Bias and Box-Cox If the forecast is the mean on the transformed scale, it is not the mean on the original scale. If the forecast is the median on the transformed scale, it is the median on the original scale. Quantiles are preserved because the transformation is monotonically increasing. If E(W) = µ and Var(W) = σ2, then Bias E(Y)−eµ ≈ (λµ + 1)1/λ 1 + σ2(1−λ) 2(λµ+1)2 − eµ if λ = 0; 1 2 eµσ2 if λ = 0. Making forecasting easier Bias adjustment 33

Slide 55

Slide 55 text

Bias adjustment Making forecasting easier Bias adjustment 34 0 100 200 300 1900 1950 2000 2050 Time cents Price of a dozen eggs fit <- ets(eggs, lambda=0) fc <- forecast(fit, h=50, level=95) fc2 <- forecast(fit, h=50, level=95, biasadj=TRUE) autoplot(fc, main="Price of a dozen eggs", ylab="cents") + geom_forecast(fc2, plot.conf=FALSE, color="red") + guides(fill=FALSE)

Slide 56

Slide 56 text

Bias adjustment Making forecasting easier Bias adjustment 35 0 100 200 300 1900 1950 2000 2050 Time cents Price of a dozen eggs

Slide 57

Slide 57 text

For further information robjhyndman.com Slides for this talk. Links to all papers and books. Links to R packages. A blog about forecasting research. OTexts.org/fpp Free online book based on forecast package for R. Making forecasting easier Bias adjustment 36