【问题标题】:sapply with Performance Analytics datasapply 与性能分析数据
【发布时间】:2014-01-09 16:22:00
【问题描述】:

我有一个由返回时间序列组成的数据框,其中包含以下列

date x1 x3 x8 x11

x.R 是我的数据框,包含返回值

我想在性能分析工具中使用 findDrawdowns 方法并将其应用于每个时间序列。我想将结果存储在一个列表中,以便我可以访问 findDrawdowns 的所有输出

sapply(x.R,  sortDrawdowns(findDrawdowns))

上面的命令产生下面的。不知道如何访问这些值。非常感谢任何帮助!

             x1          x3         x8         x11      
return       Numeric,47 Numeric,47 Numeric,47 Numeric,49
from         Numeric,47 Numeric,47 Numeric,47 Numeric,49
trough       Numeric,47 Numeric,47 Numeric,47 Numeric,49 
to           Numeric,47 Numeric,47 Numeric,47 Numeric,49 
length       Numeric,47 Numeric,47 Numeric,47 Numeric,49 
peaktotrough Numeric,47 Numeric,47 Numeric,47 Numeric,49
recovery     Numeric,47 Numeric,47 Numeric,47 Numeric,49 

【问题讨论】:

  • 尝试在sapply(..., simplify = TRUE) 中设置simplify = TRUE 或直接使用lapply 获取列表,或者您可能想查看plyr 包以获取合适的格式。也努力改进您的问题,将其设为reproducible

标签: r sapply performanceanalytics


【解决方案1】:

您需要一个嵌套的sapply,因为sortDrawdowns 返回所有无法以二维显示的回撤(即您上面表格中的每个单元格都包含所有回撤。这是一个包含数据的解决方案:

# Make up data
len=29
data <- data.frame(
  x1=rnorm(len, 0, .05),
  x3=rnorm(len, 0, .05),
  x8=rnorm(len, 0, .05),
  x11=rnorm(len, 0, .05)
)    
rownames(data) <- as.character(as.Date("2013-12-01") - (len:1))

# Get worst drawdowns    
sapply(
  names(data),    # for each entry in df
  function(x) {   
    sapply(       # cycle through each item in a drawDown object
      sortDrawdowns(findDrawdowns(data[, x, drop=F])),  # get drawdows sorted
      `[[`, 1                                           # extract 1st item
    )
  } 
)
#                      x1         x3         x8        x11
# return       -0.1887651 -0.3425831 -0.1592202 -0.2928802
# from         17.0000000  5.0000000 16.0000000  1.0000000
# trough       20.0000000 24.0000000 27.0000000 16.0000000
# to           25.0000000 30.0000000 30.0000000 30.0000000
# length        9.0000000 26.0000000 15.0000000 30.0000000
# peaktotrough  4.0000000 20.0000000 12.0000000 16.0000000
# recovery      5.0000000  6.0000000  3.0000000 14.0000000

【讨论】:

    猜你喜欢
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    • 2018-09-02
    • 2015-06-11
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    • 2019-08-16
    相关资源
    最近更新 更多