tests/testthat/test-enn.R

test_that("basic usage", {
  rec1 <- recipe(class ~ x + y, data = circle_example) |>
    step_enn(class)

  rec1_p <- prep(rec1)

  te_xtab <- table(bake(rec1_p, new_data = circle_example)$class, useNA = "no")
  og_xtab <- table(circle_example$class, useNA = "no")

  expect_all_true(as.vector(te_xtab <= og_xtab))

  expect_no_warning(prep(rec1))
})

test_that("neighbors argument changes result", {
  baked1 <- recipe(class ~ x + y, data = circle_example) |>
    step_enn(class, neighbors = 1) |>
    prep() |>
    bake(new_data = NULL)

  baked3 <- recipe(class ~ x + y, data = circle_example) |>
    step_enn(class, neighbors = 3) |>
    prep() |>
    bake(new_data = NULL)

  expect_false(identical(nrow(baked1), nrow(baked3)))
})

test_that("times argument applies ENN repeatedly", {
  set.seed(1)
  n <- 200
  noisy <- data.frame(
    x = rnorm(n),
    y = rnorm(n),
    class = factor(sample(c("a", "b"), n, replace = TRUE))
  )

  n1 <- nrow(enn(noisy, var = "class", times = 1))
  n2 <- nrow(enn(noisy, var = "class", times = 2))
  n_inf <- nrow(enn(noisy, var = "class", times = Inf))

  expect_lt(n2, n1)
  expect_lte(n_inf, n2)
})

test_that("times converges before reaching the cap", {
  set.seed(1)
  n <- 200
  noisy <- data.frame(
    x = rnorm(n),
    y = rnorm(n),
    class = factor(sample(c("a", "b"), n, replace = TRUE))
  )

  expect_identical(
    enn(noisy, var = "class", times = 100),
    enn(noisy, var = "class", times = Inf)
  )
})

test_that("all_k applies ENN with increasing neighbors", {
  set.seed(1)
  n <- 200
  noisy <- data.frame(
    x = rnorm(n),
    y = rnorm(n),
    class = factor(sample(c("a", "b"), n, replace = TRUE))
  )

  n1 <- nrow(enn(noisy, var = "class", neighbors = 3, times = 1))
  n_all <- nrow(enn(noisy, var = "class", neighbors = 3, all_k = TRUE))

  expect_lt(n_all, n1)
})

test_that("all_k argument accepted by step_enn()", {
  baked <- recipe(class ~ x + y, data = circle_example) |>
    step_enn(class, all_k = TRUE) |>
    prep() |>
    bake(new_data = NULL)

  expect_all_true(
    as.vector(table(baked$class) <= table(circle_example$class))
  )
})

test_that("kind_sel = 'all' removes at least as much as 'mode'", {
  circle_numeric <- circle_example[, c("x", "y", "class")]

  n_mode <- nrow(enn(circle_numeric, var = "class", kind_sel = "mode"))
  n_all <- nrow(enn(circle_numeric, var = "class", kind_sel = "all"))

  expect_lt(n_all, n_mode)
})

test_that("kind_sel = 'all' removes a superset of what 'mode' removes", {
  circle_numeric <- circle_example[, c("x", "y", "class")]

  mode_removed <- enn_impl(circle_numeric, var = "class", kind_sel = "mode")
  all_removed <- enn_impl(circle_numeric, var = "class", kind_sel = "all")

  expect_all_true(mode_removed %in% all_removed)
})

test_that("kind_sel argument accepted by step_enn()", {
  baked <- recipe(class ~ x + y, data = circle_example) |>
    step_enn(class, kind_sel = "all") |>
    prep() |>
    bake(new_data = NULL)

  expect_identical(
    baked,
    tibble::as_tibble(
      enn(circle_example[, c("x", "y", "class")], "class", kind_sel = "all")
    )[, c("x", "y", "class")]
  )
})

