---
title: "robin"
subtitle: "ROBustness In Network"
author: "Valeria Policastro"
date: "`r Sys.Date()`"
vignette: >
  %\VignetteIndexEntry{robin}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
output:
  knitr:::html_vignette:
    toc: yes
    toc_depth: 2
---


# 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:<br>
**1)** a procedure to examine the robustness of a community detection algorithm against a random graph; <br>
**2)** a procedure to choose among different community detection algorithms the one that better fits the network of interest;<br>
**3)** two tests to determine the statistical difference between the curves;<br>
**4)** a graphical interactive representation.

# Installation
```{r}
#install.packages("robin")
```

If there are problems with the installation try:
```{r}
# if (!requireNamespace("BiocManager", quietly = TRUE))
#     install.packages("BiocManager")
# BiocManager::install("gprege")
# 
# install.packages("robin")
```

# Loading package
```{r message=FALSE, warning=FALSE, paged.print=TRUE}
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.

```{r}
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")
graph
```


# Network visualization
**plotGraph** function offers a graphical representation of the network with the aid of *networkD3* package.

```{r}
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".

```{r}
methodCommunity(graph=graph, method="fastGreedy") 
```
**membershipCommunities** function detects the community membership.
```{r}
membershipCommunities(graph=graph, method="fastGreedy") 
```

## Community visualization

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

```{r}
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. 

```{r}
graphRandom <- random(graph=graph)
graphRandom
```

## 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.

```{r}
proc <- robinRobust(graph=graph, graphRandom=graphRandom, method="louvain")
```
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.*  

```{r}
plot(proc)
```
<br>
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.

```{r message=FALSE, warning=FALSE}
robinFDATest(proc)
```

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.
```{r message=FALSE, warning=FALSE}
robinGPTest(proc)
```
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: <br>
**IF B10 IS…	THEN YOU HAVE…<br>**
° > 100	Extreme evidence for H1 <br>
° 30 – 100	Very strong evidence for H1 <br>
° 10 – 30	Strong evidence for H1<br>
° 3 – 10	Moderate evidence for H1<br>
° 1 – 3	Anecdotal evidence for H0<br>
° 1	No evidence<br>
° 1/3 – 1	Anecdotal evidence for H0<br>
° 1/3 – 1/10	Moderate evidence for H0<br>
° 1/10 – 1/30	Strong evidence for H0<br>
° 1/30 – 1/100	Very strong evidence for H0<br>
° < 1/100	Extreme evidence for H0<br>


**robinAUC** function implements the AUC.

```{r}
robinAUC(proc)
```
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.

```{r}
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.

```{r}
comp <- robinCompare(graph=graph, method1="fastGreedy", method2="louvain")
```


Thirdly, we plot the curves of the compared methods.

```{r}
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.
```{r message=FALSE, warning=FALSE}
robinFDATest(comp)

```

```{r message=FALSE, warning=FALSE}
robinGPTest(comp)
```

 
```{r}
robinAUC(comp)
```

