optimize_leiden_resolution: Optimize Leiden Resolution

View source: R/optimize_leiden_resolution.R

optimize_leiden_resolutionR Documentation

Optimize Leiden Resolution

Description

Optimizes the resolution parameter for Leiden community detection.

Usage

optimize_leiden_resolution(
  data,
  corr = "cor_auto",
  gamma_values = c(0.01, 0.05, 0.1, 0.5, 1),
  objective_function = "CPM",
  verbose = TRUE
)

Arguments

data

Data frame with the data.

corr

Correlation method (default: "cor_auto").

gamma_values

Numeric vector of gamma resolution values to test.

objective_function

Leiden objective function (default: "CPM").

verbose

Logical; print progress messages.

Value

A list with best_gamma (the resolution with the lowest TEFI), max_valid_gamma (the largest resolution tested before TEFI became undefined), best_model (the EGA object for best_gamma), all_results (the EGA objects of the valid resolutions), comparison (a data frame with the TEFI and number of communities per resolution) and optimization_plot (a ggplot of TEFI against gamma).

Examples

set.seed(123)
n <- 300
f1 <- rnorm(n)
f2 <- 0.3 * f1 + rnorm(n)
items <- data.frame(
  sapply(1:4, function(i) 0.7 * f1 + rnorm(n, 0, 0.7)),
  sapply(1:4, function(i) 0.7 * f2 + rnorm(n, 0, 0.7))
)
names(items) <- c(paste0("A", 1:4), paste0("B", 1:4))

opt <- optimize_leiden_resolution(items, corr = "pearson",
                                  gamma_values = c(0.05, 0.1, 0.5),
                                  verbose = FALSE)
opt$comparison

InterconectaR documentation built on Oct. 16, 2026, 1:06 a.m.