The Federal Reserve, plays an important role in the United States economy. Established by Congress, its dual mandate is controlling inflation and making sure unemployment is low.

# libraries I need:

library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.2
library(httr)
## Warning: package 'httr' was built under R version 4.3.2
library(fredr)
## Warning: package 'fredr' was built under R version 4.3.2
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.3.2
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
#my API:

fredr_set_key("49fd726cb26f01e1caf136508b1abe50")

#Series ID of my CPI data & print the data:

cpi_data <- fredr(series_id = "USACPALTT01CTGYM")

print(cpi_data)
## # A tibble: 168 × 5
##    date       series_id           value realtime_start realtime_end
##    <date>     <chr>               <dbl> <date>         <date>      
##  1 2010-01-01 USACPALTT01CTGYM -0.161   2024-03-10     2024-03-10  
##  2 2010-02-01 USACPALTT01CTGYM -0.120   2024-03-10     2024-03-10  
##  3 2010-03-01 USACPALTT01CTGYM -0.0542  2024-03-10     2024-03-10  
##  4 2010-04-01 USACPALTT01CTGYM -0.00107 2024-03-10     2024-03-10  
##  5 2010-05-01 USACPALTT01CTGYM  0.0265  2024-03-10     2024-03-10  
##  6 2010-06-01 USACPALTT01CTGYM  0.0200  2024-03-10     2024-03-10  
##  7 2010-07-01 USACPALTT01CTGYM  0.0530  2024-03-10     2024-03-10  
##  8 2010-08-01 USACPALTT01CTGYM  0.0608  2024-03-10     2024-03-10  
##  9 2010-09-01 USACPALTT01CTGYM  0.107   2024-03-10     2024-03-10  
## 10 2010-10-01 USACPALTT01CTGYM  0.113   2024-03-10     2024-03-10  
## # ℹ 158 more rows
# unemployment data & print: 

unemployment <- fredr(series_id = "U2RATE")

print(unemployment)
## # A tibble: 686 × 5
##    date       series_id value realtime_start realtime_end
##    <date>     <chr>     <dbl> <date>         <date>      
##  1 1967-01-01 U2RATE      1.6 2024-03-10     2024-03-10  
##  2 1967-02-01 U2RATE      1.6 2024-03-10     2024-03-10  
##  3 1967-03-01 U2RATE      1.5 2024-03-10     2024-03-10  
##  4 1967-04-01 U2RATE      1.6 2024-03-10     2024-03-10  
##  5 1967-05-01 U2RATE      1.6 2024-03-10     2024-03-10  
##  6 1967-06-01 U2RATE      1.7 2024-03-10     2024-03-10  
##  7 1967-07-01 U2RATE      1.6 2024-03-10     2024-03-10  
##  8 1967-08-01 U2RATE      1.5 2024-03-10     2024-03-10  
##  9 1967-09-01 U2RATE      1.6 2024-03-10     2024-03-10  
## 10 1967-10-01 U2RATE      1.6 2024-03-10     2024-03-10  
## # ℹ 676 more rows
# Federal funds data $ print: 

fedfunds <- fredr(series_id = "EFFR")

print(fedfunds)
## # A tibble: 6,179 × 5
##    date       series_id value realtime_start realtime_end
##    <date>     <chr>     <dbl> <date>         <date>      
##  1 2000-07-03 EFFR       7.03 2024-03-10     2024-03-10  
##  2 2000-07-04 EFFR      NA    2024-03-10     2024-03-10  
##  3 2000-07-05 EFFR       6.52 2024-03-10     2024-03-10  
##  4 2000-07-06 EFFR       6.51 2024-03-10     2024-03-10  
##  5 2000-07-07 EFFR       6.42 2024-03-10     2024-03-10  
##  6 2000-07-10 EFFR       6.51 2024-03-10     2024-03-10  
##  7 2000-07-11 EFFR       6.39 2024-03-10     2024-03-10  
##  8 2000-07-12 EFFR       6.38 2024-03-10     2024-03-10  
##  9 2000-07-13 EFFR       6.5  2024-03-10     2024-03-10  
## 10 2000-07-14 EFFR       6.51 2024-03-10     2024-03-10  
## # ℹ 6,169 more rows
# Filtering data for the last 10 years & print new datasets:

