问题
如果我想计算的滚动我的每39股之间在其列stock_returns和market_return(独立XTS对象,与市场回报只有一列)与rollapply相关(XTS对象):
rolling_3yearcor <- rollapply(stock_returns,width=750,FUN=cor,y=market_return)
我得到这个错误:
Error in FUN(.subset_xts(data, (i - width + 1):i, j), ...) :
incompatible dimensions
即使我在market_return与子集的单柱
rolling_3yearcor <- rollapply(stock_returns,width=750,FUN=cor,y=market_return$market)
我得到的错误以及,即使它们具有相同的尺寸? (1个栏,同样的行数)。
我想什么有:
我想与股票的相关性的XTS对象[I]随着市场各39股票列在滚动750天窗口,而不是在stock_returns日常的回报。
不该rollapply做到这些?
EDIT 1:一天的问题数据样本向后移
Returns StockA
1997-01-03 -0.0054065397
1997-01-06 0.0024139001
1997-01-07 -0.0030085614
1997-01-08 0.0054329941
1997-01-09 -0.0005990317
1997-01-10 -0.0102205387
...
与代码:
ind <- market_return
ind[] <- seq_along(market_return)
rolling_3yearcor <- function(x,y,ind){
rollapply(ind,width=5,function(i) cor(x[i],y[i]))
}
rollcor_3year <- lapply(stock_returns,rolling_3yearcor,market_return,ind)
rollcor_3year <- as.data.frame(rollcor_3year,col.names=names(stock_returns))
colnames(rollcor_3year) <- colnames(stock_returns)
rollcor_3year <- as.xts(rollcor_3year)
给我:
dput(head(rollcor_3year$StockA.N))
structure(c(NA, NA, NA, NA, 0.30868769358199, 0.576490782746284
), .indexCLASS = c("POSIXct", "POSIXt"), tclass = c("POSIXct",
"POSIXt"), .indexTZ = "", tzone = "", class = c("xts", "zoo"), index =
structure(c(852246000,
852505200, 852591600, 852678000, 852764400, 852850800), tzone = "", tclass =
c("POSIXct",
"POSIXt")), .Dim = c(6L, 1L), .Dimnames = list(NULL, "StockA.N"))
然后用:
indexTZ(rollcor_3year) <- "UTC"
dput(head(rollcor_3year$StockA.N))
structure(c(NA, NA, NA, NA, 0.30868769358199, 0.576490782746284
), .indexCLASS = c("POSIXct", "POSIXt"), tclass = c("POSIXct",
"POSIXt"), .indexTZ = c(TZ = "UTC"), tzone = c(TZ = "UTC"), class = c("xts",
"zoo"), index = structure(c(852246000, 852505200, 852591600,
852678000, 852764400, 852850800), tzone = c(TZ = "UTC"), tclass =
c("POSIXct",
"POSIXt")), .Dim = c(6L, 1L), .Dimnames = list(NULL, "StockA.N"))
它给了我:
head(rollcor_3year$StockA.N)
1997-01-02 23:00:00 NA
1997-01-05 23:00:00 NA
1997-01-06 23:00:00 NA
1997-01-07 23:00:00 NA
1997-01-08 23:00:00 0.3086877
1997-01-09 23:00:00 0.5764908
使用rollapplyr
与所指示的功能和by.column = FALSE
。
# test data
stock_returns <- xts(anscombe[6:8], as.Date("2000-01-01") + seq(0, length=nrow(anscombe)))
market <- xts(anscombe[, 5], time(stock_returns))
x <- cbind(market, stock_returns)
rollapplyr(x, 5, function(x) cor(x[, 1], x[, -1]), by.column = FALSE)
赠送:
2000-01-01 NA NA NA
2000-01-02 NA NA NA
2000-01-03 NA NA NA
2000-01-04 NA NA NA
2000-01-05 0.6912899 -0.19831742 0.8437913
2000-01-06 -0.0904641 -0.08067339 0.3773026
2000-01-07 0.3714166 -0.05974574 0.3604551
2000-01-08 0.9013902 0.90672036 -0.6537459
2000-01-09 0.9059692 0.91388127 -0.7673776
2000-01-10 0.7996265 0.89299770 -0.7032847
2000-01-11 0.7812519 0.89427224 -0.6959074
问题是,你正在尝试计算不同长度的向量之间的相关性。尝试cor(1:10, 1:9)
直接看到这一点。 rollapply
只辊它的第一个参数,所以market_return$market
以其整体使用。
用于处理这种情况的一个方法是将滚过矢量的索引(假设两个原本相同的长度)。我没有访问您的数据,因此一些数据:
set.seed(2)
df1 <- as.data.frame(replicate(5, runif(10), simplify=FALSE))
names(df1) <- paste0("V", 1:5)
vec2 <- runif(10)
综观框架的第一列,就可以显示验证的概念:
rollapply(seq_along(vec2), 3, function(i) cor(df1$V1[i], vec2[i]))
# [1] 0.2873624 -0.8522555 -0.9859923 -0.6394554 -0.4626926 0.4939377 0.5590373 0.9994124
要轻松此应用到帧的所有列,我们可以做一个辅助函数:
rollcor <- function(v1,v2) {
rollapply(seq_along(v1), 3, function(i) cor(v1[i], v2[i]))
}
lapply(df1, rollcor, vec2)
# $V1
# [1] 0.2873624 -0.8522555 -0.9859923 -0.6394554 -0.4626926 0.4939377 0.5590373 0.9994124
# $V2
# [1] 0.79602807 0.16857013 -0.24970680 0.01997719 0.96922386 -0.99937633 -0.32920929
# [8] -0.34819538
# $V3
# [1] 0.78978134 -0.08632500 -0.13991114 -0.26078798 -0.05284222 0.24405994 -0.68231437
# [8] -0.48694537
# $V4
# [1] 0.9850739 0.9823811 0.9743629 0.8470096 0.7337313 -0.9617746 -0.7033091 -0.4968143
# $V5
# [1] -0.6696637 -0.8672182 -0.9074534 -0.7671002 -0.3954844 -0.9864078 -0.2806075 -0.5689732
编辑
既然你说这是一个时间序列,而我们仍然需要使用索引(而不是时间序列的载体本身),我们可以用两种方法保存的时间序列:
zoo:::rollapply.ts
在未修改的代码(在此之前编辑)。这略微气馁,因为它是靠一个未导出功能。我认为它一般是安全的,但它不是在长期运行良好的状态。ind <- vec2
ind[] <- seq_along(vec2)
rollapply(ind, 3, function(i) cor(df1$V1[i], vec2[i]))
rollcor <- function(v1,v2,ind) {
rollapply(ind, 3, function(i) cor(v1[i], v2[i]))
}
lapply(df1, rollcor, vec2, ind)