```{r libraries, echo = FALSE, include = FALSE}
require("ggplot2")
require("tidyr")
require("dplyr")
require("patchwork")
require("latex2exp")
```

\section{Network clustering of simulated networks}\label{sec:network-clustering-of-simulated-networks}

```{r impoting-data, echo = FALSE}
filenames <- list.files(
    path = "./Rcodes/simulation/data",
    pattern = "simulated_collection_clustering_*",
    full.names = TRUE
)

rep(gsub(".*_(iid|pi|rho|pirho)_.*", "\\1", filenames), each = 6)

# data_list <- lapply(filenames, function(file) lapply(readRDS(file), function(model) model$list_clustering))
data_list <- lapply(filenames, readRDS)


for (model in data_list) {
            list_clustering <- lapply(
                seq_along(model), function(s) model[[s]]$list_of_clusterings
            )

            list_best_partition <- lapply(
                seq_along(list_clustering), function(s) {
                    list(
                        epsilon = result_clustering[[s]]$epsilon,
                        best_partition = unlist(extract_best_bipartite_partition(list_clustering[[s]]))
                    )
                }
            )
            data.frame(
                epsilon = sapply(
                    list_best_partition,
                    function(best_partition) {
                        best_partition$epsilon
                    }
                ),
                ARI = sapply(
                    list_best_partition,
                    function(best_partition) {
                        aricode::ARI(rep(1:3, each = 3), unlist(
                            lapply(
                                seq_along(best_partition$best_partition),
                                function(idx) {
                                    setNames(
                                        rep(idx, 
                                        best_partition$best_partition[[idx]]$M), 
                                        best_partition$best_partition[[idx]]$net_id
                                    )
                                }
                            )
                        ))
                    }
                )
            )
}

lapply(
    data_list,
    function(model) {
        list_clustering <- lapply(
        seq_along(model), function(s) model[[s]]$list_of_clusterings
        )

        list_best_partition <- lapply(
            seq_along(list_clustering), function(s) {
                list(
                    epsilon = result_clustering[[s]]$epsilon,
                    best_partition = unlist(extract_best_bipartite_partition(list_clustering[[s]]))
                )
            }
        )
    data.frame(
        epsilon = sapply(
            list_best_partition,
            function(best_partition) {
                best_partition$epsilon
            }
        ),
        ARI = sapply(
            list_best_partition,
            function(best_partition) {
                aricode::ARI(rep(1:3, each = 3), unlist(
                    lapply(
                        seq_along(best_partition$best_partition),
                        function(idx) {
                            setNames(
                                rep(idx, best_partition$best_partition[[idx]]$M), best_partition$best_partition[[idx]]$net_id
                            )
                        }
                    )
                ))
            }
        )
    )

}
)
    data.frame(
        epsilon = sapply(
            list_best_partition,
            function(best_partition) {
                best_partition$epsilon
            }
        ),
        ARI = sapply(
            list_best_partition,
            function(best_partition) {
                aricode::ARI(rep(1:3, each = 3), unlist(
                    lapply(
                        seq_along(best_partition$best_partition),
                        function(idx) {
                            setNames(
                                rep(idx, best_partition$best_partition[[idx]]$M), best_partition$best_partition[[idx]]$net_id
                            )
                        }
                    )
                ))
            }
        )
    )
```