An example of ROC curves plotting with ROCR

[This article was first published on Farmacokratia, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Decided to start githib with ROC curve plotting example. There is not a one ROC curve but several – according to the number of comparisons (classifications), also legend with maximal and minimal ROC AUC are added to the plot. ROC curves and ROC AUC were calculated with ROCR package.


What else should be added to the plot for ease of understanding?




# ROC curve(s) with ROCR
# Chupakhin Vladimir (chupvl@gmail.com)
# loading ROCR library
library("ROCR")
# loading active compounds, or compounds with label1
active <- read.table("sample.active", sep=",", header=FALSE)
# loading inactive compounds, or compounds with label2
inactive <- read.table("sample.inactive", sep=",", header=FALSE)
# binding them and converting to matrix because ROCR works with matrix data
target_pred <- as.matrix(rbind(active,inactive))
# because number of the colums should be the same - making additional param
ncol <- ncol(inactive)
# generating classes (1 for active, 0 for inactive, but it can be 1 and -1 - there is no difference)
class.active <- matrix(sample(1, (ncol(active)*nrow(active)), replace=T), ncol=ncol)
class.inactive <- matrix(sample(0, (ncol(inactive)*nrow(inactive)), replace=T), ncol=ncol)
# binding the classes
target_class <- rbind(class.active,class.inactive)
#target_class1 <- target_class[,1]
# calculating the values for ROC curve
pred <- prediction(target_pred, target_class)
perf <- performance(pred,"tpr","fpr")
# changing params for the ROC plot - width, etc
par(mar=c(5,5,2,2),xaxs = "i",yaxs = "i",cex.axis=1.3,cex.lab=1.4)
# plotting the ROC curve
plot(perf,col="black",lty=3, lwd=3)
# calculating AUC
auc <- performance(pred,"auc")
# now converting S4 class to vector
auc <- unlist(slot(auc, "y.values"))
# adding min and max ROC AUC to the center of the plot
minauc<-min(round(auc, digits = 2))
maxauc<-max(round(auc, digits = 2))
minauct <- paste(c("min(AUC) = "),minauc,sep="")
maxauct <- paste(c("max(AUC) = "),maxauc,sep="")
legend(0.3,0.6,c(minauct,maxauct,"\n"),border="white",cex=1.7,box.col = "white")
#
view raw gistfile1.r hosted with ❤ by GitHub

To leave a comment for the author, please follow the link and comment on their blog: Farmacokratia.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)