Instructions The Federal Reserve’s mandate from Congress is to control inflation and to maintain low unemployment. These seem to be contradictory objectives.

For this story you will need to source the following data for the last 25 years;

The Consumer Price Index (CPI) (Bureau of Labor Statistics)

The FED Funds Rate (FRED) (Federal Reserve Board)

Unemployment Rate (Bureau of Labor Statistics) Your Data Visualizations should be designed to answer the question “Has the FED been able to fulfill the mandate given to it by Congress?”

Packages:

library(httr)
library(tidyverse)
library(glue)
library(devtools)
library(fredr)
library(lubridate)

Specifically for Fred

devtools::install_github("sboysel/fredr")

Fred API Pull (Fed Funds Rate)

FRED_API <- Sys.getenv("FRED_API_KEY")
api_key <- fredr_set_key(FRED_API)
fed_funds_data <- fredr(series_id = "DFF", frequency = "m")

BLS API Pull ()

Specifically for BLS Note: BLS only lets you pull

#library(devtools)
#install_github("mikeasilva/blsAPI")
bls_API <- Sys.getenv("BLS_API_KEY")
blsURL <- "https://api.bls.gov/publicAPI/v2/timeseries/data/"

Payload Body - 1977-1996

payload_96 <- glue('{
"seriesid":["CUUR0000SA0", "LNS14000000"],
"startyear":"1977",
"endyear":"1996",
"registrationkey":"{{bls_API}}"
}', .open="{{", .close="}}")

response_96 <- POST(blsURL,
                 body = payload_96,
                 content_type("application/json"),
                 encode = "json")

x_96 <- content(response_96, "text") %>% jsonlite::fromJSON()
## No encoding supplied: defaulting to UTF-8.
CPI_96 <- x_96$Results$series$data[[1]] %>% as_tibble()
UNEMP_96 <- x_96$Results$series$data[[2]] %>% as_tibble()

Series Title : All items in U.S. city average, all urban consumers, not seasonally adjusted Series ID : CUUR0000SA0 Seasonality : Not Seasonally Adjusted Survey Name : Consumer Price Index for All Urban Consumers (CPI-U) Measure Data Type : All items Area : U.S. city average Item : All items

Series Id: LNS14000000 Seasonally Adjusted Series title: (Seas) Unemployment Rate Labor force status: Unemployment rate Type of data: Percent or rate Age: 16 years and over

1997 - 2016

payload_16 <- glue('{
"seriesid":["CUUR0000SA0", "LNS14000000"],
"startyear":"1997",
"endyear":"2017",
"registrationkey":"{{bls_API}}"
}', .open="{{", .close="}}")

response_16 <- POST(blsURL,
                 body = payload_16,
                 content_type("application/json"),
                 encode = "json")

x_16 <- content(response_16, "text") %>% jsonlite::fromJSON()
## No encoding supplied: defaulting to UTF-8.
CPI_16 <- x_16$Results$series$data[[1]] %>% as_tibble()

UNEMP_16 <- x_16$Results$series$data[[2]] %>% as_tibble()

2017 - 2024

payload_24 <- glue('{
"seriesid":["CUUR0000SA0", "LNS14000000"],
"startyear":"2017",
"endyear":"2024",
"registrationkey":"{{bls_API}}"
}', .open="{{", .close="}}")

response_24 <- POST(blsURL,
                 body = payload_24,
                 content_type("application/json"),
                 encode = "json")

x_24 <- content(response_24, "text") %>% jsonlite::fromJSON()
## No encoding supplied: defaulting to UTF-8.
CPI_24 <- x_24$Results$series$data[[1]] %>% as_tibble()

UNEMP_24 <- x_24$Results$series$data[[2]] %>% as_tibble()

Clean data frames and combine

library(dplyr)

CPI_96_cl <- CPI_96 %>%
  select(1:4)

CPI_16_cl <- CPI_16 %>% 
    select(1:4)
