【发布时间】:2015-03-20 11:25:08
【问题描述】:
我想使用自定义性能指标来使用caret. 训练模型使用清晰的文档here,我能够创建一个新的性能指标。但是,我想将每个预测的附加信息传递给下面的 performance.metric 函数。我看到data 有pred 和obs 的列,它们分别是预测数据和观察数据。我还看到可以添加 weights 和 classProbs 作为文档明确指定的。是否可以为每个预测传递额外的信息?
具体来说,我想使用模型生成的预测序列中的美元回报来评估资产预测算法的性能。我的预测 (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 对象添加列,以便这个函数有必要的数据吗?
【问题讨论】: