'update'
[epclust.git] / epclust / tests / testthat / test-assignMedoids.R
1 context("assignMedoids")
2
3 test_that("assignMedoids behave as expected",
4 {
5 # Generate a gaussian mixture
6 n = 999
7 L = 7
8 medoids = cbind( rep(0,L), rep(-5,L), rep(5,L) )
9 # short series...
10 series = t( rbind( MASS::mvrnorm(n/3, medoids[,1], diag(L)),
11 MASS::mvrnorm(n/3, medoids[,2], diag(L)),
12 MASS::mvrnorm(n/3, medoids[,3], diag(L)) ) )
13
14 # With high probability, medoids indices should resemble 1,1,1,...,2,2,2,...,3,3,3,...
15 mi = epclust:::assignMedoids(medoids, series)
16 mi_ref = rep(1:3, each=n/3)
17 expect_lt( mean(mi != mi_ref), 0.01 )
18
19 # Now with a random matrix, compare with (~trusted) R version
20 series = matrix(runif(n*L, min=-7, max=7), nrow=L)
21 mi = epclust:::assignMedoids(medoids, series)
22 mi_ref = R_assignMedoids(medoids, series)
23 expect_equal(mi, mi_ref)
24 })
25
26 # R-equivalent of , requiring a matrix
27 # (thus potentially breaking "fit-in-memory" hope)
28 R_assignMedoids <- function(medoids, series)
29 {
30 nb_series = ncol(series) #series in columns
31
32 mi = rep(NA,nb_series)
33 for (i in 1:nb_series)
34 mi[i] <- which.min( colSums( sweep(medoids, 1, series[,i], '-')^2 ) )
35
36 mi
37 }