File: test_sampling_options.R

package info (click to toggle)
r-cran-caret 7.0-1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 4,036 kB
  • sloc: ansic: 210; sh: 10; makefile: 2
file content (96 lines) | stat: -rw-r--r-- 2,493 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
library(caret)
library(testthat)

context("sampling options")
load(system.file("models", "sampling.RData", package = "caret"))

test_that('check appropriate sampling calls by name', {
  skip_on_cran()
  arg_names <- c("up", "down", "rose", "smote")
  arg_funcs <- sampling_methods
  arg_first <- c(TRUE, FALSE)

  ## test that calling by string gives the right result
  for(i in arg_names) {
    out <- caret:::parse_sampling(i, check_install = FALSE)
    expected <- list(name = i,
                     func = sampling_methods[[i]],
                     first = TRUE)
    expect_equivalent(out, expected)
  }
})

test_that('check appropriate sampling calls by function', {
  skip_on_cran()
  arg_names <- c("up", "down", "rose", "smote")
  arg_funcs <- sampling_methods
  arg_first <- c(TRUE, FALSE)

  ## test that calling by function gives the right result
  for(i in arg_names) {
    out <- caret:::parse_sampling(sampling_methods[[i]], 
                                  check_install = FALSE)
    expected <- list(name = "custom",
                     func = sampling_methods[[i]],
                     first = TRUE)
    expect_equivalent(out, expected)
  }
})

test_that('check bad sampling name', {
  skip_on_cran()
  expect_error(caret:::parse_sampling("what?"))
})

test_that('check bad first arg', {
  skip_on_cran()
  expect_error(
    caret:::parse_sampling(
      list(name = "yep", func = sampling_methods[["up"]], first = 2), 
      check_install = FALSE)
    )
})

test_that('check bad func arg', {
  skip_on_cran()
  expect_error(
    caret:::parse_sampling(
      list(name = "yep", func = I, first = 2), 
      check_install = FALSE)
    )
})

test_that('check incomplete list', {
  skip_on_cran()
  expect_error(caret:::parse_sampling(list(name = "yep"), check_install = FALSE))
})

test_that('check call', {
  skip_on_cran()
  expect_error(caret:::parse_sampling(14, check_install = FALSE))
})

###################################################################
##

test_that('check getting all methods', {
  skip_on_cran()
  expect_equivalent(getSamplingInfo(), sampling_methods)
})

test_that('check getting one method', {
  skip_on_cran()
  arg_names <- c("up", "down", "rose", "smote")
  for(i in arg_names) {
    out <- getSamplingInfo(i, regex = FALSE)
    expected <- list(sampling_methods[[i]])
    names(expected) <- i
    expect_equivalent(out, expected)
  }
})

test_that('check missing method', {
  skip_on_cran()
  expect_error(getSamplingInfo("plum"))
})