我似乎无法让adabag的bagging和predict.bagging工作。
在predict.bagging手册页面中,有以下内容:
library(adabag)
library(rpart)
data(iris)
names(iris)<-c("LS","AS","LP","AP","Especies")
sub <- c(sample(1:50, 25), sample(51:100, 25), sample(101:150, 25))
iris.bagging <- bagging(Especies ~ ., data=iris[sub,], mfinal=10)
iris.predbagging<- predict.bagging(iris.bagging, newdata=iris[-sub,])
iris.predbagging这是很好的工作。但是,当我在newdata中稍微更改predict.bagging时,它就停止工作了。
主要是,我不能真正删除或更改Especies列,这是奇怪的,因为这是一个我应该预测的!举个例子。
testdata <- iris[-sub, ]
result <- predict.bagging(iris.bagging, newdata=testdata)....this工作得很好,几乎是示例的副本。但是,这会产生一个错误。
testdata <- iris[-sub, -5] #this deletes the Especies column!
result <- predict.bagging(iris.bagging, newdata=testdata)但这也是
testdata <- iris[-sub, ]
testdata$Especies <- c("virginica") #sets up everything as virginica
result <- predict.bagging(iris.bagging, newdata=testdata)产生一个错误!
怎么一回事?我想用bagging来做一个分类器,但是我不能提前知道结果,这就不正确了。
编辑:好吧,它变得更奇怪了。
> testdata <- iris[150,]
> predict.bagging(iris.bagging, newdata=testdata) #all working
> testdata
LS AS LP AP Especies
150 5.9 3 5.1 1.8 virginica
> is(testdata)
[1] "data.frame" "list" "oldClass" "vector"
> testdata$Especies = "virginica"
> testdata
LS AS LP AP Especies
150 5.9 3 5.1 1.8 virginica #!same thing!
> is(testdata)
[1] "data.frame" "list" "oldClass" "vector" #the same object type!!!
>
> predict.bagging(iris.bagging, newdata = testdata)
Error in matrix(unlist(value, recursive = FALSE, use.names = FALSE), nrow = nr, :
length of 'dimnames' [2] not equal to array extent
In addition: Warning messages:
1: In is.na(e2) : is.na() applied to non-(list or vector) of type 'NULL'
2: In is.na(e2) : is.na() applied to non-(list or vector) of type 'NULL'
3: In is.na(e2) : is.na() applied to non-(list or vector) of type 'NULL'
4: In is.na(e2) : is.na() applied to non-(list or vector) of type 'NULL'
5: In is.na(e2) : is.na() applied to non-(list or vector) of type 'NULL'
6: In is.na(e2) : is.na() applied to non-(list or vector) of type 'NULL'
7: In is.na(e2) : is.na() applied to non-(list or vector) of type 'NULL'
8: In is.na(e2) : is.na() applied to non-(list or vector) of type 'NULL'
9: In is.na(e2) : is.na() applied to non-(list or vector) of type 'NULL'
10: In is.na(e2) : is.na() applied to non-(list or vector) of type 'NULL'
> 发布于 2012-01-24 08:17:05
哦,我明白了,有一点。
显然,在最后一个Especies列中,有一个因子,而不是字符串向量。因此,为了改变它,我必须这样分解它:
testdata$Especies <- factor(c("virginica"), levels=c("setosa", "versicolor", "virginica"))
如果我有一个没有最后一列的数据框架,我无论如何都要添加它,并且因子的级别必须与原始表的因子完全一样,实际的内容是无关的。
我不接受我的答案是迄今为止最好的,因为有人可以更好地解释原因。
https://stackoverflow.com/questions/8983060
复制相似问题