【问题标题】:dailyReturn with xts objectdailyReturn 与 xts 对象
【发布时间】:2013-05-07 15:45:51
【问题描述】:

我在具有多个返回系列的 xts 对象上使用 dailyReturn 函数时遇到了困难。

a<-Cl(getSymbols("INTC",auto.assign=FALSE))
b<-Cl(getSymbols("IBM",auto.assign=FALSE))
a<-merge(a,b)
dailyReturn(a[,1]) #This works!
dailyReturn(a) #Only return the result for first series
apply(a,2, dailyReturn) 
#Error in array(ans, c(len.a%/%d2, d.ans), if (!all(vapply(dn.ans, is.null,  : 
length of 'dimnames' [1] not equal to array extent

如何让dailyReturn 返回 xts 对象中多个系列的每日收益?

【问题讨论】:

    标签: r xts quantmod


    【解决方案1】:

    我也更喜欢ROC,但如果你必须使用dailyReturn,你可以lapply覆盖列,然后cbind将它们重新组合在一起。

    > head(do.call(cbind, lapply(a, dailyReturn)))
               daily.returns daily.returns.1
    2007-01-03  0.0000000000     0.000000000
    2007-01-04  0.0402948403     0.010691889
    2007-01-05 -0.0033065659    -0.009052996
    2007-01-08 -0.0042654028     0.015191952
    2007-01-09  0.0009519277     0.011830131
    2007-01-10  0.0233000476    -0.011791746
    

    我使用了do.call,以便它可以处理任意数量的列。

    【讨论】:

      【解决方案2】:

      我会改用TTR::ROC

      > head(r <- ROC(a, type="discrete"))
                    INTC.Close    IBM.Close
      2007-01-03            NA           NA
      2007-01-04  0.0402948403  0.010691889
      2007-01-05 -0.0033065659 -0.009052996
      2007-01-08 -0.0042654028  0.015191952
      2007-01-09  0.0009519277  0.011830131
      2007-01-10  0.0233000476 -0.011791746
      

      【讨论】:

        猜你喜欢
        • 2020-09-01
        • 2016-12-13
        • 1970-01-01
        • 2011-10-29
        • 2013-05-27
        • 2011-11-06
        • 1970-01-01
        • 1970-01-01
        • 2019-06-15
        相关资源
        最近更新 更多