library(devtools)
## Loading required package: usethis
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)
library(rvest)
##
## Attaching package: 'rvest'
##
## The following object is masked from 'package:readr':
##
## guess_encoding
library(blsAPI)
library(devtools)
library(httr2)
library(httr)
library(rvest)
library(dplyr)
library(plotly)
##
## Attaching package: 'plotly'
##
## The following object is masked from 'package:httr':
##
## config
##
## The following object is masked from 'package:ggplot2':
##
## last_plot
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following object is masked from 'package:graphics':
##
## layout
library(fredr)
Using the FRED API to get the Federal Funds Rate
fredr_set_key(api_key)
series_id <- "FEDFUNDS"
observation_start <- as.Date("1970-01-01")
fed_funds_data <- fredr(series_id, observation_start = observation_start)
fed_funds_data$year <- as.numeric(format(fed_funds_data$date, "%Y"))
fed_funds_by_year <- aggregate(value ~ year, data = fed_funds_data, FUN = mean)
df3 <- fed_funds_by_year %>%
rename(
Year = year,
"Federal Funds Rate" = value
)
head(df3)
## Year Federal Funds Rate
## 1 1970 7.183333
## 2 1971 4.662500
## 3 1972 4.434167
## 4 1973 8.727500
## 5 1974 10.502500
## 6 1975 5.824167
Getting the CPI
# Getting the CPI
cpi_link <- "https://www.minneapolisfed.org/about-us/monetary-policy/inflation-calculator/consumer-price-index-1913-"
cpi_page <- read_html(cpi_link)
CPI <- cpi_page %>%
html_nodes("td:nth-child(2) div") %>%
html_text() %>%
as.numeric
CPI_year <- cpi_page %>%
html_nodes("td:nth-child(1) div") %>%
html_text()%>%
as.numeric()
## Warning in cpi_page %>% html_nodes("td:nth-child(1) div") %>% html_text() %>% :
## NAs introduced by coercion
df <- data.frame("CPI" = CPI , "Year" = CPI_year)
years <- 2013:2024
for (i in 1:11) {
df[100+i,2] = years[i]
}
head(df)
## CPI Year
## 1 9.9 1913
## 2 10.0 1914
## 3 10.1 1915
## 4 10.9 1916
## 5 12.8 1917
## 6 15.0 1918
Getting the Unemployment Rate
UE_link <- "https://www.investopedia.com/historical-us-unemployment-rate-by-year-7495494"
UE_page <- read_html(UE_link)
UE <- UE_page %>%
html_nodes("tr+ tr td:nth-child(2)") %>%
html_text() %>%
str_remove("%") %>%
as.numeric()
UE_year <- UE_page %>%
html_nodes("tr+ tr td:nth-child(1)") %>%
html_text()%>%
as.numeric()
df2 <- data.frame("Unemployment" = UE , "Year" = UE_year)
head(df2)
## Unemployment Year
## 1 3.2 1929
## 2 8.7 1930
## 3 15.9 1931
## 4 23.6 1932
## 5 24.9 1933
## 6 21.7 1934
Merging the dataframes
df4 <- merge(df,df2, by = "Year")
finaldf <- merge(df4,df3, by = "Year")
finaldf_25_years <- finaldf %>%
filter(Year %in% 1997:2022)
cpi_diff <- c(NA, diff(finaldf_25_years$CPI) / lag(finaldf_25_years$CPI)) * 100
## Warning in diff(finaldf_25_years$CPI)/lag(finaldf_25_years$CPI): longer object
## length is not a multiple of shorter object length
finaldf_25_years$CPI_Percentage_Change <- cpi_diff[-length(cpi_diff)]
unemployment_diff <- c(NA, diff(finaldf_25_years$Unemployment) / lag(finaldf_25_years$Unemployment)) * 100
## Warning in
## diff(finaldf_25_years$Unemployment)/lag(finaldf_25_years$Unemployment): longer
## object length is not a multiple of shorter object length
finaldf_25_years$Unemployment_Percentage_Change <- unemployment_diff[-length(unemployment_diff)]
federal_funds_diff <- c(NA, diff(finaldf_25_years$`Federal Funds Rate`) / lag(finaldf_25_years$`Federal Funds Rate`)) * 100
## Warning in diff(finaldf_25_years$`Federal Funds
## Rate`)/lag(finaldf_25_years$`Federal Funds Rate`): longer object length is not
## a multiple of shorter object length
finaldf_25_years$Federal_Funds_Rate_Percentage_Change <- federal_funds_diff[-length(federal_funds_diff)]
head(finaldf_25_years)
## Year CPI Unemployment Federal Funds Rate CPI_Percentage_Change
## 1 1997 160.5 4.7 5.460000 NA
## 2 1998 163.0 4.4 5.353333 NA
## 3 1999 166.6 4.0 4.970000 2.242991
## 4 2000 172.2 3.9 6.235833 3.435583
## 5 2001 177.1 5.7 3.887500 2.941176
## 6 2002 179.9 6.0 1.666667 1.626016
## Unemployment_Percentage_Change Federal_Funds_Rate_Percentage_Change
## 1 NA NA
## 2 NA NA
## 3 -8.510638 -7.020757
## 4 -2.272727 23.645704
## 5 45.000000 -47.250168
## 6 7.692308 -35.614059
#Visualizations
library(lattice)
xyplot(CPI ~ Year, data = finaldf_25_years, type = "l",
xlab = "Year", ylab = "CPI",
main = "Consumer Price Index (CPI) over the Years")
xyplot(Unemployment ~ Year, data = finaldf_25_years, type = "l",
xlab = "Year", ylab = "Unemployment Rate",
main = "Unemployment Rate over the Years")
xyplot(`Federal Funds Rate` ~ Year, data = finaldf_25_years, type = "l",
xlab = "Year", ylab = "Federal Funds Rate",
main = "Federal Funds Rate over the Years")
library(lattice)
trellis.par.set(
layout.widths = list(right.padding = 10, left.padding = 10), # Adjust padding
layout.heights = list(top.padding = 6, bottom.padding = 6) # Adjust padding
)
xyplot(CPI_Percentage_Change + Unemployment_Percentage_Change ~ Year, data = finaldf_25_years,
type = "l",
xlab = "Year", ylab = "Percentage Change",
main = "Comparison of CPI and Unemployment Rate Percentage Change over Time",
col = c("blue", "red"),
auto.key = NULL,
scales = list(x = list(rot = 45), y = list(relation = "free"))
)
# Calculate the correlation coefficient after removing missing values
correlation <- cor(na.omit(finaldf_25_years$CPI_Percentage_Change), na.omit(finaldf_25_years$Unemployment_Percentage_Change))
# Print the correlation coefficient
print(correlation)
## [1] -0.2689623
“Has the FED been able to fulfill the mandate given to it by Congress?
Based on this although the Federal Funds Rate is a key economic instrument, its impact on controlling inflation and unemployment seems to be modest .