【问题标题】:running randomForest loop and variable importance in R在 R 中运行 randomForest 循环和变量重要性
【发布时间】:2016-09-09 21:57:16
【问题描述】:

我想在 R 中运行 100 次 randomForest 回归,为每次运行获取变量重要性并将变量重要性的结果写入 csv 文件(包括变量重要性的 100 个结果)。这是我的代码及其错误:

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

如何解决?非常感谢。

【问题讨论】:

    标签: r random-forest


    【解决方案1】:

    有一些小事情,rbind 而不是 cbindresultresultsnames() 冲突、对未定义对象 imp 的索引等:

    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")
    

    【讨论】:

      猜你喜欢
      • 2015-07-25
      • 2018-05-06
      • 2017-12-23
      • 2014-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-10
      相关资源
      最近更新 更多