1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
skip_if_not_installed("bayestestR")
test_that("equivalence_test", {
data(mtcars)
m <- lm(mpg ~ gear + wt + cyl + hp, data = mtcars)
x <- equivalence_test(m)
expect_identical(c(nrow(x), ncol(x)), c(5L, 9L))
expect_type(capture.output(equivalence_test(m)), "character")
expect_snapshot(print(x))
})
test_that("equivalence_test, robust", {
skip_if_not_installed("sandwich")
data(mtcars)
m <- lm(mpg ~ gear + wt + cyl + hp, data = mtcars)
x <- equivalence_test(m, vcov = "HC3")
expect_snapshot(print(x))
})
test_that("equivalence_test, unequal rope-range", {
data(iris)
m <- lm(Sepal.Length ~ Species, data = iris)
rez <- equivalence_test(m, range = c(-Inf, 0.1))
expect_identical(rez$ROPE_Equivalence, c("Rejected", "Rejected", "Rejected"))
expect_identical(rez$ROPE_low, c(-Inf, -Inf, -Inf))
rez <- equivalence_test(m, range = c(-99, 0.1))
expect_identical(rez$ROPE_Equivalence, c("Rejected", "Rejected", "Rejected"))
expect_identical(rez$ROPE_low, c(-99, -99, -99))
data(mtcars)
mtcars[c("gear", "cyl")] <- lapply(mtcars[c("gear", "cyl")], as.factor)
m <- lm(mpg ~ hp + gear + cyl, data = mtcars)
rez <- equivalence_test(m, range = c(-Inf, 0.5))
expect_identical(
rez$ROPE_Equivalence,
c("Rejected", "Accepted", "Undecided", "Rejected", "Accepted", "Undecided")
)
# validate that range of CI equals approximated normal distribution
diff_ci <- abs(diff(c(rez$CI_low[3], rez$CI_high[3])))
set.seed(123)
out <- bayestestR::distribution_normal(
n = 1000,
mean = rez$CI_high[3] - (diff_ci / 2),
sd = (diff_ci / 2) / 3.290525
)
expect_equal(range(out)[1], rez$CI_low[3], tolerance = 1e-4)
expect_equal(range(out)[2], rez$CI_high[3], tolerance = 1e-4)
# need procedure for SGP here...
diff_ci <- abs(diff(c(rez$CI_low[3], rez$CI_high[3])))
z_value <- stats::qnorm((1 + 0.95) / 2)
sd_dist <- diff_ci / diff(c(-1 * z_value, z_value))
set.seed(123)
out <- bayestestR::distribution_normal(
n = 10000,
mean = rez$CI_high[3] - (diff_ci / 2),
sd = sd_dist
)
expect_equal(
rez$SGPV[3],
bayestestR::rope(out, range = c(-Inf, 0.5), ci = 1)$ROPE_Percentage,
tolerance = 1e-4
)
rez <- equivalence_test(m, range = c(-0.5, 0.5))
expect_identical(
rez$ROPE_Equivalence,
c("Rejected", "Accepted", "Undecided", "Rejected", "Rejected", "Undecided")
)
rez <- equivalence_test(m, range = c(-2, 2))
expect_identical(
rez$ROPE_Equivalence,
c("Rejected", "Accepted", "Undecided", "Rejected", "Rejected", "Undecided")
)
})
test_that("equivalence_test, unequal rope-range, plots", {
skip_on_cran()
skip_if_not_installed("see")
skip_if_not_installed("vdiffr")
data(iris)
m <- lm(Sepal.Length ~ Species, data = iris)
rez <- equivalence_test(m, range = c(-Inf, 0.1))
vdiffr::expect_doppelganger(
"Equivalence-Test 1",
plot(rez)
)
rez <- equivalence_test(m, range = c(-99, 0.1))
vdiffr::expect_doppelganger(
"Equivalence-Test 2",
plot(rez)
)
data(mtcars)
mtcars[c("gear", "cyl")] <- lapply(mtcars[c("gear", "cyl")], as.factor)
m <- lm(mpg ~ hp + gear + cyl, data = mtcars)
rez <- equivalence_test(m, range = c(-Inf, 0.5))
vdiffr::expect_doppelganger(
"Equivalence-Test 3",
plot(rez)
)
rez <- equivalence_test(m, range = c(-0.5, 0.5))
vdiffr::expect_doppelganger(
"Equivalence-Test 4",
plot(rez)
)
rez <- equivalence_test(m, range = c(-2, 2))
vdiffr::expect_doppelganger(
"Equivalence-Test 5",
plot(rez)
)
})
|