Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in performance(pred, "tpr", "fpr")

Tags:

r

roc

I am trying to plot an ROC curve using the ROCR package in R but am stuck with the following error:

Error in performance(pred, "tpr", "fpr") : 
Assertion on 'pred' failed: Must have class 'Prediction', but has class 'prediction'

This is the code I am using to get to the performance segment call:

#convert actual and predicted labels to numeric
predicted<-as.numeric(as.character(test$Class))
actual<-as.numeric(as.character(test$overall_satisfaction))

#generate confusion matrix and label positive class
library(caret)
confusionMatrix(predicted,actual,positive="1")

The confusion matrix output looks just fine. However, in the next segment, performance function in ROCR throws an error and as a consequence, the ROC curve is not plotted.

#ROC curve
library(ROCR)
pred<-prediction(predicted, actual)
perf<-performance(pred,"tpr","fpr")
plot(perf,col="red", main="mlr_parameters_ROC")
abline(0,1, lty = 8, col = "grey")

I am unable to figure out what is wrong with the above code. Can somebody please help?

like image 476
veg2020 Avatar asked Mar 06 '23 16:03

veg2020


2 Answers

It appears that the above code was unable to access the performance function in ROCR package and this was the reason why I was seeing my error.

I kept everything else the same, but solved the problem as follows:

perf<-ROCR::performance(pred,"tpr","fpr")

The ROC curve plots just fine now!

like image 77
veg2020 Avatar answered Mar 08 '23 04:03

veg2020


I have encountered the same problem today. And here is my print(performance):

> print(performance)
function (pred, measures, task = NULL, model = NULL, feats = NULL) 
{
    if (!is.null(pred)) 
        assertClass(pred, classes = "Prediction")
    measures = checkMeasures(measures, pred$task.desc)
    res = vnapply(measures, doPerformanceIteration, pred = pred, 
        task = task, model = model, td = NULL, feats = feats)
    setNames(res, extractSubList(measures, "id"))
}
<bytecode: 0x0000000024d441e0>
<environment: namespace:mlr>

So it looks like mlr was the library that has a conflict on performance() with ROCR

like image 27
A story-teller Avatar answered Mar 08 '23 05:03

A story-teller