Skip to content

Instantly share code, notes, and snippets.

@cmrnp
Last active August 30, 2022 11:31
Show Gist options
  • Save cmrnp/e9d2a64d8cfc487b3f03c5dad766436b to your computer and use it in GitHub Desktop.
Save cmrnp/e9d2a64d8cfc487b3f03c5dad766436b to your computer and use it in GitHub Desktop.
library(tidyverse)
library(lubridate)
library(glue)
library(jsonlite)
library(cowplot)
# thanks to u/doubleunplussed on reddit for this dataset
json_url <- "https://pastebin.com/raw/gxZAUJwd"
ib_rate_data_orig <- fromJSON(json_url)
# make a tidy data frame
ib_rate_data <-
tibble(fetch_date = names(ib_rate_data_orig),
dat = ib_rate_data_orig) %>%
unnest_longer(
col = dat,
values_to = "rate",
indices_to = "rate_date"
) %>%
mutate(
fetch_date = ymd(fetch_date),
rate_date = dmy(glue("01-{rate_date}")),
latest = fetch_date == max(fetch_date),
freshness = (as.numeric(fetch_date - min(fetch_date)) /
as.numeric(max(fetch_date) - min(fetch_date)))
)
# plot settings
theme_set(
theme_cowplot(font_size = 11, rel_small = 1, rel_tiny = 8/11, rel_large = 1) +
background_grid("xy") +
theme(legend.position = "off",
plot.background = element_rect(colour = NA, fill = "white"))
)
# plot of predictions overlaid with most recent highlighted in red
ib_rate_data %>%
ggplot(aes(x = rate_date, y = rate, group = fetch_date,
colour = latest, size = latest, alpha = latest)) +
geom_line() +
scale_colour_manual(values = c("dodgerblue3", "firebrick3")) +
scale_size_manual(values = c(0.5, 2.0)) +
scale_alpha_manual(values = c(0.3, 1.0)) +
expand_limits(y = c(0, 4.5)) +
scale_x_date(date_labels = "%b %Y",
limits = ymd(c("2022-01-01", "2024-01-01"))) +
labs(x = "Month interest rate predicted for",
y = "Market-implied interest rate (%)",
caption = "Red line shows most recent prediction. Data from asx.com.au via u/doubleunplussed.")
ggsave("interest_rate_predictions_1.png", width = 7, height = 5, dpi = 600)
# panel plot showing time series for each future month's predictions
ib_rate_data %>%
filter(rate_date != "2022-04-01") %>%
mutate(rate_date = rate_date %>%
format("%b %Y") %>%
fct_inorder()) %>%
ggplot(aes(x = fetch_date, y = rate, group = rate_date)) +
expand_limits(y = c(0, 4.5)) +
geom_line() +
geom_point(colour = "firebrick3", size = 2.0, data = ~filter(., latest)) +
facet_wrap(~rate_date) +
panel_border() +
labs(x = "Date prediction made",
y = "Market-implied interest rate (%)",
caption = "Red point shows most recent prediction. Data from asx.com.au via u/doubleunplussed.") +
theme(axis.text.x = element_text(size = rel(8/11)))
ggsave("interest_rate_predictions_2.png", width = 7, height = 6, dpi = 600)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment