Module 3 Discussion
#to clean the whole environment
remove(list=ls())
#Set up all of the libraries needed
library(fpp3)
## Registered S3 method overwritten by 'tsibble':
## method from
## as_tibble.grouped_df dplyr
## ── Attaching packages ──────────────────────────────────────────── fpp3 1.0.2 ──
## ✔ tibble 3.3.0 ✔ tsibble 1.1.6
## ✔ dplyr 1.1.4 ✔ tsibbledata 0.4.1
## ✔ tidyr 1.3.1 ✔ feasts 0.4.2
## ✔ lubridate 1.9.4 ✔ fable 0.4.1
## ✔ ggplot2 3.5.2
## ── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
## ✖ lubridate::date() masks base::date()
## ✖ dplyr::filter() masks stats::filter()
## ✖ tsibble::intersect() masks base::intersect()
## ✖ tsibble::interval() masks lubridate::interval()
## ✖ dplyr::lag() masks stats::lag()
## ✖ tsibble::setdiff() masks base::setdiff()
## ✖ tsibble::union() masks base::union()
library(fredr)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0 ✔ readr 2.1.5
## ✔ purrr 1.1.0 ✔ stringr 1.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ tsibble::interval() masks lubridate::interval()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(patchwork)
library(knitr)
library(kableExtra)
##
## Attaching package: 'kableExtra'
##
## The following object is masked from 'package:dplyr':
##
## group_rows
library(writexl)
library(dplyr)
library(feasts)
#Set up my FRED Key
#430508d0ad1df25ecddc34d5581dafc8
fredr_set_key("430508d0ad1df25ecddc34d5581dafc8")
#Pulling out the data and setting is as time series data
#A CPI Data
cpi <- fredr("CPIAUCSL",
observation_start = as.Date("2000-01-01"),
observation_end = as.Date("2025-08-01")
) |>
mutate(Month = yearmonth(date), value) |>
as_tsibble(index = Month)
#checking the data
autoplot(cpi, value)

any(is.na(cpi))
## [1] FALSE
is_tibble(cpi)
## [1] TRUE
##ACF and PCF
ACF(cpi) |>
autoplot()
## Response variable not specified, automatically selected `var = date`

#Strong trend, not seasonal
PACF(cpi) |>
autoplot()
## Response variable not specified, automatically selected `var = date`

#AR(1) model could effectively model the data.
cpi |>
model(
classical_decomposition(value, type = "additive")
) |>
components() |>
autoplot() +
labs(title = "Consumer Price Index")
## Warning: Removed 6 rows containing missing values or values outside the scale range
## (`geom_line()`).

#B 30-Year Mortgage Rate Data
#MORTGAGE30US
mor30 <- fredr("MORTGAGE30US",
observation_start = as.Date("1971-04-02"),
observation_end = as.Date("2025-09-18")
) |>
mutate(Week = yearweek(date), value) |>
as_tsibble(index = Week)
#checking the data
autoplot(mor30, value)

any(is.na(mor30))
## [1] FALSE
is_tibble(mor30)
## [1] TRUE
##ACF and PCF
ACF(mor30) |>
autoplot()
## Response variable not specified, automatically selected `var = date`

#Strong trend, not seasonal.
PACF(mor30) |>
autoplot()
## Response variable not specified, automatically selected `var = date`

#AR(1) model could effectively model the data.
#Classical Decomposition
mor30 |>
model(
classical_decomposition(value, type = "additive")
) |>
components() |>
autoplot() +
labs(title = "30-Year Mortgage Rate")
## Warning: Removed 26 rows containing missing values or values outside the scale range
## (`geom_line()`).

#C Cost of a Dozen Eggs Data
#APU0000708111
egg <- fredr("APU0000708111",
observation_start = as.Date("1980-01-01"),
observation_end = as.Date("2025-08-01")
) |>
mutate(Month = yearmonth(date), value) |>
as_tsibble(index = Month)
#checking the data
autoplot(egg, value)

any(is.na(egg))
## [1] FALSE
is_tibble(egg)
## [1] TRUE
##ACF and PCF
ACF(egg) |>
autoplot()
## Response variable not specified, automatically selected `var = date`

#Strong trend, not seasonal.
PACF(egg) |>
autoplot()
## Response variable not specified, automatically selected `var = date`

#AR(1) model could effectively model the data.
#Classical Decomposition
egg |>
model(
classical_decomposition(value, type = "additive")
) |>
components() |>
autoplot() +
labs(title = "US Egg Cost")
## Warning: Removed 6 rows containing missing values or values outside the scale range
## (`geom_line()`).
