Author: Valeria Policastro
Date: 2025-01-22

# robin

### ROBustness In Network

#### Valeria Policastro

#### 2025-01-22

# robin

In network analysis, many community detection algorithms have been developed. However,their applications leave unaddressed one important question: the statistical validation of the results.

robin (ROBustness in Network) has a double aim: tests the robustness of a community detection algorithm to detect if the community structure found is statistically significant and compares two detection algorithms to choose the one that better fits the network of interest.

Reference in Policastro V., Righelli D., Carissimo A., Cutillo L., De Feis I. (2021) https://journal.r-project.org/archive/2021/RJ-2021-040/index.html.

It provides:
1) a procedure to examine the robustness of a community detection algorithm against a random graph;
2) a procedure to choose among different community detection algorithms the one that better fits the network of interest;
3) two tests to determine the statistical difference between the curves;
4) a graphical interactive representation.

# Installation

    #install.packages("robin")

If there are problems with the installation try:

    # if (!requireNamespace("BiocManager", quietly = TRUE))
    #     install.packages("BiocManager")
    # BiocManager::install("gprege")
    #
    # install.packages("robin")

# Loading package

    library("robin")

# Input

prepGraph function creates an igraph object from the input file. This step is necessary for robin execution

The unweighted graph can be read from different format: edgelist, pajek, graphml, gml, ncol, lgl, dimacs, graphdb and igraph graphs.

    my_network <- system.file("example/football.gml", package="robin")
    # downloaded from: http://www-personal.umich.edu/~mejn/netdata/
    graph <- prepGraph(file=my_network, file.format="gml")

    ## Warning in read.graph.gml(file, ...): At vendor/cigraph/src/io/gml.c:148 :
    ## Unterminated entity or stray & character found, will be returned verbatim.

    graph

    ## IGRAPH 7b11184 U--- 115 613 --
    ## + attr: id (v/n), label (v/c), value (v/n)
    ## + edges from 7b11184:
    ##  [1] 1--  2 1--  5 1-- 10 1-- 17 1-- 24 1-- 34 1-- 36 1-- 42 1-- 66 1-- 91
    ## [11] 1-- 94 1--105 2-- 26 2-- 28 2-- 34 2-- 38 2-- 46 2-- 58 2-- 90 2--102
    ## [21] 2--104 2--106 2--110 3--  4 3--  7 3-- 14 3-- 15 3-- 16 3-- 48 3-- 61
    ## [31] 3-- 65 3-- 73 3-- 75 3--101 3--107 4--  6 4-- 12 4-- 27 4-- 41 4-- 53
    ## [41] 4-- 59 4-- 73 4-- 75 4-- 82 4-- 85 4--103 5--  6 5-- 10 5-- 17 5-- 24
    ## [51] 5-- 29 5-- 42 5-- 70 5-- 94 5--105 5--109 6-- 11 6-- 12 6-- 53 6-- 75
    ## [61] 6-- 82 6-- 85 6-- 91 6-- 98 6-- 99 6--108 7--  8 7-- 33 7-- 40 7-- 48
    ## [71] 7-- 56 7-- 59 7-- 61 7-- 65 7-- 86 7--101 7--107 8--  9 8-- 22 8-- 23
    ## + ... omitted several edges

# Network visualization

plotGraph function offers a graphical representation of the network with the aid of networkD3 package.

    plotGraph(graph)

## Community detection

methodCommunity function detects communities using all the algorithms implemented in igraph package: “walktrap”, “edgeBetweenness”, “fastGreedy”, “spinglass”, “leadingEigen”, “labelProp”, “infomap”, “optimal”, “other”.

    methodCommunity(graph=graph, method="fastGreedy")

    ## IGRAPH clustering fast greedy, groups: 6, mod: 0.55
    ## + groups:
    ##   $`1`
    ##    [1]   7  14  16  33  40  48  61  65 101 107
    ##
    ##   $`2`
    ##    [1]   8   9  10  17  22  23  24  42  47  50  52  54  68  69  74  78  79  89
    ##   [19] 105 109 111 112 115
    ##
    ##   $`3`
    ##    [1]   1   2  20  26  30  31  34  36  38  46  56  80  81  83  90  94  95 102
    ##   [19] 104 106 110
    ##   + ... omitted several groups/vertices

membershipCommunities function detects the community membership.

    membershipCommunities(graph=graph, method="fastGreedy")

    ##   [1] 3 3 5 5 5 5 1 2 2 2 5 5 4 1 4 1 2 6 4 3 6 2 2 2 5 3 4 6 5 3 3 4 1 3 4 3 6
    ##  [38] 3 4 1 5 2 6 4 6 3 2 1 6 2 5 2 5 2 4 3 6 6 6 6 1 4 6 6 1 6 6 2 2 5 6 4 5 2
    ##  [75] 5 6 6 2 2 3 3 5 3 5 5 4 6 6 2 3 5 6 6 3 3 6 6 6 5 4 1 3 5 3 2 3 1 5 2 3 2
    ## [112] 2 6 6 2

