Fig 1. Historical ts plot of S&P500 returns <= -5%
The following R code produced a time series plot of historical occasions where this occurred.
###################################################
library(quantmod)
getSymbols("^GSPC",from="1950-01-01",to="2012-01-01")
adj<-GSPC$GSPC.Adjusted
rtn<-(adj/lag(adj,1)-1)[2:length(adj)]
r05<-rtn[rtn<= -.05]
plot(sort(r05),type='o',main='S&P500 1950-present returns <= -5%')
###################################################
Although the frequency of such occurrences is arguably rare, the 1987 drop is much more worthy of the 1 day label 'plunge.'
One other disturbing observation in the data, however, is the large temporal clustering of occurrences in the recent 2008 region. Now that's behavior to be concerned about (not to mention revised flash crash data pts.).
filtered 1 day cl-cl returns <=-5% sorted by date
Hi,
ReplyDeleteI'm just getting started using R for research and teaching. I've been trying to bring in bond market data to R for a few hours with very little success. Is there a trick I'm missing?
Thanks again for this blog, it's been a lifesaver trying to get up the R curve as an economist.
Best,
Stephen
Stephen,
ReplyDeleteIf you have a yahoo symbol you can just
use with TNX 10yr as example:
library(quantmod)
getSymbols("^TNX")
Alternatively, you can go to a historical site like http://www.federalreserve.gov/releases/h15/data.htm then download the file of interest locally to your drive as a .csv file.
You can then read and clean up into R as a time series object.
ex:
twoyr<-read.csv("C:/Stuff/bonds.csv",skip=20,colClasses=c("character","numeric"),na.strings='ND',sep=",",col.names=c('Date','yield'))
#clean up ND data & convert to zoo and data frame objects if desired
twoyr<-twoyr[!is.na(twoyr$yield),]
twoyrzoo <- zoo(twoyr[, 2], as.Date(twoyr$Date, format = "%m/%d/%Y"))
twoyrdf<-as.data.frame(twoyrzoo)
names(twoyrdf)<-'yield'
Hope that helps,
IT
If you are already using quantmod in R you could also go to the St. Louis fed aka FRED if you know what your series ID is...
ReplyDeleteGet the 1-Year Treasury Constant Maturity Rate like so...
tcmr1y <- getSymbols('DGS1',src='FRED',auto.assign=FALSE)
tcmr1y now holds your time series or rates...
Cordially,
-DD-
Looks much simpler,
ReplyDeletethanks DD.