R:组合使用 Caret 生成的森林时出现 randomForest 错误

R: randomForest error when combining forest produced using Caret

我正在尝试使用 randomForest 'combine' 函数在 R 中组合多个随机森林,但不能使用 'caret' 包包装器的 randomForest 输出。

返回的对象具有 class 'train',而不是 'randomForest' - 有什么想法吗?

我不清楚如何在 运行ning caret 的 'train' 函数之后检索 randomForest 对象,我认为它应该包含它们。

这样做的原因是我正在 运行 对一个大数据集进行分析,该数据集太大而无法在我的硬件上 运行 randomForest。

为了使用可用内存管理数据集,我首先生成了许多较小的森林,然后使用 rf 'combine' 函数将它们组合起来。结果很好,我想对 caret 的输出做同样的事情。

问题代码概述(我宁愿使用apply函数而不是循环,但我对这个例子的应用也不清楚)

trainData.Slices <- list() #My data is 'sliced' into manageable pieces, each one being run through randomForest individually before being recombined 
trainData.Slices[[1]] <-data.frame("y.val" = runif(1000, 0, 1), pred1 = runif(1000, 1, 5), pred1 = runif(1000, 10, 20))
trainData.Slices[[2]] <- data.frame("y.val" = runif(1000, 0, 1), pred1 = runif(1000, 1, 5), pred1 = runif(1000, 10, 20))
trainData.Slices[[3]] <- data.frame("y.val" = runif(1000, 0, 1), pred1 = runif(1000, 1, 5), pred1 = runif(1000, 10, 20))


slicesRun <- length(trainData.Slices) #Specify how many slices to cut the data into for individual processing
forestList <- list() #The list into which each small forest will be added
nVar <- length(trainData.Slices[[1]])


for (i in 1:slicesRun) {
trainData <- trainData.Slices[[i]]

#The standard randomForest code works perfectly
forestList[[i]] <- randomForest(x=trainData[,-1], y=trainData[,1],ntree=200, importance=TRUE, proximity=TRUE)
print(class(forestList[[i]])) 

#caret is returning 'train' objects rather than randomForest objects
forestList_caret[[i]] <- train(y=trainData[,1], x=trainData[,-1], method="rf", trControl=trainControl(method="cv", number=5), prox=TRUE, allowParallel=TRUE)
print(class(forestList_caret[[i]])) 
#How can the rf objects be returned instead, or train objects combined?

} 


rf.all <- do.call("combine",forestList) #Combine the forests into one
rf.all_caret <- do.call("combine",forestList) #Combine the forests into one    

我也遇到了这个问题,并从 post 中发现了以下内容:

randomForest 对象在 $finalModel 中,因此在您的示例中是 forestList_caret[[i]]$finalModel。您的代码适用于以下更改:

第 8 行到 forestList <- forestList_caret <- list()

第 28 行到 rf.all_caret <- do.call("combine",forestList_caret)

在第 22 行之后插入:

forestList_caret[[i]] <- forestList_caret[[i]]$finalModel print(class(forestList_caret[[i]]))

存储 $finalModel 对象让你可以在最后组合它们,结果是一个 class randomForest 的对象。检查:

print(class(rf.all_caret))