I have a data frame with two classes. I want to find the true positive and false positive rate and then plot the ROC curve.
I tried this:
new <- data.frame(ytrue=c(1,0,1,1,0,0,1,0,1,0),
ypred=c(0.98,0.94,0.86,0.74,0.73,0.64,0.53,0.39,0.34,0.31))
new
ytrue ypred
1 1 0.98
2 0 0.94
3 1 0.86
4 1 0.74
5 0 0.73
6 0 0.64
7 1 0.53
8 0 0.39
9 1 0.34
10 0 0.31
library(ROCR)
pred <- prediction( new$ypred, new$true )
pred
An object of class "prediction"
Slot "predictions":
[[1]]
[1] 0.98 0.94 0.86 0.74 0.73 0.64 0.53 0.39 0.34 0.31
Slot "labels":
[[1]]
[1] 1 0 1 1 0 0 1 0 1 0
Levels: 0 < 1
Slot "cutoffs":
[[1]]
[1] Inf 0.98 0.94 0.86 0.74 0.73 0.64 0.53 0.39 0.34 0.31
Slot "fp":
[[1]]
[1] 0 0 1 1 1 2 3 3 4 4 5
Slot "tp":
[[1]]
[1] 0 1 1 2 3 3 3 4 4 5 5
Slot "tn":
[[1]]
[1] 5 5 4 4 4 3 2 2 1 1 0
Slot "fn":
[[1]]
[1] 5 4 4 3 2 2 2 1 1 0 0
Slot "n.pos":
[[1]]
[1] 5
Slot "n.neg":
[[1]]
[1] 5
Slot "n.pos.pred":
[[1]]
[1] 0 1 2 3 4 5 6 7 8 9 10
Slot "n.neg.pred":
[[1]]
[1] 10 9 8 7 6 5 4 3 2 1 0
perf <- performance( pred, "tpr", "fpr" )
perf
An object of class "performance"
Slot "x.name":
[1] "False positive rate"
Slot "y.name":
[1] "True positive rate"
Slot "alpha.name":
[1] "Cutoff"
Slot "x.values":
[[1]]
[1] 0.0 0.0 0.2 0.2 0.2 0.4 0.6 0.6 0.8 0.8 1.0
Slot "y.values":
[[1]]
[1] 0.0 0.2 0.2 0.4 0.6 0.6 0.6 0.8 0.8 1.0 1.0
Slot "alpha.values":
[[1]]
[1] Inf 0.98 0.94 0.86 0.74 0.73 0.64 0.53 0.39 0.34 0.31
plot(perf)
I am unsure of the output. And the ROC curve looks like a step function plot.