1library(testthat)
2library(recipes)
3library(ddalpha)
4
5
6test_that("defaults", {
7  rec <- recipe(Species ~ ., data = iris) %>%
8    step_depth(all_predictors(), class = "Species", metric = "spatial", id = "")
9  trained <- prep(rec, training = iris, verbose = FALSE)
10  depths <- bake(trained, new_data = iris)
11  depths <- depths[, grepl("depth", names(depths))]
12  depths <- as.data.frame(depths)
13
14  split_up <- split(iris[, 1:4], iris$Species)
15  spatial <- function(x, y)
16    depth.spatial(x = y, data = x)
17
18  exp_res <- lapply(split_up, spatial, y = iris[, 1:4])
19  exp_res <- as.data.frame(exp_res)
20
21  for(i in 1:ncol(exp_res))
22    expect_equal(depths[, i], exp_res[, i])
23
24  depth_tibble_un <-
25    tibble(terms = "all_predictors()",
26           class = NA_character_,
27           id = "")
28  depth_tibble_tr <-
29    tibble(terms = names(iris)[1:4],
30           class = rep("Species", 4),
31           id = "")
32
33  expect_equal(tidy(rec, 1), depth_tibble_un)
34  expect_equal(tidy(trained, 1), depth_tibble_tr)
35
36})
37
38test_that("alt args", {
39  rec <- recipe(Species ~ ., data = iris) %>%
40    step_depth(all_predictors(), class = "Species",
41               metric = "Mahalanobis",
42               options = list(mah.estimate = "MCD", mah.parMcd = .75))
43  trained <- prep(rec, training = iris, verbose = FALSE)
44  depths <- bake(trained, new_data = iris)
45  depths <- depths[, grepl("depth", names(depths))]
46  depths <- as.data.frame(depths)
47
48  split_up <- split(iris[, 1:4], iris$Species)
49  Mahalanobis <- function(x, y)
50    depth.Mahalanobis(x = y, data = x, mah.estimate = "MCD", mah.parMcd = .75)
51
52  exp_res <- lapply(split_up, Mahalanobis, y = iris[, 1:4])
53  exp_res <- as.data.frame(exp_res)
54
55  head(exp_res)
56  head(depths)
57
58  for(i in 1:ncol(exp_res))
59    expect_equal(depths[, i], exp_res[, i])
60})
61
62
63test_that('printing', {
64  rec <- recipe(Species ~ ., data = iris) %>%
65    step_depth(all_predictors(), class = "Species", metric = "spatial")
66  expect_output(print(rec))
67  expect_output(prep(rec, training = iris, verbose = TRUE))
68})
69
70test_that('prefix', {
71  rec <- recipe(Species ~ ., data = iris) %>%
72    step_depth(all_predictors(), class = "Species",
73               metric = "spatial", prefix = "spatial_")
74  trained <- prep(rec, training = iris, verbose = FALSE)
75  dists <- bake(trained, new_data = iris)
76  expect_false(any(grepl("depth_", names(dists))))
77  expect_true(any(grepl("spatial_", names(dists))))
78})
79