swh:1:snp:2c68a6c5a8af2f06ac2c0225927f25b54fd1f9d0
Raw File
Tip revision: 6313ce21ea98857cf95d996de7978cfd52175e59 authored by Dominique Makowski on 08 April 2021, 04:40:02 UTC
version 0.9.0
Tip revision: 6313ce2
test-hdi.R
if (require("rstanarm") && require("brms") && require("httr") && require("testthat") && require("BayesFactor")) {

  # numeric -------------------------------
  test_that("hdi", {
    expect_equal(hdi(distribution_normal(1000), ci = .90)$CI_low[1], -1.64, tolerance = 0.02)
    expect_equal(nrow(hdi(distribution_normal(1000), ci = c(.80, .90, .95))), 3, tolerance = 0.01)
    expect_equal(hdi(distribution_normal(1000), ci = 1)$CI_low[1], -3.09, tolerance = 0.02)
    expect_equal(nchar(capture.output(print(hdi(distribution_normal(1000))))), 22)
    expect_equal(length(capture.output(print(hdi(distribution_normal(1000), ci = c(.80, .90))))), 5)


    expect_warning(hdi(c(2, 3, NA)))
    expect_warning(hdi(c(2, 3)))
    expect_warning(hdi(distribution_normal(1000), ci = 0.0000001))
    expect_warning(hdi(distribution_normal(1000), ci = 950))
    expect_warning(hdi(c(distribution_normal(1000, 0, 1), distribution_normal(1000, 6, 1), distribution_normal(1000, 12, 1)), ci = .10))
  })



  .runThisTest <- Sys.getenv("RunAllbayestestRTests") == "yes"
  if (.runThisTest) {
    if (require("insight")) {
      # stanreg ---------------------------
      m <- insight::download_model("stanreg_merMod_5")
      p <- insight::get_parameters(m, effects = "all")

      test_that("ci", {
        expect_equal(
          hdi(m, ci = c(.5, .8), effects = "all")$CI_low,
          hdi(p, ci = c(.5, .8))$CI_low,
          tolerance = 1e-3
        )
      })

      # brms ---------------------------
      m <- insight::download_model("brms_zi_3")
      p <- insight::get_parameters(m, effects = "all", component = "all")

      test_that("rope", {
        expect_equal(
          hdi(m, ci = c(.5, .8), effects = "all", component = "all")$CI_low,
          hdi(p, ci = c(.5, .8))$CI_low,
          tolerance = 1e-3
        )
      })

      # BayesFactor ---------------------------
      mod_bf <- proportionBF(y = 15, N = 25, p = .5)
      p_bf <- insight::get_parameters(mod_bf)

      test_that("ci - BayesFactor", {
        expect_equal(
          hdi(mod_bf, ci = c(.5, .8), effects = "all", component = "all")$CI_low,
          hdi(p_bf, ci = c(.5, .8))$CI_low,
          tolerance = 0.1
        )
      })
    }
  }
}
back to top