Analyze Stock Price Data Using R, Part2

Master R

By Guangming Lang Comment

When constructing a portfolio, it’s important to pick un-correlated assets. This is just a corollary of the “don’t put all your eggs in one basket” principle. If asset B goes up (or down) when asset A goes up (or down) and vice versa, there’s really no reason to own both assets. By and large, assets of different classes are weakly correlated, for example, stocks and bonds, stocks and gold, and etc. In part1, I said I’m interested in a vanguard energy fund (VGENX) because oil price has been hammered. However, because I already own the Total Stock Market Index Fund (VTSMX), I want to find out how correlated VGENX is with VTSMX.

Step 1. Load libraries and helper functions

library(PerformanceAnalytics)
library(zoo)
library(tseries)

Step 2. Download the monthly adjusted closing price data on VGENX and VTSMX since Sept 2005 from Yahoo.

VGENX = get.hist.quote(instrument="VGENX", start="2005-09-30", 
                       end="2014-09-30", origin="1970-01-01",
                       quote="AdjClose", provider="yahoo", 
                       compression="m", retclass="zoo")
## time series ends   2014-09-02
VTSMX = get.hist.quote(instrument="VTSMX", start="2005-09-30", 
                       end="2014-09-30", origin="1970-01-01",
                       quote="AdjClose", provider="yahoo",
                       compression="m", retclass="zoo")
## time series ends   2014-09-02

Step 3. Change the class of the time index to yearmon.

index(VGENX) = as.yearmon(index(VGENX))
index(VTSMX) = as.yearmon(index(VTSMX))

Step 4. Merge both price series into one data frame and Calculate continuously compounded returns.

prices = merge(VGENX, VTSMX)
colnames(prices) = c("VGENX", "VTSMX")
ret.cc = diff(log(prices))
head(ret.cc, 3)
##                VGENX        VTSMX
## Oct 2005 -0.08818443 -0.018813600
## Nov 2005  0.02153350  0.038941761
## Dec 2005  0.03146979  0.001354186

Step 5. Plot cumulative returns.

ret.simple = diff(prices) / lag(prices, k=-1)
chart.CumReturns(ret.simple, legend.loc="topleft", wealth.index=TRUE, 
                 ylab="$", main="Future Value of $1 invested")

center

Step 6. Display pair-wise scatter plots.

return_matrix = coredata(ret.cc)
pairs(return_matrix, pch=16, col="dodgerblue2")

center

Both plots show the returns of VGENX and VTSMX are pretty correlated.

Step 7. Compute correlation matrices.

cor(return_matrix)
##           VGENX     VTSMX
## VGENX 1.0000000 0.7833628
## VTSMX 0.7833628 1.0000000

We see their correlation is 0.78, which is pretty high.

If you enjoyed this post, get updates. It's FREE