library(tidyverse)
library(fpp3)
library(fredr)
library(ggthemes)
pce <- fredr(
series_id = "NA000349Q",
observation_start = as.Date("1950-01-01"),
observation_end = as.Date("2023-10-01"),
frequency = "q", # quarterly
units = "lin"
) |>
select(date, value) |>
mutate(date = tsibble::yearquarter(date)) |>
as_tsibble()Discussion 2
Pick a time series dataset of interest to you. Using this data (be sure to sort in ascending order), build two or three ETS models. Which performs better? What explanation might explain that?
Collect data from FRED
Seasonally un-adjusted quarterly PCE
Plot
Change points -
- Decline during GFC
- Increase post-COVID pandemic recession
pce |>
ggplot(aes(date, value)) +
geom_line(linewidth = 1.25) +
scale_x_yearquarter(
breaks = scales::breaks_width("10 years")
) +
scale_y_continuous(
breaks = scales::breaks_width(750000),
labels = scales::label_dollar()
) +
labs(
title = "Personal consumption expenditures",
subtitle = "Not seasonally adjusted, in billions of dollars"
) +
ggthemes::theme_economist()STL decomposition
decomp <- pce |>
model(stl = STL(value))
decomp |>
components() |>
autoplot()pce |>
autoplot(value) +
autolayer(components(decomp), season_adjust, color = "darkred") +
scale_y_continuous(
breaks = scales::breaks_width(500000),
labels = scales::label_dollar()
) +
ggthemes::theme_economist()Train/test splits
train <- pce |>
filter_index("1950 Q1" ~ "2000 Q1")fit <- train |>
model(
additive = ETS(value ~ error("A") + trend("A") + season("A")),
multiplicative = ETS(value ~ error("M") + trend("M") + season("M")),
additive_errors = ETS(value ~ error("A") + trend("N") + season("N")),
mult_errors = ETS(value ~ error("M") + trend("N") + season("N"))
)Forecast
ets_fc <- fit |>
forecast(h = 92)ets_fc |>
autoplot(
pce,
level = NULL
) +
ggthemes::theme_economist()Model performance
fit |> report()# A tibble: 4 × 9
.model sigma2 log_lik AIC AICc BIC MSE AMSE MAE
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 additive 3.32e+7 -2270. 4557. 4558. 4587. 31924094. 4.97e7 3.64e+3
2 multiplicative 1.80e-4 -2173. 4365. 4366. 4394. 25961771. 4.27e7 9.13e-3
3 additive_errors 7.42e+8 -2585. 5175. 5176. 5185. 734928640. 1.14e9 1.91e+4
4 mult_errors 2.98e-3 -2453. 4913. 4913. 4923. 737952658. 1.16e9 4.73e-2
Forecast on full data set with additive model
additive_model <- pce |>
model(additive = ETS(value ~ error("A") + trend("A") + season("A")))additive_model |>
forecast(h = 24) |>
autoplot(
linewidth = 1.25
) +
autolayer(
pce,
level = NULL,
linewidth = 1.25,
show_gap = FALSE
) +
labs(
title = "Forecasted PCE 24 months ahead using additive ETS"
) +
ggthemes::theme_economist()