2012-01-24 32 views
4

我似乎無法得到adabagbaggingpredict.bagging工作。設置裝訂在R adabag

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 

這是好的和工作。但是,當我稍微更改predict.bagging中的newdata時,它停止工作。

主要是,我不能真正刪除或更改Especies列,這很奇怪,因爲那是我應該預測的那一列!一個例子。

testdata <- iris[-sub, ] 
result <- predict.bagging(iris.bagging, newdata=testdata) 

....這工作正常,幾乎是副本的例子。然而,這將產生一個錯誤

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 #!!!the 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' 
> 

回答

3

哦,我知道了,有點。

顯然,在最後的Especies列中,是一個因子,而不是字符串的向量。因此,爲了改變它,我有因式分解這樣的:

testdata$Especies <- factor(c("virginica"), levels=c("setosa", "versicolor", "virginica"))

如果我有沒有最後一列的數據幀,我不得不反正它添加和因子的水平必須完全像原始表格的因素一樣,實際內容是無關緊要的。

我不接受我的答案,因爲目前爲止,因爲有人可以更好地解釋原因。

+1

我還是不明白爲什麼會這樣,但我很高興其他人也有這個問題。通常,預測函數只需要新的輸入數據而不是響應。 – Max

+0

因此,創建虛擬響應變量行並將其解釋爲一個「因素」有訣竅嗎? –

+0

我近兩年沒有碰過R(幸運的是!!),所以我現在不能說真的:( –