Installing bags in R adabag

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] #this deletes the Especies column! result <- predict.bagging(iris.bagging, newdata=testdata) 

but this one

 testdata <- iris[-sub, ] testdata$Especies <- c("virginica") #sets up everything as virginica result <- predict.bagging(iris.bagging, newdata=testdata) 

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' > 
+4
source share
1 answer

Oh, I get a little.

Apparently, in the last column, Especies has as a factor, not a row vector. Therefore, to change it, I have to decompose it like this:

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

If I have a data frame without the last column, I have to add it anyway, and the factor levels should be exactly like the factor of the original table, the actual content does not matter.

I do not accept my answer as the best so far, because someone can better explain why.

+3
source

Source: https://habr.com/ru/post/1392622/


All Articles