Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
As a first step in visualizing/exploring the data from my last post, FOMC Dates – Scraping Data From Web Pages, I’ll plot the FOMC announcement dates along with the following price series: 2-Year and 10-Year US Treasury yields, S&P500 ETF (SPY) and USD Index ETF (UUP).
I’ll use the quantmod R package to download the price data from the US Federal Reserve Economic Data (FRED) database and Yahoo Finance.
For the graphics, I like the cool gglot2 R package, and although it’s more complicated to code, it’s worth it.
First, install and load the quantmod, reshape2 and ggplot2 R packages.
install.packages(c("quantmod", "reshape2", "ggplot2"), repos = "http://cran.us.r-project.org") library(quantmod) library(reshape2) library(ggplot2)
Download the price series data and store as xts objects, and load the FOMC announcement dates from the file saved in the last post.
# get 2-year and 10-year US Treasury yields getSymbols(c("DGS2", "DGS10"), src = "FRED") DGS2 <- DGS2["2009-01-02/"] DGS10 <- DGS10["2009-01-02/"] # get S&P500 ETF prices getSymbols("SPY", from = "2009-01-02") # get USD Index ETF prices getSymbols("UUP", from = "2009-01-02") # load FOMC announcement dates from file previously saved in working directory load("fomcdates.RData")
Reshape the yield data in order to plot both the 2-Year and 10-Year yields on the same chart. The FOMC announcement dates will be shown as dashed vertical lines.
# prepare yield data yields <- data.frame(index(DGS2), DGS2, DGS10) names(yields) <- c("Date", "2Yr Yield", "10Yr Yield") yieldsmelt <- melt(yields, id.vars = "Date") # plot yield chart gp <- ggplot(yieldsmelt, aes(x = Date, y = value)) + geom_line(aes(colour = variable)) + labs(list(title = "US Treasury Yields with FOMC Dates", x = "", y = "% p.a.")) + scale_colour_manual(name = "Yield", values = c("darkblue", "darkred")) + geom_vline(xintercept = as.numeric(fomcdates), linetype = "dashed", size = 0.5, alpha = 0.5) + scale_x_date() print(gp)
Finally, plot the charts for the S&P500 and USD Index ETFs – no need to prep the data as we’re plotting 1 time series per chart.
# plot S&P500 chart gp1 <- autoplot.zoo(SPY[, "SPY.Adjusted"]) + labs(list(title = "S&P500 ETF (SPY) with FOMC Dates", x = "", y = "USD")) + geom_line(colour = "darkblue") + geom_vline(xintercept = as.numeric(fomcdates), linetype = "dashed", size = 0.5, alpha = 0.5) + scale_x_date() print(gp1)
# plot USD Index chart gp2 <- autoplot.zoo(UUP[, "UUP.Adjusted"]) + labs(list(title = "USD Index ETF (UUP) with FOMC Dates", x = "", y = "USD")) + geom_line(colour = "darkblue") + geom_vline(xintercept = as.numeric(fomcdates), linetype = "dashed", size = 0.5, alpha = 0.5) + scale_x_date() print(gp2)
All look OK but a lot more exploring to do…
Click here for the R code on GitHub.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.