在R中装袋:如何使用带包功能的火车

时间:2016-01-28 00:51:21

标签: r machine-learning rpart

在R中,我试图使用具有列车功能的行李功能。我首先在简单的虹膜数据集上使用trainrpart作为分类树模型。现在我想用袋子功能创造一袋这样的10棵树。文档说聚合参数必须是从所有袋装模型中选择值的函数,因此我创建了一个名为agg的函数,它选择频率最高的字符串。但是,bag函数会出现以下错误:

Error in fitter(btSamples[[iter]], x = x, y = y, ctrl = bagControl, v = vars,  : 
  task 1 failed - "attempt to apply non-function"

这是我的完整代码:

# Use bagging to create a bagged classification tree from 10 classification trees created with rpart.
data(iris)

# Create training and testing data sets:
inTrain = createDataPartition(y=iris$Species, p=0.7, list=F)
train = iris[inTrain,]
test = iris[-inTrain,]

# Create regressor and outcome datasets for bag function:
regressors = train[,-5]
species = train[,5]

# Create aggregate function:
agg = function(x, type) {
  y = count(x)
  y = y[order(y$freq, decreasing=T),]
  as.character(y$x[1])
}

# Create bagged trees with bag function:
treebag = bag(regressors, species, B=10,
              bagControl = bagControl(fit = train(data=train, species ~ ., method="rpart"),
                                      predict = predict,
                                      aggregate = agg
                                      )
              )

这给出了上述错误消息。我不明白为什么拒绝agg功能。

1 个答案:

答案 0 :(得分:0)

来自?bag()

  

使用带火车的行李时,分类模型应使用type =   "概率"在预测函数内部,以便predict.train(对象,   newdata,type =" prob")将起作用。

所以我想你可能想尝试一下:

bagControl = bagControl(fit = train(data=train, species ~ .,
                                    method="rpart", type="prob"),
                        predict = predict,
                        aggregate = agg
                        )