tests/testthat/testOutput.R

# this file tests the equivalence of the output of the implemented method
# comparing to the standard kmeans function of the stat package

set.seed(27)
k <- sample.int(10, 1)
set.seed(27)
h <- sample.int(10, 1)
X <- rbind(matrix(rnorm(100, sd = 0.5), ncol = 2),
           matrix(rnorm(100, mean = 1, sd = 0.5), ncol = 2))
set.seed(27)
result <- kMeansLloyd(x = scale(X), centroids = k, nStart = h)
set.seed(27)
reference <- kmeans(x = scale(X), centers = k, nstart = h, algorithm = "Lloyd")
sumRes <- summary(result)

# cluster allocation
test_that("cluster allocation", {
  expect_equal(result$cluster,
               reference$cluster)
})

# centroids
test_that("cluster allocation", {
  expect_equal(result$cluster,
               reference$cluster)
})

# iterations
test_that("iterations", {
  expect_equal(result$iterations,
               reference$iter)
})

# groupSizes
test_that("cluster allocation", {
  expect_equal(result$groupSizes,
               reference$size)
})

# withinSS
test_that("within sum of squares", {
  expect_equal(result$withinSS,
               reference$withinss)
})

# withinTot
test_that("sum within sum of squares", {
  expect_equal(result$withinTot,
               reference$tot.withinss)
})

# between
test_that("between sum of squares", {
  expect_equal(sumRes$betweenSS,
               reference$betweenss)
})

# total
test_that("total sum of squares", {
  expect_equal(sumRes$totalSS,
               reference$totss)
})
heiligerl/kMeans_Rpackage documentation built on Aug. 16, 2020, 4:04 p.m.