leiden (version 0.2.3)

leiden: Run Leiden clustering algorithm

Description

Implements the Leiden clustering algorithm in R using reticulate to run the Python version. Requires the python "leidenalg" and "igraph" modules to be installed. Returns a vector of partition indices.

Usage

leiden(adj_mat, partition_type = c("RBConfigurationVertexPartition",
  "ModularityVertexPartition", "RBERVertexPartition", "CPMVertexPartition",
  "MutableVertexPartition", "SignificanceVertexPartition",
  "SurpriseVertexPartition"), initial_membership = NULL,
  weights = NULL, node_sizes = NULL, resolution_parameter = 1)

Arguments

adj_mat

An adjacency matrix compatible with igraph object.

partition_type

Type of partition to use. Defaults to RBConfigurationVertexPartition. Options include: ModularityVertexPartition, RBERVertexPartition, CPMVertexPartition, MutableVertexPartition, SignificanceVertexPartition, SurpriseVertexPartition (see the Leiden python module documentation for more details)

initial_membership, weights, node_sizes

Parameters to pass to the Python leidenalg function (defaults initial_membership=None, weights=None).

resolution_parameter

A parameter controlling the coarseness of the clusters

Value

A parition of clusters as a vector of integers

Examples

Run this code
# NOT RUN {
#check if python is availble
modules <- reticulate::py_module_available("leidenalg") && reticulate::py_module_available("igraph")
if(modules){
#generate example data
adjacency_matrix <- rbind(cbind(matrix(round(rbinom(4000, 1, 0.8)), 20, 20),
                                matrix(round(rbinom(4000, 1, 0.3)), 20, 20),
                                matrix(round(rbinom(400, 1, 0.1)), 20, 20)),
                          cbind(matrix(round(rbinom(400, 1, 0.3)), 20, 20),
                                matrix(round(rbinom(400, 1, 0.8)), 20, 20),
                                matrix(round(rbinom(4000, 1, 0.2)), 20, 20)),
                          cbind(matrix(round(rbinom(400, 1, 0.3)), 20, 20),
                                matrix(round(rbinom(4000, 1, 0.1)), 20, 20),
                                matrix(round(rbinom(4000, 1, 0.9)), 20, 20)))
rownames(adjacency_matrix) <- 1:60
colnames(adjacency_matrix) <- 1:60
#generate partitions
partition <- leiden(adjacency_matrix)
table(partition)

#generate partitions at a lower resolution
partition <- leiden(adjacency_matrix, resolution_parameter = 0.5)
table(partition)

#generate example weights
weights <- sample(1:10, sum(adjacency_matrix!=0), replace=TRUE)
partition <- leiden(adjacency_matrix, weights = weights)
table(partition)

#generate example weighted matrix
adjacency_matrix[adjacency_matrix == 1] <- weights
partition <- leiden(adjacency_matrix)
table(partition)
}


# }

Run the code above in your browser using DataLab