简体   繁体   中英

Issues with tuneGrid parameter in random forest

I've been dealing with some extremely imbalanced data and I would like to use stratified sampling to created more balanced random forests

Right now, I'm using the caret package, mainly to for tuning the random forests. So I try to setup a tuneGrid to pass in the mtry and sampsize parameters into caret train method as follows.

mtryGrid <- data.frame(.mtry = 100),.sampsize=80)
rfTune<- train(x = trainX,
               y = trainY,
               method = "rf",
               trControl = ctrl,
               metric = "Kappa",
               ntree = 1000,
               tuneGrid = mtryGrid,
               importance = TRUE)

When I run this example, I get the following error

The tuning parameter grid should have columns mtry

I've come across discussions like this suggesting that passing in these parameters in should be possible.

On the other hand, this page suggests that the only parameter that can be passed in is mtry

Can I even pass in sampsize into the random forests via caret?

It looks like there is a bracket issue with your mtryGrid . Alternatively, you can also use expand.grid to give the different values of mtry you want to try. By default the only parameter you can tune for a random forest is mtry . However you can still pass the others parameters to train . But those will have a fix value an so won't be tuned by train . But you can still ask to use a stratified sample in train . Below is how I would do, assuming that trainY is a boolean variable according which you want to stratify your samples, and that you want samples of size 80 for each category:

mtryGrid <- expand.grid(mtry = 100) # you can put different values for mtry
rfTune<- train(x = trainX,
               y = trainY,
               method = "rf",
               trControl = ctrl,
               metric = "Kappa",
               ntree = 1000,
               tuneGrid = mtryGrid,
               strata = factor(trainY),
               sampsize = c(80, 80), 
               importance = TRUE)

I doubt one can directly pass sampsize and strata to train . But from here I believe the solution is to use trControl() . That is,

mtryGrid <- data.frame(.mtry = 100),.sampsize=80)
rfTune<- train(x = trainX,
               y = trainY,
               method = "rf",
               trControl = trainControl(sampling=X),
               metric = "Kappa",
               ntree = 1000,
               tuneGrid = mtryGrid,
               importance = TRUE)

where X can be one of c("up","down","smote","rose") .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM