Forecasting using R Rob J Hyndman 1.3 Seasonality and trends Forecasting using R 1
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-term increase or decrease in the data. Seasonal pattern exists when a series is influenced by seasonal factors (e.g., the quarter of the year, the month, or day of the week). Cyclic pattern exists when data exhibit rises and falls that are not of fixed period (duration usually of at least 2 years). Forecasting using R Time series components 3
Time series components Differences between seasonal and cyclic patterns: seasonal pattern constant length; cyclic pattern variable length average length of cycle longer than length of seasonal pattern magnitude of cycle more variable than magnitude of seasonal pattern Forecasting using R Time series components 4
Time series patterns Sales of new one family houses, USA 80 Total sales 60 40 1975 1980 1985 1990 1995 Year Forecasting using R Time series components 5
Time series patterns US Treasury Bill Contracts 90 price 88 86 0 20 40 60 80 100 Day Forecasting using R Time series components 6
Time series patterns 16000 Australian monthly electricity production 12000 GWh 8000 4000 1960 1970 1980 1990 Year Forecasting using R Time series components 7
Time series patterns Daily change in Dow Jones Index 50 0 50 100 0 50 100 150 200 250 300 Day Forecasting using R Time series components 8
Time series decomposition Y t = f(s t, T t, E t ) where Y t = data at period t S t = seasonal component at period t T t = trend-cycle component at period t E t = remainder (or irregular or error) component at period t Additive decomposition: Y t = S t + T t + E t. Multiplicative decomposition: Y t = S t T t E t. Forecasting using R Time series components 9
Time series decomposition Y t = f(s t, T t, E t ) where Y t = data at period t S t = seasonal component at period t T t = trend-cycle component at period t E t = remainder (or irregular or error) component at period t Additive decomposition: Y t = S t + T t + E t. Multiplicative decomposition: Y t = S t T t E t. Forecasting using R Time series components 9
Time series decomposition Y t = f(s t, T t, E t ) where Y t = data at period t S t = seasonal component at period t T t = trend-cycle component at period t E t = remainder (or irregular or error) component at period t Additive decomposition: Y t = S t + T t + E t. Multiplicative decomposition: Y t = S t T t E t. Forecasting using R Time series components 9
Time series decomposition Additive model appropriate if magnitude of seasonal fluctuations does not vary with level. If seasonal are proportional to level of series, then multiplicative model appropriate. Multiplicative decomposition more prevalent with economic series Alternative: use a Box-Cox transformation, and then use additive decomposition. Logs turn multiplicative relationship into an additive relationship: Y t = S t T t E t log Y t = log S t + log T t + log E t. Forecasting using R Time series components 10
Euro electrical equipment Electrical equipment manufacturing (Euro area) 120 New orders index 100 80 series Data Trend 60 2000 2005 2010 Forecasting using R Time series components 11
Euro electrical equipment 120 remainder trend seasonal data 100 80 60 10 0 10 20 110 100 90 80 4 0 4 8 2000 2005 2010 Time Forecasting using R Time series components 12
Euro electrical equipment 10 Seasonal 0 10 20 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Month Forecasting using R Time series components 13
Seasonal adjustment Useful by-product of decomposition: an easy way to calculate seasonally adjusted data. Additive decomposition: seasonally adjusted data given by Y t S t = T t + E t Multiplicative decomposition: seasonally adjusted data given by Y t /S t = T t E t Forecasting using R Time series components 14
Euro electrical equipment Electrical equipment manufacturing 120 New orders index 100 80 series Data SeasAdjust 60 2000 2005 2010 Forecasting using R Time series components 15
Seasonal adjustment We use estimates of S based on past values to seasonally adjust a current value. Seasonally adjusted series reflect remainders as well as trend. Therefore they are not smooth and downturns or upturns can be misleading. It is better to use the trend-cycle component to look for 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 for modern X-12-ARIMA method. STL method introduced in 1983 TRAMO/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 of seasonality. Seasonal component allowed to change over time, and rate of change controlled by user. Smoothness of trend-cycle also controlled by user. Robust to outliers Not trading day or calendar adjustments. Only additive. Forecasting using R STL decomposition 19
Euro electrical equipment library(magrittr) elecequip %>% stl(s.window=5) %>% autoplot trend seasonal data 120 100 80 60 10 0 10 20 110 100 90 80 remainder 4 0 4 8 2000 2005 2010 Forecasting using R Time STL decomposition 20
Euro electrical equipment elecequip %>% stl(t.window=15, s.window='periodic', robust=true) %>% autoplot seasonal data 120 100 80 60 10 0 10 remainder trend 110 100 90 80 10 5 0 5 2000 2005 2010 Forecasting using R Time 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 last year Forecast seasonally adjusted data using non-seasonal time series method. E.g., Holt s method next topic Random walk with drift model Combine forecasts of seasonal component with forecasts of seasonally adjusted data to get forecasts of original data. Sometimes a decomposition is useful just for understanding the data before building a separate forecasting model. Forecasting using R Forecasting and decomposition 24
Seas adj elec equipment 120 Naive forecasts of seasonally adjusted data 110 y 100 level 80 95 90 80 2000 2005 2010 New orders index Forecasting using R Forecasting and decomposition 25
Seas adj elec equipment Forecasts from STL + Random walk 120 New orders index 100 80 level 80 95 60 2000 2005 2010 Time 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 the seasonally adjusted forecasts and modify them with the seasonal component. This ignores the uncertainty in the seasonal component estimate. It also ignores the uncertainty in the future seasonal pattern. 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