Essential Forecasting Tools Guide
Essential Forecasting Tools Guide
2
Forcaster’s toolbox
3
A forecasting workflow
1. Preparing data
2. Data visualisation
3. Specifying a model
4. Model estimation
5. Accuracy & performance evaluation
6. Producing forecasts
4
Data preparation (tidy)
gdppc %>%
filter(Country=="Canada") %>%
autoplot(GDP_per_capita) +
labs(title = "GDP per capita for Canada", y = "$US")
50000
40000
30000
$US
20000
10000
0
1960 1980 2000
Year [1Y]
6
Model estimation
## # A mable: 263 x 2
## # Key: Country [263]
## Country trend_model
## <fct> <model>
## 1 Afghanistan <TSLM>
## 2 Albania <TSLM>
## 3 Algeria <TSLM>
## 4 American Samoa <TSLM>
## 5 Andorra <TSLM>
## 6 Angola <TSLM> 7
Producing forecasts
60000
level
40000
$US
80
95
20000
0
1960 1980 2000 2020
Year
9
Some simple forecasting methods
10
Some simple forecasting methods
500
Bricks
400
300
200
450
Bricks
400
350
300
1995 Q1 2000 Q1 2005 Q1 2010 Q1
Quarter
12
Some simple forecasting methods
450
Bricks
400
350
300
1995 Q1 2000 Q1 2005 Q1 2010 Q1
Quarter
13
Model fitting
## # A mable: 1 x 3
## Seasonal_naive Naive Mean
## <model> <model> <model>
## 1 <SNAIVE> <NAIVE> <MEAN>
14
Producing forecasts
## # A fable: 60 x 4 [1Q]
## # Key: .model [3]
## .model Quarter Bricks .mean
## <chr> <qtr> <dist> <dbl>
## 1 Seasonal_naive 2005 Q3 N(428, 2336) 428
## 2 Seasonal_naive 2005 Q4 N(397, 2336) 397
## 3 Seasonal_naive 2006 Q1 N(355, 2336) 355
## 4 Seasonal_naive 2006 Q2 N(435, 2336) 435
## # ... with 56 more rows
15
Visualising forecasts
brick_fc %>%
autoplot(aus_production, level = NULL) +
labs(title = "Clay brick production in Australia",
y = "Millions of bricks") +
guides(colour = guide_legend(title = "Forecast"))
500
Millions of bricks
Forecast
400 Mean
Naive
Seasonal_naive
300
200
16
Facebook closing stock price
17
Facebook closing stock price
200
150 Forecast
$US
Mean
Naive
100
50
0 500 1000
trading_day
18
Exercise to do!
19
Residual diagnostics
20
Fitted values
For example:
• ŷt = ȳ for average method.
21
Forecasting residuals
Assumptions:
1. et ’s are uncorrelated. If they aren’t, then information
left in residuals that should be used in computing
forecasts.
2. et ’s have mean zero. If they don’t, then forecasts are
biased.
Properties (for distributions & prediction intervals):
3. et ’s have constant variance.
4. et ’s are normally distributed.
22
Facebook closing stock price
200
150
Close
100
50
0 400 800 1200
trading_day [1]
23
Facebook closing stock price
fit <- fb_stock %>% model(NAIVE(Close))
augment(fit)
Naïve forecasts:
ŷt|t−1 = yt−1
et = yt − ŷt|t−1 = yt − yt−1 24
Facebook closing stock price
augment(fit) %>%
ggplot(aes(x = trading_day)) +
geom_line(aes(y = Close, colour = "Data")) +
geom_line(aes(y = .fitted, colour = "Fitted"))
200
150 colour
Close
Data
Fitted
100
50
0 400 800 1200
trading_day
25
Facebook closing stock price
augment(fit) %>%
filter(trading_day > 1100) %>%
ggplot(aes(x = trading_day)) +
geom_line(aes(y = Close, colour = "Data")) +
geom_line(aes(y = .fitted, colour = "Fitted"))
200
colour
Close
175
Data
Fitted
150
125
augment(fit) %>%
autoplot(.resid) +
labs(y = "$US",
title = "Residuals from naïve method")
0
$US
−20
−40
27
Facebook closing stock price
augment(fit) %>%
ggplot(aes(x = .resid)) +
geom_histogram(bins = 150) +
labs(title = "Histogram of residuals")
Histogram of residuals
150
100
count
50
0
−40 −20 0
.resid
28
Facebook closing stock price
augment(fit) %>%
ACF(.resid) %>%
autoplot() + labs(title = "ACF of residuals")
ACF of residuals
0.06
0.03
acf
0.00
−0.03
−0.06
10 20 30
lag [1]
29
gg_tsresiduals() function
gg_tsresiduals(fit)
Innovation residuals
−20
−40
0 400 800 1200
trading_day
0.06 150
0.03
100
count
acf
0.00
50
−0.03
−0.06 0
10 20 30 −40 −20 0
lag [1] .resid
30
ACF of residuals
31
Portmanteau tests for autocorrelation
32
Portmanteau tests
33
Portmanteau tests
augment(fit) %>%
features(.resid, ljung_box, lag=10, dof=0)
## # A tibble: 1 x 4
## Symbol .model lb_stat lb_pvalue
## <chr> <chr> <dbl> <dbl>
## 1 FB NAIVE(Close) 12.1 0.276
For Q ∗ , the results are not significant (i.e., the p-values are relatively large).
Thus, we can conclude that the residuals are not distinguishable from a white
noise series.
34
Exercise to do!
35
Forecasts distributions and
prediction intervals
36
Forecast distributions
37
Forecast distributions
38
Prediction intervals
v
u
u 1 XT
σ̂ = t e2,
T − K − M t=1 t
## # A tsibble: 60 x 5 [1Q]
## # Key: .model [3]
## .model Quarter Bricks .mean `95%`
## <chr> <qtr> <dist> <dbl> <hilo>
## 1 Seasonal_naive 2005 Q3 N(428, 2336) 428 [333, 523]95
## 2 Seasonal_naive 2005 Q4 N(397, 2336) 397 [302, 492]95
## 3 Seasonal_naive 2006 Q1 N(355, 2336) 355 [260, 450]95
## 4 Seasonal_naive 2006 Q2 N(435, 2336) 435 [340, 530]95
## 5 Seasonal_naive 2006 Q3 N(428, 4672) 428 [294, 562]95
## 6 Seasonal_naive 2006 Q4 N(397, 4672) 397 [263, 531]95
## 7 Seasonal_naive 2007 Q1 N(355, 4672) 355 [221, 489]95
## 8 Seasonal_naive 2007 Q2 N(435, 4672) 435 [301, 569]95
## 9 Seasonal_naive 2007 Q3 N(428, 7008) 428 [264, 592]95
## 10 Seasonal_naive 2007 Q4 N(397, 7008) 397 [233, 561]95
40
## # ... with 50 more rows
Prediction intervals
41
Evaluating forecast accuracy
42
Training and test sets
• 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.
43
Forecast errors
44
Measures of forecast accuracy
500
Forecast
Megalitres
Mean
450 Naive
Seasonal_naive
400
45
Measures of forecast accuracy
yT +h = (T + h)th observation, h = 1, . . . , H
ŷT +h|T = its forecast based on data up to time T .
eT +h = yT +h − ŷT +h|T
MAE = mean(|eT +h |) q
MSE = mean(eT2 +h ) RMSE = mean(eT2 +h )
MAPE = 100mean(|eT +h |/|yT +h |)
46
Measures of forecast accuracy
X
T
−1
Q = (T − 1) |yt − yt−1 |
t=2
47
Measures of forecast accuracy
X
T
−1
Q = (T − m) |yt − yt−m |
t=m+1
48
Measures of forecast accuracy
500
Forecast
Megalitres
Drift
450 Mean
Naive
Seasonal_naive
400
49
Measures of forecast accuracy
50
Measures of forecast accuracy
accuracy(beer_fit)
## # A tibble: 4 x 6
## .model .type RMSE MAE MAPE MASE
## <chr> <chr> <dbl> <dbl> <dbl> <dbl>
## 1 Drift Training 65.3 54.8 12.2 3.83
## 2 Mean Training 43.6 35.2 7.89 2.46
## 3 Naive Training 65.3 54.7 12.2 3.83
## 4 Seasonal_naive Training 16.8 14.3 3.31 1
accuracy(beer_fc, recent_production)
## # A tibble: 4 x 6
## .model .type RMSE MAE MAPE MASE
## <chr> <chr> <dbl> <dbl> <dbl> <dbl>
## 1 Drift Test 64.9 58.9 14.6 4.12
## 2 Mean Test 38.4 34.8 8.28 2.44
## 3 Naive Test 62.7 57.4 14.2 4.01
## 4 Seasonal_naive Test 14.3 13.4 3.17 0.937
51
Time series cross-validation
52
Time series cross-validation
Traditional evaluation
Training data Test data
time
time
53
Time series cross-validation
Traditional evaluation
Training data Test data
time
time
54
Time series cross-validation
Traditional evaluation
Training data Test data
time
time
55
Time series cross-validation
Traditional evaluation
Training data Test data
time
time
## # A mable: 1,255 x 3
## # Key: .id, Symbol [1,255]
## .id Symbol `NAIVE(Close)`
## <int> <chr> <model>
## 1 1 FB <NAIVE>
## 2 2 FB <NAIVE>
## 3 3 FB <NAIVE>
## 4 4 FB <NAIVE>
## # ... with 1,251 more rows
58
Time series cross-validation
59
Time series cross-validation
# Cross-validated
fc_cv1 %>% accuracy(fb_stock)
# Training set
fb_stock %>% model(NAIVE(Close))%>% accuracy()
A good way to choose the best forecasting model is to find the model
with the smallest RMSE computed using time series cross-validation.
60
Exercise for Class!
61
Exercise for Class!
61
Exercise for Class!
61
Exercise to do!
• Extract data from the Gold Coast region using filter() and
aggregate total overnight trips (sum over Purpose) using
summarise(). Call this new dataset gc_tourism.
• Using slice() or filter(), create three training sets for this
data excluding the last 1, 2 and 3 years. For example,
gc_train_1 <- gc_tourism |> slice(1:(n()-4)).
• Compute one year of forecasts for each training set using the
seasonal naïve ( SNAIVE() ) method. Call these gc_fc_1,
gc_fc_2 and gc_fc_3, respectively.
• Use accuracy() to compare the test set forecast accuracy
using MAPE. Comment on these.
62
Next Lecture!
63