[英]Bagging in R: How to use train with the bag function
In R, I am trying to use the bag function with the train function. 在R中,我尝试将bag函数与train函数一起使用。 I start with using
train
and rpart
for a classification tree model, on the simple iris data set. 我首先在简单的虹膜数据集上使用
train
和rpart
进行分类树模型。 Now I want to create a bag of such 10 trees with the bag function. 现在,我要使用bag函数创建一个由10棵树组成的bag。 The documentation says that the aggregate parameter must be a function to choose a value from all bagged models, so I created one called
agg
, which chooses the string of greatest frequency. 该文档说,聚合参数必须是从所有袋装模型中选择一个值的函数,因此我创建了一个名为
agg
的函数,该函数选择频率最高的字符串。 However, the bag function gives the following error: 但是,bag函数给出以下错误:
Error in fitter(btSamples[[iter]], x = x, y = y, ctrl = bagControl, v = vars, :
task 1 failed - "attempt to apply non-function"
Here is my complete code: 这是我完整的代码:
# 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
)
)
This gives the error message stated above. 这给出了上述错误信息。 I don't understand why it rejects the
agg
function. 我不明白为什么它拒绝
agg
函数。
from ?bag()
来自
?bag()
When using bag with train, classification models should use type = "prob" inside of the predict function so that predict.train(object, newdata, type = "prob") will work.
当使用带火车的包时,分类模型应在预测函数内部使用type =“ prob”,以便predict.train(object,newdata,type =“ prob”)将起作用。
So I guess you might want to try: 因此,我想您可能想尝试:
bagControl = bagControl(fit = train(data=train, species ~ .,
method="rpart", type="prob"),
predict = predict,
aggregate = agg
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.