Skip to content

Commit 6598746

Browse files
Merge pull request #164 from tidymodels/more-testing
2 parents cccf0e9 + 6676e77 commit 6598746

File tree

3 files changed

+214
-0
lines changed

3 files changed

+214
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
test_that("fitting", {
2+
set.seed(1234)
3+
spec <- hier_clust(num_clusters = 3) %>%
4+
set_engine("stats")
5+
6+
expect_no_error(
7+
res <- fit(spec, ~., mtcars)
8+
)
9+
10+
expect_no_error(
11+
res <- fit_xy(spec, mtcars)
12+
)
13+
})
14+
15+
test_that("predicting", {
16+
set.seed(1234)
17+
spec <- hier_clust(num_clusters = 3) %>%
18+
set_engine("stats")
19+
20+
res <- fit(spec, ~., iris)
21+
22+
preds <- predict(res, iris[c(25, 75, 125), ])
23+
24+
expect_identical(
25+
preds,
26+
tibble::tibble(.pred_cluster = factor(paste0("Cluster_", 1:3)))
27+
)
28+
})
29+
30+
test_that("extract_centroids() works", {
31+
set.seed(1234)
32+
spec <- hier_clust(num_clusters = 3) %>%
33+
set_engine("stats")
34+
35+
res <- fit(spec, ~., iris)
36+
37+
centroids <- extract_centroids(res)
38+
39+
expect_identical(
40+
colnames(centroids),
41+
c(".cluster", "Sepal.Length", "Sepal.Width", "Petal.Length",
42+
"Petal.Width", "Speciesversicolor", "Speciesvirginica")
43+
)
44+
45+
expect_identical(
46+
centroids$.cluster,
47+
factor(c("Cluster_1", "Cluster_2", "Cluster_3"))
48+
)
49+
})
50+
51+
test_that("extract_cluster_assignment() works", {
52+
set.seed(1234)
53+
spec <- hier_clust(num_clusters = 3) %>%
54+
set_engine("stats")
55+
56+
res <- fit(spec, ~., iris)
57+
58+
clusters <- extract_cluster_assignment(res)
59+
60+
expected <- vctrs::vec_cbind(
61+
tibble::tibble(.cluster = factor(paste0("Cluster_", cutree(res$fit, k = 3))))
62+
)
63+
64+
expect_identical(
65+
clusters,
66+
expected
67+
)
68+
})
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
test_that("fitting", {
2+
skip_if_not_installed("ClusterR")
3+
4+
set.seed(1234)
5+
spec <- k_means(num_clusters = 3) %>%
6+
set_engine("ClusterR")
7+
8+
expect_no_error(
9+
res <- fit(spec, ~., mtcars)
10+
)
11+
12+
expect_no_error(
13+
res <- fit_xy(spec, mtcars)
14+
)
15+
})
16+
17+
test_that("predicting", {
18+
skip_if_not_installed("ClusterR")
19+
20+
set.seed(1234)
21+
spec <- k_means(num_clusters = 3) %>%
22+
set_engine("ClusterR")
23+
24+
res <- fit(spec, ~., mtcars)
25+
26+
preds <- predict(res, mtcars[c(1:5), ])
27+
28+
expect_identical(
29+
preds,
30+
tibble::tibble(.pred_cluster = factor(paste0("Cluster_", c(1, 1, 1, 2, 2)),
31+
paste0("Cluster_", 1:3)))
32+
)
33+
})
34+
35+
test_that("extract_centroids() works", {
36+
skip_if_not_installed("ClusterR")
37+
38+
set.seed(1234)
39+
spec <- k_means(num_clusters = 3) %>%
40+
set_engine("ClusterR")
41+
42+
res <- fit(spec, ~., mtcars)
43+
44+
centroids <- extract_centroids(res)
45+
46+
expected <- vctrs::vec_cbind(
47+
tibble::tibble(.cluster = factor(paste0("Cluster_", 1:3))),
48+
tibble::as_tibble(res$fit$centroids)
49+
)
50+
51+
expect_identical(
52+
centroids,
53+
expected
54+
)
55+
})
56+
57+
test_that("extract_cluster_assignment() works", {
58+
skip_if_not_installed("ClusterR")
59+
60+
set.seed(1234)
61+
spec <- k_means(num_clusters = 3) %>%
62+
set_engine("ClusterR")
63+
64+
res <- fit(spec, ~., mtcars)
65+
66+
clusters <- extract_cluster_assignment(res)
67+
68+
exp_cluster <- res$fit$cluster
69+
exp_cluster <- order(unique(exp_cluster))[exp_cluster]
70+
71+
expected <- vctrs::vec_cbind(
72+
tibble::tibble(.cluster = factor(paste0("Cluster_", exp_cluster)))
73+
)
74+
75+
expect_identical(
76+
clusters,
77+
expected
78+
)
79+
})
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
test_that("fitting", {
2+
set.seed(1234)
3+
spec <- k_means(num_clusters = 5) %>%
4+
set_engine("stats")
5+
6+
expect_no_error(
7+
res <- fit(spec, ~., mtcars)
8+
)
9+
10+
expect_no_error(
11+
res <- fit_xy(spec, mtcars)
12+
)
13+
})
14+
15+
test_that("predicting", {
16+
set.seed(1234)
17+
spec <- k_means(num_clusters = 3) %>%
18+
set_engine("stats")
19+
20+
res <- fit(spec, ~., iris)
21+
22+
preds <- predict(res, iris[c(25, 75, 125), ])
23+
24+
expect_identical(
25+
preds,
26+
tibble::tibble(.pred_cluster = factor(paste0("Cluster_", 1:3)))
27+
)
28+
})
29+
30+
test_that("extract_centroids() works", {
31+
set.seed(1234)
32+
spec <- k_means(num_clusters = 3) %>%
33+
set_engine("stats")
34+
35+
res <- fit(spec, ~., iris)
36+
37+
centroids <- extract_centroids(res)
38+
39+
expected <- vctrs::vec_cbind(
40+
tibble::tibble(.cluster = factor(paste0("Cluster_", 1:3))),
41+
tibble::as_tibble(res$fit$centers)
42+
)
43+
44+
expect_identical(
45+
centroids,
46+
expected
47+
)
48+
})
49+
50+
test_that("extract_cluster_assignment() works", {
51+
set.seed(1234)
52+
spec <- k_means(num_clusters = 3) %>%
53+
set_engine("stats")
54+
55+
res <- fit(spec, ~., iris)
56+
57+
clusters <- extract_cluster_assignment(res)
58+
59+
expected <- vctrs::vec_cbind(
60+
tibble::tibble(.cluster = factor(paste0("Cluster_", res$fit$cluster)))
61+
)
62+
63+
expect_identical(
64+
clusters,
65+
expected
66+
)
67+
})

0 commit comments

Comments
 (0)