-#shorthand: map 1->1, 2->2, 3->3, 4->1, ..., 149->2, 150->3, ... (is base==3)
-I = function(i, base)
- (i-1) %% base + 1
-
-test_that("computeClusters1&2 behave as expected",
-{
- require("MASS", quietly=TRUE)
- if (!require("clue", quietly=TRUE))
- skip("'clue' package not available")
-
- # 3 gaussian clusters, 300 items; and then 7 gaussian clusters, 490 items
- n = 300
- d = 5
- K = 3
- for (ndK in list( c(300,5,3), c(490,10,7) ))
- {
- n = ndK[1] ; d = ndK[2] ; K = ndK[3]
- cs = n/K #cluster size
- Id = diag(d)
- coefs = sapply(1:K, function(i) MASS::mvrnorm(cs, c(rep(0,(i-1)),5,rep(0,d-i)), Id))
- indices_medoids1 = computeClusters1(coefs, K, verbose=TRUE)
- indices_medoids2 = computeClusters2(dist(coefs), K, verbose=TRUE)
- # Get coefs assignments (to medoids)
- assignment1 = sapply(seq_len(n), function(i)
- which.min( colSums( sweep(coefs[,indices_medoids1],1,coefs[,i],'-')^2 ) ) )
- assignment2 = sapply(seq_len(n), function(i)
- which.min( colSums( sweep(coefs[,indices_medoids2],1,coefs[,i],'-')^2 ) ) )
- for (i in 1:K)
- {
- expect_equal(sum(assignment1==i), cs, tolerance=5)
- expect_equal(sum(assignment2==i), cs, tolerance=5)
- }
-
- costs_matrix1 = matrix(nrow=K,ncol=K)
- costs_matrix2 = matrix(nrow=K,ncol=K)
- for (i in 1:K)
- {
- for (j in 1:K)
- {
- # assign i (in result) to j (order 1,2,3)
- costs_matrix1[i,j] = abs( mean(assignment1[((i-1)*cs+1):(i*cs)]) - j )
- costs_matrix2[i,j] = abs( mean(assignment2[((i-1)*cs+1):(i*cs)]) - j )
- }
- }
- permutation1 = as.integer( clue::solve_LSAP(costs_matrix1) )
- permutation2 = as.integer( clue::solve_LSAP(costs_matrix2) )
- for (i in 1:K)
- {
- expect_equal(
- mean(assignment1[((i-1)*cs+1):(i*cs)]), permutation1[i], tolerance=0.05)
- expect_equal(
- mean(assignment2[((i-1)*cs+1):(i*cs)]), permutation2[i], tolerance=0.05)
- }
- }
-})
-