R - 如何对来自其他包的函数(如 tseries 的 maxdrawdown)使用 rollapply? [英] R - How to use rollapply on functions from other package like maxdrawdown from tseries?

查看:33
本文介绍了R - 如何对来自其他包的函数(如 tseries 的 maxdrawdown)使用 rollapply?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 zoo 包中的 rollapply 和 tseries 包中的 maxdrawdown 函数:

I am trying to use rollapply from zoo package with function maxdrawdown from tseries package:

library(zoo)
library(tseries)
x <- c(1,2,4,6,7,10,8,6,5,4,6,7,8)
y <- zoo(x)
z <- rollapply(y,3,maxdrawdown,fill=NA,align="right")

但是我收到此错误消息:

However I get this error message:

Error in zoo(rval, index(x)[i]) : 
 "x" : attempt to define invalid zoo object

这是否意味着我不能在函数 maxdrawdown 上使用 rollapply?是否有其他解决方案来解决这个问题?

Does that mean I cannot use rollapply on function maxdrawdown? Is there alternative solution to work this out?

推荐答案

maxdrawdown 返回一个列表,但我们只想要该列表的 maxdrawdown 组件,所以试试这个:

maxdrawdown returns a list but we just want the maxdrawdown component of that list so try this:

rollapplyr(y, 3, function(x) maxdrawdown(x)$maxdrawdown, fill = NA)

这篇关于R - 如何对来自其他包的函数(如 tseries 的 maxdrawdown)使用 rollapply?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