forked from EPINetz/EPINetz-Policy-Parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
policy_parser_coding_sample.R
385 lines (328 loc) · 17.9 KB
/
policy_parser_coding_sample.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# Pulling Samples for the Policy Parser Evaluation with manually-coded data
{
library(tidyverse)
library(vroom)
library(data.table)
library(openxlsx)
library(scales)
}
seed_sample <- function(data, # data
seed, # seed
... # arguments to pass to slice_sample()
) { # slice_sample needs the seed specified before each run (?!), so we build a small wrapper function
set.seed(seed)
res <- data %>% slice_sample(...)
return(res)
}
seed_twitter <- 20231218
seed_news <- 20231218
#set.seed(seed)
threshold_twitter <- 0.7
threshold_news <- 0.7
## Read data
### Twitter
tweet_data <- vroom(file = "Tokenizer/data_init_tweets_2023-06-22.csv.tar.gz",
# Important! specify coltypes to preserve correct IDs
col_types = list(
`_id` = "c",
`_source.author_id` = "c",
`_source.conversation_id` = "c",
`_source.in_reply_to_user_id`= "c",
`_source.attachments.poll_ids` = "c",
`_source.withheld.scope` = "c",
`_source.withheld.country_codes` = "c",
`_source.entities.cashtags` = "c"
), guess_max = 10000)
tweet_data <- tweet_data %>% mutate(is_retweet = case_when( # add RT indicator
str_detect(`_source.text`, "^RT") ~ TRUE,
.default = FALSE)) %>%
mutate(week = ceiling_date(`_source.created_at`, unit = "weeks")) # add week indicator
tweet_classification <- readRDS("init_classification/init_classified_tweets.RDS")
# tweets_highest <- tweet_classification %>% # reduce to highest score per document to determine its policy field
# imap(\(week, date)
# {week %>% .[["classified_documents"]]} %>%
# slice_max(score_norm, by = doc_id) %>%
# mutate(week = as_date(date))) %>%
# rbindlist() %>%
# rename(`_id` = doc_id) %>% # add variables
# left_join(tweet_data_full %>%
# select(`_id`, `_source.created_at`,
# `_source.real_name`, `_source.text`),
# by = join_by(`_id`))
classified_tweets <- tweet_classification %>% # unnest
map(\(dat) dat$classified_documents) %>%
rbindlist() %>%
filter(score_norm >= threshold_twitter) %>% # select classifications above threshold (reduced nr of policy fields per tweet, drops tweets below)
left_join(tweet_data %>% # add additional vars like text
select(`_id`, `_source.created_at`,
`_source.real_name`, `_source.text`,
week, is_retweet, is_reply),
by = join_by(doc_id == `_id`)) %>%
filter(!is_retweet & !is_reply) # drop replies and retweets for the samples (replies were not classified, retweets need not be classified)
### News
news_data <- list.files("news_classification/data", full.names = T) %>%
map(\(file) vroom(file)) %>% rbindlist(fill = TRUE)
news_data <- news_data %>% # add outlet indicator
mutate(outlet = str_remove_all(`_source.host`,
paste0(c("^www\\.", "\\.de$", "\\.net$",
"\\.co.uk$", "\\.com$"),
collapse = "|"))) %>%
mutate(week = ceiling_date(`_source.estimated_date`, unit = "weeks")) # add week indicator
news_data <- news_data %>% # remove a small number of non-german outlets accidentially in the data / only keep select outlets (drops 20 docs)
filter(outlet %in% c("faz", "welt", "bild", "spiegel",
"zeit", "sueddeutsche", "stuttgarter-zeitung"))
news_classification <- readRDS("news_classification/init_classified_news.RDS")
# news_highest <- news_classification %>% # reduce to highest score per document to determine its policy field
# imap(\(week, date)
# {week %>% .[["classified_documents"]]} %>%
# slice_max(score_norm, by = doc_id) %>%
# mutate(week = as_date(date))) %>%
# rbindlist() %>%
# rename(`_id` = doc_id) %>% # add variables
# left_join(news_data %>%
# select(`_id`, `_source.estimated_date`,
# outlet, `_source.title`, `_source.body`),
# by = join_by(`_id`))
classified_news <- news_classification %>% # unnest
map(\(dat) dat$classified_documents) %>%
rbindlist() %>%
filter(score_norm >= threshold_news) %>% # select classifications above threshold (reduced nr of policy fields per tweet, drops tweets below)
left_join(news_data %>% # add additional vars like text
select(`_id`, `_source.estimated_date`,
outlet, `_source.title`, `_source.body`,
week),
by = join_by(doc_id == `_id`))
## Pull Samples
### Twitter
#### 1000 tweets per coder, 300 tweets intercoder sample = 2.400 total tweets classified (~0.01%) with 12.5% of tweets coded by all coders
#### 60% of sampled data are pulled over the timeframes (weeks), 40% are pulled over tweets associated with policy fields (highest association) to not underrepresent small fields
tweet_sample_1 <-
classified_tweets %>% # temporal sample
seed_sample(seed_twitter, n = 2, by = week) %>%
mutate(sample = "temporal") %>%
bind_rows(
classified_tweets %>% # policy field sample
filter(!(doc_id %in% (classified_tweets %>% # not in temporal sample
seed_sample(seed_twitter, n = 2, by = week) %>% pull(doc_id)))) %>%
seed_sample(seed_twitter, n = 24, by = policy_field) %>%
mutate(sample = "policy")
) %>%
select(doc_id, `_source.created_at`, `_source.text`,
policy_field, sample)
tweet_sample_1 %>% summarise(n = n(), .by = sample) # a roughly 60/40 ratio
twitter_intercoder_sample <- tweet_sample_1 %>% # make intercoder sample
seed_sample(seed_twitter, n = 150, by = sample) # .. with even amounts of temporal and policy sourced tweets
tweet_sample_2 <- classified_tweets %>% # temporal sample
filter(!(doc_id %in% tweet_sample_1$doc_id)) %>% # not in previous samples
seed_sample(seed_twitter, n = 2, by = week) %>%
mutate(sample = "temporal") %>%
bind_rows(
classified_tweets %>% # policy field sample
filter(!(doc_id %in% tweet_sample_1$doc_id), # not in previous samples
!(doc_id %in% (classified_tweets %>% # not in temporal sample
seed_sample(seed_twitter, n = 2, by = week) %>% pull(doc_id)))) %>%
seed_sample(seed_twitter, n = 24, by = policy_field) %>%
mutate(sample = "policy")
) %>%
seed_sample(seed_twitter, n = 700, weight_by = ifelse(sample == "temporal", 0.6, 0.4)) %>% # pull 700 of the 1000 documents, maintaining the 60/40 ratio
bind_rows(twitter_intercoder_sample) %>%
select(doc_id, `_source.created_at`, `_source.text`,
policy_field, sample)
tweet_sample_2 %>% summarise(n = n(), .by = sample)
tweet_sample_3 <- classified_tweets %>% # temporal sample
filter(!(doc_id %in% tweet_sample_1$doc_id), # not in previous samples
!(doc_id %in% tweet_sample_2$doc_id)) %>%
seed_sample(seed_twitter, n = 2, by = week) %>%
mutate(sample = "temporal") %>%
bind_rows(
classified_tweets %>% # policy field sample
filter(!(doc_id %in% tweet_sample_1$doc_id), # not in previous samples
!(doc_id %in% tweet_sample_2$doc_id),
!(doc_id %in% (classified_tweets %>% # not in temporal sample
seed_sample(seed_twitter, n = 2, by = week) %>% pull(doc_id)))) %>%
seed_sample(seed_twitter, n = 24, by = policy_field) %>%
mutate(sample = "policy")
) %>%
seed_sample(seed_twitter, n = 700, weight_by = ifelse(sample == "temporal", 0.6, 0.4)) %>% # pull 700 of the 1000 documents, maintaining the 60/40 ratio
bind_rows(twitter_intercoder_sample) %>%
select(doc_id, `_source.created_at`, `_source.text`,
policy_field, sample)
tweet_sample_3 %>% summarise(n = n(), .by = sample)
#### export samples
export_sample <- function(data, sort_col, filename) {
data <- as.data.frame(data)
data["correct"] <- NA # add empty column for T/F indicators
data %>%
arrange(!!as.name(sort_col)) %>% # order by date
mutate(intercoder_sample = case_when((doc_id %in% twitter_intercoder_sample$doc_id) ~ TRUE, # indicate intercoder sample
.default = FALSE)) %>%
arrange(desc(intercoder_sample)) %>% # intercoder sample first
select(!c(any_of("sample"))) %>% # drop sample indicator
mutate(across(.cols = where(is.character), ~ utf8::as_utf8(.x))) %>% # utf8 conversion
write.xlsx(file = filename)
}
export_sample(twitter_intercoder_sample, "_source.created_at", "evaluation_samples/twitter_sample_intercoder.xlsx")
export_sample(tweet_sample_1, "_source.created_at", "evaluation_samples/twitter_sample_1.xlsx")
export_sample(tweet_sample_2, "_source.created_at", "evaluation_samples/twitter_sample_2.xlsx")
export_sample(tweet_sample_3, "_source.created_at", "evaluation_samples/twitter_sample_3.xlsx")
# #### in some cases, the text body of the samples was missing (due to RTs being present in the classified tweets, but not in the join data used)
#
# tweet_sample_1_missing <- tweet_sample_1 %>% filter(is.na(`_source.text`)) %>%
# select(`_id`) %>%
# left_join(tweet_data_full %>% select(`_id`, `_source.created_at`,
# `_source.real_name`, `_source.text`),
# by = "_id")
#
# export_sample(tweet_sample_1_missing, "_source.created_at",
# "evaluation_samples/twitter_sample_1_missing.xlsx")
#
#
# tweet_sample_2_missing <- tweet_sample_2 %>% filter(is.na(`_source.text`)) %>%
# select(`_id`) %>%
# left_join(tweet_data_full %>% select(`_id`, `_source.created_at`,
# `_source.real_name`, `_source.text`),
# by = "_id")
#
# export_sample(tweet_sample_2_missing, "_source.created_at",
# "evaluation_samples/twitter_sample_2_missing.xlsx")
#
#
# tweet_sample_3_missing <- tweet_sample_3 %>% filter(is.na(`_source.text`)) %>%
# select(`_id`) %>%
# left_join(tweet_data_full %>% select(`_id`, `_source.created_at`,
# `_source.real_name`, `_source.text`),
# by = "_id")
#
# export_sample(tweet_sample_3_missing, "_source.created_at",
# "evaluation_samples/twitter_sample_3_missing.xlsx")
### News
#### 437 articles per coder, 129 docs intercoder sample = 1.049 total docs classified (~0.001%) with 12.5% of docs coded by all coders
#### 66% of sampled data are pulled over the timeframes (weeks),
#### 20% are pulled over docs associated with policy fields (highest association) to not underrepresent small fields,
#### 14% are pulled over outlet, to represent all outlets
news_sample_1 <-
classified_news %>% # temporal sample
seed_sample(seed_news, n = 1, by = week) %>%
mutate(sample = "temporal") %>%
bind_rows( # policy field sample
classified_news %>%
filter(!(doc_id %in% (classified_news %>% # not in temporal sample
seed_sample(seed_news, n = 1, by = week) %>% pull(doc_id)))) %>%
seed_sample(seed_news, n = 5, by = policy_field) %>%
mutate(sample = "policy")
) %>%
bind_rows( # outlet sample
classified_news %>%
filter(!(doc_id %in% (classified_news %>% # not in temporal sample
seed_sample(seed_news, n = 1, by = week) %>%
pull(doc_id)))) %>%
filter(!(doc_id %in% (classified_news %>% # not in policy sample
filter(!(doc_id %in% (classified_news %>%
seed_sample(seed_news, n = 1,
by = week) %>%
pull(doc_id)))) %>%
seed_sample(seed_news, n = 5, by = policy_field)))) %>%
seed_sample(seed_news, n = 9, by = outlet) %>%
mutate(sample = "outlet")
) %>%
select(doc_id, `_source.estimated_date`, outlet, `_source.title`,
`_source.body`, policy_field, sample)
news_sample_1 %>% summarise(n = n(), .by = sample) %>%
mutate(percent = percent(n/sum(n), accuracy = 0.01)) # a roughly 66/19/15 ratio
news_intercoder_sample <- news_sample_1 %>% # make intercoder sample
seed_sample(seed_news, n = 43, by = sample) # .. with even amounts of temporal and policy sourced tweets
news_sample_2 <- classified_news %>% # temporal sample
filter(!(doc_id %in% news_sample_1$doc_id)) %>% # not in previous samples
seed_sample(seed_news, n = 1, by = week) %>%
mutate(sample = "temporal") %>%
bind_rows(
classified_news %>% # policy field sample
filter(!(doc_id %in% news_sample_1$doc_id)) %>% # not in previous samples
filter(!(doc_id %in% (classified_news %>% # not in temporal sample
seed_sample(seed_news, n = 1, by = week) %>%
pull(doc_id)))) %>%
seed_sample(seed_news, n = 5, by = policy_field) %>%
mutate(sample = "policy")
) %>%
bind_rows( # outlet sample
classified_news %>%
filter(!(doc_id %in% news_sample_1$doc_id)) %>% # not in previous samples
filter(!(doc_id %in% (classified_news %>% # not in temporal sample
seed_sample(seed_news, n = 1, by = week) %>%
pull(doc_id)))) %>%
filter(!(doc_id %in% (classified_news %>% # not in policy sample
filter(!(doc_id %in% (classified_news %>%
seed_sample(seed_news, n = 1,
by = week) %>%
pull(doc_id)))) %>%
seed_sample(seed_news, n = 5, by = policy_field)))) %>%
seed_sample(seed_news, n = 9, by = outlet) %>%
mutate(sample = "outlet")
) %>%
seed_sample(seed_news, n = 308, weight_by = ifelse(sample == "temporal", 0.66,
ifelse(sample == "policy", 0.20, 0.14))) %>% # pull 700 of the 1000 documents, maintaining the 60/40 ratio
bind_rows(news_intercoder_sample) %>%
select(doc_id, `_source.estimated_date`, outlet, `_source.title`,
`_source.body`, policy_field, sample)
news_sample_2 %>% summarise(n = n(), .by = sample) %>%
mutate(percent = percent(n/sum(n), accuracy = 0.01)) # a roughly 66/19/15 ratio
news_sample_3 <- classified_news %>% # temporal sample
filter(!(doc_id %in% news_sample_1$doc_id), # not in previous samples
!(doc_id %in% news_sample_2$doc_id)) %>%
seed_sample(seed_news, n = 1, by = week) %>%
mutate(sample = "temporal") %>%
bind_rows(
classified_news %>% # policy field sample
filter(!(doc_id %in% news_sample_1$doc_id), # not in previous samples
!(doc_id %in% news_sample_2$doc_id)) %>%
filter(!(doc_id %in% (classified_news %>% # not in temporal sample
seed_sample(seed_news, n = 1, by = week) %>%
pull(doc_id)))) %>%
seed_sample(seed_news, n = 5, by = policy_field) %>%
mutate(sample = "policy")
) %>%
bind_rows( # outlet sample
classified_news %>%
filter(!(doc_id %in% news_sample_1$doc_id), # not in previous samples
!(doc_id %in% news_sample_2$doc_id)) %>%
filter(!(doc_id %in% (classified_news %>% # not in temporal sample
seed_sample(seed_news, n = 1, by = week) %>%
pull(doc_id)))) %>%
filter(!(doc_id %in% (classified_news %>% # not in policy sample
filter(!(doc_id %in% (classified_news %>%
seed_sample(seed_news, n = 1,
by = week) %>%
pull(doc_id)))) %>%
seed_sample(seed_news, n = 5, by = policy_field)))) %>%
seed_sample(seed_news, n = 9, by = outlet) %>%
mutate(sample = "outlet")
) %>%
seed_sample(seed_news, n = 308, weight_by = ifelse(sample == "temporal", 0.66,
ifelse(sample == "policy", 0.20, 0.14))) %>% # pull 700 of the 1000 documents, maintaining the 60/40 ratio
bind_rows(news_intercoder_sample) %>%
select(doc_id, `_source.estimated_date`, outlet, `_source.title`,
`_source.body`, policy_field, sample)
news_sample_3 %>% summarise(n = n(), .by = sample) %>%
mutate(percent = percent(n/sum(n), accuracy = 0.01)) # a roughly 66/19/15 ratio
#### export samples
export_sample <- function(data, sort_col, filename) {
data <- as.data.frame(data)
data["correct"] <- NA # add empty column for T/F indicators
data <- data %>% mutate(`_source.body` = case_when( # truncate long documents
str_length(`_source.body`) > 32767 ~ str_trunc(`_source.body`,
width = 32767,
side = "right"),
.default = `_source.body`))
data %>%
arrange(!!as.name(sort_col)) %>% # order by date
mutate(intercoder_sample = case_when((doc_id %in% news_intercoder_sample$doc_id) ~ TRUE, # indicate intercoder sample
.default = FALSE)) %>%
arrange(desc(intercoder_sample)) %>% # intercoder sample first
select(!c(any_of("sample"))) %>% # drop sample indicator
mutate(across(.cols = where(is.character), ~ utf8::as_utf8(.x))) %>% # utf8 conversion
write.xlsx(file = filename)
}
export_sample(news_intercoder_sample, "_source.estimated_date", "evaluation_samples/news_sample_intercoder.xlsx")
export_sample(news_sample_1, "_source.estimated_date", "evaluation_samples/news_sample_1.xlsx")
export_sample(news_sample_2, "_source.estimated_date", "evaluation_samples/news_sample_2.xlsx")
export_sample(news_sample_3, "_source.estimated_date", "evaluation_samples/news_sample_3.xlsx")