Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 插入符号混淆矩阵度量是错误的吗?_R_Machine Learning_R Caret_Confusion Matrix - Fatal编程技术网

R 插入符号混淆矩阵度量是错误的吗?

R 插入符号混淆矩阵度量是错误的吗?,r,machine-learning,r-caret,confusion-matrix,R,Machine Learning,R Caret,Confusion Matrix,我制作了一个函数来计算混淆矩阵的灵敏度和特异性,后来才发现caret包中有一个,confusionMatrix()。当我尝试它时,事情变得非常混乱,因为它似乎caret使用了错误的公式 示例数据: dat <- data.frame(real = as.factor(c(1,1,1,0,0,1,1,1,1)), pred = as.factor(c(1,1,0,1,0,1,1,1,0))) cm <- table(dat$real, dat$pre

我制作了一个函数来计算混淆矩阵的灵敏度和特异性,后来才发现
caret
包中有一个,
confusionMatrix()
。当我尝试它时,事情变得非常混乱,因为它似乎
caret
使用了错误的公式

示例数据:

dat <- data.frame(real = as.factor(c(1,1,1,0,0,1,1,1,1)),
                  pred = as.factor(c(1,1,0,1,0,1,1,1,0)))
cm <- table(dat$real, dat$pred)
cm
    0 1
  0 1 1
  1 2 5
灵敏度和特异性似乎在我的函数和
confusionMatrix()
之间交换。我以为我用错了公式,但我仔细检查了一下,我是对的。我还仔细检查了我是否从混淆矩阵中调用了正确的值,我很确定我是这样做的。插入符号也表明它使用了正确的公式,所以我不知道发生了什么


插入符号功能是错误的,还是(更有可能)我犯了一些令人尴尬的明显错误?

插入符号功能没有错误

首先。考虑如何构造表。code>table(first,second)将生成一个表,行中有
first
,列中有
second

此外,在对表进行子集设置时,应按列对单元格进行计数。例如,在函数中,计算灵敏度的正确方法是

 sens <- cm[4] / (cm[4] + cm[2])

插入符号功能没有错

首先。考虑如何构造表。code>table(first,second)将生成一个表,行中有
first
,列中有
second

此外,在对表进行子集设置时,应按列对单元格进行计数。例如,在函数中,计算灵敏度的正确方法是

 sens <- cm[4] / (cm[4] + cm[2])
 sens <- cm[4] / (cm[4] + cm[2])
confusionMatrix(data = dat$pred, reference = dat$real, positive = "1")

Confusion Matrix and Statistics

          Reference
Prediction 0 1
         0 1 2
         1 1 5
                                          
               Accuracy : 0.6667          
                 95% CI : (0.2993, 0.9251)
    No Information Rate : 0.7778          
    P-Value [Acc > NIR] : 0.8822          
                                          
                  Kappa : 0.1818          
                                          
 Mcnemar's Test P-Value : 1.0000          
                                          
            Sensitivity : 0.7143          
            Specificity : 0.5000          
         Pos Pred Value : 0.8333          
         Neg Pred Value : 0.3333          
             Prevalence : 0.7778          
         Detection Rate : 0.5556          
   Detection Prevalence : 0.6667          
      Balanced Accuracy : 0.6071          
                                          
       'Positive' Class : 1