## ----setup, include = FALSE---------------------------------------------------
library(fred)
key_available <- nzchar(Sys.getenv("FRED_API_KEY"))
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 4.5,
  eval = key_available
)

## -----------------------------------------------------------------------------
library(fred)

## -----------------------------------------------------------------------------
inflation_now <- fred_series(
  c("CPIAUCSL", "CPILFESL"),
  from = "2018-01-01",
  transform = "yoy_pct",
  format = "wide"
)
plot(inflation_now, ylab = "% YoY",
     main = "Headline vs core CPI, latest vintage")

## -----------------------------------------------------------------------------
core_first <- fred_first_release(
  "CPILFESL",
  from = "2018-01-01",
  units = "pc1"   # YoY percent change
)
core_latest <- fred_series(
  "CPILFESL",
  from = "2018-01-01",
  transform = "yoy_pct"
)

# Plot both on one chart
plot(core_first$date, core_first$value, type = "l",
     xlab = "", ylab = "% YoY",
     main = "Core CPI: first release vs latest vintage",
     ylim = range(c(core_first$value, core_latest$value), na.rm = TRUE))
graphics::lines(core_latest$date, core_latest$value, col = "tomato")
graphics::legend("topleft",
                 legend = c("First release", "Latest vintage"),
                 col = c("black", "tomato"), lty = 1, bty = "n")

## -----------------------------------------------------------------------------
sep_meetings <- fred_fomc_dates(year = 2024, sep_only = TRUE)
sep_meetings

## -----------------------------------------------------------------------------
panel <- fred_real_time_panel(
  "CPILFESL",
  vintages = sep_meetings$date,
  from = "2022-01-01"
)
head(panel)

## -----------------------------------------------------------------------------
panel_wide <- stats::reshape(
  panel[, c("date", "value", "realtime_start")],
  idvar = "date", timevar = "realtime_start",
  direction = "wide"
)
names(panel_wide) <- sub("^value\\.", "v", names(panel_wide))
panel_wide <- panel_wide[order(panel_wide$date), ]

cols <- c("#1F77B4", "#FF7F0E", "#2CA02C", "#D62728")
plot(panel_wide$date, panel_wide[[2]], type = "l", col = cols[1],
     ylim = range(unlist(panel_wide[, -1]), na.rm = TRUE),
     xlab = "", ylab = "Core CPI level",
     main = "Core CPI as seen at four 2024 SEP meetings")
for (i in 3:ncol(panel_wide)) {
  graphics::lines(panel_wide$date, panel_wide[[i]], col = cols[i - 1L])
}
graphics::legend("topleft",
                 legend = format(sep_meetings$date, "%b %Y"),
                 col = cols, lty = 1, bty = "n", cex = 0.8)

## -----------------------------------------------------------------------------
rev <- fred_vintage_revisions("CPILFESL", from = "2020-01-01")
head(rev)
summary(rev$revision_total_pct)

## ----eval = TRUE--------------------------------------------------------------
fred_cite_series(
  "CPILFESL",
  vintage_date = "2024-12-18",
  format = "bibtex"
)

