Reputation: 683
I am a new user of the randomForest
R package. I would like to run randomForest classifications for iterations of mtry =1,5,7
.
For example, I would like to run mtry =1
100 times and mtry=2
100 times. The output should show the out of bag errors for each run (100 results for mtry =1 and 100 results for mtry =2).
I only can write the code for 1 run, I do not know how to run the iteration of code with different values for mtry
.
rf <- randomForest(class_name ~ ., data=tr,ntree=1000,
importance=TRUE, proximity=TRUE, mtry=2)
Upvotes: 1
Views: 1814
Reputation: 93761
Here's a quick and dirty method using the built-in iris
data frame. The code below gives the final OOB error rate for each value of mtry
.
mtry = 1:4
oob = data.frame()
# Loop over each value of mtry and store result in a data frame
for (i in mtry) {
rf1 <- randomForest(Species ~ ., data=iris, ntree=100, mtry=i)
result = data.frame(mtry=i,
OOB=rf1[["err.rate"]][nrow(rf1[["err.rate"]]),"OOB"])
oob = rbind(oob, result)
}
oob
mtry OOB
OOB 1 0.04666667
OOB1 2 0.04000000
OOB2 3 0.04000000
OOB3 4 0.04000000
To keep all of the ntree
OOB errors from each value of mtry
, just change this:
OOB=rf1[["err.rate"]][nrow(rf1[["err.rate"]]),"OOB"])
To this:
OOB=rf1[["err.rate"]][ ,"OOB"])
Upvotes: 4