Skip to content

Commit

Permalink
Tests cleanup (#282)
Browse files Browse the repository at this point in the history
* silence messages in tests, use `skip_if_not_or_load_if_installed`

* missing "MASS" in one of the tests
  • Loading branch information
etiennebacher authored Mar 24, 2023
1 parent 64e2626 commit c413bbe
Show file tree
Hide file tree
Showing 13 changed files with 183 additions and 193 deletions.
16 changes: 5 additions & 11 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
@@ -1,12 +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 all hard dependencies
requiet("bayestestR")
requiet("insight")
requiet("datawizard")
requiet("parameters")
6 changes: 3 additions & 3 deletions tests/testthat/test-as.list.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
requiet("poorman")
requiet("ggplot2")

test_that("as.list", {
skip_if_not_or_load_if_installed("poorman")
skip_if_not_or_load_if_installed("ggplot2")

# no groups
set.seed(123)
expect_snapshot(as.list(correlation(mtcars)))
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-cor_multilevel.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
requiet("lme4")

test_that("comparison rmcorr", {
requiet("rmcorr")
skip_if_not_or_load_if_installed("lme4")
skip_if_not_or_load_if_installed("rmcorr")
set.seed(123)
rez_rmcorr <- rmcorr::rmcorr(Species, Sepal.Length, Sepal.Width, dataset = iris)

Expand All @@ -17,6 +16,7 @@ test_that("comparison rmcorr", {


test_that("Reductio ad absurdum", {
skip_if_not_or_load_if_installed("lme4")
cormatrix <- matrix(
c(
1.0, 0.3, 0.6,
Expand Down
135 changes: 66 additions & 69 deletions tests/testthat/test-cor_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,51 +15,50 @@ test_that("cor_test kendall", {


test_that("cor_test bayesian", {
if (requiet("BayesFactor")) {
out <- cor_test(iris, "Petal.Length", "Petal.Width", bayesian = TRUE)
expect_equal(out$r, 0.9591191, tolerance = 0.01)
skip_if_not_or_load_if_installed("BayesFactor")
out <- cor_test(iris, "Petal.Length", "Petal.Width", bayesian = TRUE)
expect_equal(out$r, 0.9591191, tolerance = 0.01)

set.seed(123)
df_1 <- cor_test(iris, "Petal.Length", "Petal.Width", bayesian = TRUE)

set.seed(123)
df_2 <- cor_test(iris, "Petal.Length", "Petal.Width", method = "auto", bayesian = TRUE)
expect_equal(df_1, df_2, tolerance = 0.001)

out2 <- cor_test(iris, "Petal.Length", "Petal.Width", method = "spearman", bayesian = TRUE)
expect_equal(out2$rho, 0.9323004, tolerance = 0.01)

df <- iris
df$Petal.Length2 <- df$Petal.Length
out3 <- cor_test(df, "Petal.Length", "Petal.Length2", bayesian = TRUE)
expect_equal(out3$rho, 1.000, tolerance = 0.01)

if (getRversion() >= "3.6") {
set.seed(123)
df_1 <- cor_test(iris, "Petal.Length", "Petal.Width", bayesian = TRUE)
out5 <- cor_test(mtcars, "wt", "mpg", method = "shepherd", bayesian = TRUE)
expect_equal(out5$rho, -0.7795719, tolerance = 0.01)

set.seed(123)
df_2 <- cor_test(iris, "Petal.Length", "Petal.Width", method = "auto", bayesian = TRUE)
expect_equal(df_1, df_2, tolerance = 0.001)

out2 <- cor_test(iris, "Petal.Length", "Petal.Width", method = "spearman", bayesian = TRUE)
expect_equal(out2$rho, 0.9323004, tolerance = 0.01)

df <- iris
df$Petal.Length2 <- df$Petal.Length
out3 <- cor_test(df, "Petal.Length", "Petal.Length2", bayesian = TRUE)
expect_equal(out3$rho, 1.000, tolerance = 0.01)

if (getRversion() >= "3.6") {
set.seed(123)
out5 <- cor_test(mtcars, "wt", "mpg", method = "shepherd", bayesian = TRUE)
expect_equal(out5$rho, -0.7795719, tolerance = 0.01)

set.seed(123)
out6 <- cor_test(mtcars, "wt", "mpg", method = "gaussian", bayesian = TRUE)
expect_equal(out6$rho, -0.8294838, tolerance = 0.01)
}

# unsupported
expect_error(cor_test(mtcars, "wt", "mpg", method = "biserial", bayesian = TRUE))
expect_error(cor_test(mtcars, "wt", "mpg", method = "polychoric", bayesian = TRUE))
expect_error(cor_test(mtcars, "wt", "mpg", method = "tetrachoric", bayesian = TRUE))
expect_error(cor_test(mtcars, "wt", "mpg", method = "biweight", bayesian = TRUE))
expect_error(cor_test(mtcars, "wt", "mpg", method = "distance", bayesian = TRUE))
expect_error(cor_test(mtcars, "wt", "mpg", method = "percentage", bayesian = TRUE))
expect_error(cor_test(mtcars, "wt", "mpg", method = "blomqvist", bayesian = TRUE))
expect_error(cor_test(mtcars, "wt", "mpg", method = "hoeffding", bayesian = TRUE))
expect_error(cor_test(mtcars, "wt", "mpg", method = "gamma", bayesian = TRUE))
out6 <- cor_test(mtcars, "wt", "mpg", method = "gaussian", bayesian = TRUE)
expect_equal(out6$rho, -0.8294838, tolerance = 0.01)
}

# unsupported
expect_error(cor_test(mtcars, "wt", "mpg", method = "biserial", bayesian = TRUE))
expect_error(cor_test(mtcars, "wt", "mpg", method = "polychoric", bayesian = TRUE))
expect_error(cor_test(mtcars, "wt", "mpg", method = "tetrachoric", bayesian = TRUE))
expect_error(cor_test(mtcars, "wt", "mpg", method = "biweight", bayesian = TRUE))
expect_error(cor_test(mtcars, "wt", "mpg", method = "distance", bayesian = TRUE))
expect_error(cor_test(mtcars, "wt", "mpg", method = "percentage", bayesian = TRUE))
expect_error(cor_test(mtcars, "wt", "mpg", method = "blomqvist", bayesian = TRUE))
expect_error(cor_test(mtcars, "wt", "mpg", method = "hoeffding", bayesian = TRUE))
expect_error(cor_test(mtcars, "wt", "mpg", method = "gamma", bayesian = TRUE))
})

test_that("cor_test tetrachoric", {
skip_if_not_installed("psych")
skip_if_not_installed("polycor")
skip_if_not_or_load_if_installed("psych")
skip_if_not_or_load_if_installed("polycor")
data <- iris
data$Sepal.Width_binary <- ifelse(data$Sepal.Width > 3, 1, 0)
data$Petal.Width_binary <- ifelse(data$Petal.Width > 1.2, 1, 0)
Expand Down Expand Up @@ -101,22 +100,21 @@ test_that("cor_test robust", {

test_that("cor_test distance", {
skip_if(getRversion() < "4.0")
skip_if_not_or_load_if_installed("energy")

if (requiet("energy")) {
out <- cor_test(iris, "Petal.Length", "Petal.Width", method = "distance")
comparison <- energy::dcorT.test(iris$Petal.Length, iris$Petal.Width)
expect_equal(out$r, as.numeric(comparison$estimate), tolerance = 0.001)
expect_identical(out$Method, "Distance (Bias Corrected)")
}
out <- cor_test(iris, "Petal.Length", "Petal.Width", method = "distance")
comparison <- energy::dcorT.test(iris$Petal.Length, iris$Petal.Width)
expect_equal(out$r, as.numeric(comparison$estimate), tolerance = 0.001)
expect_identical(out$Method, "Distance (Bias Corrected)")
})


test_that("cor_test percentage", {
if (requiet("WRS2")) {
out <- cor_test(iris, "Petal.Length", "Petal.Width", method = "percentage")
comparison <- WRS2::pbcor(iris$Petal.Length, iris$Petal.Width)
expect_equal(out$r, as.numeric(comparison$cor), tolerance = 0.01)
}
skip_if_not_or_load_if_installed("WRS2")

out <- cor_test(iris, "Petal.Length", "Petal.Width", method = "percentage")
comparison <- WRS2::pbcor(iris$Petal.Length, iris$Petal.Width)
expect_equal(out$r, as.numeric(comparison$cor), tolerance = 0.01)
})


Expand All @@ -125,33 +123,31 @@ test_that("cor_test shepherd", {
out <- cor_test(iris, "Petal.Length", "Petal.Width", method = "shepherd")
expect_equal(out$r, 0.94762, tolerance = 0.01)

if (requiet("BayesFactor")) {
set.seed(333)
out2 <- cor_test(iris, "Petal.Length", "Petal.Width", method = "shepherd", bayesian = TRUE)
expect_equal(out2$rho, 0.9429992, tolerance = 0.01)
}
skip_if_not_or_load_if_installed("BayesFactor")
set.seed(333)
out2 <- cor_test(iris, "Petal.Length", "Petal.Width", method = "shepherd", bayesian = TRUE)
expect_equal(out2$rho, 0.9429992, tolerance = 0.01)
})


test_that("cor_test blomqvist", {
if (requiet("wdm")) {
set.seed(333)
out <- cor_test(iris, "Petal.Length", "Petal.Width", method = "blomqvist")
expect_equal(out$r, 0.9066667, tolerance = 0.01)
}
skip_if_not_or_load_if_installed("wdm")

set.seed(333)
out <- cor_test(iris, "Petal.Length", "Petal.Width", method = "blomqvist")
expect_equal(out$r, 0.9066667, tolerance = 0.01)
})

test_that("cor_test hoeffding and somers", {
if (requiet("Hmisc")) {
set.seed(333)
out <- cor_test(iris, "Petal.Length", "Petal.Width", method = "hoeffding")
expect_equal(out$r, 0.5629277, tolerance = 0.01)

set.seed(333)
df <- data.frame(x = 1:6, y = c(0, 0, 1, 0, 1, 1))
out2 <- cor_test(df, "y", "x", method = "somers")
expect_equal(out2$Dxy, 0.7777778, tolerance = 0.01)
}
skip_if_not_or_load_if_installed("Hmisc")
set.seed(333)
out <- cor_test(iris, "Petal.Length", "Petal.Width", method = "hoeffding")
expect_equal(out$r, 0.5629277, tolerance = 0.01)

set.seed(333)
df <- data.frame(x = 1:6, y = c(0, 0, 1, 0, 1, 1))
out2 <- cor_test(df, "y", "x", method = "somers")
expect_equal(out2$Dxy, 0.7777778, tolerance = 0.01)
})

test_that("cor_test gamma", {
Expand All @@ -161,7 +157,8 @@ test_that("cor_test gamma", {
})

test_that("cor_test gaussian", {
requiet("BayesFactor")
skip_if_not_or_load_if_installed("BayesFactor")

set.seed(333)
out <- cor_test(iris, "Petal.Length", "Petal.Width", method = "gaussian")
expect_equal(out$r, 0.87137, tolerance = 0.01)
Expand Down
46 changes: 23 additions & 23 deletions tests/testthat/test-cor_test_na_present.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test_that("cor_test frequentist", {
skip_if_not_installed("ggplot2")
skip_if_not_or_load_if_installed("ggplot2")

expect_error(cor_test(ggplot2::msleep, brainwt, sleep_rem))

Expand All @@ -8,7 +8,7 @@ test_that("cor_test frequentist", {
})

test_that("cor_test kendall", {
skip_if_not_installed("ggplot2")
skip_if_not_or_load_if_installed("ggplot2")

out <- cor_test(ggplot2::msleep, "brainwt", "sleep_rem", method = "kendall")
out2 <- stats::cor.test(ggplot2::msleep$brainwt, ggplot2::msleep$sleep_rem, method = "kendall")
Expand All @@ -18,17 +18,17 @@ test_that("cor_test kendall", {
})

test_that("cor_test bayesian", {
if (requiet("BayesFactor")) {
set.seed(123)
out <- cor_test(ggplot2::msleep, "brainwt", "sleep_rem", bayesian = TRUE)
expect_equal(out$r, -0.1947696, tolerance = 0.01)
}
skip_if_not_or_load_if_installed("BayesFactor")

set.seed(123)
out <- cor_test(ggplot2::msleep, "brainwt", "sleep_rem", bayesian = TRUE)
expect_equal(out$r, -0.1947696, tolerance = 0.01)
})

test_that("cor_test tetrachoric", {
skip_if_not_installed("psych")
skip_if_not_installed("polycor")
skip_if_not_installed("ggplot2")
skip_if_not_or_load_if_installed("psych")
skip_if_not_or_load_if_installed("polycor")
skip_if_not_or_load_if_installed("ggplot2")

data <- ggplot2::msleep
data$brainwt_binary <- ifelse(data$brainwt > 3, 1, 0)
Expand All @@ -51,7 +51,7 @@ test_that("cor_test tetrachoric", {


test_that("cor_test robust", {
skip_if_not_installed("ggplot2")
skip_if_not_or_load_if_installed("ggplot2")

out1 <- cor_test(ggplot2::msleep, "brainwt", "sleep_rem", method = "pearson", ranktransform = TRUE)
out2 <- cor_test(ggplot2::msleep, "brainwt", "sleep_rem", method = "spearman", ranktransform = FALSE)
Expand All @@ -60,9 +60,9 @@ test_that("cor_test robust", {


test_that("cor_test distance", {
skip_if_not_installed("ggplot2")
skip_if_not_installed("energy")
skip_if_not_installed("poorman")
skip_if_not_or_load_if_installed("ggplot2")
skip_if_not_or_load_if_installed("energy")
skip_if_not_or_load_if_installed("poorman")

out <- cor_test(ggplot2::msleep, "brainwt", "sleep_rem", method = "distance")
df <- poorman::filter(ggplot2::msleep, !is.na(brainwt), !is.na(sleep_rem))
Expand All @@ -72,8 +72,8 @@ test_that("cor_test distance", {


test_that("cor_test percentage", {
skip_if_not_installed("ggplot2")
skip_if_not_installed("WRS2")
skip_if_not_or_load_if_installed("ggplot2")
skip_if_not_or_load_if_installed("WRS2")

out <- cor_test(ggplot2::msleep, "brainwt", "sleep_rem", method = "percentage")
comparison <- WRS2::pbcor(ggplot2::msleep$brainwt, ggplot2::msleep$sleep_rem)
Expand All @@ -82,45 +82,45 @@ test_that("cor_test percentage", {


test_that("cor_test shepherd", {
skip_if_not_installed("ggplot2")
skip_if_not_or_load_if_installed("ggplot2")

set.seed(333)
expect_error(cor_test(ggplot2::msleep, "brainwt", "sleep_rem", method = "shepherd"))
})


test_that("cor_test blomqvist", {
skip_if_not_installed("wdm")
skip_if_not_or_load_if_installed("wdm")

set.seed(333)
out <- cor_test(ggplot2::msleep, "brainwt", "sleep_rem", method = "blomqvist")
expect_equal(out$r, -0.4583333, tolerance = 0.01)
})

test_that("cor_test hoeffding", {
skip_if_not_installed("Hmisc")
skip_if_not_or_load_if_installed("Hmisc")

set.seed(333)
out <- cor_test(ggplot2::msleep, "brainwt", "sleep_rem", method = "hoeffding")
expect_equal(out$r, 0.04427718, tolerance = 0.01)
})

test_that("cor_test gamma", {
skip_if_not_installed("ggplot2")
skip_if_not_or_load_if_installed("ggplot2")

set.seed(333)
out <- cor_test(ggplot2::msleep, "brainwt", "sleep_rem", method = "gamma")
expect_equal(out$r, -0.2675799, tolerance = 0.01)
})

test_that("cor_test gaussian", {
skip_if_not_installed("ggplot2")
skip_if_not_or_load_if_installed("ggplot2")

set.seed(333)
out <- cor_test(ggplot2::msleep, "brainwt", "sleep_rem", method = "gaussian")
expect_equal(out$r, -0.3679795, tolerance = 0.01)

skip_if_not_installed("BayesFactor")
skip_if_not_or_load_if_installed("BayesFactor")
out <- cor_test(ggplot2::msleep, "brainwt", "sleep_rem", method = "gaussian", bayesian = TRUE)
expect_equal(out$r, -0.3269572, tolerance = 0.01)
})
Expand All @@ -131,7 +131,7 @@ test_that("cor_test gaussian", {


test_that("cor_test one-sided p value", {
skip_if_not_installed("ggplot2")
skip_if_not_or_load_if_installed("ggplot2")

baseline <- cor.test(ggplot2::msleep$brainwt, ggplot2::msleep$sleep_rem, alternative = "greater")

Expand Down
8 changes: 5 additions & 3 deletions tests/testthat/test-cor_to_pcor.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
requiet("ppcor")
requiet("Hmisc")

test_that("pcor_to_cor", {
skip_if_not_or_load_if_installed("ppcor")
skip_if_not_or_load_if_installed("Hmisc")

set.seed(333)

# easycormatrix
Expand Down Expand Up @@ -35,6 +35,8 @@ test_that("pcor_to_cor", {


test_that("spcor_to_cor", {
skip_if_not_or_load_if_installed("ppcor")

set.seed(333)

# easycormatrix
Expand Down
Loading

0 comments on commit c413bbe

Please sign in to comment.