-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMulti-Label_Agreement.qmd
224 lines (196 loc) · 6.82 KB
/
Multi-Label_Agreement.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
---
title: "Multi-label agreement"
author: "Dr. Gordon McDonald"
format: html
editor: visual
---
## Load libraries
(install if you don't have already)
```{r,message=FALSE}
library(irrCAC)
library(tidyverse)
```
## Define functions to calculate MASI
```{r}
#| code-fold: true
#| code-summary: "Show the code defining the functions"
#' Parse string into a character vector
#'
#' @param x string, e.g. "label_1, label_2"
#' @param sep separator, e.g. ", "
#'
#' @return character vector of labels, e.g. c("label_1", "label_2")
#' @export
#'
#' @examples
#' elements_from_string("l1, l2, l3", sep = ", ")
elements_from_string <- function(x, sep = ", ") {str_split(x,sep,simplify = F)[[1]]}
#' Measuring Agreement on Set-valued Items (MASI) distance from text string
#' MASI Similarity or Distance (pairwise)
#'
#' @param x Person x string of labels such as "label_1, label_2, label_3"
#' @param y Person y string of labels such as "label_4, label_1, label_5, label_7"
#' @param sep Label separator in the string, default = ", "
#' @param jaccard_only Only return Jaccard index instead of MASI (default = FALSE)
#' @param type one of "dist" or "sim" (default) for a distance or similarity score.
#'
#' @return Jaccard Distance between the two sets
#' @export
#'
#' @examples
#' masi("l1, l2, l3", "l7, l2")
masi <- function(x,y,sep = ", ", jaccard_only = F, type = "sim"){
# Define the labels for each rater
lab_x <- elements_from_string(x)
lab_y <- elements_from_string(y)
# compute set diff and intersection size
diff_xy_size <- length(setdiff(lab_x,lab_y)) # number of elements in set x but not in set y
diff_yx_size <- length(setdiff(lab_y,lab_x)) # number of elements in set y but not in set x
intersection_size <- length(intersect(lab_x,lab_y)) # number of elements in common between two sets
# monotonicity simillarity coefficient, M, see http://www.lrec-conf.org/proceedings/lrec2006/pdf/636_pdf.pdf Rebecca Passonneau. 2006. Measuring Agreement on Set-valued Items (MASI) for Semantic and Pragmatic Annotation. In Proceedings of the Fifth International Conference on Language Resources and Evaluation (LREC’06), Genoa, Italy. European Language Resources Association (ELRA).
m_sim <- case_when(
(diff_xy_size == 0) & (diff_yx_size == 0) ~ 1, # the sets are identical, return 1
(diff_xy_size == 0) | (diff_yx_size == 0) ~ 2/3, # one set is a subset of the other, return 2/3
(diff_xy_size != 0) & (diff_yx_size != 0) & (intersection_size !=0) ~ 1/3, # some overlap, some non-overlap in each set, return 1/3
intersection_size ==0 ~ 0 # disjoint sets, return 0
)
# Calculate Jaccard simmilarity; J=1 means same, J=0 means no overlap at all. See https://en.wikipedia.org/wiki/Jaccard_index
jaccard_sim <- intersection_size/(length(lab_x) + length(lab_y) - intersection_size)
#MASI sim is M*J; MASI dist is 1-M*J
masi_sim <- if_else(jaccard_only,
jaccard_sim,
m_sim*jaccard_sim)
return(if_else(type == "sim",
masi_sim,
1-masi_sim))
}
MASI_simmilarity_matrix <- function(df, sep = ", ") {
labels_all_combos <- sort(unique(unlist(df))) # alphabetical sorted list of all strings of labels
num_label_combos <- length(labels_all_combos) # number of combinations above
masi_sim_mat <- matrix(nrow = num_label_combos,
ncol = num_label_combos,
dimnames = list(labels_all_combos,
labels_all_combos))
for(i in 1:num_label_combos){
for(j in 1:num_label_combos)
{
masi_sim_mat[i,j] <- masi(x = labels_all_combos[i],
y = labels_all_combos[j],
sep = sep)
}}
return(masi_sim_mat)
}
```
## Test data
```{r}
#creating the dataset as dataframe
#you'll want to load in your data frame / tibble from a csv file instead,
# e.g.
# dataset <- read_csv("my_labels.csv")
dataset <- tribble(
~Coder1, ~Coder2, ~Coder3,
"l1, l2", "l1", "l2",
"l1, l2", "l1, l2", "l1, l2",
"l1", "l1", "l1",
"l3", "l3", NA_character_,
"l3", "l1, l3", "l1, l3",
"l4", "l4", "l4",
"l2", "l4", "l5",
"l1, l2", "l1", "l2",
"l1, l2", "l1, l2, l3", "l1, l2, l3, l9",
"l1", "l2, l4", "l1",
"l1", "l1", "l5"
)
```
# Calculate Inter-rater reliability
```{r}
# calculate MASI set difference between each pair of labels
wt <- MASI_simmilarity_matrix(dataset, sep = ", ")
# calculating krippendorff alpha
ka <- krippen.alpha.raw(ratings = dataset,
weights = wt,
categ.labels = rownames(wt),
conflev = 0.95
)
# calculating fleiss' kappa
fk <- fleiss.kappa.raw(ratings = dataset,
weights = wt,
categ.labels = rownames(wt),
conflev = 0.95
)
bind_rows(fk$est,ka$est)
```
So Krippendorff's Alpha is
```{r}
(kav <- ka$est$coeff.val)
```
And the 95% confidence interval is
```{r}
ka$est$conf.int
```
And Fleiss' Kappa is
```{r}
(fkv <- fk$est$coeff.val)
```
And sampling 500 reshuffles to see what the coefficient looks like:
```{r}
#| code-fold: true
# "randomly" reshuffled data
reshuffle <- function(df){
df %>%
unlist %>%
{sample(.,size = length(.),replace = F)} %>%
matrix(ncol = ncol(df),
dimnames = list(row.names(df),
names(df))) %>%
as_tibble()
}
#reshuffled <- reshuffle(dataset)
#calculating krippendorff alpha
shuffle_ka_vec = c()
for (i in 1:500){
ka_r <- krippen.alpha.raw(ratings = reshuffle(dataset),
weights = wt,
categ.labels = rownames(wt),
conflev = 0.95
)
shuffle_ka_vec[i] <- ka_r$est$coeff.val
}
#calculating fleiss' kappa
shuffle_fk_vec = c()
for (i in 1:500){
fk_r <- fleiss.kappa.raw(ratings = reshuffle(dataset),
weights = wt,
categ.labels = rownames(wt),
conflev = 0.95
)
shuffle_fk_vec[i] <- fk_r$est$coeff.val
}
```
Plot random reshuffle vs the actual result you got.
```{r}
#| code-fold: true
hist(shuffle_ka_vec,
xlim = c(min(c(shuffle_ka_vec,kav)),1),
main = "Krippendorff's alpha",
xlab = "Krippendorff's alpha")
abline(v = kav,col="red")
text(x = c(0,kav,1),
y=c(10,10,10),
col=c("black","red","black"),
labels = c("random",paste0("value = ",round(kav,3)),"agree")
)
```
```{r}
#| code-fold: true
hist(shuffle_fk_vec,
xlim = c(min(c(shuffle_fk_vec,fkv)),1),
main = "Fleiss's Kappa",
xlab = "Fleiss' Kappa")
abline(v = fkv,col="blue")
text(x = c(0,fkv,1),
y=c(10,10,10),
col=c("black","blue","black"),
labels = c("random",paste0("value = ",round(fkv,3)),"agree")
)
```