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
|
library(igraph)
setwd("~/Documents/Violence Cascades/")
load('Raw Data/lcc.RData')
library(foreach)
library(doMC)
registerDoMC(cores=4)
lcc2 = remove.edge.attribute(lcc,'weight')
vics = split(vic_ids, ceiling(seq_along(vic_ids)/500))
dag_dat_lcc = c()
for(i in 1:length(vics)){
ptm = proc.time()
print(c(i,length(vics)))
vic_ids = unlist(vics[i], use.names=F)
ddl = foreach (u = vic_ids, .combine=rbind) %dopar% {
if ((which(vic_ids==u) %% 100)==0) print(which(vic_ids==u))
nbhd = unlist(neighborhood(lcc2,nodes=u,order=3)) # get nodes within neighborhood
nbhd = nbhd[-1] # don't want to include u in the neighborhood
dists = as.numeric(shortest.paths(lcc2,u,nbhd))
# make edge for every infection
ddlu = data.frame(matrix(nrow=1,ncol=4))
ei = 1
for (j in c(17:21,16)){
tu = lcc_verts[u,j]
if (is.na(tu)) next
ddlu[ei:(ei+length(nbhd)-1),] = data.frame(rep(u,length(nbhd)), nbhd,
rep(tu,length(nbhd)), dists, row.names=NULL)
ei = ei + length(nbhd)
}
return(ddlu)
}
dag_dat_lcc = rbind(dag_dat_lcc,ddl)
print(proc.time()-ptm)
}
colnames(dag_dat_lcc) = c('from','to','t1','dist')
rownames(dag_dat_lcc) = NULL
save(dag_dat_lcc, file='Results/dag_dat_lcc.RData')
write.csv(dag_dat_lcc, file='Results/dag_dat_lcc.csv')
# dag_dat_vics = dag_dat_lcc[!is.na(dag_dat_lcc$t2),]
# save(dag_dat_lcc_vics, file='Results/dag_dat_lcc_vics.RData')
# write.csv(dag_dat_lcc_vics, file='Results/dag_dat_lcc_vics.csv')
####
# create lcc_vic_times
vic_times_lcc = lcc_verts[,c('name','nonfatal_day_1','nonfatal_day_2',
'nonfatal_day_3','nonfatal_day_4',
'nonfatal_day_5','fatal_day')]
save(vic_times_lcc, file='Results/vic_times_lcc.RData')
write.csv(vic_times_lcc, file='Results/vic_times_lcc.csv')
|