【问题标题】:ROC Curve using plotROC package and geom_roc(), transforming data to "M1 markers"ROC 曲线使用 plotROC 包和 geom_roc(),将数据转换为“M1 标记”
【发布时间】:2019-04-24 16:57:28
【问题描述】:

我正在尝试使用 plotROC 包为 ggplot2 绘制 ROC 曲线,但我不确定如何将我拥有的数据转换为 M1 标记格式。文档提供了以下示例:

# plotROC documentation example
library(plotROC)
library(ggplot2)
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)

D         M1         M2
1  1.4995932  0.5508204
1  0.4181619  1.6339181
0 -0.3620614 -1.0428972
1  0.7991132 -1.6396751
0  0.9574047  2.1159753
1  1.3440595  1.3026485

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

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

Sample ROC plot output by plotROC

我的数据是一个测试子集的逻辑回归分数:

# Example starting point
test <- rbinom(200, size = 1, prob = 0.2)
scores.prob <- runif(200, min = 0 , max = 1)
scores.class <- ifelse(scores.prob > 0.5, 1, 0)

# Example generated data
test scores.prob scores.class
 0   0.7323306            1
 0   0.7860687            1
 0   0.9535123            1
 1   0.3082551            0
 0   0.5762784            1
 1   0.4613730            0

我想知道M1 是什么以及如何转换我的数据以获取该字段。

【问题讨论】:

  • 我不太明白,但是这篇论文:ncbi.nlm.nih.gov/pmc/articles/PMC3952000 有一大堆关于 ROC 曲线、M1 和 M2 的内容。这有帮助吗?
  • 还可以查看?StatRoc 页面,因为它更详细地介绍了 d 和 m 参数
  • @user10626943 ?StatRoc 是一个很棒的资源!感谢您的快速响应。

标签: r ggplot2 roc


【解决方案1】:
library(plotROC) 
library(ggplot2) 
test <- rbinom(200, size = 1, prob = 0.2)
scores.prob <- runif(200, min = 0 , max = 1) 
test <- data.frame(D = test,
           M1 = scores.prob, stringsAsFactors = FALSE)
ggplot(test, aes(d = D, m = M1)) + 
geom_roc()

您的标记/预测变量是 glm 模型的拟合值。 ROC 将让您了解您的模型如何工作(通过 AUC)以及将人员分配到类的最佳概率阈值(ROC 截止值)。 如果您想可视化不同的多变量/单变量方法的附加值,这是一种有用的方法。 这是 mtcars 数据集的完整示例。希望对您有所帮助。

# Loading data
data(mtcars)
# Manual transmission (am = 1) depends on 1/4 mile time (qsec) and miles/(US) gallon (mpg)
glmfit <- glm(am ~ qsec + mpg, data = mtcars, binomial)
mtcars$fitted_am <- glmfit$fitted.values
# Loading packages
library(plotROC)
library(ggplot2)
library(pROC)
# Calculating ROC curve, AUC and threshold according to Youden index
rocfit <- roc(mtcars$am, mtcars$fitted_am)
auc(rocfit)
coords(rocfit, x = "b")
basicplot <- ggplot(mtcars, aes(d = am, m = fitted_am))
basicplot + 
  geom_roc() + 
  style_roc(theme = theme_grey) +
  theme(axis.text = element_text(colour = "blue")) +
  ggtitle("Automatic transmission prediction") +
  scale_x_continuous("1 - Specificity", breaks = seq(0, 1, by = .1))
plot(rocfit)
prop.table(table(mtcars$am))

【讨论】:

    猜你喜欢
    • 2019-05-18
    • 1970-01-01
    • 2018-05-08
    • 1970-01-01
    • 2014-07-20
    • 2015-08-29
    • 2018-12-23
    • 2021-12-27
    • 2016-05-26
    相关资源
    最近更新 更多