r - Roc curve plotting am I doing this right? -



r - Roc curve plotting am I doing this right? -

i wrote own code create roc curve , calculate auc definition getting different results other packages , don't understand doing wrong here. here code. give thanks in advance.

roc.sort <- function(pred,true,vals,decreasing=false){ sorted.index <- sort(vals,index=true,decreasing=decreasing)$ix sort.pred <- pred[sorted.index] sort.true <- true[sorted.index] return(list(sort.pred,sort.true)) } roc.ploter <- function(pred,true,add.to.plot=false,color="black"){ index <- which(pred == 1) vals <- true[index] n.true <- 1/length(which(vals==1)) n.false <- 1/length(which(vals==0)) points <- data.frame(0,0) last.point <- c(0,0) for(i in 1:length(vals)){ if(vals[i] == 0){ next.point <- last.point + c(n.false,0) last.point <- next.point points <- rbind(points,next.point) } else { next.point <- last.point + c(0,n.true) last.point <- next.point points <- rbind(points,next.point) } } if(add.to.plot){ points(x=points[,1],y=points[,2],col=color,type="l") } else { plot(x=points[,1],y=points[,2],xlab="false positives",ylab="true positives", main= "roc",type="l",col=color) points(x=c(0,1),y=c(0,1),type='l') } return(points) } find.auc <- function(points){ x <- points[,1] y <- points[,2] result <- 0 n <- length(x) x.prev <- x[1] y.prev <- y[1] for(i in 2:n){ x.curr <- x[i] y.curr <- y[i] if(x.curr == x.prev){ y.prev <- y.curr } else { result <- result + y.prev*(x.curr-x.prev) x.prev <- x.curr } } return(result) }

and here working example.

library(liblinear) size <- 10000 truth <- rbinom(size,1,.9) dat <- cbind(truth+rnorm(size),truth*rnorm(size)) training.index <- (1:(floor(2*size/3))) model <- liblinear(data=dat[training.index,],labels=truth[training.index]) predicted <- predict(model,dat[-training.index,],decisionvalues=true) pred.labs <- predicted$predictions pred.scores <- predicted$decisionvalues sorted <- roc.sort(pred.labs,truth[-training.index],pred.scores,decreasing=true) pts <- roc.ploter(sorted[[1]],sorted[[2]]) auc <- find.auc(pts) print(auc)

and create plot rocr roc curve on , own roc curve on same plot.

pred <- prediction(pred.scores, truth[testing.index]) perf <- performance(pred, measure = "tpr", x.measure = "fpr") plot(perf, col=rainbow(10)) pts <- roc.ploter(sorted[[1]],sorted[[2]],add.to.plot=true)

r roc

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -