File: test-makepredictcall.R

package info (click to toggle)
r-cran-datawizard 1.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,300 kB
  • sloc: sh: 13; makefile: 2
file content (67 lines) | stat: -rw-r--r-- 2,430 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
62
63
64
65
66
67
test_that("makepredictcall", {
  data("mtcars")
  nd <- data.frame(hp = c(100, 200))

  m1 <- lm(mpg ~ scale(hp, scale = FALSE), mtcars)
  m2 <- lm(mpg ~ center(hp), mtcars)

  m3 <- lm(mpg ~ scale(hp), mtcars)
  m4 <- lm(mpg ~ standardize(hp), mtcars)

  p1 <- predict(m1, nd)
  expect_equal(p1, predict(m2, nd))
  expect_equal(p1, predict(m3, nd))
  expect_equal(p1, predict(m4, nd))

  X <- matrix(rnorm(100), ncol = 2)
  Y <- rnorm(50)
  expect_error(lm(Y ~ standardize(X)), "matrices")
})


test_that("makepredictcall, normalize", {
  data("mtcars")
  m1 <- lm(mpg ~ normalize(hp), data = mtcars)
  m2 <- lm(mpg ~ hp, data = mtcars)
  m3 <- lm(mpg ~ normalize(hp, include_bounds = FALSE), data = mtcars)

  out1 <- predict(m1, newdata = data.frame(hp = c(100, 110, 120)))
  out2 <- predict(m2, newdata = data.frame(hp = c(100, 110, 120)))
  out3 <- predict(m3, newdata = data.frame(hp = c(100, 110, 120)))

  expect_equal(out1, out2, tolerance = 1e-3, ignore_attr = TRUE)
  expect_equal(out1, out3, tolerance = 1e-3, ignore_attr = TRUE)
  expect_equal(out2, out3, tolerance = 1e-3, ignore_attr = TRUE)

  out1 <- predict(m1, newdata = data.frame(hp = 110))
  out2 <- predict(m2, newdata = data.frame(hp = 110))
  out3 <- predict(m3, newdata = data.frame(hp = 110))

  expect_equal(out1, out2, tolerance = 1e-3, ignore_attr = TRUE)
  expect_equal(out1, out3, tolerance = 1e-3, ignore_attr = TRUE)
  expect_equal(out2, out3, tolerance = 1e-3, ignore_attr = TRUE)
})


test_that("makepredictcall, rescale", {
  data("mtcars")
  m1 <- lm(mpg ~ rescale(hp, to = c(50, 80)), data = mtcars)
  m2 <- lm(mpg ~ hp, data = mtcars)
  m3 <- lm(mpg ~ rescale(hp), data = mtcars)

  out1 <- predict(m1, newdata = data.frame(hp = c(100, 110, 120)))
  out2 <- predict(m2, newdata = data.frame(hp = c(100, 110, 120)))
  out3 <- predict(m3, newdata = data.frame(hp = c(100, 110, 120)))

  expect_equal(out1, out2, tolerance = 1e-3, ignore_attr = TRUE)
  expect_equal(out1, out3, tolerance = 1e-3, ignore_attr = TRUE)
  expect_equal(out2, out3, tolerance = 1e-3, ignore_attr = TRUE)

  out1 <- predict(m1, newdata = data.frame(hp = 110))
  out2 <- predict(m2, newdata = data.frame(hp = 110))
  out3 <- predict(m3, newdata = data.frame(hp = 110))

  expect_equal(out1, out2, tolerance = 1e-3, ignore_attr = TRUE)
  expect_equal(out1, out3, tolerance = 1e-3, ignore_attr = TRUE)
  expect_equal(out2, out3, tolerance = 1e-3, ignore_attr = TRUE)
})