## Community visualization

plotComm function produces an interactive 3D plot of the communites detected by the chosen algorithm.

    members <- membershipCommunities(graph=graph, method="fastGreedy")
    plotComm(graph=graph, members=members)

# Robustness of a community detection algorithm

## Null model

robin offers two choices for the null model:

1. it can be generated by using the function random

2. it can be built externally and passed directly to the argument graphRandom of the robinRobust function.

The function random creates a random graph with the same degree distribution of the original graph, but with completely random edges. The graph argument must be the same returned by prepGraph function.

    graphRandom <- random(graph=graph)
    graphRandom

    ## IGRAPH 75cb303 U--- 115 613 --
    ## + attr: id (v/n), label (v/c), value (v/n)
    ## + edges from 75cb303:
    ##  [1]  1--103  1--112  1-- 10  1-- 17  1-- 24  1-- 87  1-- 36 63--107 66-- 89
    ## [10] 34-- 56 16-- 94  1--  8  2-- 71  9-- 76  2-- 17  2-- 89 40--112  2-- 58
    ## [19]  2--103  2-- 48 23-- 42  2-- 96  2--110  3-- 98  3-- 88  3-- 56  3--111
    ## [28]  3-- 16  3-- 96 61-- 98 22--103  3-- 73 56-- 66  3--101  3-- 57  6-- 76
    ## [37] 12-- 42  4-- 27  4-- 41  4-- 48  4-- 59  4-- 73  4-- 53  4-- 82  4-- 85
    ## [46]  4--102  8-- 74  5-- 31  5-- 17  5--114  2-- 29  5-- 42  5-- 58  3-- 94
    ## [55]  5--105  7-- 75 31-- 61  6-- 12  6--  9 61-- 71 54--114  6-- 51  6-- 25
    ## [64]  6--104  6-- 95 78--108  7-- 45  7-- 88  7-- 40  7-- 48  7-- 56 59-- 63
    ## + ... omitted several edges

## robinRobust

robinRobust function implements the validation of the community robustness. In this example we used “vi” distance as stability measure, “independent” type procedure and “louvain” as community detection algorithm.

Users can choose also different measures as: “nmi”,“split.join”, “adjusted.rand”.

The graph argument must be the one returned by prepGraph function. The graphRandom must be the one returned by random function or own random graph.

    proc <- robinRobust(graph=graph, graphRandom=graphRandom, method="louvain")

    ## [1] "Unweighted Network Parallel Function"
    ## Detected robin method type independent
    ## It can take time ... It depends on the size of the network.

As output robinRobust will give all the measures at different level of perturbation from 0% to 60% for the real and random graph.

plotRobin function plots the curves. The (x,y)-axes represents the percentage of perturbation and the average of the stability measure, respectively. The arguments of model1 and model2 must be the measures for the real graph and the random graph that are the outputs of the robinRobust function.

We will expect that with a robust algorithm the behavior of the two curves is different. We expect that the curve of the real graph vary less than the curve of the random graph, this visually means that the curve of the real graph is lower than the one of the random graph, so it is more stable than a random graph.

    plot(proc)

The procedure implemented depends on the network of interest. In this example, the Louvain algorithm fits good the network of interest,as the curve of the stability measure assumes lower values than the one obtained by the null model.

## Statistical tests

The differences between the stability measure curves are tested using:

1. Functional Data Analysis (FDA);

2. Gaussian Process (GP).

Moreover to quantify the differences between the curves when they are very close the Area Under the Curves (AUC) are evaluated.

robinFDATest function implements a test giving a p-value for different intervals of the curves. It tests in which interval the two curves are different.

    robinFDATest(proc)

    ## [1] "First step: basis expansion"
    ## Swapping 'y' and 'argvals', because 'y' is  simpler,
    ##   and 'argvals' should be;  now  dim(argvals) =  13 ;  dim(y) =  13 x 20
    ## [1] "Second step: joint univariate tests"
    ## [1] "Third step: interval-wise combination and correction"
    ## [1] "creating the p-value matrix: end of row 2 out of 9"
    ## [1] "creating the p-value matrix: end of row 3 out of 9"
    ## [1] "creating the p-value matrix: end of row 4 out of 9"
    ## [1] "creating the p-value matrix: end of row 5 out of 9"
    ## [1] "creating the p-value matrix: end of row 6 out of 9"
    ## [1] "creating the p-value matrix: end of row 7 out of 9"
    ## [1] "creating the p-value matrix: end of row 8 out of 9"
    ## [1] "creating the p-value matrix: end of row 9 out of 9"
    ## [1] "Interval Testing Procedure completed"

    ## TableGrob (1 x 2) "arrange": 2 grobs
    ##   z     cells    name           grob
    ## 1 1 (1-1,1-1) arrange gtable[layout]
    ## 2 2 (1-1,2-2) arrange gtable[layout]

    ## $adj.pvalue
    ## [1] 0.8575 0.0109 0.0019 0.0016 0.0016 0.0017 0.0160 0.0416 0.0149
    ##
    ## $pvalues
    ## [1] 0.8575 0.0016 0.0016 0.0016 0.0016 0.0016 0.0160 0.0416 0.0016

