Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Making forecasting easier: forecast v7 for R

Rob J Hyndman
February 18, 2016

Making forecasting easier: forecast v7 for R

Talk given at the WOMBAT 2016 conference

Rob J Hyndman

February 18, 2016
Tweet

More Decks by Rob J Hyndman

Other Decks in Technology

Transcript

  1. Outline 1 Motivation and history 2 Automatic forecasting in R

    3 ggplot2 graphics 4 Bias adjustment Making forecasting easier Motivation and history 2
  2. 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
  3. 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
  4. 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
  5. Outline 1 Motivation and history 2 Automatic forecasting in R

    3 ggplot2 graphics 4 Bias adjustment Making forecasting easier Automatic forecasting in R 6
  6. 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”
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. Outline 1 Motivation and history 2 Automatic forecasting in R

    3 ggplot2 graphics 4 Bias adjustment Making forecasting easier ggplot2 graphics 21
  27. 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")
  28. 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")
  29. 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))
  30. 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
  31. 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")
  32. ggplot2 graphics autoplot methods ts forecast acf stl Arima ets

    . . . Other ggplot2 graphics ggseasonplot ggmonthplot ggtsdisplay Making forecasting easier ggplot2 graphics 27
  33. Outline 1 Motivation and history 2 Automatic forecasting in R

    3 ggplot2 graphics 4 Bias adjustment Making forecasting easier Bias adjustment 28
  34. 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
  35. 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
  36. 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
  37. 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
  38. 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
  39. 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
  40. 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
  41. 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
  42. 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
  43. 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
  44. 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
  45. 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
  46. 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
  47. 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)
  48. Bias adjustment Making forecasting easier Bias adjustment 35 0 100

    200 300 1900 1950 2000 2050 Time cents Price of a dozen eggs
  49. 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