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