File: test_base_resample_repcv.R

package info (click to toggle)
r-cran-mlr 2.19.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 8,392 kB
  • sloc: ansic: 65; sh: 13; makefile: 5
file content (71 lines) | stat: -rwxr-xr-x 2,409 bytes parent folder | download | duplicates (2)
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

test_that("repcv instance works", {
  rin = makeResampleInstance(makeResampleDesc("RepCV", folds = 10, reps = 3),
    task = multiclass.task)

  iters = rin$desc$iters
  expect_equal(iters, 10 * 3)
  reps = rin$desc$reps
  expect_equal(reps, 3)

  for (j in 1:3) {
    bag = NULL
    for (i in 1:10) {
      k = as.integer((j - 1) * 10L + i)
      i1 = rin$train.inds[[i]]
      i2 = rin$test.inds[[i]]
      expect_equal(length(unique(i1)), 135)
      expect_equal(length(unique(i2)), 15)
      bag = c(bag, i2)
    }
    expect_equal(sort(unique(bag)), 1:150)
  }
})

test_that("repcv resampling works", {
  rdesc = makeResampleDesc("RepCV", folds = 2, reps = 2)
  m = setAggregation(mmce, testgroup.mean)
  res = resample(makeLearner("classif.lda"), multiclass.task, rdesc)
  expect_class(res, "ResampleResult")
})

test_that("repcv instance is stochastic", {
  rin = makeResampleInstance(makeResampleDesc("RepCV", folds = 10, reps = 3),
    task = multiclass.task)

  iters = rin$desc$iters
  expect_equal(iters, 10 * 3)
  reps = rin$desc$reps
  expect_equal(reps, 3)

  for (j in 1:3) {
    bag = NULL
    for (i in 1:10) {
      k = as.integer((j - 1) * 10L + i)
      i1 = rin$train.inds[[i]]
      i2 = rin$test.inds[[i]]
      expect_equal(length(unique(i1)), 135)
      expect_equal(length(unique(i2)), 15)
      bag = c(bag, i2)
    }
    expect_equal(sort(unique(bag)), 1:150)
  }
  rin1 = makeResampleInstance(makeResampleDesc("RepCV", folds = 2, reps = 2), size = 500)
  rin2 = makeResampleInstance(makeResampleDesc("RepCV", folds = 2, reps = 2), size = 500)
  expect_true(!all(sort(rin1$test.inds[[1]]) == sort(rin2$test.inds[[1]])))
})

test_that("test.join works somehow", {
  df = data.frame(t = factor(c(rep(c("a", "b"), each = 3), "c", "c")), x = 1:8)
  task = makeClassifTask(data = df, target = "t")
  lrn = makeLearner("classif.rpart")
  measures = list(mmce, setAggregation(mmce, test.join))
  rin = makeResampleInstance(makeResampleDesc("RepCV", reps = 5, folds = 3), task = task)
  res = resample(learner = lrn, task = task, resampling = rin, measures = measures)
  expect_equal(res$measures.test[, 2L], res$measures.test[, 3L])
  expect_true(diff(res$aggr) > 0)

  lrn = setPredictType(lrn, predict.type = "prob")
  res.prob = resample(learner = lrn, task = task, resampling = rin, measures = measures)
  expect_equal(res.prob$measures.test[, 2L], res.prob$measures.test[, 3L])
})