From e36b7500767da4c0a8dbd29f69667aa8fa275e8c Mon Sep 17 00:00:00 2001 From: Ben Green Date: Fri, 21 Aug 2015 13:06:12 -0400 Subject: added age to sim analysis and updated data generation for new model --- R Scripts/data-prep.R | 24 +++++----- R Scripts/generate-dag-dat.R | 94 ++++++++++++++++++++++----------------- R Scripts/predict-victims-plots.R | 10 ++--- R Scripts/sim-analysis.R | 94 ++++++++++++++++++++++----------------- 4 files changed, 123 insertions(+), 99 deletions(-) diff --git a/R Scripts/data-prep.R b/R Scripts/data-prep.R index 3104ea2..2994575 100755 --- a/R Scripts/data-prep.R +++ b/R Scripts/data-prep.R @@ -1,5 +1,5 @@ library(igraph) -setwd('~/Documents/Cascade Project/Raw Data/') +setwd('~/Documents/Violence Cascades/Raw Data/') #================ # (1) load data @@ -99,14 +99,6 @@ start_date = as.Date("2005-12-31") ## Get first arrest date in the study period for each person sub.arrests$dates = as.Date(arrests$arrest_date,format='%m/%d/%Y') sub.arrests = sub.arrests[order(sub.arrests$dates),] -sub.arrests = sub.arrests[match(unique(sub.arrests$individuals),sub.arrests$individuals),] -arrest.dates = as.Date(sub.arrests$dates,format='%m/%d/%y') -arrest.days = as.numeric(arrest.dates-start_date) -V(person)$arrest.day = arrest.days[match(V(person)$name, sub.arrests$individuals)] - -V(person)$age.arrest = floor(difftime(arrest.dates[match(V(person)$name, sub.arrests$individuals)], - V(person)$dob, - units='days')/365.25) #=================================================================== @@ -192,22 +184,30 @@ V(person)$faction.name <- as.character(gangs$FACTION_NAME[match_vector]) #=================================================================== # create id number -V(person)$id = rank(V(person)$name) # save data # person = remove.edge.attribute(person,'weight') # person_data = get.data.frame(person,'both') -save(person, file="chi-19mar2015.RData") +save(person, file="chi-19aug2015.RData") #=================================================================== # get LCC of the network lcc = induced.subgraph(person,which(clusters(person)$membership==which.max(clusters(person)$csize))) +V(lcc)$id = rank(V(lcc)$name) V(lcc)$ir_no = V(lcc)$name +V(lcc)$name = V(lcc)$id vic_ids = which(V(lcc)$vic) +lcc_edges = as_data_frame(lcc,'edges') + +# update lcc_verts lcc_verts = get.data.frame(lcc,'vertices') -lcc_edges = get.data.frame(lcc,'edges') +lcc_verts = lcc_verts[,c(1,23,24,2:22)] + +# save file save(lcc, lcc_verts, lcc_edges, vic_ids, file="lcc.RData") +##### +# old stuff lcc_data = get.data.frame(lcc,'both') lcc = set.vertex.attribute(graph=lcc, name='name', value=V(lcc)$id) row.names(lcc_data$vertices) = lcc_data$vertices$id diff --git a/R Scripts/generate-dag-dat.R b/R Scripts/generate-dag-dat.R index a2df165..adef5f6 100755 --- a/R Scripts/generate-dag-dat.R +++ b/R Scripts/generate-dag-dat.R @@ -1,46 +1,60 @@ library(igraph) -setwd("~/Documents/Cascade Project/") -load('Results/hyper-lcc.RData') - -vic_ids = which(V(hyp_lcc)$vic==TRUE) - -edgeWeights = function(eis){return(c(hyp_lcc_edges$weight[eis],Inf,Inf)[1:3])} - -dag_dat_all = data.frame(matrix(nrow=1,ncol=10)) -hyp_lcc2 = remove.edge.attribute(hyp_lcc,'weight') -ei = 1 -ptm=proc.time() -for (u in vic_ids){ - if ((which(vic_ids==u) %% 1000)==0) print(which(vic_ids==u)) - tu = hyp_lcc_verts$vic.day[u] - u_spawn = hyp_lcc_verts$spawn.date[u] - nbhd = unlist(neighborhood(hyp_lcc,nodes=u,order=3)) # get nodes within neighborhood - nbhd = nbhd[-1] # don't want to include u in the neighborhood - tvs = hyp_lcc_verts$vic.day[nbhd] - v_spawn = hyp_lcc_verts$spawn.date[nbhd] - nbhd = nbhd[tu>v_spawn & (is.na(tvs) | tu1) vics = vic_ids # sims 2 + 3 - if (sim==0) {vics = vic_ids; sim.dates = inf.dates} # data - - vic.time = c() - for (i in 1:n.infections){ - u = vics[i] - nbhd = unlist(neighborhood(graph, nodes=u, order=1)) - nbhd = intersect(vic_ids,nbhd) - nbhd = setdiff(nbhd,u) - nbhd = nbhd[u1) sim.dates[year_ids] = sample(inf.dates[year_ids]) + } +# {vics = vic_ids; sim.dates = inf.dates} # data + + vic.time = rep(NA,14885) + idx = 1 + for (i in 1:n.infections){ + u = vics[i] + nbhd = nbrs[[i]] + nbhd = intersect(vic_ids,nbhd) + nbhd = nbhd[u