-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7_kappa_alpha_imbalanced_error_rate_sim.R
140 lines (124 loc) · 3.4 KB
/
7_kappa_alpha_imbalanced_error_rate_sim.R
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
rm(list = ls())
library(krippendorffsalpha)
library(kripp.boot)
library(ggplot2)
library(ggthemes)
library(tidyr)
library(dplyr)
library(psych)
source("./__simulation_functions.R")
error_rates <- c(
seq(from = 0, to = 0.175, by = 0.025),
seq(from = 0.2, to = 1, by = 0.1)
)
# Simulate data
kappa_alpha_list <- lapply(error_rates, \(error_rate) {
irr_matrix <- generate_dummy_rater_data(
error_rate = error_rate,
prop_negative_class = 0.9
)
c(
"error_rate" = error_rate,
get_cohen_kappa(irr_matrix),
get_k_alpha_kap(irr_matrix)
)
})
# Create dataframe
kappa_alpha_df <- bind_rows(kappa_alpha_list) |>
mutate(
diff_kappa = cohen_k_upper - cohen_k_lower,
diff_alpha = upper_kap - lower_kap
)
#
kappa_alpha_long <- kappa_alpha_df |>
select(
error_rate,
estimate_kappa = cohen_k,
upper_kappa = cohen_k_upper,
lower_kappa = cohen_k_lower,
estimate_alpha = alpha_kap,
lower_alpha = lower_kap,
upper_alpha = upper_kap,
diff_alpha,
diff_kappa
) |>
pivot_longer(
-error_rate,
names_to = c("measure", "greek_letter"),
names_sep = "_"
) |>
pivot_wider(
id_cols = c(error_rate, greek_letter),
names_from = "measure"
)
nudge_x <- rep(c(0, 0.01), nrow(kappa_alpha_long))
plot_title <- "Comparison between Krippendorf's alpha and Cohen's Kappa at different error rates"
plot_subtitle <- "0.9 negative class, 300 samples"
plot_caption <- "Note: some randomness as confidence estimates created with bootstrapping"
ggplot(kappa_alpha_long, mapping = aes(
x = error_rate
)) +
geom_point(
aes(
y = estimate,
color = greek_letter
),
position = position_nudge(
x = nudge_x
),
size = 3
) +
geom_linerange(
aes(
ymin = lower,
ymax = upper,
color = greek_letter
),
position = position_nudge(
x = nudge_x
),
size = 1.4
) +
expand_limits(x = 0, y = 0) +
theme_stata(base_size = 16) +
labs(
title = plot_title,
subtitle = plot_subtitle,
x = "Number of samples",
y = "value (line represents CI)",
caption = plot_caption
) +
scale_color_stata() +
coord_cartesian(ylim = (c(-1, 1))) +
theme(
legend.title = element_blank()
)
ggsave("./plots/kappa_alpha_comparison/line_plot_error_rate_imbalanced.png", w = 12, h = 7.5)
ggplot(kappa_alpha_long) +
geom_point(
mapping = aes(
x = error_rate,
y = diff,
color = greek_letter
),
size = 3,
position = position_nudge(
x = nudge_x
)
) +
theme_stata(base_size = 16) +
labs(
title = plot_title,
subtitle = plot_subtitle,
x = "n",
y = "Difference between lower and upper bound",
caption = "Note: some randomness as confidence estimates created with bootstrapping"
) +
expand_limits(x = 0, y = 0) +
scale_color_stata() +
theme(
legend.title = element_blank()
) +
coord_cartesian(ylim = (c(-1, 1)))
ggsave("./plots/kappa_alpha_comparison/scatter_error_rate_imbalanced.png", w = 12, h = 7.5)
write.csv(kappa_alpha_long, "./simulated_data/7_kappa_alpha_imbalanced_error_rate.csv", row.names = FALSE)