-
Notifications
You must be signed in to change notification settings - Fork 0
/
Risky-Business.Rmd
276 lines (210 loc) · 7.99 KB
/
Risky-Business.Rmd
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
---
title: "Risky Business"
author: "Jack Carter"
date: "2/21/2022"
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(dplyr)
library(jsonlite)
library(knitr)
library(ggplot2)
library(ggthemes)
# reads in the risk headlines.
data <- read_csv("C:\\Users\\HUAWEI\\Desktop\\Projects\\Risky-Business\\Data\\risk_headlines.csv") %>%
filter(!Category == "regime_instability")
# inserted here to make the code example under
# the Manually Check Labels section work.
scores <- data
# gets the z score for a specific set of data points.
get_z_score <- function(data) {
mean <- mean(data)
sigma <- sd(data)
z_scores <- list()
for(i in 1:length(data)) {
z_scores[i] <- (data[i] - mean) / sigma
}
return(unlist(z_scores))
}
# get the z-scores for each category of a single continent.
get_continent_z_scores <- function(continent) {
data <- data[data["Continent"]==continent,]
count <- data %>%
group_by(Category) %>%
count()
z_scores <- tibble(z_score = get_z_score(count$n))
final_data <- count[,1] %>%
cbind(z_scores) %>%
mutate(Continent = continent)
return(final_data)
}
# gets the z-scores for each category by continent.
z_scores <- rbind_pages(lapply(unique(data$Continent), get_continent_z_scores))
# my personal plot theme for data visualizations.
my_theme <- theme_economist_white(gray_bg = FALSE) +
theme(plot.title = element_text(hjust = 0.5,
vjust = 10,
size = 10,
color = "#474747"),
plot.margin = unit(c(1.5, 1, 1.5, 1), "cm"),
axis.text = element_text(size = 9,
color = "gray30"),
axis.text.x=element_text(vjust = -2.5),
axis.title.x = element_text(size = 9,
color = "gray30",
vjust = -10),
axis.title.y = element_text(size = 9,
color = "gray30",
vjust = 10),
legend.direction = "vertical",
legend.position = "right",
legend.title = element_blank(),
legend.text = element_text(size = 11,
color = "gray20"),
legend.margin=margin(1, -15, 1, 0),
legend.spacing.x = unit(0.25, "cm"),
legend.key.size = unit(1, "cm"),
legend.key.height = unit(0.75, "cm"),
strip.text = element_text(hjust = 0.5,
vjust = 1,
size = 10,
color = "#474747"),
panel.spacing = unit(2, "lines"))
```
## **Summary**
This project uses web scraping, regex and a labeling mechanism to
uncover the relative distribution of different violence risks in the
emerging world. The results include 822 risk headlines from 140 news sources. Just as Tom Cruise discovered in Risky Business,
those seeking to venture into the unknown could find things get out of
hand quickly if they’re not careful.
## Results
The results show a strong risk of military conflict in Europe due to the Ukraine crisis and a high risk of internal conflict, such as organized crime, social unrest and terrorism, in regions with weak states like Africa and Latin America.
```{r, echo = FALSE, message = FALSE, warning = FALSE, dpi=600}
# total bar plot.
total_data <- data %>%
group_by(Category, Continent) %>%
count()
total_data %>%
ggplot(aes(x=Continent, y=n, fill=Category)) +
geom_bar(stat = "identity", position = "fill") +
ggtitle("Continent") +
ylab("%") +
xlab("") +
my_theme +
theme(axis.text.y=element_blank()) +
scale_fill_discrete(name = "Risks", labels = c("Military Conflict",
"Organized Crime",
"Social Unrest",
"Terrorism")) +
scale_x_discrete(guide = guide_axis(n.dodge=2))
```
## **Disclaimer**
The data above show only the relative distribution of headlines for each region, not differences in the total number of headlines. This means we can only hope to better understand variations in the types of risks in such regions, not how dangerous they are overall.
## **Method**
### **1) Assemble Links Database:**
A database containing over 250 online newspapers in the emerging world is
assembled.
```{r, echo = FALSE, message = FALSE, warning = FALSE, dpi=600}
# reads in the relevant links data frame.
read_links_df <- function(region) {
links <- read_csv(
paste0(
"C:\\Users\\HUAWEI\\Desktop\\Projects\\Risky-Business\\data\\Links\\",
region,
"_links.csv"
)
)
return(links)
}
# reads in the links data frame.
links <- read_links_df("africa") %>%
rbind(read_links_df("asia")) %>%
rbind(read_links_df("europe")) %>%
rbind(read_links_df("latin_america")) %>%
rbind(read_links_df("mena"))
# creates a summary table of the total hits for each term.
summary_table <- links %>%
group_by(Continent) %>%
summarise("Total Links"=length(Site)) %>%
spread(Continent, "Total Links")
kable(summary_table)
```
### **2) Scrape Headlines:**
The links in the database is scraped several times a week from 17 January 2022 to present.
—EXAMPLE CODE SNIPET—
```{r, echo = TRUE, message = FALSE, warning = FALSE, dpi=600}
# reads in the link html text.
read_link <- function(link) {
elements <- c("h1", "h2", "h3", "h4", "h5")
headlines <- read_html(link)
html <- lapply(elements, html_elements, x = headlines)
text <- unlist(lapply(html, html_text))
on.exit(closeAllConnections())
return(text)
}
```
### **3) Regex:**
A series of regex strings, including 1) keywords, 2) actors and actions, 3) and hyperbolic situations, are combined to calculate the scores of four different kinds of violence.
—EXAMPLE CODE SNIPET—
```{r, echo = TRUE, message = FALSE, warning = FALSE, dpi=600}
# strong capture words.
strong_capture_words <- paste0(
"(",
capture <- "captur(e|es|ed|ing)\\b|",
hijack <- "hijac(k|ks|ked|king)\\b|",
hold_prisoner <- "h(old|olds|eld|olding) (hostag(e|es)|to ransom)\\b|",
take_prisoner <- "t(ake|akes|ook|aken|aking) (prisoner|hostage)\\b|",
abduct <- "abduc(t|ts|ted|ting)\\b|",
kidnap <- "kidna(p|ps|ped|ping)\\b",
")"
)
```
### **4) Labeling:**
The headlines are assigned one of the four violence type labels, including military conflict, organized crime, social unrest and terrorism.
—EXAMPLE CODE SNIPET—
```{r, echo = TRUE, message = FALSE, warning = FALSE, dpi=600}
# determines the main category under which the headlines fall.
assign_category <- function(scores) {
data <- tibble()
for(i in 1:nrow(scores)) {
data[i,1] <- if(sum(scores[i,1:5])==0) "None"
else {
categories[max(which(scores[i,1:5]==max(scores[i,1:5])))]
}
}
return(data)
}
```
### **5) Check & Re-assign:**
The top 100 scoring headlines are checked each time the data is scraped, with only those deemed genuine violence risks kept and those mislabeled re-assigned.
—EXAMPLE CODE SNIPET—
```{r, echo = TRUE, message = FALSE, warning = FALSE, dpi=600}
# creates a list of mislabeled headlines.
mislabeled <- tibble(
number = c(2),
category = c("organized_crime")
)
# adjusts mislabeled headlines.
mislabeled_adjust <- function(scores) {
scores <- scores
for(i in 1:nrow(mislabeled))
scores[pull(mislabeled[i,1]),3] <- mislabeled$category[i]
return(scores)
}
mislabeled_adjusted <- mislabeled_adjust(scores)
```
## **Sources**
- Chong (2021) https://towardsdatascience.com/regular-expressions-clearly-explained-with-examples-822d76b037b4
- The Economist (2021) https://www.economist.com/leaders/2021/07/31/unrest-and-economic-underperformance-stalk-the-emerging-world
- UN (2020) https://www.un.org/en/un75/new-era-conflict-and-violence
- World Bank (2022) https://www.worldbank.org/en/topic/fragilityconflictviolence/overview#1