Skip to content

Cross correlation problems

11 messages · Rob Schmidt, wlblount, Brian G. Peterson

#
Hi starter123,

I don't have any good suggestions but here's a little code snippet that
might give you some ideas.  ( Thanks to rosy2 from elitetrader)

Best Regards,

Rob

library(quantmod)
getSymbols("YHOO",src="google")
getSymbols("GOOG",src="google")
getSymbols("MSFT",src="google")
df = data.frame( dailyReturn(YHOO), dailyReturn(GOOG), dailyReturn(MSFT) )
names(df)<- c("YHOO","GOOG","MSFT")
round(cor(df),2)




--
View this message in context: http://r.789695.n4.nabble.com/Cross-correlation-problems-tp4663151p4663338.html
Sent from the Rmetrics mailing list archive at Nabble.com.
#
just curious why this would not work

library(quantmod) 
getSymbols("SPY",src="google") 
getSymbols("ILF",src="google") 
getSymbols("EWZ",src="google") 
df = data.frame( dailyReturn(SPY), dailyReturn(ILF), dailyReturn(EWZ) ) 
names(df)<- c("SPY","ILF","EWZ") 
round(cor(df),2)

i assume it has to do with the "names" function.

can some one point me in the direction of... 

1 - using variables as to only have to input symbol once  

2 - how about expanding to 4 symbols (or any amount of 
symbols)

Thanks,
Bill




--
View this message in context: http://r.789695.n4.nabble.com/Cross-correlation-problems-tp4663151p4663403.html
Sent from the Rmetrics mailing list archive at Nabble.com.
#
I think of setting the initDate as a cheesy way to induce the rows to match
better without having to actually deal with it.  Here's a more mangled
version:

library(quantmod)

sym1 <- "SPY"
sym2 <- "ILF"
sym3 <- "EWZ"
sym4 <- "IBM"
initDate <- "2010-01-01"
getSymbols(sym1,from=initDate)
getSymbols(sym2,from=initDate)
getSymbols(sym3,from=initDate)
getSymbols(sym4,from=initDate)
df <- data.frame( dailyReturn(get(sym1)), dailyReturn(get(sym2)), 
                 dailyReturn(get(sym3)), dailyReturn(get(sym4)) )
names(df) <- c(sym1,sym2,sym3,sym4)
print(round(cor(df),2))

Best regards,

Rob




--
View this message in context: http://r.789695.n4.nabble.com/Cross-correlation-problems-tp4663151p4663410.html
Sent from the Rmetrics mailing list archive at Nabble.com.
#
And here is the way that a non-newbie would approach the problem.  (Thanks to
Brian who must be taking pity on us, LOL)

library(quantmod)
symbols <- c("SPY","ILF","EWZ","IBM")
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(df),2))




--
View this message in context: http://r.789695.n4.nabble.com/Cross-correlation-problems-tp4663151p4663425.html
Sent from the Rmetrics mailing list archive at Nabble.com.
#
Doh!  Might as well fix my typo at the bottom.

library(quantmod)
symbols <- c("SPY","ILF","EWZ","IBM")
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))    




--
View this message in context: http://r.789695.n4.nabble.com/Cross-correlation-problems-tp4663151p4663428.html
Sent from the Rmetrics mailing list archive at Nabble.com.
#
so then the above with 4 symbols works fine, but when you add a symbol

library(quantmod) 

sym1 <- "SPY" 
sym2 <- "ILF" 
sym3 <- "EWZ" 
sym4 <- "DIA"  
sym5 <- "EPP"
initDate <- "2010-01-01" 
getSymbols(sym1,from=initDate) 
getSymbols(sym2,from=initDate) 
getSymbols(sym3,from=initDate) 
getSymbols(sym4,from=initDate) 
getSymbols(sym5,from=initDate) 

df <- data.frame( dailyReturn(get(sym1)), dailyReturn(get(sym2)), 
                  dailyReturn(get(sym3)), dailyReturn(get(sym4)),
			dailyReturn(get(sym5))) 
names(df) <- c(sym1,sym2,sym3,sym4,sym5) 
print(round(cor(df),2))





you get an error message.  what am i missing?



--
View this message in context: http://r.789695.n4.nabble.com/Cross-correlation-problems-tp4663151p4663457.html
Sent from the Rmetrics mailing list archive at Nabble.com.
#
On 04/05/2013 12:16 PM, wlblount wrote:
<... broken code deleted>
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))
#
Hi wlblount,

The snippet I found was just barely functional.  You could "fix" the frame
buffer on the five sized version by changing the initDate to 2011.  It is
sensitive to the length of the data.  But the right way is by using the xts
object.   And you get lots of nice hidden features that way.  For example,
put 2003 for the year in the xts version.  You will find that some
correlations are missing but the calculation still doesn't give any errors.

Best regards,

Rob



--
View this message in context: http://r.789695.n4.nabble.com/Cross-correlation-problems-tp4663151p4663484.html
Sent from the Rmetrics mailing list archive at Nabble.com.
#
Hi wlblount,

I'm sorry, I think my poor explanation might be  misleading you.  I'm good
at that.   I don't think it is the total data length, rather that each
column has to match the others in the data frame.  (That's another thing
that using the xts object solves for us.)  My cheesy solution of shortening
the columns was to attempt to just make the columns match by getting rid of
missing data, etc.  Here is the five sized example showing the column
lengths not matching.

Best regards,

Rob

library(quantmod)

sym1 <- "SPY"
sym2 <- "ILF"
sym3 <- "EWZ"
sym4 <- "DIA"  
sym5 <- "EPP"
initDate <- "2010-01-01"
getSymbols(sym1,from=initDate)
getSymbols(sym2,from=initDate)
getSymbols(sym3,from=initDate)
getSymbols(sym4,from=initDate)
getSymbols(sym5,from=initDate)
print(length(get(sym1)))
print(length(get(sym2)))
print(length(get(sym3)))
print(length(get(sym4)))
print(length(get(sym5)))

df <- data.frame( dailyReturn(get(sym1)), dailyReturn(get(sym2)),
                  dailyReturn(get(sym3)), dailyReturn(get(sym4)),
                        dailyReturn(get(sym5)))
names(df) <- c(sym1,sym2,sym3,sym4,sym5)
print(round(cor(df),2))




--
View this message in context: http://r.789695.n4.nabble.com/Cross-correlation-problems-tp4663151p4663512.html
Sent from the Rmetrics mailing list archive at Nabble.com.