1

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

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 对象中返回多个系列的每日收益?

4

2 回答 2

2

I prefer ROC also, but if you must use dailyReturn, you can lapply over the columns and cbind them back together.

> 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

I used do.call so that it will work with any number of columns.

于 2013-05-07T16:37:09.333 回答
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
于 2013-05-07T15:55:37.077 回答