test_that("kind_sel is validated", {
  expect_snapshot(
    error = TRUE,
    enn(circle_example[, c("x", "y", "class")], "class", kind_sel = "most")
  )
  expect_snapshot(
    error = TRUE,
    recipe(class ~ x + y, data = circle_example) |>
      step_enn(class, kind_sel = "most")
  )
})

test_that("warns when both times and all_k are set", {
  expect_snapshot(
    tmp <- enn(
      circle_example[, c("x", "y", "class")],
      var = "class",
      times = 2,
      all_k = TRUE
    )
  )
  expect_snapshot(
    recipe(class ~ x + y, data = circle_example) |>
      step_enn(class, times = 2, all_k = TRUE) |>
      prep()
  )
})

test_that("errors when not enough observations for neighbors", {
  small <- circle_example[1:3, c("x", "y", "class")]

  expect_snapshot(
    error = TRUE,
    recipe(class ~ x + y, data = small) |>
      step_enn(class, neighbors = 5, skip = FALSE) |>
      prep() |>
      bake(new_data = NULL)
  )
})

test_that("works with a single predictor", {
  skip_if_not_installed("modeldata")

  data("hpc_data", package = "modeldata")

  expect_no_error(
    recipe(class ~ compounds, data = hpc_data) |>
      step_enn(all_outcomes()) |>
      prep() |>
      bake(NULL)
  )
})

test_that("bad data", {
  rec <- recipe(~., data = circle_example)
  # numeric check
  expect_snapshot(
    error = TRUE,
    rec |>
      step_enn(x) |>
      prep()
  )
  # Multiple variable check
  expect_snapshot(
    error = TRUE,
    rec |>
      step_enn(class, id) |>
      prep()
  )
})

test_that("errors if character are present", {
  df_char <- data.frame(
    x = factor(1:2),
    y = c("A", "A"),
    stringsAsFactors = FALSE
  )

  expect_snapshot(
    error = TRUE,
    recipe(~., data = df_char) |>
      step_enn(x) |>
      prep()
  )
})

test_that("NA in response", {
  skip_if_not_installed("modeldata")

  data("credit_data", package = "modeldata")
  credit_data0 <- credit_data
  credit_data0[1, 1] <- NA

  expect_snapshot(
    error = TRUE,
    recipe(Status ~ Age, data = credit_data0) |>
      step_enn(Status) |>
      prep()
  )
})

test_that("test tidy()", {
  rec <- recipe(class ~ x + y, data = circle_example) |>
    step_enn(class, id = "")

  rec_p <- prep(rec)

  untrained <- tibble(
    terms = "class",
    id = ""
  )

  trained <- tibble(
    terms = "class",
    id = ""
  )

  expect_equal(untrained, tidy(rec, number = 1))
  expect_equal(trained, tidy(rec_p, number = 1))
})

test_that("factor levels are not affected by alphabet ordering or class sizes", {
  circle_example_alt_levels <- list()
  for (i in 1:4) {
    circle_example_alt_levels[[i]] <- circle_example
  }

  # Checking for forgetting levels by majority/minor switching
  for (i in c(2, 4)) {
    levels(circle_example_alt_levels[[i]]$class) <-
      rev(levels(circle_example_alt_levels[[i]]$class))
  }

  # Checking for forgetting levels by alphabetical switching
  for (i in c(3, 4)) {
    circle_example_alt_levels[[i]]$class <-
      factor(
        x = circle_example_alt_levels[[i]]$class,
        levels = rev(levels(circle_example_alt_levels[[i]]$class))
      )
  }

  for (i in 1:4) {
    rec_p <- recipe(class ~ x + y, data = circle_example_alt_levels[[i]]) |>
      step_enn(class) |>
      prep()

    expect_equal(
      levels(circle_example_alt_levels[[i]]$class), # Original levels
      rec_p$levels$class$values # New levels
    )
    expect_equal(
      levels(circle_example_alt_levels[[i]]$class), # Original levels
      levels(bake(rec_p, new_data = NULL)$class) # New levels
    )
  }
})

