File: test-model_parameters.glm.R

package info (click to toggle)
r-cran-parameters 0.24.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,852 kB
  • sloc: sh: 16; makefile: 2
file content (76 lines) | stat: -rw-r--r-- 2,992 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
68
69
70
71
72
73
74
75
76
skip_if_not_installed("boot")

test_that("model_parameters.lm", {
  model <- lm(mpg ~ wt, data = mtcars)
  params <- model_parameters(model, verbose = FALSE)
  expect_identical(c(nrow(params), ncol(params)), c(2L, 9L))
  expect_equal(params$CI_high, c(41.119752761418, -4.20263490802709), tolerance = 1e-3)
  expect_equal(attributes(params)$sigma, 3.045882, tolerance = 1e-3)

  params <- model_parameters(model, ci = c(0.8, 0.9), verbose = FALSE)
  expect_identical(c(nrow(params), ncol(params)), c(2L, 10L))

  params <- model_parameters(model, dispersion = TRUE, bootstrap = TRUE, iterations = 500, verbose = FALSE)
  expect_identical(c(nrow(params), ncol(params)), c(2L, 7L))

  model <- lm(mpg ~ wt + cyl, data = mtcars)
  params <- model_parameters(model, verbose = FALSE)
  expect_identical(c(nrow(params), ncol(params)), c(3L, 9L))

  model <- lm(mpg ~ wt * cyl, data = mtcars)
  params <- model_parameters(model, verbose = FALSE)
  expect_identical(c(nrow(params), ncol(params)), c(4L, 9L))

  params <- model_parameters(model, component = "conditional", effects = "fixed", verbose = FALSE)
})

test_that("print digits model_parameters.lm", {
  model <- lm(mpg ~ wt, data = mtcars)
  params <- model_parameters(model, digits = 4, ci_digits = 5, verbose = FALSE)
  out <- capture.output(print(params))
  expect_identical(out[3], "(Intercept) |     37.2851 | 1.8776 | [33.45050, 41.11975] | 19.8576 | < .001")
})


test_that("print digits model_parameters.lm", {
  skip_if_not_installed("performance")
  model <- lm(mpg ~ wt, data = mtcars)

  params <- model_parameters(model, include_info = TRUE, verbose = FALSE)
  expect_snapshot(params)

  params <- model_parameters(model, include_info = FALSE, verbose = FALSE)
  expect_snapshot(params)
})

test_that("model_parameters.glm - binomial", {
  set.seed(333)
  model <- glm(vs ~ wt + cyl, data = mtcars, family = "binomial")

  params <- model_parameters(model, verbose = FALSE)
  expect_identical(c(nrow(params), ncol(params)), c(3L, 9L))

  params <- suppressWarnings(model_parameters(model, bootstrap = TRUE, iterations = 500, verbose = FALSE))
  expect_identical(c(nrow(params), ncol(params)), c(3L, 6L))

  params <- model_parameters(model, component = "conditional", effects = "fixed", verbose = FALSE)
})

test_that("model_parameters.glm - Gamma - print", {
  # test printing for prevalence ratios
  clotting <- data.frame(
    u = c(5, 10, 15, 20, 30, 40, 60, 80, 100),
    lot1 = c(118, 58, 42, 35, 27, 25, 21, 19, 18),
    lot2 = c(69, 35, 26, 21, 18, 16, 13, 12, 12)
  )
  m <- glm(lot1 ~ log(u), data = clotting, family = Gamma("log"))
  mp <- model_parameters(m, exponentiate = TRUE)
  expect_snapshot(mp)
})

test_that("model_parameters.glm - glm, identity link", {
  data(mtcars)
  m <- glm(am ~ vs, data = mtcars, family = binomial(link = "identity"))
  p <- model_parameters(m)
  expect_identical(attributes(p)$coefficient_name, "Risk")
})