Skip to content

Commit ab44523

Browse files
safer tests because it fails on some distribution
1 parent 9ebe2e6 commit ab44523

File tree

2 files changed

+56
-40
lines changed

2 files changed

+56
-40
lines changed

tests/testthat/test-cor_test.R

+24-12
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
11
context("cor_test")
22

33

4-
54
test_that("cor_test frequentist", {
6-
if (requireNamespace("psych")) {
7-
data <- iris
8-
data$Sepal.Width_binary <- ifelse(data$Sepal.Width > 3, 1, 0)
9-
data$Petal.Width_binary <- ifelse(data$Petal.Width > 1.2, 1, 0)
105

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

12-
testthat::expect_error(cor_test(data, Petal.Length, Petal.Width))
8+
out <- cor_test(iris, "Petal.Length", "Petal.Width")
9+
testthat::expect_equal(out$r, 0.962, tol = 0.01)
10+
11+
})
1312

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

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

17+
out <- cor_test(iris, "Petal.Length", "Petal.Width", bayesian = TRUE)
18+
testthat::expect_equal(out$r, 0.962, tol = 0.01)
19+
20+
}
21+
})
22+
23+
test_that("cor_test tetrachoric", {
24+
if (requireNamespace("psych")) {
25+
data <- iris
26+
data$Sepal.Width_binary <- ifelse(data$Sepal.Width > 3, 1, 0)
27+
data$Petal.Width_binary <- ifelse(data$Petal.Width > 1.2, 1, 0)
2028

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

8795
test_that("cor_test hoeffding", {
96+
if (requireNamespace("Hmisc")) {
8897
set.seed(333)
8998
out <- cor_test(iris, "Petal.Length", "Petal.Width", method = "hoeffding")
9099
testthat::expect_equal(out$r, as.numeric(0.5629277), tol = 0.01)
100+
}
91101
})
92102

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

104-
out <- cor_test(iris, "Petal.Length", "Petal.Width", method = "gaussian", bayesian = TRUE)
105-
testthat::expect_equal(out$r, as.numeric(0.8620878), tol = 0.01)
114+
if (requireNamespace("BayesFactor")) {
115+
out <- cor_test(iris, "Petal.Length", "Petal.Width", method = "gaussian", bayesian = TRUE)
116+
testthat::expect_equal(out$r, as.numeric(0.8620878), tol = 0.01)
117+
}
106118
})
107119

108120

tests/testthat/test-correlation.R

+32-28
Original file line numberDiff line numberDiff line change
@@ -68,40 +68,42 @@ test_that("comparison with other packages", {
6868

6969

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

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

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

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

8485

85-
# Bayesian - Partial
86-
out <- correlation(iris, include_factors = FALSE, bayesian = TRUE, partial = TRUE)
87-
rez <- as.data.frame(as.table(out))
86+
# Bayesian - Partial
87+
out <- correlation(iris, include_factors = FALSE, bayesian = TRUE, partial = TRUE)
88+
rez <- as.data.frame(as.table(out))
8889

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

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

9798

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

102-
r <- as.matrix(rez[2:5])
103-
ppcor <- ppcor::pcor(iris[1:4])
104-
testthat::expect_equal(max(r - as.matrix(ppcor$estimate)), 0, tol = 0.02)
103+
r <- as.matrix(rez[2:5])
104+
ppcor <- ppcor::pcor(iris[1:4])
105+
testthat::expect_equal(max(r - as.matrix(ppcor$estimate)), 0, tol = 0.02)
106+
}
105107
}
106108
})
107109

@@ -140,10 +142,12 @@ test_that("format checks", {
140142
}
141143

142144
# Bayesian full partial
143-
out <- correlation(iris, include_factors = TRUE, multilevel = TRUE, bayesian = TRUE, partial = TRUE, partial_bayesian = TRUE)
144-
testthat::expect_equal(c(nrow(out), ncol(out)), c(6, 13))
145-
testthat::expect_equal(c(nrow(as.table(out)), ncol(as.table(out))), c(4, 5))
146-
testthat::expect_equal(c(nrow(summary(out)), ncol(summary(out))), c(3, 4))
145+
if (requireNamespace("BayesFactor")) {
146+
out <- correlation(iris, include_factors = TRUE, multilevel = TRUE, bayesian = TRUE, partial = TRUE, partial_bayesian = TRUE)
147+
testthat::expect_equal(c(nrow(out), ncol(out)), c(6, 13))
148+
testthat::expect_equal(c(nrow(as.table(out)), ncol(as.table(out))), c(4, 5))
149+
testthat::expect_equal(c(nrow(summary(out)), ncol(summary(out))), c(3, 4))
150+
}
147151
})
148152

149153

0 commit comments

Comments
 (0)