| 1 |
skip_if_not_installed("modeltests") |
| 2 |
library(modeltests) |
| 3 |
|
| 4 |
skip_if_not_installed("modeldata") |
| 5 |
library(modeldata) |
| 6 |
data(hpc_data) |
| 7 |
|
| 8 |
test_that("htest tidier arguments", { |
| 9 |
check_arguments(tidy.htest) |
| 10 |
check_arguments(glance.htest) |
| 11 |
}) |
| 12 |
|
| 13 |
test_that("tidy.htest same as glance.htest", { |
| 14 |
tt <- t.test(rnorm(10)) |
| 15 |
expect_identical(tidy(tt), glance(tt)) |
| 16 |
}) |
| 17 |
|
| 18 |
test_that("tidy.htest/oneway.test", { |
| 19 |
mtcars$cyl <- as.factor(mtcars$cyl) |
| 20 |
ot <- oneway.test(mpg ~ cyl, mtcars) |
| 21 |
expect_snapshot(td <- tidy(ot)) |
| 22 |
expect_snapshot(gl <- glance(ot)) |
| 23 |
|
| 24 |
check_tidy_output(td) |
| 25 |
check_dims(td, expected_cols = 5) |
| 26 |
check_glance_outputs(gl) |
| 27 |
}) |
| 28 |
|
| 29 |
test_that("tidy.htest/cor.test", { |
| 30 |
pco <- cor.test(mtcars$mpg, mtcars$wt) |
| 31 |
td <- tidy(pco) |
| 32 |
gl <- glance(pco) |
| 33 |
|
| 34 |
check_tidy_output(td) |
| 35 |
check_glance_outputs(gl, strict = FALSE) |
| 36 |
|
| 37 |
sco <- suppressWarnings(cor.test(mtcars$mpg, mtcars$wt, method = "spearman")) |
| 38 |
td2 <- tidy(sco) |
| 39 |
gl2 <- glance(sco) |
| 40 |
|
| 41 |
check_tidy_output(td2) |
| 42 |
check_glance_outputs(gl2, strict = FALSE) |
| 43 |
}) |
| 44 |
|
| 45 |
test_that("tidy.htest/t.test", { |
| 46 |
tt <- t.test(mpg ~ am, mtcars) |
| 47 |
td <- tidy(tt) |
| 48 |
gl <- glance(tt) |
| 49 |
|
| 50 |
check_tidy_output(td) |
| 51 |
check_glance_outputs(gl, strict = FALSE) |
| 52 |
}) |
| 53 |
|
| 54 |
test_that("tidy.htest/wilcox.test", { |
| 55 |
wt <- suppressWarnings(wilcox.test(mpg ~ am, mtcars)) |
| 56 |
td <- tidy(wt) |
| 57 |
gl <- glance(wt) |
| 58 |
|
| 59 |
check_tidy_output(td) |
| 60 |
check_glance_outputs(gl) |
| 61 |
}) |
| 62 |
|
| 63 |
test_that("tidy.pairwise.htest", { |
| 64 |
pht <- with(hpc_data, pairwise.t.test(compounds, class)) |
| 65 |
td <- tidy(pht) |
| 66 |
# gl <- glance(pht) |
| 67 |
|
| 68 |
check_arguments(tidy.pairwise.htest) |
| 69 |
check_tidy_output(td) |
| 70 |
# check_glance_outputs(gl). doesn't exist yet |
| 71 |
}) |
| 72 |
|
| 73 |
test_that("tidy.power.htest", { |
| 74 |
ptt <- power.t.test(n = 2:30, delta = 1) |
| 75 |
td <- tidy(ptt) |
| 76 |
# gl <- glance(ptt) |
| 77 |
|
| 78 |
check_arguments(tidy.power.htest) |
| 79 |
check_tidy_output(td, strict = FALSE) |
| 80 |
# check_glance_outputs(gl). doesn't exist yet. |
| 81 |
}) |
| 82 |
|
| 83 |
|
| 84 |
test_that("augment.htest (chi squared test)", { |
| 85 |
# doesn't have a data argument |
| 86 |
check_arguments(augment.htest, strict = FALSE) |
| 87 |
|
| 88 |
df <- as.data.frame(Titanic) |
| 89 |
tab <- xtabs(Freq ~ Sex + Class, data = df) |
| 90 |
|
| 91 |
chit <- chisq.test(tab) # 2D table |
| 92 |
au <- augment(chit) |
| 93 |
check_tibble(au, method = "augment", strict = FALSE) |
| 94 |
|
| 95 |
chit2 <- chisq.test(c(A = 20, B = 15, C = 25)) # 1D table |
| 96 |
au2 <- augment(chit2) |
| 97 |
check_tibble(au2, method = "augment", strict = FALSE) |
| 98 |
|
| 99 |
tt <- t.test(rnorm(10)) |
| 100 |
expect_snapshot(error = TRUE, augment(tt)) |
| 101 |
|
| 102 |
wt <- wilcox.test(mpg ~ am, data = mtcars, conf.int = TRUE, exact = FALSE) |
| 103 |
expect_snapshot(error = TRUE, augment(wt)) |
| 104 |
|
| 105 |
ct <- cor.test(mtcars$wt, mtcars$mpg) |
| 106 |
expect_snapshot(error = TRUE, augment(ct)) |
| 107 |
}) |
| 108 |
|
| 109 |
test_that("tidy.htest does not return matrix columns", { |
| 110 |
skip_if_not_installed("survey") |
| 111 |
data(api, package = "survey") |
| 112 |
dclus1 <- survey::svydesign( |
| 113 |
id = ~dnum, |
| 114 |
weights = ~pw, |
| 115 |
data = apiclus1, |
| 116 |
fpc = ~fpc |
| 117 |
) |
| 118 |
|
| 119 |
expect_true( |
| 120 |
survey::svychisq( |
| 121 |
~ sch.wide + stype, |
| 122 |
design = dclus1, |
| 123 |
statistic = "Wald" |
| 124 |
) |> |
| 125 |
tidy() |> |
| 126 |
purrr::none(~ inherits(., "matrix")) |
| 127 |
) |
| 128 |
}) |
| 129 |
|
| 130 |
test_that("tidy.htest handles various test types", { |
| 131 |
tt <- t.test(rnorm(10)) |
| 132 |
tt$parameter <- c(9, 10) |
| 133 |
|
| 134 |
expect_snapshot(.res <- tidy(tt)) |
| 135 |
}) |