I am using ranger
to fit random forest. As evaluation metric, I am using roc-auc-score, by cvAUC
. After making predictions, when I try to evaluate the auc score, I get an error: Format of predictions is invalid. It couldn't be coerced to a list
. I think this is due to predictions consisting a Level
part which shows the unique levels for predictions. However, I could not get rid of that part. The minimum reproducible example is below, that throws the error:
library(caret)
install.packages("cvAUC")
library(cvAUC)
# Columns for training set
cat.column <- c("cat", "dog", "monkey", "shark", "seal")
num.column <- c(1,2,5,7,9)
class <- c(0,1,0,0,1)
train.set <- data.frame(num.column, cat.column, class)
# Columns for test set
cat.column <- c("cat", "elephant-shrew", "monkey", "monkey", "seal")
num.column <- c(1,11,5,6,8)
class <- c(1,0,1,0,1)
test.set <- data.frame(num.column, cat.column, class)
# Drop the target variable from the test set
target.test <- test.set["class"]
test.set <- test.set[,!names(test.set) %in% "class"]
# Fit random forest
rf = ranger(formula = as.factor(class) ~ . , data = train.set, verbose = FALSE)
# Get predictions
pred <- predict(rf, test.set)
predictions <- pred$predictions
# Get AUC score
auc <- AUC(as.factor(predictions), as.factor(unlist(target.test)), label.ordering = NULL)
cat(auc)