R中的装袋:如何使用带袋功能的列车
在R中,我尝试将bag功能与train功能结合使用。我首先使用R中的装袋:如何使用带袋功能的列车,r,machine-learning,rpart,R,Machine Learning,Rpart,在R中,我尝试将bag功能与train功能结合使用。我首先使用train和rpart对简单的虹膜数据集进行分类树模型。现在我想用bag函数创建一个包含10棵树的包。文档中说,aggregate参数必须是一个从所有打包模型中选择值的函数,因此我创建了一个名为agg,它选择频率最高的字符串。但是,行李功能会出现以下错误: Error in fitter(btSamples[[iter]], x = x, y = y, ctrl = bagControl, v = vars, : task 1
train
和rpart
对简单的虹膜数据集进行分类树模型。现在我想用bag函数创建一个包含10棵树的包。文档中说,aggregate参数必须是一个从所有打包模型中选择值的函数,因此我创建了一个名为agg
,它选择频率最高的字符串。但是,行李功能会出现以下错误:
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
函数。来自?bag()
当将行李与列车一起使用时,分类模型应使用类型=
predict函数中的“prob”,以便predict.train(对象,
newdata,type=“prob”)将起作用
所以我想你可能想试试:
bagControl = bagControl(fit = train(data=train, species ~ .,
method="rpart", type="prob"),
predict = predict,
aggregate = agg
)
我添加了type=“prob”,解决了之前收到的错误消息。然而,现在这行代码崩溃了,出现了一条新的错误消息:error in train.default(x,y,weights=w,…):Stopping