-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoa-code_data_figures.Rmd
345 lines (257 loc) · 9 KB
/
soa-code_data_figures.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
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
---
title: 'Scales of Aggression: Figures'
author: "Blau & Paxton"
output:
html_document:
keep_md: yes
number_sections: yes
---
This R markdown creates figures for our project investigating the fractal
structure of conflict from international to interpersonal levels (Blau & Paxton,
in press, *Complexity*).
To run this from scratch, you will need the following files:
* `./data/wars-iei.csv`: File containing inter-war-interval data.
* `./data/riots-iei.csv`: File containing inter-riot-intervals data.
* `./data/crime_data/appleton-ici.csv`: File containing inter-crime-interval data
(violent crime only) for Appleton, WI.
* `./data/prepped_data-DCC.csv`: File with raw movement data derived from head-mounted
accelerometers. Data were originally collected as part of Paxton and Dale
(2017, *Frontiers in Psychology*). Data are freely available in the OSF repository for
the original project (https://osf.io/x9ay6/) and are linked in the OSF repository
for the current project (https://osf.io/8qcya/).
**Code written by**: A. Paxton (University of Connecticut)
**Date last modified**: 04 November 2020
***
# Preliminaries
```{r preliminaries, message=FALSE}
# clear things out
rm(list=ls())
# load in the required packages
library(tidyverse)
library(ggplot2)
library(viridis)
library(cowplot)
library(nonlinearTseries)
```
## Load data
```{r load-data}
# load in the riot data
riot_data = read.table('./data/riots-iei.csv',
sep=',', header=FALSE) %>%
rename(IEI = V1) %>%
rownames_to_column("Event") %>%
mutate(Event = as.numeric(Event))
# load in the war data
war_data = read.table('./data/wars-iei.csv',
sep=',', header=FALSE) %>%
rename(IEI = V1) %>%
rownames_to_column("Event") %>%
mutate(Event = as.numeric(Event))
# load in the crime data
crime_data = read.table('./data/crime_data/appleton-ici.csv',
sep=',', header=FALSE) %>%
rename(IEI = V1) %>%
rownames_to_column("Event") %>%
mutate(Event = as.numeric(Event))
# load in affiliative movement data
affiliative_movement_data = read.table('./data/movement_data/threshold-partic_1_dyad_11_type_0.csv',
sep=',', header=TRUE) %>%
# calculate IEI
mutate(IEI = t - lag(t)) %>%
drop_na() %>%
# create event counter
rownames_to_column("Event") %>%
mutate(Event = as.numeric(Event))
# load in argumentative movement data
argumentative_movement_data = read.table('./data/movement_data/threshold-partic_1_dyad_16_type_1.csv',
sep=',', header=TRUE) %>%
# calculate IEI
mutate(IEI = t - lag(t)) %>%
drop_na() %>%
# create event counter
rownames_to_column("Event") %>%
mutate(Event = as.numeric(Event))
```
## Specify global plotting parameters
```{r global-plotting}
# get total palette
total_palette = viridis(5)
# specify each event type
affiliative_color = total_palette[5]
argumentative_color = total_palette[4]
crime_color = total_palette[3]
riot_color = total_palette[2]
war_color = total_palette[1]
# specify mean H values
affiliative_H_value = .637
argumentative_H_value = .722
crime_H_value = .534
riot_H_value = .741
war_H_value = .743
```
***
# Plot timescales
```{r plot-war-ts}
# plot the war timeseries
war_ts = ggplot(war_data, aes(y = IEI,
x = Event)) +
geom_path(color=war_color) +
theme_light() +
ggtitle(paste0("Very Macro-Scale Data:\n",
"War Timeseries"))
# display here
war_ts
```
```{r plot-riot-ts}
# plot the riot timeseries
riot_ts = ggplot(riot_data, aes(y = IEI,
x = Event)) +
geom_path(color=riot_color) +
theme_light() +
ggtitle(paste0("Macro-Scale Data:\n",
"Riot Timeseries"))
# display here
riot_ts
```
```{r plot-crime-ts}
# plot the crime timeseries
crime_ts = ggplot(crime_data, aes(y = IEI,
x = Event)) +
geom_path(color=crime_color) +
theme_light() +
ggtitle(paste0("Micro-Scale Data:\n",
"Representative Violent Crime Timeseries"))
# display here
crime_ts
```
```{r plot-affiliative-ts}
# plot the movement timeseries
affiliative_ts = ggplot(affiliative_movement_data, aes(y = IEI,
x = Event)) +
geom_path(color=affiliative_color) +
theme_light() +
ggtitle(paste0("Very Micro-Scale Data:\n",
"Representative Affiliative Movement Timeseries"))
# display here
affiliative_ts
```
```{r plot-argument-ts}
# plot the movement timeseries
argument_ts = ggplot(argumentative_movement_data, aes(y = IEI,
x = Event)) +
geom_path(color=argumentative_color) +
theme_light() +
ggtitle(paste0("Very Micro-Scale Data:\n",
"Representative Argument Movement Timeseries"))
# display here
argument_ts
```
```{r stack_ts_plots}
# create stacked plot visualization
stacked_ts = cowplot::plot_grid(war_ts,
riot_ts,
crime_ts,
argument_ts,
affiliative_ts,
nrow=5,
rel_widths=c(1))
# save it
save_plot(filename='./figures/soa-stacked_ts_figure.jpg',
plot=stacked_ts,
base_height = 9,
base_width = 7,
dpi = 300)
```
***
# Plot DFA plots
```{r plot-war-dfa}
# calculate war DFA and convert to usable dataframe
war_dfa = nonlinearTseries::dfa(war_data$IEI,
do.plot=FALSE)
war_dfa_df = data.frame(bin = war_dfa$window.sizes,
fluctuation = war_dfa$fluctuation.function)
# plot with ggplot
war_dfa_plot = ggplot(war_dfa_df, aes(y = log(fluctuation),
x = log(bin))) +
geom_path(color = war_color) +
theme_light() +
ggtitle(paste0("Very Macro-Scale:\nWars"))
# show here
war_dfa_plot
```
```{r plot-riot-dfa}
# calculate riot DFA and convert to usable dataframe
riot_dfa = nonlinearTseries::dfa(riot_data$IEI,
do.plot=FALSE)
riot_dfa_df = data.frame(bin = riot_dfa$window.sizes,
fluctuation = riot_dfa$fluctuation.function)
# plot with ggplot
riot_dfa_plot = ggplot(riot_dfa_df, aes(y = log(fluctuation),
x = log(bin))) +
geom_path(color = riot_color) +
theme_light() +
ggtitle(paste0("Macro-Scale:\nRiots"))
# show here
riot_dfa_plot
```
```{r plot-crime-dfa}
# calculate crime DFA and convert to usable dataframe
crime_dfa = nonlinearTseries::dfa(crime_data$IEI,
do.plot=FALSE)
crime_dfa_df = data.frame(bin = crime_dfa$window.sizes,
fluctuation = crime_dfa$fluctuation.function)
# plot with ggplot
crime_dfa_plot = ggplot(crime_dfa_df, aes(y = log(fluctuation),
x = log(bin))) +
geom_path(color = crime_color) +
theme_light() +
ggtitle(paste0("Micro-Scale:\nRepresentative Municipality"))
# show here
crime_dfa_plot
```
```{r plot-affiliative-dfa}
# calculate affiliative DFA and convert to usable dataframe
affiliative_dfa = nonlinearTseries::dfa(affiliative_movement_data$IEI,
do.plot=FALSE)
affiliative_dfa_df = data.frame(bin = affiliative_dfa$window.sizes,
fluctuation = affiliative_dfa$fluctuation.function)
# plot with ggplot
affiliative_dfa_plot = ggplot(affiliative_dfa_df, aes(y = log(fluctuation),
x = log(bin))) +
geom_path(color = affiliative_color) +
theme_light() +
ggtitle(paste0("Very Micro-Scale:\nRepresentative Affiliative"))
# show here
affiliative_dfa_plot
```
```{r plot-argument-dfa}
# calculate argument DFA and convert to usable dataframe
argument_dfa = nonlinearTseries::dfa(argumentative_movement_data$IEI,
do.plot=FALSE)
argument_dfa_df = data.frame(bin = argument_dfa$window.sizes,
fluctuation = argument_dfa$fluctuation.function)
# plot with ggplot
argument_dfa_plot = ggplot(argument_dfa_df, aes(y = log(fluctuation),
x = log(bin))) +
geom_path(color = argumentative_color) +
theme_light() +
ggtitle(paste0("Very Micro-Scale:\nRepresentative Argument"))
# show here
argument_dfa_plot
```
```{r stack_dfa_plots}
# create stacked plot visualization
stacked_dfa_plot = cowplot::plot_grid(war_dfa_plot,
riot_dfa_plot,
crime_dfa_plot,
affiliative_dfa_plot,
argument_dfa_plot,
nrow=5,
rel_widths=c(1))
# save it
save_plot(filename='./figures/soa-stacked_dfa_figure.jpg',
plot=stacked_dfa_plot,
base_height = 9,
base_width = 2.8,
dpi = 300)
```