Daniel Martinez
Daniel Martinez

Reputation: 21

How do I make and plot ROC curves in R for multiclass classification?

I am doing my degree work, I am studying the GAUSSIAN MIXTURE MODELS (GMM), which I am using to carry out classification, as I am using the GMM for classification, it is of interest to me to see how accurate the model is being as a classifier. I am working with a dataset that has 5 labels, which refer to 5 classes of Rice, I split the dataset into test data and training data and ran the GMM using the Mclust function from the mlcust library in r, I would like to know how I can make a ROC curve for this case of multi-classes, and calculate the AUC, I saw that with a graph of the type 1 vs all it was possible, but they do not detail how, the examples that are found in other places do not run well, and for so it's easy to see how it works

I enclose an example code of the GMM using iris, for this case I would like to see what the ROC curves would be like

library(mclust)

Datos<-iris

set.seed(2022)

indices<-sample(1:nrow(Datos),size = floor(0.40*nrow(Datos)),replace = FALSE)
entreno<-Datos[indices,]
prueba<-Datos[-indices,]
table(Datos[indices,]$Species)
X<-Datos[indices,-5]
y <-Datos[indices,5]



GMM_iris<-Mclust(X,G=3)
summary(GMM_iris,parameters = TRUE)
table(y,GMM_iris$classification)

prediccion<-predict(GMM_iris,newdata = prueba[,-5])
table(Datos[-indices,]$Species,prediccion$classification)```

Upvotes: 2

Views: 4175

Answers (1)

YH Jang
YH Jang

Reputation: 1348

You can plot roc curves of multiclass classification model with pROC.

library(mclust)
#> Package 'mclust' version 5.4.9
#> Type 'citation("mclust")' for citing this R package in publications.

Datos<-iris

set.seed(2022)

indices<-sample(1:nrow(Datos),size = floor(0.40*nrow(Datos)),replace = FALSE)
entreno<-Datos[indices,]
prueba<-Datos[-indices,]

X<-Datos[indices,-5]
y <-Datos[indices,5]


GMM_iris<-Mclust(X,G=3)



table(y,GMM_iris$classification)
#>             
#> y             1  2  3
#>   setosa      0 21  0
#>   versicolor  0  0 22
#>   virginica  17  0  0

prediction<-predict(GMM_iris,newdata = prueba[,-5])


library(pROC)
result <- pROC::multiclass.roc(prediction$classification, 
                               as.numeric(prueba$Species))

prueba$Species was changed to numeric for multiclass.roc.

plot.roc(result$rocs[[1]], 
         print.auc=T,
         legacy.axes = T)
plot.roc(result$rocs[[2]],
         add=T, col = 'red',
         print.auc = T,
         legacy.axes = T,
         print.auc.adj = c(0,3))
plot.roc(result$rocs[[3]],add=T, col = 'blue',
         print.auc=T,
         legacy.axes = T,
         print.auc.adj = c(0,5))

legend('bottomright',
       legend = c('setosa-versicolor',
                  'setosa-virginica',
                  'versicolor-virginica'),
       col=c('black','red','blue'),lwd=2)

There are 3 rocs in result, which is the number of combination of iris species.
(setosa & versicolor, setosa & virginica, versicolor & virginica).

Created on 2022-05-10 by the reprex package (v2.0.1)

Upvotes: 5

Related Questions