# Extracting ground-truth clusters from sample\_sbm result

**URL:** <https://igraph.discourse.group/t/extracting-ground-truth-clusters-from-sample-sbm-result/114>\
**Category:** Usage\
**Tags:** R\
**Created:** [26 February 2020 07:40 UTC](https://igraph.discourse.group/t/extracting-ground-truth-clusters-from-sample-sbm-result/114 "2020-02-26T07:40:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![graphenthus](https://avatars.discourse-cdn.com/v4/letter/g/a88e57/32.png) [@graphenthus](https://igraph.discourse.group/u/graphenthus)\
**Post date:** [26 February 2020 07:40 UTC](https://igraph.discourse.group/t/extracting-ground-truth-clusters-from-sample-sbm-result/114/1 "2020-02-26T07:40:17Z")

</div>

Hello!

Firstly, really, really, really wonderful package; thank you so much for creating/maintaining it!

I’ve run into an issue using `igraph` on `R` that I can’t quite figure out.

I generate a graph using `sample_sbm` with 100 nodes and 2 clusters and intra-cluster edge probability greater than inter-cluster edge probability. That is, I tell `sample_sbm` to generate a graph with two clusters by passing a 2-by-2 matrix into the `pref.matrix` parameter. My resulting graph looks like this:

![](https://global.discourse-cdn.com/free1/uploads/igraph/original/1X/12c874153f1129685f60dd7e13881b9b113997e3.jpeg)

Is there some way to recover the ground-truth clusters (i.e., the clusters that `sample_sbm` presumably creates using `pref.matrix`)? It seems using `cluster_leading_eigen` worked for this particular case, but I was wondering if it’d be possible to recover the information used to generate the network in the first place. I’m having some trouble finding a solution in the documentation, etc.

If this is not possible, which of the clustering algorithms that igraph provides would be best suited (in terms of accuracy and time)? I’m working with larger networks with more clusters as well, so any way to recover this information efficiently would be fantastic!

Thank you!

---

<div class="post-metadata">

**Author:** ![vtraag](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/vtraag/32/38_2.png) [@vtraag](https://igraph.discourse.group/u/vtraag)\
**Post date:** [26 February 2020 08:21 UTC](https://igraph.discourse.group/t/extracting-ground-truth-clusters-from-sample-sbm-result/114/2 "2020-02-26T08:21:02Z")

</div>

If I understand the [documentation](https://igraph.org/r/doc/sample_sbm.html) correctly, the nodes are simply assigned to a block in consecutive order. That is, all nodes `1` to `block.sizes[1]` are in block `1` and all nodes in `block.sizes[1] + 1` to `block.sizes[2]` are in block `2`, and so on. So, in your example, nodes `1` - `50` should be in block `1` while nodes `51`-`100` are in block 2. Does that answer your question?

For extracting communities from the graph there are quite a number of different options, see all `cluster_*` methods. In my experience `cluster_infomap` and `cluster_louvain` work quite well. Hopefully soon the `cluster_leiden` method will also become available in R, which improves on the `cluster_louvain` method (disclaimer: I am the author of the Leiden algorithm).

---

<div class="post-metadata">

**Author:** ![graphenthus](https://avatars.discourse-cdn.com/v4/letter/g/a88e57/32.png) [@graphenthus](https://igraph.discourse.group/u/graphenthus)\
**Post date:** [26 February 2020 08:43 UTC](https://igraph.discourse.group/t/extracting-ground-truth-clusters-from-sample-sbm-result/114/3 "2020-02-26T08:43:26Z")

</div>

Ah, thank you!

Are you getting that from the description of the `block.sizes` argument?

Using `cluster_louvain` on another dataset, this indeed seems to be the case:

```auto
[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  [35] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2
  [69] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 [103] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 [137] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 11 11 11
 [171] 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11
 [205] 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
 [239] 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
 [273] 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5 5
 [307] 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
 [341] 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5

```

Thank you so much for your help!

---

<div class="post-metadata">

**Author:** ![vtraag](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/vtraag/32/38_2.png) [@vtraag](https://igraph.discourse.group/u/vtraag)\
**Post date:** [26 February 2020 08:56 UTC](https://igraph.discourse.group/t/extracting-ground-truth-clusters-from-sample-sbm-result/114/4 "2020-02-26T08:56:01Z")

</div>

> [@graphenthus](#):
>
> Are you getting that from the description of the `block.sizes` argument?

Well, I must admit that the documentation is a bit unclear at this point, which should be improved. I just opened an [issue](https://github.com/igraph/rigraph/issues/373) for that on GitHub.

The [C documentation](https://igraph.org/c/doc/igraph-Generators.html#igraph_sbm_game) and the [Python documentation](https://igraph.org/python/doc/igraph.GraphBase-class.html#SBM) do mention the consecutive node order.

---

<div class="post-metadata">

**Author:** ![graphenthus](https://avatars.discourse-cdn.com/v4/letter/g/a88e57/32.png) [@graphenthus](https://igraph.discourse.group/u/graphenthus)\
**Post date:** [26 February 2020 16:20 UTC](https://igraph.discourse.group/t/extracting-ground-truth-clusters-from-sample-sbm-result/114/5 "2020-02-26T16:20:27Z")

</div>

Ah, amazing; the documentation for Python and C clear things up significantly. I should have thought to look there. Thank you so much! Looking forward to the R implementation of `cluster_leiden`!
