-
Notifications
You must be signed in to change notification settings - Fork 1
/
wrangle_satp.qmd
327 lines (256 loc) · 11.2 KB
/
wrangle_satp.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
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
---
title: "Wrangle SATP"
format: html
---
Load packages.
```{r}
library(tidyverse)
library(readxl)
library(janitor)
```
Read in data.
```{r}
satp_raw_data <- read_excel("data/satp-dataset.xlsx", sheet = "analysis")
```
First, let's clean up these variable names.
```{r}
satp_clean_varnames <- satp_raw_data |>
clean_names() |>
rename(
incident_number = incidence_number,
longitude = longitude_dd,
latitude = latitude_dd,
perpetrator = type_of_perpetrator,
first_action = first_action_type,
second_action = second_action_type,
third_action = third_action_type,
action_successful = was_the_action_successful,
first_target = first_target_type,
second_target = second_target_type,
first_civilian_target = first_civilian_target_type,
second_civilian_target = second_civilian_target_type,
total_fatalities = total_number_of_fatalities,
govt_official_fatalities = government_official_fatalties,
other_armed_grp_fatalities = non_maoist_armed_group_fatalities,
total_injuries = total_number_of_injuries,
govt_official_injuries = government_official_injuries,
other_armed_grp_fatalities = non_maoist_armed_group_fatalities,
property_damage = was_there_any_reported_property_damage,
value_property_damage = if_yes_to_property_damage_total_value_or_nature_of_property_damage,
total_abducted = total_number_of_hostages_or_kidnappings,
total_arrests = total_number_of_arrests,
total_surrenders = total_number_of_surrenders
)
glimpse(satp_clean_varnames)
```
Looking at these variables, most seem worth keeping except for the name of the coder, the number of perpetrators (usually not reported) and the value of property damage (not reported in a unified way). I am going to drop the name of the coder and the number of perpetrators for now. The value of property damage I am going to keep because I wonder if an LLM could help come up with a better and more useful coding.
```{r}
satp_selected_vars <- satp_clean_varnames |>
select(!c(last_name, total_number_of_perpetrators))
glimpse(satp_selected_vars)
```
Next, I would like to identify the duplicate incidents and see if there is anything we can do to merge the codings of them or at least think about how they should be handled.
```{r}
satp_duplicates <- satp_selected_vars |>
get_dupes(incident_summary)
#write_csv(satp_duplicates, "satp_duplicates.csv")
```
What I see when I inspect the duplicates are some duplicates due to the fact that a report covered connected incidents that occurred on the same day in multiple locations. These seem like legitimate cases where the incident report should be broken up and treated as multiple incidents.
In other cases, incident reports are duplicated because the coders saw more than one event occurring in the same incident. For example, there might have been an armed assault and seizure of weapons when police raided a Maoist hideout. These probably should have been coded as single incidents with multiple actions.
I think the plan here should be to separate these out and work on recoding them at the same time that I move forward on setting up the workflow for the LLM. Maybe I can talk to Shiva about getting some Indian students to work on recoding these incidents this summer.
Let's filter out the observations with duplicates from the original data and go from there.
```{r}
satp_no_dupes <- satp_selected_vars |>
filter(!incident_summary %in% satp_duplicates$incident_summary)
glimpse(satp_no_dupes)
```
10,729 - 808 = 9921. That seems about right.
Curious to know how many actions were successful versus unsuccessful.
```{r}
satp_no_dupes |>
count(action_successful, sort = TRUE)
```
Should I drop the unsuccessful actions? Might be interesting to see if the LLM can code for it. Let's keep them for now.
How about civilian target types? Are these worth keeping?
```{r}
satp_no_dupes |>
count(first_civilian_target, sort = TRUE)
```
```{r}
satp_no_dupes |>
count(second_civilian_target, sort = TRUE)
```
These data look OK although the categories seem to be a bit messed up, e.g. ("High Caste\\Landowner" or "Polic informer").
What about property damage?
```{r}
satp_no_dupes |>
count(property_damage, sort = TRUE)
```
Actually there were substantial numbers of incidents with property damage so let's keep that.
For the `value_property_damage` variable, we should also keep that for now and try to recode it with a high, medium, low value and then have the LLM try to replicate it.
Out of curiousity, how many of each type do we have for the first, second and third action types?
```{r}
satp_no_dupes |>
count(first_action, sort = TRUE)
```
```{r}
satp_no_dupes |>
count(second_action, sort = TRUE)
```
```{r}
satp_no_dupes |>
count(third_action, sort = TRUE)
```
## Data for Classification Models
Let's recode the action type as separate variables. First, what kinds of actions do we have?
```{r}
satp_no_dupes |>
select(first_action) |>
distinct()
```
<!-- Let's trim down the dataset. -->
<!-- ```{r} -->
<!-- satp_trimmed <- satp_no_dupes |> -->
<!-- select( -->
<!-- incident_number:second_civilian_target, -->
<!-- property_damage:value_property_damage, -->
<!-- incident_summary -->
<!-- ) -->
<!-- glimpse(satp_trimmed) -->
<!-- ``` -->
Now let's recode the action_type variable.
```{r}
satp_recoded_action_types <- satp_no_dupes |>
rowwise() |>
mutate(
armed_assault = if_else('Armed Assault' %in% c(first_action, second_action, third_action), 1, 0),
arrest = if_else('Arrest' %in% c(first_action, second_action, third_action), 1, 0),
bombing = if_else('Bombing' %in% c(first_action, second_action, third_action), 1, 0),
infrastructure = if_else('Facility/Infrastructure Attack' %in% c(first_action, second_action, third_action), 1, 0),
surrender = if_else('Surrender' %in% c(first_action, second_action, third_action), 1, 0),
seizure = if_else('Seizure of Money, Weapons, Property etc' %in% c(first_action, second_action, third_action), 1, 0),
abduction = if_else('Hijacking/Kidnapping' %in% c(first_action, second_action, third_action), 1, 0)
) |>
ungroup() |>
relocate(armed_assault:abduction, .after = third_action)
glimpse(satp_recoded_action_types)
```
Next, we will recode the success variable as a binary variable.
```{r}
satp_recoded_action_types |>
count(action_successful, sort = TRUE)
satp_recoded_success <- satp_recoded_action_types |>
mutate(
action_successful = case_when(
action_successful == "Yes" ~ 1,
action_successful == "No" ~ 0,
action_successful == "Unknown" ~ NA,
TRUE ~ NA # Catch-all for any other unexpected values
)
)
satp_recoded_success |>
count(action_successful, sort = TRUE)
glimpse(satp_recoded_success)
```
Now let's recode the civilian target types. What kinds of target types do we have?
```{r}
satp_recoded_action_types |>
select(second_target) |>
distinct()
```
1. Civilians
2. Maoist
3. Government Officials
4. Security
5. Private Property
6. Mining Company
7. NGOs
8. Government Infrastructure, Government Property
9. Non-maoist armed group, Non-Maoist Armed Group, Non-Maoist armed group
10. No Target, Unknown, None
```{r}
satp_recoded_target_types <- satp_recoded_success |>
rowwise() |>
mutate(
civilians = if_else('Civilians' %in% c(first_target, second_target), 1, 0),
maoist = if_else('Maoist' %in% c(first_target, second_target), 1, 0),
government_officials = if_else('Government Officials' %in% c(first_target, second_target), 1, 0),
security = if_else('Security' %in% c(first_target, second_target), 1, 0),
private_property = if_else('Private Property' %in% c(first_target, second_target), 1, 0),
mining_company = if_else('Mining Company' %in% c(first_target, second_target), 1, 0),
ngos = if_else('NGOs' %in% c(first_target, second_target), 1, 0),
government_infrastructure = if_else(
str_detect(first_target, "Government Infrastructure|Government Property") |
str_detect(second_target, "Government Infrastructure|Government Property"),
1, 0),
non_maoist_armed_group = if_else(
str_detect(first_target, regex("Non-Maoist armed group", ignore_case = TRUE)) |
str_detect(second_target, regex("Non-Maoist armed group", ignore_case = TRUE)),
1, 0),
no_target = if_else(str_detect(first_target, "No Target|Unknown|None"), 1, 0)
) |>
ungroup() |>
relocate(civilians:no_target, .after = second_target)
glimpse(satp_recoded_target_types)
```
Let's recode the civilian target types. What kinds of target types do we have?
```{r}
satp_recoded_target_types |>
select(first_civilian_target) |>
distinct()
```
1. Other Civilian, Other
2. Former Maoist
3. High Caste/Landowner, High Caste\\Landowner, High caste/Landowner
4. Police Informer, Polic Informer
5. Businessman
6. Aspiring Politician, Aspiring Politicians
7. Other Elite
```{r}
satp_recoded_civilian_target_types <- satp_recoded_target_types |>
rowwise() |>
mutate(
former_maoist = if_else('Former Maoist' %in% c(first_civilian_target, second_civilian_target), 1, 0),
high_caste_landowner = if_else(
!is.na(first_civilian_target) & str_detect(first_civilian_target, regex("High Caste/Landowner|High Caste\\Landowner", ignore_case = TRUE)) |
!is.na(second_civilian_target) & str_detect(second_civilian_target, regex("High Caste/Landowner|High Caste\\Landowner", ignore_case = TRUE)),
1, 0),
police_informer = if_else(
!is.na(first_civilian_target) & str_detect(first_civilian_target, "Police Informer|Polic Informer") |
!is.na(second_civilian_target) & str_detect(second_civilian_target, "Police Informer|Polic Informer"),
1, 0),
businessman = if_else('Businessman' %in% c(first_civilian_target, second_civilian_target), 1, 0),
aspiring_politician = if_else(
!is.na(first_civilian_target) & str_detect(first_civilian_target, "Aspiring Politician|Aspiring Politicians") |
!is.na(second_civilian_target) & str_detect(second_civilian_target, "Aspiring Politician|Aspiring Politicians"),
1, 0),
other_elite = if_else('Other Elite' %in% c(first_civilian_target, second_civilian_target), 1, 0),
other_civilian = if_else(
!is.na(first_civilian_target) & str_detect(first_civilian_target, "Other Civilian|Other") |
!is.na(second_civilian_target) & str_detect(second_target, "Other Civilian|Other"),
1, 0)
) |>
ungroup() |>
relocate(former_maoist:other_civilian, .after = second_civilian_target)
glimpse(satp_recoded_civilian_target_types)
```
Finally, let's recode property damage as a binary variable.
```{r}
satp_recoded_civilian_target_types |>
count(property_damage, sort = TRUE)
satp_recoded_property_damage <- satp_recoded_civilian_target_types |>
mutate(property_damage = if_else(property_damage == "Yes", 1, 0))
satp_recoded_property_damage |>
count(property_damage, sort = TRUE)
```
We need to get rid of the whitespace and return characters at the beginning of some of the cells for our character variables.
```{r}
satp_removed_whitespace <- satp_recoded_property_damage |>
mutate(across(where(is.character), ~str_trim(.)))
```
Now let's have a final look at the cleaned data and write it to a CSV gile
```{r}
satp_final <- satp_removed_whitespace
write_csv(satp_final, "data/satp_clean.csv")
glimpse(satp_final)
```