Register for API keys

Within this assignment I will be utilizing data from the Bureau of Labor Statistics Public Data API and the Federal Reserve Board. In order to obtain the data I require I will sign up for API keys from both of these organizations. https://fred.stlouisfed.org/docs/api/api_key.html https://data.bls.gov/registrationEngine/

API KEYS FRED: ab3dc205dd28ac1d8daf9da1678c6612 BLS : 6f4c57a4d5264889adb615c97905e8cc

fed<- "ab3dc205dd28ac1d8daf9da1678c6612"
bls<- "6f4c57a4d5264889adb615c97905e8cc"

Read Data

#CPI
id <- 'APU0000702111'
url <- glue("https://api.bls.gov/publicAPI/v2/timeseries/data/{id}?registrationkey={bls}&startyear=2004&endyear=2023")
cpiraw <- GET(url)
cpirawt <- content(cpiraw, "text", encoding = "UTF-8")
cpijson <- fromJSON(cpirawt, flatten = TRUE)
cpi1 <- as.data.frame(cpijson[[4]])$series.data[[1]]
cpi1 <- cpi1[-4]

#This data stops at 2003 and we need 25 years
url2 <- glue("https://api.bls.gov/publicAPI/v2/timeseries/data/{id}?registrationkey={bls}&startyear=1998&endyear=2003")
cpiraw2 <- GET(url2)
cpirawt2 <- content(cpiraw2, "text", encoding = "UTF-8")
cpijson2 <- fromJSON(cpirawt2, flatten = TRUE)
cpi2 <- as.data.frame(cpijson2[[4]])$series.data[[1]]

# Combining our CPI data
cpi <- rbind(cpi1, cpi2)

# Converting dates from BLS format
#cpi$date <- str_c(cpi$periodName," ", cpi$year)
#cpi$date <- myd(cpi$date, truncated=1)

# cast types
#cpi$cpi <- as.double(cpi$value)
#cpi <- cpi[, c("date", "cpi")]
#Federal Funds Rate
url <- glue("https://api.stlouisfed.org/fred/series/observations?series_id=FEDFUNDS&api_key={fed}&file_type=json&frequency=m&observation_start=1998-01-01")
fredraw <- GET(url)
fredrawt <- content(fredraw, "text", encoding = "UTF-8")
fredjson <- json <- fromJSON(fredrawt, flatten = TRUE)
fred <- fredjson$observations
#Unemployment
#second key had to be gotten due to attempts
bls<-"58ad2df26ad741cab30a10633f53e719"
bls_set_key(bls) 
series_id = "LNS14000000"

un1<-get_series_table(
  series_id,
  api_key = bls,
  start_year = 2014,
  end_year = 2023,
  year_limit = NULL,
  parse_values = TRUE
)

un2<-get_series_table(
  series_id,
  api_key = bls,
  start_year = 2004,
  end_year = 2013,
  year_limit = NULL,
  parse_values = TRUE
)

un3<-get_series_table(
  series_id,
  api_key = bls,
  start_year = 1998,
  end_year = 2003,
  year_limit = NULL,
  parse_values = TRUE
)

unn <- rbind(un1,un2)
unemployment <- rbind(unn,un3)
head(cpi)
##   year period periodName value footnotes
## 1 2023    M08     August 1.970      NULL
## 2 2023    M07       July 1.980      NULL
## 3 2023    M06       June 1.937      NULL
## 4 2023    M05        May 1.951      NULL
## 5 2023    M04      April 1.989      NULL
## 6 2023    M03      March 1.936      NULL
head(unemployment)
## # A tibble: 6 × 4
##    year period periodName value
##   <int> <chr>  <chr>      <dbl>
## 1  2023 M08    August       3.8
## 2  2023 M07    July         3.5
## 3  2023 M06    June         3.6
## 4  2023 M05    May          3.7
## 5  2023 M04    April        3.4
## 6  2023 M03    March        3.5
head(fred)
##   realtime_start realtime_end       date value
## 1     2023-09-24   2023-09-24 1998-01-01  5.56
## 2     2023-09-24   2023-09-24 1998-02-01  5.51
## 3     2023-09-24   2023-09-24 1998-03-01  5.49
## 4     2023-09-24   2023-09-24 1998-04-01  5.45
## 5     2023-09-24   2023-09-24 1998-05-01  5.49
## 6     2023-09-24   2023-09-24 1998-06-01  5.56

Clean Data

Since we know that CPI and Unemployment are from the same site we cna cleanse them the same and add them to the same data frame

# Directly assigning a new name to the column
unemployment$`unemployment rate` <- unemployment$value
unemployment <- unemployment[-4] 
cpi$`cpi rate` <- cpi$value
cpi <- cpi[-4] 
cpi <- cpi[, !(names(cpi) %in% c("period","footnotes"))]
fred$year <- year(fred$date)
fred$periodName <- month.name[month(fred$date)]
fred$`federal funds rate` <- fred$value
fred <- fred[, !(names(fred) %in% c("realtime_start","value", "realtime_end", "date"))]
df <- cpi
df$`unemployment rate` <- unemployment$`unemployment rate`
df <- merge(df, fred, by = c("year", "periodName"))

Data Visualization

# Create a new column "Month-Year" by combining "year" and "periodName"
df$MonthYear <- paste(df$year, df$periodName, sep = "-")
df$MonthYear <- as.yearmon(df$MonthYear, format = "%Y-%B")
write.csv(df, file = "df.csv", row.names = FALSE)
library(ggplot2)

# Assuming your DataFrame is called df and the "MonthYear" column is of class 'yearmon'
# If you haven't already converted it, you can do it with:
# df$MonthYear <- as.yearmon(df$MonthYear, format = "%Y-%B")

# Create a line plot for CPI
plot_cpi <- ggplot(df, aes(x = MonthYear, y = `cpi rate`)) +
  geom_line(color = "blue") +
  labs(title = "CPI Over Time", y = "CPI Rate")

# Create a line plot for Unemployment Rate
plot_unemployment <- ggplot(df, aes(x = MonthYear, y = `unemployment rate`)) +
  geom_line(color = "red") +
  labs(title = "Unemployment Rate Over Time", y = "Unemployment Rate")

# Create a line plot for Federal Funds Rate
plot_fed_funds <- ggplot(df, aes(x = MonthYear, y = `federal funds rate`)) +
  geom_line(color = "green") +
  labs(title = "Federal Funds Rate Over Time", y = "Federal Funds Rate")

# Arrange the plots in a grid
library(cowplot)

economy_plot <- plot_grid(plot_cpi, plot_unemployment, plot_fed_funds,
                          ncol = 1, align = "v")

# Show the combined plot
economy_plot

Conclusion

If you look at this illustration it suggests that If CPI remains steady or stable, then The Federal Reserve has been controlling inflation and has policy in place to tackle the phenomenon. When we look at the Unemployment rate overtime, we want to see that it is also low suggesting that the Federal Reserve is also taking control of that5 scenario. Last the Funds Rate is the tool the Federal Reserve utilizes to control these parameters so we want to see where this has direct correlation with unemployment and CPI. Overall The data shows that the Federal Reserve has been controlling the economy with policy besides in the years of JAN2020. This is significant because this would be a black swan year where COVID-19 would create a pandemic.This is overall alarming however because if we look at the funds rate currently. It means that there is a serious wave of layoffs soon to come.

#Graphic not knitted well so will be displayed below
knitr::include_graphics('image.PNG')