tests/testthat/test-cluster.R

if (require("igraph")) {
  seed <- 1234

  ## Generate simple highly structured graph SE2 should be able to easily
  ## cluster.
  set.seed(5678)
  n_nodes <- 40
  n_types <- 4
  mu <- 0.2
  pref <- matrix(mu, n_types, n_types)
  diag(pref) <- 1 - mu
  graph <- igraph::sample_pref(n_nodes, types = n_types, pref.matrix = pref)
  gt_membership <- igraph::V(graph)$type

  test_that("matrix works", {
    graph_i <- as.matrix(as.matrix(graph))
    actual <- speakeasyR::cluster(graph_i, seed = seed)
    expect_gt(igraph::compare(gt_membership, actual, "nmi"), 0.9)
  })

  test_that("sparse matrix works", {
    graph_i <- as.matrix(graph)
    actual <- speakeasyR::cluster(graph_i, seed = seed)
    expect_gt(igraph::compare(gt_membership, actual, "nmi"), 0.9)
  })

  test_that("igraph works", {
    actual <- speakeasyR::cluster(graph, seed = seed)
    expect_gt(igraph::compare(gt_membership, actual, "nmi"), 0.9)
  })

  test_that("Results are reproducible", {
    actual_1 <- speakeasyR::cluster(graph, seed = seed)
    graph_i <- as.matrix(graph)
    actual_2 <- speakeasyR::cluster(graph_i, seed = seed)
    expect_equal(actual_1, actual_2)
  })
}

Try the speakeasyR package in your browser

Any scripts or data that you put into this service are public.

speakeasyR documentation built on Sept. 9, 2025, 5:45 p.m.