test_that("distance_with allows non-numeric columns to be present", {
  df_mixed <- data.frame(
    x = c(rnorm(50, 0, 1), rnorm(20, 0.5, 1)),
    y = c(rnorm(50, 0, 1), rnorm(20, 0.5, 1)),
    name = c(rep("alice", 50), rep("bob", 20)),
    class = factor(c(rep("majority", 50), rep("minority", 20)))
  )

  expect_no_error(
    recipe(class ~ ., data = df_mixed) |>
      step_enn(class, distance_with = c(x, y)) |>
      prep() |>
      bake(new_data = NULL)
  )
})

test_that("distance_with errors on non-numeric column", {
  df_mixed <- data.frame(
    x = c(1:5, 1:2),
    name = c(rep("a", 5), rep("b", 2)),
    class = factor(c(rep("majority", 5), rep("minority", 2)))
  )

  expect_snapshot(
    error = TRUE,
    recipe(class ~ ., data = df_mixed) |>
      step_enn(class, distance_with = c(x, name)) |>
      prep()
  )
})

test_that("id variables are ignored", {
  rec_id <- recipe(class ~ ., data = circle_example) |>
    update_role(id, new_role = "id") |>
    step_enn(class) |>
    prep()

  expect_equal(ncol(bake(rec_id, new_data = NULL)), 4)
})

test_that("id variables don't turn predictors to factors", {
  rec_id <- recipe(class ~ ., data = circle_example) |>
    update_role(id, new_role = "id") |>
    step_enn(class) |>
    prep() |>
    bake(new_data = NULL)

  expect_equal(is.double(rec_id$x), TRUE)
  expect_equal(is.double(rec_id$y), TRUE)
})

test_that("distance argument accepted by step_enn()", {
  expect_no_error(
    recipe(class ~ x + y, data = circle_example) |>
      step_enn(class, distance = "euclidean") |>
      prep() |>
      bake(new_data = NULL)
  )
  expect_no_error(
    recipe(class ~ x + y, data = circle_example) |>
      step_enn(class, distance = "cosine") |>
      prep() |>
      bake(new_data = NULL)
  )
  expect_no_error(
    recipe(class ~ x + y, data = circle_example) |>
      step_enn(class, distance = "mahalanobis") |>
      prep() |>
      bake(new_data = NULL)
  )
  expect_no_error(
    recipe(class ~ x + y, data = circle_example) |>
      step_enn(class, distance = "manhattan") |>
      prep() |>
      bake(new_data = NULL)
  )
  expect_no_error(
    recipe(class ~ x + y, data = circle_example) |>
      step_enn(class, distance = "chebyshev") |>
      prep() |>
      bake(new_data = NULL)
  )
})

test_that("bad distance arg for step_enn()", {
  expect_snapshot(
    error = TRUE,
    bake(
      prep(step_enn(
        recipe(class ~ x + y, data = circle_example),
        class,
        distance = "L2"
      )),
      new_data = NULL
    )
  )
})

test_that("tunable", {
  rec <- recipe(class ~ x + y, data = circle_example) |>
    step_enn(class)

  tune_args <- tunable(rec$steps[[1]])
  expect_equal(tune_args$name, c("neighbors", "all_k"))
  expect_equal(nrow(tune_args), 2L)
  expect_equal(tune_args$call_info[[1]]$range, c(1, 10))
})

test_that("tunable is setup to works with extract_parameter_set_dials", {
  skip_if_not_installed("dials")
  rec <- recipe(class ~ x + y, data = circle_example) |>
    step_enn(class, neighbors = hardhat::tune())

  params <- extract_parameter_set_dials(rec)

  expect_s3_class(params, "parameters")
  expect_identical(nrow(params), 1L)
})

test_that("bad args", {
  expect_snapshot(
    error = TRUE,
    recipe(~., data = mtcars) |>
      step_enn(seed = TRUE)
  )
  expect_snapshot(
    error = TRUE,
    recipe(class ~ x + y, data = circle_example) |>
      step_enn(class, neighbors = -1) |>
      prep()
  )
  expect_snapshot(
    error = TRUE,
    recipe(class ~ x + y, data = circle_example) |>
      step_enn(class, times = -1) |>
      prep()
  )
  expect_snapshot(
    error = TRUE,
    recipe(class ~ x + y, data = circle_example) |>
      step_enn(class, all_k = 1) |>
      prep()
  )
})

test_that("unused outcome levels are skipped with a warning (#238)", {
  circle_example$class <- factor(
    circle_example$class,
    levels = c(levels(circle_example$class), "unused")
  )

  expect_snapshot(
    res <- recipe(class ~ x + y, data = circle_example) |>
      step_enn(class) |>
      prep() |>
      bake(new_data = NULL)
  )

  expect_gt(nrow(res), 0)
})

test_that("enn() works with a character `var` (#261)", {
  df <- circle_example[c("x", "y", "class")]
  df$class <- as.character(df$class)

  res <- enn(df, "class")

  expect_type(res$class, "character")
  expect_identical(sort(unique(res$class)), c("Circle", "Rest"))
  expect_identical(sum(is.na(res$class)), 0L)
})

test_that("enn() handles exact-duplicate coordinates (#247)", {
  df <- data.frame(
    x = c(rep(0, 6), 1, 2, 3, 4),
    y = c(rep(0, 6), 1, 2, 3, 4),
    class = factor(c(rep("a", 3), rep("b", 3), "a", "b", "a", "b"))
  )

  # Duplicated points at (0, 0) belong to both classes; a point must not be
  # kept purely because it counted itself as a neighbor.
  expect_no_error(enn(df, var = "class", neighbors = 3))
})

# Infrastructure ---------------------------------------------------------------

test_that("bake method errors when needed non-standard role columns are missing", {
  rec <- recipe(class ~ x + y, data = circle_example) |>
    step_enn(class, skip = FALSE) |>
    add_role(class, new_role = "potato") |>
    update_role_requirements(role = "potato", bake = FALSE)

  trained <- prep(rec, training = circle_example, verbose = FALSE)

  expect_snapshot(
    error = TRUE,
    bake(trained, new_data = circle_example[, -3])
  )
})

test_that("empty printing", {
  rec <- recipe(mpg ~ ., mtcars)
  rec <- step_enn(rec)

  expect_snapshot(rec)

  rec <- prep(rec, mtcars)

  expect_snapshot(rec)
})

test_that("empty selection prep/bake is a no-op", {
  rec1 <- recipe(mpg ~ ., mtcars)
  rec2 <- step_enn(rec1)

  rec1 <- prep(rec1, mtcars)
  rec2 <- prep(rec2, mtcars)

  baked1 <- bake(rec1, mtcars)
  baked2 <- bake(rec2, mtcars)

  expect_identical(baked1, baked2)
})

test_that("empty selection tidy method works", {
  rec <- recipe(mpg ~ ., mtcars)
  rec <- step_enn(rec)

  expect <- tibble(terms = character(), id = character())

  expect_identical(tidy(rec, number = 1), expect)

  rec <- prep(rec, mtcars)

  expect_identical(tidy(rec, number = 1), expect)
})

test_that("printing", {
  rec <- recipe(class ~ x + y, data = circle_example) |>
    step_enn(class)

  expect_snapshot(print(rec))
  expect_snapshot(prep(rec))
})

test_that("0 and 1 rows data work in bake method", {
  rec <- recipe(class ~ x + y, data = circle_example) |>
    step_enn(class, skip = FALSE) |>
    prep()

  expect_identical(nrow(bake(rec, new_data = slice(circle_example, 0))), 0L)
  expect_identical(nrow(bake(rec, new_data = slice(circle_example, 1))), 1L)
})

Try the themis package in your browser

Any scripts or data that you put into this service are public.

themis documentation built on Aug. 2, 2026, 9:07 a.m.