R - Forecasting
Contents |
[edit] Approaches to Forecasting
- ARIMA (AutoRegresive Integrated Moving Average)
- ETS (Exponential smoothing state space model)
We will discuss how those methods work and how to use them.
[edit] Forecast package overview
# Install Libraries install.packages("forecast") library("forecast") rawdata <- read.table("http://training-course-material.com/images/1/19/Sales-time-series.txt",h=T) rawdata$Date <- as.Date(rawdata$Date) head(rawdata) plot(rawdata) # Using build in ts object sts <- ts(rawdata$Sales,start=2001,frequency=12) plot(sts) # Forecast using ETS method fc.ets = forecast(sts) plot(fc.ets) plot(fc.ets$residuals) plot(fc.ets$fitted) # Forecast using ARIMA method ar = auto.arima(sts) ar fc.arima = forecast(ar) fc.arima plot(fc.arima) accuracy(fc.ets) accuracy(fc.arima)
[edit] Exponential Smoothing
- Names
- AKA: exponentially weighted moving average (EWMA)
- Equivalent to ARIMA (0,1,1) model with no constant term
- Used for
- smoothed data for presentation
- make forecasts
- simple moving average: past observations are weighted equally
- exponential smoothing: assigns exponentially decreasing weights over time
- Formula
- {x_{t}} - raw data sequence
- {s_{t}} - output of the exponential smoothing algorithm (estimate of the next value of x)
- α - smoothing factor, 0 < α < 1.
- Choosing right α
- no formal way of choosing α
- statistical technique may be used to optimize the value of α (e.g. OLS)
- the bigger the α the close it gets to naive forecasting (the same ports as original series with one period lag)
[edit] Double Exponential Smoothing
- Simple exponential smoothing does not do well when there is a trend (there will be always bias)
- Double exponential smoothing is a group of methods dealing with the problem
[edit] Holt-Winters double exponential smoothing
- Input
- {x_{t}} - raw data sequence of observations
- t = 0
- Model
- {s_{t}} - smoothed value for time t
- {b_{t}} - best estimate of the trend at time t
And for t > 1 by
where α is the data smoothing factor, 0 < α < 1, and β is the trend smoothing factor, 0 < β < 1.
- Output
- F_{t+m} - an estimate of the value of x at time t+m, m>0 based on the raw data up to time t
To forecast beyond x_{t}
[edit] Triple exponential smoothing
- takes into account seasonal changes as well as trends
- first suggested by Holt's student, Peter Winters, in 1960
- Input
- {x_{t}} - raw data sequence of observations
- t = 0
- L length a cycle of seasonal change
The method calculates:
- a trend line for the data
- seasonal indices that weight the values in the trend line based on where that time point falls in the cycle of length L.
- {s_{t}} represents the smoothed value of the constant part for time t.
- {b_{t}} represents the sequence of best estimates of the linear trend that are superimposed on the seasonal changes
- {c_{t}} is the sequence of seasonal correction factors
- c_{t} is the expected proportion of the predicted trend at any time t mod L in the cycle that the observations take on
- To initialize the seasonal indices c_{t-L} there must be at least one complete cycle in the data
The output of the algorithm is again written as F_{t+m}, an estimate of the value of x at time t+m, m>0 based on the raw data up to time t. Triple exponential smoothing is given by the formulas
where α is the data smoothing factor, 0 < α < 1, β is the trend smoothing factor, 0 < β < 1, and γ is the seasonal change smoothing factor, 0 < γ < 1.
The general formula for the initial trend estimate b_{0} is:
Setting the initial estimates for the seasonal indices c_{i} for i = 1,2,...,L is a bit more involved. If N is the number of complete cycles present in your data, then:
where
Note that A_{j} is the average value of x in the jth cycle of your data.
[edit] ETS
- Error, Trend, Seasonality
[edit] Overridng parameters
rawdata <- read.table("http://training-course-material.com/images/1/19/Sales-time-series.txt",h=T) rawdata$Date <- as.Date(rawdata$Date) head(rawdata) plot(rawdata) # Using build in ts object sts <- ts(rawdata$Sales,start=2001,frequency=12) plot(sts) # Forecast using ETS method model.ets = ets(sts,model="ANA") fc.ets = forecast(model.ets) model.ets1 = ets(sts,model="AAA",beta=0.2) fc.ets1 = forecast(model.ets1) plot(fc.ets1) accuracy(fc.ets) accuracy(fc.ets1)