简体   繁体   English

R:在随机森林中调整 mtry 时出错(回归)

[英]R: Error tuning mtry in random forest (regression)

I have the following code to tune the mtry hyperparameter of a random forest regression model:我有以下代码来调整随机森林回归 model 的 mtry 超参数:

set.seed(42)

mtry <- 1:10

# Define train control
trControl <- trainControl(method = "cv",
                          number = 10,
                          search = "grid")

for (i in mtry) {
  rf_random <- train(Price.Gas~., data=data_train,
                 method = "rf",
                 mtry = i,
                 metric = "RMSE",
                 trControl = trControl)
}

However, I get the error (that actually repeats itself for different values of mtry) :但是,我得到了错误(实际上对于不同的 mtry 值会重复)

model fit failed for Fold01: mtry= 2 Error in randomForest.default(x, y, mtry = param$mtry, ...) : 
  formal argument "mtry" matched by multiple actual arguments

How can I make this work to test different mtry values?我怎样才能使这项工作来测试不同的 mtry 值?

by default caret would tune the mtry over a grid, see manual so you don't need use a loop, but instead define it in tuneGrid= :默认情况下,插入符号会在网格上调整 mtry,请参阅 手册,因此您不需要使用循环,而是在tuneGrid=中定义它:

library(caret)
set.seed(42)

data_train = data.frame(Price.Gas = rnorm(100),matrix(rnorm(1000),ncol=10))

trControl <- trainControl(method = "cv",number = 10)

rf_random <- train(Price.Gas~., data=data_train,
                   method = "rf",
                   tuneGrid = data.frame(mtry = 1:10),
                   metric = "RMSE",
                   trControl = trControl)

Random Forest 

100 samples
 10 predictor

No pre-processing
Resampling: Cross-Validated (10 fold) 
Summary of sample sizes: 89, 90, 91, 89, 91, 90, ... 
Resampling results across tuning parameters:

  mtry  RMSE       Rsquared   MAE      
   1    0.8556649  0.2122988  0.6921878
   2    0.8458829  0.2102749  0.6808978
   3    0.8518204  0.1975061  0.6909111
   4    0.8451160  0.1918390  0.6871511
   5    0.8386129  0.2037676  0.6808157
   6    0.8476718  0.1949056  0.6889514
   7    0.8434816  0.2082844  0.6833892
   8    0.8447137  0.1979602  0.6860908
   9    0.8419739  0.1960369  0.6825207
  10    0.8533284  0.1876459  0.6892574

RMSE was used to select the optimal model using the smallest value.
The final value used for the model was mtry = 5.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为随机森林回归同时调整 mtry 和树数? - How to tune mtry and number of trees simultaneously for a Random Forest Regression? 为随机森林回归模型设置 ntree 和 mtry 的值 - setting values for ntree and mtry for random forest regression model 错误:调整参数网格应该有列 mtry、SVM 回归 - Error : The tuning parameter grid should have columns mtry, SVM Regression 使用 R 中的 tidymodel 框架进行随机森林超参数调整的错误 - error in random forest hyperparam tuning using tidymodel framework in R 有没有办法在Caret的火车功能中使用随机森林和PCA预处理来启用mtry的网格调整? - Is there a way to enable grid tuning of mtry using Random Forest and PCA pre-processing in train function from Caret? R:随机森林回归 model 中的错误训练数据 - R: Error training data in random forest regression model 在随机森林mtry图中添加点 - Add points in a Random Forest mtry plot 在Caret交叉验证随机森林方法中的mtry - mtry in Caret cross validation Random Forest method 使用插入符号在随机森林中显式设置 ntree 和 mtry - Setting ntree and mtry explicitly in Random Forest with caret 随机森林、SVM 和多项 Logistic 回归与 R - Random Forest, SVM and Multinomial Logistic Regression with R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM