使用mtry运行randomForest

时间:2019-07-07 14:12:25

标签: r random-forest

我在研究randomforest时遇到这种错误。

library(caret)
library(randomForest)
rf_model_housing <-train(SalePrice ~., # Standard formula notation
data=train_housing[,-1],
method="rf",
nodesize= 10,
mtry= 5,            
ntree = 500,
trControl=trainControl(method="repeatedcv", number=2,repeats=1),
tuneGrid = expand.grid(mtry = c(123)))
  

错误:正在停止   另外:警告消息:   1:Fold1.Rep1的模型拟合失败:randomForest.default(x,y,mtry = param $ mtry,...)中的mtry = 123错误:     形式参数“ mtry”与多个实际参数匹配    2:Fold2.Rep1的模型拟合失败:randomForest.default(x,y,mtry = param $ mtry,...)中的mtry = 123错误:     形式参数“ mtry”与多个实际参数匹配    3:在nominalTrainWorkflow中(x = x,y = y,wts =权重,info = trainInfo,:重新采样的性能度量中缺少值。

1 个答案:

答案 0 :(得分:0)

根据插入符号here的randomForest文档,您使用了太多的tunig参数,因此需要将它们放在data.frame网格中(请参阅here for one example)。 此外,您定义了2倍mtry(用于拆分的预测变量数)。我建议尝试类似的东西:

rf_control <- trainControl(method="repeatedcv", number=5, repeats=5)

rf_grid <-  expand.grid(mtry = c(floor(sqrt(ncol(x)) / 10),
                        floor(sqrt(ncol(x)) / 5),
                        floor(sqrt(ncol(x)))
)

set.seed(123)
rf_fit <- train(SalePrice ~., 
                 data = train_housing[,-1], 
                 method = "rf", 
                 trControl = rf_control,
                 tuneGrid = rf_grid)
rf_fit