我想将递归特征消除与rfe()
并与模型选择一起进行调整trainControl()
使用该方法rf
(随机森林)。我想要的是 MAPE(平均绝对百分比误差),而不是标准的汇总统计数据。因此我尝试使用以下代码ChickWeight
数据集:
library(caret)
library(randomForest)
library(MLmetrics)
# Compute MAPE instead of other metrics
mape <- function(data, lev = NULL, model = NULL){
mape <- MAPE(y_pred = data$pred, y_true = data$obs)
c(MAPE = mape)
}
# specify trainControl
trc <- trainControl(method="repeatedcv", number=10, repeats=3, search="grid", savePred =T,
summaryFunction = mape)
# set up grid
tunegrid <- expand.grid(.mtry=c(1:3))
# specify rfeControl
rfec <- rfeControl(functions=rfFuncs, method="cv", number=10, saveDetails = TRUE)
set.seed(42)
results <- rfe(weight ~ Time + Chick + Diet,
sizes=c(1:3), # number of predictors from which should algorithm chose the best predictor
data = ChickWeight,
method="rf",
ntree = 250,
metric= "RMSE",
tuneGrid=tunegrid,
rfeControl=rfec,
trControl = trc)
代码运行没有错误。但是我在哪里可以找到 MAPE,我将其定义为summaryFunction
in trainControl
? Is trainControl
被执行还是被忽略?
我如何重写代码以进行递归特征消除rfe
然后调整超参数mtry
using trainControl
within rfe
并同时计算附加误差测量(MAPE)?