使用 plotROC 包和 geom_roc() 的 ROC 曲线,将数据转换为 "M1 markers"
ROC Curve using plotROC package and geom_roc(), transforming data to "M1 markers"
我正在尝试使用 ggplot2
的 plotROC
包绘制 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
是什么以及如何转换我的数据以获得该字段。
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()
您的 marker/predictor 是您的 glm 模型的拟合值。 ROC 将让您了解模型的工作原理(通过 AUC)以及将人员分配给 类 的最佳概率阈值(ROC 截止值)。
如果您想可视化 fdifferent multivariate/univariate 方法的附加值,这是一种有用的方法。
这是 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))
我正在尝试使用 ggplot2
的 plotROC
包绘制 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
是什么以及如何转换我的数据以获得该字段。
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()
您的 marker/predictor 是您的 glm 模型的拟合值。 ROC 将让您了解模型的工作原理(通过 AUC)以及将人员分配给 类 的最佳概率阈值(ROC 截止值)。 如果您想可视化 fdifferent multivariate/univariate 方法的附加值,这是一种有用的方法。 这是 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))