Last active
March 24, 2023 20:50
-
-
Save jkr216/73ce484e200f74befdc2436810454a43 to your computer and use it in GitHub Desktop.
FOMC meeting dates, rate changes, scrape with R code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Setup | |
```{r, include = FALSE} | |
library(tidyverse) | |
library(tidyquant) | |
library(timetk) | |
library(scales) | |
library(janitor) | |
library(httr) | |
library(jsonlite) | |
library(XML) | |
library(rvest) | |
library(htmltools) | |
library(gt) | |
library(gtExtras) | |
``` | |
### Introduction | |
Below is a function to scrape the federal reserve website for fomc meeting dates and rate decisions. | |
Note that this takes us back only from 2023 to 2003. The data are here: | |
https://www.federalreserve.gov/monetarypolicy/openmarket.htm | |
For 2002 back to 1990, the Fed stores those at a separate archive html here: | |
https://www.federalreserve.gov/monetarypolicy/openmarket_archive.htm | |
### Build Function | |
```{r} | |
fed_funds_change_scraper <- function(chosen_number){ | |
url <- "https://www.federalreserve.gov/monetarypolicy/openmarket.htm" | |
# 1 = 2023 | |
# 14 = 2003 | |
# Read the HTML content of the webpage | |
webpage <- read_html(url) | |
html_cell_number <- chosen_number | |
# Extract the table containing the interest rate change dates | |
table <- html_nodes(webpage, "table")[[html_cell_number]] | |
# Extract the rows of the table containing the interest rate change dates | |
rows <- html %>% | |
html_nodes("table") %>% | |
.[[1]] | |
# Extract the dates from the rows and filter for changes since 1970 | |
change_dates <- | |
html_nodes(rows, "td:nth-child(1)") %>% | |
html_text() | |
increases <- | |
html_nodes(rows, "td:nth-child(2)") %>% | |
html_text() | |
decreases <- | |
html_nodes(rows, "td:nth-child(3)") %>% | |
html_text() %>% | |
str_replace("75-100", "85") | |
year <- html_nodes(webpage, "h4")[[html_cell_number +1]] %>% | |
html_text() | |
tibble( | |
date = str_glue("{change_dates}, {year}") %>% parse_date_time(orders = "%B %d, %Y") %>% ymd(), | |
increases = as.numeric(increases)/100, | |
decreases = as.numeric(decreases)/-100 | |
) | |
} | |
# use map_dfr() to iterate across the html nodes | |
fed_funds_changes_current <- | |
map_dfr(seq(1, 14), | |
fed_funds_change_scraper) %>% | |
mutate(change = | |
case_when( | |
increases != 0 ~ increases, | |
T ~ decreases | |
)) %>% | |
select(date, change) | |
fed_funds_changes_current %>% | |
gt() %>% | |
fmt_percent(change, | |
scale_values = F) %>% | |
gt_theme_538() | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment