简体   繁体   中英

running randomForest loop and variable importance in R

I would like to run 100 times randomForest regression in R, get variable importance for each running and write the result of variable importance as csv file(including 100 results for variable importance). This is my code and its error:

result<-data.frame(IncMSE="%IncMSE", IncNodePurity="IncNodePurity")
for (i in 1:3){
imp[i]<- importance(randomForest(train[,1:11], train[,12], data = train,importance = TRUE, ntree =5000, proximity = TRUE, mtry=3))
results<-cbind(result,imp[i])  
}
write.csv(results,"D:/vari.csv")

Warning messages:
In imp[i] <- importance(randomForest(train[, 1:11], train[, 12],  :
number of items to replace is not a multiple of replacement length

How to fix it? Many thanks.

There were a few small things, rbind instead of cbind , result vs. results , a names() conflict, indexing on the undefined object imp , etc:

data("mtcars")
train <- mtcars
require(randomForest)

result <- data.frame()
for (i in 1:3){
  imp    <- importance(randomForest(train[,2:10], y = train[,1], data = train,importance = TRUE, ntree =5000, proximity = TRUE, mtry=3))
  result <- rbind(result, imp)  
}
write.csv(result, "D:/vari.csv")

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