【问题标题】:Pass Additional Information to Alternative Performance Metric in R and caret将附加信息传递给 R 和插入符号中的替代性能指标
【发布时间】:2015-03-20 11:25:08
【问题描述】:

我想使用自定义性能指标来使用caret. 训练模型使用清晰的文档here,我能够创建一个新的性能指标。但是,我想将每个预测的附加信息传递给下面的 performance.metric 函数。我看到datapredobs 的列,它们分别是预测数据和观察数据。我还看到可以添加 weightsclassProbs 作为文档明确指定的。是否可以为每个预测传递额外的信息?

具体来说,我想使用模型生成的预测序列中的美元回报来评估资产预测算法的性能。我的预测 (data$pred) 是资产的每日变化。要获得每天的 DollarReturn,我需要传入资产的每日变化。我不知道如何传递assetChange 对象的信息。

这是性能指标:

performance.metric = function(data, lev= NULL, model = NULL,
                              investment = 20000){

  if (!all(levels(data[, "pred"]) == levels(data[, "obs"]))) 
    stop("levels of observed and predicted data do not match")

  #custom performance metric
  assetChange = #this should be a vector of length nrows(data)
    #with the percentage change for the asset each day

  percReturn = ifelse(data[,"obs"] == data[, "pred"], abs(assetChange), -abs(assetChange) )
  #the strategy involves buying when predicting to increase and selling when predicted to decrease
  #so when the prediction is right, it gets the abs of the percent change and else loses that amount

  dollarReturn = rep(0, nrow(data))
  dollarReturn[1] = investment*percReturn[1]
  for (i in 2:length(dollarReturn)){
    dollarReturn[i] = dollarReturn[i-1]*percReturn[i]
  }

  out <- c(dollarReturn)
  names(out) <- c("dollarReturn")
  out
}

我可以想象一种(hackish)方式通过data 中的weights 列传递信息,但更一般地说,是否可以从performance.metric 外部向data 对象添加列,以便这个函数有必要的数据吗?

【问题讨论】:

    标签: r r-caret


    【解决方案1】:

    不,我看不到这样做的简单方法。我想避免重构函数以传递辅助信息,因为这可能会破坏向后兼容性。

    但是,我添加了 an issue to github,这可能会有所帮助。暴露给用于计算性能的函数的数据暴露了观察结果、预测值和类别概率的列。我还将传入行索引,以便将保留值绑定回原始数据,并让词法范围来完成其余的工作。

    如果您创建了一个综合示例并将其添加到问题中以便我们可以测试解决方案,将会有所帮助。

    最大

    【讨论】:

    • 感谢您的回复!是的,对于此类性能指标,行索引应该足够了。我将处理一个综合示例,并在时间允许的情况下添加到 github 上的问题。
    猜你喜欢
    • 2014-05-28
    • 1970-01-01
    • 1970-01-01
    • 2012-03-16
    • 1970-01-01
    • 1970-01-01
    • 2020-10-12
    • 2015-03-30
    • 1970-01-01
    相关资源
    最近更新 更多