The plot is just fine. After you run prediction()
from the package ROCR you get an object, pred
, with several components. Two of them are pred$tp
and pred$fp
, which represent the number of true positives and false positives respectively. To calculate the rate you divide them by the maximum, which is 5 in this case:
tpr <- pred$tp/max(pred$tp)
fpr <- pred$fp/max(pred$fp)
plot(fpr, tpr, type = "l")
These values are the same you get after running performance(pred, "tpr", "fpr")
. Check the x.values
and y.values
components in perf
. These are the values plotted with plot(perf)
. You get an identical plot using plot(fpr, tpr, type="l")
. The plots look like a step function because you only have five values.
UPDATE
It seems the structure of the object returned by prediction()
has changed in recent ROCR
versions. Now prediction()
returns an S4 object, and so the information is stored in slots. This would be how the result from the OP data would look like now:
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
Also, each slot is a list, I guess to potentially include several predictions together. The code in my original response should therefore be adapted to this new format. Something like this:
tpr <- pred@tp[[1]]/max(pred@tp[[1]])
fpr <- pred@fp[[1]]/max(pred@fp[[1]])
plot(fpr, tpr, type = "l")
When I understand your question correctly you are asking which class is the positive one and which is the negative one.
The answer is that this is to a certain extent arbitrary, so you have to decide that considering the problem at hand.
From "Machine Learning with R" by Brett Lantz, 2.nd edition, 2015, p. 318:
The most common performance measures consider the model's ability to
discern one class versus all others. The class of interest is known as
the positive class, while all others are known as negative.
The use of the terms positive and negative is not intended to imply
any value judgment (that is, good versus bad), nor does it necessarily
suggest that the outcome is present or absent (such as birth defect
versus none). The choice of the positive outcome can even be
arbitrary, as in cases where a model is predicting categories such as
sunny versus rainy or dog versus cat.
The relationship between the positive class and negative class
predictions can be depicted as a 2 x 2 confusion matrix that tabulates
whether predictions fall into one of the four categories:
• True Positive (TP): Correctly classified as the class of
interest
• True Negative (TN): Correctly classified as not the
class of interest
• False Positive (FP): Incorrectly
classified as the class of interest
• False Negative (FN):
Incorrectly classified as not the class of interest
This is the reason that you e.g. have to specify the positive class when using generic performance measure functions, like ConfusionMatrix
in the caret
package in R.
Now another complicating factor is of course your multiclass setting, but this is answered here:
How to compute precision/recall for multiclass-multilabel classification?
In general the most popular approach is to calculate these measures for each class by comparing each class level to the remaining levels (i.e. a "one versus all" approach).
Best Answer
There is quite a bit of terminological confusion in this area. Personally, I always find it useful to come back to a confusion matrix to think about this. In a classification / screening test, you can have four different situations:
In this table, “true positive”, “false negative”, “false positive” and “true negative” are events (or their probability). What you have is therefore probably a true positive rate and a false negative rate. The distinction matters because it emphasizes that both numbers have a numerator and a denominator.
Where things get a bit confusing is that you can find several definitions of “false positive rate” and “false negative rate”, with different denominators.
For example, Wikipedia provides the following definitions (they seem pretty standard):
In all cases, the denominator is the column total. This also gives a cue to their interpretation: The true positive rate is the probability that the test says “A” when the real value is indeed A (i.e., it is a conditional probability, conditioned on A being true). This does not tell you how likely you are to be correct when calling “A” (i.e., the probability of a true positive, conditioned on the test result being “A”).
Assuming the false negative rate is defined in the same way, we then have $FNR = 1 - TPR$ (note that your numbers are consistent with this). We cannot however directly derive the false positive rate from either the true positive or false negative rates because they provide no information on the specificity, i.e., how the test behaves when “not A” is the correct answer. The answer to your question would therefore be “no, it's not possible” because you have no information on the right column of the confusion matrix.
There are however other definitions in the literature. For example, Fleiss (Statistical methods for rates and proportions) offers the following:
(He also acknowledges the previous definitions but considers them “wasteful of precious terminology”, precisely because they have a straightforward relationship with sensitivity and specificity.)
Referring to the confusion matrix, it means that $FPR = FP / (TP + FP)$ and $FNR = FN / (TN + FN)$ so the denominators are the row totals. Importantly, under these definitions, the false positive and false negative rates cannot directly be derived from the sensitivity and specificity of the test. You also need to know the prevalence (i.e., how frequent A is in the population of interest).
Fleiss does not use or define the phrases “true negative rate” or the “true positive rate” but if we assume those are also conditional probabilities given a particular test result / classification, then @guill11aume answer is the correct one.
In any case, you need to be careful with the definitions because there is no indisputable answer to your question.