简体   繁体   English

如何在R中创建包含多个判断的混淆矩阵?

[英]How to create a confusion matrix containing multiple judgments in R?

I've got a data set from two raters judging a set of videoclips on multiple (binary) criteria. 我有一个来自两个评估者的数据集来判断多个(二进制)标准上的一组视频片段。 I'd like to plot a confusion matrix to better understand their agreement/disagreement. 我想绘制一个混淆矩阵,以便更好地理解他们的一致意见/不同意见。 But all the examples I've found so far are for cases where each judge only rates on one criteria per clip. 但到目前为止我发现的所有例子都是针对每个裁判仅按照一个标准评分的情况。 In my case, judges rate every criteria for each clip. 就我而言,评委会对每个剪辑的每个标准进行评分。

Say I have 4 binary criteria (A_Con..A_Mod), judged by two raters (A and B), for a set of videoclips (in this case 80): 假设我有4个二进制标准(A_Con..A_Mod),由两个评估者(A和B)判断,对于一组视频片段(在这种情况下为80):

str (mydata)
'data.frame':   160 obs. of  6 variables:
 $ A_Con: int  0 0 0 0 0 0 0 0 0 0 ...
 $ A_Dom: int  0 0 0 1 0 0 0 0 0 0 ...
 $ A_Met: int  0 0 0 0 0 0 1 0 0 1 ...
 $ A_Mod: int  0 0 0 1 0 1 0 0 0 1 ...
 $ Rater: Factor w/ 2 levels "A","B": 2 2 2 2 2 2 2 2 2 2 ...
 $ Clip : int  1 2 3 4 5 6 7 8 9 10 ...

I can melt this into: 我可以将其融入:

> str(mymolten)
'data.frame':   640 obs. of  4 variables:
 $ Rater   : Factor w/ 2 levels "A","B": 2 2 2 2 2 2 2 2 2 2 ...
 $ Clip    : int  1 2 3 4 5 6 7 8 9 10 ...
 $ variable: Factor w/ 4 levels "A_Con","A_Dom",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ value   : int  0 0 0 0 0 0 0 0 0 0 ...

But I can't figure out how to cast it into a confusion matrix that would count the combinations (which are not nearly so perfect as this): 但是我无法弄清楚如何将它转换成一个混淆矩阵来计算组合(这些组合并不是那么完美):

                        Rater B
              A_Con  A_Dom  A_Met  A_Mod
         A_Con  19      1      0      0
Rater A  A_Dom   1     20      0      0
         A_Met   0      0     20      5
         A_Mod   0      2      0     20

It seems like the table() function is the way to go, but how to format the data? 似乎table()函数是要走的路,但如何格式化数据呢?

This may not be the simplest solution. 这可能不是最简单的解决方案。 You can separate the data for the two raters, and merge the resulting data.frames. 您可以分隔两个评估者的数据,并merge生成的data.frames。

# Sample data
n <- 80
d0 <- data.frame(
  A_Con = round(runif(2*n)),
  A_Dom = round(runif(2*n)),
  A_Met = round(runif(2*n)),
  A_Mod = round(runif(2*n)),
  Rater = rep(c("A","B"), n),
  Clip = rep(1:n,each=2)
)

library(reshape2)
library(plyr)
d <- melt(d0, id.vars=c("Rater","Clip"))
d <- d[ d$value==1, ]
A <- d[d$Rater=="A",] 
B <- d[d$Rater=="B",]
A <- data.frame( Clip=A$Clip, A=A$variable )
B <- data.frame( Clip=B$Clip, B=B$variable )
d <- merge(A, B, all=FALSE)
d <- ddply( d, c("A", "B"), summarize, n=length(Clip) )
dcast( d, A ~ B )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM