簡體   English   中英

R 中 multiclass.roc 的平均 AUC 的 Plot

[英]Plot of average AUC from multiclass.roc in R

我想 plot 宏平均 AUC 三類。 我的工作示例是:

roc <- multiclass.roc(Claims$CLAIMS2 ~ Claims$PredA)
roc

#Call:
#multiclass.roc.formula(formula = Claims$CLAIMS2 ~ Claims$PredA)
#Data: ClaimsA$PredA with 3 levels of ClaimsA$CLAIMS2: >=2, 0, 1.
#Multi-class area under the curve: 0.6905

我發現三類索賠的宏觀平均 AUC 為 0.6905。 然后,我在一個 plot 中繪制了三個類:

rsA <- roc[['rocs']]

par(pty="s")
plot(rsA[[1]],cex.axis=1.8,lwd = 4,grid = FALSE, xlab="",ylab="",legacy.axes = F,colorize=FALSE, col="gray80", print.auc=F,print.auc.x=0.8,print.auc.y=0.33) # plot ROC curve
plot(rsA[[2]],cex.axis=1.8,cex.lab=2,lwd = 4,colorize=FALSE, xlab="",ylab="",grid = FALSE, legacy.axes = F,col="gray50", add=TRUE, print.auc=F,print.auc.x=0.8, print.auc.y=0.33)
plot(rsA[[3]],cex.axis=1.8,cex.lab=2,lwd = 4,colorize=FALSE, xlab="",ylab="",grid = FALSE, legacy.axes = F,col="black", add=TRUE, print.auc=F,print.auc.x=0.8, print.auc.y=0.33)
title(xlab = "1-Specificity", line = 4,cex.lab=2.5)            # Add x-axis text
title(ylab = "Sensitivity", line = 5,cex.lab=2.5)            # Add y-axis text
legend(0.7, 0.35, legend=c(TeX("Claims 0 vs 1: 0.664"), TeX("Claims 0 vs >=2: 0.770"), TeX("Claims 1 vs >=2: 0.637")),
       col=c("black", "gray50", "gray80","red"), lty=c(1, 1, 1, 3),lwd = 4, cex=1.7,
       box.lty=0)

在此處輸入圖像描述

我想在上面的 plot 中包含宏觀平均 AUC。 有什么辦法可以將 plot 宏觀平均 AUC 與一個 plot 三類合二為一?

我的理解是 roc_auc 是一個單一的分數,使用 roc_curve 計算。 所以嘗試 plot 是沒有意義的。

無論如何,我一直在玩tidymodels並嘗試使用tidymodels book 中的代碼。 下面是一些計算宏觀加權 roc_auc 和 plot 底層 roc 的最小代碼:

library(tidymodels)
library(palmerpenguins)
#> Attaching package: 'palmerpenguins'
#> The following object is masked from 'package:modeldata':
#> 
#>     penguins

penguins_split <- initial_split(penguins,
                                strata = "species")
penguins_train <- training(penguins_split)

rf_spec <- rand_forest() |>
  set_mode("classification")

results <- workflow(preprocessor = recipe(species ~ island + year,
                                          data = penguins_train),
                    spec = rf_spec) |>
  last_fit(penguins_split)

results |>
  collect_predictions() |>
  roc_auc(
    truth = species ,
    .pred_Adelie,
    .pred_Chinstrap,
    .pred_Gentoo,
    estimator = "macro_weighted"
  )
#> # A tibble: 1 × 3
#>   .metric .estimator     .estimate
#>   <chr>   <chr>              <dbl>
#> 1 roc_auc macro_weighted     0.806

results |>
  collect_predictions() |>
  roc_curve(truth = species ,
            .pred_Adelie, .pred_Chinstrap, .pred_Gentoo) |>
  autoplot()

代表 package (v2.0.1) 於 2022 年 7 月 29 日創建

我假設您想在 plot 上添加 AUC 的現有值。 正如@Desmond 所說,平均值不是曲線,因此不能單獨繪制。

您可以輕松地將值添加到圖例中:

# Get the AUC into a string
avg_auc <- roc$auc
avg_auc_legend <- sprintf("Average: %.3f", avg_auc)

# Plot the legend text
legend(0.7, 0.35, 
       legend=c("Claims 0 vs 1: 0.664",
                "Claims 0 vs >=2: 0.770", 
                "Claims 1 vs >=2: 0.637", 
                avg_auc_legend), 
       col=c("black", "gray50", "gray80"), 
       lty=c(1, 1, 1, 0), 
       lwd = 4, 
       cex=1.7,
       box.lty=0)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM