I cannot get adabag bagging and predict.bagging to work.
The predict.bagging page has the following:
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
It works well. However, when I change newdata to predict.bagging , it stops working.
Basically, I really canโt delete or change the Especies column, which is weird, since this is the one I have to predict! Example.
testdata <- iris[-sub, ] result <- predict.bagging(iris.bagging, newdata=testdata)
.... this works great and is almost a copy of the example. However this causes an error
testdata <- iris[-sub, -5]
but this one
testdata <- iris[-sub, ] testdata$Especies <- c("virginica")
gives an error message!
What's happening? I want to make a classifier using bagging , but I cannot know in advance the results that defeat the point.
edit: Ok, this is even weird.
> 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' >