reitcorr.R

newguy — Jul 31, 2013, 9:53 PM

require(quantmod)# make sure you have library installed
Loading required package: quantmod
getSymbols(c("XIC.TO","XRE.TO"), src='yahoo', from="2002-10-23")#dnld data
[1] "XIC.TO" "XRE.TO"
xic<-dailyReturn(XIC.TO[,6])#get adjusted column into daily returns
xre<-dailyReturn(XRE.TO[,6])
data<-merge.xts(xic,xre) # merge total return columns
names(data)=c("xic","xre")
#plot just the chart of underlying
plot.zoo(merge.xts(cumsum(xic)*100,cumsum(xre)*100),
         ylab="",xlab="",main="Total % Returns XIC & XRE",
         plot.type="single", col=c("red","blue"))
grid()
legend("topleft",lty=1,lwd=2,c("XIC","XRE"),col=c("red","blue"))

#get daily correlations
corrd<-rollapply(data[,c("xic", "xre")], 252,function(x) cor(x[,1], x[,2]), by = 1, by.column = FALSE)

# try monthly return correlation
xic<-monthlyReturn(XIC.TO[,6])#get adjusted column into monthly returns
xre<-monthlyReturn(XRE.TO[,6])
data<-merge.xts(xic,xre) # merge total return columns
names(data)=c("xic","xre")
corrm<-rollapply(data[,c("xic", "xre")], 12,function(x) cor(x[,1], x[,2]), by = 1, by.column = FALSE)

plot.zoo(na.omit(merge.xts(corrd,corrm)),
         ylab="",xlab="",main="Daily & Monthly Return 1 Year Rolling Correlation XIC vs. XRE",
         plot.type="single", col=c("red","blue"))
grid()
legend("topright",lty=1,lwd=2,c("daily","monthly"),col=c("red","blue"))

plot of chunk unnamed-chunk-1

plot of chunk unnamed-chunk-1

Free Web Hosting