Skip to content

Instantly share code, notes, and snippets.

@jfaganUK
Created January 18, 2015 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfaganUK/ad83c8378bf320614eb4 to your computer and use it in GitHub Desktop.
Save jfaganUK/ad83c8378bf320614eb4 to your computer and use it in GitHub Desktop.
analyze lots of networks
library(data.table)
library(igraph)
x <- fread("all_networks_140923.csv")
igraph.options(vertex.label = NA, edge.arrow.size = 0, vertex.size = 5)
g <- graph.data.frame(x[,list(source, target, weight, type, survey)])
### Graph level statistics ################################################### size
# edges
# density
# components
# apl
# modularity
### filter the graph
graph.stats <- function(edge.type, edge.survey) {
g1 <- subgraph.edges(g, which(E(g)$type == edge.type & E(g)$survey == edge.survey), delete.vertices = T)
g1 <- simplify(as.undirected(g1))
m <- edge.betweenness.community(g1)
bet <- betweenness(g1)
data.table(type = edge.type, survey = edge.survey,
size = vcount(g1),
edge.count = length(E(g1)),
density = graph.density(g1),
components = clusters(g1)$no,
avg.path.length = average.path.length(g1),
modularity = modularity(m),
centralization = centralization.degree(g1)$centralization,
assortativity = assortativity.degree(g1),
bet.m = mean(bet, na.rm=T)
bet.sd = sd(bet, na.rm=T))
}
survey.u <- x[,.N, by=list(survey,type)][,N := NULL]
graph.stats("know", "fu13")
all.stats <- data.table(type = character(), survey = character(),
size = numeric(), edge.count = numeric(),
density = numeric(), components = numeric(),
avg.path.length = numeric(), modularity = numeric(),
centralization = numeric(),
assortativity = numeric())
for(i in 1:6) {
cat("Running stats for ", survey.u$type[i], " and ", survey.u$survey[i], "\n", sep="")
all.stats <- rbind(all.stats,
graph.stats(edge.type = survey.u$type[i],
edge.survey = survey.u$survey[i]))
}
#cat("Running stats for zipcode ", i, " of ", length(zip.u), "\n", sep="")
xx <- do.call('rbind', mclapply(1:nrow(survey.u),
function(i) {
graph.stats(edge.type = survey.u$type[i], edge.survey = survey.u$survey[i])
}, mc.cores = 6))
gs <- list()
for(i in 1:nrow(survey.u)) {
gs[[i]] <- subgraph.edges(g, which(E(g)$type == survey.u$type[i] & E(g)$survey == survey.u$survey[i]), delete.vertices = T)
}
lapply(gs, "graph.density")
# izip = indexed zip
izip <- 40517
g <- induced.subgraph(a, vids = which(V(a)$zip3 == izip))
### Node level stats ##########################################################
# centralities
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment