summaryrefslogtreecommitdiffstats
path: root/R Scripts/generate-dag-dat.R
blob: adef5f667b0012c45565c56bab6f71eef704dbd3 (plain)
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
library(igraph)
setwd("~/Documents/Violence Cascades/")
load('Raw Data/lcc.RData')

library(foreach)
library(doMC)
registerDoMC(cores=4)

edgeWeights = function(eis){return(c(lcc_edges$weight[eis],Inf,Inf)[1:3])}
lcc2 = remove.edge.attribute(lcc,'weight')

vics = split(vic_ids, ceiling(seq_along(vic_ids)/98))
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(lcc,nodes=u,order=1)) # get nodes within neighborhood
    nbhd  = nbhd[-1] # don't want to include u in the neighborhood
    
    dists = as.numeric(shortest.paths(lcc2,u,nbhd))
    es = get.shortest.paths(lcc2,u,nbhd,output='epath')$epath
    weights = matrix(unlist(lapply(es,edgeWeights),use.names = F),ncol=3,byrow=T)
    
    # make edge for every infection
    ddlu = data.frame(matrix(nrow=1,ncol=7))
    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, 
                                                weights, 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','w1','w2','w3')
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