ETW3420: Principles of Forecasting and Applications
Principles of
Copyright By PowCoder代写 加微信 powcoder
Forecasting and
Applications
Topic 4: Time Series Decomposition
1 Introduction
2 Time series components
3 Seasonal adjustment
4 History of time series decomposition
5 Moving Averages
6 Classical Decomposition
7 X-11 decomposition (OPTIONAL)
8 STL decomposition
9 Forecasting with decomposition
Introduction
Time series data can exhibit a huge variety of patterns and it
is helpful to categorize some of the patterns and behaviours
that can be seen in time series.
It is also sometimes useful to try to split a time series into
several components, each representing one of the underlying
categories of pattern.
In this topic, we consider some common patterns and
methods to extract the associated components from a time
Often this is done to help understand the time series better,
but it can also be used to improve forecasts.
1 Introduction
2 Time series components
3 Seasonal adjustment
4 History of time series decomposition
5 Moving Averages
6 Classical Decomposition
7 X-11 decomposition (OPTIONAL)
8 STL decomposition
9 Forecasting with decomposition
Time series patterns
Trend pattern exists when there is a long-term increase or
decrease in the data.
Cyclic pattern exists when data exhibit rises and falls that
are not of fixed period (duration usually of at least 2
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).
Time series patterns
2000 2005 2010
1980 1990 2000 2010
1960 1970 1980 1990
1970 1980 1990 2000 2010
Time series decomposition
yt = f(St, Tt, Rt)
where yt = data at period t
Tt = trend-cycle component at period t
St = seasonal component at period t
Rt = remainder component at period t
Additive decomposition: yt = St + Tt + Rt.
Multiplicative decomposition: yt = St × Tt × Rt.
Time series decomposition
yt = f(St, Tt, Rt)
where yt = data at period t
Tt = trend-cycle component at period t
St = seasonal component at period t
Rt = remainder component at period t
Additive decomposition: yt = St + Tt + Rt.
Multiplicative decomposition: yt = St × Tt × Rt.
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:
yt = St × Tt × Et ⇒ log yt = log St + log Tt + log Rt.
Euro electrical equipment
fit <- stl(elecequip, s.window=7)
autoplot(fit) + xlab("Year")
2000 2005 2010
Euro electrical equipment
ggsubseriesplot(seasonal(fit))
Mar Apr Jul Aug Sep Oct Nov Dec
Euro electrical equipment
autoplot(elecequip, series="Data") +
autolayer(trendcycle(fit), series="Trend-cycle")
2000 2005 2010
Trend−cycle
Helper functions
seasonal() extracts the seasonal component
trendcycle() extracts the trend-cycle component
remainder() extracts the remainder component.
seasadj() returns the seasonally adjusted series.
Self-Practice
Repeat the decomposition using
elecequip %>%
stl(s.window=7, t.window=11) %>%
autoplot()
What happens as you change s.window and t.window?
1 Introduction
2 Time series components
3 Seasonal adjustment
4 History of time series decomposition
5 Moving Averages
6 Classical Decomposition
7 X-11 decomposition (OPTIONAL)
8 STL decomposition
9 Forecasting with decomposition
Seasonal adjustment
Useful by-product of decomposition: an easy way to calculate
seasonally adjusted data.
Additive decomposition: seasonally adjusted data given by
yt − St = Tt + Rt
Multiplicative decomposition: seasonally adjusted data given
yt/St = Tt × Rt
Euro electrical equipment
fit <- stl(elecequip, s.window=7) autoplot(elecequip, series="Data") + autolayer(seasadj(fit), series="Seasonally Adjusted") 2000 2005 2010 Seasonally Adjusted Electrical equipment manufacturing (Euro area) 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. 1 Introduction 2 Time series components 3 Seasonal adjustment 4 History of time series decomposition 5 Moving Averages 6 Classical Decomposition 7 X-11 decomposition (OPTIONAL) 8 STL decomposition 9 Forecasting with decomposition History of time series decomposition Classical method originated in 1920s. Census II method introduced in 1957. Basis for X-11 method and variants (including X-12-ARIMA, X-13-ARIMA) STL method introduced in 1983 TRAMO/SEATS introduced in 1990s. National Statistics Offices ABS uses X-12-ARIMA US Census Bureau uses X-13-ARIMA-SEATS Statistics Canada uses X-12-ARIMA ONS (UK) uses X-12-ARIMA EuroStat use X-13-ARIMA-SEATS Department of Statistics Malaysia ???? History of time series decomposition Classical method originated in 1920s. Census II method introduced in 1957. Basis for X-11 method and variants (including X-12-ARIMA, X-13-ARIMA) STL method introduced in 1983 TRAMO/SEATS introduced in 1990s. National Statistics Offices ABS uses X-12-ARIMA US Census Bureau uses X-13-ARIMA-SEATS Statistics Canada uses X-12-ARIMA ONS (UK) uses X-12-ARIMA EuroStat use X-13-ARIMA-SEATS Department of Statistics Malaysia ???? 19 1 Introduction 2 Time series components 3 Seasonal adjustment 4 History of time series decomposition 5 Moving Averages 6 Classical Decomposition 7 X-11 decomposition (OPTIONAL) 8 STL decomposition 9 Forecasting with decomposition Classical Decomposition The classical method of time series decompostion originated in the 1920s and was widely used until the 1950s. It still forms the basis of many time series decomposition methods, so it is important to know how it works. The first step in a classical decomposition is to use a moving average method to estimate the trend-cycle. Thus, this section discusses moving averages. Moving average smoothing A moving average of orderm (i.e. m-MA) can be written as wherem = 2k + 1. The estimate of the trend-cycle at time t is obtained by averaging values of the time series within k periods of t. Observations that are nearby in time are also likely to be close in value. Therefore, the average eliminates some of the randomness in the data, leaving a smooth trend-cycle component. Moving average smoothing: Example autoplot(elecsales) + xlab("Year") + ylab("GWh") + ggtitle("Annual electricity sales to residential customers: South Australia") 1990 1995 2000 2005 Annual electricity sales to residential customers: South Australia Moving average smoothing: Example Year Sales X5.MA 1989 2354.34 NA 1990 2379.71 NA 1991 2318.52 2381.530 1992 2468.99 2424.556 1993 2386.09 2463.758 Year Sales X5.MA 16 2004 3176.20 3307.296 17 2005 3430.60 3398.754 18 2006 3527.48 3485.434 19 2007 3637.89 NA 20 2008 3655.00 NA Moving average smoothing: Example The last column of the table provides an estimate of the trend cycle using a 5-MA. (i.e. m = 5, k = 2) The first value in this column is the average of the first five observations (1989-1993); the second value in the 5-MA column is the average of the values for 1990-1994; and so on. Each value in the 5-MA column is the average of the observations in the five year window centred on the corresponding year. The MA values are easily computed using ma(elecsales, There are no values for either the first two years or the last two years, because we do not have two observations on either side. Moving average smoothing: Example 1990 1995 2000 2005 Annual electricity sales: South Australia Moving average smoothing: Example Notice that the trend-cycle (in red) is smoother than the original data and captures themain movement of the time series without all of the minor fluctuations. The order of the MA determines the smoothness of the trend-cycle estimate. Larger order ⇒ smoother curve. Moving average smoothing: Example 1990 1995 2000 2005 1990 1995 2000 2005 1990 1995 2000 2005 1990 1995 2000 2005 Moving average smoothing Simple moving averages such as the above are usually of an odd order (e.g. 3, 5, 7, etc.). This is so that they are symmetric: in a moving average of orderm = 2k + 1, the middle observation, and k observations on either side, are averaged. Ifm was even ⇒ no longer symmetric. Moving averages of moving averages Ifm is even, a moving average has to be applied to a moving average to make an even-order moving average symmetric. For example, we might take a MA of order 4, and then apply another MA of order 2 to the results. Moving averages of moving averages: Example beer2 <- window(ausbeer, start = 1992) ma4 <- ma(beer2, order = 4, centre = F) ma2x4 <- ma(beer2, order = 4, centre = T) ## Observation 4-MA 2x4-MA ## 1992 Q1 443 NA NA ## 1992 Q2 410 451.25 NA ## 1992 Q3 420 448.75 450.000 ## 1992 Q4 532 451.50 450.125 ## 1993 Q1 433 449.00 450.250 ## 1993 Q2 421 444.00 446.500 ## 1993 Q3 410 448.00 446.000 ## 1993 Q4 512 438.00 443.000 ## 1994 Q1 449 441.25 439.625 ## 1994 Q2 381 446.00 443.625 Moving averages of moving averages: Example The notation 2 x 4-MA in the last column means a 4-MA followed by a 2-MA. The values in the last column are obtained by taking a moving average of order 2 of the values in the previous column. For example, the first two values in the 4-MA column are (443 + 410 + 420 + 532) (410 + 420 + 532 + 433) Moving averages of moving averages: Example The first value in the 2x4-MA column is the average of these (451.25 + 448.75) Moving averages of moving averages When a 2-MA follows a moving average of an even order (such as 4), it is called a centred moving average of order 4. This is because the results are now symmetric. To see that this is the case, we can write the 2× 4-MA as follows: yt−2 + yt−1 + yt + yt+1 yt−1 + yt + yt+1 + yt+2 Note that the 2×m-MA is now equivalent to a weighted moving average of orderm + 1 where all observations take the weight 1/m, except for the first and last terms which take weights 1/(2m). In our example,m = 4. Moving averages of moving averages: Application The most common application of CMA is for estimating the trend-cycle from seasonal data. Consider the 2× 4-MA: When applied to quarterly data, each quarter of the year is given equal weight as the first and last terms apply to the same quarter in consecutive years. Consequently, the seasonal variation will be averaged out and the resulting values of T̂t will have little or no seasonal variation remaining. Moving averages of moving averages: Application If the seasonal period is even and of orderm, we use a 2×m-MA to estimate the trend-cycle. If the seasonal period is odd and of orderm, we use am-MA to estimate the trend-cycle. For example, a 2× 12-MA can be used to estimate the trend-cycle ofmonthly data; a 7-MA can be used to estimate the trend-cycle of daily data with a weekly seasonality. Other choices for the order of the MA will usually result in trend-cycle estimates being contaminated by seasonality in Example: 2 x 12 MA for Electrical Equipment Manufactur- 2000 2005 2010 Electrical equipment manufacturing (Euro area) 1 Introduction 2 Time series components 3 Seasonal adjustment 4 History of time series decomposition 5 Moving Averages 6 Classical Decomposition 7 X-11 decomposition (OPTIONAL) 8 STL decomposition 9 Forecasting with decomposition Classical Decomposition It is a relatively simple procedure, and forms the starting point for most other methods of time series decomposition. Two forms of classical decomposition: an additive decomposition and a multiplicative decomposition. We will describe them for a time series with seasonal period The seasonal component is assumed constant from year to For multiplicative seasonality, them values that form the seasonal component are sometimes called the seasonal Additive Decomposition Ifm is an even number, compute the trend-cycle component T̂t using a 2×mMA. Ifm is an odd number, compute the trend-cycle component T̂t using anm-MA. Calculate the detrended series: yt − T̂t To estimate the seasonal component for each season, simply average the detrended values for that season. For example, with monthly data, the seasonal component for March is the average of all the detrended March values in the data. These seasonal component values are then adjusted to ensure that they add to zero. 40 Additive Decomposition Step 3 (Continued) The seasonal component is obtained by stringing together these monthly values, and then replicating the sequence for each year of data. This gives Ŝt. The remainder component is calculated by subtracting the estimated seasonal and trend-cycle components: R̂t = yt − T̂t − Ŝt. Multiplicative Decomposition Similar with additive, except: Subtractions from Step 2 and Step 4 are replaced by divisions; In Step 3, the seasonal indexes are adjusted to ensure that they add tom. Euro Electrical Equipment fit <- decompose(elecequip, type = "additive") autoplot(fit) + xlab("Year") 2000 2005 2010 Decomposition of additive time series Euro Electrical Equipment ggsubseriesplot(seasonal(fit)) Mar Apr Jul Aug Sep Oct Nov Dec 1 Introduction 2 Time series components 3 Seasonal adjustment 4 History of time series decomposition 5 Moving Averages 6 Classical Decomposition 7 X-11 decomposition (OPTIONAL) 8 STL decomposition 9 Forecasting with decomposition X-11 decomposition library(seasonal) fit <- seas(elecequip, x11="") autoplot(fit) 2000 2005 2010 X11 decomposition of electrical equipment index (Dis)advantages of X-11 Advantages Relatively robust to outliers Completely automated choices for trend and seasonal Very widely tested on economic data over a long period of Disadvantages No prediction/confidence intervals Ad hoc method with no underlying model Only developed for quarterly and monthly data (Dis)advantages of X-11 Advantages Relatively robust to outliers Completely automated choices for trend and seasonal Very widely tested on economic data over a long period of Disadvantages No prediction/confidence intervals Ad hoc method with no underlying model Only developed for quarterly and monthly data Extensions: X-12-ARIMA and X-13-ARIMA The X-11, X-12-ARIMA and X-13-ARIMA methods are based on Census II decomposition. These allow adjustments for trading days and other explanatory variables. Known outliers can be omitted. Level shifts and ramp effects can be modelled. Missing values estimated and replaced. Holiday factors (e.g., Easter, Labour Day) can be estimated. 1 Introduction 2 Time series components 3 Seasonal adjustment 4 History of time series decomposition 5 Moving Averages 6 Classical Decomposition 7 X-11 decomposition (OPTIONAL) 8 STL decomposition 9 Forecasting with decomposition STL decomposition STL: “Seasonal and Trend decomposition using Loess”; Loess refers to a non-linear regression technique Very versatile and robust. 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, so that occasional unusual observations will not affect the estimates of the trend-cycle and seasonal components. Will not go through technical derivations of it, but will learn by looking at examples and experiment with STL decomposition fit <- stl(elecequip, s.window=5, robust=TRUE) autoplot(fit) + ggtitle("STL decomposition of electrical equipment index") 2000 2005 2010 STL decomposition of electrical equipment index STL decomposition fit <- stl(elecequip, s.window="periodic", robust=TRUE) autoplot(fit) + ggtitle("STL decomposition of electrical equipment index") 2000 2005 2010 STL decomposition of electrical equipment index STL decomposition The two main parameters to be chosen when using STL are the trend-cycle window (t.window) and the seasonal window (s.window). These control how rapidly the trend-cycle and seasonal components can change. Smaller values allow for more rapid changes. Both t.window and s.window should be odd numbers; t.window is the number of consecutive observations to be used when estimating the trend-cycle; controls wiggliness of trend component. s.window is the number of consecutive years to be used in estimating each value in the seasonal component; controls variation on seasonal component STL decomposition s.windowmust be specified; there is no default value for it in the stl() function. If s.window = "periodic", then it is equivalent to forcing the seasonal component to be identical across years. 1 Introduction 2 Time series components 3 Seasonal adjustment 4 History of time series decomposition 5 Moving Averages 6 Classical Decomposition 7 X-11 decomposition (OPTIONAL) 8 STL decomposition 9 Forecasting with decomposition Forecasting and decomposition Decompose time series into seasonally adjusted and seasonal Forecast seasonal component by repeating the last year (i.e. seasonal naive forecast) Forecast seasonally adjusted data using non-seasonal time series method. 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. Electrical equipment fit <- stl(elecequip, t.window=13, s.window="periodic") fit %>% seasadj() %>% naive() %>%
autoplot() + ylab(“New orders index”) +
ggtitle(“ETS forecasts of seasonally adjusted data”)
2000 2005 2010
ETS forecasts of seasonally adjusted data
## Electrical equipment
Above figure shows naive forec
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com