R Under development (unstable) (2026-03-06 r89561 ucrt) -- "Unsuffered Consequences" Copyright (C) 2026 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > ############################################################################### > ## INCVCommunityDetection — Toy Example > ## > ## A complete walkthrough for new users demonstrating how to simulate a > ## network, select the number of communities with INCV, and compare with > ## ECV and NCV. > ## > ## Usage: > ## library(INCVCommunityDetection) > ## source("tests/toy_example.R") > ############################################################################### > > library(INCVCommunityDetection) > > # ── 1. Simulate a network with 3 communities ────────────────────── > set.seed(42) > net <- community.sim( + k = 3, # 3 communities + n = 200, # 200 nodes + n1 = 60, # smallest community has 60 nodes + p = 0.4, # within-community connection probability + q = 0.05 # between-community connection probability + ) > > cat("True community sizes:\n") True community sizes: > print(table(net$membership)) 1 2 3 60 70 70 > > # ── 2. Select K using INCV (f-fold) ─────────────────────────────── > result <- nscv.f.fold( + A = net$adjacency, + k.vec = 2:7, + f = 10, + method = "affinity" + ) > > cat("\nINCV f-fold results:\n") INCV f-fold results: > cat(" K selected by loss:", result$k.loss, "\n") K selected by loss: 3 > cat(" K selected by MSE: ", result$k.mse, "\n") K selected by MSE: 3 > > # Plot the CV loss curve > plot(2:7, result$cv.loss, type = "b", pch = 19, col = "steelblue", + xlab = "Number of communities (K)", + ylab = "CV Negative Log-Likelihood", + main = "INCV: Selecting the number of communities") > abline(v = result$k.loss, lty = 2, col = "red") > legend("topright", legend = paste("Selected K =", result$k.loss), + col = "red", lty = 2) > > # ── 3. Select K using INCV (random split) ───────────────────────── > result2 <- nscv.random.split( + A = net$adjacency, + k.vec = 2:7, + split = 0.66, + ite = 30 + ) > > cat("\nINCV random-split results:\n") INCV random-split results: > cat(" K selected:", result2$k.chosen, "\n") K selected: 3 > > # ── 4. Compare with ECV ─────────────────────────────────────────── > ecv <- ECV.for.blockmodel(net$adjacency, max.K = 7, B = 3) > cat("\nECV results:\n") ECV results: > cat(" Model (deviance):", ecv$dev.model, "\n") Model (deviance): SBM-3 > cat(" Model (L2): ", ecv$l2.model, "\n") Model (L2): SBM-3 > > # ── 5. Compare with NCV ─────────────────────────────────────────── > ncv <- NCV.for.blockmodel(net$adjacency, max.K = 7, cv = 3) > cat("\nNCV results:\n") NCV results: > cat(" Model (deviance):", ncv$dev.model, "\n") Model (deviance): SBM-3 > cat(" Model (L2): ", ncv$l2.model, "\n") Model (L2): SBM-3 > > # ── 6. Inspect the spectral clustering at the chosen K ──────────── > cl <- SBM.spectral.clustering(net$adjacency, k = result$k.loss) > prob <- SBM.prob(cl$cluster, k = result$k.loss, + A = net$adjacency, restricted = TRUE) > > cat("\nEstimated block probabilities:\n") Estimated block probabilities: > cat(" Within-community p: ", round(prob$p.matrix[1, 1], 3), "\n") Within-community p: 0.397 > cat(" Between-community q:", round(prob$p.matrix[1, 2], 3), "\n") Between-community q: 0.049 > > # ── 7. Summary ──────────────────────────────────────────────────── > cat("\n══ Summary ══════════════════════════════════════\n") ══ Summary ══════════════════════════════════════ > cat(" True K = 3\n") True K = 3 > cat(" INCV f-fold selected K =", result$k.loss, "\n") INCV f-fold selected K = 3 > cat(" INCV random selected K =", result2$k.chosen, "\n") INCV random selected K = 3 > cat(" ECV selected:", ecv$dev.model, "\n") ECV selected: SBM-3 > cat(" NCV selected:", ncv$dev.model, "\n") NCV selected: SBM-3 > > proc.time() user system elapsed 10.46 1.14 11.60