I want to collapse a graph into its respective communities/clusters. Let me illustrate this with the following toy example:
set.seed(123)
#toy graph
g <- barabasi.game(10) %>%
as.undirected()
#identify communities
c_g <- fastgreedy.community(g)
There are three communities, as seen in the following graph.
I want to reduce the collapse the vertices so that vertices in the resulting graph correspond to the membership of the previous vertices. See the graph.
I'm new to the igraph
package and I'm not familiar with the best way of dealing with igraph
objects.
Classes related to graph clustering. Clustering Class representing a clustering of an arbitrary ordered set. CohesiveBlocks The cohesive block structure of a graph. Cover Class representing a cover of an arbitrary ordered set. Dendrogram The hierarchical clustering (dendrogram) of some dataset.
A correlation matrix can be visualized as a network diagram. Each entity of the dataset will be a node. And 2 nodes will be connected if their correlation or distance reach a threshold ( 0.995 here). To make a graph object from the correlation matrix, use the graph_from_adjacency_matrix () function of the igraph package.
This post explains the functioning of the spectral graph clustering algorithm, then it looks at a variant named self tuned graph clustering. This adaptation has the advantage of providing an estimation for the optimal number of clusters and also for the similarity measure between data points.
A second way to estimate the number of clusters is to analyze the eigenvalues ( the largest eigenvalue of L will be a repeated eigenvalue of magnitude 1 with multiplicity equal to the number of groups C. This implies one could estimate C by counting the number of eigenvalues equaling 1).
You could try contract
:
library(igraph)
set.seed(123)
g <- barabasi.game(10) %>% as.undirected()
c_g <- fastgreedy.community(g)
V(g)$name <- letters[1:vcount(g)]
g2 <- contract(g, membership(c_g), vertex.attr.comb=toString)
par(mfrow=c(1,2))
plot(g, vertex.color=membership(c_g))
plot(simplify(g2), vertex.color=1:vcount(g2))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With