How to create a bipartite network in R with igraph or tnet
I have an edgelist for a two mode network, similar to this:
person Event
Amy football_game
Sam picnic
Bob art_show
I want to perform an analysis on this in R, but seemingly everything I try fails. Converting it to a one mode network runs into memory limitations, and I can't figure out how to analyze it as bipartite in either igraph or tnet.
In igraph, bipartite.projection
gives me all FALSE
, on the igraph object created using
net <- graph.edgelist(myobject)
On tnet, I can't convert the igraph net to a tnet one, and when I try to use the original data frame, it refuses because of duplicates in the graph.
So answers to any of the following would be super appreciated:
- How do I use the
bipartite.mapping
function? - How do I input an igraph object into tnet?
- If all else fails, how I do I input a data frame with duplicate edges into tnet?
Sorry if these are basic questions, but there's very little documentation.
EDIT
Example:
edgelist <- read.table(text="Person Event
Amy football
Bob picnic
Sam artshow",
header=TRUE)
edgelist <- as.matrix(edgelist)
## Igraph Issues
igraph <- graph.edgelist(edgelist)
typevector <- bipartite.projection(igraph)
# gets all FALSE
edgelist2 <- get.edgelist(igraph)
typevector <- bipartite.projection(edgelist2)
# same thing
## tnet issues
tnet <- as.tnet(edgelist)
# gives error: "There are duplicate events in the edgelist"
tnet <- as.tnet(edgelist2)
clusterMat <- clustering_local_tm(tnet)
# gives error: "max not meaningful for factors"
onemode <- projecting_tm(tnet, method="Newman")
# gives error: "arguments must have same length"
Solution 1:
In igraph a bipartite network is one that has a type
vertex attribute. This attribute must be logical and must the TRUE
for one of the node types and FALSE
for the others. So to create a bipartite network from your edge list, you simply create a regular graph and then add the type
vertex attribute:
edgelist <- read.table(text="Person Event
Amy football
Bob picnic
Sam artshow",
header=TRUE)
igraph <- graph.data.frame(edgelist)
V(igraph)$type <- V(igraph)$name %in% edgelist[,1]
igraph
# IGRAPH DN-B 6 3 --
# + attr: name (v/c), type (v/x)
The 'B' letter tells you that this is a bipartite graph. You can create the unipartite projections of this network via:
bipartite.projection(igraph)
# $proj1
# IGRAPH UN-B 3 0 --
# + attr: name (v/c), type (v/x)
#
# $proj2
# IGRAPH UN-B 3 0 --
# + attr: name (v/c), type (v/x)
This will return a list of two graphs. If you think that the projection might be too big, you can first call the bipartite.projection.size
function, this will give you the number of vertices and edges in both projections. The memory requirement for an igraph graph is (4m+2n)*8+O(1) bytes, where 'n' is the number of vertices and 'm' is the number of edges.