I want to understand the ROC curve. I need to create a ROC curve without using any package in R I have a dataset with two groups
k <- c(rep(0,23), rep(1,23))
x1 <- rnorm(46,0.963,0.26)
x2 <- rnorm(46,-0.006957,0.12)
x3 <- rnorm(46,2.033,1)
df <- data.frame(x1, x2, x3, k)
I am using linear discriminant analysis to make predictions
library(MASS)
md <- lda(k~., data = df)
df$pred <- predict(md, df)$class
What I need to do is generating different values of the threshold to identify how the true positive and false-negative change into a ROC curve. This was what I tried to do:
true_pos <- (sum(df$k==1 & df$pred==1)-cumsum(df$k==1 & df$pred==1))/sum(df$k==1)
ts <- df$pred == 1 & df$k==1
t_pos <- (sum(ts)-cumsum(ts))/sum(df$k==1)
fs <- df$pred == 1 & df$k == 0
f_pos <- (sum(fs)-cumsum(fs))/sum(df$k==0)
plot(f_pos, t_pos, type = 'l')
This doesn't give me what I need. The above picture is what I want.
Either way, here it is, step by step:
set.seed( 100 )
k <- c(rep(0,23), rep(1,23))
x1 <- rnorm(46,0.963,0.26)
x2 <- rnorm(46,-0.006957,0.12)
x3 <- rnorm(46,2.033,1)
x3[1:23] <- x3[1:23]*0.8 ## give it something to work with!!
df <- data.frame(x1, x2, x3, k)
library(MASS)
md <- lda(k~., data = df)
df$pred <- predict(md, df)$posterior[,2]
df.pos <- df %>% filter( k == 1 )
df.neg <- df %>% filter( k == 0 )
fpr <- function( threshold ) {
sum( df.neg$pred > threshold ) / nrow(df.neg)
}
tpr <- function( threshold ) {
sum( df.pos$pred > threshold ) / nrow(df.pos)
}
all.predictions <- c( 0, sort( df$pred ), 1 )
perf <- sapply( all.predictions, function(threshold) {
c( fpr(threshold), tpr(threshold) )
})
perf <- t(perf)
plot( NA, type="n", xlim=c(0,1), ylim=c(0,1), xlab="Fpr", ylab="Tpr", main="Manual Labor Builds Character" )
lines( perf )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.