File: test-ci.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 (48 lines) | stat: -rw-r--r-- 2,007 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
test_that("ci", {
  skip_if_not_installed("lme4")

  model <- lm(mpg ~ wt, data = mtcars)
  expect_equal(suppressMessages(ci(model))[1, 3], 33.4505, tolerance = 0.01)
  expect_equal(suppressMessages(ci(model, ci = c(0.7, 0.8)))[1, 3], 35.30486, tolerance = 0.01)

  model <- glm(vs ~ wt, family = "binomial", data = mtcars)
  expect_equal(suppressMessages(ci(model))[1, 3], 1.934013, tolerance = 0.01)

  model <- lme4::lmer(wt ~ cyl + (1 | gear), data = mtcars)
  expect_equal(suppressMessages(ci(model, method = "normal"))[1, 3], -0.335063, tolerance = 0.01)

  model <- lme4::lmer(wt ~ cyl + (1 | gear), data = mtcars)
  expect_equal(ci(model)[1, 3], -0.3795646, tolerance = 0.01)

  set.seed(1)
  val <- ci(model, method = "boot")[1, 3]
  expect_equal(val, -0.555424, tolerance = 0.01)

  model <- lme4::glmer(vs ~ cyl + (1 | gear), data = mtcars, family = "binomial")
  expect_equal(ci(model)[1, 3], -0.7876679, tolerance = 0.01)

  model <- lme4::glmer(vs ~ drat + cyl + (1 | gear), data = mtcars, family = "binomial")
  expect_equal(ci(model)[1, 3], -48.14195, tolerance = 0.01)
})


test_that("vs. sandwich & lmtest", {
  skip_if_not_installed("sandwich")
  skip_if_not_installed("lmtest")

  model <- lm(mpg ~ wt, data = mtcars)
  known <- lmtest::coefci(model, vcov = sandwich::vcovHC)
  unknown <- ci(model, vcov = sandwich::vcovHC)
  expect_equal(unknown[["CI_low"]], known[, "2.5 %"], ignore_attr = TRUE)
  expect_equal(unknown[["CI_high"]], known[, "97.5 %"], ignore_attr = TRUE)

  model <- glm(am ~ wt, data = mtcars, family = binomial)
  known <- lmtest::coefci(model, vcov = sandwich::vcovHC)
  unknown <- ci(model, vcov = sandwich::vcovHC, method = "wald")
  expect_equal(unknown[["CI_low"]], known[, "2.5 %"], ignore_attr = TRUE)
  expect_equal(unknown[["CI_high"]], known[, "97.5 %"], ignore_attr = TRUE)

  suppressMessages(
    expect_message(ci(model, vcov = sandwich::vcovHC), regexp = "vcov.*are not available with.*profile")
  )
})