r - How to calculate a correlation table using the shortest lifespan per pair instead of per universe? -
the below r-code calculates correlation matrix , writes csv using lifespan of youngest asset in universe, here eem. how modify code using shortest lifespan each asset pair?
require(quantmod) require(performanceanalytics) symbols <- c("dbc", "eem", "efa", "gld", "iwm", "iyr", "spy", "tlt") getsymbols(symbols, from="1900-01-01") prices <- list() for(i in 1:length(symbols)) { prices[[i]] <- ad(get(symbols[i])) } prices <- do.call(cbind, prices) colnames(prices) <- gsub("\\.[a-z]*", "", colnames(prices)) returns <- return.calculate(prices) returns <- na.omit(returns) result <- table.correlation(returns, returns) result <- result[,1] cormatrix <- matrix(result, nrow=length(symbols), ncol=length(symbols), byrow=t) cormatrix <- round(cormatrix, digits=2) dimnames(cormatrix) = list(colnames(prices), colnames(prices)) write.csv(cormatrix, file="correlationtable.csv") print(cormatrix) [edit:]
to clarify pair wise approach:
- use eem's lifespan dbc:eem pair, since eem later introduced dbc
- use tlt's lifespan spy:tlt pair, since tlt later introduced spy
lifespan: number of trading days (data points) since asset's inception date, i.e. eem introduced on feb 6, 2006.
to more precise: since correlations calculated daily returns, calculation period should length - 1
return: today's adjusted close divided yesterday's adjusted close.
Comments
Post a Comment