File: test-glmer.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 (82 lines) | stat: -rw-r--r-- 2,609 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
skip_on_cran()
skip_if_not_installed("lme4")
data("cbpp", package = "lme4")

set.seed(123)
model <- lme4::glmer(
  cbind(incidence, size - incidence) ~ period + (1 | herd),
  data = cbpp,
  family = binomial(),
  nAGQ = 0
)
params <- model_parameters(model, effects = "fixed")

test_that("model_parameters.glmer", {
  expect_equal(params$SE, c(0.22758, 0.30329, 0.32351, 0.42445), tolerance = 1e-2)
})

test_that("print model_parameters", {
  skip_if_not_installed("withr")
  skip_if_not_installed("merDeriv")
  withr::local_options(
    list(
      parameters_exponentiate = TRUE,
      parameters_warning_exponentiate = TRUE
    )
  )
  expect_snapshot(params)

  suppressMessages({
    mp <- model_parameters(model, effects = "all", exponentiate = TRUE)
  })
  expect_snapshot(mp)

  set.seed(123)
  model <- lme4::glmer(
    cbind(incidence, size - incidence) ~ period + (1 | herd),
    data = cbpp,
    family = binomial(),
    nAGQ = 2
  )
  mp <- model_parameters(model, effects = "all")
  expect_snapshot(mp)
})


test_that("model_parameters.glmer ml1", {
  params <- model_parameters(model, ci_method = "ml1", effects = "fixed")
  expect_equal(params$SE, c(0.22758, 0.30329, 0.32351, 0.42445), tolerance = 1e-2)
  expect_equal(params$df, c(54, 54, 54, 54), tolerance = 1e-2)
})

test_that("model_parameters.glmer betwithin", {
  params <- model_parameters(model, ci_method = "betwithin", effects = "fixed")
  expect_equal(params$SE, c(0.23009, 0.30433, 0.32476, 0.42632), tolerance = 1e-2)
  expect_equal(params$df, c(822, 822, 822, 822), tolerance = 1e-2)
})

set.seed(123)
cbpp$time <- runif(nrow(cbpp), 1, 4)
model <- lme4::glmer(
  cbind(incidence, size - incidence) ~ period + time + (1 + time | herd),
  data = cbpp,
  family = binomial(),
  nAGQ = 0
)

test_that("model_parameters.glmer", {
  params <- model_parameters(model, effects = "fixed")
  expect_equal(params$SE, c(0.66539, 0.36178, 0.36223, 0.45528, 0.2379), tolerance = 1e-2)
})

test_that("model_parameters.glmer ml1", {
  params <- model_parameters(model, ci_method = "ml1", effects = "fixed")
  expect_equal(params$SE, c(0.66539, 0.36178, 0.36223, 0.45528, 0.2379), tolerance = 1e-2)
  expect_equal(params$df, c(53, 53, 53, 53, 53), tolerance = 1e-2)
})

test_that("model_parameters.glmer betwithin", {
  params <- model_parameters(model, ci_method = "betwithin", effects = "fixed")
  expect_equal(params$SE, c(0.66539, 0.36178, 0.36223, 0.45528, 0.2379), tolerance = 1e-2)
  expect_equal(params$df, c(821, 821, 821, 821, 9), tolerance = 1e-2)
})