-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ard_aod_wald_test.R
118 lines (110 loc) · 3.71 KB
/
ard_aod_wald_test.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#' ARD Wald Test
#'
#' @description
#' Function takes a regression model object and calculates Wald
#' statistical test using [`aod::wald.test()`].
#'
#' @param x regression model object
#' @param ... arguments passed to `aod::wald.test(...)`
#' @inheritParams ard_regression
#'
#' @return data frame
#' @export
#'
#' @examplesIf do.call(asNamespace("cardx")$is_pkg_installed, list(pkg = c("aod", "broom.helpers")))
#' lm(AGE ~ ARM, data = cards::ADSL) |>
#' ard_aod_wald_test()
ard_aod_wald_test <- function(x, tidy_fun = broom.helpers::tidy_with_broom_or_parameters, ...) {
set_cli_abort_call()
# check installed packages ---------------------------------------------------
check_pkg_installed(c("aod", "broom.helpers"))
# check inputs ---------------------------------------------------------------
check_not_missing(x)
# run regression() -----------------------------------------------------------
reg_model <- cards::eval_capture_conditions(
ard_regression_basic(x, tidy_fun = tidy_fun, intercept = TRUE, stats_to_remove = c(
"var_type",
"var_label",
"var_class", "label",
"contrasts_type", "contrasts", "var_nlevels", "std.error",
"conf.low", "conf.high", "statistic", "p.value", "estimate"
))
)
if (!is.null(reg_model[["error"]])) {
cli::cli_abort(
c("Unable to identify underlying variable names in regression model.",
i = "Is this model type supported by {.fun broom.helpers::tidy_plus_plus}, which is the function used to identify variable names?"
),
call = get_cli_abort_call()
)
}
aod <-
reg_model[["result"]] %>%
dplyr::select(c(
variable = "variable",
model_terms = "stat"
)) %>%
dplyr::mutate(term_id = dplyr::row_number()) %>%
tidyr::nest(data = -"variable") %>%
dplyr::rowwise() %>%
dplyr::mutate(
model_terms = unlist(.data$data[["model_terms"]]) %>% list(),
model_terms_id = rlang::set_names(.data$data[["term_id"]]) %>% list()
)
# run wald.test() -----------------------------------------------------------
wald_test <-
cards::eval_capture_conditions(lapply(seq_len(length(aod$model_terms_id)), function(terms_id) {
aod::wald.test(
Sigma = stats::vcov(x),
b = stats::coef(x), Terms = aod$model_terms_id[[terms_id]]
)
}))
df_list <- do.call(rbind, lapply(wald_test$result, .extract_wald_results))
cbind(aod$variable, df_list) %>%
tidyr::pivot_longer(
cols = !"aod$variable",
names_to = "stat_name",
values_to = "stat"
) %>%
dplyr::rename(
"variable" = "aod$variable"
) |>
dplyr::mutate(
stat = as.list(.data$stat),
stat_label =
dplyr::case_when(
.data$stat_name %in% "statistic" ~ "Statistic",
.data$stat_name %in% "df" ~ "Degrees of Freedom",
.data$stat_name %in% "p.value" ~ "p-value",
TRUE ~ .data$stat_name
),
fmt_fn =
map(
.data$stat,
function(.x) {
# styler: off
if (is.integer(.x)) return(0L)
if (is.numeric(.x)) return(1L)
# styler: on
NULL
}
),
context = "aod_wald_test",
warning = wald_test["warning"],
error = wald_test["error"]
) |>
cards::as_card() |>
cards::tidy_ard_column_order()
}
#' Extract data from wald.test object
#'
#' @param wald_test (`data.frame`)\cr wald test object object from `aod::wald.test()`
#'
#' @return a data frame containing the wald test results.
#' @keywords internal
.extract_wald_results <- function(wald_test) {
df <- wald_test$result$chi2[("df")]
statistic <- wald_test$result$chi2[("chi2")]
p.value <- wald_test$result$chi2[("P")]
data.frame(df, statistic, p.value)
}