在 R 中编写函数以使用 pROC 绘制 ROC 曲线
Writing a function in R to plot ROC curve using pROC
我正在尝试编写一个函数来根据我必须预测结果的不同评分系统绘制 ROC 曲线。
我有一个数据框 data_all,包含列 "score_1" 和 "Threshold.2000"。我根据需要生成 ROC 曲线:
plot.roc(data_all$Threshold.2000, data_all$score_1)
我的目标是为许多不同的结果(例如 Threshold.1000)和分数(score_1、score_2 等)生成 ROC 曲线,但我最初尝试设置它只是为了不同的分数。我的函数如下:
roc_plot <- function(dataframe_of_interest, score_of_interest) {
plot.roc(dataframe_of_interest$Threshold.2000, dataframe_of_interest$score_of_interest)}
I get the following error: Error in roc.default(x, predictor, plot =
TRUE, ...) : No valid data provided.
如果有人能找出我的功能不起作用的原因,我将不胜感激!我是一名 python 编码员和 R 的新手,并且在尝试许多不同的事情时运气不佳。非常感谢。
编辑:
这是与 mtcars 相同的示例,因此它是可重现的:
data(mtcars)
plot.roc(mtcars$vs, mtcars$mpg) # --> makes correct graph
roc_plot <- function(dataframe_of_interest, score_of_interest) {
plot.roc(dataframe_of_interest$mpg, dataframe_of_interest$score_of_interest)}
结果:
roc.default(x, predictor, plot = TRUE, ...) 中的错误:未提供有效数据。
roc_plot(mtcars, 对比)
这是一种按需要工作的解决方案(即让用户为 score_of_interest
指定不同的值):
library(pROC)
data(mtcars)
plot.roc(mtcars$vs, mtcars$mpg) # --> makes correct graph
# expects `score_of_interest` to be a string!!!
roc_plot <- function(dataframe_of_interest, score_of_interest) {
plot.roc(dataframe_of_interest$vs, dataframe_of_interest[, score_of_interest])
}
roc_plot(mtcars, 'mpg')
roc_plot(mtcars, 'cyl')
请注意,您的错误不是由不正确的列名引起的,而是由于 data.frame
class 的不正确使用引起的。注意一个更简单的函数会发生什么:
foo <- function(x, col_name) {
head(x$col_name)
}
foo(mtcars, mpg)
## NULL
这个returnsNULL
。因此,在您尝试使用 dataframe_of_interest$score_of_interest
提供 plot.roc
的原始函数中,您实际上是在向 plot.roc
提供 NULL
.
当列名称存储在对象中时,有几种方法可以通过列名称从 data.frame
中提取列(这就是将它作为参数传递给函数时所做的事情).也许最简单的方法是记住 data.frame
就像一个二维数组类型的对象,因此我们可以使用熟悉的 object[i, j]
语法,但我们要求所有行并按名称指定列,例如, mtcars[, 'mpg']
。如果我们将字符串 'mpg'
分配给对象,这仍然有效:
x <- 'mpg'
mtcars[, x]
这就是我生成解决方案的方式。更进一步,不难想象您将如何同时提供 score_of_interest
和 threshold_of_interest
:
roc_plot2 <- function(dataframe_of_interest, threshold_of_interest, score_of_interest) {
plot.roc(dataframe_of_interest[, threshold_of_interest],
dataframe_of_interest[, score_of_interest])
}
roc_plot2(mtcars, 'vs', 'mpg')
我正在尝试编写一个函数来根据我必须预测结果的不同评分系统绘制 ROC 曲线。
我有一个数据框 data_all,包含列 "score_1" 和 "Threshold.2000"。我根据需要生成 ROC 曲线:
plot.roc(data_all$Threshold.2000, data_all$score_1)
我的目标是为许多不同的结果(例如 Threshold.1000)和分数(score_1、score_2 等)生成 ROC 曲线,但我最初尝试设置它只是为了不同的分数。我的函数如下:
roc_plot <- function(dataframe_of_interest, score_of_interest) {
plot.roc(dataframe_of_interest$Threshold.2000, dataframe_of_interest$score_of_interest)}
I get the following error: Error in roc.default(x, predictor, plot = TRUE, ...) : No valid data provided.
如果有人能找出我的功能不起作用的原因,我将不胜感激!我是一名 python 编码员和 R 的新手,并且在尝试许多不同的事情时运气不佳。非常感谢。
编辑: 这是与 mtcars 相同的示例,因此它是可重现的:
data(mtcars)
plot.roc(mtcars$vs, mtcars$mpg) # --> makes correct graph
roc_plot <- function(dataframe_of_interest, score_of_interest) {
plot.roc(dataframe_of_interest$mpg, dataframe_of_interest$score_of_interest)}
结果: roc.default(x, predictor, plot = TRUE, ...) 中的错误:未提供有效数据。 roc_plot(mtcars, 对比)
这是一种按需要工作的解决方案(即让用户为 score_of_interest
指定不同的值):
library(pROC)
data(mtcars)
plot.roc(mtcars$vs, mtcars$mpg) # --> makes correct graph
# expects `score_of_interest` to be a string!!!
roc_plot <- function(dataframe_of_interest, score_of_interest) {
plot.roc(dataframe_of_interest$vs, dataframe_of_interest[, score_of_interest])
}
roc_plot(mtcars, 'mpg')
roc_plot(mtcars, 'cyl')
请注意,您的错误不是由不正确的列名引起的,而是由于 data.frame
class 的不正确使用引起的。注意一个更简单的函数会发生什么:
foo <- function(x, col_name) {
head(x$col_name)
}
foo(mtcars, mpg)
## NULL
这个returnsNULL
。因此,在您尝试使用 dataframe_of_interest$score_of_interest
提供 plot.roc
的原始函数中,您实际上是在向 plot.roc
提供 NULL
.
当列名称存储在对象中时,有几种方法可以通过列名称从 data.frame
中提取列(这就是将它作为参数传递给函数时所做的事情).也许最简单的方法是记住 data.frame
就像一个二维数组类型的对象,因此我们可以使用熟悉的 object[i, j]
语法,但我们要求所有行并按名称指定列,例如, mtcars[, 'mpg']
。如果我们将字符串 'mpg'
分配给对象,这仍然有效:
x <- 'mpg'
mtcars[, x]
这就是我生成解决方案的方式。更进一步,不难想象您将如何同时提供 score_of_interest
和 threshold_of_interest
:
roc_plot2 <- function(dataframe_of_interest, threshold_of_interest, score_of_interest) {
plot.roc(dataframe_of_interest[, threshold_of_interest],
dataframe_of_interest[, score_of_interest])
}
roc_plot2(mtcars, 'vs', 'mpg')