Search code examples
rclassificationaucprecision-recall

Easy way of counting precision, recall and F1-score in R


I am using an rpart classifier in R. The question is - I would want to test the trained classifier on a test data. This is fine - I can use the predict.rpart function.

But I also want to calculate precision, recall and F1 score.

My question is - do I have to write functions for those myself, or is there any function in R or any of CRAN libraries for that?


Solution

  • The ROCR library calculates all these and more (see also http://rocr.bioinf.mpi-sb.mpg.de):

    library (ROCR);
    ...
    
    y <- ... # logical array of positive / negative cases
    predictions <- ... # array of predictions
    
    pred <- prediction(predictions, y);
    
    # Recall-Precision curve             
    RP.perf <- performance(pred, "prec", "rec");
    
    plot (RP.perf);
    
    # ROC curve
    ROC.perf <- performance(pred, "tpr", "fpr");
    plot (ROC.perf);
    
    # ROC area under the curve
    auc.tmp <- performance(pred,"auc");
    auc <- as.numeric([email protected])
    
    ...