2017-01-04 46 views
2

我有一个年度数据的xts。我试图获得每年之间的排名相关性。例如,这是我的xts的子集:如何计算xts中行之间的滚动相关性?

> yearlyRanks[16:20,45:55] 
      35881 35880 42261 33445 46087 31486 8981 7687 8203 8202 41383 
2009-12-31  8  9 19  8 18 18 16 4 16 16 20 
2010-12-31  4  3 20  6 19  2 17 17 17 17 21 
2011-12-31  3  4 21  3 20  1 18 18 18 18 22 
2012-12-31  6  6 22  5 21 19 19 19 19 19  4 
2013-12-31  7  7  3  4 22 20 20 20 20 20  2 

我想知道每年的排名与上一年的相关性。 (试图告诉今年的排名如何以及去年的预测。)

我试图用这个:

yearlyCors <- rollapplyr(coredata(yearlyRanks), width = 2, function(x) cor(x[1], x[2], use = 'n')) 

但它永远,它似乎并没有工作。我认为这是因为我通过它一组2行,所以它想要返回2值,但我只期待1。(这是否有道理?)

任何想法,我将如何做到这一点?

编辑:

只要是明确的,这就是我想从子想:

> test <- yearlyRanks[16:20,45:55] 
> c(cor(test[1,], test[2,]), cor(test[2,], test[3,]), cor(test[3,], test[4,]), cor(test[4,], test[5,])) 
[1] 0.4679246 0.9930253 0.4854528 0.7193598 

编辑:

我要的是诊断()+ 1相关矩阵。这里是转置的相关矩阵:

> cor(t(test)) 
      2009-12-31 2010-12-31 2011-12-31 2012-12-31 2013-12-31 
2009-12-31 1.00000000 *0.4679246* 0.4716995 0.3722922 0.08786426 
2010-12-31 0.46792463 1.0000000 *0.9930253* 0.4654688 0.17192856 
2011-12-31 0.47169948 0.9930253 1.0000000 *0.4854528* 0.20237689 
2012-12-31 0.37229225 0.4654688 0.4854528 1.0000000 *0.71935975* 
2013-12-31 0.08786426 0.1719286 0.2023769 0.7193598 1.00000000 

你可以看到星号值是我想要的值。有没有办法访问diag + 1(如果你遵循)?

回答

3

这是你能得到你想要的结果的一种方法:

data <- "35881 35880 42261 33445 46087 31486 8981 7687 8203 8202 41383 
2009-12-31  8  9 19  8 18 18 16 4 16 16 20 
2010-12-31  4  3 20  6 19  2 17 17 17 17 21 
2011-12-31  3  4 21  3 20  1 18 18 18 18 22 
2012-12-31  6  6 22  5 21 19 19 19 19 19  4 
2013-12-31  7  7  3  4 22 20 20 20 20 20  2" 
dat <- read.table(text = data) 
yearlyRanks <- xts(dat, order.by = as.POSIXct(row.names(dat))) 

m_yearlyRanks <- t(coredata(yearlyRanks)) 
unlist(lapply(1:(NCOL(m_yearlyRanks) -1), function(i, x) cor(x[,i], x[, i + 1]), x = m_yearlyRanks)) 
# > unlist(lapply(1:(NCOL(m_yearlyRanks) -1), function(i, x) cor(x[,i], x[, i + 1]), x = m_yearlyRanks)) 
# [1] 0.4679246 0.9930253 0.4854528 0.7193598 

最后一行代码可能有点棘手。它可以被更冗长表示为(结果是相同的):

res <- vector("numeric", length = NCOL(m_yearlyRanks) -1) 
for (i in 1:(NCOL(m_yearlyRanks) -1)) { 
    res[i] <- cor(m_yearlyRanks[,i], m_yearlyRanks[, i + 1]) 
} 
# > res 
# [1] 0.4679246 0.9930253 0.4854528 0.7193598 

你在这个码错误:

yearlyCors <- rollapplyr(coredata(yearlyRanks), width = 2, function(x) cor(x[1], x[2], use = 'n')) 

源于x返回数据的一列(数字向量)至其中x [1]和x [2]是x的元素1和2,然后传递到corcor期待两个数据向量,但每次调用roll函数时都会得到2个标量。尝试使用browser调试该功能,它将立即变得很明显,问题是什么。例如尝试调用:

yearlyCors <- rollapplyr(coredata(GS), width = 20, function(x) { 
    browser() 
    cor(x[1], x[2], use = 'n') 
    } 
    ) 
1

我想我想通了。我只是把第一列关转置的相关矩阵,然后拿着诊断:

> test <- yearlyRanks[16:20,45:55] 
> tester <- cor(t(test), use = 'p') 
> tester 
      2009-12-31 2010-12-31 2011-12-31 2012-12-31 2013-12-31 
2009-12-31 1.0000000 0.6309825 0.6167215 0.7106686 0.6076932 
2010-12-31 0.6309825 1.0000000 0.9799418 0.4088352 0.2449624 
2011-12-31 0.6167215 0.9799418 1.0000000 0.3973902 0.2471984 
2012-12-31 0.7106686 0.4088352 0.3973902 1.0000000 0.7315524 
2013-12-31 0.6076932 0.2449624 0.2471984 0.7315524 1.0000000 
> xts(diag(tester[,-1]), order.by = as.Date(rownames(test))[-1]) 
       [,1] 
2010-12-31 0.6309825 
2011-12-31 0.9799418 
2012-12-31 0.3973902 
2013-12-31 0.7315524 

不过,我不相信这是做到这一点的韧皮方式,因为它似乎像它可能是低效。我正在计算一个我不需要的相关性的BUNCH。这很快,但如果有人想发布更有效的解决方案,请做!

(道歉的价值观改变了我做错之前,但没有打扰你们都应该得到的要点。!)

4

使用by.column=FALSE并确保功能是指行:

cor2 <- function(x) cor(x[1,], x[2,]) 
rollapplyr(coredata(yearlyRanks), 2, cor2, by.column = FALSE) 
## [1] 0.4679246 0.9930253 0.4854528 0.7193598 

我们也可以这样做:

z <- rollapplyr(as.zoo(yearlyRanks), 2, cor2, by.column = FALSE) 
as.xts(z) 

捐赠:

   [,1] 
2010-12-31 0.4679246 
2011-12-31 0.9930253 
2012-12-31 0.4854528 
2013-12-31 0.7193598 
+0

使用'rollapply' - thx进行分享 – FXQuantTrader

相关问题