Advertisement
BenjaminLind

Reading SNA data for #snaspb2013

Aug 19th, 2013
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.24 KB | None | 0 0
  1. library(RCurl)
  2. library(igraph)
  3. library(compiler)
  4.  
  5. su<-function(x) return(sort(unique(x)))
  6. su<-cmpfun(su)
  7.  
  8. read.sstab<-function(theurl, ...){ #Function to read in a data table from online
  9.   #_theurl_ refers to the location of the data
  10.   #_..._ are parameters passed onto read.table
  11.   require(RCurl)
  12.   outtab<-getURL(theurl, ssl.verifypeer=FALSE)
  13.   outtab<-textConnection(outtab)
  14.   outtab<-read.table(outtab, sep="\t", ...)
  15.   return(outtab)
  16.   }
  17.  
  18. #Import the data
  19. wikidat<-read.sstab("http://pastebin.com/raw.php?i=UVmTznBj", header=TRUE, skip=6)
  20. #Convert the data to igraph
  21. wikidat<-graph.data.frame(wikidat, directed=TRUE)
  22.  
  23. V(wikidat)$comp.w<-clusters(wikidat, mode="weak")$membership
  24. V(wikidat)$comp.s<-clusters(wikidat, mode="strong")$membership
  25.  
  26. #I'll need to recalculate these scores
  27. wikidat.eb<-read.sstab("http://pastebin.com/raw.php?i=w4fTuUje", header=TRUE) #Read in edge betweenness scores
  28. wikidat<-igraph::set.edge.attribute(wikidat, "edge.betw", value=wikidat.eb$Edge.Betw) #Set edge betweenness as an edge attribute
  29. wikidat<-igraph::set.edge.attribute(wikidat, "max.minus.edge.betw", value=wikidat.eb$max.minus.Edge.Betw) #Set max(edge betweenness) - edge betweenness as an attribute
  30. rm(wikidat.eb)
  31.  
  32. download.file("http://pastebin.com/raw.php?i=a7sF1V75", "snaspb2013.net") #Download data
  33. snaspb2013<-read.graph("snaspb2013.net", "pajek") #Create igraph object
  34. file.remove("snaspb2013.net") #Remove downloaded item
  35.  
  36. V(snaspb2013)$indegree<-degree(snaspb2013, mode="in")
  37. V(snaspb2013)$outdegree<-degree(snaspb2013, mode="out")
  38. V(snaspb2013)$totaldegree<-degree(snaspb2013, mode="total")
  39. V(snaspb2013)$loc.trans<-transitivity(snaspb2013, "local")
  40.  
  41. V(snaspb2013)$betw<-betweenness(snaspb2013)
  42. E(snaspb2013)$eb<-edge.betweenness(snaspb2013)
  43. V(snaspb2013)$comp.w<-clusters(snaspb2013, mode="weak")$membership
  44. V(snaspb2013)$comp.s<-clusters(snaspb2013, mode="strong")$membership
  45. V(snaspb2013)$closeness<-closeness(snaspb2013)
  46. V(snaspb2013)$evcent<-evcent(snaspb2013)$vector
  47. V(snaspb2013)$kc.undir<-graph.coreness(as.undirected(snaspb2013, mode="collapse"))
  48.  
  49. snaspb2013.sg.members<-which(V(snaspb2013)$comp.w == which.max(clusters(snaspb2013, mode="weak")$csize))
  50. snaspb2013.sg<-induced.subgraph(snaspb2013, snaspb2013.sg.members)
  51. snaspb2013.comms<-multilevel.community(as.undirected(snaspb2013.sg, mode="collapse"))
  52. snaspb2013.comms$modularity
  53. V(snaspb2013.sg)$comms<-snaspb2013.comms$membership
  54. names(snaspb2013.comms$membership)<-V(snaspb2013.sg)$id
  55. sort(snaspb2013.comms$membership)
  56. snaspb2013.comms.w<-multilevel.community(as.undirected(snaspb2013.sg, mode="collapse"), weights=max(E(snaspb2013.sg)$eb)-E(snaspb2013.sg)$eb)
  57. snaspb2013.comms.w$modularity
  58. V(snaspb2013.sg)$comms.w<-snaspb2013.comms.w$membership
  59. names(snaspb2013.comms.w$membership)<-V(snaspb2013.sg)$id
  60. sort(snaspb2013.comms.w$membership)
  61.  
  62. snaspb.layout<-layout.fruchterman.reingold(snaspb2013, params=list(niter=5000, area=vcount(snaspb2013)^3))
  63. V(snaspb2013)$x<-snaspb.layout[,1]
  64. V(snaspb2013)$y<-snaspb.layout[,2]
  65. rm(snaspb.layout)
  66.  
  67. snaspb2013.sg.layout<-layout.fruchterman.reingold(snaspb2013.sg, params=list(niter=5000, area=vcount(snaspb2013.sg)^3))
  68. V(snaspb2013.sg)$x<-snaspb2013.sg.layout[,1]
  69. V(snaspb2013.sg)$y<-snaspb2013.sg.layout[,2]
  70. rm(snaspb2013.sg.layout)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement