Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
And as usual, I always want a longer data set, so after a little playing with R-Excel, we can extend our historical sources of bond returns to 1919. If nothing else, maybe you can find other uses for the Shiller Dataset in R.
From TimelyPortfolio |
And as an added bonus, we can prepare a boxplot.
From TimelyPortfolio |
As always, please criticize or make suggestions.
R code:
require(quantmod)
require(PerformanceAnalytics)
getSymbols(“BAA”,src=”FRED”) #load Corporate for credit from Fed Fred
#get Shiller data for inflation and US Treasury 10 year
require(gdata)
URL <- “http://www.econ.yale.edu/~shiller/data/ie_data.xls”
shillerData <- read.xls(URL,sheet=”Data”,pattern=”Rate GS10″)
#strip out date(1), CPI(5), and GS10(7)
shillerData <- shillerData[,c(1,5,7)]
#get data starting in January 1919 to match with BAA xts series
shillerData <- shillerData[rownames(shillerData[which(shillerData$Date==format(index(BAA)[1],”%Y.%m”)):NROW(shillerData),]),2:3]
shillerData <- xts(shillerData[1:NROW(BAA),1:2],order.by=index(BAA))
bondReturnSources<-merge(BAA,shillerData)
bondReturnSources[,1]<-bondReturnSources[,1]-bondReturnSources[,3]
#get 12 month CPI change
bondReturnSources[,2]<-ROC(bondReturnSources[,2],12,type=”discrete”)*100
bondReturnSources[,3]<-bondReturnSources[,3]-bondReturnSources[,2]
bondReturnSources<-merge(bondReturnSources,bondReturnSources[,1]+bondReturnSources[,2]+bondReturnSources[,3])
colnames(bondReturnSources)<-c(“Credit”,”Inflation”,”Real”,”Total”)
chart.TimeSeries(bondReturnSources,legend.loc=”bottom”,main=”Historical Sources of Bond Returns”,ylab=”Yield as %”,colorset=c(“cadetblue”,”darkolivegreen3″,”goldenrod”,”gray70″))
#add source as caption
mtext(“Source: Federal Reserve FRED and Robert Shiller”,side=1,adj=0)
chart.Boxplot(bondReturnSources,main=”Historical Sources of Bond Returns”,colorset=c(“cadetblue”,”darkolivegreen3″,”goldenrod”,”gray70″))
mtext(“Source: Federal Reserve FRED and Robert Shiller”,side=1,adj=0)
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.