# check error messages / messages if the inputs violate the desired format
X <- scale(rbind(matrix(rnorm(100, sd = 0.5), ncol = 2),
matrix(rnorm(100, mean = 1, sd = 0.5), ncol = 2)))
test_that("x", {
expect_error(kMeansLloyd(rbind(X, c(1, NA)), 2), "'x' contains missing values")
expect_error(kMeansLloyd(X[, 2], 2, "ncol(x) > 1 required"))
expect_error(kMeansLloyd(rbind(X, c(1, "string")), 2),
"cannot process non-numeric data")
})
test_that("centroids", {
expect_error(kMeansLloyd(X),
"'centroids' missing")
expect_error(kMeansLloyd(X, 1:3),
"must have same number of columns in 'x' and 'centroids'")
expect_error(kMeansLloyd(X, matrix(c(.2, 1000, .4, 1000), 2)),
"empty cluster: please provide better starting centroids")
expect_message(kMeansLloyd(X, 3.5),
"Note: 'centroids' is not an integer, input is truncated")
expect_error(kMeansLloyd(X, "string"), "'centroids' neither number nor matrix")
expect_error(kMeansLloyd(X, TRUE), "'centroids' neither number nor matrix")
expect_error(kMeansLloyd(X, -2), "'centroids' must be positive")
})
test_that("maxIter", {
expect_error(kMeansLloyd(X, 2, maxIter = 1:3),
"'maxIter' must be positive number")
expect_message(kMeansLloyd(X, 2, maxIter = 10.5),
"Note: 'maxIter' is not an integer, input is truncated")
expect_error(kMeansLloyd(X, 2, maxIter = "string"),
"'maxIter' must be positive number")
expect_error(kMeansLloyd(X, 2, maxIter = TRUE),
"'maxIter' must be positive number")
expect_error(kMeansLloyd(X, 2, maxIter = -2),
"'maxIter' must be positive number")
})
test_that("nStart", {
expect_error(kMeansLloyd(X, 2, nStart = 1:3),
"'nStart' must be positive number")
expect_message(kMeansLloyd(X, 2, nStart = 10.5),
"Note: 'nStart' is not an integer, input is truncated")
expect_error(kMeansLloyd(X, 2, nStart = "string"),
"'nStart' must be positive number")
expect_error(kMeansLloyd(X, 2, nStart = TRUE),
"'nStart' must be positive number")
expect_error(kMeansLloyd(X, 2, nStart = -2),
"'nStart' must be positive number")
})
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.