Professional Documents
Culture Documents
Forecasting in R
4. Seasonality and trends
bit.ly/fable2020
Outline
2
Outline
3
Time series decomposition
5
Decomposition dable
dcmp <- elecequip %>%
model(STL(value ~ season(window = 7))) %>%
components()
dcmp
autoplot(dcmp) + xlab("Year")
STL decomposition
value = trend + season_year + remainder
120
value
100
80
60
110
100
trend
90
80
10
season_year
0
−10
−20
remainder
0
−4
−20
−10
0
10
2000
Jan
2005
2010
2000
Feb
2005
2010
2000
Mar
2005
2010
2000
Apr
2005
2010
2000
May
2005
2010
2000
Jun
2005
2010
index
Euro electrical equipment
2000
Jul
2005
2010
2000
Aug
2005
2010
2000
Sep
2005
2010
2000
Oct
2005
dcmp %>% gg_subseries(season_year)
2010
2000
Nov
2005
2010
2000
Dec
2005
2010 STL(value ~ season(window = 7))
8
Euro electrical equipment
elecequip %>%
autoplot(value) +
autolayer(dcmp, trend, col='blue')
120
100
value
80
60
2000 2005 2010
index [1M]
9
Australian holidays
holidays %>% autoplot(Trips) +
ylab("thousands of trips") + xlab("Year") +
ggtitle("Australian domestic holiday nights")
State
3000 ACT
thousands of trips
2000 Queensland
South Australia
Tasmania
Victoria
1000
Western Australia
0
2000 2005 2010 2015
Year 10
Holidays decomposition
holidays %>%
model(stl = STL(Trips ~ season(window = "periodic"), robust = TRUE)) %>%
components() %>%
autoplot()
STL decomposition
Trips = trend + season_year + remainder
4000
3000
Trips
2000
1000
0 State/.model
3000 ACT/stl
trend
2000 New South Wales/stl
1000 Northern Territory/stl
0 Queensland/stl
season_year
500 South Australia/stl
250
Tasmania/stl
0
−250 Victoria/stl
remainder
250
0
−250
−500
2000 2005 2010 2015
Quarter
11
Holidays decomposition
holidays %>%
model(stl = STL(Trips ~ season(window = 5), robust = TRUE)) %>%
components() %>%
autoplot()
STL decomposition
Trips = trend + season_year + remainder
4000
3000
Trips
2000
1000
0 State/.model
3000 ACT/stl
trend
2000 New South Wales/stl
1000 Northern Territory/stl
0 Queensland/stl
season_year
500 South Australia/stl
250
0 Tasmania/stl
−250 Victoria/stl
−500
Western Australia/stl
500
remainder
250
0
−250
−500
2000 2005 2010 2015
Quarter
12
STL decomposition
holidays %>%
model(stl = STL(Trips ~ trend(window=15) + season(window=13),
robust = TRUE))
Q1 Q2 Q3 Q4
40
ACT
20
stl
New South
0
−20
600
400
200
stl
Northern
0
−200
Wales Territory
150
100
50
stl
0
−50
−100
Queensland
season_year
200
100
stl
0
South Australia
−100
100
50
stl
0
−50
−100
TasmaniaVictoria
200
100
stl
0
−100
−200
750
500
250
stl
Western Australia
0
−250
−500
100
stl
0
−100
2000
2005
2010
2015
2000
2005
2010
2015
2000
2005
2010
2015
2000
2005
2010
2015
15
Quarter
Holidays decomposition
autoplot(dcmp, trend, scale_bars = FALSE) +
autolayer(holidays, alpha = 0.4)
STL decomposition
trend State/.model
4000 ACT
ACT/stl
New South Wales
New South Wales/stl
3000
Northern Territory
Northern Territory/stl
Queensland
trend
2000 Queensland/stl
South Australia
South Australia/stl
Tasmania
1000
Tasmania/stl
Victoria
Victoria/stl
0 Western Australia
2000 2005 2010 2015 Western Australia/stl 16
Quarter
Outline
17
Lab Session 8
1 Produce the following decomposition
canadian_gas %>%
model(STL(Volume ~ season(window=7) + trend(window=11))) %>%
components() %>%
autoplot()
19
Outline
20
Seasonal adjustment
21
Euro electrical equipment
dcmp <- elecequip %>%
model(STL(value ~ season(window = 7))) %>%
components()
elecequip %>% autoplot(value, col = "gray") +
autolayer(dcmp, season_adjust, col = "blue") +
xlab("Year") + ylab("New orders index") +
ggtitle("Electrical equipment manufacturing (Euro area)")
120
New orders index
100
80
60
2000 2005 2010 22
Year
Seasonal adjustment
23
The ABS stuff-up
24
The ABS stuff-up
25
The ABS stuff-up
26
The ABS stuff-up
employed
Total employed
10000
Thousands
8000
6000
Total employed
11500
11000
Thousands
10500
10000
29
2006 2008 2010 2012 2014
The ABS stuff-up
employed %>%
filter(Year >= 2005) %>%
gg_season(Employed, label = "right") +
ggtitle("Total employed") + ylab("Thousands")
Total employed
2014 2013
11500 2012
2011
2010
11000 2009
2008
Thousands
2007
10500
2006
2005
10000
30
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
The ABS stuff-up
employed %>%
mutate(diff = difference(Employed)) %>%
filter(Month == "Sep") %>%
ggplot(aes(y = diff, x = 1)) +
geom_boxplot() + coord_flip() +
ggtitle("Sep - Aug: total employed") +
xlab("") + ylab("Thousands") +
scale_x_continuous(breaks = NULL, labels = NULL)
31
The ABS stuff-up
dcmp <- employed %>%
filter(Year >= 2005) %>%
model(stl = STL(Employed ~ season(window = 11), robust = TRUE))
components(dcmp) %>% autoplot()
STL decomposition
Employed = trend + season_year + remainder
11500
Employed
11000
10500
10000
11500
11000
trend
10500
10000
season_year remainder
100
50
0
−50
−100
100
50
0
−50
−100 32
2006 2008 2010 2012 2014
Time
The ABS stuff-up
components(dcmp) %>%
filter(year(Time) == 2013) %>%
gg_season(season_year) +
ggtitle("Seasonal component") +
guides(colour = "none")
Seasonal component
100
50
season_year
−50
−100
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 33
Time
The ABS stuff-up
components(dcmp) %>%
as_tsibble() %>%
autoplot(season_adjust)
11500
season_adjust
11000
10500
10000
34
The ABS stuff-up