Tue Sep 6 13:27:32 2016
Only interative plots were used in this report, so be sure to hover your mouse all over the graphs and legends to get further details
Here are all the packages and options necessary to reproduce this analysis.
suppressMessages(library(RCurl))
suppressMessages(library(plyr))
suppressMessages(library(xtable))
suppressMessages(library(rCharts))
suppressMessages(library(googleVis))
knitr::opts_chunk$set(cache=TRUE)
knitr::opts_chunk$set(results='asis')
options("scipen"=100, "digits"=4)
options(warn=-1)
To make the project reproducible, we used an API to download all necessary data. They were downloaded on the 10/23/2015. Next we proceeded with some data wrangling to choose the the required date range for the US treasury bonds (2005-2015 i.e 10 years), and merged the two time series based on common dates. Finally we ran a quick sanity check just to make sure there wasn't any missing data or other irregularities.
url_libor <- "https://www.quandl.com/api/v3/datasets/FRED/USD3MTD156N.csv"
x <- getURL(url_libor, .opts=curlOptions(followlocation = TRUE))
libor <- read.csv(text=x, stringsAsFactors = FALSE)
url_bond <- "https://www.quandl.com/api/v3/datasets/USTREASURY/YIELD.csv"
x <- getURL(url_bond, .opts=curlOptions(followlocation = TRUE))
bonds <- read.csv(text=x, stringsAsFactors = FALSE)
dates <- as.Date(bonds$Date)
bonds = data.frame(date = bonds$Date,
year = as.integer(format(dates, format = "%Y")),
month = format(dates, format = "%b"),
day = as.integer(format(dates, format = "%d")),
bonds_10yr=bonds$X10.YR)
bonds <- bonds[bonds$year >= 2005, ]
data <- merge(bonds, libor, by.x = "date", by.y = "DATE")
names(data)[6] <- "libor"
sapply(data, function(x) sum(is.na(x)))
date year month day bonds_10yr libor
0 0 0 0 0 0
xt <- xtable(head(data))
print(xt, type="html")
| date | year | month | day | bonds_10yr | libor | |
|---|---|---|---|---|---|---|
| 1 | 2005-01-04 | 2005 | Jan | 4 | 4.29 | 2.57 |
| 2 | 2005-01-05 | 2005 | Jan | 5 | 4.29 | 2.59 |
| 3 | 2005-01-06 | 2005 | Jan | 6 | 4.29 | 2.61 |
| 4 | 2005-01-07 | 2005 | Jan | 7 | 4.29 | 2.61 |
| 5 | 2005-01-10 | 2005 | Jan | 10 | 4.29 | 2.62 |
| 6 | 2005-01-11 | 2005 | Jan | 11 | 4.26 | 2.63 |
This section wasn't required, but we were just curious as know how these equities performed during the choosen date range. The 10-year treasury bonds performed consistently better from around 2009 and upward.
r_plot <- mPlot(x = "date", y = c("libor", "bonds_10yr"), type = "Line", data = data)
r_plot$set(pointSize = 0, lineWidth = 1)
r_plot$show("iframesrc")
Using the plyr package, monthly and yearly beta values were computed. And the data frame needed to create our final plot was constructed.
month_betas <- ddply(data, .(year, month), summarize,
monthly_betas=coef(lm(libor ~ bonds_10yr))[2])
year_betas <- ddply(data, .(year), summarize,
yearly_betas=coef(lm(libor ~ bonds_10yr))[2])
month_betas$monthly_betas <- round(month_betas$monthly_betas, 2)
year_betas$yearly_betas <- round(year_betas$yearly_betas, 2)
betas <- merge(month_betas, year_betas)
betas$date <- paste(betas$month, betas$year)
betas$year <- as.integer(betas$year)
xt <- xtable(head(betas))
print(xt, type="html")
| year | month | monthly_betas | yearly_betas | date | |
|---|---|---|---|---|---|
| 1 | 2005 | Apr | -0.21 | 1.29 | Apr 2005 |
| 2 | 2005 | Aug | -0.39 | 1.29 | Aug 2005 |
| 3 | 2005 | Dec | -0.37 | 1.29 | Dec 2005 |
| 4 | 2005 | Feb | 0.45 | 1.29 | Feb 2005 |
| 5 | 2005 | Jan | -0.87 | 1.29 | Jan 2005 |
| 6 | 2005 | Jul | 0.66 | 1.29 | Jul 2005 |
Again, the graph below is quite interactive. One can see that the yearly beta values were at their lowest in 2009, and the monthly beta values were at the peak in January 2008.
g_plot <- gvisLineChart(betas, "date", c("monthly_betas","yearly_betas"),
options=list(
series="[{targetAxisIndex: 0},
{targetAxisIndex:1}]",
vAxes="[{title:'Monthly_Betas'}, {title:'Yearly_Betas'}]"
))
g_plot
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<!– LineChart generated in R 3.3.1 by googleVis 0.6.1 package –>
R version 3.3.1 (2016-06-21) • Google Terms of Use • Documentation and Data Policy