[英]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.