# Test script for ensemble clustering functionality library(bootcluster) # Generate test data set.seed(123) n <- 150 # number of points centers <- matrix(c(0,0, 2,2, -2,2), ncol=2, byrow=TRUE) clusters <- sample(1:3, n, replace=TRUE) x <- matrix(0, nrow=n, ncol=2) for(i in 1:n) { x[i,] <- centers[clusters[i],] + rnorm(2, 0, 0.3) } # Test ensemble clustering with different k values test_ensemble <- function() { # Test case 1: Equal k values res1 <- ensemble_clustering(x, k_km=3, k_hc=3, k_sc=3) cat("Test 1 - Equal k values:\n") cat("Number of clusters found:", length(unique(res1$membership)), "\n") cat("Modularity:", res1$modularity, "\n\n") # Test case 2: Different k values res2 <- ensemble_clustering(x, k_km=3, k_hc=4, k_sc=2) cat("Test 2 - Different k values:\n") cat("Number of clusters found:", length(unique(res2$membership)), "\n") cat("Modularity:", res2$modularity, "\n\n") # Test visualization plots <- plot_clustering_results(x, res1, pca=FALSE) print(plots$ensemble) # Test Smin analysis smin_plot <- plot_smin_analysis(res1) print(smin_plot) } # Run tests test_ensemble() # Test reference distributions test_reference <- function() { ref_data <- ref_dist(x) cat("Reference distribution shape:", dim(ref_data), "\n") smin_delta <- calculate_smin_delta(x, k=3, method="kmeans") cat("Smin delta:", smin_delta$Smin_delta, "\n") } # Run reference distribution tests test_reference() # Test graph construction test_graph <- function() { # Get base clustering results km_res <- calculate_smin_delta(x, k=3, method="kmeans") hc_res <- calculate_smin_delta(x, k=3, method="hierarchical") sc_res <- calculate_smin_delta(x, k=3, method="spectral") # Create consensus graph cluster_results <- list( kmeans = km_res$results, hierarchical = hc_res$results, spectral = sc_res$results ) smin_deltas <- c(km_res$Smin_delta, hc_res$Smin_delta, sc_res$Smin_delta) graph <- create_consensus_graph(x, cluster_results, smin_deltas) cat("Graph properties:\n") cat("Number of vertices:", vcount(graph), "\n") cat("Number of edges:", ecount(graph), "\n") cat("Is weighted:", is_weighted(graph), "\n") } # Run graph construction tests test_graph()