我有两个XTS对象;一个带有时间序列的十分位数,一个带有时间序列的收益,类似于下面的内容。如何创建按Decile分组的每个月的平均回报的时间序列(在Decile平均回报表中进行了描述)。
Decile Series
A B C D
20180331 7 3 3
20180430 4 2 2
20180531 1 8 3 8
20180630 2 4 4 1
20180731 3 9 9
20180831 6 4 9
Return Series
A B C D
20180331 0.50% -4.80% NA 1.60%
20180430 1.50% -5.00% NA 0.10%
20180531 -1.80% 1.00% 1.80% 0.10%
20180630 -1.08% 2.00% 1.75% -2.00%
20180731 NA 1.50% 3.02% -1.50%
20180831 NA 1.00% 0.80% 1.00%
Decile Average Return
Date Min 1 2 3 4 5 6 7 8 9 Max
20180331 -4.80% 0.00% 0.00% -1.60% 0.00% 0.00% 0.00% 0.50% 0.00% 0.00% 1.60%
20180430 -5.00% 0.00% -2.45% 0.00% 1.50% 0.00% 0.00% 0.00% 0.00% 0.00% 1.50%
20180531 -1.80% -1.80% 0.00% 1.80% 0.00% 0.00% 0.00% 0.00% 0.55% 0.00% 1.80%
20180630 -2.00% -2.00% -1.08% 0.00% 1.87% 0.00% 0.00% 0.00% 0.00% 0.00% 2.00%
20180731 -1.50% 0.00% 0.00% 1.50% 0.00% 0.00% 0.00% 0.00% 0.00% 0.76% 3.02%
20180831 0.80% 0.00% 0.00% 0.00% 0.80% 0.00% 1.00% 0.00% 0.00% 1.00% 1.00%
答案 0 :(得分:0)
我最终通过for循环找到了答案。 x等于十进制序列,y =返回序列
DecileCharacteristic <- function(x, y){
w <- xts(order.by=index(x))
for (i in 1:9){
r <- x == i
z <- y * r
z[z==0]<- NA
w <- merge(w,xts(apply(z,1,mean, na.rm=TRUE),order.by=index(x)))
}
w <- merge(w,xts(apply(z,1,max, na.rm=TRUE),order.by=index(x)))
w <- merge(w,xts(apply(z,1,min, na.rm=TRUE),order.by=index(x)))
colnames(w) <- c("1","2","3","4","5","6","7","8","9","max","min")
}