library(MiscImport)

library(tidyverse)

library(mFilter)

library(zoo)
cpi = read_csv(paste0(Sys.getenv("USERPROFILE"),
                      "\\OneDrive - Bank Of Israel\\Data",
                      "\\CBS\\cpi_monthly.csv"),
               show_col_types = FALSE) %>% 
  pivot_longer(-year,names_to = "month") %>% 
  filter(complete.cases(.)) %>% 
  unite(c("year", "month"),sep = "-", col = "date") %>% 
  mutate(date = as.yearmon(date, format = "%Y-%m")) %>% 
  group_by(date = as.yearqtr(date)) %>% 
  summarise(cpi = mean(value), .groups = "drop") %>% 
  mutate(cpi = cpi / cpi[date == as.yearqtr("2000 Q1")])
broad_money = read_csv(paste0(Sys.getenv("USERPROFILE"),
                                      "\\OneDrive - Bank Of Israel\\Data",
                                      "\\BoI\\Monetary",
                                      "\\broad_money_series.csv"))
broad_money = broad_money %>% 
  mutate(date = as.yearmon(date, format = "%d/%m/%Y"))
# house_price = read_csv(paste0(Sys.getenv("USERPROFILE"),
#                                "\\OneDrive - Bank Of Israel\\Data",
#                                "\\BoI\\HousePrices",
#                                "\\HousePrices_quarterly_",
#                               "1994Q3_2022Q2.csv"))

bis_houseprice = import_bis_selected_property_prices(
  paste0(Sys.getenv("USERPROFILE"),
         "\\OneDrive - Bank Of Israel\\Data\\BIS",
         "\\WS_SPP_csv_col.csv"),
  my_unit_of_measure = "Index, 2010 = 100",
  my_frequency = "Quarterly",
  my_value = "Nominal",
  pivot_to_long = TRUE) %>% 
  select(-breaks, -coverage, -title) %>% 
  filter(complete.cases(.))
bis_houseprice %>% 
  filter(country == "Israel") %>% 
  mutate(property_price = property_price / 
           property_price[date == as.yearqtr("1994 Q2")]) %>% 
  select(date, property_price) %>% 
  inner_join(house_price %>% 
               filter(complete.cases(.)) %>% 
               mutate(date = as.yearqtr(date,
                                        format = "%d/%m/%Y"))%>% 
               mutate(house_price_index = house_price_index / 
                        house_price_index[date == as.yearqtr("1994 Q2")])) %>% 
  pivot_longer(-date) %>% 
  ggplot(aes(date, value, color = name)) + 
  geom_line()
bis_credit = import_bis_total_credit(
  paste0("C:\\Users\\Home",
         "\\OneDrive - Bank Of Israel\\Data\\BIS",
         "\\WEBSTATS_TOTAL_CREDIT_DATAFLOW_csv_col.csv"),
  my_frequency = "Quarterly",
  my_unit_type = "Domestic currency",
  pivot_to_long = TRUE)

# boi_credit = import_boi_credit_df()
bis_credit %>% 
  filter(country == "Israel") %>% 
  filter(lending_sector == "All sectors") %>% 
  filter(borrowing_sector == "Private non-financial sector") %>% 
  select(date, total_credit) %>% 
  filter(complete.cases(.)) %>% 
  mutate(date = as.yearqtr(date, format = "%Y-Q%q")) %>% 
  inner_join(boi_credit %>% 
               filter(borrower %in% c("business_sector","households")) %>% 
               filter(instrument == "all_instruments" | is.na(instrument)) %>%
               filter(category == "total" | is.na(category)) %>% 
               select(date, value) %>% 
               group_by(date) %>% 
               summarise(value = sum(value, na.rm = TRUE),
                         .groups = "drop")) %>% 
  pivot_longer(-date) %>% 
  ggplot(aes(date, value, color = name)) + 
  geom_line()
temp_df = bis_credit %>% 
  filter(country == "Israel") %>% 
  filter(lending_sector == "Banks, domestic") %>% 
  filter(type_of_adjustment == "Unadjusted for breaks") %>% 
  select(date,total_credit) %>% 
  filter(complete.cases(.)) %>% 
  mutate(date = as.yearqtr(date, format = "%Y-Q%q")) %>% 
  inner_join(broad_money %>% 
               group_by(date = as.yearqtr(date)) %>% 
               summarise(broad_money = mean(broad_money) * 10 ^ (-3),
                         .groups = "drop"), by = "date")

temp_df %>% 
  pivot_longer(-date) %>% 
  ggplot(aes(date, value, color = name)) + 
  geom_line()

temp_df %>% 
  mutate(money_credit_ratio = total_credit / broad_money) %>% 
  ggplot(aes(date, money_credit_ratio)) + 
  geom_line()
cycle_df = bis_credit %>% 
  filter(country == "Israel") %>% 
  filter(lending_sector == "All sectors") %>% 
  filter(borrowing_sector == "Private non-financial sector") %>% 
  filter(type_of_adjustment == "Unadjusted for breaks") %>% 
  select(date, total_credit) %>% 
  filter(complete.cases(.)) %>% 
  mutate(date = as.yearqtr(date, format = "%Y-Q%q")) %>% 
  inner_join(bis_houseprice %>% 
               filter(country == "Israel") %>% 
               select(date, property_price)) %>% 
  pivot_longer(-date)


cycle_df = cycle_df %>% 
  inner_join(cpi) %>% 
  mutate(value = value / cpi) %>% 
  select(-cpi)


cycle_df = cycle_df %>% 
  group_by(name) %>% 
  arrange(date) %>% 
  mutate(value = value / value[date == as.yearqtr("2000 Q1")]) %>%
  mutate(value = log(value)) %>% 
  mutate(cycle = cffilter(value,pl = 32 ,pu = 80 )$cycle[,1]) %>%
  ungroup() 
cycle_df %>% 
  group_by(date) %>% 
  summarise(cycle = mean(cycle), .groups = "drop") %>% 
  ggplot(aes(date, cycle)) + 
  geom_line() + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "blue") + 
  scale_x_yearqtr(format = "%Y-Q%q",n = 12) + 
  xlab(NULL) + ylab(NULL) + 
  ggtitle("Financial cycle (log normalized to 2000 Q1)")
  theme() + 
cycle_df %>% 
  ggplot(aes(date, cycle)) + 
  geom_col(aes(fill = name), position = "dodge") + 
  scale_fill_manual(values = c("gray", "lightblue")) + 
  scale_x_yearqtr(n = 12) + 
  xlab(NULL) + ylab(NULL) + 
  theme(legend.title = element_blank())


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