Skip to content

Instantly share code, notes, and snippets.

@vikjam
Created June 30, 2017 11:48
Show Gist options
  • Save vikjam/14e5ea9d36f04dcb7cd267a6a50fe4a0 to your computer and use it in GitHub Desktop.
Save vikjam/14e5ea9d36f04dcb7cd267a6a50fe4a0 to your computer and use it in GitHub Desktop.
Plot of yields
library(rvest)
library(ggplot2)
library(ggthemes)
library(MASS)
library(splines)
treas.html <- html("http://www.treasury.gov/resource-center/data-chart-center/interest-rates/Pages/TextView.aspx?data=yield")
yield.data <- treas.html %>%
html_nodes("table") %>%
.[[67]] %>%
html_table(fill = TRUE)
treas.plot <- data.frame(end = c(1/12, 3/12, 6/12, 1, 2, 3, 5, 7, 10, 20, 30),
rate = as.vector(t(yield.data[ 10, 2:ncol(yield.data)])))
treas.gg <- ggplot(aes(x = end, y = rate), data = treas.plot)
treas.curve <- treas.gg + geom_point() +
geom_smooth(se = FALSE,
method = "lm",
formula = y ~ ns(x, 3),
colour = "black") +
theme_tufte(base_size = 15) +
xlab("Maturity (years)") +
ylab("Yield (%)")
ggsave(file = "treas-curve.svg")
greek.html <- html("http://www.bankofgreece.gr/Pages/en/Statistics/rates_markets/titloieldimosiou/titloieldimosiou.aspx")
greek.data <- greek.html %>%
html_nodes("table") %>%
html_nodes("table") %>%
.[[268]] %>%
html_table(fill = TRUE)
greek.plot <- data.frame(end = c(3, 5, 10, 15, 20, 30),
rate = as.vector(t(greek.data[1, seq(3, ncol(greek.data), by = 2)])))
greek.gg <- ggplot(aes(x = end, y = rate), data = greek.plot)
greek.curve <- greek.gg + geom_point() +
geom_smooth(se = FALSE,
method = "lm",
formula = y ~ ns(x, 3),
colour = "black") +
theme_tufte(base_size = 15) +
xlab("Maturity (years)") +
ylab("Yield (%)")
ggsave(file = "greek-curve.svg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment