3

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.

4

4 回答 4

3

只是使用有什么问题rollapply()吗?apply.rolling()只是它的包装。

rollapply(managers, FUN = mean, width = 36, by.column = TRUE)
于 2017-08-22T12:45:38.700 回答
1

您可以对对象apply起作用:apply.rollingxts

apply(managers, 2, function(x){apply.rolling(x, FUN="mean", width=36)})
于 2017-08-22T12:40:20.707 回答
0

您可以与包中的组合lapply()rollapply()zoo多个列上执行滚动功能。在下面的代码中,我选择除第一个 ( select(-1)) 之外的所有列,并对剩余的列执行滚动功能。

dplyr()代码中:

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()
于 2018-03-01T08:48:48.907 回答
0

在它的帮助页面中apply.rolling说:

Details

Wrapper function for rollapply to hide some of the complexity of managing **single-column zoo objects**.

我认为这意味着目的是不要在具有多列的对象上使用它。它是一个简化的包装器,让您不必为rollapply().

于 2017-08-25T20:26:12.750 回答