library(fpp3)
Exercise 5.1
gdppc <- global_economy |>
mutate(GDP_per_capita = GDP / Population)
gdppc |>
filter(Country == "Sweden") |>
autoplot(GDP_per_capita) +
labs(y = "$US", title = "GDP per capita for Sweden")

fit <- gdppc |>
model(trend_model = TSLM(GDP_per_capita ~ trend()))
## Warning: 7 errors (1 unique) encountered for trend_model
## [7] 0 (non-NA) cases
fit
## # 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 Antigua and Barbuda <TSLM>
## 8 Arab World <TSLM>
## 9 Argentina <TSLM>
## 10 Armenia <TSLM>
## # ℹ 253 more rows
fit |>
forecast(h = "3 years") |>
filter(Country == "Sweden") |>
autoplot(gdppc) +
labs(y = "$US", title = "Forecast GDP per capita for Sweden")

Exercise 5.2
bricks <- aus_production |>
filter_index("1970 Q1" ~ "2004 Q4") |>
select(Bricks)
bricks
## # A tsibble: 140 x 2 [1Q]
## Bricks Quarter
## <dbl> <qtr>
## 1 386 1970 Q1
## 2 428 1970 Q2
## 3 434 1970 Q3
## 4 417 1970 Q4
## 5 385 1971 Q1
## 6 433 1971 Q2
## 7 453 1971 Q3
## 8 436 1971 Q4
## 9 399 1972 Q1
## 10 461 1972 Q2
## # ℹ 130 more rows
bricks_fit <- bricks |>
model(
Mean = MEAN(Bricks),
Naive = NAIVE(Bricks),
Seasonal_Naive = SNAIVE(Bricks),
Drift = RW(Bricks ~ drift())
)
bricks_fit
## # A mable: 1 x 4
## Mean Naive Seasonal_Naive Drift
## <model> <model> <model> <model>
## 1 <MEAN> <NAIVE> <SNAIVE> <RW w/ drift>
bricks_fit |>
forecast(h = 8) |>
autoplot(bricks)

Exercise 5.3
train <- aus_production |>
filter_index("1992 Q1" ~ "2006 Q4")
beer_fit <- train |>
model(
Mean = MEAN(Beer),
Naive = NAIVE(Beer),
Seasonal_Naive = SNAIVE(Beer)
)
augment(beer_fit)
## # A tsibble: 180 x 6 [1Q]
## # Key: .model [3]
## .model Quarter Beer .fitted .resid .innov
## <chr> <qtr> <dbl> <dbl> <dbl> <dbl>
## 1 Mean 1992 Q1 443 436. 6.55 6.55
## 2 Mean 1992 Q2 410 436. -26.4 -26.4
## 3 Mean 1992 Q3 420 436. -16.4 -16.4
## 4 Mean 1992 Q4 532 436. 95.6 95.6
## 5 Mean 1993 Q1 433 436. -3.45 -3.45
## 6 Mean 1993 Q2 421 436. -15.4 -15.4
## 7 Mean 1993 Q3 410 436. -26.4 -26.4
## 8 Mean 1993 Q4 512 436. 75.6 75.6
## 9 Mean 1994 Q1 449 436. 12.6 12.6
## 10 Mean 1994 Q2 381 436. -55.4 -55.4
## # ℹ 170 more rows
Exercise 5.4
train |>
model(NAIVE(Beer)) |>
gg_tsresiduals()
## Warning: `gg_tsresiduals()` was deprecated in feasts 0.4.2.
## ℹ Please use `ggtime::gg_tsresiduals()` instead.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 1 row containing non-finite outside the scale range
## (`stat_bin()`).
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_rug()`).

Exercise 5.7
us_retail_employment <- us_employment |>
filter(year(Month) >= 1990, Title == "Retail Trade")
fit_dcmp <- us_retail_employment |>
model(
stlf = decomposition_model(
STL(Employed ~ trend(window = 7), robust = TRUE),
NAIVE(season_adjust)
)
)
fit_dcmp |>
forecast() |>
autoplot(us_retail_employment)
