Search code examples
rigraphbipartite

igraph package in RStudio: Bipartite graph projection error


In an attempt to learn the "nuts-and-bolts" of social network theory within the igraph package in R, I created a basic toy example of a bipartite graph of terror attacks during a single year of the Algerian Civil War. The vertices consist of terror perpetrators and targets, while the edges represent which group attacked which target.

I can plot the general unipartite graph of this relationship (as well as basic analyses of network centrality), but am having problems creating a bipartite projection of the network.

Per @GaborCsardi's suggestion, I've only loaded the igraph package into the global environment, as to ensure that the sna or networks packages do not conflict with the commands for igraph.

Nonetheless, the problem persists:

 library(igraph)

 perpetrator <- c("Algerian Islamic Extremists", 
             "Salafist Group for Preaching and Fighting (GSPC)", 
             "Salafist Group for Preaching and Fighting (GSPC)", 
             "Algerian Islamic Extremists", 
             "Salafist Group for Preaching and Fighting (GSPC)", 
             "Muslim Extremists",
             "Armed Islamic Group (GIA)",
             "Armed Islamic Group (GIA)",
             "Armed Islamic Group (GIA)",
             "Muslim Militants")

 target <- c("Police", "Military", "Terrorists/Non-state Militia", "Police",
             "Military", "Private Citizens & Property", 
             "Private Citizens & Property", "Private Citizens & Property", 
             "Private Citizens & Property", "Private Citizens & Property")

 dat <- cbind(perpetrator, target)

 net <- graph.edgelist(as.matrix(dat)) 

 plot(net, main="Domestic Terrorism during the Algerian Civil War")

 V(net)$type <- FALSE

 V(net)$type[V(net)$name%in%dat$perpetrator] <- TRUE

 V(net)$type[V(net)$name%in%dat$target] <- TRUE

 bipartite.mapping(net)

 proj_net <- bipartite.projection(net, type=V(net)$type)

At which point, RStudio produces the following error:

 Error in .Call("R_igraph_bipartite_projection", graph, types, as.integer(probe1),  :  
 At bipartite.c:198 : Non-bipartite edge found in bipartite projection, Invalid value

Solution

  • According to the documentation, bipartite.mapping(...)

    decides whether the vertices of a network can be mapped to two vertex types in a way that no vertices of the same type are connected.

    If it is possible to do this, then the $type element in the list returned by bipartite.mapping(...) identifies which sub-network each vertex belongs to (via TRUE or FALSE). Note that for your graph, there is more than one way to do this.

    You seem to be (trying to) define your sub-networks yourself. Although in general, the sub-networks are not necessarily bipartite when you do this, in your case they are. So you can just use bipartite.projection(...) to divide net into the sub-networks, as follows:

    V(net)$type <- FALSE
    V(net)$type[V(net)$name%in%dat$perpetrator] <- TRUE
    proj_net <- bipartite.projection(net)
    

    proj_net is now a list with two elements, the subgraphs.

    If you want to use bipartite.mapping(...) to identify the subnetworks, do it this way:

    V(net)$type <- bipartite.mapping(net)$type
    proj_net <- bipartite.projection(net)
    set.seed(123)  # for reproducible plot
    plot(net,vertex.color=ifelse(V(net)$type,"green","red"))
    

    This does not group all the targets together, nor the perpetrators, but the red and green subnetworks are bipartite.