-
Notifications
You must be signed in to change notification settings - Fork 7
/
test-tidy_plus_plus.R
103 lines (93 loc) · 2.69 KB
/
test-tidy_plus_plus.R
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
test_that("tidy_plus_plus() works for basic models", {
mod <- glm(response ~ stage + grade + trt, gtsummary::trial, family = binomial)
expect_error(
mod %>% tidy_plus_plus(add_header_rows = TRUE, keep = c(stage, grade)),
NA
)
})
test_that("tidy_plus_plus() and functionnal programming", {
# works with glm
expect_error(
res <- dplyr::tibble(grade = c("I", "II", "III")) %>%
dplyr::mutate(df_model = purrr:::map(grade, ~ gtsummary::trial %>% dplyr::filter(grade == ..1))) %>%
dplyr::mutate(
mv_formula_char = "response ~ trt + age + marker",
mv_formula = purrr::map(mv_formula_char, ~ as.formula(.x)),
mv_model_form =
purrr::map2(
mv_formula, df_model,
~ glm(..1, data = ..2)
),
mv_tbl_form =
purrr::map(
mv_model_form,
~ tidy_plus_plus(..1, exponentiate = TRUE, add_header_rows = TRUE)
)
),
NA
)
# for coxph, identification of variables will not work
# will display a message
# but a result should be returned
expect_message(
res <- dplyr::tibble(grade = c("I", "II", "III")) %>%
dplyr::mutate(df_model = purrr:::map(grade, ~ gtsummary::trial %>% dplyr::filter(grade == ..1))) %>%
dplyr::mutate(
mv_formula_char = "survival::Surv(ttdeath, death) ~ trt + age + marker",
mv_formula = purrr::map(mv_formula_char, ~ as.formula(.x)),
mv_model_form =
purrr::map2(
mv_formula, df_model,
~ survival::coxph(..1, data = ..2)
),
mv_tbl_form =
purrr::map(
mv_model_form,
~ tidy_plus_plus(..1, exponentiate = TRUE)
)
)
)
})
test_that("tidy_plus_plus() with mice objects", {
# impute missing values
imputed_trial <-
suppressWarnings(mice::mice(gtsummary::trial, maxit = 2, m = 2))
# build regression model
mod <- with(imputed_trial, lm(age ~ marker + grade))
# testing pre-pooled results
expect_error(
tidy_plus_plus(
mod,
exponentiate = FALSE,
tidy_fun = function(x, ...) mice::pool(x) %>% mice::tidy(...)
),
NA
)
})
test_that("tidy_plus_plus() with tidyselect", {
# build regression model
mod <- lm(age ~ trt + marker + grade, gtsummary::trial)
expect_error(
tidy_plus_plus(
mod,
add_header_rows = TRUE,
show_single_row = trt,
no_reference_row = grade
),
NA
)
expect_equal(
tidy_plus_plus(
mod,
add_header_rows = TRUE,
show_single_row = "trt",
no_reference_row = "grade"
),
tidy_plus_plus(
mod,
add_header_rows = TRUE,
show_single_row = trt,
no_reference_row = grade
)
)
})