1
votes

I am having difficulty using dailyReturn function on an xts object with multiple return series.

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

How do I get dailyReturn to return the daily returns for multiple series in xts object?

2

2 Answers

2
votes

I would just use TTR::ROC instead.

> 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
2
votes

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.