Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • 130374b
  • /
  • tests
  • /
  • testthat
  • /
  • test-bayesfactor_restricted.R
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge
swh:1:cnt:f885cc94bb758ddba2d84d50ca5d0c283dc3d5eb
directory badge
swh:1:dir:d4189109de18f8c2d2003bccb4a329a40247ea94

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
test-bayesfactor_restricted.R
if (require("rstanarm") &&
  require("BayesFactor") &&
  require("testthat")) {

  # bayesfactor_restricted data.frame ---------------------------------------

  test_that("bayesfactor_restricted df", {
    prior <- data.frame(
      X = distribution_normal(100),
      X1 = c(distribution_normal(50), distribution_normal(50)),
      X3 = c(distribution_normal(80), distribution_normal(20))
    )

    posterior <- data.frame(
      X = distribution_normal(100, .4, .2),
      X1 = distribution_normal(100, -.2, .2),
      X3 = distribution_normal(100, .2)
    )

    hyps <- c(
      "X > X1 & X1 > X3",
      "X > X1"
    )

    bfr <- bayesfactor_restricted(posterior, hypothesis = hyps, prior = prior)

    testthat::expect_equal(bfr$Prior_prob, c(0.2, 0.5), tolerance = 0.1)
    testthat::expect_equal(bfr$Posterior_prob, c(0.31, 1), tolerance = 0.1)
    testthat::expect_equal(log(bfr$BF), c(0.43, 0.69), tolerance = 0.1)
    testthat::expect_equal(bfr$BF, bfr$Posterior_prob / bfr$Prior_prob, tolerance = 0.1)

    testthat::expect_error(bayesfactor_restricted(posterior, prior, hypothesis = "Y < 0"))
  })


  # bayesfactor_restricted RSTANARM -----------------------------------------


  test_that("bayesfactor_restricted RSTANARM", {
    testthat::skip_on_cran()
    library(rstanarm)
    fit_stan <- stan_glm(mpg ~ wt + cyl + am, data = mtcars, refresh = 0)

    hyps <- c(
      "am > 0 & cyl < 0",
      "cyl < 0",
      "wt - cyl > 0"
    )

    set.seed(444)
    fit_p <- unupdate(fit_stan)
    bfr1 <- bayesfactor_restricted(fit_stan, prior = fit_p, hypothesis = hyps)

    set.seed(444)
    bfr2 <- bayesfactor_restricted(fit_stan, hypothesis = hyps)

    testthat::expect_equal(bfr1, bfr2)
  })
}

back to top

Software Heritage — Copyright (C) 2015–2026, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API