knitr::opts_chunk$set(echo = FALSE, warning = FALSE,message = FALSE)
library(tidyverse)

library(lubridate)

library(xts)
theme_set(theme_bw())
df = read_csv(paste0(file.path(Sys.getenv("USERPROFILE"), fsep = "\\"),
                     "\\OneDrive - Bank Of Israel\\Data\\BIS\\",
                     "WEBSTATS_CBPOL_D_DATAFLOW_csv_row.csv"),skip = 1)

df = df %>% 
  rename_all(.funs = ~str_remove(.,"^[A-Z]{2}:")) %>% 
  rename_all(.funs = ~str_replace_all(.,"\\s","_")) %>% 
  rename_all(tolower) %>% 
  rename(date = reference_area)

df = df %>% 
  slice(-1) %>% 
  pivot_longer(-date, names_to = "country",values_to = "rate") %>% 
  filter(complete.cases(.)) %>% 
  mutate(rate = as.numeric(rate))


avg_df = df %>% 
  group_by(date = as.yearmon(date), country) %>% 
  summarise(avg_rate = mean(rate, na.rm = TRUE),.groups = "drop")
avg_df %>% 
  filter(country == "israel") %>% 
  ggplot(aes(x = date, y = avg_rate)) + 
  geom_line() + 
  scale_y_continuous(labels = scales::percent_format(scale = 1,
                                                     accuracy = 1)) + 
  labs(x = "", y = "", title = "Israel average policy rate") + 
  theme(plot.title = element_text(hjust = 0.5))
avg_df %>% 
  filter(country %in% c("israel","united_states")) %>% 
  ggplot(aes(x = date, y = avg_rate, color = country)) + 
  geom_line() + 
  scale_color_manual(values = c("israel" = "black",
                                "united_states" = "lightblue")) + 
  labs(x = "", y = "", title = "Israel vs USA average policy rate") + 
  theme(plot.title = element_text(hjust = 0.5),
        legend.position = "bottom", legend.title = element_blank())
avg_df %>% 
  filter(date >= as.yearmon("Jan 2000")) %>% 
  filter(country %in% c("israel","united_states",
                        "united_kingdom","canada",
                        "japan","switzerland")) %>% 
    ggplot(aes(x = date, y = avg_rate, color = country)) + 
  geom_line() + 
  labs(x = "", y = "", title = "Average policy rates") + 
  theme(plot.title = element_text(hjust = 0.5),
        legend.position = "bottom", legend.title = element_blank())


MichaelGurkov/LearningMaterials documentation built on July 9, 2022, 5:17 p.m.