cpi_data <- cpi_data %>% filter(date >= Sys.Date() - 365 * 10)
fedfunds <- fedfunds %>% filter(date >= Sys.Date() - 365 * 10)
unemployment <- unemployment%>% filter(date >= Sys.Date() - 365 * 10)

print(cpi_data)
## # A tibble: 117 × 5
##    date       series_id         value realtime_start realtime_end
##    <date>     <chr>             <dbl> <date>         <date>      
##  1 2014-04-01 USACPALTT01CTGYM  1.95  2024-03-10     2024-03-10  
##  2 2014-05-01 USACPALTT01CTGYM  2.14  2024-03-10     2024-03-10  
##  3 2014-06-01 USACPALTT01CTGYM  2.09  2024-03-10     2024-03-10  
##  4 2014-07-01 USACPALTT01CTGYM  2.01  2024-03-10     2024-03-10  
##  5 2014-08-01 USACPALTT01CTGYM  1.72  2024-03-10     2024-03-10  
##  6 2014-09-01 USACPALTT01CTGYM  1.68  2024-03-10     2024-03-10  
##  7 2014-10-01 USACPALTT01CTGYM  1.68  2024-03-10     2024-03-10  
##  8 2014-11-01 USACPALTT01CTGYM  1.34  2024-03-10     2024-03-10  
##  9 2014-12-01 USACPALTT01CTGYM  0.755 2024-03-10     2024-03-10  
## 10 2015-01-01 USACPALTT01CTGYM -0.159 2024-03-10     2024-03-10  
## # ℹ 107 more rows
print(fedfunds)
## # A tibble: 2,606 × 5
##    date       series_id value realtime_start realtime_end
##    <date>     <chr>     <dbl> <date>         <date>      
##  1 2014-03-13 EFFR       0.08 2024-03-10     2024-03-10  
##  2 2014-03-14 EFFR       0.08 2024-03-10     2024-03-10  
##  3 2014-03-17 EFFR       0.08 2024-03-10     2024-03-10  
##  4 2014-03-18 EFFR       0.08 2024-03-10     2024-03-10  
##  5 2014-03-19 EFFR       0.08 2024-03-10     2024-03-10  
##  6 2014-03-20 EFFR       0.08 2024-03-10     2024-03-10  
##  7 2014-03-21 EFFR       0.08 2024-03-10     2024-03-10  
##  8 2014-03-24 EFFR       0.09 2024-03-10     2024-03-10  
##  9 2014-03-25 EFFR       0.09 2024-03-10     2024-03-10  
## 10 2014-03-26 EFFR       0.08 2024-03-10     2024-03-10  
## # ℹ 2,596 more rows
print(unemployment)
## # A tibble: 119 × 5
##    date       series_id value realtime_start realtime_end
##    <date>     <chr>     <dbl> <date>         <date>      
##  1 2014-04-01 U2RATE      3.3 2024-03-10     2024-03-10  
##  2 2014-05-01 U2RATE      3.2 2024-03-10     2024-03-10  
##  3 2014-06-01 U2RATE      3.1 2024-03-10     2024-03-10  
##  4 2014-07-01 U2RATE      3.1 2024-03-10     2024-03-10  
##  5 2014-08-01 U2RATE      3   2024-03-10     2024-03-10  
##  6 2014-09-01 U2RATE      2.9 2024-03-10     2024-03-10  
##  7 2014-10-01 U2RATE      2.8 2024-03-10     2024-03-10  
##  8 2014-11-01 U2RATE      2.9 2024-03-10     2024-03-10  
##  9 2014-12-01 U2RATE      2.8 2024-03-10     2024-03-10  
## 10 2015-01-01 U2RATE      2.7 2024-03-10     2024-03-10  
## # ℹ 109 more rows
# Create plots: 

ggplot(cpi_data, aes(x = date, y = value)) +
  geom_point() +
  labs(title = "CPI over the last 10 years",
       x = "Date",
       y = "CPI",
       col="green")

# CPI is high on the years 2022-2023 and it started decreasing.

ggplot(fedfunds, aes(x = date, y = value)) +
  geom_point() +
  labs(title = "federal funds over the last 10 yeras",
       x = "Date",
       y = "federal funds",
       col="blue")
