Data Plus Packages, Voila!!

Fortunately for us, the R-Community has developed a large number of packages for our use.  Especially so since the amount of real-world data that is available even on a freely available basis is staggering.

We are interested in market data, think rolling chrons on CNBC, particularly price data on stocks, indexes etc.,  We may also want to look at fundamental data relating to particular companies, e.g., balance sheet, valuation ratios, trends and the like.  Analysis of such data is very easily performed using many of the financial oriented packages for R.  These can very easily installed using the Tools feature provided in the R-Studio IDE.

Looking at the broad market and its major components should provide a good starting point.   Data for the SP500 and its major sectors can be used in allocation decisions for example, since returns are quite different.  Investment options are available in the sense that there are low-cost mutual funds which are strictly sector specific. A good overview of the generally defined sectors, as exemplified by mutual fund offerings for example can be gleaned from this image below.  It is instructive to look at some of the data and some code provided for that as well.

 

The next step would be to look at the historical price data for these sectors and analyze their individual, collective and correlations if any.   Comparisons can be made to a benchmark, in this case, the entire SP500.   I have compiled a couple of datasets from web-available data. This dataset

Sector daily prices for the sectors above e.g., XLE, XLY etc., are available for download in a zip file, Sector Close Prices , from 2000 onwards for all the sectors excepting a couple for which data is not available.  The file can be downloaded and unzipped and placed in a location on your computer.  In my example code below,  I just happened to use “Z:/Workspace/QSBlog/SectorClosePrices.csv”.  Benchmark data for SP500 can be used for comparison purposes and is provided as monthly here.

The R-package ‘PerformanceAnalytics’ needs to be installed.  As luck would have it, this package will automatically install two other critical packages that handle time series data, namely the xts and zoo packages.  With just a few simple lines of code, we can do amazing amount of digging into the data.  But for now, only some charts will be shown – these should all be reproducible.

#####################################################  
#####      Analysis of prices & returns  ############
#####
library(PerformanceAnalytics)
#####    get sector price data from downloaded/unzipped file 
readETF<-as.xts(read.zoo("Z:/WorkSpace/QSBlog/SectorClosePrices.csv",head=TRUE,as.is=TRUE,sep=",",format="%Y-%m-%d"))
mnthlySectorRet <- Return.calculate(readETF[endpoints(readETF, on="months")], method="log")
chart.Boxplot(mnthlySectorRet, sort.by="mean", colorset="red", 
              symbol.color="blue", median.symbol="@", element.color="darkgray")
chart.RiskReturnScatter(mnthlySectorRet)
charts.PerformanceSummary(mnthlySectorRet, main="ETF Performance Summary")
chart.RollingPerformance(mnthlySectorRet["2005-01::2011-12"], legend.loc="bottomleft")
chart.CumReturns(mnthlySectorRet,main="ETF Cumulative Returns", legend.loc="topleft" )

Boxplot

Risk Return Scatter

Performance Summary

Rolling Performance Summary – Note subsetting of dates (only from 2005-01 to 2011-12, Housing bubble crash anyone!)

Cumulative Returns

Thanx for reading