File: test-rstanarm.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 (50 lines) | stat: -rw-r--r-- 1,369 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
skip_on_os("mac")

skip_on_cran()

test_that("mp", {
  skip_if_not_installed("rstanarm")
  set.seed(123)
  model <- rstanarm::stan_glm(
    vs ~ mpg + cyl,
    data = mtcars,
    refresh = 0,
    family = "binomial",
    seed = 123
  )

  mp <- model_parameters(model, centrality = "mean")
  s <- summary(model)
  expect_equal(mp$Mean, unname(s[1:3, 1]), tolerance = 1e-2, ignore_attr = TRUE)
  expect_equal(mp$Prior_Scale, c(2.5, 0.4148, 1.39984), tolerance = 1e-2)
})


test_that("mp2", {
  skip_if_not_installed("rstanarm")
  data(pbcLong, package = "rstanarm")
  pbcLong$ybern <- as.integer(pbcLong$logBili >= mean(pbcLong$logBili))
  set.seed(123)
  invisible(capture.output({
    model <- rstanarm::stan_mvmer(
      formula = list(
        ybern ~ year + (1 | id),
        albumin ~ sex + year + (year | id)
      ),
      data = pbcLong,
      refresh = 0,
      seed = 123
    )
  }))

  mp <- suppressWarnings(model_parameters(model, centrality = "mean"))
  s <- summary(model)
  expect_equal(
    mp$Mean,
    unname(s[c("y1|(Intercept)", "y1|year", "y2|(Intercept)", "y2|sexf", "y2|year"), 1]),
    tolerance = 1e-2,
    ignore_attr = TRUE
  )
  expect_identical(mp$Response, c("y1", "y1", "y2", "y2", "y2"))
  expect_equal(mp$Prior_Scale, c(4.9647, 0.3465, 5.57448, 1.39362, 0.38906), tolerance = 1e-2)
})