Cohesive blocking

From InterSciWiki

Jump to: navigation, search


Copy http://intersci.ss.uci.edu/wiki/pub/mwExample1.net to your directory, then 
Cut and paste into R as a command
source("http://intersci.ss.uci.edu/wiki/pub/MW.R")
plot.bgraph(gBlocks,layout=layout.fruchterman.reingold, vertex.size = 25)

iGraph objects -- Peter McMahan is the author of this program, which implements Moody and White (2003) in R: Structural cohesion

Contents

[edit] Setup

getwd()
setwd("C:/Program Files/R//R-2.6.2/")
list.files()
library(igraph)
library(digest)
library(RSQLite)
#help(cohesive.blocks) #graph object of class igraph
example(cohesive.blocks) #creates gBlocks
plot(gBlocks) #
#plot(gBlocks, layout=layout.spring) #igraph Network spring embedding in R
plot.bgraph(gBlocks,layout=layout.kamada.kawai) #igraph 
plot.bgraph(gBlocks,layout=layout.fruchterman.reingold) #igraph
require(igraph)
require(digest)
require(RSQLite)
example(cohesive.blocks)
plot.bgraph(gBlocks,layout=layout.kamada.kawai)
write.pajek.bgraph(gBlocks,file="gBlocks")
require(igraph)
require(digest)
require(RSQLite)
g <- read.graph(file="mwExample1.net", format="pajek")
gBlocks <- cohesive.blocks(g)                           
plot.bgraph(gBlocks,layout=layout.kamada.kawai)
write.pajek.bgraph(gBlocks,file="gBlocks")
source("C:\\Program Files\\R\\R-2.6.2\\MW.R")
source("http://intersci.ss.uci.edu/wiki/pub/MW.R")

[edit] From Pajek to Cohesive Blocks

g <- read.graph(file="davis.net", format="pajek")
g <- read.graph(file="Garfa.net", format="pajek")
g <- read.graph(file="livorno.net", format="pajek")
g <- read.graph(file="TARO.NET", format="pajek")
g <- read.graph(file="methodscamp.net", format="pajek")
gBlocks <- cohesive.blocks(g)                           #igraph  help(cohesive.blocks)
plot.bgraph(gBlocks,layout=layout.fruchterman.reingold) #igraph  help(plot.bgraph)  Excellent layout
plot.bgraph(gBlocks,layout=layout.kamada.kawai)         #igraph  Not as good
plot(gBlocks, layout=layout.spring)                     #not well separated -- shown as a clump
plot(g, layout=layout.spring) #just the original graph  
Network spring embedding in R

[edit] Mark Handcock R programs to get from Netdata (Network package) to Cohesive Blocks

Netdata60
#The problem is to get cohesive.blocks working inside the network package, reading netdata for example.
#library(igraph, warn.conflicts = FALSE, quietly=TRUE)
#
#library(digest)
#library(RSQLite)
#
#library(network)
#library(netdata)
#library(RBGL)
#tte packages need to be installed but will be activated by cutting and pasting these two lines
source("http://intersci.ss.uci.edu/wiki/pub/CohesiveBlocks.network.R")
source("http://intersci.ss.uci.edu/wiki/pub/CohesiveBlocksExamples.R")
mwExample1.clu                          
mwExample1.net                          
mwExample2.clu                          
mwExample2.net 
#Dont use these
http://www.csde.washington.edu/~handcock/CohesiveBlocks.network.R
http://www.csde.washington.edu/~handcock/CohesiveBlocksExamples.R

MW example

The key function is network2igraph (see the code). The plotting in igraph does not work for me. --Mark Handcock

[edit] Carter Butts' proposal for data conversion

There's not yet a routine to convert network objects into igraph objects. However, one quick and dirty approach is to convert the network into a matrix, and then build an igraph object from that. Here are two ways to put a network object into adjacency matrix form:

as.sociomatrix(mynet)  #Converts to sociomatrix
mynet[,]               #This works, too.

as.sociomatrix has some extra options, and the [,] operator allows for fancy overloading, but both work similarly for simple data. You can also convert the object into an edge list if the network is extremely large -- check out the as.matrix.network help pages for more options.

Hope that helps! --Carter

[edit] Peter McMahan's proposal for data conversion

Is the edgelist an object from the "network" package in R? if so then it's probably best to convert to an adjacency matrix then to an igraph object. Starting with "network" object G

require(igraph);require(network)
g <- graph.adjacency(as.matrix.network(G))

should give you an igraph graph object g. This will not preserve vertex attributes.

If the edgelist is a text file that looks like what you sent, then it will take some complicated text parsing to do the trick.

Check out igraph's file import capabilities:

?read.graph

the ability to read a number of existing file formats is built in to that function. -- Peter

[edit] Update from Peter for Mark

DRW: Some enhanced collaboration here!) `Oh, great. It looks like the only change Mark Handcock made was to add a function "network2igraph", which does what I described but looks like it does a good job of keeping vertex attributes intact.

It also looks like he's using an older version of the cohesive.blocks() code, so that may explain the strange saving behavior. The code in the attached file should implement his conversion without modifying the original function. This way it should keep up with any improvements to the cohesive.blocks() funtion that come along with new versions of igraph (such as a significant speed imporovement upcoming in v.0.5). To use it just source the file ( source("CB.networkCompatibility.R") ) and it defines a new function cohesive.blocks.network() that takes network objects rather than igraph objects. So, with network object G:

source('CB.networkCompatibility.R') ## if not done already - ELSE source('http://intersci.ss.uci.edu/wiki/pub/CB.networkCompatibility.R)
cb <- cohesive.blocks.network(G)
plot(cb,layout=layout.kamada.kawai)
write.pajek.bgraph(H,"G",T)

The output object is an igraph object still, so write.pajek.bgraph whould work fine. -- Peter 13:33, 26 February 2008 (PST)

[edit] Vertex cohesion

g <- erdos.renyi.game(50, 5/50)
g <- as.directed(g)
g <- subgraph(g, subcomponent(g, 1))
graph.cohesion(g)
#gBlocks <- cohesive.blocks(g)                           #igraph  help(cohesive.blocks)
plot.bgraph(g,layout=layout.fruchterman.reingold) #igraph  help(plot.bgraph)  Excellent layout


g <- CEOs  #not igraph
gg=g[,1]
cohesive.blocks(gg)
  1. HighTech.paj - multirelational network with 21 vertices and 190+102+20 arcs
  2. Padgett.paj - multirelational network with 16 vertices and 30+40 arcs
  3. EIES.paj - multirelational network with 48/32 vertices and 695+830/460 arcs
  4. Trade.paj - multirelational network with 24 vertices and 307+307+310+135+369 arcs
  5. CEOs.net - two-mode network with 41 vertices and 98 edges.

[edit] Forget this stuff for now

test.3 <- read.paj("Garfa.net")   #Import/Export igraph network
plot(test.3,main=test.3$gal$title)
plot(test.3,layout=layout.fruchterman.reingold)
plot(test.3,layout=layout.kamada.kawai)
g <- read.graph("test.3", format="edgelist")
cohesive.blocks(test.3)


Netdata60

  1. read.graph("filename.net","pajek") #(igraph) read.graph(file, format = c("edgelist", "pajek", "ncol", "lgl","graphml", "dimacs", "graphdb", "gml"), ...)

R plot networks

Personal tools