2016-02-02 94 views
2

我試圖多次隨機森林中的R結合,採用隨機森林「結合」函數生成,但不能從「插入符號」包包裝輸出隨機森林這麼做。R:結合森林時隨機森林的錯誤使用插入符號

返回的對象具有類「火車」,而不是「隨機森林」 - 任何想法嗎?

我不清楚如何檢索運行插入符號的「訓練」功能,我認爲應該包含這些後隨機森林的對象。

這樣做的原因是,我運行在一個大的數據集分析,太大了,與我的硬件上運行隨機森林。

要管理和可用內存我已經第一次產生許多小森林數據集,然後使用RF「合併」功能將它們結合在一起。結果是好的,我想對照片中的輸出做同樣的事情。

問題代碼的概要(我寧願使用一個比一個循環中使用的功能,但我還不清楚應用到本示例)

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  
+2

歡迎堆棧溢出 - 見[此FAQ](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)有關提供可重現示例的提示。 – nrussell

+0

謝謝nrussel。現在編輯。 – Jernau

回答

1

我也有這個問題,並發現了以下從這個職位:Error when using predict() on a randomForest object trained with caret's train() using formula

randomForest對象在$finalModel,所以forestList_caret[[i]]$finalModel在你的例子。您的代碼工作有以下變化:

線線22後8〜forestList <- forestList_caret <- list()

線28 rf.all_caret <- do.call("combine",forestList_caret)

插入:

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

存放$finalModel對象讓你可在端部將它們結合起來,並且將結果與類的對象。請與:

print(class(rf.all_caret))