Skip to content

Commit

Permalink
safer tests because it fails on some distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
DominiqueMakowski committed Sep 26, 2020
1 parent 9ebe2e6 commit ab44523
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 40 deletions.
36 changes: 24 additions & 12 deletions tests/testthat/test-cor_test.R
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
context("cor_test")



test_that("cor_test frequentist", {
if (requireNamespace("psych")) {
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)

testthat::expect_error(cor_test(iris, Petal.Length, Petal.Width))

testthat::expect_error(cor_test(data, Petal.Length, Petal.Width))
out <- cor_test(iris, "Petal.Length", "Petal.Width")
testthat::expect_equal(out$r, 0.962, tol = 0.01)

})

out <- cor_test(data, "Petal.Length", "Petal.Width")
testthat::expect_equal(out$r, 0.962, tol = 0.01)

out <- cor_test(data, "Petal.Length", "Petal.Width", bayesian = TRUE)
testthat::expect_equal(out$r, 0.962, tol = 0.01)
test_that("cor_test bayesian", {
if (requireNamespace("BayesFactor")) {

out <- cor_test(iris, "Petal.Length", "Petal.Width", bayesian = TRUE)
testthat::expect_equal(out$r, 0.962, tol = 0.01)

}
})

test_that("cor_test tetrachoric", {
if (requireNamespace("psych")) {
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)

# With Factors / Binary
out <- cor_test(data, "Sepal.Width_binary", "Petal.Width_binary", method = "tetrachoric")
Expand Down Expand Up @@ -85,9 +93,11 @@ test_that("cor_test blomqvist", {
})

test_that("cor_test hoeffding", {
if (requireNamespace("Hmisc")) {
set.seed(333)
out <- cor_test(iris, "Petal.Length", "Petal.Width", method = "hoeffding")
testthat::expect_equal(out$r, as.numeric(0.5629277), tol = 0.01)
}
})

test_that("cor_test gamma", {
Expand All @@ -101,8 +111,10 @@ test_that("cor_test gaussian", {
out <- cor_test(iris, "Petal.Length", "Petal.Width", method = "gaussian")
testthat::expect_equal(out$r, as.numeric(0.87137), tol = 0.01)

out <- cor_test(iris, "Petal.Length", "Petal.Width", method = "gaussian", bayesian = TRUE)
testthat::expect_equal(out$r, as.numeric(0.8620878), tol = 0.01)
if (requireNamespace("BayesFactor")) {
out <- cor_test(iris, "Petal.Length", "Petal.Width", method = "gaussian", bayesian = TRUE)
testthat::expect_equal(out$r, as.numeric(0.8620878), tol = 0.01)
}
})


Expand Down
60 changes: 32 additions & 28 deletions tests/testthat/test-correlation.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,40 +68,42 @@ test_that("comparison with other packages", {


# Bayesian
out <- correlation(iris, include_factors = FALSE, bayesian = TRUE)
rez <- as.data.frame(as.table(out))
if (requireNamespace("BayesFactor")) {
out <- correlation(iris, include_factors = FALSE, bayesian = TRUE)
rez <- as.data.frame(as.table(out))

r <- as.matrix(rez[2:5])
testthat::expect_equal(mean(r - cor(iris[1:4])), 0, tol = 0.01)
r <- as.matrix(rez[2:5])
testthat::expect_equal(mean(r - cor(iris[1:4])), 0, tol = 0.01)

hmisc <- Hmisc::rcorr(as.matrix(iris[1:4]), type = c("pearson"))
testthat::expect_equal(mean(r - hmisc$r), 0, tol = 0.01)
hmisc <- Hmisc::rcorr(as.matrix(iris[1:4]), type = c("pearson"))
testthat::expect_equal(mean(r - hmisc$r), 0, tol = 0.01)

pd <- as.matrix(attributes(rez)$pd[2:5])
p <- bayestestR::pd_to_p(pd)
testthat::expect_equal(mean(p - hmisc$P, na.rm = TRUE), 0, tol = 0.01)
pd <- as.matrix(attributes(rez)$pd[2:5])
p <- bayestestR::pd_to_p(pd)
testthat::expect_equal(mean(p - hmisc$P, na.rm = TRUE), 0, tol = 0.01)


# Bayesian - Partial
out <- correlation(iris, include_factors = FALSE, bayesian = TRUE, partial = TRUE)
rez <- as.data.frame(as.table(out))
# Bayesian - Partial
out <- correlation(iris, include_factors = FALSE, bayesian = TRUE, partial = TRUE)
rez <- as.data.frame(as.table(out))

r <- as.matrix(rez[2:5])
ppcor <- ppcor::pcor(iris[1:4])
testthat::expect_equal(max(r - as.matrix(ppcor$estimate)), 0, tol = 0.02)
r <- as.matrix(rez[2:5])
ppcor <- ppcor::pcor(iris[1:4])
testthat::expect_equal(max(r - as.matrix(ppcor$estimate)), 0, tol = 0.02)

pd <- as.matrix(attributes(rez)$pd[2:ncol(rez)])
p <- bayestestR::pd_to_p(pd)
testthat::expect_equal(mean(abs(p - as.matrix(ppcor$p.value))), 0, tol = 0.001)
pd <- as.matrix(attributes(rez)$pd[2:ncol(rez)])
p <- bayestestR::pd_to_p(pd)
testthat::expect_equal(mean(abs(p - as.matrix(ppcor$p.value))), 0, tol = 0.001)


# Bayesian (Full) - Partial
out <- correlation(iris, include_factors = FALSE, bayesian = TRUE, partial = TRUE, partial_bayesian = TRUE)
rez <- as.data.frame(as.table(out))
# Bayesian (Full) - Partial
out <- correlation(iris, include_factors = FALSE, bayesian = TRUE, partial = TRUE, partial_bayesian = TRUE)
rez <- as.data.frame(as.table(out))

r <- as.matrix(rez[2:5])
ppcor <- ppcor::pcor(iris[1:4])
testthat::expect_equal(max(r - as.matrix(ppcor$estimate)), 0, tol = 0.02)
r <- as.matrix(rez[2:5])
ppcor <- ppcor::pcor(iris[1:4])
testthat::expect_equal(max(r - as.matrix(ppcor$estimate)), 0, tol = 0.02)
}
}
})

Expand Down Expand Up @@ -140,10 +142,12 @@ test_that("format checks", {
}

# Bayesian full partial
out <- correlation(iris, include_factors = TRUE, multilevel = TRUE, bayesian = TRUE, partial = TRUE, partial_bayesian = TRUE)
testthat::expect_equal(c(nrow(out), ncol(out)), c(6, 13))
testthat::expect_equal(c(nrow(as.table(out)), ncol(as.table(out))), c(4, 5))
testthat::expect_equal(c(nrow(summary(out)), ncol(summary(out))), c(3, 4))
if (requireNamespace("BayesFactor")) {
out <- correlation(iris, include_factors = TRUE, multilevel = TRUE, bayesian = TRUE, partial = TRUE, partial_bayesian = TRUE)
testthat::expect_equal(c(nrow(out), ncol(out)), c(6, 13))
testthat::expect_equal(c(nrow(as.table(out)), ncol(as.table(out))), c(4, 5))
testthat::expect_equal(c(nrow(summary(out)), ncol(summary(out))), c(3, 4))
}
})


Expand Down

0 comments on commit ab44523

Please sign in to comment.