Для кривой ROC вы можете использовать пакет pROC
.Вам просто нужно убедиться, что predictions
равно as.numeric()
.
Здесь я покажу это на примере, я воспроизвел проблему двоичной классификации с данными iris
.
data <- iris
# change the problem to a binary classifier (setosa or not setosa)
data$bin_response <- as.factor(ifelse(data$Species=="setosa", 1, 0))
data <- data[, -5] # remove "Species"
set.seed(123)
train_test <- sample(150, 100, replace = F) # we sample casually 100 values for the train
# split train-test data
train <- data[train_test, ]
test <- data[-train_test, ]
Теперь модель и кривая:
# - model
library(randomForest)
rf_mod <- randomForest(bin_response ~ ., data=train)
# make pred on test data
predictions <- predict(rf_mod, newdata = test[, -5]) # note we remove the "bin_response" col
head(predictions) # lets look at them to check if it's fine
# 2 4 10 13 19 21
# 1 1 1 1 1 1
# Levels: 0 1
# now the ROC curve
library(pROC)
roc_result <- roc(test$bin_response, as.numeric(predictions))# Draw ROC curve.
plot(roc_result, print.thres="best", print.thres.best.method="closest.topleft")
![enter image description here](https://i.stack.imgur.com/RIjTi.png)