The first figure represents the stability measure plot using Louvain algorithm for detecting communities. The second one represents the corresponding p-values and adjusted p-values of the Interval Testing procedure. Horizontal red line corresponds to the critical value 0.05.

robinGPTest function implements the GP testing.

    robinGPTest(proc)

    ##  Profile  1
    ##  Profile  2

    ## [1] 70.62017

It tests the two curves globally. The null hypothesis claims that the two curves come from the same process, the alternative hypothesis that they come from two different processes. The output is the Bayes Factor. One of the most common interpretations is the one proposed by Harold Jeffereys (1961) and slightly modified by Lee and Wagenmakers in 2013:
IF B10 IS… THEN YOU HAVE…
° > 100 Extreme evidence for H1
° 30 – 100 Very strong evidence for H1
° 10 – 30 Strong evidence for H1
° 3 – 10 Moderate evidence for H1
° 1 – 3 Anecdotal evidence for H0
° 1 No evidence
° 1/3 – 1 Anecdotal evidence for H0
° 1/3 – 1/10 Moderate evidence for H0
° 1/10 – 1/30 Strong evidence for H0
° 1/30 – 1/100 Very strong evidence for H0
° < 1/100 Extreme evidence for H0

robinAUC function implements the AUC.

    robinAUC(proc)

    ##  Area real data Area null model
    ##       0.1144035       0.2630685

The outputs are the area under the two curves.

# Comparison of two community detection algorithms

In this example we want to compare the “Fast Greedy” and the “Louvain” algorithms to see which is the best algorithm.

We firstly plot the communities detected by both algorithms.

    membersFast <- membershipCommunities(graph=graph, method="fastGreedy")
    membersLouv <- membershipCommunities(graph=graph, method="louvain")
    plotComm(graph=graph, members=membersFast)

    plotComm(graph=graph, members=membersLouv)

Secondly, we compare them with robinCompare function.

robinCompare function compares two detection algorithms on the same network to choose the one that better fits the network of interest.

    comp <- robinCompare(graph=graph, method1="fastGreedy", method2="louvain")

    ## [1] "Unweighted Network Parallel Function"
    ## Detected robin method type independent
    ## It can take time ... It depends on the size of the network.

Thirdly, we plot the curves of the compared methods.

    plot(comp)

In this example, the Louvain algorithm fits better the network of interest, as the curve of the stability measure assumes lower values than the one obtained by the Fast greedy method.

Fourthly we test the statistical differences between these two curves that now are created on two different community detection algorithm. The tests are already explained with more detail above.

    robinFDATest(comp)

    ## [1] "First step: basis expansion"
    ## Swapping 'y' and 'argvals', because 'y' is  simpler,
    ##   and 'argvals' should be;  now  dim(argvals) =  13 ;  dim(y) =  13 x 20
    ## [1] "Second step: joint univariate tests"
    ## [1] "Third step: interval-wise combination and correction"
    ## [1] "creating the p-value matrix: end of row 2 out of 9"
    ## [1] "creating the p-value matrix: end of row 3 out of 9"
    ## [1] "creating the p-value matrix: end of row 4 out of 9"
    ## [1] "creating the p-value matrix: end of row 5 out of 9"
    ## [1] "creating the p-value matrix: end of row 6 out of 9"
    ## [1] "creating the p-value matrix: end of row 7 out of 9"
    ## [1] "creating the p-value matrix: end of row 8 out of 9"
    ## [1] "creating the p-value matrix: end of row 9 out of 9"
    ## [1] "Interval Testing Procedure completed"

    ## TableGrob (1 x 2) "arrange": 2 grobs
    ##   z     cells    name           grob
    ## 1 1 (1-1,1-1) arrange gtable[layout]
    ## 2 2 (1-1,2-2) arrange gtable[layout]

    ## $adj.pvalue
    ## [1] 0.8425 0.0749 0.0012 0.0011 0.0045 0.0381 0.4393 0.8425 0.8425
    ##
    ## $pvalues
    ## [1] 0.6604 0.0064 0.0011 0.0011 0.0045 0.0059 0.0737 0.5858 0.6026

    robinGPTest(comp)

    ##  Profile  1
    ##  Profile  2

    ## [1] 36.52914

    robinAUC(comp)

    ## Area fastGreedy    Area louvain
    ##       0.1622694       0.1120990