Run cforest with controls = cforest_unbiased() usi

2019-07-07 11:22发布

问题:

I would like to run an unbiased cforest using the caret package. Is this possible?

tc <- trainControl(method="cv",
               number=f,
               index=indexList,
               savePredictions=T,
               classProbs = TRUE,
               summaryFunction = twoClassSummary)
createCfGrid <- function(len, data) {
    g = createGrid("cforest", len, data)
    g = expand.grid(.controls = cforest_unbiased(mtry = 5, ntree = 1000))
    return(g)
}
set.seed(1)
(cfMatFit <- train(as.factor(f1win) ~ .,
                   data=df,
                   method="cforest",
                   metric="ROC",
                   trControl=tc,
                   tuneGrid = createCfGrid))

The error is Error in as.character.default(<S4 object of class "ForestControl">) : no method for coercing this S4 class to a vector

This is because cforest_control() can not be coerced into a data frame. The function does work if I use:

...
g = expand.grid(.mtry = 5)
...

However if I want to change ntree, this has no effect:

...
g = expand.grid(.mtry = 5, .ntree = 1000)
...

This does not error like randomForest does.

回答1:

The grid should be a simple data frame with a column called .mtry. The code

 g = createGrid("cforest", len, data)

will generate that for you. If you want to specify ntree you just pass a controls object in as another argument to train but leave out mtry:

 mod <- train(Species ~ ., data = iris,
              method = "cforest",
              controls = cforest_unbiased(ntree = 10))

caret takes care of changing mtry for you.

Max