Chapter2 Forecasting
Chapter2 Forecasting
tools
Instructor: Truong Buu Chau
Email: [email protected]
November 6, 2018
1 Graphics
2 Numerical summaries
3 Some simple forecasting methods
4 Box-Cox transformations
5 Residual diagnostics
6 Evaluating forecast accuracy
7 Prediction intervals
For observations that are more frequent than once per year,
add a ’frequency’ argument.
Ex: monthly data stored as a numerical vector ’z’:
Class: “ts”
Print and plotting methods available.
ausgdp
autoplot(ausgdp)
> library(fpp2)
> library(fpp2)
This loads:
some data for use in examples and exercises
forecast package (for forecasting functions)
ggplot2 package (for graphics functions)
fma package (for lots of time series data)
expsmooth package (for more time series data)
autoplot(melsyd[,"Economy.Class"])
Your turn
Create plots of the following time series: dole,
bricksq, lynx, goog
Use help() to find out about the data in each series.
For the last plot, modify the axis labels and title.
ggseasonplot(a10, year.labels=TRUE,
year.labels.left=TRUE) + ylab("$ million") +
ggtitle("Seasonal plot: antidiabetic drug sales")
ggseasonplot(beer,year.labels=TRUE)
ggsubseriesplot(beer)
Your turn
The arrivals data set comprises quarterly international
arrivals (in thousands) to Australia from Japan, New
Zealand, UK and the US.
autoplot(ustreas) +
ggtitle("US Treasury Bill Contracts") +
xlab("Day") + ylab("price")
90
price
88
86
0 20 40 60 80 100
Day
autoplot(elecdemand[,c("Demand","Temperature")], facets=TRUE)
ylab(" ") + xlab("Year: 2014") +
ggtitle("Half-hourly electricity demand: Victoria, Australia")
Your turn
Can you spot any seasonality, cyclicity and trend? What do
you learn about the series?
hsales
usdeaths
bricksq
sunspotarea
gasoline
autoplot(hsales) +
ggtitle("Sales of new one-family houses, USA")
+ xlab("Year") + ylab("Total sales")
autoplot(bricksq) +
ggtitle("Australian clay brick production") +
xlab("Year") + ylab("million units")
Covariance
1 X n
CovXY = (Xi − X̄ )(Yi − Ȳ ) (1)
n − 1 i=1
Correlation
Pn
CovXY i=1 (Xi − X̄ )(Yi − Ȳ )
rXY = = qP qP (2)
SX SY n
i=1 (Xi − X̄ )
2 n
i=1 (Yi − Ȳ )
2
Autocorrelation
Example: Beer production
r4 higher than for the other lags. This is due to the
seasonal pattern in the data: the peaks tend to be 4
quarters apart and the troughs tend to be 2 quarters
apart.
r2 is more negative than for the other lags because
troughs tend to be 2 quarters behind peaks.
wn <- ts(rnorm(36))
autoplot(wn)
r1 -0.23 0.2
r2 0.04
ACF
0.0
r3 -0.20
r4 0.03 −0.2
r5 0.19 1 2 3 4 5 6 7 8
Lag
9 10 11 12 13 14 15
r6 -0.01
r7 -0.23
r8 0.07
r9 0.01
r10 -0.01
We expect each autocorrelation to be close to zero.
Your turn
You can compute the daily changes in the Google stock
price using
Average method
Forecast of all future values is equal to mean of historical
data {y1 , . . . , yT }.
Forecasts: ŷT +h|T = ȳ = (y1 + · · · + yT )/T
Average method
Forecast of all future values is equal to mean of historical
data {y1 , . . . , yT }.
Forecasts: ŷT +h|T = ȳ = (y1 + · · · + yT )/T
Naïve method
Forecasts equal to last observed value.
Forecasts: ŷT +h|T = yT .
Consequence of efficient market hypothesis.
Drift method
Forecasts equal to last value plus average change.
Forecasts: T
h X
ŷT +h|T = yT + (yt − yt−1 )
T − 1 t=2
h
= yT + (yT − y1 ).
T −1
Equivalent to extrapolating a line drawn between first and
last observations.
Truong Buu Chau C03043 - Chapter 2 November 6, 2018 46 / 70
3. Some simple forecasting methods
Forecasts for quarterly beer production
500
Forecast
Megalitres
Mean
450 Naive
Seasonal naive
400
Your turn
Logarithms, in particular, are useful because they are more interpretable: changes
in a log value are relative (percent) changes on the original scale.
Truong Buu Chau C03043 - Chapter 2 November 6, 2018 50 / 70
4. Box-cox-transformations
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
exp(wt ), λ = 0;
yt =
(λWt + 1)1/λ , λ 6= 0.
Assumptions
1 {et } uncorrelated. If they aren’t, then information left in
residuals that should be used in computing forecasts.
2 {et } have mean zero. If they don’t, then forecasts are
biased.
Assumptions
1 {et } uncorrelated. If they aren’t, then information left in
residuals that should be used in computing forecasts.
2 {et } have mean zero. If they don’t, then forecasts are
biased.
Useful properties (for prediction intervals)
3 {et } have constant variance.
4 {et } are normally distributed.
Truong Buu Chau C03043 - Chapter 2 November 6, 2018 59 / 70
5. Residual diagnostics
ACF of residuals
We assume that the residuals are white noise
(uncorrelated, mean zero, constant variance). If they
aren’t, then there is information left in the residuals
that should be used in computing forecasts.
So a standard residual diagnostic is to check the ACF of
the residuals of a forecasting method.
We expect these to look like white noise.
A model which fits the training data well will not necessarily
forecast well.
A perfect fit can always be obtained by using a model with
enough parameters.
Over-fitting a model to data is just as bad as failing to
identify a systematic pattern in the data.
The test set must not be used for any aspect of model
development or calculation of forecasts.
Forecast accuracy is based only on the test set.
Truong Buu Chau C03043 - Chapter 2 November 6, 2018 61 / 70
6. Evaluating forecast accuracy
eT +h = yT +h − ŷT +h|T ,
500
Forecast Method
Megalitres
Mean
450 Naive
SeasonalNaive
400
MAE = mean(|eT +h |)
MSE = mean(eT2 +h )
q
RMSE = mean(eT2 +h )
MAPE = 100mean(|eT +h |/|yT +h |)
MAE = mean(|eT +h |)
MSE = mean(eT2 +h )
q
RMSE = mean(eT2 +h )
MAPE = 100mean(|eT +h |/|yT +h |)