Professional Documents
Culture Documents
Forecasting in R
6. Introduction to forecasting
bit.ly/fable2020
Outline
-1
-2
-3
Actual
-4
-5
2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020
4
Financial year ended
What can we forecast?
5
What can we forecast?
6
What can we forecast?
7
What can we forecast?
8
What can we forecast?
9
What can we forecast?
10
What can we forecast?
11
Which is easiest to forecast?
12
Which is easiest to forecast?
7.5
Data
Future 1
Millions of visitors
Future 2
Future 3
Future 4
5.0 Future 5
Future 6
Future 7
Future 8
Future 9
Future 10
2.5
15
Forecast intervals
Forecasts of total international visitors to Australia
10.0
7.5
Millions of visitors
.level
80
5.0 95
2.5
16
Statistical forecasting
17
Outline
500
megalitres
450
400
19
How would you forecast these series?
Some simple forecasting methods
Number of pigs slaughtered in Victoria, 1990−1995
130000
120000
thousands
110000
100000
90000
20
How would you forecast these series?
Some simple forecasting methods
Facebook closing stock price in 2018
200
Closing price ($USD)
175
150
125
Jan 2018 Apr 2018 Jul 2018 Oct 2018 Jan 2019
21
How would you forecast these series?
Some simple forecasting methods
MEAN(y): Average method
Forecast of all future values is equal to mean of
historical data {y1 , . . . , yT }.
Forecasts: ŷT+h|T = ȳ = (y1 + · · · + yT )/T
Clay brick production in Australia
600
500
Bricks
400
300
200
450
●
Bricks
400
350
300
1995 2000 2005 2010
Quarter
23
Some simple forecasting methods
SNAIVE(y ~ lag(m)): Seasonal naïve method
Forecasts equal to last value from same season.
Forecasts: ŷT+h|T = yT+h−m(k+1) , where m = seasonal
period and k is the integer part of (h − 1)/m.
Clay brick production in Australia
500
450
●
●
Bricks
400 ●
●
350
300
1995 2000 2005 2010
Quarter
24
Some simple forecasting methods
25
Some simple forecasting methods
Drift method
Clay brick production in Australia
600
500
Bricks
400
300
200
26
Model fitting
## # A mable: 1 x 4
## Seasonal_naïve Naïve Drift Mean
## <model> <model> <model> <model>
## 1 <SNAIVE> <NAIVE> <RW w/ drift> <MEAN>
## # A fable: 80 x 4 [1Q]
## # Key: .model [4]
## .model Quarter Bricks .distribution
## <chr> <qtr> <dbl> <dist>
## 1 Seasonal_naïve 2005 Q3 428 N(428, 2336)
## 2 Seasonal_naïve 2005 Q4 397 N(397, 2336)
## 3 Seasonal_naïve 2006 Q1 355 N(355, 2336)
## 4 Seasonal_naïve 2006 Q2 435 N(435, 2336)
## # ... with 76 more rows
500
Millions of bricks
Forecast
Drift
400 Mean
Naïve
300 Seasonal_naïve
200
## # A tsibble: 80 x 5 [1Q]
## # Key: .model [4]
## .model Quarter Bricks 50% 75%
## <chr> <qtr> <dbl> <hilo> <hilo>
## 1 Seasonal_naïve 2005 Q3 428 [395, 461]50 [372, 484]75
## 2 Seasonal_naïve 2005 Q4 397 [364, 430]50 [341, 453]75
## 3 Seasonal_naïve 2006 Q1 355 [322, 388]50 [299, 411]75
## 4 Seasonal_naïve 2006 Q2 435 [402, 468]50 [379, 491]75
## 5 Seasonal_naïve 2006 Q3 428 [382, 474]50 [349, 507]75
## 6 Seasonal_naïve 2006 Q4 397 [351, 443]50 [318, 476]75
## 7 Seasonal_naïve 2007 Q1 355 [309, 401]50 [276, 434]75
## 8 Seasonal_naïve 2007 Q2 435 [389, 481]50 [356, 514]75
## 9 Seasonal_naïve 2007 Q3 428 [372, 484]50 [332, 524]75
30
## 10 Seasonal_naïve 2007 Q4 397 [341, 453]50 [301, 493]75
Prediction intervals
brick_fc %>% hilo(level=c(50,75)) %>% unnest()
## # A tibble: 80 x 9
## .model Quarter Bricks .lower .upper .level .lower1 .upper1
## <chr> <qtr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Seaso~ 2005 Q3 428 395. 461. 50 372. 484.
## 2 Seaso~ 2005 Q4 397 364. 430. 50 341. 453.
## 3 Seaso~ 2006 Q1 355 322. 388. 50 299. 411.
## 4 Seaso~ 2006 Q2 435 402. 468. 50 379. 491.
## 5 Seaso~ 2006 Q3 428 382. 474. 50 349. 507.
## 6 Seaso~ 2006 Q4 397 351. 443. 50 318. 476.
## 7 Seaso~ 2007 Q1 355 309. 401. 50 276. 434.
## 8 Seaso~ 2007 Q2 435 389. 481. 50 356. 514.
## 9 Seaso~ 2007 Q3 428 372. 484. 50 332. 524.
## 10 Seaso~ 2007 Q4 397 341. 453. 50 301. 493.
31
## # ... with 70 more rows, and 1 more variable: .level1 <dbl>
Outline
33
Outline
35
Forecasting residuals
Residuals in forecasting: difference between
observed value and its fitted value: et = yt − ŷt|t−1 .
36
Forecasting residuals
Residuals in forecasting: difference between
observed value and its fitted value: et = yt − ŷt|t−1 .
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.
36
Forecasting residuals
Residuals in forecasting: difference between
observed value and its fitted value: et = yt − ŷt|t−1 .
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. 36
Facebook closing stock price
fb_stock <- gafa_stock %>%
filter(Symbol == "FB") %>%
mutate(trading_day = row_number()) %>%
update_tsibble(index = trading_day, regular = TRUE)
fb_stock %>% autoplot(Close)
200
150
Close
100
50
0 400 800 1200 37
trading_day [1]
Facebook closing stock price
fit <- fb_stock %>% model(NAIVE(Close))
augment(fit)
200
150 colour
Close
Data
Fitted
100
50
0 400 800 1200 39
trading_day
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
40
1100 1150 1200 1250
Facebook closing stock price
augment(fit) %>%
autoplot(.resid) + xlab("Day") + ylab("") +
ggtitle("Residuals from naïve method")
−20
−40
Histogram of residuals
150
100
count
50
0
−40 −20 0
.resid 42
Facebook closing stock price
augment(fit) %>%
ACF(.resid) %>%
autoplot() + ggtitle("ACF of residuals")
ACF of residuals
0.06
0.03
acf
0.00
−0.03
−0.06
10 20 30
lag [1]
43
ACF of residuals
44
Combined diagnostic graph
● ●
●
● ● ● ●●● ●● ●
● ● ● ● ● ● ● ● ●● ●●● ●●● ●●
● ●
●
● ● ●●● ●●
●●●●●●
●●●
● ●●●● ●●●
● ●●
● ● ● ● ● ●● ● ● ● ●●●●●●●●
● ●● ●
● ●●●● ● ●●
● ●●●●
● ●
●●●
● ●
● ●
●●●● ● ●●●
● ●●●●●●
●●●●●● ●●●●●●●●●
●●
●●●●●●● ●
●●●
●● ●
● ●
●●● ●●●●
●● ●
●
●
● ●
●
●●●●
●●
● ●● ●●
● ● ●●●●
● ●
●●●●●
0 ●● ●
●●●
● ●●
●●
●
●●
●●
● ●
●
●●
●
●●
●
● ●
●
●●
●
●
●●●
●●
●●
●●
●●
●●
●●
●
●●
●
●●
●
●
●●
●
●●
●●
●
●●
●●
●●
●
●●
●
●
●
●●
●●
●
●●●●
●●
●
●●●
●●
●
●
●●
●●
●
●
●●●
●●●●
●●
●●
●●
●●●
●●
●●
●
●●●
●
●●●●
●
●●●
●
●
●●
●●
●
●
● ●●
●●
●●
●●
●
●●●
●●●
●●
●●●
● ●●
●●●
●●
●
●●●
●●
●
●●
●●
●
●●
●●
●
●
●●
●●●
●●
●
●●
●
●●
●●●
●●
●●
●
●●
●
●●
●●
●
●●
●
●●
●
●
●●
●●●●●●●
● ●●
●
●●●
●●●
●●
●
●●
●●
●
●●
●
●●●●
●
●
●● ●
●●
●
●●
●●
● ●
●●
●
●●
●●
●●●
●●
● ●●●
●●
●
●●
●
●●
●
●
●●●
●●●
●
●●
●●
●● ●●●
●●
● ●
●●
●
●●
●●
●●●●
●●● ●●●●●
●●●●
● ●
●●
● ●
●●
● ●●
● ●●
●●●
●
●●●
●
●●
●●
●
●●●
●●
●
●●
●
● ●●
●
●●●
●● ●
●
●●●●●●●
● ●●
●
●
●● ● ●●
●●
●
●●
●
●●●
●
●●
●●
●
● ● ●●●
●●
●● ●
●●●
●●
●●
●●●●
●●●
●
●
● ● ●● ●●
●
● ●●
●
●
●●
●●●
● ●
●
● ●●● ●
●
●
●●●●
●
●●●
●
●●●
●●
●●
●
●● ● ●●●
●● ●●
●
●
●
● ●●●● ●
●
● ●●
●●●
●
●●
●
●●
● ●●●●●●●
● ●
●●●●
●●
●●●
● ●
●●
●●
● ●●
● ●●
●●
●
●●●●
●
●
●
●●●
●●●
●
●●●●●
●●●
●
●●●● ● ●●
●●●
●● ●
●●
●●
●●●●●
●●
● ●
●●
●
●●●●
●
●●
●●
●
●●● ●
●●
●●
●
●●
●●
●●
●●
●●
●●
●
●●
●
● ●
●
●●
●●●
●●
●●
●
●● ●
●●●●
● ●●●●● ●
●●●
●●●●
●● ●
●
●●
●●●●●
●●
●
●● ●●●● ●●●●● ●
●
●●●● ● ●
●●
●●●
●●
●
●●●●
●
● ●●●●
●●●
●●
●●
●●
● ●●
● ●
●
● ●●
●●●●●●●●●
● ●●
●
● ● ● ●● ● ● ●●●
●●
●● ● ●
●
●
● ● ● ●● ● ●●●● ●●
●
●●
● ●
● ● ●●
●
●
● ●●●● ●●●
● ●
●●●●
● ●●●
● ●
.resid
● ● ● ● ●● ●● ●
● ● ●
●
●
−20
−40 ●
0.03 100
count
acf
0.00
50
−0.03
−0.06 0
10 20 30 −40 −20 0
lag [1] .resid
45
Ljung-Box test
0.00
−0.03
−0.06
2.5 5.0 7.5 10.0
lag [1]
## # A tibble: 1 x 4
## Symbol .model lb_stat lb_pvalue
## <chr> <chr> <dbl> <dbl> 47
## 1 FB NAIVE(Close) 12.1 0.276
Outline
49
Outline
52
Measures of forecast accuracy
500
Megalitres
Forecast
400 mean
snaive
300
200
1960 1980 2000
Year
53
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(e2T+h ) RMSE = mean(e2T+h )
MAPE = 100mean(|eT+h |/|yT+h |)
54
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(e2T+h ) RMSE = mean(e2T+h )
MAPE = 100mean(|eT+h |/|yT+h |)
## # A tibble: 2 x 9
## .model .type ME RMSE MAE MPE MAPE MASE ACF1
## <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 mean Test -13.8 38.4 34.8 -3.97 8.28 2.20 -0.0691
## 2 snaive Test 5.2 14.3 13.4 1.15 3.17 0.847 0.132
57
Outline