1 |
#' Helper function to produce data frame with results |
|
2 |
#' of pool for a single visit |
|
3 |
#' |
|
4 |
#' `r lifecycle::badge("experimental")` |
|
5 |
#' |
|
6 |
#' @param x (`pool`) is a list of pooled object from `rbmi` analysis results. This list includes |
|
7 |
#' analysis results, confidence level, hypothesis testing type. |
|
8 |
#' @export |
|
9 |
#' |
|
10 |
#' @examples |
|
11 |
#' data("rbmi_test_data") |
|
12 |
#' pool_obj <- rbmi_test_data |
|
13 |
#' |
|
14 |
#' h_tidy_pool(pool_obj$pars[1:3]) |
|
15 |
#' |
|
16 |
h_tidy_pool <- function(x) { |
|
17 | 9x |
contr <- x[[grep("trt_", names(x))]] |
18 | 9x |
ref <- x[[grep("lsm_ref_", names(x))]] |
19 | 9x |
alt <- x[[grep("lsm_alt_", names(x))]] |
20 | ||
21 | 9x |
df_ref <- data.frame( |
22 | 9x |
group = "ref", |
23 | 9x |
est = ref$est, |
24 | 9x |
se_est = ref$se, |
25 | 9x |
lower_cl_est = ref$ci[1], |
26 | 9x |
upper_cl_est = ref$ci[2], |
27 | 9x |
est_contr = NA_real_, |
28 | 9x |
se_contr = NA_real_, |
29 | 9x |
lower_cl_contr = NA_real_, |
30 | 9x |
upper_cl_contr = NA_real_, |
31 | 9x |
p_value = NA_real_, |
32 | 9x |
relative_reduc = NA_real_, |
33 | 9x |
stringsAsFactors = FALSE |
34 |
) |
|
35 | ||
36 | 9x |
df_alt <- data.frame( |
37 | 9x |
group = "alt", |
38 | 9x |
est = alt$est, |
39 | 9x |
se_est = alt$se, |
40 | 9x |
lower_cl_est = alt$ci[1], |
41 | 9x |
upper_cl_est = alt$ci[2], |
42 | 9x |
est_contr = contr$est, |
43 | 9x |
se_contr = contr$se, |
44 | 9x |
lower_cl_contr = contr$ci[1], |
45 | 9x |
upper_cl_contr = contr$ci[2], |
46 | 9x |
p_value = contr$pvalue, |
47 | 9x |
relative_reduc = contr$est / df_ref$est, |
48 | 9x |
stringsAsFactors = FALSE |
49 |
) |
|
50 | ||
51 | 9x |
result <- rbind( |
52 | 9x |
df_ref, |
53 | 9x |
df_alt |
54 |
) |
|
55 | ||
56 | 9x |
result |
57 |
} |
|
58 | ||
59 |
#' Helper method (for [`broom::tidy()`]) to prepare a data frame from an |
|
60 |
#' `pool` `rbmi` object containing the LS means and contrasts and multiple visits |
|
61 |
#' |
|
62 |
#' `r lifecycle::badge("experimental")` |
|
63 |
#' |
|
64 |
#' @method tidy pool |
|
65 |
#' @param x (`pool`) is a list of pooled object from `rbmi` analysis results. This list includes |
|
66 |
#' analysis results, confidence level, hypothesis testing type. |
|
67 |
#' @param ... Additional arguments. Not used. Needed to match generic signature only. |
|
68 |
#' @export |
|
69 |
#' @return A dataframe |
|
70 |
#' |
|
71 |
tidy.pool <- function(x, ...) { # nolint |
|
72 | ||
73 | 2x |
ls_raw <- x$pars |
74 | ||
75 | 2x |
visit_raw_names <- names(ls_raw)[grep("trt_", names(ls_raw))] |
76 | 2x |
l_visit_names <- strsplit(visit_raw_names, "trt_") |
77 | 2x |
visit_names <- vapply(l_visit_names, `[`, 2, FUN.VALUE = character(1)) |
78 | ||
79 | 2x |
spl <- rep(visit_names, each = 3) |
80 | ||
81 | 2x |
ls_split <- split(ls_raw, spl) |
82 | ||
83 | 2x |
ls_df <- lapply(ls_split, h_tidy_pool) |
84 | ||
85 | 2x |
result <- do.call(rbind, unname(ls_df)) |
86 | ||
87 | 2x |
result$visit <- factor(rep(visit_names, each = 2)) |
88 | 2x |
result$group <- factor(result$group, levels = c("ref", "alt")) |
89 | 2x |
result$conf_level <- x$conf.level |
90 | ||
91 | 2x |
result |
92 |
} |
|
93 | ||
94 |
#' Statistics function which is extracting estimates from a tidied LS means |
|
95 |
#' data frame. |
|
96 |
#' |
|
97 |
#' `r lifecycle::badge("experimental")` |
|
98 |
#' |
|
99 |
#' @param df input dataframe |
|
100 |
#' @param .in_ref_col boolean variable, if reference column is specified |
|
101 |
#' @param show_relative "reduction" if (`control - treatment`, default) or "increase" |
|
102 |
#' (`treatment - control`) of relative change from baseline? |
|
103 |
#' @export |
|
104 |
#' |
|
105 |
#' @examples |
|
106 |
#' library(rtables) |
|
107 |
#' library(dplyr) |
|
108 |
#' library(broom) |
|
109 |
#' |
|
110 |
#' data("rbmi_test_data") |
|
111 |
#' pool_obj <- rbmi_test_data |
|
112 |
#' df <- tidy(pool_obj) |
|
113 |
#' |
|
114 |
#' s_rbmi_lsmeans(df[1, ], .in_ref_col = TRUE) |
|
115 |
#' |
|
116 |
#' s_rbmi_lsmeans(df[2, ], .in_ref_col = FALSE) |
|
117 |
#' |
|
118 |
s_rbmi_lsmeans <- function(df, .in_ref_col, show_relative = c("reduction", "increase")) { |
|
119 | 3x |
checkmate::assert_flag(.in_ref_col) |
120 | 3x |
show_relative <- match.arg(show_relative) |
121 | 3x |
if_not_ref <- function(x) `if`(.in_ref_col, character(), x) |
122 | 3x |
list( |
123 | 3x |
adj_mean_se = c(df$est, df$se_est), |
124 | 3x |
adj_mean_ci = formatters::with_label( |
125 | 3x |
c(df$lower_cl_est, df$upper_cl_est), |
126 | 3x |
f_conf_level(df$conf_level) |
127 |
), |
|
128 | 3x |
diff_mean_se = if_not_ref(c(df$est_contr, df$se_contr)), |
129 | 3x |
diff_mean_ci = formatters::with_label( |
130 | 3x |
if_not_ref(c(df$lower_cl_contr, df$upper_cl_contr)), |
131 | 3x |
f_conf_level(df$conf_level) |
132 |
), |
|
133 | 3x |
change = switch(show_relative, |
134 | 3x |
reduction = formatters::with_label(if_not_ref(df$relative_reduc), "Relative Reduction (%)"), |
135 | 3x |
increase = formatters::with_label(if_not_ref(-df$relative_reduc), "Relative Increase (%)") |
136 |
), |
|
137 | 3x |
p_value = if_not_ref(df$p_value) |
138 |
) |
|
139 |
} |
|
140 | ||
141 |
#' Formatted Analysis function which can be further customized by calling |
|
142 |
#' [`rtables::make_afun()`] on it. It is used as `afun` in [`rtables::analyze()`]. |
|
143 |
#' |
|
144 |
#' `r lifecycle::badge("experimental")` |
|
145 |
#' |
|
146 |
#' @param df input dataframe |
|
147 |
#' @param .in_ref_col boolean variable, if reference column is specified |
|
148 |
#' @param show_relative "reduction" if (`control - treatment`, default) or "increase" |
|
149 |
#' (`treatment - control`) of relative change from baseline? |
|
150 |
#' @export |
|
151 |
#' |
|
152 |
a_rbmi_lsmeans <- make_afun( |
|
153 |
s_rbmi_lsmeans, |
|
154 |
.labels = c( |
|
155 |
adj_mean_se = "Adjusted Mean (SE)", |
|
156 |
diff_mean_se = "Difference in Adjusted Means (SE)", |
|
157 |
p_value = "p-value (RBMI)" |
|
158 |
), |
|
159 |
.formats = c( |
|
160 |
# n = "xx.", # note we don't have N from `rbmi` result |
|
161 |
adj_mean_se = sprintf_format("%.3f (%.3f)"), |
|
162 |
adj_mean_ci = "(xx.xxx, xx.xxx)", |
|
163 |
diff_mean_se = sprintf_format("%.3f (%.3f)"), |
|
164 |
diff_mean_ci = "(xx.xxx, xx.xxx)", |
|
165 |
change = "xx.x%", |
|
166 |
p_value = "x.xxxx | (<0.0001)" |
|
167 |
), |
|
168 |
.indent_mods = c( |
|
169 |
adj_mean_ci = 1L, |
|
170 |
diff_mean_ci = 1L, |
|
171 |
change = 1L, |
|
172 |
p_value = 1L |
|
173 |
), |
|
174 |
.null_ref_cells = FALSE |
|
175 |
) |
|
176 | ||
177 |
#' Analyze function for tabulating LS means estimates from tidied |
|
178 |
#' `rbmi` `pool` results. |
|
179 |
#' |
|
180 |
#' `r lifecycle::badge("experimental")` |
|
181 |
#' |
|
182 |
#' @param lyt (`layout`)\cr input layout where analyses will be added to. |
|
183 |
#' @param table_names (`character`)\cr this can be customized in case that the same `vars` are analyzed multiple times, |
|
184 |
#' to avoid warnings from `rtables`. |
|
185 |
#' @param .stats (`character`)\cr statistics to select for the table. |
|
186 |
#' @param .formats (named `character` or `list`)\cr formats for the statistics. |
|
187 |
#' @param .indent_mods (named `integer`)\cr indent modifiers for the labels. |
|
188 |
#' @param .labels (named `character`)\cr labels for the statistics (without indent). |
|
189 |
#' @param ... additional argument. |
|
190 |
#' @export |
|
191 |
#' |
|
192 |
#' @examples |
|
193 |
#' library(rtables) |
|
194 |
#' library(dplyr) |
|
195 |
#' library(broom) |
|
196 |
#' |
|
197 |
#' data("rbmi_test_data") |
|
198 |
#' pool_obj <- rbmi_test_data |
|
199 |
#' |
|
200 |
#' df <- tidy(pool_obj) |
|
201 |
#' |
|
202 |
#' basic_table() %>% |
|
203 |
#' split_cols_by("group", ref_group = levels(df$group)[1]) %>% |
|
204 |
#' split_rows_by("visit", split_label = "Visit", label_pos = "topleft") %>% |
|
205 |
#' summarize_rbmi() %>% |
|
206 |
#' build_table(df) |
|
207 |
#' |
|
208 |
summarize_rbmi <- function(lyt, |
|
209 |
..., |
|
210 |
table_names = "rbmi_summary", |
|
211 |
.stats = NULL, |
|
212 |
.formats = NULL, |
|
213 |
.indent_mods = NULL, |
|
214 |
.labels = NULL) { |
|
215 | 1x |
afun <- make_afun( |
216 | 1x |
a_rbmi_lsmeans, |
217 | 1x |
.stats = .stats, |
218 | 1x |
.formats = .formats, |
219 | 1x |
.indent_mods = .indent_mods, |
220 | 1x |
.labels = .labels |
221 |
) |
|
222 | 1x |
analyze( |
223 | 1x |
lyt = lyt, |
224 | 1x |
vars = "est", |
225 | 1x |
afun = afun, |
226 | 1x |
table_names = table_names, |
227 | 1x |
extra_args = list(...) |
228 |
) |
|
229 |
} |