I know this is an old question, but the fact that the only answer is written using Python bothers me a lot, given that the question specifically asks for an R solution.
As you can see from the code below, I am using pROC::multiclass.roc()
function. The only requirement to make it work is that the names of the columns of the predictions matrix match the true classes (real_values
).
The first example generates random predictions. The second one generates a better prediction. The third one generates the perfect prediction (i.e., always assigning the highest probability to the true class.)
library(pROC)
set.seed(42)
head(real_values)
real_values <- matrix( c("class1", "class2", "class3"), nc=1 )
# [,1]
# [1,] "class1"
# [2,] "class2"
# [3,] "class3"
# Random predictions
random_preds <- matrix(rbeta(3*3,2,2), nc=3)
random_preds <- sweep(random_preds, 1, rowSums(a1), FUN="/")
colnames(random_preds) <- c("class1", "class2", "class3")
head(random_preds)
# class1 class2 class3
# [1,] 0.3437916 0.6129104 0.4733117
# [2,] 0.6016169 0.4700832 0.9364681
# [3,] 0.6741742 0.8677781 0.4823129
multiclass.roc(real_values, random_preds)
#Multi-class area under the curve: 0.1667
better_preds <- matrix(c(0.75,0.15,0.5,
0.15,0.5,0.75,
0.15,0.75,0.5), nc=3)
colnames(better_preds) <- c("class1", "class2", "class3")
head(better_preds)
# class1 class2 class3
# [1,] 0.75 0.15 0.15
# [2,] 0.15 0.50 0.75
# [3,] 0.50 0.75 0.50
multiclass.roc(real_values, better_preds)
#Multi-class area under the curve: 0.6667
perfect_preds <- matrix(c(0.75,0.15,0.5,
0.15,0.75,0.5,
0.15,0.5,0.75), nc=3)
colnames(perfect_preds) <- c("class1", "class2", "class3")
head(perfect_preds)
multiclass.roc(real_values, perfect_preds)
#Multi-class area under the curve: 1