-
Notifications
You must be signed in to change notification settings - Fork 1
/
01-ergm_migration.Rmd
265 lines (203 loc) · 5.1 KB
/
01-ergm_migration.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
---
title: "Exploring Brazil data"
author: "Juan Rocha"
date: "May 2022"
output:
html_document:
theme:
bootswatch: cosmo
code_font:
google: Fira Code
df_print: paged
code_folding: hide
toc: true
toc_float:
collapsed: true
smooth_control: true
toc_depth: 3
fig_caption: true
highlight: pygments
self_contained: false
lib_dir: libs
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE, eval = FALSE)
library(tidyverse)
library(fs)
```
Load libraries:
```{r warning=FALSE, message=FALSE, eval = TRUE}
library(tidyverse)
library(fs)
library(network)
library(ergm)
library(ergm.count)
library(tictoc)
```
## Data cleaning
I have pre-cleaned and compiled the data into the network and attributes datasets. The following chunks are not evaluated, but showcase how the data was cleaned.
```{r message = FALSE, warning = FALSE}
fls <- dir_ls("data/monthly")
fls
fls <- fls |> str_subset("2017") |> str_subset("idp")
fls
out <- map(fls, read_csv, skip = 0, col_names = 1:74)
class(out)
length(out)
```
Create adjacency matrix
```{r}
out <- map(out, function(x) as.matrix(x))
mat <- out[[1]] + out[[2]] + out[[3]] + out[[4]] + out[[5]] + out[[6]] + out[[7]] + out[[8]] + out[[9]] + out[[10]] + out[[11]] + out[[12]]
```
Process network attributes:
```{r message = FALSE, warning=FALSE}
fls <- dir_ls("data/monthly")
fls <- fls |> str_subset("2017") |> str_subset("var")
out <- map(fls, read_csv)
out <- map2(.x = out, .y = 1:12, function(x,y) {x$month <- y; return(x)})
out <-bind_rows(out)
out
```
```{r}
dat <- out |> group_by(id) |>
summarize(
fatality = sum(fatality),
conflict_occ = sum(conflict_occ),
spi_m = mean(spi),
spi_max = max(spi),
spi_min = min(spi),
pop = mean(Pop2014UNFPA),
urban = mean(Urban2014UNFPA),
rural = mean(Rural2014UNFPA)
)
dat
```
Create network:
```{r}
net <- network(mat, directed = TRUE)
net
```
Add attributes:
```{r}
net %e% "flow" <- mat
net %v% "fatality" <- dat$fatality
net %v% "conflict" <- dat$conflict_occ
net %v% "spi_m" <- dat$spi_m
net %v% "spi_max" <- dat$spi_max
net %v% "spi_min" <- dat$spi_min
net %v% "pop" <- dat$pop
net %v% "urban" <- dat$urban
net %v% "rural" <- dat$rural
plot(net)
```
Observed distribution of triangles
```{r}
summary(net~triadcensus)
## check
#??sna::triad.classify
```
## ERGMs
Same for the ergms, they were pre-computed to save computing time. The code is only to showcase what was done.
```{r eval = TRUE}
load("data/simple_ergms.RData")
```
### Non-weighted
```{r }
tic()
f0 <- ergm(net ~ edges)
toc() # 0.15s
```
```{r}
tic()
f1 <- ergm(net ~ edges + diff("spi_m") +diff("spi_max")+diff("spi_min")+
diff("conflict") + diff("fatality") + nodecov("pop") +
nodecov("conflict") + nodecov("fatality") + nodecov("spi_m") +
nodecov("spi_max") + nodecov("spi_min"))
toc() # 0.29s
```
```{r}
tic()
f2 <- ergm(net ~ edges + diff("spi_m") + diff("spi_max") +
diff("spi_min") + diff("conflict") + diff("fatality") + nodecov("pop") +
nodecov("conflict") + nodecov("fatality") + nodecov("spi_m") +
nodecov("spi_max") + nodecov("spi_min") + transitiveties)
toc() # 38s
```
```{r eval = TRUE}
summary(f0)
summary(f1)
summary(f2)
```
## Structural ERGMs
```{r}
tic()
e1 <- ergm(net ~ edges + transitiveties)
toc() #10s
```
```{r}
tic()
e2 <- ergm(net ~ edges + intransitive) #gwnsp(cutoff = 10) + gwesp(cutoff = 15)
toc() # 6s
```
```{r}
tic()
e3 <- ergm(net ~ edges + gwesp(cutoff = 15),
constraints = ~degreedist,
control = snctrl(parallel = 3)
) #gwnsp(cutoff = 10) + gwesp(cutoff = 15)
toc() # 17s in parallel
```
```{r eval = TRUE}
summary(e1)
summary(e2)
summary(e3)
```
## Weighted ERGMs
```{r}
tic()
w0 <- ergm(net ~ nonzero + sum ,
response = "flow",
reference = ~Poisson,
#constraints = ~degreedist,
control = snctrl(parallel = 3))
toc() # 358s
```
```{r eval = TRUE}
summary(w0)
```
```{r}
tic()
w1 <- ergm(net ~ nonzero + sum + diff("spi_m") + diff("spi_max") +
diff("spi_min") + diff("conflict") + diff("fatality") + nodecov("pop") +
nodecov("conflict") + nodecov("fatality") + nodecov("spi_m") +
nodecov("spi_max") + nodecov("spi_min"),
response = "flow",
reference = ~Poisson,
#constraints = ~degreedist,
control = snctrl(parallel = 3))
toc() # 628s
```
```{r eval = TRUE}
summary(w1)
```
Takes a while and the models are degenerated
```{r eval = TRUE}
mcmc.diagnostics(w1)
```
Save work space:
```{r}
# save(e1, e2, e3, f0, f1, f2, w0, w1, net, file = "data/simple_ergms.RData")
```
## Combined
```{r eval=TRUE, results='asis'}
stargazer::stargazer(
f0,f1,f2,e1,e2,e3,w0,w1,
type = "html", digits = 2
)
```
## Next steps
- Do we need weighted ergms?
- What other terms are worth exploring?
- triangles are computationally expensive, geometrically weighted terms are possible but time consuming and only converging when constrained.
- What are sensible choices for constraining the sample space?