R 中 multiclass.roc 的平均 AUC 绘图

问题描述 投票:0回答:3

我想绘制三个类别的宏观平均 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)

enter image description here

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

r plot roc multiclass-classification auc
3个回答
1
投票

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

无论如何,我正在使用

tidymodels
并尝试使用 tidymodels 书中的代码。这是一些计算宏加权 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 包于 2022 年 7 月 29 日创建(v2.0.1)


0
投票

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

0
投票

那么可以使用:

results |>
  collect_predictions() |>
  group_by(.pred_class) |>
  roc_auc(
    truth = species ,
    .pred_Adelie,
    .pred_Chinstrap,
    .pred_Gentoo,
    estimator = "macro_weighted"
  )

为了获得每个班级的 roc_auc 分数以进行进一步绘图等?

© www.soinside.com 2019 - 2024. All rights reserved.