首页 > 解决方案 > 如何以概率使用 ggplot2 中的 geom_roc()

问题描述

我使用以下代码生成 ROC 图

library(plotROC)
library(ggplot2)
library(e1071)
library(mlbench)

data(BreastCancer)
BreastCancer$Class <- ifelse(BreastCancer$Class == "malignant",1,0)

bound <- floor((nrow(BreastCancer)/4)*3)         
df <- BreastCancer[sample(nrow(BreastCancer)), ]        
training <- df[1:bound,-1]    
testing <- df[(bound+1):nrow(df), -1] 

nb_mod <- naiveBayes(formula = Class ~ ., data = training)

prob_nb <- predict(nb_mod,testing,type="raw")
df <- data.frame(Class = testing$Class,Prob = prob_nb )

basicplot <- ggplot(df, aes(d = Class, m = Prob.1)) + geom_roc()    

我很困惑,因为我在文档 https://cran.r-project.org/web/packages/plotROC/vignettes/examples.html中找到了以下示例:用作美学的标记 M 是大约 -2 之间的值2,不是概率。我是否正确使用了 geom_roc 函数?

library(plotROC)
library(ggplot2)

set.seed(2529)
D.ex <- rbinom(200, size = 1, prob = .5)
M1 <- rnorm(200, mean = D.ex, sd = .65)
M2 <- rnorm(200, mean = D.ex, sd = 1.5)

test <- data.frame(D = D.ex, D.str = c("Healthy", "Ill")[D.ex + 1], 
               M1 = M1, M2 = M2, stringsAsFactors = FALSE)

basicplot <- ggplot(test, aes(d = D, m = M1)) + geom_roc()

标签: rggplot2roc

解决方案


转换d为数字,然后就可以了。

as.numeric(D)

推荐阅读