Reputation: 367
In zoo
package, while using rollapply
function, we get an logical option 'by.column = TRUE/FALSE
'. If TRUE
, FUN
is applied to each column separately.
However, in apply.rolling
function of PerformanceAnalytics
package I don't see any such option. While trying to the following:
require(PerformanceAnalytics)
data(managers)
apply.rolling(managers[,1:10,drop=FALSE], FUN="mean", width=36)
It give me calculation of only first column. Can someone please tell me how can I run this calculation over all the columns in the dataset.
Upvotes: 4
Views: 3264
Reputation: 9623
You can combine lapply()
with rollapply()
from the zoo
package to perform your rolling function over multiple columns. In the following code, I select all columns except the first (select(-1)
) and perform my rolling function over the remaining columns.
In dplyr()
code:
m3v %>%
select(-1) %>%
lapply(function(x){rollapply(x,
width=14,
FUN=function(x){sum(x, na.rm=T)},
partial = T, fill = 0
)}) %>%
as.data.frame()
Upvotes: 0
Reputation: 1493
In the help page for apply.rolling
it says:
Details
Wrapper function for rollapply to hide some of the complexity of managing **single-column zoo objects**.
I think that means the intent is to not use this on objects with multiple columns. It is a simplified wrapper which allows you to not have to type out all the various optional parameters for rollapply()
.
Upvotes: 0
Reputation: 2960
Is there something wrong with just using rollapply()
? apply.rolling()
is just a wrapper over it.
rollapply(managers, FUN = mean, width = 36, by.column = TRUE)
Upvotes: 4
Reputation: 1460
You can apply
function apply.rolling
on xts
object:
apply(managers, 2, function(x){apply.rolling(x, FUN="mean", width=36)})
Upvotes: 1