I want to automatically tune the Random Forest Model as my variables keeps on changing on real time basis. I am using the iris
dataset.
My code is
jk = iris
library(randomForest)
library(caret)
fgl.res <- tuneRF(lm[,-5], lm[,5], stepFactor=1.5)
o/p:
mtry = 2 OOB error = 5.33%
Searching left ...
Searching right ...
mtry = 3 OOB error = 4%
0.25 0.05
mtry = 4 OOB error = 5.33%
-0.3333333 0.05
Above I want that in fgl.res
code it automatically chooses the Species
column index ie 5 in iris
dataset and insert in fgl.res
code.
Then to use a first line of fgl.res
output(o/p) and takes value of "mtry = 2 OOB error = 5.33% "
and use it in random forest code, ie assign value to mtry
and oob.error
as shown below:
mod2<-randomForest(Species~., data=lm, ntree=50, mtry=2, oob.error=0.0533)
I have tried many ways but nothing work out on how to automatically insert value into code from fgl.res
output.
I dont know if I correctly understood your problem, but you might use this approach.
When you use tuneRF you have to choose mtry with the lowest OOB error. I use invisible(capture.output(...))
function to hide display any output in console after entering tuneRF function.
Example:
# load library
library(randomForest)
library(caret)
# data
data_iris = iris
# repeat the analysis
set.seed(4543)
# tuneRF
invisible(capture.output(fgl.res <- tuneRF(x = data_iris[,-5], y=
data_iris[,5], stepFactor=1.5)))
# choose the best mtry based on the lowest OOB error
best_mtry <- fgl.res[fgl.res[, 2] == min(fgl.res[, 2]), 1]
# choose the lowest OOB error
best_oob <- fgl.res[fgl.res[, 2] == min(fgl.res[, 2]), 2]
# caluclate RF
mod2<-randomForest(Species~., data=data_iris, ntree=50, mtry=best_mtry,
oob.error=best_oob)
When you want to just extract a first line of fgl.res output and take value of mtry and OOB error, you have to use:
# choose the best mtry based on the lowest OOB error
best_mtry <- fgl.res[1, 1]
# choose the lowest OOB error
best_oob <- fgl.res[1, 2]
I know that you posted this question 10 months ago, but maybe this approach would be useful for other users.
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.