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)
## Error in library(PerformanceAnalytics): there is no package called 'PerformanceAnalytics'
library(zoo)
library(tseries)
## Error in library(tseries): there is no package called '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")
## Error in get.hist.quote(instrument = "VGENX", start = "2005-09-30", end = "2014-09-30", : could not find function "get.hist.quote"
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")
## Error in get.hist.quote(instrument = "VTSMX", start = "2005-09-30", end = "2014-09-30", : could not find function "get.hist.quote"

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

index(VGENX) = as.yearmon(index(VGENX))
## Error in index(VGENX): object 'VGENX' not found
index(VTSMX) = as.yearmon(index(VTSMX))
## Error in index(VTSMX): object 'VTSMX' not found

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

prices = merge(VGENX, VTSMX)
## Error in merge(VGENX, VTSMX): object 'VGENX' not found
colnames(prices) = c("VGENX", "VTSMX")
## Error in colnames(prices) = c("VGENX", "VTSMX"): object 'prices' not found
ret.cc = diff(log(prices))
## Error in diff(log(prices)): object 'prices' not found
head(ret.cc, 3)
## Error in head(ret.cc, 3): object 'ret.cc' not found

Step 5. Plot cumulative returns.

ret.simple = diff(prices) / lag(prices, k=-1)
## Error in diff(prices): object 'prices' not found
chart.CumReturns(ret.simple, legend.loc="topleft", wealth.index=TRUE, 
                 ylab="$", main="Future Value of $1 invested")
## Error in chart.CumReturns(ret.simple, legend.loc = "topleft", wealth.index = TRUE, : could not find function "chart.CumReturns"

Step 6. Display pair-wise scatter plots.

return_matrix = coredata(ret.cc)
## Error in coredata(ret.cc): object 'ret.cc' not found
pairs(return_matrix, pch=16, col="dodgerblue2")
## Error in pairs(return_matrix, pch = 16, col = "dodgerblue2"): object 'return_matrix' not found

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

Step 7. Compute correlation matrices.

cor(return_matrix)
## Error in is.data.frame(x): object 'return_matrix' not found

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

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