r - 了解 ROCR 的 performance() 函数返回的内容 - 在 R 中的分类

标签 r performance classification roc

我很难理解 ROCR 包的 performance() 函数返回的内容。让我用一个可重现的例子来具体说明。我使用 mpg 数据集。我的代码如下:

library(ROCR)
library(ggplot2)
library(data.table)
library(caTools)
data(mpg)
setDT(mpg)
mpg[year == 1999, Year99 := 1]
mpg[year == 2008, Year99 := 0]
table(mpg$Year99)
# 0   1 
# 117 117 
split <- sample.split(mpg$Year99, SplitRatio = 0.75)
mpg_train <- mpg[split, ]
mpg_test <- mpg[!split, ]
model <- glm(Year99 ~ displ, mpg_train, family = "binomial")
summary(model)
predict_mpg_test <- predict(model, type = "response", newdata = mpg_test)
ROCR_mpg_test <- prediction(predict_mpg_test, mpg_test$Year99)
performance(ROCR_mpg_test, "acc")

#An object of class "performance"
#Slot "x.name":
#  [1] "Cutoff"

#Slot "y.name":
#  [1] "Accuracy"

#Slot "alpha.name":
#  [1] "none"

#Slot "x.values":
#  [[1]]
#49        55        56        45        47        53        51        57        46        13        39        37        58 
#Inf 0.5983963 0.5926422 0.5868625 0.5752326 0.5635187 0.5576343 0.5458183 0.5398901 0.5280013 0.5220441 0.5101127 0.4981697 0.4921981 
#17        44        31        32        33        50        34        40        24        21        12 
#0.4802634 0.4683511 0.4564748 0.4446478 0.4328831 0.4270282 0.4095919 0.3923800 0.3866994 0.3698468 0.3265163 


#Slot "y.values":
#  [[1]]
#[1] 0.5000000 0.5172414 0.5344828 0.5344828 0.5517241 0.5344828 0.4827586 0.5000000 0.5862069 0.6206897 0.6034483 0.6206897 0.5862069
#[14] 0.5689655 0.5517241 0.5689655 0.5517241 0.5344828 0.5517241 0.5172414 0.5344828 0.4655172 0.4827586 0.4827586 0.5000000


#Slot "alpha.values":
#  list()

我的问题是:
  • 插槽“x.values”下列出的 4 行数字是多少?
  • 插槽“y.values”下列出的 2 行数字是多少?
  • 是否有可能我将一系列截止值传递给 ROCR 函数——例如cutoff = seq(0.05, 0.95, 0.05) -- 并返回给我定义的度量值 --e.g.准确度 --- 对于每个截止水平?

  • 您的建议将不胜感激。

    最佳答案

    (1) 在 x.values插槽你可以找到截止。
    这个截止向量包含模型预测的概率的唯一值集:

    prf <- performance(ROCR_mpg_test, "acc")
    
    cutoffs <- prf@x.values[[1]]
    pred.probs <- sort(unique(predict_mpg_test), decreasing=T)
    all(cutoffs[-1] == pred.probs)
    # [1] TRUE
    

    (2) 在y.values插槽有每个截止的准确度。
    accuracies1 <- prf@y.values[[1]]
    
    # Example. Calculate accuracy for the 3rd cutoff
    ( tbl <- table(predict_mpg_test>= cutoffs[3], mpg_test$Year99) )
    #         0  1
    #  FALSE 28 25
    #  TRUE   1  4
    sum(diag(tbl))/sum(tbl)
    # [1] 0.5517241
    accuracies1[3]
    # [1] 0.5517241
    
    # Calcuate the accuracies for each cutoff
    calc_accur <- function(cutoff, pred_prob, response_var) {
      confusion_matrix <- table( pred_prob >= cutoff, response_var) 
      sum(diag(confusion_matrix))/sum(confusion_matrix)
    }
    
    accuracies2 <- sapply(cutoffs, calc_accur,  
           pred_prob=predict_mpg_test, response_var=mpg_test$Year99)
    
    all(accuracies1==accuracies2)
    # [1] TRUE
    

    (3) 使用calc_accur (2) 和 sapply 中给出的函数可以通过一系列截止值并计算相应的精度。
    例如:
    seq_cut <- seq(0.3, 0.6, length.out=10)
    sapply(seq_cut, calc_accur,  
           pred_prob=predict_mpg_test, response_var=mpg_test$Year99)
    
    # [1] 0.5000000 0.5000000 0.5000000 0.5172414 0.5517241 0.5862069 0.6551724 0.6379310
    # [9] 0.5517241 0.5000000
    

    关于r - 了解 ROCR 的 performance() 函数返回的内容 - 在 R 中的分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44874491/

    相关文章:

    r - 数据.表 : unexpected error with non-standard evaluation

    r - 如何制作散点图显示单个基因与多个基因之间的相关性?

    Django 性能调优技巧?

    performance - 现代 CPU 中的小分支

    java - 稀疏 vector ,它们是什么?

    如果某些语句不满足,R 返回函数中的前一点

    r - R Markdown 的 Jekyll 转换器

    python - 为什么 numpy sum 比 + 运算符慢 10 倍?

    python - 如何对未标记的数据进行分类?

    python - 如何在 Python 中正确覆盖和调用超方法