Search code examples
rggplot2boxplotr-caretstatistical-test

Boxplots with the statistical (wilcoxon) test


I have to compare different ML algorithms using caret R package and then find the significance difference between each of these algorithms.

For instance, I am using my code as follows

nnet2 <- train(result ~ ., data = tr,
              method = "nnet",
               tuneLength = 15,
              metric = "MAE",
              preProc = c("center", "scale", "nzv"),
              trControl = ctrl)

getTrainPerf(nnet2)

svm2 <- train(result ~ ., data = tr,
             method = "svmRadial",

             tuneLength = 15,
             metric = "MAE",
             preProc = c("center", "scale", "nzv"),
             trControl = ctrl)

getTrainPerf(svm2)

and few other algorithms like that. Then I have performed wilcoxon test

wilcox.test (nnet2$resample$MAE, svm2$resample$MAE, paired=T)

My question is how can we put the results of the wilcoxon tests as a boxplot in R language?

Thanks


Solution

  • Using an example dataset:

    library(caret)
    library(mlbench)
    library(ggpubr)
    data(BostonHousing)
    tr = BostonHousing
    
    ctrl = trainControl(method="cv",number=10)
    
    nnet2 <- train(medv ~ ., data = tr,
                  method = "nnet",
                   tuneLength = 5,
                  metric = "MAE",
                  preProc = c("center", "scale", "nzv"),
                  trControl = ctrl)
    
    svm2 <- train(medv ~ ., data = tr,
                 method = "svmRadial",
    
                 tuneLength = 5,
                 metric = "MAE",
                 preProc = c("center", "scale", "nzv"),
                 trControl = ctrl)
    

    Better to create a data.frame, with the MAE and a vector representing the model:

    df = data.frame(MAE=c(nnet2$resample$MAE,svm2$resample$MAE),
    model=rep(c("nnet","svm"),each=length(svm2$resample$MAE)))
    
    ggboxplot(df, x = "model",y= "MAE",col="model",palette = c("#00AFBB", "#E7B800")) + 
    stat_compare_means()
    

    enter image description here