1
votes

I tried creating a function for Ensemble of Ensemble modelling:

library(foreach)
library(randomForest)
set.seed(10)
Y<-round(runif(1000))
x1<-c(1:1000)*runif(1000,min=0,max=2)
x2<-c(1:1000)*runif(1000,min=0,max=2)
x3<-c(1:1000)*runif(1000,min=0,max=2)
all_data<-data.frame(Y,x1,x2,x3)
bagging = function(dataFile, length_divisor = 4, iterations = 100)
{
    fit = list()
    predictions = foreach(m = 1 : iterations, .combine = cbind) %do% 
    {
        dataFile$Y = as.factor(dataFile$Y)
        rf_fit = randomForest(Y ~ ., data = dataFile, ntree = 100)
        fit[[m]] = rf_fit
        rf_fit$votes[,2]
    }
    rowMeans(predictions)
    return(list(formula = as.formula("Y ~ ."), trees = fit, ntree = 100, class = dataFile$Y, votes = predictions))
}
final_model = bagging(all_data)
predict(final_model, TestData) # It says predict doesn't support final_model object
# Error in UseMethod("predict") : no applicable method for 'predict' applied to an object of class "list"

It says -

Error in UseMethod("predict") : no applicable method for 'predict' applied to an object of class "list".

I need the above function bagging to return an aggregated model object so that I can predict on new data set.

1
If you are attempting to make a new "bagging" function and want it returning a value that will have its own predict method, then you need to assign a class values returned by it and also create a predict.thatclass-function to process the results. There really is no default predict function. There are only class-specific methods.IRTFM

1 Answers

0
votes

Your bagging function just returns an arbitrary list. Predict looks to the class of the first parameter to know "the right thing" to do. I assume you want to predict from the randomForest objects stored inside the list? You can loop over your list with Map(). For example

Map(function(x) predict(x, TestData), final_model$trees)

(untested since you didn't seem to provide TestData)