Debian Codegraph @

tests/testthat/test-stats-kmeans.R

raw
1 skip_if_not_installed("modeltests")
2 library(modeltests)
3
4 set.seed(2)
5 x <- rbind(
6 matrix(rnorm(100, sd = 0.3), ncol = 2),
7 matrix(rnorm(100, mean = 1, sd = 0.3), ncol = 2)
8 )
9
10 fit <- kmeans(x, 2)
11
12 d <- data.frame(x = runif(100), y = runif(100))
13 fit2 <- kmeans(x = d, centers = 5)
14
15 test_that("kmeans tidier arguments", {
16 check_arguments(tidy.kmeans)
17 check_arguments(glance.kmeans)
18 check_arguments(augment.kmeans, strict = FALSE)
19 })
20
21 # tidy.kmeans uses the orginal column names to name columns in output.
22 # Therefore, strict must be set to FALSE for this test to pass.
23 test_that("tidy.kmeans", {
24 td <- tidy(fit)
25 check_tidy_output(td, strict = FALSE)
26 })
27
28 test_that("tidy.kmeans", {
29 gl <- glance(fit)
30 check_glance_outputs(gl)
31 })
32
33 test_that("augment.kmeans", {
34 check_augment_function(
35 aug = augment.kmeans,
36 model = fit,
37 data = x,
38 newdata = x,
39 strict = FALSE
40 )
41
42 check_augment_function(
43 aug = augment.kmeans,
44 model = fit2,
45 data = d,
46 newdata = d,
47 strict = FALSE
48 )
49 })