1. Stationarity Analysis

fredr_set_key("25a2f276fc2718f836f19918a0e31d4a") 
# 1. Download Data
cpi_data <- fredr(series_id = "CPIAUCSL", frequency = "m")
housing_data <- fredr(series_id = "HOUST", frequency = "m")
unrate_data <- fredr(series_id = "UNRATE", frequency = "m")

# 2. Combine and Convert to tsibble (Time Series Tibble)
# Filtering from 2000 to present to make charts readable
my_series <- bind_rows(
  cpi_data %>% mutate(Title = "CPI (Inflation)"),
  housing_data %>% mutate(Title = "Housing Starts"),
  unrate_data %>% mutate(Title = "Unemployment Rate")
) %>%
  mutate(Month = yearmonth(date)) %>%
  as_tsibble(index = Month, key = Title) %>%
  filter(year(Month) >= 2000)

# Preview data
head(my_series)
## # A tsibble: 6 x 7 [1M]
## # Key:       Title [1]
##   date       series_id value realtime_start realtime_end Title             Month
##   <date>     <chr>     <dbl> <date>         <date>       <chr>             <mth>
## 1 2000-01-01 CPIAUCSL   169. 2026-02-13     2026-02-13   CPI (Inflatio… 2000 1月
## 2 2000-02-01 CPIAUCSL   170  2026-02-13     2026-02-13   CPI (Inflatio… 2000 2月
## 3 2000-03-01 CPIAUCSL   171  2026-02-13     2026-02-13   CPI (Inflatio… 2000 3月
## 4 2000-04-01 CPIAUCSL   171. 2026-02-13     2026-02-13   CPI (Inflatio… 2000 4月
## 5 2000-05-01 CPIAUCSL   171. 2026-02-13     2026-02-13   CPI (Inflatio… 2000 5月
## 6 2000-06-01 CPIAUCSL   172. 2026-02-13     2026-02-13   CPI (Inflatio… 2000 6月
my_series %>%
  autoplot(value) +
  facet_wrap(~Title, scales = "free_y", ncol = 1) +
  labs(title = "Time Series Plots (2000 - Present)", y = "Value") +
  theme_minimal()

# We will use the unitroot_kpss features from the 'feasts' package
my_series %>%
  features(value, unitroot_kpss) %>%
  knitr::kable(caption = "KPSS Test Results")
KPSS Test Results
Title kpss_stat kpss_pvalue
CPI (Inflation) 4.9305647 0.01
Housing Starts 1.0713497 0.01
Unemployment Rate 0.8197631 0.01

2. ACF and PACF Analysis

Since the raw data is non-stationary, we examine the differenced data. * Note: We use fill_gaps() to ensure the timeline is perfect. We do not remove the NAs created by differencing, as removing rows creates time gaps that break the plot.

# -----------------------------------------------------------------------------
# PLOT 1: CPI (Inflation)
# -----------------------------------------------------------------------------
my_series %>%
  fill_gaps() %>%                                  # 1. Fix missing months (if any)
  filter(Title == "CPI (Inflation)") %>%           # 2. Select just CPI
  gg_tsdisplay(difference(value), plot_type = "partial", lag_max = 36) + 
  labs(title = "CPI (Differenced): ACF & PACF", y = "Change in CPI")

# -----------------------------------------------------------------------------
# PLOT 2: Housing Starts
# -----------------------------------------------------------------------------
my_series %>%
  fill_gaps() %>%
  filter(Title == "Housing Starts") %>%
  gg_tsdisplay(difference(value), plot_type = "partial", lag_max = 36) +
  labs(title = "Housing Starts (Differenced): ACF & PACF", y = "Change in Housing")

# -----------------------------------------------------------------------------
# PLOT 3: Unemployment Rate
# -----------------------------------------------------------------------------
my_series %>%
  fill_gaps() %>%
  filter(Title == "Unemployment Rate") %>%
  gg_tsdisplay(difference(value), plot_type = "partial", lag_max = 36) +
  labs(title = "Unemployment Rate (Differenced): ACF & PACF", y = "Change in Unemployment")

3. Decomposition

# 1. Fit the STL model
# We handle missing data first, or the model will fail ("null_mdl" error)
dcmp <- my_series %>%
  fill_gaps() %>%                              # Make missing months explicit NAs
  tidyr::fill(value, .direction = "down") %>%  # Fill NAs with the previous value
  model(stl = STL(value ~ season(window = "periodic"))) %>%
  components()

# 2. Plot the decomposition
dcmp %>%
  autoplot() +
  labs(title = "STL Decomposition of Time Series")

references

Hyndman, Rob. 2025. “Fpp3: Data for”Forecasting: Principles and Practice” (3rd Edition).” https://doi.org/10.32614/CRAN.package.fpp3.

O’Hara-Wild, Mitchell, Rob Hyndman, and Earo Wang. 2024. “Fable: Forecasting Models for Tidy Time Series.” https://doi.org/10.32614/CRAN.package.fable.

———. 2025. “Feasts: Feature Extraction and Statistics for Time Series.” https://doi.org/10.32614/CRAN.package.feasts.

Wang, Earo, Dianne Cook, and Rob J Hyndman. 2020. “A New Tidy Data Structure to Support Exploration and Modeling of Temporal Data” 29: 466–78. https://doi.org/10.1080/10618600.2019.1695624.