r - 具有自定义成本函数的插入符号模型
问题描述
我想训练一个具有案例加权误差函数的 Elastic Net 分类器。该函数需要接受一个额外的参数weights
,该参数允许不同的观察值在计算性能误差时携带不同的权重。这可以在插入符号中做到吗?
# Weighted RMSE function with weights argument.
weighted_rmse <- function(prediction, target, weights) {
sum(abs(prediction - target)) * weights
}
# Example data.
trainX <- iris[1:100, 1:4]
trainOutcome <- as.numeric(iris$Species[1:100])
fit <- train(
x = trainX, y = trainOutcome,
method = "glmnet",
trControl = trainControl(
method = "loocv",
selectionFunction = "oneSE",
summaryFunction = twoClassSummary # <- replace with something like weighted_rsme()
)
)