[R-SIG-Finance] Cross correlation problems

Brian G. Peterson brian at braverock.com
Fri Apr 5 19:21:10 CEST 2013


On 04/05/2013 12:16 PM, wlblount wrote:
> so then the above with 4 symbols works fine, but when you add a symbol
<... broken code deleted>
> you get an error message.  what am i missing?

The code that Rob forwarded later, below, is elastic to basically any 
number of symbols.

library(quantmod)
symbols <- c("SPY","ILF","EWZ","IBM","AAPL","GOOG")
initDate <- "2010-01-01"
getSymbols(symbols,from=initDate)
rets <- xts(do.call(cbind.xts,
                     lapply(symbols,function(sym)
                         dailyReturn(get(sym)))),
             order.by=as.Date(index(get(symbols[1]))))
colnames(rets) <- symbols
print(round(cor(rets),2))



More information about the R-SIG-Finance mailing list