【问题标题】:Plot of average AUC from multiclass.roc in RR中multiclass.roc的平均AUC图
【发布时间】:2022-08-02 15:13:04
【问题描述】:

我想绘制三个类的宏观平均 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。 然后,我将三个类一起绘制在一个图中:

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)

我想在上面的图中包含宏观平均 AUC。有没有办法在一个图中绘制三个类的宏观平均 AUC?

  • 您能否使用dput 分享一些可重现的数据?

标签: r plot roc multiclass-classification auc


【解决方案1】:

我的理解是 roc_auc 是一个单一的分数,使用 roc_curve 计算。所以尝试绘制它是没有意义的。

无论如何,我一直在玩 tidymodels 并尝试使用来自 the tidymodels book 的代码。下面是一些计算宏观加权 roc_auc 并绘制底层 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()

由reprex package (v2.0.1) 于 2022 年 7 月 29 日创建

【讨论】:

  • 谢谢。我担心的是绘制平均 auc(roc auc 宏观加权 = 0.806)。这在R中可能吗?
  • 正如我的回答中提到的,您只能绘制 roc_curves。 roc_auc 是一个单一的分数,绘制它需要绘制一个点。
【解决方案2】:

我假设您想在绘图上添加 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-12
    • 1970-01-01
    • 1970-01-01
    • 2020-06-07
    • 2017-03-20
    • 2021-11-13
    • 2011-01-30
    • 2016-08-01
    相关资源
    最近更新 更多