Skip to content

Commit

Permalink
Tests cleanup (#596)
Browse files Browse the repository at this point in the history
* start silencing messages

* start using `skip_if_not_or_load_if_installed`

* finish `skip_if_not_or_load_if_installed`

* fix failures

* hide more messages [skip ci]

* remove remaining `requiet()` and style

* back to skipping some tests for check_prior

---------

Co-authored-by: Indrajeet Patil <[email protected]>
  • Loading branch information
etiennebacher and IndrajeetPatil authored Mar 27, 2023
1 parent a00f5a0 commit db2548d
Show file tree
Hide file tree
Showing 27 changed files with 2,416 additions and 2,164 deletions.
14 changes: 5 additions & 9 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
requiet <- function(package) {
testthat::skip_if_not_installed(package)
suppressPackageStartupMessages(
require(package, warn.conflicts = FALSE, character.only = TRUE)
)
skip_if_not_or_load_if_installed <- function(package, minimum_version = NULL) {
testthat::skip_if_not_installed(package, minimum_version = minimum_version)
suppressMessages(suppressWarnings(suppressPackageStartupMessages(
require(package, warn.conflicts = FALSE, character.only = TRUE, quietly = TRUE)
)))
}

# load hard dependencies
library(insight)
library(datawizard)
135 changes: 67 additions & 68 deletions tests/testthat/test-BFBayesFactor.R
Original file line number Diff line number Diff line change
@@ -1,86 +1,85 @@
if (requiet("BayesFactor")) {
test_that("p_direction", {
skip_if_not_or_load_if_installed("BayesFactor")
set.seed(333)
x <- BayesFactor::correlationBF(y = iris$Sepal.Length, x = iris$Sepal.Width)
test_that("p_direction", {
expect_equal(as.numeric(p_direction(x)), 0.9225, tolerance = 1)
})
x <- correlationBF(y = iris$Sepal.Length, x = iris$Sepal.Width)
expect_equal(as.numeric(p_direction(x)), 0.9225, tolerance = 1)
})


# BF t.test one sample ---------------------------
test_that("p_direction: BF t.test one sample", {
skip_if_not_or_load_if_installed("BayesFactor")
data(sleep)
diffScores <- sleep$extra[1:10] - sleep$extra[11:20]
x <- BayesFactor::ttestBF(x = diffScores)
test_that("p_direction", {
expect_equal(as.numeric(p_direction(x)), 0.99675, tolerance = 1)
})
x <- ttestBF(x = diffScores)
expect_equal(as.numeric(p_direction(x)), 0.99675, tolerance = 1)
})


# BF t.test two samples ---------------------------
test_that("p_direction: BF t.test two samples", {
skip_if_not_or_load_if_installed("BayesFactor")
data(chickwts)
chickwts <- chickwts[chickwts$feed %in% c("horsebean", "linseed"), ]
chickwts$feed <- factor(chickwts$feed)
x <- BayesFactor::ttestBF(formula = weight ~ feed, data = chickwts)
test_that("p_direction", {
expect_equal(as.numeric(p_direction(x)), 1, tolerance = 1)
})
x <- ttestBF(formula = weight ~ feed, data = chickwts)
expect_equal(as.numeric(p_direction(x)), 1, tolerance = 1)
})

# BF t.test meta-analytic ---------------------------
test_that("p_direction: BF t.test meta-analytic", {
skip_if_not_or_load_if_installed("BayesFactor")
t <- c(-.15, 2.39, 2.42, 2.43)
N <- c(100, 150, 97, 99)
x <- BayesFactor::meta.ttestBF(t = t, n1 = N, rscale = 1)
test_that("p_direction", {
expect_equal(as.numeric(p_direction(x)), 0.99975, tolerance = 1)
})
x <- meta.ttestBF(t = t, n1 = N, rscale = 1)
expect_equal(as.numeric(p_direction(x)), 0.99975, tolerance = 1)
})

# # ---------------------------
# # "BF ANOVA"
# data(ToothGrowth)
# ToothGrowth$dose <- factor(ToothGrowth$dose)
# levels(ToothGrowth$dose) <- c("Low", "Medium", "High")
# x <- BayesFactor::anovaBF(len ~ supp*dose, data=ToothGrowth)
# test_that("p_direction", {
# expect_equal(as.numeric(p_direction(x)), 91.9, tol=0.1)
# })
#
# # ---------------------------
# # "BF ANOVA Random"
# data(puzzles)
# x <- BayesFactor::anovaBF(RT ~ shape*color + ID, data = puzzles, whichRandom="ID")
# test_that("p_direction", {
# expect_equal(as.numeric(p_direction(x)), 91.9, tol=0.1)
# })
#
#
# # ---------------------------
# # "BF lm"
# x <- BayesFactor::lmBF(len ~ supp + dose, data = ToothGrowth)
# test_that("p_direction", {
# expect_equal(as.numeric(p_direction(x)), 91.9, tol=0.1)
# })
#
#
# x2 <- BayesFactor::lmBF(len ~ supp + dose + supp:dose, data = ToothGrowth)
# x <- x / x2
# test_that("p_direction", {
# expect_equal(as.numeric(p_direction(x)), 91.9, tol=0.1)
# })
# # ---------------------------
# # "BF ANOVA"
# data(ToothGrowth)
# ToothGrowth$dose <- factor(ToothGrowth$dose)
# levels(ToothGrowth$dose) <- c("Low", "Medium", "High")
# x <- BayesFactor::anovaBF(len ~ supp*dose, data=ToothGrowth)
# test_that("p_direction", {
# expect_equal(as.numeric(p_direction(x)), 91.9, tol=0.1)
# })
#
# # ---------------------------
# # "BF ANOVA Random"
# data(puzzles)
# x <- BayesFactor::anovaBF(RT ~ shape*color + ID, data = puzzles, whichRandom="ID")
# test_that("p_direction", {
# expect_equal(as.numeric(p_direction(x)), 91.9, tol=0.1)
# })
#
#
# # ---------------------------
# # "BF lm"
# x <- BayesFactor::lmBF(len ~ supp + dose, data = ToothGrowth)
# test_that("p_direction", {
# expect_equal(as.numeric(p_direction(x)), 91.9, tol=0.1)
# })
#
#
# x2 <- BayesFactor::lmBF(len ~ supp + dose + supp:dose, data = ToothGrowth)
# x <- x / x2
# test_that("p_direction", {
# expect_equal(as.numeric(p_direction(x)), 91.9, tol=0.1)
# })


test_that("rope_range", {
x <- BayesFactor::lmBF(len ~ supp + dose, data = ToothGrowth)
expect_equal(rope_range(x)[2], sd(ToothGrowth$len) / 10)
test_that("rope_range", {
skip_if_not_or_load_if_installed("BayesFactor")
x <- lmBF(len ~ supp + dose, data = ToothGrowth)
expect_equal(rope_range(x)[2], sd(ToothGrowth$len) / 10)

x <- BayesFactor::ttestBF(
ToothGrowth$len[ToothGrowth$supp == "OJ"],
ToothGrowth$len[ToothGrowth$supp == "VC"]
)
expect_equal(rope_range(x)[2], sd(ToothGrowth$len) / 10)
x <- ttestBF(
ToothGrowth$len[ToothGrowth$supp == "OJ"],
ToothGrowth$len[ToothGrowth$supp == "VC"]
)
expect_equal(rope_range(x)[2], sd(ToothGrowth$len) / 10)

x <- BayesFactor::ttestBF(formula = len ~ supp, data = ToothGrowth)
expect_equal(rope_range(x)[2], sd(ToothGrowth$len) / 10)
x <- ttestBF(formula = len ~ supp, data = ToothGrowth)
expect_equal(rope_range(x)[2], sd(ToothGrowth$len) / 10)

# else
x <- BayesFactor::correlationBF(ToothGrowth$len, ToothGrowth$dose)
expect_equal(rope_range(x, verbose = FALSE), c(-0.05, 0.05))
})
}
# else
x <- correlationBF(ToothGrowth$len, ToothGrowth$dose)
expect_equal(rope_range(x, verbose = FALSE), c(-0.05, 0.05))
})
Loading

0 comments on commit db2548d

Please sign in to comment.