File: test-bootstrap_emmeans.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 (88 lines) | stat: -rw-r--r-- 2,545 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
77
78
79
80
81
82
83
84
85
86
87
88
skip_on_cran()

test_that("emmeans | lm", {
  skip_if_not_installed("emmeans")
  skip_if_not_installed("boot")
  skip_if_not_installed("coda")

  model <- lm(mpg ~ log(wt) + factor(cyl), data = mtcars)

  set.seed(7)
  b <- bootstrap_model(model, iterations = 1000)
  expect_equal(summary(emmeans::emmeans(b, ~cyl))$emmean,
    summary(emmeans::emmeans(model, ~cyl))$emmean,
    tolerance = 0.1
  )

  set.seed(7)
  b <- bootstrap_parameters(model, iterations = 1000)
  expect_equal(summary(emmeans::emmeans(b, ~cyl))$emmean,
    summary(emmeans::emmeans(model, ~cyl))$emmean,
    tolerance = 0.1
  )

  mp <- model_parameters(emmeans::emmeans(b, consec ~ cyl), verbose = FALSE)
  expect_identical(
    colnames(mp),
    c("Parameter", "Median", "CI", "CI_low", "CI_high", "pd", "Component")
  )
  expect_identical(nrow(mp), 5L)
})


test_that("emmeans | lmer", {
  skip_if_not_installed("emmeans")
  skip_if_not_installed("boot")
  skip_if_not_installed("lme4")
  skip_if_not_installed("coda")

  model <- lme4::lmer(mpg ~ log(wt) + factor(cyl) + (1 | gear), data = mtcars)

  set.seed(7)
  b <- bootstrap_model(model, iterations = 1000)
  expect_equal(summary(emmeans::emmeans(b, ~cyl))$emmean,
    summary(emmeans::emmeans(model, ~cyl))$emmean,
    tolerance = 0.1
  )

  set.seed(7)
  b <- bootstrap_parameters(model, iterations = 1000)
  expect_equal(summary(emmeans::emmeans(b, ~cyl))$emmean,
    summary(emmeans::emmeans(model, ~cyl))$emmean,
    tolerance = 0.1
  )

  mp <- suppressWarnings(model_parameters(emmeans::emmeans(b, consec ~ cyl)))
  expect_identical(
    colnames(mp),
    c("Parameter", "Median", "CI", "CI_low", "CI_high", "pd", "Component")
  )
  expect_identical(nrow(mp), 5L)
})

test_that("emmeans | glmmTMB", {
  skip_if_not_installed("coda")
  skip_if_not_installed("emmeans")
  skip_if_not_installed("boot")
  skip_if_not_installed("lme4")
  suppressWarnings(skip_if_not_installed("glmmTMB"))

  data(Salamanders, package = "glmmTMB")
  model <- glmmTMB::glmmTMB(count ~ spp + mined + (1 | site), family = glmmTMB::nbinom2, data = Salamanders)

  set.seed(7)
  b <- bootstrap_parameters(model, iterations = 10)
  out <- summary(emmeans::emmeans(b, ~spp, type = "response"))

  expect_equal(
    out$response,
    c(0.654, 0.1515, 0.8856, 0.261, 0.9775, 1.2909, 0.9031),
    tolerance = 0.1
  )

  expect_identical(
    colnames(out),
    c("spp", "response", "lower.HPD", "upper.HPD")
  )
  expect_identical(nrow(out), 7L)
})