[R-sig-Finance] Rolling correlations with zoo object

jladekarl at worldbank.org jladekarl at worldbank.org
Mon May 22 16:23:25 CEST 2006


I'm a novice user of R so, please, excuse me for asking a basic question I hope
there is a basic answer to:

I have tried to created a correlation matrix using a "zoo object".  I don't have
any issues using the cor("zoo data") function, but when I try to expand the
process by introducing rolling correlations I run into problems. I have created
a workaround using "rapply" specifying the exact variables to use, rather than
the general zoo object, but it's not very practical solution for large matrixes.
Any hints?

Jeppe

##Connect to Bloomberg through session "conn"
conn <- blpConnect ()

##download currency data for the last 3 years into a zoo object called "data"
data <- blpGetData(conn, c("EUR Curncy", "CHF Curncy"), "PX Last",
start=as.chron(Sys.time() - 86400 * (3*365)))

##End Bloomberg Session "conn"
blpDisconnect(conn)

#plot running correlation estimated by last 30 observations rolling by "1"
EUR.chf.cor <- rapply(data[,c("EUR CURNCY", "CHF CURNCY")], 30,
function(x) cor(x[,1], x[,2]), by = 1, by.column = FALSE)
plot(EUR.chf.cor)



More information about the R-SIG-Finance mailing list