forecasting using r - rob j hyndman · outline 1time series components 2stl decomposition...
TRANSCRIPT
Forecasting using R 1
Forecasting using R
Rob J Hyndman
1.3 Seasonality and trends
Outline
1 Time series components
2 STL decomposition
3 Forecasting and decomposition
4 Lab session 5
Forecasting using R Time series components 2
Time series patterns
Trend pattern exists when there is a long-termincrease or decrease in the data.
Seasonal pattern exists when a series is influenced byseasonal factors (e.g., the quarter of the year,the month, or day of the week).
Cyclic pattern exists when data exhibit rises and fallsthat are not of fixed period (duration usually ofat least 2 years).
Forecasting using R Time series components 3
Time series components
Differences between seasonal and cyclic patterns:seasonal pattern constant length; cyclic patternvariable lengthaverage length of cycle longer than length of seasonalpatternmagnitude of cycle more variable than magnitude ofseasonal pattern
Forecasting using R Time series components 4
Time series patterns
40
60
80
1975 1980 1985 1990 1995Year
Tota
l sal
es
Sales of new one−family houses, USA
Forecasting using R Time series components 5
Time series patterns
86
88
90
0 20 40 60 80 100Day
pric
e
US Treasury Bill Contracts
Forecasting using R Time series components 6
Time series patterns
4000
8000
12000
16000
1960 1970 1980 1990Year
GW
h
Australian monthly electricity production
Forecasting using R Time series components 7
Time series patterns
−100
−50
0
50
0 50 100 150 200 250 300Day
Daily change in Dow Jones Index
Forecasting using R Time series components 8
Time series decomposition
Yt = f(St, Tt, Et)
where Yt = data at period tSt = seasonal component at period tTt = trend-cycle component at period tEt = remainder (or irregular or error) compo-
nent at period t
Additive decomposition: Yt = St + Tt + Et.Multiplicative decomposition: Yt = St × Tt × Et.
Forecasting using R Time series components 9
Time series decomposition
Yt = f(St, Tt, Et)
where Yt = data at period tSt = seasonal component at period tTt = trend-cycle component at period tEt = remainder (or irregular or error) compo-
nent at period t
Additive decomposition: Yt = St + Tt + Et.Multiplicative decomposition: Yt = St × Tt × Et.
Forecasting using R Time series components 9
Time series decomposition
Yt = f(St, Tt, Et)
where Yt = data at period tSt = seasonal component at period tTt = trend-cycle component at period tEt = remainder (or irregular or error) compo-
nent at period t
Additive decomposition: Yt = St + Tt + Et.Multiplicative decomposition: Yt = St × Tt × Et.
Forecasting using R Time series components 9
Time series decomposition
Additive model appropriate if magnitude of seasonalfluctuations does not vary with level.If seasonal are proportional to level of series, thenmultiplicative model appropriate.Multiplicative decomposition more prevalent witheconomic seriesAlternative: use a Box-Cox transformation, and thenuse additive decomposition.Logs turn multiplicative relationship into an additiverelationship:
Yt = St × Tt × Et ⇒ log Yt = log St + log Tt + log Et.Forecasting using R Time series components 10
Euro electrical equipment
60
80
100
120
2000 2005 2010
New
ord
ers
inde
x
series
Data
Trend
Electrical equipment manufacturing (Euro area)
Forecasting using R Time series components 11
Euro electrical equipment
60
80
100
120
−20
−10
0
10
80
90
100
110
−8
−4
0
4
data
seas
onal
tren
dre
mai
nder
2000 2005 2010Time
Forecasting using R Time series components 12
Euro electrical equipment
−20
−10
0
10
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecMonth
Sea
sona
l
Forecasting using R Time series components 13
Seasonal adjustment
Useful by-product of decomposition: an easy way tocalculate seasonally adjusted data.Additive decomposition: seasonally adjusted datagiven by
Yt − St = Tt + EtMultiplicative decomposition: seasonally adjusteddata given by
Yt/St = Tt × Et
Forecasting using R Time series components 14
Euro electrical equipment
60
80
100
120
2000 2005 2010
New
ord
ers
inde
x
series
Data
SeasAdjust
Electrical equipment manufacturing
Forecasting using R Time series components 15
Seasonal adjustment
We use estimates of S based on past values toseasonally adjust a current value.Seasonally adjusted series reflect remainders as wellas trend. Therefore they are not “smooth” and“downturns” or “upturns” can be misleading.It is better to use the trend-cycle component to lookfor turning points.
Forecasting using R Time series components 16
History of time series decomposition
Classical method originated in 1920s.Census II method introduced in 1957. Basis formodern X-12-ARIMA method.STL method introduced in 1983TRAMO/SEATS introduced in 1990s.
Forecasting using R Time series components 17
Outline
1 Time series components
2 STL decomposition
3 Forecasting and decomposition
4 Lab session 5
Forecasting using R STL decomposition 18
STL decomposition
STL: “Seasonal and Trend decomposition using Loess”,Very versatile and robust.Unlike X-12-ARIMA, STL will handle any type ofseasonality.Seasonal component allowed to change over time,and rate of change controlled by user.Smoothness of trend-cycle also controlled by user.Robust to outliersNot trading day or calendar adjustments.Only additive.
Forecasting using R STL decomposition 19
Euro electrical equipmentlibrary(magrittr)elecequip %>% stl(s.window=5) %>%
autoplot
60
80
100
120
−20
−10
0
10
80
90
100
110
−8
−4
0
4
data
seas
onal
tren
dre
mai
nder
2000 2005 2010TimeForecasting using R STL decomposition 20
Euro electrical equipmentelecequip %>%
stl(t.window=15, s.window='periodic', robust=TRUE) %>%autoplot
60
80
100
120
−10
0
10
80
90
100
110
−5
0
5
10
data
seas
onal
tren
dre
mai
nder
2000 2005 2010TimeForecasting using R STL decomposition 21
STL decomposition in R
t.window controls wiggliness of trend component.s.window controls variation on seasonal component.
Forecasting using R STL decomposition 22
Outline
1 Time series components
2 STL decomposition
3 Forecasting and decomposition
4 Lab session 5
Forecasting using R Forecasting and decomposition 23
Forecasting and decomposition
Forecast seasonal component by repeating the lastyearForecast seasonally adjusted data using non-seasonaltime series method. E.g.,
Holt’s method — next topicRandom walk with drift model
Combine forecasts of seasonal component withforecasts of seasonally adjusted data to get forecastsof original data.Sometimes a decomposition is useful just forunderstanding the data before building a separateforecasting model.
Forecasting using R Forecasting and decomposition 24
Seas adj elec equipment
80
90
100
110
120
2000 2005 2010New orders index
y
level
80
95
Naive forecasts of seasonally adjusted data
Forecasting using R Forecasting and decomposition 25
Seas adj elec equipment
60
80
100
120
2000 2005 2010Time
New
ord
ers
inde
x
level
80
95
Forecasts from STL + Random walk
Forecasting using R Forecasting and decomposition 26
How to do this in R
fit <- stl(elecequip, t.window=15,s.window="periodic", robust=TRUE)
eeadj <- seasadj(fit)autoplot(naive(eeadj, h=24)) +
ylab("New orders index")
fcast <- forecast(fit, method="naive", h=24)autoplot(fcast) +
ylab="New orders index")
Forecasting using R Forecasting and decomposition 27
Decomposition and prediction intervals
It is common to take the prediction intervals from theseasonally adjusted forecasts and modify them withthe seasonal component.This ignores the uncertainty in the seasonalcomponent estimate.It also ignores the uncertainty in the future seasonalpattern.
Forecasting using R Forecasting and decomposition 28
Some more R functions
fcast <- stlf(elecequip, method='naive')
fcast <- stlf(elecequip, method='naive',h=36, s.window=11, robust=TRUE)
Forecasting using R Forecasting and decomposition 29
Outline
1 Time series components
2 STL decomposition
3 Forecasting and decomposition
4 Lab session 5
Forecasting using R Lab session 5 30
Lab Session 5
Forecasting using R Lab session 5 31