CPI_24_cl <- CPI_24 %>% 
    select(1:3,5)

#combine full df together

CPI_full <- bind_rows(CPI_96_cl, CPI_16_cl, CPI_24_cl)
CPI_full$value <- as.numeric(CPI_full$value)
CPI_full_cl <- CPI_full %>%
    mutate(date = as.Date(paste(year, substr(period, 2, 3), "01", sep = "-"))) %>%
    select(-year, -period)
UNEMP_96_cl <- UNEMP_96 %>%
  select(1:4)

UNEMP_16_cl <- UNEMP_16 %>% 
    select(1:4)
UNEMP_24_cl <- UNEMP_24 %>% 
    select(1:3,5)


#combine full df together

UNEMP_full <- bind_rows(UNEMP_96_cl, UNEMP_16_cl, UNEMP_24_cl)
UNEMP_full_cl <- UNEMP_full %>%
    mutate(date = as.Date(paste(year, substr(period, 2, 3), "01", sep = "-"))) %>%
    select(-year, -period)

Renaming

CPI_full_cl <- CPI_full_cl %>% 
    rename(CPI = value) %>% 
    select(2,3)

UNEMP_full_cl <- UNEMP_full_cl %>% 
    rename(UNEMP = value) %>% 
    select(2,3)

fed_funds_data_cl <-fed_funds_data %>% 
    filter(date >= as.Date("1977-01-01") & date <= as.Date("2024-08-01")) %>% 
    select(1,3) %>% 
    rename(FFR = value)

Fred R for CPI data

inflation_data_fred <- fredr(
  series_id = "FPCPITOTLZGUSA",
  observation_start = as.Date("1977-01-01"),
  observation_end = as.Date("2024-09-22")) %>% 
  select(date, value) %>% 
  mutate(year = year(date)) %>% 
  select(year, value)

Make yearly vars from monthly

UNEMP_1 <- UNEMP_full_cl %>%
  mutate(date = as.Date(date)) %>% 
  mutate(UNEMP = as.numeric(UNEMP))

# Extract the year from the date column
UNEMP_2 <- UNEMP_1 %>%
  mutate(year = year(date))

# Calculate the annual average unemployment rate
UNEMP_yearly <- UNEMP_2 %>%
  group_by(year) %>%
  summarise(annual_avg_unemployment = mean(UNEMP, na.rm = TRUE)) %>% 
  filter(year!= 2024)

# FFR yearly average
fed_funds_1 <- fed_funds_data_cl %>% 
  mutate(date = as.Date(date)) %>% 
  mutate(FFR = as.numeric(FFR))
FFR_1 <- fed_funds_data_cl %>% 
  mutate(year = year(date))

FFR_yearly <- FFR_1 %>%
  group_by(year) %>%
  summarise(annual_avg_FFR = mean(FFR, na.rm = TRUE)) %>% 
  filter(year!= 2024)
economic_ind <- inflation_data_fred %>% 
    full_join(FFR_yearly, by = "year") %>% 
    full_join(UNEMP_yearly, by = "year") %>%
    rename(inflation_rate = value)
economic_ind <- economic_ind %>%
  filter(!is.na(annual_avg_FFR) & !is.na(annual_avg_unemployment) & !is.na(inflation_rate))

plot(economic_ind$year, economic_ind$annual_avg_FFR, type = "l", col = "blue", 
     ylim = range(c(economic_ind$annual_avg_FFR, economic_ind$annual_avg_unemployment, economic_ind$inflation_rate), na.rm = TRUE),
     xlab = "Year", ylab = "Rate", main = "Has the FED been able to fulfill the mandate given to it by Congress?")


lines(economic_ind$year, economic_ind$annual_avg_unemployment, col = "red")
lines(economic_ind$year, economic_ind$inflation_rate, col = "green")

legend("topright", legend = c("FFR", "Unemp", "Inflation"), col = c("blue", "red", "green"), lty = 1, bty = "n")