##################################
#                                #
#  Plot the yield curves for     #
#  the two latest crises vs.     #
#  the current yield curve       #
#                                #
##################################

# Treasury daily yield curve data is used

library(rvest)
#> Loading required package: xml2
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date
library(xts)
#> Loading required package: zoo
#> 
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#> 
#>     as.Date, as.Date.numeric
#> 
#> Attaching package: 'xts'
#> The following objects are masked from 'package:dplyr':
#> 
#>     first, last

# read the html page with the data in a table
# pull the table
# reformat the date field

#url <- "https://www.treasury.gov/resource-center/data-chart-center/interest-rates/Pages/TextView.aspx?data=yieldYear&year=2016"
url <- "https://www.treasury.gov/resource-center/data-chart-center/interest-rates/Pages/TextView.aspx?data=yieldAll"
yc <-
    read_html(url) %>%
    html_node("table.t-chart") %>%
    html_table(header = TRUE, trim = TRUE, fill = TRUE, dec = ".") %>%
    mutate(Date=as.Date(Date, format = "%m/%d/%y"))

# convert to an xts time-series
# replace "N/A" with NA

yield_curve <- xts(select(yc,-Date), order.by=yc$Date)
yield_curve[ yield_curve == "N/A" ] <- NA

# pull out the first trading day of each year

first_of_year  <- do.call(rbind, lapply(split(yield_curve, "years"), first))

# pull the years of interest

current  <- last(yield_curve,"day")

jan_2004 <- first_of_year["2004"]
jan_2005 <- first_of_year["2005"]
jan_2006 <- first_of_year["2006"]
jan_2007 <- first_of_year["2007"]
jan_2016 <- first_of_year["2016"]

# plot the financial crisis

ylim <- c(0,as.numeric(max(c(current,
                             jan_2004,
                             jan_2005,
                             jan_2006,
                             jan_2007,
                             jan_2016),na.rm=TRUE))*1.1)
plot(as.vector(current), type="p", pch=20,
     xaxt = "n", xlab="term",
     ylab="Interest Rate", ylim=ylim,
     main=paste0("Yield Curves\nRun-Up to the Financial Crisis to Now"))
axis(1, at=axTicks(1), labels=colnames(current)[axTicks(1)])

lines(as.vector(jan_2007), lwd=2, col="green")
lines(as.vector(jan_2006), lwd=2, col="blue")
lines(as.vector(jan_2005), lwd=2, col="red")
points(as.vector(jan_2004), pch=8)
points(as.vector(jan_2016), pch=8, col="red")

text(x=1, y=as.numeric(current[1,1]),  labels="now",  pos=3)
text(x=1, y=as.numeric(jan_2004[1,1]), labels="2004", pos=3)
text(x=1, y=as.numeric(jan_2005[1,1]), labels="2005", pos=3)
text(x=1, y=as.numeric(jan_2006[1,1]), labels="2006", pos=3)
text(x=1, y=as.numeric(jan_2007[1,1]), labels="2007", pos=3)
text(x=1, y=as.numeric(jan_2016[1,1]), labels="2016", pos=3)

legend("top", legend=c(format(index(jan_2016),"%b %d, %Y"),
                           format(index(jan_2007),"%b %d, %Y"),
                           format(index(jan_2006),"%b %d, %Y"),
                           format(index(jan_2005), "%b %d, %Y"),
                           format(index(jan_2004), "%b %d, %Y"),
                           format(index(current), "%b %d, %Y")),
       pch=c(8,20,20,20,8,20),
       col=c("red","green","blue","red","black","black"))

grid(col="black")

# ==================================================
jan_1998 <- first_of_year["1998"]
jan_1999 <- first_of_year["1999"]
jan_2000 <- first_of_year["2000"]

# plot the dot.com bubble

ylim <- c(0,as.numeric(max(c(current,
                             jan_1998,
                             jan_1999,
                             jan_2000,
                             jan_2016),na.rm=TRUE))*1.1)
plot(as.vector(current), type="p", pch=20,
     xaxt = "n", xlab="term",
     ylab="Interest Rate", ylim=ylim,
     main=paste0("Yield Curves\nRun-Up to the Dot.com Bubble to Now"))
axis(1, at=axTicks(1), labels=colnames(current)[axTicks(1)])

lines(as.vector(jan_2000), lwd=2, col="green")
lines(as.vector(jan_1998), lwd=2, col="blue")
lines(as.vector(jan_1999), lwd=2, col="red")
points(as.vector(jan_2016), pch=8, col="red")

text(x=2, y=as.numeric(current[1,2]),  labels="now",  pos=3)
text(x=2, y=as.numeric(jan_1999[1,2]), labels="1999", pos=3)
text(x=3, y=as.numeric(jan_1998[1,2]), labels="1998", pos=3)
text(x=2, y=as.numeric(jan_2000[1,2]), labels="2000", pos=3)
text(x=2, y=as.numeric(jan_2016[1,2]), labels="2016", pos=3)

legend("center", legend=c(format(index(jan_2000),"%b %d, %Y"),
                               format(index(jan_1998),"%b %d, %Y"),
                               format(index(jan_1999), "%b %d, %Y"),
                               format(index(current), "%b %d, %Y")),
       pch=c(20,20,20,20),
       col=c("green","blue","red","black"))

grid(col="black")