【问题标题】:Average of one XTS object, based off conditions met in second XTS object一个 XTS 对象的平均值,基于第二个 XTS 对象中满足的条件
【发布时间】:2019-03-19 10:46:13
【问题描述】:

我有两个 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%

【问题讨论】:

    标签: r xts


    【解决方案1】:

    我最终通过 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")
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-14
      • 1970-01-01
      • 2016-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-27
      相关资源
      最近更新 更多