3 #shorthand: map 1->1, 2->2, 3->3, 4->1, ..., 149->2, 150->3, ... (is base==3)
7 test_that("computeClusters1 behave as expected",
9 require("MASS", quietly=TRUE)
10 if (!require("clue", quietly=TRUE))
11 skip("'clue' package not available")
13 # 3 gaussian clusters, 300 items; and then 7 gaussian clusters, 490 items
17 for (ndK in list( c(300,5,3), c(490,10,7) ))
19 n = ndK[1] ; d = ndK[2] ; K = ndK[3]
20 cs = n/K #cluster size
22 coefs = do.call(rbind,
23 lapply(1:K, function(i) MASS::mvrnorm(cs, c(rep(0,(i-1)),5,rep(0,d-i)), Id)))
24 indices_medoids = computeClusters1(coefs, K)
25 # Get coefs assignments (to medoids)
26 assignment = sapply(seq_len(n), function(i)
27 which.min( rowSums( sweep(coefs[indices_medoids,],2,coefs[i,],'-')^2 ) ) )
29 expect_equal(sum(assignment==i), cs, tolerance=5)
31 costs_matrix = matrix(nrow=K,ncol=K)
36 # assign i (in result) to j (order 1,2,3)
37 costs_matrix[i,j] = abs( mean(assignment[((i-1)*cs+1):(i*cs)]) - j )
40 permutation = as.integer( clue::solve_LSAP(costs_matrix) )
44 mean(assignment[((i-1)*cs+1):(i*cs)]), permutation[i], tolerance=0.05)
49 test_that("computeSynchrones behave as expected",
53 L = length(x) #96 1/4h
57 s3 = c( s1[1:(L%/%2)] , s2[(L%/%2+1):L] )
62 series = matrix(nrow=n, ncol=L)
64 series[i,] = s[[I(i,K)]] + rnorm(L,sd=0.01)
65 getRefSeries = function(indices) {
66 indices = indices[indices < n]
67 if (length(indices)>0) series[indices,] else NULL
69 synchrones = computeSynchrones(rbind(s1,s2,s3), getRefSeries, 100)
71 expect_equal(dim(synchrones), c(K,L))
73 expect_equal(synchrones[i,], s[[i]], tolerance=0.01)
76 computeDistortion = function(series, medoids)
78 n = nrow(series) ; L = ncol(series)
81 distortion = distortion + min( rowSums( sweep(medoids,2,series[i,],'-')^2 ) / L )
85 test_that("computeClusters2 behave as expected",
89 L = length(x) #96 1/4h
92 #for (i in 1:60) {plot(x^(1+i/30)*cos(x+i),type="l",col=i,ylim=c(-50,50)); par(new=TRUE)}
93 s = lapply( seq_len(K1), function(i) x^(1+i/30)*cos(x+i) )
94 series = matrix(nrow=n, ncol=L)
96 series[i,] = s[[I(i,K1)]] + rnorm(L,sd=0.01)
97 getRefSeries = function(indices) {
98 indices = indices[indices < n]
99 if (length(indices)>0) series[indices,] else NULL
101 # Artificially simulate 60 medoids - perfect situation, all equal to one of the refs
102 medoids_K1 = do.call(rbind, lapply( 1:K1, function(i) s[[I(i,K1)]] ) )
103 medoids_K2 = computeClusters2(medoids_K1, K2, getRefSeries, 75)
105 expect_equal(dim(medoids_K2), c(K2,L))
106 # Not easy to evaluate result: at least we expect it to be better than random selection of
107 # medoids within 1...K1 (among references)
109 distorGood = computeDistortion(series, medoids_K2)
111 expect_lte( distorGood, computeDistortion(series,medoids_K1[sample(1:K1, K2),]) )
114 test_that("clusteringTask + computeClusters2 behave as expected",
118 L = length(x) #96 1/4h
121 s = lapply( seq_len(K1), function(i) x^(1+i/30)*cos(x+i) )
122 series = matrix(nrow=n, ncol=L)
123 for (i in seq_len(n))
124 series[i,] = s[[I(i,K1)]] + rnorm(L,sd=0.01)
125 getSeries = function(indices) {
126 indices = indices[indices <= n]
127 if (length(indices)>0) series[indices,] else NULL
131 getContribs = function(indices) curvesToContribs(series[indices,],wf,ctype)
132 medoids_K1 = getSeries( clusteringTask(1:n, getContribs, K1, 75, 4) )
133 medoids_K2 = computeClusters2(medoids_K1, K2, getSeries, 120)
135 expect_equal(dim(medoids_K1), c(K1,L))
136 expect_equal(dim(medoids_K2), c(K2,L))
137 # Not easy to evaluate result: at least we expect it to be better than random selection of
138 # medoids within 1...K1 (among references)
139 distorGood = computeDistortion(series, medoids_K2)
141 expect_lte( distorGood, computeDistortion(series,medoids_K1[sample(1:K1, K2),]) )