Create a ROC curve in R

Asked

Viewed 763 times

2

I need to plot a ROC curve in R, but I don’t know how to fix it.

cctrl2 <- trainControl(method = "cv", number = 10, classProbs = TRUE, savePredictions = TRUE)
modelNb <- train(Treino[, -5], Treino$TOTAL_PEDIDO, 
             method = "nb", 
             trControl = cctrl2)
test_class_pred_nb_probs <- predict(modelNb, Teste[, -5], type = "prob")
roc_nb = plot.roc(Teste[, 2],test_class_pred_nb_probs$alto, col='red')

However, the Test table$TOTAL_PEDIDO has 4 values (high, regular, low and min) and to plot the ROC curve the value must be atomic. So I did this.

aux<-Teste[which(Teste$TOTAL_PEDIDO == "alto"),]
test_class_pred_nb_probs <- predict(modelNb, aux[, -5], type = "prob")
roc_nb = plot.roc(aux[, 2],test_class_pred_nb_probs$alto, col='red')

And the message appears:

Error in sort.list(y) : 'x' must be atomic for 'sort.list'

Have you called 'Sort' on a list?

  • @Uzumakiartanis, translate? R asks if "called" the list sort function. And it was not called.

1 answer

1

cctrl2 <- trainControl(method = "cv", number = 10,  classProbs = TRUE, savePredictions = TRUE)

modelNb <- train(Treino[, -2], Treino$TOTAL_PEDIDO,
          method = "nb", 
          trControl = cctrl2)

test_pred_nb <- predict(modelNb, Teste[, -2])
test_pred_nb_probs <- predict(modelNb, Teste[, -2], type = "prob")
roc_nb = multiclass.roc(Teste[, 2],
 test_pred_nb_probs$min,
 add=TRUE,
 col='blue')
roc_nb
rs <- roc_nb[['rocs']]
plot.roc(rs[[1]])

auc(roc_nb)
  • The change in the code is the use of the multiclass.Roc function, because the TOTAL_PEDIDO attribute is multi-valued.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.