File: test_base_generateCalibration.R

package info (click to toggle)
r-cran-mlr 2.18.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 7,088 kB
  • sloc: ansic: 65; sh: 13; makefile: 2
file content (61 lines) | stat: -rw-r--r-- 2,365 bytes parent folder | download
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
context("generateCalibration")

test_that("generateCalibrationData", {
  ## single prediction
  lrn = makeLearner("classif.rpart", predict.type = "prob")
  mod = train(lrn, binaryclass.task)
  pred = predict(mod, binaryclass.task)
  cd = generateCalibrationData(pred)
  expect_that(cd$proportion, is_a("data.frame"))
  expect_that(cd$data, is_a("data.frame"))

  plotCalibration(cd)
  dir = tempdir()
  path = file.path(dir, "test.svg")
  ggsave(path)
  doc = XML::xmlParse(path)
  expect_that(length(XML::getNodeSet(doc, red.line.xpath, ns.svg)), equals(length(unique(cd$data$Learner))))
  expect_that(length(XML::getNodeSet(doc, red.circle.xpath, ns.svg)), equals(nrow(cd$proportion) + 1))

  ## resample prediction
  rdesc = makeResampleDesc("CV", iters = 2L)
  r = resample(lrn, binaryclass.task, rdesc)
  cd = generateCalibrationData(r)
  expect_that(cd$proportion, is_a("data.frame"))
  expect_that(cd$data, is_a("data.frame"))

  plotCalibration(cd)
  dir = tempdir()
  path = file.path(dir, "test.svg")
  ggsave(path)
  doc = XML::xmlParse(path)
  expect_that(length(XML::getNodeSet(doc, red.line.xpath, ns.svg)), equals(length(unique(cd$data$Learner))))
  expect_that(length(XML::getNodeSet(doc, red.circle.xpath, ns.svg)), equals(nrow(cd$proportion) + 1))

  ## benchmark result
  lrns = list(lrn, makeLearner("classif.lda", predict.type = "prob"))
  res = benchmark(lrns, binaryclass.task, rdesc, show.info = FALSE)
  cd = generateCalibrationData(res)
  plotCalibration(cd)
  ggsave(path)
  doc = XML::xmlParse(path)
  expect_that(length(XML::getNodeSet(doc, grey.rect.xpath, ns.svg)), equals(length(unique(cd$data$Learner))))
  expect_that(length(XML::getNodeSet(doc, red.circle.xpath, ns.svg)), equals(nrow(cd$proportion) + 1))

  ## list of resample predictions
  rs = lapply(lrns, crossval, task = binaryclass.task, iters = 2L)
  names(rs) = c("a", "b")
  cd = generateCalibrationData(rs)
  plotCalibration(cd)
  ggsave(path)
  doc = XML::xmlParse(path)
  expect_that(length(XML::getNodeSet(doc, grey.rect.xpath, ns.svg)), equals(length(unique(cd$data$Learner))))
  expect_that(length(XML::getNodeSet(doc, red.circle.xpath, ns.svg)), equals(nrow(cd$proportion) + 1))

  # facetting works:
  q = q = plotCalibration(cd, facet.wrap.nrow = 2L)
  testFacetting(q, 2L)

  q = q = plotCalibration(cd, facet.wrap.ncol = 2L)
  testFacetting(q, ncol = 2L)
})