Making forecasting easier forecast v7 for Outline 1 Motivation - - PowerPoint PPT Presentation
Making forecasting easier forecast v7 for Outline 1 Motivation - - PowerPoint PPT Presentation
Rob J Hyndman Making forecasting easier forecast v7 for Outline 1 Motivation and history 2 Automatic forecasting in R 3 ggplot2 graphics 4 Bias adjustment Making forecasting easier Motivation and history 2 Motivation Making
Outline
1 Motivation and history 2 Automatic forecasting in R 3 ggplot2 graphics 4 Bias adjustment
Making forecasting easier Motivation and history 2
Motivation
Making forecasting easier Motivation and history 3
Motivation
Making forecasting easier Motivation and history 3
Motivation
Making forecasting easier Motivation and history 3
Motivation
Making forecasting easier Motivation and history 3
Motivation
Making forecasting easier Motivation and history 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
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
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
Outline
1 Motivation and history 2 Automatic forecasting in R 3 ggplot2 graphics 4 Bias adjustment
Making forecasting easier Automatic forecasting in R 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”
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 100 200 300 400 500
Outline
1 Motivation and history 2 Automatic forecasting in R 3 ggplot2 graphics 4 Bias adjustment
Making forecasting easier ggplot2 graphics 21
ggplot2 graphics
Making forecasting easier ggplot2 graphics 22
Internet Usage per Minute
Minutes Internet users 20 40 60 80 100 100 150 200
plot(WWWusage, xlab="Minutes", ylab="Internet users", main="Internet Usage per Minute")
ggplot2 graphics
Making forecasting easier ggplot2 graphics 23
80 120 160 200 25 50 75 100
Minutes Internet users
Internet Usage per Minute
autoplot(WWWusage, xlab="Minutes", ylab="Internet users", main="Internet Usage per Minute")
ggplot2 graphics
Making forecasting easier ggplot2 graphics 24
100 150 200 250 300 30 60 90
Time y level
80 95
Forecasts from ETS(A,Ad,N)
autoplot(forecast(WWWusage))
ggplot2 graphics
Making forecasting easier ggplot2 graphics 25
100 150 200 250 300 30 60 90
Time y level
80 95
Forecasts from ETS(A,Ad,N)
library(magrittr) WWWusage %>% forecast %>% autoplot
ggplot2 graphics
Making forecasting easier ggplot2 graphics 26
100 150 200 250 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")
ggplot2 graphics
autoplot methods ts forecast acf stl Arima ets . . . Other ggplot2 graphics ggseasonplot ggmonthplot ggtsdisplay
Making forecasting easier ggplot2 graphics 27
Outline
1 Motivation and history 2 Automatic forecasting in R 3 ggplot2 graphics 4 Bias adjustment
Making forecasting easier Bias adjustment 28
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
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
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
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
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
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
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
Box-Cox transformations
Making forecasting easier Bias adjustment 30
λ = 1.00
Year Monthly electricity production 1960 1970 1980 1990 2000 6000 10000 14000
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
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
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 2eµσ2
if λ = 0.
Making forecasting easier Bias adjustment 33
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 2eµσ2
if λ = 0.
Making forecasting easier Bias adjustment 33
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 2eµσ2
if λ = 0.
Making forecasting easier Bias adjustment 33
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 2eµσ2
if λ = 0.
Making forecasting easier Bias adjustment 33
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 2eµσ2
if λ = 0.
Making forecasting easier Bias adjustment 33
Bias adjustment
Making forecasting easier Bias adjustment 34
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)
Bias adjustment
Making forecasting easier Bias adjustment 35
100 200 300 1900 1950 2000 2050
Time cents
Price of a dozen eggs
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