## Warning: Removed 96 rows containing missing values (`geom_point()`).

# Federal rate high on the year 2018-2020 and from 2022 it increasing dramatically.


ggplot(unemployment, aes(x = date, y = value)) +
  geom_point () +
  labs(title = "unemployment over the last 10 years",
       x = "Date",
       y = "unemployment",
       col="purple")

# From " unemployment scatter plot" Unemployment was high on 2021 and it seems it was caused by COVID-19 pandemic, people were unemployed because a lot of employers closed their businesses, and from "CPI scatter plot" and "federal funds scatter plot" the inflation and federal funding is higher  on 2023 because of the economic damage left by the COVID-19 pandemic.
# Check CPI change on the last 10 years

cpi_data <- arrange(cpi_data, date)
cpi_data <- mutate(cpi_data, inflation_rate = ((value / lag(value)) - 1) * 100)
cpi_data <- cpi_data %>% filter(!is.na(inflation_rate))
print(cpi_data)
## # A tibble: 116 × 6
##    date       series_id         value realtime_start realtime_end inflation_rate
##    <date>     <chr>             <dbl> <date>         <date>                <dbl>
##  1 2014-05-01 USACPALTT01CTG…  2.14   2024-03-10     2024-03-10            9.53 
##  2 2014-06-01 USACPALTT01CTG…  2.09   2024-03-10     2024-03-10           -2.41 
##  3 2014-07-01 USACPALTT01CTG…  2.01   2024-03-10     2024-03-10           -3.64 
##  4 2014-08-01 USACPALTT01CTG…  1.72   2024-03-10     2024-03-10          -14.4  
##  5 2014-09-01 USACPALTT01CTG…  1.68   2024-03-10     2024-03-10           -2.69 
##  6 2014-10-01 USACPALTT01CTG…  1.68   2024-03-10     2024-03-10            0.332
##  7 2014-11-01 USACPALTT01CTG…  1.34   2024-03-10     2024-03-10          -20.6  
##  8 2014-12-01 USACPALTT01CTG…  0.755  2024-03-10     2024-03-10          -43.5  
##  9 2015-01-01 USACPALTT01CTG… -0.159  2024-03-10     2024-03-10         -121.   
## 10 2015-02-01 USACPALTT01CTG… -0.0791 2024-03-10     2024-03-10          -50.2  
## # ℹ 106 more rows
# calculate the inflation:

prchange <- function(data) {
  data %>%
    arrange(date) %>%
    mutate(year = date) %>%
    group_by(year) %>%
    summarise(value = mean(value, na.rm = TRUE)) %>%
    ungroup() %>%
    mutate(pct_change = (value / lag(value) - 1) * 100) %>%
    filter(!is.na(pct_change))
}

cpi_change <- prchange(cpi_data)
fed_change <- prchange(fedfunds)
unemployment_change <- prchange(unemployment)

# gather the changes:

combined_pct_change <- bind_rows(
  cpi_change %>% mutate(type = "CPI"),
  fed_change %>% mutate(type = "FED Funds"),
  unemployment_change %>% mutate(type = "Unemployment")
)

#create a line plot of the change of all three variables:

combined_plot <- ggplot(combined_pct_change, aes(x = year, y = pct_change, color = type)) +
  geom_line() +
  labs(title = "Yearly change on economic indecators",
       x = "Year",
       y = "Percentage of Change",
       color = "Indicator Type") +
  theme_minimal()

print(combined_plot)

#The Fed’s mission is not to keep rates at zero or eliminate inflation completely. By looking at rates of change for all three economic indicators, the Fed has maintained these rates at a baseline right above zero for all years except for the crises periods. This indicates that the Fed is unable to prevent recession events but is able to manage recovery by returning these rates back to baseline.

##conclusion: Balancing the economy with public health crises it a real challenge. The analysis of the graph “Yearly change on economic indecators” underscores the difficulties encountered by the FEDs during 2020 to 2023. This period was characterized by global economic recessions, financial crises, and public health emergencies that we all lived an known as COVID-19 pandemic. Bringing down the unemployment, inflation and federal spending was hard work well done by the FEDs