1 |
#' Multivariate Logistic Regression Table
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Layout-creating function which summarizes a logistic variable regression for binary outcome with
|
|
6 |
#' categorical/continuous covariates in model statement. For each covariate category (if categorical)
|
|
7 |
#' or specified values (if continuous), present degrees of freedom, regression parameter estimate and
|
|
8 |
#' standard error (SE) relative to reference group or category. Report odds ratios for each covariate
|
|
9 |
#' category or specified values and corresponding Wald confidence intervals as default but allow user
|
|
10 |
#' to specify other confidence levels. Report p-value for Wald chi-square test of the null hypothesis
|
|
11 |
#' that covariate has no effect on response in model containing all specified covariates.
|
|
12 |
#' Allow option to include one two-way interaction and present similar output for
|
|
13 |
#' each interaction degree of freedom.
|
|
14 |
#'
|
|
15 |
#' @inheritParams argument_convention
|
|
16 |
#' @param drop_and_remove_str (`character`)\cr string to be dropped and removed.
|
|
17 |
#'
|
|
18 |
#' @return A layout object suitable for passing to further layouting functions, or to [rtables::build_table()].
|
|
19 |
#' Adding this function to an `rtable` layout will add a logistic regression variable summary to the table layout.
|
|
20 |
#'
|
|
21 |
#' @note For the formula, the variable names need to be standard `data.frame` column names without
|
|
22 |
#' special characters.
|
|
23 |
#'
|
|
24 |
#' @examples
|
|
25 |
#' library(dplyr)
|
|
26 |
#' library(broom)
|
|
27 |
#'
|
|
28 |
#' adrs_f <- tern_ex_adrs %>%
|
|
29 |
#' filter(PARAMCD == "BESRSPI") %>%
|
|
30 |
#' filter(RACE %in% c("ASIAN", "WHITE", "BLACK OR AFRICAN AMERICAN")) %>%
|
|
31 |
#' mutate(
|
|
32 |
#' Response = case_when(AVALC %in% c("PR", "CR") ~ 1, TRUE ~ 0),
|
|
33 |
#' RACE = factor(RACE),
|
|
34 |
#' SEX = factor(SEX)
|
|
35 |
#' )
|
|
36 |
#' formatters::var_labels(adrs_f) <- c(formatters::var_labels(tern_ex_adrs), Response = "Response")
|
|
37 |
#' mod1 <- fit_logistic(
|
|
38 |
#' data = adrs_f,
|
|
39 |
#' variables = list(
|
|
40 |
#' response = "Response",
|
|
41 |
#' arm = "ARMCD",
|
|
42 |
#' covariates = c("AGE", "RACE")
|
|
43 |
#' )
|
|
44 |
#' )
|
|
45 |
#' mod2 <- fit_logistic(
|
|
46 |
#' data = adrs_f,
|
|
47 |
#' variables = list(
|
|
48 |
#' response = "Response",
|
|
49 |
#' arm = "ARMCD",
|
|
50 |
#' covariates = c("AGE", "RACE"),
|
|
51 |
#' interaction = "AGE"
|
|
52 |
#' )
|
|
53 |
#' )
|
|
54 |
#'
|
|
55 |
#' df <- tidy(mod1, conf_level = 0.99)
|
|
56 |
#' df2 <- tidy(mod2, conf_level = 0.99)
|
|
57 |
#'
|
|
58 |
#' # flagging empty strings with "_"
|
|
59 |
#' df <- df_explicit_na(df, na_level = "_")
|
|
60 |
#' df2 <- df_explicit_na(df2, na_level = "_")
|
|
61 |
#'
|
|
62 |
#' result1 <- basic_table() %>%
|
|
63 |
#' summarize_logistic(
|
|
64 |
#' conf_level = 0.95,
|
|
65 |
#' drop_and_remove_str = "_"
|
|
66 |
#' ) %>%
|
|
67 |
#' build_table(df = df)
|
|
68 |
#' result1
|
|
69 |
#'
|
|
70 |
#' result2 <- basic_table() %>%
|
|
71 |
#' summarize_logistic(
|
|
72 |
#' conf_level = 0.95,
|
|
73 |
#' drop_and_remove_str = "_"
|
|
74 |
#' ) %>%
|
|
75 |
#' build_table(df = df2)
|
|
76 |
#' result2
|
|
77 |
#'
|
|
78 |
#' @export
|
|
79 |
summarize_logistic <- function(lyt, |
|
80 |
conf_level,
|
|
81 |
drop_and_remove_str = "", |
|
82 |
.indent_mods = NULL) { |
|
83 |
# checks
|
|
84 | 3x |
checkmate::assert_string(drop_and_remove_str) |
85 | ||
86 | 3x |
sum_logistic_variable_test <- logistic_summary_by_flag("is_variable_summary") |
87 | 3x |
sum_logistic_term_estimates <- logistic_summary_by_flag("is_term_summary", .indent_mods = .indent_mods) |
88 | 3x |
sum_logistic_odds_ratios <- logistic_summary_by_flag("is_reference_summary", .indent_mods = .indent_mods) |
89 | 3x |
split_fun <- drop_and_remove_levels(drop_and_remove_str) |
90 | ||
91 | 3x |
lyt <- logistic_regression_cols(lyt, conf_level = conf_level) |
92 | 3x |
lyt <- split_rows_by(lyt, var = "variable", labels_var = "variable_label", split_fun = split_fun) |
93 | 3x |
lyt <- sum_logistic_variable_test(lyt) |
94 | 3x |
lyt <- split_rows_by(lyt, var = "term", labels_var = "term_label", split_fun = split_fun) |
95 | 3x |
lyt <- sum_logistic_term_estimates(lyt) |
96 | 3x |
lyt <- split_rows_by(lyt, var = "interaction", labels_var = "interaction_label", split_fun = split_fun) |
97 | 3x |
lyt <- split_rows_by(lyt, var = "reference", labels_var = "reference_label", split_fun = split_fun) |
98 | 3x |
lyt <- sum_logistic_odds_ratios(lyt) |
99 | 3x |
lyt
|
100 |
}
|
|
101 | ||
102 |
#' Fit for Logistic Regression
|
|
103 |
#'
|
|
104 |
#' @description `r lifecycle::badge("stable")`
|
|
105 |
#'
|
|
106 |
#' Fit a (conditional) logistic regression model.
|
|
107 |
#'
|
|
108 |
#' @inheritParams argument_convention
|
|
109 |
#' @param data (`data.frame`)\cr the data frame on which the model was fit.
|
|
110 |
#' @param response_definition (`string`)\cr the definition of what an event is in terms of `response`.
|
|
111 |
#' This will be used when fitting the (conditional) logistic regression model on the left hand
|
|
112 |
#' side of the formula.
|
|
113 |
#'
|
|
114 |
#' @return A fitted logistic regression model.
|
|
115 |
#'
|
|
116 |
#' @section Model Specification:
|
|
117 |
#'
|
|
118 |
#' The `variables` list needs to include the following elements:
|
|
119 |
#' * `arm`: Treatment arm variable name.
|
|
120 |
#' * `response`: The response arm variable name. Usually this is a 0/1 variable.
|
|
121 |
#' * `covariates`: This is either `NULL` (no covariates) or a character vector of covariate variable names.
|
|
122 |
#' * `interaction`: This is either `NULL` (no interaction) or a string of a single covariate variable name already
|
|
123 |
#' included in `covariates`. Then the interaction with the treatment arm is included in the model.
|
|
124 |
#'
|
|
125 |
#' @examples
|
|
126 |
#' library(dplyr)
|
|
127 |
#'
|
|
128 |
#' adrs_f <- tern_ex_adrs %>%
|
|
129 |
#' filter(PARAMCD == "BESRSPI") %>%
|
|
130 |
#' filter(RACE %in% c("ASIAN", "WHITE", "BLACK OR AFRICAN AMERICAN")) %>%
|
|
131 |
#' mutate(
|
|
132 |
#' Response = case_when(AVALC %in% c("PR", "CR") ~ 1, TRUE ~ 0),
|
|
133 |
#' RACE = factor(RACE),
|
|
134 |
#' SEX = factor(SEX)
|
|
135 |
#' )
|
|
136 |
#' formatters::var_labels(adrs_f) <- c(formatters::var_labels(tern_ex_adrs), Response = "Response")
|
|
137 |
#' mod1 <- fit_logistic(
|
|
138 |
#' data = adrs_f,
|
|
139 |
#' variables = list(
|
|
140 |
#' response = "Response",
|
|
141 |
#' arm = "ARMCD",
|
|
142 |
#' covariates = c("AGE", "RACE")
|
|
143 |
#' )
|
|
144 |
#' )
|
|
145 |
#' mod2 <- fit_logistic(
|
|
146 |
#' data = adrs_f,
|
|
147 |
#' variables = list(
|
|
148 |
#' response = "Response",
|
|
149 |
#' arm = "ARMCD",
|
|
150 |
#' covariates = c("AGE", "RACE"),
|
|
151 |
#' interaction = "AGE"
|
|
152 |
#' )
|
|
153 |
#' )
|
|
154 |
#'
|
|
155 |
#' @export
|
|
156 |
fit_logistic <- function(data, |
|
157 |
variables = list( |
|
158 |
response = "Response", |
|
159 |
arm = "ARMCD", |
|
160 |
covariates = NULL, |
|
161 |
interaction = NULL, |
|
162 |
strata = NULL |
|
163 |
),
|
|
164 |
response_definition = "response") { |
|
165 | 62x |
assert_df_with_variables(data, variables) |
166 | 62x |
checkmate::assert_subset(names(variables), c("response", "arm", "covariates", "interaction", "strata")) |
167 | 62x |
checkmate::assert_string(response_definition) |
168 | 62x |
checkmate::assert_true(grepl("response", response_definition)) |
169 | ||
170 | 62x |
response_definition <- sub( |
171 | 62x |
pattern = "response", |
172 | 62x |
replacement = variables$response, |
173 | 62x |
x = response_definition, |
174 | 62x |
fixed = TRUE |
175 |
)
|
|
176 | 62x |
form <- paste0(response_definition, " ~ ", variables$arm) |
177 | 62x |
if (!is.null(variables$covariates)) { |
178 | 28x |
form <- paste0(form, " + ", paste(variables$covariates, collapse = " + ")) |
179 |
}
|
|
180 | 62x |
if (!is.null(variables$interaction)) { |
181 | 17x |
checkmate::assert_string(variables$interaction) |
182 | 17x |
checkmate::assert_subset(variables$interaction, variables$covariates) |
183 | 17x |
form <- paste0(form, " + ", variables$arm, ":", variables$interaction) |
184 |
}
|
|
185 | 62x |
if (!is.null(variables$strata)) { |
186 | 14x |
strata_arg <- if (length(variables$strata) > 1) { |
187 | 7x |
paste0("I(interaction(", paste0(variables$strata, collapse = ", "), "))") |
188 |
} else { |
|
189 | 7x |
variables$strata |
190 |
}
|
|
191 | 14x |
form <- paste0(form, "+ strata(", strata_arg, ")") |
192 |
}
|
|
193 | 62x |
formula <- stats::as.formula(form) |
194 | 62x |
if (is.null(variables$strata)) { |
195 | 48x |
stats::glm( |
196 | 48x |
formula = formula, |
197 | 48x |
data = data, |
198 | 48x |
family = stats::binomial("logit") |
199 |
)
|
|
200 |
} else { |
|
201 | 14x |
clogit_with_tryCatch( |
202 | 14x |
formula = formula, |
203 | 14x |
data = data, |
204 | 14x |
x = TRUE |
205 |
)
|
|
206 |
}
|
|
207 |
}
|
|
208 | ||
209 |
#' Custom Tidy Method for Binomial GLM Results
|
|
210 |
#'
|
|
211 |
#' @description `r lifecycle::badge("stable")`
|
|
212 |
#'
|
|
213 |
#' Helper method (for [broom::tidy()]) to prepare a data frame from a `glm` object
|
|
214 |
#' with `binomial` family.
|
|
215 |
#'
|
|
216 |
#' @inheritParams argument_convention
|
|
217 |
#' @param at (`NULL` or `numeric`)\cr optional values for the interaction variable. Otherwise the median is used.
|
|
218 |
#' @param x logistic regression model fitted by [stats::glm()] with "binomial" family.
|
|
219 |
#'
|
|
220 |
#' @return A `data.frame` containing the tidied model.
|
|
221 |
#'
|
|
222 |
#' @method tidy glm
|
|
223 |
#'
|
|
224 |
#' @seealso [h_logistic_regression] for relevant helper functions.
|
|
225 |
#'
|
|
226 |
#' @examples
|
|
227 |
#' library(dplyr)
|
|
228 |
#' library(broom)
|
|
229 |
#'
|
|
230 |
#' adrs_f <- tern_ex_adrs %>%
|
|
231 |
#' filter(PARAMCD == "BESRSPI") %>%
|
|
232 |
#' filter(RACE %in% c("ASIAN", "WHITE", "BLACK OR AFRICAN AMERICAN")) %>%
|
|
233 |
#' mutate(
|
|
234 |
#' Response = case_when(AVALC %in% c("PR", "CR") ~ 1, TRUE ~ 0),
|
|
235 |
#' RACE = factor(RACE),
|
|
236 |
#' SEX = factor(SEX)
|
|
237 |
#' )
|
|
238 |
#' formatters::var_labels(adrs_f) <- c(formatters::var_labels(tern_ex_adrs), Response = "Response")
|
|
239 |
#' mod1 <- fit_logistic(
|
|
240 |
#' data = adrs_f,
|
|
241 |
#' variables = list(
|
|
242 |
#' response = "Response",
|
|
243 |
#' arm = "ARMCD",
|
|
244 |
#' covariates = c("AGE", "RACE")
|
|
245 |
#' )
|
|
246 |
#' )
|
|
247 |
#' mod2 <- fit_logistic(
|
|
248 |
#' data = adrs_f,
|
|
249 |
#' variables = list(
|
|
250 |
#' response = "Response",
|
|
251 |
#' arm = "ARMCD",
|
|
252 |
#' covariates = c("AGE", "RACE"),
|
|
253 |
#' interaction = "AGE"
|
|
254 |
#' )
|
|
255 |
#' )
|
|
256 |
#'
|
|
257 |
#' df <- tidy(mod1, conf_level = 0.99)
|
|
258 |
#' df2 <- tidy(mod2, conf_level = 0.99)
|
|
259 |
#'
|
|
260 |
#' @export
|
|
261 |
tidy.glm <- function(x, # nolint |
|
262 |
conf_level = 0.95, |
|
263 |
at = NULL, |
|
264 |
...) { |
|
265 | 5x |
checkmate::assert_class(x, "glm") |
266 | 5x |
checkmate::assert_set_equal(x$family$family, "binomial") |
267 | ||
268 | 5x |
terms_name <- attr(stats::terms(x), "term.labels") |
269 | 5x |
xs_class <- attr(x$terms, "dataClasses") |
270 | 5x |
interaction <- terms_name[which(!terms_name %in% names(xs_class))] |
271 | 5x |
df <- if (length(interaction) == 0) { |
272 | 2x |
h_logistic_simple_terms( |
273 | 2x |
x = terms_name, |
274 | 2x |
fit_glm = x, |
275 | 2x |
conf_level = conf_level |
276 |
)
|
|
277 |
} else { |
|
278 | 3x |
h_logistic_inter_terms( |
279 | 3x |
x = terms_name, |
280 | 3x |
fit_glm = x, |
281 | 3x |
conf_level = conf_level, |
282 | 3x |
at = at |
283 |
)
|
|
284 |
}
|
|
285 | 5x |
for (var in c("variable", "term", "interaction", "reference")) { |
286 | 20x |
df[[var]] <- factor(df[[var]], levels = unique(df[[var]])) |
287 |
}
|
|
288 | 5x |
df
|
289 |
}
|
|
290 | ||
291 |
#' Logistic Regression Multivariate Column Layout Function
|
|
292 |
#'
|
|
293 |
#' @description `r lifecycle::badge("stable")`
|
|
294 |
#'
|
|
295 |
#' Layout-creating function which creates a multivariate column layout summarizing logistic
|
|
296 |
#' regression results. This function is a wrapper for [rtables::split_cols_by_multivar()].
|
|
297 |
#'
|
|
298 |
#' @inheritParams argument_convention
|
|
299 |
#'
|
|
300 |
#' @return A layout object suitable for passing to further layouting functions. Adding this
|
|
301 |
#' function to an `rtable` layout will split the table into columns corresponding to
|
|
302 |
#' statistics `df`, `estimate`, `std_error`, `odds_ratio`, `ci`, and `pvalue`.
|
|
303 |
#'
|
|
304 |
#' @export
|
|
305 |
logistic_regression_cols <- function(lyt, |
|
306 |
conf_level = 0.95) { |
|
307 | 4x |
vars <- c("df", "estimate", "std_error", "odds_ratio", "ci", "pvalue") |
308 | 4x |
var_labels <- c( |
309 | 4x |
df = "Degrees of Freedom", |
310 | 4x |
estimate = "Parameter Estimate", |
311 | 4x |
std_error = "Standard Error", |
312 | 4x |
odds_ratio = "Odds Ratio", |
313 | 4x |
ci = paste("Wald", f_conf_level(conf_level)), |
314 | 4x |
pvalue = "p-value" |
315 |
)
|
|
316 | 4x |
split_cols_by_multivar( |
317 | 4x |
lyt = lyt, |
318 | 4x |
vars = vars, |
319 | 4x |
varlabels = var_labels |
320 |
)
|
|
321 |
}
|
|
322 | ||
323 |
#' Logistic Regression Summary Table Constructor Function
|
|
324 |
#'
|
|
325 |
#' @description `r lifecycle::badge("stable")`
|
|
326 |
#'
|
|
327 |
#' Constructor for content functions to be used in [`summarize_logistic()`] to summarize
|
|
328 |
#' logistic regression results. This function is a wrapper for [rtables::summarize_row_groups()].
|
|
329 |
#'
|
|
330 |
#' @inheritParams argument_convention
|
|
331 |
#' @param flag_var (`string`)\cr variable name identifying which row should be used in this
|
|
332 |
#' content function.
|
|
333 |
#'
|
|
334 |
#' @return A content function.
|
|
335 |
#'
|
|
336 |
#' @export
|
|
337 |
logistic_summary_by_flag <- function(flag_var, .indent_mods = NULL) { |
|
338 | 10x |
checkmate::assert_string(flag_var) |
339 | 10x |
function(lyt) { |
340 | 10x |
cfun_list <- list( |
341 | 10x |
df = cfun_by_flag("df", flag_var, format = "xx.", .indent_mods = .indent_mods), |
342 | 10x |
estimate = cfun_by_flag("estimate", flag_var, format = "xx.xxx", .indent_mods = .indent_mods), |
343 | 10x |
std_error = cfun_by_flag("std_error", flag_var, format = "xx.xxx", .indent_mods = .indent_mods), |
344 | 10x |
odds_ratio = cfun_by_flag("odds_ratio", flag_var, format = ">999.99", .indent_mods = .indent_mods), |
345 | 10x |
ci = cfun_by_flag("ci", flag_var, format = format_extreme_values_ci(2L), .indent_mods = .indent_mods), |
346 | 10x |
pvalue = cfun_by_flag("pvalue", flag_var, format = "x.xxxx | (<0.0001)", .indent_mods = .indent_mods) |
347 |
)
|
|
348 | 10x |
summarize_row_groups( |
349 | 10x |
lyt = lyt, |
350 | 10x |
cfun = cfun_list |
351 |
)
|
|
352 |
}
|
|
353 |
}
|
1 |
#' Helper Function to create a new `SMQ` variable in `ADAE` by stacking `SMQ` and/or `CQ` records.
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Helper Function to create a new `SMQ` variable in `ADAE` that consists of all adverse events belonging to
|
|
6 |
#' selected Standardized/Customized queries. The new dataset will only contain records of the adverse events
|
|
7 |
#' belonging to any of the selected baskets.
|
|
8 |
#'
|
|
9 |
#' @inheritParams argument_convention
|
|
10 |
#' @param baskets (`character`)\cr variable names of the selected Standardized/Customized queries.
|
|
11 |
#' @param smq_varlabel (`string`)\cr a label for the new variable created.
|
|
12 |
#' @param keys (`character`)\cr names of the key variables to be returned along with the new variable created.
|
|
13 |
#' @param aag_summary (`data.frame`)\cr containing the `SMQ` baskets and the levels of interest for the final `SMQ`
|
|
14 |
#' variable. This is useful when there are some levels of interest that are not observed in the `df` dataset.
|
|
15 |
#' The two columns of this dataset should be named `basket` and `basket_name`.
|
|
16 |
#'
|
|
17 |
#' @return `data.frame` with variables in `keys` taken from `df` and new variable `SMQ` containing
|
|
18 |
#' records belonging to the baskets selected via the `baskets` argument.
|
|
19 |
#'
|
|
20 |
#' @examples
|
|
21 |
#' adae <- tern_ex_adae[1:20, ] %>% df_explicit_na()
|
|
22 |
#' h_stack_by_baskets(df = adae)
|
|
23 |
#'
|
|
24 |
#' aag <- data.frame(
|
|
25 |
#' NAMVAR = c("CQ01NAM", "CQ02NAM", "SMQ01NAM", "SMQ02NAM"),
|
|
26 |
#' REFNAME = c(
|
|
27 |
#' "D.2.1.5.3/A.1.1.1.1 AESI", "X.9.9.9.9/Y.8.8.8.8 AESI",
|
|
28 |
#' "C.1.1.1.3/B.2.2.3.1 AESI", "C.1.1.1.3/B.3.3.3.3 AESI"
|
|
29 |
#' ),
|
|
30 |
#' SCOPE = c("", "", "BROAD", "BROAD"),
|
|
31 |
#' stringsAsFactors = FALSE
|
|
32 |
#' )
|
|
33 |
#'
|
|
34 |
#' basket_name <- character(nrow(aag))
|
|
35 |
#' cq_pos <- grep("^(CQ).+NAM$", aag$NAMVAR)
|
|
36 |
#' smq_pos <- grep("^(SMQ).+NAM$", aag$NAMVAR)
|
|
37 |
#' basket_name[cq_pos] <- aag$REFNAME[cq_pos]
|
|
38 |
#' basket_name[smq_pos] <- paste0(
|
|
39 |
#' aag$REFNAME[smq_pos], "(", aag$SCOPE[smq_pos], ")"
|
|
40 |
#' )
|
|
41 |
#'
|
|
42 |
#' aag_summary <- data.frame(
|
|
43 |
#' basket = aag$NAMVAR,
|
|
44 |
#' basket_name = basket_name,
|
|
45 |
#' stringsAsFactors = TRUE
|
|
46 |
#' )
|
|
47 |
#'
|
|
48 |
#' result <- h_stack_by_baskets(df = adae, aag_summary = aag_summary)
|
|
49 |
#' all(levels(aag_summary$basket_name) %in% levels(result$SMQ))
|
|
50 |
#'
|
|
51 |
#' h_stack_by_baskets(
|
|
52 |
#' df = adae,
|
|
53 |
#' aag_summary = NULL,
|
|
54 |
#' keys = c("STUDYID", "USUBJID", "AEDECOD", "ARM"),
|
|
55 |
#' baskets = "SMQ01NAM"
|
|
56 |
#' )
|
|
57 |
#'
|
|
58 |
#' @export
|
|
59 |
h_stack_by_baskets <- function(df, |
|
60 |
baskets = grep("^(SMQ|CQ).+NAM$", names(df), value = TRUE), |
|
61 |
smq_varlabel = "Standardized MedDRA Query", |
|
62 |
keys = c("STUDYID", "USUBJID", "ASTDTM", "AEDECOD", "AESEQ"), |
|
63 |
aag_summary = NULL, |
|
64 |
na_level = "<Missing>") { |
|
65 |
# Use of df_explicit_na() in case the user has not previously used
|
|
66 | 5x |
df <- df_explicit_na(df, na_level = na_level) |
67 | ||
68 | 5x |
smq_nam <- baskets[startsWith(baskets, "SMQ")] |
69 |
# SC corresponding to NAM
|
|
70 | 5x |
smq_sc <- gsub(pattern = "NAM", replacement = "SC", x = smq_nam, fixed = TRUE) |
71 | 5x |
smq <- stats::setNames(smq_sc, smq_nam) |
72 | ||
73 | 5x |
checkmate::assert_character(baskets) |
74 | 5x |
checkmate::assert_string(smq_varlabel) |
75 | 5x |
checkmate::assert_data_frame(df) |
76 | 5x |
checkmate::assert_true(all(startsWith(baskets, "SMQ") | startsWith(baskets, "CQ"))) |
77 | 4x |
checkmate::assert_true(all(endsWith(baskets, "NAM"))) |
78 | 3x |
checkmate::assert_subset(baskets, names(df)) |
79 | 3x |
checkmate::assert_subset(keys, names(df)) |
80 | 3x |
checkmate::assert_subset(smq_sc, names(df)) |
81 | 3x |
checkmate::assert_string(na_level) |
82 | ||
83 | 3x |
if (!is.null(aag_summary)) { |
84 | 1x |
assert_df_with_variables( |
85 | 1x |
df = aag_summary, |
86 | 1x |
variables = list(val = c("basket", "basket_name")) |
87 |
)
|
|
88 |
# Warning in case there is no match between `aag_summary$basket` and `baskets` argument.
|
|
89 |
# Honestly, I think those should completely match. Target baskets should be the same.
|
|
90 | 1x |
if (length(intersect(baskets, unique(aag_summary$basket))) == 0) { |
91 | ! |
warning("There are 0 baskets in common between aag_summary$basket and `baskets` argument.") |
92 |
}
|
|
93 |
}
|
|
94 | ||
95 | 3x |
var_labels <- c(formatters::var_labels(df[, keys]), "SMQ" = smq_varlabel) |
96 | ||
97 |
# convert `na_level` records from baskets to NA for the later loop and from wide to long steps
|
|
98 | 3x |
df[, c(baskets, smq_sc)][df[, c(baskets, smq_sc)] == na_level] <- NA |
99 | ||
100 | 3x |
if (all(is.na(df[, baskets]))) { # in case there is no level for the target baskets |
101 | 1x |
df_long <- df[-seq_len(nrow(df)), keys] # we just need an empty dataframe keeping all factor levels |
102 |
} else { |
|
103 |
# Concatenate SMQxxxNAM with corresponding SMQxxxSC
|
|
104 | 2x |
df_cnct <- df[, c(keys, baskets[startsWith(baskets, "CQ")])] |
105 | ||
106 | 2x |
for (nam in names(smq)) { |
107 | 4x |
sc <- smq[nam] # SMQxxxSC corresponding to SMQxxxNAM |
108 | 4x |
nam_notna <- !is.na(df[[nam]]) |
109 | 4x |
new_colname <- paste(nam, sc, sep = "_") |
110 | 4x |
df_cnct[nam_notna, new_colname] <- paste0(df[[nam]], "(", df[[sc]], ")")[nam_notna] |
111 |
}
|
|
112 | ||
113 | 2x |
df_cnct$unique_id <- seq(1, nrow(df_cnct)) |
114 | 2x |
var_cols <- names(df_cnct)[!(names(df_cnct) %in% c(keys, "unique_id"))] |
115 |
# have to convert df_cnct from tibble to dataframe
|
|
116 |
# as it throws a warning otherwise about rownames.
|
|
117 |
# tibble do not support rownames and reshape creates rownames
|
|
118 | ||
119 | 2x |
df_long <- stats::reshape( |
120 | 2x |
data = as.data.frame(df_cnct), |
121 | 2x |
varying = var_cols, |
122 | 2x |
v.names = "SMQ", |
123 | 2x |
idvar = names(df_cnct)[names(df_cnct) %in% c(keys, "unique_id")], |
124 | 2x |
direction = "long", |
125 | 2x |
new.row.names = seq(prod(length(var_cols), nrow(df_cnct))) |
126 |
)
|
|
127 | ||
128 | 2x |
df_long <- df_long[!is.na(df_long[, "SMQ"]), !(names(df_long) %in% c("time", "unique_id"))] |
129 | 2x |
df_long$SMQ <- as.factor(df_long$SMQ) |
130 |
}
|
|
131 | ||
132 | 3x |
smq_levels <- setdiff(levels(df_long[["SMQ"]]), na_level) |
133 | ||
134 | 3x |
if (!is.null(aag_summary)) { |
135 |
# A warning in case there is no match between df and aag_summary records
|
|
136 | 1x |
if (length(intersect(smq_levels, unique(aag_summary$basket_name))) == 0) { |
137 | 1x |
warning("There are 0 basket levels in common between aag_summary$basket_name and df.") |
138 |
}
|
|
139 | 1x |
df_long[["SMQ"]] <- factor( |
140 | 1x |
df_long[["SMQ"]], |
141 | 1x |
levels = sort( |
142 | 1x |
c( |
143 | 1x |
smq_levels,
|
144 | 1x |
setdiff(unique(aag_summary$basket_name), smq_levels) |
145 |
)
|
|
146 |
)
|
|
147 |
)
|
|
148 |
} else { |
|
149 | 2x |
all_na_basket_flag <- vapply(df[, baskets], function(x) { |
150 | 6x |
all(is.na(x)) |
151 | 2x |
}, FUN.VALUE = logical(1)) |
152 | 2x |
all_na_basket <- baskets[all_na_basket_flag] |
153 | ||
154 | 2x |
df_long[["SMQ"]] <- factor( |
155 | 2x |
df_long[["SMQ"]], |
156 | 2x |
levels = sort(c(smq_levels, all_na_basket)) |
157 |
)
|
|
158 |
}
|
|
159 | 3x |
formatters::var_labels(df_long) <- var_labels |
160 | 3x |
tibble::tibble(df_long) |
161 |
}
|
1 |
#' Univariate Formula Special Term
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' The special term `univariate` indicate that the model should be fitted individually for
|
|
6 |
#' every variable included in univariate.
|
|
7 |
#'
|
|
8 |
#' @param x A vector of variable name separated by commas.
|
|
9 |
#'
|
|
10 |
#' @return When used within a model formula, produces univariate models for each variable provided.
|
|
11 |
#'
|
|
12 |
#' @details
|
|
13 |
#' If provided alongside with pairwise specification, the model
|
|
14 |
#' `y ~ ARM + univariate(SEX, AGE, RACE)` lead to the study and comparison of the models
|
|
15 |
#' + `y ~ ARM`
|
|
16 |
#' + `y ~ ARM + SEX`
|
|
17 |
#' + `y ~ ARM + AGE`
|
|
18 |
#' + `y ~ ARM + RACE`
|
|
19 |
#'
|
|
20 |
#' @export
|
|
21 |
univariate <- function(x) { |
|
22 | 1x |
structure(x, varname = deparse(substitute(x))) |
23 |
}
|
|
24 | ||
25 |
# Get the right-hand-term of a formula
|
|
26 |
rht <- function(x) { |
|
27 | 4x |
checkmate::assert_formula(x) |
28 | 4x |
y <- as.character(rev(x)[[1]]) |
29 | 4x |
return(y) |
30 |
}
|
|
31 | ||
32 |
#' Hazard Ratio Estimation in Interactions
|
|
33 |
#'
|
|
34 |
#' This function estimates the hazard ratios between arms when an interaction variable is given with
|
|
35 |
#' specific values.
|
|
36 |
#'
|
|
37 |
#' @param variable,given Names of two variable in interaction. We seek the estimation of the levels of `variable`
|
|
38 |
#' given the levels of `given`.
|
|
39 |
#' @param lvl_var,lvl_given corresponding levels has given by `levels`.
|
|
40 |
#' @param mmat A name numeric filled with 0 used as template to obtain the design matrix.
|
|
41 |
#' @param coef Numeric of estimated coefficients.
|
|
42 |
#' @param vcov Variance-covariance matrix of underlying model.
|
|
43 |
#' @param conf_level Single numeric for the confidence level of estimate intervals.
|
|
44 |
#'
|
|
45 |
#' @details Given the cox regression investigating the effect of Arm (A, B, C; reference A)
|
|
46 |
#' and Sex (F, M; reference Female). The model is abbreviated: y ~ Arm + Sex + Arm x Sex.
|
|
47 |
#' The cox regression estimates the coefficients along with a variance-covariance matrix for:
|
|
48 |
#'
|
|
49 |
#' - b1 (arm b), b2 (arm c)
|
|
50 |
#' - b3 (sex m)
|
|
51 |
#' - b4 (arm b: sex m), b5 (arm c: sex m)
|
|
52 |
#'
|
|
53 |
#' Given that I want an estimation of the Hazard Ratio for arm C/sex M, the estimation
|
|
54 |
#' will be given in reference to arm A/Sex M by exp(b2 + b3 + b5)/ exp(b3) = exp(b2 + b5),
|
|
55 |
#' therefore the interaction coefficient is given by b2 + b5 while the standard error is obtained
|
|
56 |
#' as $1.96 * sqrt(Var b2 + Var b5 + 2 * covariance (b2,b5))$ for a confidence level of 0.95.
|
|
57 |
#'
|
|
58 |
#' @return A list of matrix (one per level of variable) with rows corresponding to the combinations of
|
|
59 |
#' `variable` and `given`, with columns:
|
|
60 |
#' * `coef_hat`: Estimation of the coefficient.
|
|
61 |
#' * `coef_se`: Standard error of the estimation.
|
|
62 |
#' * `hr`: Hazard ratio.
|
|
63 |
#' * `lcl, ucl`: Lower/upper confidence limit of the hazard ratio.
|
|
64 |
#'
|
|
65 |
#' @seealso [s_cox_multivariate()].
|
|
66 |
#'
|
|
67 |
#' @examples
|
|
68 |
#' library(dplyr)
|
|
69 |
#' library(survival)
|
|
70 |
#'
|
|
71 |
#' ADSL <- tern_ex_adsl %>%
|
|
72 |
#' filter(SEX %in% c("F", "M"))
|
|
73 |
#'
|
|
74 |
#' adtte <- tern_ex_adtte %>% filter(PARAMCD == "PFS")
|
|
75 |
#' adtte$ARMCD <- droplevels(adtte$ARMCD)
|
|
76 |
#' adtte$SEX <- droplevels(adtte$SEX)
|
|
77 |
#'
|
|
78 |
#' mod <- coxph(
|
|
79 |
#' formula = Surv(time = AVAL, event = 1 - CNSR) ~ (SEX + ARMCD)^2,
|
|
80 |
#' data = adtte
|
|
81 |
#' )
|
|
82 |
#'
|
|
83 |
#' mmat <- stats::model.matrix(mod)[1, ]
|
|
84 |
#' mmat[!mmat == 0] <- 0
|
|
85 |
#'
|
|
86 |
#' @keywords internal
|
|
87 |
estimate_coef <- function(variable, given, |
|
88 |
lvl_var, lvl_given, |
|
89 |
coef,
|
|
90 |
mmat,
|
|
91 |
vcov,
|
|
92 |
conf_level = 0.95) { |
|
93 | 8x |
var_lvl <- paste0(variable, lvl_var[-1]) # [-1]: reference level |
94 | 8x |
giv_lvl <- paste0(given, lvl_given) |
95 | ||
96 | 8x |
design_mat <- expand.grid(variable = var_lvl, given = giv_lvl) |
97 | 8x |
design_mat <- design_mat[order(design_mat$variable, design_mat$given), ] |
98 | 8x |
design_mat <- within( |
99 | 8x |
data = design_mat, |
100 | 8x |
expr = { |
101 | 8x |
inter <- paste0(variable, ":", given) |
102 | 8x |
rev_inter <- paste0(given, ":", variable) |
103 |
}
|
|
104 |
)
|
|
105 | ||
106 | 8x |
split_by_variable <- design_mat$variable |
107 | 8x |
interaction_names <- paste(design_mat$variable, design_mat$given, sep = "/") |
108 | ||
109 | 8x |
design_mat <- apply( |
110 | 8x |
X = design_mat, MARGIN = 1, FUN = function(x) { |
111 | 27x |
mmat[names(mmat) %in% x[-which(names(x) == "given")]] <- 1 |
112 | 27x |
return(mmat) |
113 |
}
|
|
114 |
)
|
|
115 | 8x |
colnames(design_mat) <- interaction_names |
116 | ||
117 | 8x |
betas <- as.matrix(coef) |
118 | ||
119 | 8x |
coef_hat <- t(design_mat) %*% betas |
120 | 8x |
dimnames(coef_hat)[2] <- "coef" |
121 | ||
122 | 8x |
coef_se <- apply(design_mat, 2, function(x) { |
123 | 27x |
vcov_el <- as.logical(x) |
124 | 27x |
y <- vcov[vcov_el, vcov_el] |
125 | 27x |
y <- sum(y) |
126 | 27x |
y <- sqrt(y) |
127 | 27x |
return(y) |
128 |
}) |
|
129 | ||
130 | 8x |
q_norm <- stats::qnorm((1 + conf_level) / 2) |
131 | 8x |
y <- cbind(coef_hat, `se(coef)` = coef_se) |
132 | ||
133 | 8x |
y <- apply(y, 1, function(x) { |
134 | 27x |
x["hr"] <- exp(x["coef"]) |
135 | 27x |
x["lcl"] <- exp(x["coef"] - q_norm * x["se(coef)"]) |
136 | 27x |
x["ucl"] <- exp(x["coef"] + q_norm * x["se(coef)"]) |
137 | ||
138 | 27x |
return(x) |
139 |
}) |
|
140 | ||
141 | 8x |
y <- t(y) |
142 | 8x |
y <- by(y, split_by_variable, identity) |
143 | 8x |
y <- lapply(y, as.matrix) |
144 | ||
145 | 8x |
attr(y, "details") <- paste0( |
146 | 8x |
"Estimations of ", variable, |
147 | 8x |
" hazard ratio given the level of ", given, " compared to ", |
148 | 8x |
variable, " level ", lvl_var[1], "." |
149 |
)
|
|
150 | 8x |
return(y) |
151 |
}
|
|
152 | ||
153 |
#' `tryCatch` around `car::Anova`
|
|
154 |
#'
|
|
155 |
#' Captures warnings when executing [car::Anova].
|
|
156 |
#'
|
|
157 |
#' @inheritParams car::Anova
|
|
158 |
#'
|
|
159 |
#' @return A list with item `aov` for the result of the model and `error_text` for the captured warnings.
|
|
160 |
#'
|
|
161 |
#' @examples
|
|
162 |
#' # `car::Anova` on cox regression model including strata and expected
|
|
163 |
#' # a likelihood ratio test triggers a warning as only `Wald` method is
|
|
164 |
#' # accepted.
|
|
165 |
#'
|
|
166 |
#' library(survival)
|
|
167 |
#'
|
|
168 |
#' mod <- coxph(
|
|
169 |
#' formula = Surv(time = futime, event = fustat) ~ factor(rx) + strata(ecog.ps),
|
|
170 |
#' data = ovarian
|
|
171 |
#' )
|
|
172 |
#'
|
|
173 |
#' @keywords internal
|
|
174 |
try_car_anova <- function(mod, |
|
175 |
test.statistic) { # nolint |
|
176 | 2x |
y <- tryCatch( |
177 | 2x |
withCallingHandlers( |
178 | 2x |
expr = { |
179 | 2x |
warn_text <- c() |
180 | 2x |
list( |
181 | 2x |
aov = car::Anova( |
182 | 2x |
mod,
|
183 | 2x |
test.statistic = test.statistic, |
184 | 2x |
type = "III" |
185 |
),
|
|
186 | 2x |
warn_text = warn_text |
187 |
)
|
|
188 |
},
|
|
189 | 2x |
warning = function(w) { |
190 |
# If a warning is detected it is handled as "w".
|
|
191 | ! |
warn_text <<- trimws(paste0("Warning in `try_car_anova`: ", w)) |
192 | ||
193 |
# A warning is sometimes expected, then, we want to restart
|
|
194 |
# the execution while ignoring the warning.
|
|
195 | ! |
invokeRestart("muffleWarning") |
196 |
}
|
|
197 |
),
|
|
198 | 2x |
finally = { |
199 |
}
|
|
200 |
)
|
|
201 | ||
202 | 2x |
return(y) |
203 |
}
|
|
204 | ||
205 |
#' Fit the Cox Regression Model and `Anova`
|
|
206 |
#'
|
|
207 |
#' The functions allows to derive from the [survival::coxph()] results the effect p.values using [car::Anova()].
|
|
208 |
#' This last package introduces more flexibility to get the effect p.values.
|
|
209 |
#'
|
|
210 |
#' @inheritParams t_coxreg
|
|
211 |
#'
|
|
212 |
#' @return A list with items `mod` (results of [survival::coxph()]), `msum` (result of `summary`) and
|
|
213 |
#' `aov` (result of [car::Anova()]).
|
|
214 |
#'
|
|
215 |
#' @noRd
|
|
216 |
fit_n_aov <- function(formula, |
|
217 |
data = data, |
|
218 |
conf_level = conf_level, |
|
219 |
pval_method = c("wald", "likelihood"), |
|
220 |
...) { |
|
221 | 1x |
pval_method <- match.arg(pval_method) |
222 | ||
223 | 1x |
environment(formula) <- environment() |
224 | 1x |
suppressWarnings({ |
225 |
# We expect some warnings due to coxph which fails strict programming.
|
|
226 | 1x |
mod <- survival::coxph(formula, data = data, ...) |
227 | 1x |
msum <- summary(mod, conf.int = conf_level) |
228 |
}) |
|
229 | ||
230 | 1x |
aov <- try_car_anova( |
231 | 1x |
mod,
|
232 | 1x |
test.statistic = switch(pval_method, |
233 | 1x |
"wald" = "Wald", |
234 | 1x |
"likelihood" = "LR" |
235 |
)
|
|
236 |
)
|
|
237 | ||
238 | 1x |
warn_attr <- aov$warn_text |
239 | ! |
if (!is.null(aov$warn_text)) message(warn_attr) |
240 | ||
241 | 1x |
aov <- aov$aov |
242 | 1x |
y <- list(mod = mod, msum = msum, aov = aov) |
243 | 1x |
attr(y, "message") <- warn_attr |
244 | ||
245 | 1x |
return(y) |
246 |
}
|
|
247 | ||
248 |
# argument_checks
|
|
249 |
check_formula <- function(formula) { |
|
250 | 1x |
if (!(inherits(formula, "formula"))) { |
251 | 1x |
stop("Check `formula`. A formula should resemble `Surv(time = AVAL, event = 1 - CNSR) ~ study_arm(ARMCD)`.") |
252 |
}
|
|
253 | ||
254 | ! |
invisible() |
255 |
}
|
|
256 | ||
257 |
check_covariate_formulas <- function(covariates) { |
|
258 | 1x |
if (!all(vapply(X = covariates, FUN = inherits, what = "formula", FUN.VALUE = TRUE)) || is.null(covariates)) { |
259 | 1x |
stop("Check `covariates`, it should be a list of right-hand-term formulas, e.g. list(Age = ~AGE).") |
260 |
}
|
|
261 | ||
262 | ! |
invisible() |
263 |
}
|
|
264 | ||
265 |
name_covariate_names <- function(covariates) { |
|
266 | 1x |
miss_names <- names(covariates) == "" |
267 | 1x |
no_names <- is.null(names(covariates)) |
268 | ! |
if (any(miss_names)) names(covariates)[miss_names] <- vapply(covariates[miss_names], FUN = rht, FUN.VALUE = "name") |
269 | ! |
if (no_names) names(covariates) <- vapply(covariates, FUN = rht, FUN.VALUE = "name") |
270 | 1x |
return(covariates) |
271 |
}
|
|
272 | ||
273 |
check_increments <- function(increments, covariates) { |
|
274 | 1x |
if (!is.null(increments)) { |
275 | 1x |
covariates <- vapply(covariates, FUN = rht, FUN.VALUE = "name") |
276 | 1x |
lapply( |
277 | 1x |
X = names(increments), FUN = function(x) { |
278 | 3x |
if (!x %in% covariates) { |
279 | 1x |
warning( |
280 | 1x |
paste( |
281 | 1x |
"Check `increments`, the `increment` for ", x, |
282 | 1x |
"doesn't match any names in investigated covariate(s)."
|
283 |
)
|
|
284 |
)
|
|
285 |
}
|
|
286 |
}
|
|
287 |
)
|
|
288 |
}
|
|
289 | ||
290 | 1x |
invisible() |
291 |
}
|
|
292 | ||
293 |
#' Multivariate Cox Model - Summarized Results
|
|
294 |
#'
|
|
295 |
#' Analyses based on multivariate Cox model are usually not performed for the Controlled Substance Reporting or
|
|
296 |
#' regulatory documents but serve exploratory purposes only (e.g., for publication). In practice, the model usually
|
|
297 |
#' includes only the main effects (without interaction terms). It produces the hazard ratio estimates for each of the
|
|
298 |
#' covariates included in the model.
|
|
299 |
#' The analysis follows the same principles (e.g., stratified vs. unstratified analysis and tie handling) as the
|
|
300 |
#' usual Cox model analysis. Since there is usually no pre-specified hypothesis testing for such analysis,
|
|
301 |
#' the p.values need to be interpreted with caution. (**Statistical Analysis of Clinical Trials Data with R**,
|
|
302 |
#' `NEST's bookdown`)
|
|
303 |
#'
|
|
304 |
#' @param formula (`formula`)\cr A formula corresponding to the investigated [survival::Surv()] survival model
|
|
305 |
#' including covariates.
|
|
306 |
#' @param data (`data.frame`)\cr A data frame which includes the variable in formula and covariates.
|
|
307 |
#' @param conf_level (`proportion`)\cr The confidence level for the hazard ratio interval estimations. Default is 0.95.
|
|
308 |
#' @param pval_method (`character`)\cr The method used for the estimation of p-values, should be one of
|
|
309 |
#' `"wald"` (default) or `"likelihood"`.
|
|
310 |
#' @param ... Optional parameters passed to [survival::coxph()]. Can include `ties`, a character string specifying the
|
|
311 |
#' method for tie handling, one of `exact` (default), `efron`, `breslow`.
|
|
312 |
#'
|
|
313 |
#' @return A `list` with elements `mod`, `msum`, `aov`, and `coef_inter`.
|
|
314 |
#'
|
|
315 |
#' @details The output is limited to single effect terms. Work in ongoing for estimation of interaction terms
|
|
316 |
#' but is out of scope as defined by the Global Data Standards Repository
|
|
317 |
#' (**`GDS_Standard_TLG_Specs_Tables_2.doc`**).
|
|
318 |
#'
|
|
319 |
#' @seealso [estimate_coef()].
|
|
320 |
#'
|
|
321 |
#' @examples
|
|
322 |
#' library(dplyr)
|
|
323 |
#'
|
|
324 |
#' adtte <- tern_ex_adtte
|
|
325 |
#' adtte_f <- subset(adtte, PARAMCD == "OS") # _f: filtered
|
|
326 |
#' adtte_f <- filter(
|
|
327 |
#' adtte_f,
|
|
328 |
#' PARAMCD == "OS" &
|
|
329 |
#' SEX %in% c("F", "M") &
|
|
330 |
#' RACE %in% c("ASIAN", "BLACK OR AFRICAN AMERICAN", "WHITE")
|
|
331 |
#' )
|
|
332 |
#' adtte_f$SEX <- droplevels(adtte_f$SEX)
|
|
333 |
#' adtte_f$RACE <- droplevels(adtte_f$RACE)
|
|
334 |
#'
|
|
335 |
#' @keywords internal
|
|
336 |
s_cox_multivariate <- function(formula, data, |
|
337 |
conf_level = 0.95, |
|
338 |
pval_method = c("wald", "likelihood"), |
|
339 |
...) { |
|
340 | 1x |
tf <- stats::terms(formula, specials = c("strata")) |
341 | 1x |
covariates <- rownames(attr(tf, "factors"))[-c(1, unlist(attr(tf, "specials")))] |
342 | 1x |
lapply( |
343 | 1x |
X = covariates, |
344 | 1x |
FUN = function(x) { |
345 | 3x |
if (is.character(data[[x]])) { |
346 | 1x |
data[[x]] <<- as.factor(data[[x]]) |
347 |
}
|
|
348 | 3x |
invisible() |
349 |
}
|
|
350 |
)
|
|
351 | 1x |
pval_method <- match.arg(pval_method) |
352 | ||
353 |
# Results directly exported from environment(fit_n_aov) to environment(s_function_draft)
|
|
354 | 1x |
y <- fit_n_aov( |
355 | 1x |
formula = formula, |
356 | 1x |
data = data, |
357 | 1x |
conf_level = conf_level, |
358 | 1x |
pval_method = pval_method, |
359 |
...
|
|
360 |
)
|
|
361 | 1x |
mod <- y$mod |
362 | 1x |
aov <- y$aov |
363 | 1x |
msum <- y$msum |
364 | 1x |
list2env(as.list(y), environment()) |
365 | ||
366 | 1x |
all_term_labs <- attr(mod$terms, "term.labels") |
367 | 1x |
term_labs <- all_term_labs[which(attr(mod$terms, "order") == 1)] |
368 | 1x |
names(term_labs) <- term_labs |
369 | ||
370 | 1x |
coef_inter <- NULL |
371 | 1x |
if (any(attr(mod$terms, "order") > 1)) { |
372 | 1x |
for_inter <- all_term_labs[attr(mod$terms, "order") > 1] |
373 | 1x |
names(for_inter) <- for_inter |
374 | 1x |
mmat <- stats::model.matrix(mod)[1, ] |
375 | 1x |
mmat[!mmat == 0] <- 0 |
376 | 1x |
mcoef <- stats::coef(mod) |
377 | 1x |
mvcov <- stats::vcov(mod) |
378 | ||
379 | 1x |
estimate_coef_local <- function(variable, given) { |
380 | 6x |
estimate_coef( |
381 | 6x |
variable, given, |
382 | 6x |
coef = mcoef, mmat = mmat, vcov = mvcov, conf_level = conf_level, |
383 | 6x |
lvl_var = levels(data[[variable]]), lvl_given = levels(data[[given]]) |
384 |
)
|
|
385 |
}
|
|
386 | ||
387 | 1x |
coef_inter <- lapply( |
388 | 1x |
for_inter, function(x) { |
389 | 3x |
y <- attr(mod$terms, "factor")[, x] |
390 | 3x |
y <- names(y[y > 0]) |
391 | 3x |
Map(estimate_coef_local, variable = y, given = rev(y)) |
392 |
}
|
|
393 |
)
|
|
394 |
}
|
|
395 | ||
396 | 1x |
list(mod = mod, msum = msum, aov = aov, coef_inter = coef_inter) |
397 |
}
|
1 |
#' Cumulative Counts with Thresholds
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Summarize cumulative counts of a (`numeric`) vector that is less than, less or equal to,
|
|
6 |
#' greater than, or greater or equal to user-specific thresholds.
|
|
7 |
#'
|
|
8 |
#' @inheritParams h_count_cumulative
|
|
9 |
#' @inheritParams argument_convention
|
|
10 |
#'
|
|
11 |
#' @seealso Relevant helper function [h_count_cumulative()], and descriptive function [d_count_cumulative()].
|
|
12 |
#'
|
|
13 |
#' @name count_cumulative
|
|
14 |
NULL
|
|
15 | ||
16 |
#' Helper Function for [s_count_cumulative()]
|
|
17 |
#'
|
|
18 |
#' @description `r lifecycle::badge("stable")`
|
|
19 |
#'
|
|
20 |
#' Helper function to calculate count and fraction of `x` values in the lower or upper tail given a threshold.
|
|
21 |
#'
|
|
22 |
#' @inheritParams argument_convention
|
|
23 |
#' @param threshold (`number`)\cr a cutoff value as threshold to count values of `x`.
|
|
24 |
#' @param lower_tail (`logical`)\cr whether to count lower tail, default is `TRUE`.
|
|
25 |
#' @param include_eq (`logical`)\cr whether to include value equal to the `threshold` in
|
|
26 |
#' count, default is `TRUE`.
|
|
27 |
#' @param .N_col (`count`)\cr denominator for fraction calculation.
|
|
28 |
#'
|
|
29 |
#' @return A named vector with items:
|
|
30 |
#' * `count`: the count of values less than, less or equal to, greater than, or greater or equal to a threshold
|
|
31 |
#' of user specification.
|
|
32 |
#' * `fraction`: the fraction of the count.
|
|
33 |
#'
|
|
34 |
#' @seealso [count_cumulative]
|
|
35 |
#'
|
|
36 |
#' @examples
|
|
37 |
#' set.seed(1, kind = "Mersenne-Twister")
|
|
38 |
#' x <- c(sample(1:10, 10), NA)
|
|
39 |
#' .N_col <- length(x)
|
|
40 |
#' h_count_cumulative(x, 5, .N_col = .N_col)
|
|
41 |
#' h_count_cumulative(x, 5, lower_tail = FALSE, include_eq = FALSE, na.rm = FALSE, .N_col = .N_col)
|
|
42 |
#' h_count_cumulative(x, 0, lower_tail = FALSE, .N_col = .N_col)
|
|
43 |
#' h_count_cumulative(x, 100, lower_tail = FALSE, .N_col = .N_col)
|
|
44 |
#'
|
|
45 |
#' @export
|
|
46 |
h_count_cumulative <- function(x, |
|
47 |
threshold,
|
|
48 |
lower_tail = TRUE, |
|
49 |
include_eq = TRUE, |
|
50 |
na.rm = TRUE, # nolint |
|
51 |
.N_col) { # nolint |
|
52 | 20x |
checkmate::assert_numeric(x) |
53 | 20x |
checkmate::assert_numeric(threshold) |
54 | 20x |
checkmate::assert_numeric(.N_col) |
55 | 20x |
checkmate::assert_flag(lower_tail) |
56 | 20x |
checkmate::assert_flag(include_eq) |
57 | 20x |
checkmate::assert_flag(na.rm) |
58 | ||
59 | 20x |
is_keep <- if (na.rm) !is.na(x) else rep(TRUE, length(x)) |
60 | 20x |
count <- if (lower_tail && include_eq) { |
61 | 7x |
length(x[is_keep & x <= threshold]) |
62 | 20x |
} else if (lower_tail && !include_eq) { |
63 | ! |
length(x[is_keep & x < threshold]) |
64 | 20x |
} else if (!lower_tail && include_eq) { |
65 | 6x |
length(x[is_keep & x >= threshold]) |
66 | 20x |
} else if (!lower_tail && !include_eq) { |
67 | 7x |
length(x[is_keep & x > threshold]) |
68 |
}
|
|
69 | ||
70 | 20x |
result <- c(count = count, fraction = count / .N_col) |
71 | 20x |
result
|
72 |
}
|
|
73 | ||
74 |
#' Description of Cumulative Count
|
|
75 |
#'
|
|
76 |
#' @description `r lifecycle::badge("stable")`
|
|
77 |
#'
|
|
78 |
#' This is a helper function that describes the analysis in [s_count_cumulative()].
|
|
79 |
#'
|
|
80 |
#' @inheritParams h_count_cumulative
|
|
81 |
#'
|
|
82 |
#' @return Labels for [s_count_cumulative()].
|
|
83 |
#'
|
|
84 |
#' @export
|
|
85 |
d_count_cumulative <- function(threshold, lower_tail, include_eq) { |
|
86 | 18x |
checkmate::assert_numeric(threshold) |
87 | 18x |
lg <- if (lower_tail) "<" else ">" |
88 | 18x |
eq <- if (include_eq) "=" else "" |
89 | 18x |
paste0(lg, eq, " ", threshold) |
90 |
}
|
|
91 | ||
92 |
#' @describeIn count_cumulative Statistics function that produces a named list given a numeric vector of thresholds.
|
|
93 |
#'
|
|
94 |
#' @param thresholds (`numeric`)\cr vector of cutoff value for the counts.
|
|
95 |
#'
|
|
96 |
#' @return
|
|
97 |
#' * `s_count_cumulative()` returns a named list of `count_fraction`s: a list with each `thresholds` value as a
|
|
98 |
#' component, each component containing a vector for the count and fraction.
|
|
99 |
#'
|
|
100 |
#' @keywords internal
|
|
101 |
s_count_cumulative <- function(x, |
|
102 |
thresholds,
|
|
103 |
lower_tail = TRUE, |
|
104 |
include_eq = TRUE, |
|
105 |
.N_col, # nolint |
|
106 |
...) { |
|
107 | 5x |
checkmate::assert_numeric(thresholds, min.len = 1, any.missing = FALSE) |
108 | ||
109 | 5x |
count_fraction_list <- Map(function(thres) { |
110 | 10x |
result <- h_count_cumulative(x, thres, lower_tail, include_eq, .N_col = .N_col, ...) |
111 | 10x |
label <- d_count_cumulative(thres, lower_tail, include_eq) |
112 | 10x |
formatters::with_label(result, label) |
113 | 5x |
}, thresholds) |
114 | ||
115 | 5x |
names(count_fraction_list) <- thresholds |
116 | 5x |
list(count_fraction = count_fraction_list) |
117 |
}
|
|
118 | ||
119 |
#' @describeIn count_cumulative Formatted analysis function which is used as `afun`
|
|
120 |
#' in `count_cumulative()`.
|
|
121 |
#'
|
|
122 |
#' @return
|
|
123 |
#' * `a_count_cumulative()` returns the corresponding list with formatted [rtables::CellValue()].
|
|
124 |
#'
|
|
125 |
#' @keywords internal
|
|
126 |
a_count_cumulative <- make_afun( |
|
127 |
s_count_cumulative,
|
|
128 |
.formats = c(count_fraction = format_count_fraction) |
|
129 |
)
|
|
130 | ||
131 |
#' @describeIn count_cumulative Layout-creating function which can take statistics function arguments
|
|
132 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()].
|
|
133 |
#'
|
|
134 |
#' @return
|
|
135 |
#' * `count_cumulative()` returns a layout object suitable for passing to further layouting functions,
|
|
136 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing
|
|
137 |
#' the statistics from `s_count_cumulative()` to the table layout.
|
|
138 |
#'
|
|
139 |
#' @examples
|
|
140 |
#' basic_table() %>%
|
|
141 |
#' split_cols_by("ARM") %>%
|
|
142 |
#' add_colcounts() %>%
|
|
143 |
#' count_cumulative(
|
|
144 |
#' vars = "AGE",
|
|
145 |
#' thresholds = c(40, 60)
|
|
146 |
#' ) %>%
|
|
147 |
#' build_table(tern_ex_adsl)
|
|
148 |
#'
|
|
149 |
#' @export
|
|
150 |
count_cumulative <- function(lyt, |
|
151 |
vars,
|
|
152 |
var_labels = vars, |
|
153 |
show_labels = "visible", |
|
154 |
...,
|
|
155 |
table_names = vars, |
|
156 |
.stats = NULL, |
|
157 |
.formats = NULL, |
|
158 |
.labels = NULL, |
|
159 |
.indent_mods = NULL) { |
|
160 | 2x |
afun <- make_afun( |
161 | 2x |
a_count_cumulative,
|
162 | 2x |
.stats = .stats, |
163 | 2x |
.formats = .formats, |
164 | 2x |
.labels = .labels, |
165 | 2x |
.indent_mods = .indent_mods, |
166 | 2x |
.ungroup_stats = "count_fraction" |
167 |
)
|
|
168 | 2x |
analyze( |
169 | 2x |
lyt,
|
170 | 2x |
vars,
|
171 | 2x |
afun = afun, |
172 | 2x |
table_names = table_names, |
173 | 2x |
var_labels = var_labels, |
174 | 2x |
show_labels = show_labels, |
175 | 2x |
extra_args = list(...) |
176 |
)
|
|
177 |
}
|
1 |
#' Confidence Intervals for a Difference of Binomials
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("experimental")`
|
|
4 |
#'
|
|
5 |
#' Several confidence intervals for the difference between proportions.
|
|
6 |
#'
|
|
7 |
#' @param grp (`factor`)\cr vector assigning observations to one out of two groups
|
|
8 |
#' (e.g. reference and treatment group).
|
|
9 |
#'
|
|
10 |
#' @name desctools_binom
|
|
11 |
NULL
|
|
12 | ||
13 |
#' Recycle List of Parameters
|
|
14 |
#'
|
|
15 |
#' This function recycles all supplied elements to the maximal dimension.
|
|
16 |
#'
|
|
17 |
#' @param ... (`any`)\cr Elements to recycle.
|
|
18 |
#'
|
|
19 |
#' @return A `list`.
|
|
20 |
#'
|
|
21 |
#' @keywords internal
|
|
22 |
#' @noRd
|
|
23 |
h_recycle <- function(...) { |
|
24 | 60x |
lst <- list(...) |
25 | 60x |
maxdim <- max(lengths(lst)) |
26 | 60x |
res <- lapply(lst, rep, length.out = maxdim) |
27 | 60x |
attr(res, "maxdim") <- maxdim |
28 | 60x |
return(res) |
29 |
}
|
|
30 | ||
31 |
#' @describeIn desctools_binom Several confidence intervals for the difference between proportions.
|
|
32 |
#'
|
|
33 |
#' @return A `matrix` of 3 values:
|
|
34 |
#' * `est`: estimate of proportion difference.
|
|
35 |
#' * `lwr.ci`: estimate of lower end of the confidence interval.
|
|
36 |
#' * `upr.ci`: estimate of upper end of the confidence interval.
|
|
37 |
#'
|
|
38 |
#' @keywords internal
|
|
39 |
desctools_binom <- function(x1, n1, x2, n2, conf.level = 0.95, sides = c( # nolint |
|
40 |
"two.sided",
|
|
41 |
"left", "right" |
|
42 |
), method = c( |
|
43 |
"ac", "wald", "waldcc", "score", |
|
44 |
"scorecc", "mn", "mee", "blj", "ha", "hal", "jp" |
|
45 |
)) { |
|
46 | 18x |
if (missing(sides)) { |
47 | 18x |
sides <- match.arg(sides) |
48 |
}
|
|
49 | 18x |
if (missing(method)) { |
50 | 1x |
method <- match.arg(method) |
51 |
}
|
|
52 | 18x |
iBinomDiffCI <- function(x1, n1, x2, n2, conf.level, sides, # nolint |
53 | 18x |
method) { |
54 | 18x |
if (sides != "two.sided") { |
55 | ! |
conf.level <- 1 - 2 * (1 - conf.level) # nolint |
56 |
}
|
|
57 | 18x |
alpha <- 1 - conf.level |
58 | 18x |
kappa <- stats::qnorm(1 - alpha / 2) |
59 | 18x |
p1_hat <- x1 / n1 |
60 | 18x |
p2_hat <- x2 / n2 |
61 | 18x |
est <- p1_hat - p2_hat |
62 | 18x |
switch(method, |
63 | 18x |
wald = { |
64 | 2x |
vd <- p1_hat * (1 - p1_hat) / n1 + p2_hat * (1 - p2_hat) / n2 |
65 | 2x |
term2 <- kappa * sqrt(vd) |
66 | 2x |
ci_lwr <- max(-1, est - term2) |
67 | 2x |
ci_upr <- min(1, est + term2) |
68 |
},
|
|
69 | 18x |
waldcc = { |
70 | 2x |
vd <- p1_hat * (1 - p1_hat) / n1 + p2_hat * (1 - p2_hat) / n2 |
71 | 2x |
term2 <- kappa * sqrt(vd) |
72 | 2x |
term2 <- term2 + 0.5 * (1 / n1 + 1 / n2) |
73 | 2x |
ci_lwr <- max(-1, est - term2) |
74 | 2x |
ci_upr <- min(1, est + term2) |
75 |
},
|
|
76 | 18x |
ac = { |
77 | 2x |
n1 <- n1 + 2 |
78 | 2x |
n2 <- n2 + 2 |
79 | 2x |
x1 <- x1 + 1 |
80 | 2x |
x2 <- x2 + 1 |
81 | 2x |
p1_hat <- x1 / n1 |
82 | 2x |
p2_hat <- x2 / n2 |
83 | 2x |
est1 <- p1_hat - p2_hat |
84 | 2x |
vd <- p1_hat * (1 - p1_hat) / n1 + p2_hat * (1 - p2_hat) / n2 |
85 | 2x |
term2 <- kappa * sqrt(vd) |
86 | 2x |
ci_lwr <- max(-1, est1 - term2) |
87 | 2x |
ci_upr <- min(1, est1 + term2) |
88 |
},
|
|
89 | 18x |
exact = { |
90 | ! |
ci_lwr <- NA |
91 | ! |
ci_upr <- NA |
92 |
},
|
|
93 | 18x |
score = { |
94 | 2x |
w1 <- desctools_binomci( |
95 | 2x |
x = x1, n = n1, conf.level = conf.level, |
96 | 2x |
method = "wilson" |
97 |
)
|
|
98 | 2x |
w2 <- desctools_binomci( |
99 | 2x |
x = x2, n = n2, conf.level = conf.level, |
100 | 2x |
method = "wilson" |
101 |
)
|
|
102 | 2x |
l1 <- w1[2] |
103 | 2x |
u1 <- w1[3] |
104 | 2x |
l2 <- w2[2] |
105 | 2x |
u2 <- w2[3] |
106 | 2x |
ci_lwr <- est - kappa * sqrt(l1 * (1 - l1) / n1 + |
107 | 2x |
u2 * (1 - u2) / n2) |
108 | 2x |
ci_upr <- est + kappa * sqrt(u1 * (1 - u1) / n1 + |
109 | 2x |
l2 * (1 - l2) / n2) |
110 |
},
|
|
111 | 18x |
scorecc = { |
112 | 1x |
w1 <- desctools_binomci( |
113 | 1x |
x = x1, n = n1, conf.level = conf.level, |
114 | 1x |
method = "wilsoncc" |
115 |
)
|
|
116 | 1x |
w2 <- desctools_binomci( |
117 | 1x |
x = x2, n = n2, conf.level = conf.level, |
118 | 1x |
method = "wilsoncc" |
119 |
)
|
|
120 | 1x |
l1 <- w1[2] |
121 | 1x |
u1 <- w1[3] |
122 | 1x |
l2 <- w2[2] |
123 | 1x |
u2 <- w2[3] |
124 | 1x |
ci_lwr <- max(-1, est - sqrt((p1_hat - l1)^2 + |
125 | 1x |
(u2 - p2_hat)^2)) |
126 | 1x |
ci_upr <- min(1, est + sqrt((u1 - p1_hat)^2 + (p2_hat - |
127 | 1x |
l2)^2)) |
128 |
},
|
|
129 | 18x |
mee = { |
130 | 1x |
.score <- function(p1, n1, p2, n2, dif) { |
131 | ! |
if (dif > 1) dif <- 1 |
132 | ! |
if (dif < -1) dif <- -1 |
133 | 24x |
diff <- p1 - p2 - dif |
134 | 24x |
if (abs(diff) == 0) { |
135 | ! |
res <- 0 |
136 |
} else { |
|
137 | 24x |
t <- n2 / n1 |
138 | 24x |
a <- 1 + t |
139 | 24x |
b <- -(1 + t + p1 + t * p2 + dif * (t + 2)) |
140 | 24x |
c <- dif * dif + dif * (2 * p1 + t + 1) + p1 + |
141 | 24x |
t * p2 |
142 | 24x |
d <- -p1 * dif * (1 + dif) |
143 | 24x |
v <- (b / a / 3)^3 - b * c / (6 * a * a) + d / a / 2 |
144 | 24x |
if (abs(v) < .Machine$double.eps) v <- 0 |
145 | 24x |
s <- sqrt((b / a / 3)^2 - c / a / 3) |
146 | 24x |
u <- ifelse(v > 0, 1, -1) * s |
147 | 24x |
w <- (3.141592654 + acos(v / u^3)) / 3 |
148 | 24x |
p1d <- 2 * u * cos(w) - b / a / 3 |
149 | 24x |
p2d <- p1d - dif |
150 | 24x |
n <- n1 + n2 |
151 | 24x |
res <- (p1d * (1 - p1d) / n1 + p2d * (1 - p2d) / n2) |
152 |
}
|
|
153 | 24x |
return(sqrt(res)) |
154 |
}
|
|
155 | 1x |
pval <- function(delta) { |
156 | 24x |
z <- (est - delta) / .score( |
157 | 24x |
p1_hat, n1, p2_hat, |
158 | 24x |
n2, delta |
159 |
)
|
|
160 | 24x |
2 * min(stats::pnorm(z), 1 - stats::pnorm(z)) |
161 |
}
|
|
162 | 1x |
ci_lwr <- max(-1, stats::uniroot(function(delta) { |
163 | 12x |
pval(delta) - |
164 | 12x |
alpha
|
165 | 1x |
}, interval = c(-1 + 1e-06, est - 1e-06))$root) |
166 | 1x |
ci_upr <- min(1, stats::uniroot(function(delta) { |
167 | 12x |
pval(delta) - |
168 | 12x |
alpha
|
169 | 1x |
}, interval = c(est + 1e-06, 1 - 1e-06))$root) |
170 |
},
|
|
171 | 18x |
blj = { |
172 | 1x |
p1_dash <- (x1 + 0.5) / (n1 + 1) |
173 | 1x |
p2_dash <- (x2 + 0.5) / (n2 + 1) |
174 | 1x |
vd <- p1_dash * (1 - p1_dash) / n1 + p2_dash * (1 - |
175 | 1x |
p2_dash) / n2 |
176 | 1x |
term2 <- kappa * sqrt(vd) |
177 | 1x |
est_dash <- p1_dash - p2_dash |
178 | 1x |
ci_lwr <- max(-1, est_dash - term2) |
179 | 1x |
ci_upr <- min(1, est_dash + term2) |
180 |
},
|
|
181 | 18x |
ha = { |
182 | 4x |
term2 <- 1 / (2 * min(n1, n2)) + kappa * sqrt(p1_hat * |
183 | 4x |
(1 - p1_hat) / (n1 - 1) + p2_hat * (1 - p2_hat) / (n2 - |
184 | 4x |
1)) |
185 | 4x |
ci_lwr <- max(-1, est - term2) |
186 | 4x |
ci_upr <- min(1, est + term2) |
187 |
},
|
|
188 | 18x |
mn = { |
189 | 1x |
.conf <- function(x1, n1, x2, n2, z, lower = FALSE) { |
190 | 2x |
p1 <- x1 / n1 |
191 | 2x |
p2 <- x2 / n2 |
192 | 2x |
p_hat <- p1 - p2 |
193 | 2x |
dp <- 1 + ifelse(lower, 1, -1) * p_hat |
194 | 2x |
i <- 1 |
195 | 2x |
while (i <= 50) { |
196 | 46x |
dp <- 0.5 * dp |
197 | 46x |
y <- p_hat + ifelse(lower, -1, 1) * dp |
198 | 46x |
score <- .score(p1, n1, p2, n2, y) |
199 | 46x |
if (score < z) { |
200 | 20x |
p_hat <- y |
201 |
}
|
|
202 | 46x |
if ((dp < 1e-07) || (abs(z - score) < 1e-06)) { |
203 | 2x |
(break)() |
204 |
} else { |
|
205 | 44x |
i <- i + |
206 | 44x |
1
|
207 |
}
|
|
208 |
}
|
|
209 | 2x |
return(y) |
210 |
}
|
|
211 | 1x |
.score <- function(p1, n1, p2, n2, dif) { |
212 | 46x |
diff <- p1 - p2 - dif |
213 | 46x |
if (abs(diff) == 0) { |
214 | ! |
res <- 0 |
215 |
} else { |
|
216 | 46x |
t <- n2 / n1 |
217 | 46x |
a <- 1 + t |
218 | 46x |
b <- -(1 + t + p1 + t * p2 + dif * (t + 2)) |
219 | 46x |
c <- dif * dif + dif * (2 * p1 + t + 1) + p1 + |
220 | 46x |
t * p2 |
221 | 46x |
d <- -p1 * dif * (1 + dif) |
222 | 46x |
v <- (b / a / 3)^3 - b * c / (6 * a * a) + d / a / 2 |
223 | 46x |
s <- sqrt((b / a / 3)^2 - c / a / 3) |
224 | 46x |
u <- ifelse(v > 0, 1, -1) * s |
225 | 46x |
w <- (3.141592654 + acos(v / u^3)) / 3 |
226 | 46x |
p1d <- 2 * u * cos(w) - b / a / 3 |
227 | 46x |
p2d <- p1d - dif |
228 | 46x |
n <- n1 + n2 |
229 | 46x |
var <- (p1d * (1 - p1d) / n1 + p2d * (1 - p2d) / n2) * |
230 | 46x |
n / (n - 1) |
231 | 46x |
res <- diff^2 / var |
232 |
}
|
|
233 | 46x |
return(res) |
234 |
}
|
|
235 | 1x |
z <- stats::qchisq(conf.level, 1) |
236 | 1x |
ci_lwr <- max(-1, .conf(x1, n1, x2, n2, z, TRUE)) |
237 | 1x |
ci_upr <- min(1, .conf(x1, n1, x2, n2, z, FALSE)) |
238 |
},
|
|
239 | 18x |
beal = { |
240 | ! |
a <- p1_hat + p2_hat |
241 | ! |
b <- p1_hat - p2_hat |
242 | ! |
u <- ((1 / n1) + (1 / n2)) / 4 |
243 | ! |
v <- ((1 / n1) - (1 / n2)) / 4 |
244 | ! |
V <- u * ((2 - a) * a - b^2) + 2 * v * (1 - a) * b # nolint |
245 | ! |
z <- stats::qchisq(p = 1 - alpha / 2, df = 1) |
246 | ! |
A <- sqrt(z * (V + z * u^2 * (2 - a) * a + z * v^2 * (1 - a)^2)) # nolint |
247 | ! |
B <- (b + z * v * (1 - a)) / (1 + z * u) # nolint |
248 | ! |
ci_lwr <- max(-1, B - A / (1 + z * u)) |
249 | ! |
ci_upr <- min(1, B + A / (1 + z * u)) |
250 |
},
|
|
251 | 18x |
hal = { |
252 | 1x |
psi <- (p1_hat + p2_hat) / 2 |
253 | 1x |
u <- (1 / n1 + 1 / n2) / 4 |
254 | 1x |
v <- (1 / n1 - 1 / n2) / 4 |
255 | 1x |
z <- kappa |
256 | 1x |
theta <- ((p1_hat - p2_hat) + z^2 * v * (1 - 2 * |
257 | 1x |
psi)) / (1 + z^2 * u) |
258 | 1x |
w <- z / (1 + z^2 * u) * sqrt(u * (4 * psi * (1 - psi) - |
259 | 1x |
(p1_hat - p2_hat)^2) + 2 * v * (1 - 2 * psi) * |
260 | 1x |
(p1_hat - p2_hat) + 4 * z^2 * u^2 * (1 - psi) * |
261 | 1x |
psi + z^2 * v^2 * (1 - 2 * psi)^2) |
262 | 1x |
c(theta + w, theta - w) |
263 | 1x |
ci_lwr <- max(-1, theta - w) |
264 | 1x |
ci_upr <- min(1, theta + w) |
265 |
},
|
|
266 | 18x |
jp = { |
267 | 1x |
psi <- 0.5 * ((x1 + 0.5) / (n1 + 1) + (x2 + 0.5) / (n2 + |
268 | 1x |
1)) |
269 | 1x |
u <- (1 / n1 + 1 / n2) / 4 |
270 | 1x |
v <- (1 / n1 - 1 / n2) / 4 |
271 | 1x |
z <- kappa |
272 | 1x |
theta <- ((p1_hat - p2_hat) + z^2 * v * (1 - 2 * |
273 | 1x |
psi)) / (1 + z^2 * u) |
274 | 1x |
w <- z / (1 + z^2 * u) * sqrt(u * (4 * psi * (1 - psi) - |
275 | 1x |
(p1_hat - p2_hat)^2) + 2 * v * (1 - 2 * psi) * |
276 | 1x |
(p1_hat - p2_hat) + 4 * z^2 * u^2 * (1 - psi) * |
277 | 1x |
psi + z^2 * v^2 * (1 - 2 * psi)^2) |
278 | 1x |
c(theta + w, theta - w) |
279 | 1x |
ci_lwr <- max(-1, theta - w) |
280 | 1x |
ci_upr <- min(1, theta + w) |
281 |
},
|
|
282 |
)
|
|
283 | 18x |
ci <- c( |
284 | 18x |
est = est, lwr.ci = min(ci_lwr, ci_upr), |
285 | 18x |
upr.ci = max(ci_lwr, ci_upr) |
286 |
)
|
|
287 | 18x |
if (sides == "left") { |
288 | ! |
ci[3] <- 1 |
289 | 18x |
} else if (sides == "right") { |
290 | ! |
ci[2] <- -1 |
291 |
}
|
|
292 | 18x |
return(ci) |
293 |
}
|
|
294 | 18x |
method <- match.arg(arg = method, several.ok = TRUE) |
295 | 18x |
sides <- match.arg(arg = sides, several.ok = TRUE) |
296 | 18x |
lst <- h_recycle( |
297 | 18x |
x1 = x1, n1 = n1, x2 = x2, n2 = n2, conf.level = conf.level, |
298 | 18x |
sides = sides, method = method |
299 |
)
|
|
300 | 18x |
res <- t(sapply(1:attr(lst, "maxdim"), function(i) { |
301 | 18x |
iBinomDiffCI( |
302 | 18x |
x1 = lst$x1[i], |
303 | 18x |
n1 = lst$n1[i], x2 = lst$x2[i], n2 = lst$n2[i], conf.level = lst$conf.level[i], |
304 | 18x |
sides = lst$sides[i], method = lst$method[i] |
305 |
)
|
|
306 |
})) |
|
307 | 18x |
lgn <- h_recycle(x1 = if (is.null(names(x1))) { |
308 | 18x |
paste("x1", seq_along(x1), sep = ".") |
309 |
} else { |
|
310 | ! |
names(x1) |
311 | 18x |
}, n1 = if (is.null(names(n1))) { |
312 | 18x |
paste("n1", seq_along(n1), sep = ".") |
313 |
} else { |
|
314 | ! |
names(n1) |
315 | 18x |
}, x2 = if (is.null(names(x2))) { |
316 | 18x |
paste("x2", seq_along(x2), sep = ".") |
317 |
} else { |
|
318 | ! |
names(x2) |
319 | 18x |
}, n2 = if (is.null(names(n2))) { |
320 | 18x |
paste("n2", seq_along(n2), sep = ".") |
321 |
} else { |
|
322 | ! |
names(n2) |
323 | 18x |
}, conf.level = conf.level, sides = sides, method = method) |
324 | 18x |
xn <- apply(as.data.frame(lgn[sapply(lgn, function(x) { |
325 | 126x |
length(unique(x)) != |
326 | 126x |
1
|
327 | 18x |
})]), 1, paste, collapse = ":") |
328 | 18x |
rownames(res) <- xn |
329 | 18x |
return(res) |
330 |
}
|
|
331 | ||
332 |
#' @describeIn desctools_binom Compute confidence intervals for binomial proportions.
|
|
333 |
#'
|
|
334 |
#' @param x (`count`)\cr number of successes
|
|
335 |
#' @param n (`count`)\cr number of trials
|
|
336 |
#' @param conf.level (`proportion`)\cr confidence level, defaults to 0.95.
|
|
337 |
#' @param sides (`character`)\cr side of the confidence interval to compute. Must be one of `"two-sided"` (default),
|
|
338 |
#' `"left"`, or `"right"`.
|
|
339 |
#' @param method (`character`)\cr method to use. Can be one out of: `"wald"`, `"wilson"`, `"wilsoncc"`,
|
|
340 |
#' `"agresti-coull"`, `"jeffreys"`, `"modified wilson"`, `"modified jeffreys"`, `"clopper-pearson"`, `"arcsine"`,
|
|
341 |
#' `"logit"`, `"witting"`, `"pratt"`, `"midp"`, `"lik"`, and `"blaker"`.
|
|
342 |
#'
|
|
343 |
#' @return A `matrix` with 3 columns containing:
|
|
344 |
#' * `est`: estimate of proportion difference.
|
|
345 |
#' * `lwr.ci`: lower end of the confidence interval.
|
|
346 |
#' * `upr.ci`: upper end of the confidence interval.
|
|
347 |
#'
|
|
348 |
#' @keywords internal
|
|
349 |
desctools_binomci <- function(x, |
|
350 |
n,
|
|
351 |
conf.level = 0.95, # nolint |
|
352 |
sides = c("two.sided", "left", "right"), |
|
353 |
method = c( |
|
354 |
"wilson", "wald", "waldcc", "agresti-coull", |
|
355 |
"jeffreys", "modified wilson", "wilsoncc", "modified jeffreys", |
|
356 |
"clopper-pearson", "arcsine", "logit", "witting", "pratt", |
|
357 |
"midp", "lik", "blaker" |
|
358 |
),
|
|
359 |
rand = 123, |
|
360 |
tol = 1e-05) { |
|
361 | 24x |
if (missing(method)) { |
362 | 1x |
method <- "wilson" |
363 |
}
|
|
364 | 24x |
if (missing(sides)) { |
365 | 23x |
sides <- "two.sided" |
366 |
}
|
|
367 | 24x |
iBinomCI <- function(x, n, conf.level = 0.95, sides = c( # nolint |
368 | 24x |
"two.sided",
|
369 | 24x |
"left", "right" |
370 | 24x |
), method = c( |
371 | 24x |
"wilson", "wilsoncc", "wald", |
372 | 24x |
"waldcc", "agresti-coull", "jeffreys", "modified wilson", |
373 | 24x |
"modified jeffreys", "clopper-pearson", "arcsine", "logit", |
374 | 24x |
"witting", "pratt", "midp", "lik", "blaker" |
375 | 24x |
), rand = 123, |
376 | 24x |
tol = 1e-05) { |
377 | 24x |
if (length(x) != 1) { |
378 | ! |
stop("'x' has to be of length 1 (number of successes)") |
379 |
}
|
|
380 | 24x |
if (length(n) != 1) { |
381 | ! |
stop("'n' has to be of length 1 (number of trials)") |
382 |
}
|
|
383 | 24x |
if (length(conf.level) != 1) { |
384 | ! |
stop("'conf.level' has to be of length 1 (confidence level)") |
385 |
}
|
|
386 | 24x |
if (conf.level < 0.5 || conf.level > 1) { |
387 | ! |
stop("'conf.level' has to be in [0.5, 1]") |
388 |
}
|
|
389 | 24x |
sides <- match.arg(sides, choices = c( |
390 | 24x |
"two.sided", "left", |
391 | 24x |
"right"
|
392 | 24x |
), several.ok = FALSE) |
393 | 24x |
if (sides != "two.sided") { |
394 | 1x |
conf.level <- 1 - 2 * (1 - conf.level) # nolint |
395 |
}
|
|
396 | 24x |
alpha <- 1 - conf.level |
397 | 24x |
kappa <- stats::qnorm(1 - alpha / 2) |
398 | 24x |
p_hat <- x / n |
399 | 24x |
q_hat <- 1 - p_hat |
400 | 24x |
est <- p_hat |
401 | 24x |
switch(match.arg(arg = method, choices = c( |
402 | 24x |
"wilson",
|
403 | 24x |
"wald", "waldcc", "wilsoncc", "agresti-coull", "jeffreys", |
404 | 24x |
"modified wilson", "modified jeffreys", "clopper-pearson", |
405 | 24x |
"arcsine", "logit", "witting", "pratt", "midp", "lik", |
406 | 24x |
"blaker"
|
407 |
)), |
|
408 | 24x |
wald = { |
409 | 1x |
term2 <- kappa * sqrt(p_hat * q_hat) / sqrt(n) |
410 | 1x |
ci_lwr <- max(0, p_hat - term2) |
411 | 1x |
ci_upr <- min(1, p_hat + term2) |
412 |
},
|
|
413 | 24x |
waldcc = { |
414 | 1x |
term2 <- kappa * sqrt(p_hat * q_hat) / sqrt(n) |
415 | 1x |
term2 <- term2 + 1 / (2 * n) |
416 | 1x |
ci_lwr <- max(0, p_hat - term2) |
417 | 1x |
ci_upr <- min(1, p_hat + term2) |
418 |
},
|
|
419 | 24x |
wilson = { |
420 | 6x |
term1 <- (x + kappa^2 / 2) / (n + kappa^2) |
421 | 6x |
term2 <- kappa * sqrt(n) / (n + kappa^2) * sqrt(p_hat * |
422 | 6x |
q_hat + kappa^2 / (4 * n)) |
423 | 6x |
ci_lwr <- max(0, term1 - term2) |
424 | 6x |
ci_upr <- min(1, term1 + term2) |
425 |
},
|
|
426 | 24x |
wilsoncc = { |
427 | 3x |
lci <- (2 * x + kappa^2 - 1 - kappa * sqrt(kappa^2 - |
428 | 3x |
2 - 1 / n + 4 * p_hat * (n * q_hat + 1))) / (2 * |
429 | 3x |
(n + kappa^2)) |
430 | 3x |
uci <- (2 * x + kappa^2 + 1 + kappa * sqrt(kappa^2 + |
431 | 3x |
2 - 1 / n + 4 * p_hat * (n * q_hat - 1))) / (2 * |
432 | 3x |
(n + kappa^2)) |
433 | 3x |
ci_lwr <- max(0, ifelse(p_hat == 0, 0, lci)) |
434 | 3x |
ci_upr <- min(1, ifelse(p_hat == 1, 1, uci)) |
435 |
},
|
|
436 | 24x |
`agresti-coull` = { |
437 | 1x |
x_tilde <- x + kappa^2 / 2 |
438 | 1x |
n_tilde <- n + kappa^2 |
439 | 1x |
p_tilde <- x_tilde / n_tilde |
440 | 1x |
q_tilde <- 1 - p_tilde |
441 | 1x |
est <- p_tilde |
442 | 1x |
term2 <- kappa * sqrt(p_tilde * q_tilde) / sqrt(n_tilde) |
443 | 1x |
ci_lwr <- max(0, p_tilde - term2) |
444 | 1x |
ci_upr <- min(1, p_tilde + term2) |
445 |
},
|
|
446 | 24x |
jeffreys = { |
447 | 1x |
if (x == 0) { |
448 | ! |
ci_lwr <- 0 |
449 |
} else { |
|
450 | 1x |
ci_lwr <- stats::qbeta( |
451 | 1x |
alpha / 2, |
452 | 1x |
x + 0.5, n - x + 0.5 |
453 |
)
|
|
454 |
}
|
|
455 | 1x |
if (x == n) { |
456 | ! |
ci_upr <- 1 |
457 |
} else { |
|
458 | 1x |
ci_upr <- stats::qbeta(1 - |
459 | 1x |
alpha / 2, x + 0.5, n - x + 0.5) |
460 |
}
|
|
461 |
},
|
|
462 | 24x |
`modified wilson` = { |
463 | 1x |
term1 <- (x + kappa^2 / 2) / (n + kappa^2) |
464 | 1x |
term2 <- kappa * sqrt(n) / (n + kappa^2) * sqrt(p_hat * |
465 | 1x |
q_hat + kappa^2 / (4 * n)) |
466 | 1x |
if ((n <= 50 & x %in% c(1, 2)) | (n >= 51 & x %in% |
467 | 1x |
c(1:3))) { |
468 | ! |
ci_lwr <- 0.5 * stats::qchisq(alpha, 2 * |
469 | ! |
x) / n |
470 |
} else { |
|
471 | 1x |
ci_lwr <- max(0, term1 - term2) |
472 |
}
|
|
473 | 1x |
if ((n <= 50 & x %in% c(n - 1, n - 2)) | (n >= 51 & |
474 | 1x |
x %in% c(n - (1:3)))) { |
475 | ! |
ci_upr <- 1 - 0.5 * stats::qchisq( |
476 | ! |
alpha,
|
477 | ! |
2 * (n - x) |
478 | ! |
) / n |
479 |
} else { |
|
480 | 1x |
ci_upr <- min(1, term1 + |
481 | 1x |
term2) |
482 |
}
|
|
483 |
},
|
|
484 | 24x |
`modified jeffreys` = { |
485 | 1x |
if (x == n) { |
486 | ! |
ci_lwr <- (alpha / 2)^(1 / n) |
487 |
} else { |
|
488 | 1x |
if (x <= 1) { |
489 | ! |
ci_lwr <- 0 |
490 |
} else { |
|
491 | 1x |
ci_lwr <- stats::qbeta( |
492 | 1x |
alpha / 2, |
493 | 1x |
x + 0.5, n - x + 0.5 |
494 |
)
|
|
495 |
}
|
|
496 |
}
|
|
497 | 1x |
if (x == 0) { |
498 | ! |
ci_upr <- 1 - (alpha / 2)^(1 / n) |
499 |
} else { |
|
500 | 1x |
if (x >= n - 1) { |
501 | ! |
ci_upr <- 1 |
502 |
} else { |
|
503 | 1x |
ci_upr <- stats::qbeta(1 - |
504 | 1x |
alpha / 2, x + 0.5, n - x + 0.5) |
505 |
}
|
|
506 |
}
|
|
507 |
},
|
|
508 | 24x |
`clopper-pearson` = { |
509 | 1x |
ci_lwr <- stats::qbeta(alpha / 2, x, n - x + 1) |
510 | 1x |
ci_upr <- stats::qbeta(1 - alpha / 2, x + 1, n - x) |
511 |
},
|
|
512 | 24x |
arcsine = { |
513 | 1x |
p_tilde <- (x + 0.375) / (n + 0.75) |
514 | 1x |
est <- p_tilde |
515 | 1x |
ci_lwr <- sin(asin(sqrt(p_tilde)) - 0.5 * kappa / sqrt(n))^2 |
516 | 1x |
ci_upr <- sin(asin(sqrt(p_tilde)) + 0.5 * kappa / sqrt(n))^2 |
517 |
},
|
|
518 | 24x |
logit = { |
519 | 1x |
lambda_hat <- log(x / (n - x)) |
520 | 1x |
V_hat <- n / (x * (n - x)) # nolint |
521 | 1x |
lambda_lower <- lambda_hat - kappa * sqrt(V_hat) |
522 | 1x |
lambda_upper <- lambda_hat + kappa * sqrt(V_hat) |
523 | 1x |
ci_lwr <- exp(lambda_lower) / (1 + exp(lambda_lower)) |
524 | 1x |
ci_upr <- exp(lambda_upper) / (1 + exp(lambda_upper)) |
525 |
},
|
|
526 | 24x |
witting = { |
527 | 1x |
set.seed(rand) |
528 | 1x |
x_tilde <- x + stats::runif(1, min = 0, max = 1) |
529 | 1x |
pbinom_abscont <- function(q, size, prob) { |
530 | 22x |
v <- trunc(q) |
531 | 22x |
term1 <- stats::pbinom(v - 1, size = size, prob = prob) |
532 | 22x |
term2 <- (q - v) * stats::dbinom(v, size = size, prob = prob) |
533 | 22x |
return(term1 + term2) |
534 |
}
|
|
535 | 1x |
qbinom_abscont <- function(p, size, x) { |
536 | 2x |
fun <- function(prob, size, x, p) { |
537 | 22x |
pbinom_abscont(x, size, prob) - p |
538 |
}
|
|
539 | 2x |
stats::uniroot(fun, |
540 | 2x |
interval = c(0, 1), size = size, |
541 | 2x |
x = x, p = p |
542 | 2x |
)$root |
543 |
}
|
|
544 | 1x |
ci_lwr <- qbinom_abscont(1 - alpha, size = n, x = x_tilde) |
545 | 1x |
ci_upr <- qbinom_abscont(alpha, size = n, x = x_tilde) |
546 |
},
|
|
547 | 24x |
pratt = { |
548 | 1x |
if (x == 0) { |
549 | ! |
ci_lwr <- 0 |
550 | ! |
ci_upr <- 1 - alpha^(1 / n) |
551 | 1x |
} else if (x == 1) { |
552 | ! |
ci_lwr <- 1 - (1 - alpha / 2)^(1 / n) |
553 | ! |
ci_upr <- 1 - (alpha / 2)^(1 / n) |
554 | 1x |
} else if (x == (n - 1)) { |
555 | ! |
ci_lwr <- (alpha / 2)^(1 / n) |
556 | ! |
ci_upr <- (1 - alpha / 2)^(1 / n) |
557 | 1x |
} else if (x == n) { |
558 | ! |
ci_lwr <- alpha^(1 / n) |
559 | ! |
ci_upr <- 1 |
560 |
} else { |
|
561 | 1x |
z <- stats::qnorm(1 - alpha / 2) |
562 | 1x |
A <- ((x + 1) / (n - x))^2 # nolint |
563 | 1x |
B <- 81 * (x + 1) * (n - x) - 9 * n - 8 # nolint |
564 | 1x |
C <- (0 - 3) * z * sqrt(9 * (x + 1) * (n - x) * (9 * n + 5 - z^2) + n + 1) # nolint |
565 | 1x |
D <- 81 * (x + 1)^2 - 9 * (x + 1) * (2 + z^2) + 1 # nolint |
566 | 1x |
E <- 1 + A * ((B + C) / D)^3 # nolint |
567 | 1x |
ci_upr <- 1 / E |
568 | 1x |
A <- (x / (n - x - 1))^2 # nolint |
569 | 1x |
B <- 81 * x * (n - x - 1) - 9 * n - 8 # nolint |
570 | 1x |
C <- 3 * z * sqrt(9 * x * (n - x - 1) * (9 * n + 5 - z^2) + n + 1) # nolint |
571 | 1x |
D <- 81 * x^2 - 9 * x * (2 + z^2) + 1 # nolint |
572 | 1x |
E <- 1 + A * ((B + C) / D)^3 # nolint |
573 | 1x |
ci_lwr <- 1 / E |
574 |
}
|
|
575 |
},
|
|
576 | 24x |
midp = { |
577 | 1x |
f_low <- function(pi, x, n) { |
578 | 12x |
1 / 2 * stats::dbinom(x, size = n, prob = pi) + stats::pbinom(x, |
579 | 12x |
size = n, prob = pi, lower.tail = FALSE |
580 |
) - |
|
581 | 12x |
(1 - conf.level) / 2 |
582 |
}
|
|
583 | 1x |
f_up <- function(pi, x, n) { |
584 | 12x |
1 / 2 * stats::dbinom(x, size = n, prob = pi) + stats::pbinom(x - |
585 | 12x |
1, size = n, prob = pi) - (1 - conf.level) / 2 |
586 |
}
|
|
587 | 1x |
ci_lwr <- 0 |
588 | 1x |
ci_upr <- 1 |
589 | 1x |
if (x != 0) { |
590 | 1x |
ci_lwr <- stats::uniroot(f_low, |
591 | 1x |
interval = c(0, p_hat), |
592 | 1x |
x = x, n = n |
593 | 1x |
)$root |
594 |
}
|
|
595 | 1x |
if (x != n) { |
596 | 1x |
ci_upr <- stats::uniroot(f_up, interval = c( |
597 | 1x |
p_hat,
|
598 | 1x |
1
|
599 | 1x |
), x = x, n = n)$root |
600 |
}
|
|
601 |
},
|
|
602 | 24x |
lik = { |
603 | 2x |
ci_lwr <- 0 |
604 | 2x |
ci_upr <- 1 |
605 | 2x |
z <- stats::qnorm(1 - alpha * 0.5) |
606 | 2x |
tol <- .Machine$double.eps^0.5 |
607 | 2x |
BinDev <- function(y, x, mu, wt, bound = 0, tol = .Machine$double.eps^0.5, # nolint |
608 |
...) { |
|
609 | 40x |
ll_y <- ifelse(y %in% c(0, 1), 0, stats::dbinom(x, wt, |
610 | 40x |
y,
|
611 | 40x |
log = TRUE |
612 |
)) |
|
613 | 40x |
ll_mu <- ifelse(mu %in% c(0, 1), 0, stats::dbinom(x, |
614 | 40x |
wt, mu, |
615 | 40x |
log = TRUE |
616 |
)) |
|
617 | 40x |
res <- ifelse(abs(y - mu) < tol, 0, sign(y - |
618 | 40x |
mu) * sqrt(-2 * (ll_y - ll_mu))) |
619 | 40x |
return(res - bound) |
620 |
}
|
|
621 | 2x |
if (x != 0 && tol < p_hat) { |
622 | 2x |
ci_lwr <- if (BinDev( |
623 | 2x |
tol, x, p_hat, n, -z, |
624 | 2x |
tol
|
625 | 2x |
) <= 0) { |
626 | 2x |
stats::uniroot( |
627 | 2x |
f = BinDev, interval = c(tol, if (p_hat < |
628 | 2x |
tol || p_hat == 1) { |
629 | ! |
1 - tol |
630 |
} else { |
|
631 | 2x |
p_hat
|
632 | 2x |
}), bound = -z, |
633 | 2x |
x = x, mu = p_hat, wt = n |
634 | 2x |
)$root |
635 |
}
|
|
636 |
}
|
|
637 | 2x |
if (x != n && p_hat < (1 - tol)) { |
638 | 2x |
ci_upr <- if (BinDev(y = 1 - tol, x = x, mu = ifelse(p_hat > |
639 | 2x |
1 - tol, tol, p_hat), wt = n, bound = z, tol = tol) < |
640 | 2x |
0) { |
641 | ! |
ci_lwr <- if (BinDev( |
642 | ! |
tol, x, if (p_hat < |
643 | ! |
tol || p_hat == 1) { |
644 | ! |
1 - tol |
645 |
} else { |
|
646 | ! |
p_hat
|
647 | ! |
}, n, |
648 | ! |
-z, tol |
649 | ! |
) <= 0) { |
650 | ! |
stats::uniroot( |
651 | ! |
f = BinDev, interval = c(tol, p_hat), |
652 | ! |
bound = -z, x = x, mu = p_hat, wt = n |
653 | ! |
)$root |
654 |
}
|
|
655 |
} else { |
|
656 | 2x |
stats::uniroot( |
657 | 2x |
f = BinDev, interval = c(if (p_hat > |
658 | 2x |
1 - tol) { |
659 | ! |
tol
|
660 |
} else { |
|
661 | 2x |
p_hat
|
662 | 2x |
}, 1 - tol), bound = z, |
663 | 2x |
x = x, mu = p_hat, wt = n |
664 | 2x |
)$root |
665 |
}
|
|
666 |
}
|
|
667 |
},
|
|
668 | 24x |
blaker = { |
669 | 1x |
acceptbin <- function(x, n, p) { |
670 | 3954x |
p1 <- 1 - stats::pbinom(x - 1, n, p) |
671 | 3954x |
p2 <- stats::pbinom(x, n, p) |
672 | 3954x |
a1 <- p1 + stats::pbinom(stats::qbinom(p1, n, p) - 1, n, p) |
673 | 3954x |
a2 <- p2 + 1 - stats::pbinom( |
674 | 3954x |
stats::qbinom(1 - p2, n, p), n, |
675 | 3954x |
p
|
676 |
)
|
|
677 | 3954x |
return(min(a1, a2)) |
678 |
}
|
|
679 | 1x |
ci_lwr <- 0 |
680 | 1x |
ci_upr <- 1 |
681 | 1x |
if (x != 0) { |
682 | 1x |
ci_lwr <- stats::qbeta((1 - conf.level) / 2, x, n - |
683 | 1x |
x + 1) |
684 | 1x |
while (acceptbin(x, n, ci_lwr + tol) < (1 - |
685 | 1x |
conf.level)) { |
686 | 1976x |
ci_lwr <- ci_lwr + tol |
687 |
}
|
|
688 |
}
|
|
689 | 1x |
if (x != n) { |
690 | 1x |
ci_upr <- stats::qbeta(1 - (1 - conf.level) / 2, x + |
691 | 1x |
1, n - x) |
692 | 1x |
while (acceptbin(x, n, ci_upr - tol) < (1 - |
693 | 1x |
conf.level)) { |
694 | 1976x |
ci_upr <- ci_upr - tol |
695 |
}
|
|
696 |
}
|
|
697 |
}
|
|
698 |
)
|
|
699 | 24x |
ci <- c(est = est, lwr.ci = max(0, ci_lwr), upr.ci = min( |
700 | 24x |
1,
|
701 | 24x |
ci_upr
|
702 |
)) |
|
703 | 24x |
if (sides == "left") { |
704 | 1x |
ci[3] <- 1 |
705 | 23x |
} else if (sides == "right") { |
706 | ! |
ci[2] <- 0 |
707 |
}
|
|
708 | 24x |
return(ci) |
709 |
}
|
|
710 | 24x |
lst <- list( |
711 | 24x |
x = x, n = n, conf.level = conf.level, sides = sides, |
712 | 24x |
method = method, rand = rand |
713 |
)
|
|
714 | 24x |
maxdim <- max(unlist(lapply(lst, length))) |
715 | 24x |
lgp <- lapply(lst, rep, length.out = maxdim) |
716 | 24x |
lgn <- h_recycle(x = if (is.null(names(x))) { |
717 | 24x |
paste("x", seq_along(x), sep = ".") |
718 |
} else { |
|
719 | ! |
names(x) |
720 | 24x |
}, n = if (is.null(names(n))) { |
721 | 24x |
paste("n", seq_along(n), sep = ".") |
722 |
} else { |
|
723 | ! |
names(n) |
724 | 24x |
}, conf.level = conf.level, sides = sides, method = method) |
725 | 24x |
xn <- apply(as.data.frame(lgn[sapply(lgn, function(x) { |
726 | 120x |
length(unique(x)) != |
727 | 120x |
1
|
728 | 24x |
})]), 1, paste, collapse = ":") |
729 | 24x |
res <- t(sapply(1:maxdim, function(i) { |
730 | 24x |
iBinomCI( |
731 | 24x |
x = lgp$x[i], |
732 | 24x |
n = lgp$n[i], conf.level = lgp$conf.level[i], sides = lgp$sides[i], |
733 | 24x |
method = lgp$method[i], rand = lgp$rand[i] |
734 |
)
|
|
735 |
})) |
|
736 | 24x |
colnames(res)[1] <- c("est") |
737 | 24x |
rownames(res) <- xn |
738 | 24x |
return(res) |
739 |
}
|
1 |
#' Re-implemented [range()] Default S3 method for numerical objects
|
|
2 |
#'
|
|
3 |
#' This function returns `c(NA, NA)` instead of `c(-Inf, Inf)` for zero-length data
|
|
4 |
#' without any warnings.
|
|
5 |
#'
|
|
6 |
#' @param x (`numeric`)\cr a sequence of numbers for which the range is computed.
|
|
7 |
#' @param na.rm (`logical`)\cr indicating if `NA` should be omitted.
|
|
8 |
#' @param finite (`logical`)\cr indicating if non-finite elements should be removed.
|
|
9 |
#'
|
|
10 |
#' @return A 2-element vector of class `numeric`.
|
|
11 |
#'
|
|
12 |
#' @keywords internal
|
|
13 |
range_noinf <- function(x, na.rm = FALSE, finite = FALSE) { # nolint |
|
14 | ||
15 | 733x |
checkmate::assert_numeric(x) |
16 | ||
17 | 733x |
if (finite) { |
18 | 24x |
x <- x[is.finite(x)] # removes NAs too |
19 | 709x |
} else if (na.rm) { |
20 | 468x |
x <- x[!is.na(x)] |
21 |
}
|
|
22 | ||
23 | 733x |
if (length(x) == 0) { |
24 | 47x |
rval <- c(NA, NA) |
25 | 47x |
mode(rval) <- typeof(x) |
26 |
} else { |
|
27 | 686x |
rval <- c(min(x, na.rm = FALSE), max(x, na.rm = FALSE)) |
28 |
}
|
|
29 | ||
30 | 733x |
return(rval) |
31 |
}
|
|
32 | ||
33 |
#' Utility function to create label for confidence interval
|
|
34 |
#'
|
|
35 |
#' @description `r lifecycle::badge("stable")`
|
|
36 |
#'
|
|
37 |
#' @inheritParams argument_convention
|
|
38 |
#'
|
|
39 |
#' @return A `string`.
|
|
40 |
#'
|
|
41 |
#' @export
|
|
42 |
f_conf_level <- function(conf_level) { |
|
43 | 998x |
assert_proportion_value(conf_level) |
44 | 996x |
paste0(conf_level * 100, "% CI") |
45 |
}
|
|
46 | ||
47 |
#' Utility function to create label for p-value
|
|
48 |
#'
|
|
49 |
#' @description `r lifecycle::badge("stable")`
|
|
50 |
#'
|
|
51 |
#' @param test_mean (`number`)\cr mean value to test under the null hypothesis.
|
|
52 |
#'
|
|
53 |
#' @return A `string`.
|
|
54 |
#'
|
|
55 |
#' @export
|
|
56 |
f_pval <- function(test_mean) { |
|
57 | 232x |
checkmate::assert_numeric(test_mean, len = 1) |
58 | 230x |
paste0("p-value (H0: mean = ", test_mean, ")") |
59 |
}
|
|
60 | ||
61 |
#' Utility function to return a named list of covariate names.
|
|
62 |
#'
|
|
63 |
#' @param covariates (`character`)\cr a vector that can contain single variable names (such as
|
|
64 |
#' `"X1"`), and/or interaction terms indicated by `"X1 * X2"`.
|
|
65 |
#'
|
|
66 |
#' @return A named `list` of `character` vector.
|
|
67 |
#'
|
|
68 |
#' @keywords internal
|
|
69 |
get_covariates <- function(covariates) { |
|
70 | 14x |
checkmate::assert_character(covariates) |
71 | 12x |
cov_vars <- unique(trimws(unlist(strsplit(covariates, "\\*")))) |
72 | 12x |
stats::setNames(as.list(cov_vars), cov_vars) |
73 |
}
|
|
74 | ||
75 |
#' Replicate Entries of a Vector if Required
|
|
76 |
#'
|
|
77 |
#' @description `r lifecycle::badge("stable")`
|
|
78 |
#'
|
|
79 |
#' Replicate entries of a vector if required.
|
|
80 |
#'
|
|
81 |
#' @inheritParams argument_convention
|
|
82 |
#' @param n (`count`)\cr how many entries we need.
|
|
83 |
#'
|
|
84 |
#' @return `x` if it has the required length already or is `NULL`,
|
|
85 |
#' otherwise if it is scalar the replicated version of it with `n` entries.
|
|
86 |
#'
|
|
87 |
#' @note This function will fail if `x` is not of length `n` and/or is not a scalar.
|
|
88 |
#'
|
|
89 |
#' @export
|
|
90 |
to_n <- function(x, n) { |
|
91 | 1x |
if (is.null(x)) { |
92 | ! |
NULL
|
93 | 1x |
} else if (length(x) == 1) { |
94 | ! |
rep(x, n) |
95 | 1x |
} else if (length(x) == n) { |
96 | 1x |
x
|
97 |
} else { |
|
98 | ! |
stop("dimension mismatch") |
99 |
}
|
|
100 |
}
|
|
101 | ||
102 |
#' Check Element Dimension
|
|
103 |
#'
|
|
104 |
#' Checks if the elements in `...` have the same dimension.
|
|
105 |
#'
|
|
106 |
#' @param ... (`data.frame`s or `vector`s)\cr any data frames/vectors.
|
|
107 |
#' @param omit_null (`logical`)\cr whether `NULL` elements in `...` should be omitted from the check.
|
|
108 |
#'
|
|
109 |
#' @return A `logical` value.
|
|
110 |
#'
|
|
111 |
#' @keywords internal
|
|
112 |
check_same_n <- function(..., omit_null = TRUE) { |
|
113 | 2x |
dots <- list(...) |
114 | ||
115 | 2x |
n_list <- Map( |
116 | 2x |
function(x, name) { |
117 | 5x |
if (is.null(x)) { |
118 | ! |
if (omit_null) { |
119 | 2x |
NA_integer_
|
120 |
} else { |
|
121 | ! |
stop("arg", name, "is not supposed to be NULL") |
122 |
}
|
|
123 | 5x |
} else if (is.data.frame(x)) { |
124 | ! |
nrow(x) |
125 | 5x |
} else if (is.atomic(x)) { |
126 | 5x |
length(x) |
127 |
} else { |
|
128 | ! |
stop("data structure for ", name, "is currently not supported") |
129 |
}
|
|
130 |
},
|
|
131 | 2x |
dots, names(dots) |
132 |
)
|
|
133 | ||
134 | 2x |
n <- stats::na.omit(unlist(n_list)) |
135 | ||
136 | 2x |
if (length(unique(n)) > 1) { |
137 | ! |
sel <- which(n != n[1]) |
138 | ! |
stop("dimension mismatch:", paste(names(n)[sel], collapse = ", "), " do not have N=", n[1]) |
139 |
}
|
|
140 | ||
141 | 2x |
TRUE
|
142 |
}
|
|
143 | ||
144 |
#' Make Names Without Dots
|
|
145 |
#'
|
|
146 |
#' @param nams (`character`)\cr vector of original names.
|
|
147 |
#'
|
|
148 |
#' @return A `character` `vector` of proper names, which does not use dots in contrast to [make.names()].
|
|
149 |
#'
|
|
150 |
#' @keywords internal
|
|
151 |
make_names <- function(nams) { |
|
152 | 6x |
orig <- make.names(nams) |
153 | 6x |
gsub(".", "", x = orig, fixed = TRUE) |
154 |
}
|
|
155 | ||
156 |
#' Conversion of Months to Days
|
|
157 |
#'
|
|
158 |
#' @description `r lifecycle::badge("stable")`
|
|
159 |
#'
|
|
160 |
#' Conversion of Months to Days. This is an approximative calculation because it
|
|
161 |
#' considers each month as having an average of 30.4375 days.
|
|
162 |
#'
|
|
163 |
#' @param x (`numeric`)\cr time in months.
|
|
164 |
#'
|
|
165 |
#' @return A `numeric` vector with the time in days.
|
|
166 |
#'
|
|
167 |
#' @examples
|
|
168 |
#' x <- c(13.25, 8.15, 1, 2.834)
|
|
169 |
#' month2day(x)
|
|
170 |
#'
|
|
171 |
#' @export
|
|
172 |
month2day <- function(x) { |
|
173 | 1x |
checkmate::assert_numeric(x) |
174 | 1x |
x * 30.4375 |
175 |
}
|
|
176 | ||
177 |
#' Conversion of Days to Months
|
|
178 |
#'
|
|
179 |
#' @param x (`numeric`)\cr time in days.
|
|
180 |
#'
|
|
181 |
#' @return A `numeric` vector with the time in months.
|
|
182 |
#'
|
|
183 |
#' @examples
|
|
184 |
#' x <- c(403, 248, 30, 86)
|
|
185 |
#' day2month(x)
|
|
186 |
#'
|
|
187 |
#' @export
|
|
188 |
day2month <- function(x) { |
|
189 | 15x |
checkmate::assert_numeric(x) |
190 | 15x |
x / 30.4375 |
191 |
}
|
|
192 | ||
193 |
#' Return an empty numeric if all elements are `NA`.
|
|
194 |
#'
|
|
195 |
#' @param x (`numeric`)\cr vector.
|
|
196 |
#'
|
|
197 |
#' @return An empty `numeric` if all elements of `x` are `NA`, otherwise `x`.
|
|
198 |
#'
|
|
199 |
#' @examples
|
|
200 |
#' x <- c(NA, NA, NA)
|
|
201 |
#' # Internal function - empty_vector_if_na
|
|
202 |
#' @keywords internal
|
|
203 |
empty_vector_if_na <- function(x) { |
|
204 | 683x |
if (all(is.na(x))) { |
205 | 220x |
numeric() |
206 |
} else { |
|
207 | 463x |
x
|
208 |
}
|
|
209 |
}
|
|
210 | ||
211 |
#' Combine Two Vectors Element Wise
|
|
212 |
#'
|
|
213 |
#' @param x (`vector`)\cr first vector to combine.
|
|
214 |
#' @param y (`vector`)\cr second vector to combine.
|
|
215 |
#'
|
|
216 |
#' @return A `list` where each element combines corresponding elements of `x` and `y`.
|
|
217 |
#'
|
|
218 |
#' @examples
|
|
219 |
#' combine_vectors(1:3, 4:6)
|
|
220 |
#'
|
|
221 |
#' @export
|
|
222 |
combine_vectors <- function(x, y) { |
|
223 | 49x |
checkmate::assert_vector(x) |
224 | 49x |
checkmate::assert_vector(y, len = length(x)) |
225 | ||
226 | 49x |
result <- lapply(as.data.frame(rbind(x, y)), `c`) |
227 | 49x |
names(result) <- NULL |
228 | 49x |
result
|
229 |
}
|
|
230 | ||
231 |
#' Extract Elements by Name
|
|
232 |
#'
|
|
233 |
#' This utility function extracts elements from a vector `x` by `names`.
|
|
234 |
#' Differences to the standard `[` function are:
|
|
235 |
#'
|
|
236 |
#' - If `x` is `NULL`, then still always `NULL` is returned (same as in base function).
|
|
237 |
#' - If `x` is not `NULL`, then the intersection of its names is made with `names` and those
|
|
238 |
#' elements are returned. That is, `names` which don't appear in `x` are not returned as `NA`s.
|
|
239 |
#'
|
|
240 |
#' @param x (named `vector`)\cr where to extract named elements from.
|
|
241 |
#' @param names (`character`)\cr vector of names to extract.
|
|
242 |
#'
|
|
243 |
#' @return `NULL` if `x` is `NULL`, otherwise the extracted elements from `x`.
|
|
244 |
#'
|
|
245 |
#' @keywords internal
|
|
246 |
extract_by_name <- function(x, names) { |
|
247 | 3425x |
if (is.null(x)) { |
248 | 3001x |
return(NULL) |
249 |
}
|
|
250 | 424x |
checkmate::assert_named(x) |
251 | 424x |
checkmate::assert_character(names) |
252 | 424x |
which_extract <- intersect(names(x), names) |
253 | 424x |
if (length(which_extract) > 0) { |
254 | 204x |
x[which_extract] |
255 |
} else { |
|
256 | 220x |
NULL
|
257 |
}
|
|
258 |
}
|
|
259 | ||
260 |
#' Labels for Adverse Event Baskets
|
|
261 |
#'
|
|
262 |
#' @description `r lifecycle::badge("stable")`
|
|
263 |
#'
|
|
264 |
#' @param aesi (`character`)\cr with standardized `MedDRA` query name (e.g. `SMQzzNAM`) or customized query
|
|
265 |
#' name (e.g. `CQzzNAM`).
|
|
266 |
#' @param scope (`character`)\cr with scope of query (e.g. `SMQzzSC`).
|
|
267 |
#'
|
|
268 |
#' @return A `string` with the standard label for the `AE` basket.
|
|
269 |
#'
|
|
270 |
#' @examples
|
|
271 |
#' adae <- tern_ex_adae
|
|
272 |
#'
|
|
273 |
#' # Standardized query label includes scope.
|
|
274 |
#' aesi_label(adae$SMQ01NAM, scope = adae$SMQ01SC)
|
|
275 |
#'
|
|
276 |
#' # Customized query label.
|
|
277 |
#' aesi_label(adae$CQ01NAM)
|
|
278 |
#'
|
|
279 |
#' @export
|
|
280 |
aesi_label <- function(aesi, scope = NULL) { |
|
281 | 3x |
checkmate::assert_character(aesi) |
282 | 3x |
checkmate::assert_character(scope, null.ok = TRUE) |
283 | 3x |
aesi_label <- obj_label(aesi) |
284 | 3x |
aesi <- sas_na(aesi) |
285 | 3x |
aesi <- unique(aesi)[!is.na(unique(aesi))] |
286 | ||
287 | 3x |
lbl <- if (length(aesi) == 1 && !is.null(scope)) { |
288 | 1x |
scope <- sas_na(scope) |
289 | 1x |
scope <- unique(scope)[!is.na(unique(scope))] |
290 | 1x |
checkmate::assert_string(scope) |
291 | 1x |
paste0(aesi, " (", scope, ")") |
292 | 3x |
} else if (length(aesi) == 1 && is.null(scope)) { |
293 | 1x |
aesi
|
294 |
} else { |
|
295 | 1x |
aesi_label
|
296 |
}
|
|
297 | ||
298 | 3x |
lbl
|
299 |
}
|
|
300 | ||
301 |
#' Indicate Study Arm Variable in Formula
|
|
302 |
#'
|
|
303 |
#' We use `study_arm` to indicate the study arm variable in `tern` formulas.
|
|
304 |
#'
|
|
305 |
#' @param x arm information
|
|
306 |
#'
|
|
307 |
#' @return `x`
|
|
308 |
#'
|
|
309 |
#' @keywords internal
|
|
310 |
study_arm <- function(x) { |
|
311 | ! |
structure(x, varname = deparse(substitute(x))) |
312 |
}
|
|
313 | ||
314 |
#' Smooth Function with Optional Grouping
|
|
315 |
#'
|
|
316 |
#' @description `r lifecycle::badge("stable")`
|
|
317 |
#'
|
|
318 |
#' This produces `loess` smoothed estimates of `y` with Student confidence intervals.
|
|
319 |
#'
|
|
320 |
#' @param df (`data.frame`)\cr data set containing all analysis variables.
|
|
321 |
#' @param x (`character`)\cr value with x column name.
|
|
322 |
#' @param y (`character`)\cr value with y column name.
|
|
323 |
#' @param groups (`character`)\cr vector with optional grouping variables names.
|
|
324 |
#' @param level (`numeric`)\cr level of confidence interval to use (0.95 by default).
|
|
325 |
#'
|
|
326 |
#' @return A `data.frame` with original `x`, smoothed `y`, `ylow`, and `yhigh`, and
|
|
327 |
#' optional `groups` variables formatted as `factor` type.
|
|
328 |
#'
|
|
329 |
#' @export
|
|
330 |
get_smooths <- function(df, x, y, groups = NULL, level = 0.95) { |
|
331 | 5x |
checkmate::assert_data_frame(df) |
332 | 5x |
df_cols <- colnames(df) |
333 | 5x |
checkmate::assert_string(x) |
334 | 5x |
checkmate::assert_subset(x, df_cols) |
335 | 5x |
checkmate::assert_numeric(df[[x]]) |
336 | 5x |
checkmate::assert_string(y) |
337 | 5x |
checkmate::assert_subset(y, df_cols) |
338 | 5x |
checkmate::assert_numeric(df[[y]]) |
339 | ||
340 | 5x |
if (!is.null(groups)) { |
341 | 4x |
checkmate::assert_character(groups) |
342 | 4x |
checkmate::assert_subset(groups, df_cols) |
343 |
}
|
|
344 | ||
345 | 5x |
smooths <- function(x, y) { |
346 | 18x |
stats::predict(stats::loess(y ~ x), se = TRUE) |
347 |
}
|
|
348 | ||
349 | 5x |
if (!is.null(groups)) { |
350 | 4x |
cc <- stats::complete.cases(df[c(x, y, groups)]) |
351 | 4x |
df_c <- df[cc, c(x, y, groups)] |
352 | 4x |
df_c_ordered <- df_c[do.call("order", as.list(df_c[, groups, drop = FALSE])), , drop = FALSE] |
353 | 4x |
df_c_g <- data.frame(Map(as.factor, df_c_ordered[groups])) |
354 | ||
355 | 4x |
df_smooth_raw <- |
356 | 4x |
by(df_c_ordered, df_c_g, function(d) { |
357 | 17x |
plx <- smooths(d[[x]], d[[y]]) |
358 | 17x |
data.frame( |
359 | 17x |
x = d[[x]], |
360 | 17x |
y = plx$fit, |
361 | 17x |
ylow = plx$fit - stats::qt(level, plx$df) * plx$se, |
362 | 17x |
yhigh = plx$fit + stats::qt(level, plx$df) * plx$se |
363 |
)
|
|
364 |
}) |
|
365 | ||
366 | 4x |
df_smooth <- do.call(rbind, df_smooth_raw) |
367 | 4x |
df_smooth[groups] <- df_c_g |
368 | ||
369 | 4x |
df_smooth
|
370 |
} else { |
|
371 | 1x |
cc <- stats::complete.cases(df[c(x, y)]) |
372 | 1x |
df_c <- df[cc, ] |
373 | 1x |
plx <- smooths(df_c[[x]], df_c[[y]]) |
374 | ||
375 | 1x |
df_smooth <- data.frame( |
376 | 1x |
x = df_c[[x]], |
377 | 1x |
y = plx$fit, |
378 | 1x |
ylow = plx$fit - stats::qt(level, plx$df) * plx$se, |
379 | 1x |
yhigh = plx$fit + stats::qt(level, plx$df) * plx$se |
380 |
)
|
|
381 | ||
382 | 1x |
df_smooth
|
383 |
}
|
|
384 |
}
|
|
385 | ||
386 |
#' Number of Available (Non-Missing Entries) in a Vector
|
|
387 |
#'
|
|
388 |
#' Small utility function for better readability.
|
|
389 |
#'
|
|
390 |
#' @param x (`any`)\cr vector in which to count non-missing values.
|
|
391 |
#'
|
|
392 |
#' @return Number of non-missing values.
|
|
393 |
#'
|
|
394 |
#' @keywords internal
|
|
395 |
n_available <- function(x) { |
|
396 | 196x |
sum(!is.na(x)) |
397 |
}
|
|
398 | ||
399 |
#' Reapply Variable Labels
|
|
400 |
#'
|
|
401 |
#' This is a helper function that is used in tests.
|
|
402 |
#'
|
|
403 |
#' @param x (`vector`)\cr vector of elements that needs new labels.
|
|
404 |
#' @param varlabels (`character`)\cr vector of labels for `x`.
|
|
405 |
#' @param ... further parameters to be added to the list.
|
|
406 |
#'
|
|
407 |
#' @return `x` with variable labels reapplied.
|
|
408 |
#'
|
|
409 |
#' @export
|
|
410 |
reapply_varlabels <- function(x, varlabels, ...) { |
|
411 | 10x |
named_labels <- c(as.list(varlabels), list(...)) |
412 | 10x |
formatters::var_labels(x)[names(named_labels)] <- as.character(named_labels) |
413 | 10x |
x
|
414 |
}
|
|
415 | ||
416 |
# Wrapper function of survival::clogit so that when model fitting failed, a more useful message would show
|
|
417 |
clogit_with_tryCatch <- function(formula, data, ...) { # nolint |
|
418 | 30x |
tryCatch( |
419 | 30x |
survival::clogit(formula = formula, data = data, ...), |
420 | 30x |
error = function(e) stop("model not built successfully with survival::clogit") |
421 |
)
|
|
422 |
}
|
1 |
#' Control Function for Descriptive Statistics
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Sets a list of parameters for summaries of descriptive statistics. Typically used internally to specify
|
|
6 |
#' details for [s_summary()].
|
|
7 |
#'
|
|
8 |
#' @inheritParams argument_convention
|
|
9 |
#' @param quantiles (`numeric`)\cr of length two to specify the quantiles to calculate.
|
|
10 |
#' @param quantile_type (`numeric`)\cr between 1 and 9 selecting quantile algorithms to be used.
|
|
11 |
#' Default is set to 2 as this matches the default quantile algorithm in SAS `proc univariate` set by `QNTLDEF=5`.
|
|
12 |
#' This differs from R's default. See more about `type` in [stats::quantile()].
|
|
13 |
#' @param test_mean (`numeric`)\cr to test against the mean under the null hypothesis when calculating p-value.
|
|
14 |
#'
|
|
15 |
#' @return A list of components with the same names as the arguments.
|
|
16 |
#'
|
|
17 |
#' @export
|
|
18 |
control_summarize_vars <- function(conf_level = 0.95, |
|
19 |
quantiles = c(0.25, 0.75), |
|
20 |
quantile_type = 2, |
|
21 |
test_mean = 0) { |
|
22 | 199x |
checkmate::assert_vector(quantiles, len = 2) |
23 | 199x |
checkmate::assert_int(quantile_type, lower = 1, upper = 9) |
24 | 199x |
checkmate::assert_numeric(test_mean) |
25 | 199x |
nullo <- lapply(quantiles, assert_proportion_value) |
26 | 198x |
assert_proportion_value(conf_level) |
27 | 197x |
list(conf_level = conf_level, quantiles = quantiles, quantile_type = quantile_type, test_mean = test_mean) |
28 |
}
|
|
29 | ||
30 |
#' Format Function for Descriptive Statistics
|
|
31 |
#'
|
|
32 |
#' Returns format patterns for descriptive statistics. The format is understood by the `rtables`.
|
|
33 |
#'
|
|
34 |
#' @param type (`string`)\cr choice of a summary data type. Only `counts` and `numeric` types are currently supported.
|
|
35 |
#'
|
|
36 |
#' @return A named `vector` of default statistic formats for the given data type.
|
|
37 |
#'
|
|
38 |
#' @keywords internal
|
|
39 |
summary_formats <- function(type = "numeric") { |
|
40 | 13x |
if (type == "counts") { |
41 | 6x |
c( |
42 | 6x |
n = "xx.", |
43 | 6x |
count = "xx.", |
44 | 6x |
count_fraction = format_count_fraction, |
45 | 6x |
n_blq = "xx." |
46 |
)
|
|
47 |
} else { |
|
48 | 7x |
c( |
49 | 7x |
n = "xx.", |
50 | 7x |
sum = "xx.x", |
51 | 7x |
mean = "xx.x", |
52 | 7x |
sd = "xx.x", |
53 | 7x |
se = "xx.x", |
54 | 7x |
mean_sd = "xx.x (xx.x)", |
55 | 7x |
mean_se = "xx.x (xx.x)", |
56 | 7x |
mean_ci = "(xx.xx, xx.xx)", |
57 | 7x |
mean_sei = "(xx.xx, xx.xx)", |
58 | 7x |
mean_sdi = "(xx.xx, xx.xx)", |
59 | 7x |
mean_pval = "xx.xx", |
60 | 7x |
median = "xx.x", |
61 | 7x |
mad = "xx.x", |
62 | 7x |
median_ci = "(xx.xx, xx.xx)", |
63 | 7x |
quantiles = "xx.x - xx.x", |
64 | 7x |
iqr = "xx.x", |
65 | 7x |
range = "xx.x - xx.x", |
66 | 7x |
cv = "xx.x", |
67 | 7x |
min = "xx.x", |
68 | 7x |
max = "xx.x", |
69 | 7x |
median_range = "xx.x (xx.x - xx.x)", |
70 | 7x |
geom_mean = "xx.x", |
71 | 7x |
geom_cv = "xx.x" |
72 |
)
|
|
73 |
}
|
|
74 |
}
|
|
75 | ||
76 |
#' Label Function for Descriptive Statistics
|
|
77 |
#'
|
|
78 |
#' Returns labels of descriptive statistics for numeric variables.
|
|
79 |
#'
|
|
80 |
#' @return A named `vector` of default statistic labels.
|
|
81 |
#'
|
|
82 |
#' @keywords internal
|
|
83 |
summary_labels <- function() { |
|
84 | 1x |
c( |
85 | 1x |
mean = "Mean", |
86 | 1x |
sum = "Sum", |
87 | 1x |
sd = "SD", |
88 | 1x |
se = "SE", |
89 | 1x |
mean_sd = "Mean (SD)", |
90 | 1x |
mean_se = "Mean (SE)", |
91 | 1x |
median = "Median", |
92 | 1x |
mad = "Median Absolute Deviation", |
93 | 1x |
iqr = "IQR", |
94 | 1x |
range = "Min - Max", |
95 | 1x |
median_range = "Median (Min - Max)", |
96 | 1x |
cv = "CV (%)", |
97 | 1x |
min = "Minimum", |
98 | 1x |
max = "Maximum", |
99 | 1x |
geom_mean = "Geometric Mean", |
100 | 1x |
geom_cv = "CV % Geometric Mean", |
101 | 1x |
n = "n" |
102 |
)
|
|
103 |
}
|
|
104 | ||
105 |
#' Summarize Variables
|
|
106 |
#'
|
|
107 |
#' @description `r lifecycle::badge("stable")`
|
|
108 |
#'
|
|
109 |
#' We use the S3 generic function [s_summary()] to implement summaries for different `x` objects. This
|
|
110 |
#' is used as a statistics function in combination with the analyze function [summarize_vars()].
|
|
111 |
#'
|
|
112 |
#' @inheritParams argument_convention
|
|
113 |
#'
|
|
114 |
#' @name summarize_variables
|
|
115 |
NULL
|
|
116 | ||
117 |
#' @describeIn summarize_variables S3 generic function to produces a variable summary.
|
|
118 |
#'
|
|
119 |
#' @return
|
|
120 |
#' * `s_summary()` returns different statistics depending on the class of `x`.
|
|
121 |
#'
|
|
122 |
#' @export
|
|
123 |
s_summary <- function(x, |
|
124 |
na.rm = TRUE, # nolint |
|
125 |
denom,
|
|
126 |
.N_row, # nolint |
|
127 |
.N_col, # nolint |
|
128 |
.var,
|
|
129 |
...) { |
|
130 | 264x |
checkmate::assert_flag(na.rm) |
131 | 264x |
UseMethod("s_summary", x) |
132 |
}
|
|
133 | ||
134 |
#' @describeIn summarize_variables Method for `numeric` class.
|
|
135 |
#'
|
|
136 |
#' @param control (`list`)\cr parameters for descriptive statistics details, specified by using
|
|
137 |
#' the helper function [control_summarize_vars()]. Some possible parameter options are:
|
|
138 |
#' * `conf_level` (`proportion`)\cr confidence level of the interval for mean and median.
|
|
139 |
#' * `quantiles` (`numeric`)\cr vector of length two to specify the quantiles.
|
|
140 |
#' * `quantile_type` (`numeric`)\cr between 1 and 9 selecting quantile algorithms to be used.
|
|
141 |
#' See more about `type` in [stats::quantile()].
|
|
142 |
#' * `test_mean` (`numeric`)\cr value to test against the mean under the null hypothesis when calculating p-value.
|
|
143 |
#'
|
|
144 |
#' @return
|
|
145 |
#' * If `x` is of class `numeric`, returns a `list` with the following named `numeric` items:
|
|
146 |
#' * `n`: The [length()] of `x`.
|
|
147 |
#' * `sum`: The [sum()] of `x`.
|
|
148 |
#' * `mean`: The [mean()] of `x`.
|
|
149 |
#' * `sd`: The [stats::sd()] of `x`.
|
|
150 |
#' * `se`: The standard error of `x` mean, i.e.: (`sd(x) / sqrt(length(x))`).
|
|
151 |
#' * `mean_sd`: The [mean()] and [stats::sd()] of `x`.
|
|
152 |
#' * `mean_se`: The [mean()] of `x` and its standard error (see above).
|
|
153 |
#' * `mean_ci`: The CI for the mean of `x` (from [stat_mean_ci()]).
|
|
154 |
#' * `mean_sei`: The SE interval for the mean of `x`, i.e.: ([mean()] -/+ [stats::sd()] / [sqrt()]).
|
|
155 |
#' * `mean_sdi`: The SD interval for the mean of `x`, i.e.: ([mean()] -/+ [stats::sd()]).
|
|
156 |
#' * `mean_pval`: The two-sided p-value of the mean of `x` (from [stat_mean_pval()]).
|
|
157 |
#' * `median`: The [stats::median()] of `x`.
|
|
158 |
#' * `mad`: The median absolute deviation of `x`, i.e.: ([stats::median()] of `xc`,
|
|
159 |
#' where `xc` = `x` - [stats::median()]).
|
|
160 |
#' * `median_ci`: The CI for the median of `x` (from [stat_median_ci()]).
|
|
161 |
#' * `quantiles`: Two sample quantiles of `x` (from [stats::quantile()]).
|
|
162 |
#' * `iqr`: The [stats::IQR()] of `x`.
|
|
163 |
#' * `range`: The [range_noinf()] of `x`.
|
|
164 |
#' * `min`: The [max()] of `x`.
|
|
165 |
#' * `max`: The [min()] of `x`.
|
|
166 |
#' * `median_range`: The [median()] and [range_noinf()] of `x`.
|
|
167 |
#' * `cv`: The coefficient of variation of `x`, i.e.: ([stats::sd()] / [mean()] * 100).
|
|
168 |
#' * `geom_mean`: The geometric mean of `x`, i.e.: (`exp(mean(log(x)))`).
|
|
169 |
#' * `geom_cv`: The geometric coefficient of variation of `x`, i.e.: (`sqrt(exp(sd(log(x)) ^ 2) - 1) * 100`).
|
|
170 |
#'
|
|
171 |
#' @note
|
|
172 |
#' * If `x` is an empty vector, `NA` is returned. This is the expected feature so as to return `rcell` content in
|
|
173 |
#' `rtables` when the intersection of a column and a row delimits an empty data selection.
|
|
174 |
#' * When the `mean` function is applied to an empty vector, `NA` will be returned instead of `NaN`, the latter
|
|
175 |
#' being standard behavior in R.
|
|
176 |
#'
|
|
177 |
#' @method s_summary numeric
|
|
178 |
#'
|
|
179 |
#' @examples
|
|
180 |
#' # `s_summary.numeric`
|
|
181 |
#'
|
|
182 |
#' ## Basic usage: empty numeric returns NA-filled items.
|
|
183 |
#' s_summary(numeric())
|
|
184 |
#'
|
|
185 |
#' ## Management of NA values.
|
|
186 |
#' x <- c(NA_real_, 1)
|
|
187 |
#' s_summary(x, na.rm = TRUE)
|
|
188 |
#' s_summary(x, na.rm = FALSE)
|
|
189 |
#'
|
|
190 |
#' x <- c(NA_real_, 1, 2)
|
|
191 |
#' s_summary(x, stats = NULL)
|
|
192 |
#'
|
|
193 |
#' ## Benefits in `rtables` contructions:
|
|
194 |
#' require(rtables)
|
|
195 |
#' dta_test <- data.frame(
|
|
196 |
#' Group = rep(LETTERS[1:3], each = 2),
|
|
197 |
#' sub_group = rep(letters[1:2], each = 3),
|
|
198 |
#' x = 1:6
|
|
199 |
#' )
|
|
200 |
#'
|
|
201 |
#' ## The summary obtained in with `rtables`:
|
|
202 |
#' basic_table() %>%
|
|
203 |
#' split_cols_by(var = "Group") %>%
|
|
204 |
#' split_rows_by(var = "sub_group") %>%
|
|
205 |
#' analyze(vars = "x", afun = s_summary) %>%
|
|
206 |
#' build_table(df = dta_test)
|
|
207 |
#'
|
|
208 |
#' ## By comparison with `lapply`:
|
|
209 |
#' X <- split(dta_test, f = with(dta_test, interaction(Group, sub_group)))
|
|
210 |
#' lapply(X, function(x) s_summary(x$x))
|
|
211 |
#'
|
|
212 |
#' @export
|
|
213 |
s_summary.numeric <- function(x, |
|
214 |
na.rm = TRUE, # nolint |
|
215 |
denom,
|
|
216 |
.N_row, # nolint |
|
217 |
.N_col, # nolint |
|
218 |
.var,
|
|
219 |
control = control_summarize_vars(), |
|
220 |
...) { |
|
221 | 175x |
checkmate::assert_numeric(x) |
222 | ||
223 | 175x |
if (na.rm) { |
224 | 175x |
x <- x[!is.na(x)] |
225 |
}
|
|
226 | ||
227 | 175x |
y <- list() |
228 | ||
229 | 175x |
y$n <- c("n" = length(x)) |
230 | ||
231 | 175x |
y$sum <- c("sum" = ifelse(length(x) == 0, NA_real_, sum(x, na.rm = FALSE))) |
232 | ||
233 | 175x |
y$mean <- c("mean" = ifelse(length(x) == 0, NA_real_, mean(x, na.rm = FALSE))) |
234 | ||
235 | 175x |
y$sd <- c("sd" = stats::sd(x, na.rm = FALSE)) |
236 | ||
237 | 175x |
y$se <- c("se" = stats::sd(x, na.rm = FALSE) / sqrt(length(stats::na.omit(x)))) |
238 | ||
239 | 175x |
y$mean_sd <- c(y$mean, "sd" = stats::sd(x, na.rm = FALSE)) |
240 | ||
241 | 175x |
y$mean_se <- c(y$mean, y$se) |
242 | ||
243 | 175x |
mean_ci <- stat_mean_ci(x, conf_level = control$conf_level, na.rm = FALSE, gg_helper = FALSE) |
244 | 175x |
y$mean_ci <- formatters::with_label(mean_ci, paste("Mean", f_conf_level(control$conf_level))) |
245 | ||
246 | 175x |
mean_sei <- y$mean[[1]] + c(-1, 1) * stats::sd(x, na.rm = FALSE) / sqrt(y$n) |
247 | 175x |
names(mean_sei) <- c("mean_sei_lwr", "mean_sei_upr") |
248 | 175x |
y$mean_sei <- formatters::with_label(mean_sei, "Mean -/+ 1xSE") |
249 | ||
250 | 175x |
mean_sdi <- y$mean[[1]] + c(-1, 1) * stats::sd(x, na.rm = FALSE) |
251 | 175x |
names(mean_sdi) <- c("mean_sdi_lwr", "mean_sdi_upr") |
252 | 175x |
y$mean_sdi <- formatters::with_label(mean_sdi, "Mean -/+ 1xSD") |
253 | ||
254 | 175x |
mean_pval <- stat_mean_pval(x, test_mean = control$test_mean, na.rm = FALSE, n_min = 2) |
255 | 175x |
y$mean_pval <- formatters::with_label(mean_pval, paste("Mean", f_pval(control$test_mean))) |
256 | ||
257 | 175x |
y$median <- c("median" = stats::median(x, na.rm = FALSE)) |
258 | ||
259 | 175x |
y$mad <- c("mad" = stats::median(x - y$median, na.rm = FALSE)) |
260 | ||
261 | 175x |
median_ci <- stat_median_ci(x, conf_level = control$conf_level, na.rm = FALSE, gg_helper = FALSE) |
262 | 175x |
y$median_ci <- formatters::with_label(median_ci, paste("Median", f_conf_level(control$conf_level))) |
263 | ||
264 | 175x |
q <- control$quantiles |
265 | 175x |
if (any(is.na(x))) { |
266 | ! |
qnts <- rep(NA_real_, length(q)) |
267 |
} else { |
|
268 | 175x |
qnts <- stats::quantile(x, probs = q, type = control$quantile_type, na.rm = FALSE) |
269 |
}
|
|
270 | 175x |
names(qnts) <- paste("quantile", q, sep = "_") |
271 | 175x |
y$quantiles <- formatters::with_label(qnts, paste0(paste(paste0(q * 100, "%"), collapse = " and "), "-ile")) |
272 | ||
273 | 175x |
y$iqr <- c("iqr" = ifelse( |
274 | 175x |
any(is.na(x)), |
275 | 175x |
NA_real_,
|
276 | 175x |
stats::IQR(x, na.rm = FALSE, type = control$quantile_type) |
277 |
)) |
|
278 | ||
279 | 175x |
y$range <- stats::setNames(range_noinf(x, na.rm = FALSE), c("min", "max")) |
280 | 175x |
y$min <- y$range[1] |
281 | 175x |
y$max <- y$range[2] |
282 | ||
283 | 175x |
y$median_range <- formatters::with_label(c(y$median, y$range), "Median (Min - Max)") |
284 | ||
285 | 175x |
y$cv <- c("cv" = unname(y$sd) / unname(y$mean) * 100) |
286 | ||
287 |
# Convert negative values to NA for log calculation.
|
|
288 | 175x |
x_no_negative_vals <- x |
289 | 175x |
x_no_negative_vals[x_no_negative_vals <= 0] <- NA |
290 | 175x |
y$geom_mean <- c("geom_mean" = exp(mean(log(x_no_negative_vals), na.rm = FALSE))) |
291 | 175x |
geom_mean_ci <- stat_mean_ci(x, conf_level = control$conf_level, na.rm = FALSE, gg_helper = FALSE, geom_mean = TRUE) |
292 | 175x |
y$geom_mean_ci <- formatters::with_label(geom_mean_ci, paste("Geometric Mean", f_conf_level(control$conf_level))) |
293 | ||
294 | 175x |
y$geom_cv <- c("geom_cv" = sqrt(exp(stats::sd(log(x_no_negative_vals), na.rm = FALSE) ^ 2) - 1) * 100) # styler: off |
295 | ||
296 | 175x |
y
|
297 |
}
|
|
298 | ||
299 |
#' @describeIn summarize_variables Method for `factor` class.
|
|
300 |
#'
|
|
301 |
#' @param denom (`string`)\cr choice of denominator for factor proportions. Options are:
|
|
302 |
#' * `n`: number of values in this row and column intersection.
|
|
303 |
#' * `N_row`: total number of values in this row across columns.
|
|
304 |
#' * `N_col`: total number of values in this column across rows.
|
|
305 |
#'
|
|
306 |
#' @return
|
|
307 |
#' * If `x` is of class `factor` or converted from `character`, returns a `list` with named `numeric` items:
|
|
308 |
#' * `n`: The [length()] of `x`.
|
|
309 |
#' * `count`: A list with the number of cases for each level of the factor `x`.
|
|
310 |
#' * `count_fraction`: Similar to `count` but also includes the proportion of cases for each level of the
|
|
311 |
#' factor `x` relative to the denominator, or `NA` if the denominator is zero.
|
|
312 |
#'
|
|
313 |
#' @note
|
|
314 |
#' * If `x` is an empty `factor`, a list is still returned for `counts` with one element
|
|
315 |
#' per factor level. If there are no levels in `x`, the function fails.
|
|
316 |
#' * If factor variables contain `NA`, these `NA` values are excluded by default. To include `NA` values
|
|
317 |
#' set `na.rm = FALSE` and missing values will be displayed as an `NA` level. Alternatively, an explicit
|
|
318 |
#' factor level can be defined for `NA` values during pre-processing via [df_explicit_na()] - the
|
|
319 |
#' default `na_level` (`"<Missing>"`) will also be excluded when `na.rm` is set to `TRUE`.
|
|
320 |
#'
|
|
321 |
#' @method s_summary factor
|
|
322 |
#'
|
|
323 |
#' @examples
|
|
324 |
#' # `s_summary.factor`
|
|
325 |
#'
|
|
326 |
#' ## Basic usage:
|
|
327 |
#' s_summary(factor(c("a", "a", "b", "c", "a")))
|
|
328 |
#' # Empty factor returns NA-filled items.
|
|
329 |
#' s_summary(factor(levels = c("a", "b", "c")))
|
|
330 |
#'
|
|
331 |
#' ## Management of NA values.
|
|
332 |
#' x <- factor(c(NA, "Female"))
|
|
333 |
#' x <- explicit_na(x)
|
|
334 |
#' s_summary(x, na.rm = TRUE)
|
|
335 |
#' s_summary(x, na.rm = FALSE)
|
|
336 |
#'
|
|
337 |
#' ## Different denominators.
|
|
338 |
#' x <- factor(c("a", "a", "b", "c", "a"))
|
|
339 |
#' s_summary(x, denom = "N_row", .N_row = 10L)
|
|
340 |
#' s_summary(x, denom = "N_col", .N_col = 20L)
|
|
341 |
#'
|
|
342 |
#' @export
|
|
343 |
s_summary.factor <- function(x, |
|
344 |
na.rm = TRUE, # nolint |
|
345 |
denom = c("n", "N_row", "N_col"), |
|
346 |
.N_row, # nolint |
|
347 |
.N_col, # nolint |
|
348 |
...) { |
|
349 | 26x |
assert_valid_factor(x) |
350 | 23x |
denom <- match.arg(denom) |
351 | ||
352 | 23x |
if (na.rm) { |
353 | 21x |
x <- x[!is.na(x)] %>% fct_discard("<Missing>") |
354 |
} else { |
|
355 | 2x |
x <- x %>% explicit_na(label = "NA") |
356 |
}
|
|
357 | ||
358 | 23x |
y <- list() |
359 | ||
360 | 23x |
y$n <- length(x) |
361 | ||
362 | 23x |
y$count <- as.list(table(x, useNA = "ifany")) |
363 | 23x |
dn <- switch(denom, |
364 | 23x |
n = length(x), |
365 | 23x |
N_row = .N_row, |
366 | 23x |
N_col = .N_col |
367 |
)
|
|
368 | 23x |
y$count_fraction <- lapply( |
369 | 23x |
y$count, |
370 | 23x |
function(x) { |
371 | 61x |
c(x, ifelse(dn > 0, x / dn, 0)) |
372 |
}
|
|
373 |
)
|
|
374 | ||
375 | 23x |
y$n_blq <- sum(grepl("BLQ|LTR|<[1-9]", x)) |
376 | ||
377 | 23x |
y
|
378 |
}
|
|
379 | ||
380 |
#' @describeIn summarize_variables Method for `character` class. This makes an automatic
|
|
381 |
#' conversion to factor (with a warning) and then forwards to the method for factors.
|
|
382 |
#'
|
|
383 |
#' @param verbose (`logical`)\cr Defaults to `TRUE`, which prints out warnings and messages. It is mainly used
|
|
384 |
#' to print out information about factor casting.
|
|
385 |
#'
|
|
386 |
#' @note
|
|
387 |
#' * Automatic conversion of character to factor does not guarantee that the table
|
|
388 |
#' can be generated correctly. In particular for sparse tables this very likely can fail.
|
|
389 |
#' It is therefore better to always pre-process the dataset such that factors are manually
|
|
390 |
#' created from character variables before passing the dataset to [rtables::build_table()].
|
|
391 |
#'
|
|
392 |
#' @method s_summary character
|
|
393 |
#'
|
|
394 |
#' @examples
|
|
395 |
#' # `s_summary.character`
|
|
396 |
#'
|
|
397 |
#' ## Basic usage:
|
|
398 |
#' s_summary(c("a", "a", "b", "c", "a"), .var = "x", verbose = FALSE)
|
|
399 |
#' s_summary(c("a", "a", "b", "c", "a", ""), .var = "x", na.rm = FALSE, verbose = FALSE)
|
|
400 |
#'
|
|
401 |
#' @export
|
|
402 |
s_summary.character <- function(x, |
|
403 |
na.rm = TRUE, # nolint |
|
404 |
denom = c("n", "N_row", "N_col"), |
|
405 |
.N_row, # nolint |
|
406 |
.N_col, # nolint |
|
407 |
.var,
|
|
408 |
verbose = TRUE, |
|
409 |
...) { |
|
410 | 3x |
if (na.rm) { |
411 | 2x |
y <- as_factor_keep_attributes(x, x_name = .var, verbose = verbose) |
412 |
} else { |
|
413 | 1x |
y <- as_factor_keep_attributes(x, x_name = .var, verbose = verbose, na_level = "NA") |
414 |
}
|
|
415 | ||
416 | 3x |
s_summary( |
417 | 3x |
x = y, |
418 | 3x |
na.rm = na.rm, |
419 | 3x |
denom = denom, |
420 | 3x |
.N_row = .N_row, |
421 | 3x |
.N_col = .N_col, |
422 |
...
|
|
423 |
)
|
|
424 |
}
|
|
425 | ||
426 |
#' @describeIn summarize_variables Method for `logical` class.
|
|
427 |
#'
|
|
428 |
#' @param denom (`string`)\cr choice of denominator for proportion. Options are:
|
|
429 |
#' * `n`: number of values in this row and column intersection.
|
|
430 |
#' * `N_row`: total number of values in this row across columns.
|
|
431 |
#' * `N_col`: total number of values in this column across rows.
|
|
432 |
#'
|
|
433 |
#' @return
|
|
434 |
#' * If `x` is of class `logical`, returns a `list` with named `numeric` items:
|
|
435 |
#' * `n`: The [length()] of `x` (possibly after removing `NA`s).
|
|
436 |
#' * `count`: Count of `TRUE` in `x`.
|
|
437 |
#' * `count_fraction`: Count and proportion of `TRUE` in `x` relative to the denominator, or `NA` if the
|
|
438 |
#' denominator is zero. Note that `NA`s in `x` are never counted or leading to `NA` here.
|
|
439 |
#'
|
|
440 |
#' @method s_summary logical
|
|
441 |
#'
|
|
442 |
#' @examples
|
|
443 |
#' # `s_summary.logical`
|
|
444 |
#'
|
|
445 |
#' ## Basic usage:
|
|
446 |
#' s_summary(c(TRUE, FALSE, TRUE, TRUE))
|
|
447 |
#'
|
|
448 |
#' ## Management of NA values.
|
|
449 |
#' x <- c(NA, TRUE, FALSE)
|
|
450 |
#' s_summary(x, na.rm = TRUE)
|
|
451 |
#' s_summary(x, na.rm = FALSE)
|
|
452 |
#'
|
|
453 |
#' ## Different denominators.
|
|
454 |
#' x <- c(TRUE, FALSE, TRUE, TRUE)
|
|
455 |
#' s_summary(x, denom = "N_row", .N_row = 10L)
|
|
456 |
#' s_summary(x, denom = "N_col", .N_col = 20L)
|
|
457 |
#'
|
|
458 |
#' @export
|
|
459 |
s_summary.logical <- function(x, |
|
460 |
na.rm = TRUE, # nolint |
|
461 |
denom = c("n", "N_row", "N_col"), |
|
462 |
.N_row, # nolint |
|
463 |
.N_col, # nolint |
|
464 |
...) { |
|
465 | 83x |
denom <- match.arg(denom) |
466 | 81x |
if (na.rm) x <- x[!is.na(x)] |
467 | 83x |
y <- list() |
468 | 83x |
y$n <- length(x) |
469 | 83x |
count <- sum(x, na.rm = TRUE) |
470 | 83x |
dn <- switch(denom, |
471 | 83x |
n = length(x), |
472 | 83x |
N_row = .N_row, |
473 | 83x |
N_col = .N_col |
474 |
)
|
|
475 | 83x |
y$count <- count |
476 | 83x |
y$count_fraction <- c(count, ifelse(dn > 0, count / dn, NA)) |
477 | 83x |
y$n_blq <- 0L |
478 | 83x |
y
|
479 |
}
|
|
480 | ||
481 |
#' @describeIn summarize_variables Formatted analysis function which is used as `afun` in `summarize_vars()`.
|
|
482 |
#'
|
|
483 |
#' @return
|
|
484 |
#' * `a_summary()` returns the corresponding list with formatted [rtables::CellValue()].
|
|
485 |
#'
|
|
486 |
#' @export
|
|
487 |
a_summary <- function(x, |
|
488 |
...,
|
|
489 |
.N_row, # nolint |
|
490 |
.N_col, # nolint |
|
491 |
.var) { |
|
492 | ! |
UseMethod("a_summary", x) |
493 |
}
|
|
494 | ||
495 |
.a_summary_numeric_formats <- summary_formats() |
|
496 |
.a_summary_numeric_labels <- summary_labels() |
|
497 | ||
498 |
#' @describeIn summarize_variables Formatted analysis function method for `numeric` class.
|
|
499 |
#'
|
|
500 |
#' @examples
|
|
501 |
#' # `a_summary.numeric`
|
|
502 |
#' a_summary(rnorm(10), .N_col = 10, .N_row = 20, .var = "bla")
|
|
503 |
#'
|
|
504 |
#' @export
|
|
505 |
a_summary.numeric <- make_afun( |
|
506 |
s_summary.numeric,
|
|
507 |
.formats = .a_summary_numeric_formats, |
|
508 |
.labels = .a_summary_numeric_labels |
|
509 |
)
|
|
510 | ||
511 |
.a_summary_counts_formats <- summary_formats(type = "counts") |
|
512 | ||
513 |
#' @describeIn summarize_variables Formatted analysis function method for `factor` class.
|
|
514 |
#'
|
|
515 |
#' @examples
|
|
516 |
#' # `a_summary.factor`
|
|
517 |
#' # We need to ungroup `count` and `count_fraction` first so that the rtables formatting
|
|
518 |
#' # functions can be applied correctly.
|
|
519 |
#' afun <- make_afun(
|
|
520 |
#' getS3method("a_summary", "factor"),
|
|
521 |
#' .ungroup_stats = c("count", "count_fraction")
|
|
522 |
#' )
|
|
523 |
#' afun(factor(c("a", "a", "b", "c", "a")), .N_row = 10, .N_col = 10)
|
|
524 |
#'
|
|
525 |
#' @export
|
|
526 |
a_summary.factor <- make_afun( |
|
527 |
s_summary.factor,
|
|
528 |
.formats = .a_summary_counts_formats |
|
529 |
)
|
|
530 | ||
531 |
#' @describeIn summarize_variables Formatted analysis function method for `character` class.
|
|
532 |
#'
|
|
533 |
#' @examples
|
|
534 |
#' # `a_summary.character`
|
|
535 |
#' afun <- make_afun(
|
|
536 |
#' getS3method("a_summary", "character"),
|
|
537 |
#' .ungroup_stats = c("count", "count_fraction")
|
|
538 |
#' )
|
|
539 |
#' afun(c("A", "B", "A", "C"), .var = "x", .N_col = 10, .N_row = 10, verbose = FALSE)
|
|
540 |
#'
|
|
541 |
#' @export
|
|
542 |
a_summary.character <- make_afun( |
|
543 |
s_summary.character,
|
|
544 |
.formats = .a_summary_counts_formats |
|
545 |
)
|
|
546 | ||
547 |
#' @describeIn summarize_variables Formatted analysis function method for `logical` class.
|
|
548 |
#'
|
|
549 |
#' @examples
|
|
550 |
#' # `a_summary.logical`
|
|
551 |
#' afun <- make_afun(
|
|
552 |
#' getS3method("a_summary", "logical")
|
|
553 |
#' )
|
|
554 |
#' afun(c(TRUE, FALSE, FALSE, TRUE, TRUE), .N_row = 10, .N_col = 10)
|
|
555 |
#'
|
|
556 |
#' @export
|
|
557 |
a_summary.logical <- make_afun( |
|
558 |
s_summary.logical,
|
|
559 |
.formats = .a_summary_counts_formats |
|
560 |
)
|
|
561 | ||
562 |
#' Constructor Function for [summarize_vars()] and [summarize_colvars()]
|
|
563 |
#'
|
|
564 |
#' @description `r lifecycle::badge("stable")`
|
|
565 |
#'
|
|
566 |
#' Constructor function which creates a combined formatted analysis function.
|
|
567 |
#'
|
|
568 |
#' @inheritParams argument_convention
|
|
569 |
#' @param .indent_mods (named `vector` of `integer`)\cr indent modifiers for the labels. Each element of the vector
|
|
570 |
#' should be a name-value pair with name corresponding to a statistic specified in `.stats` and value the indentation
|
|
571 |
#' for that statistic's row label.
|
|
572 |
#'
|
|
573 |
#' @return Combined formatted analysis function for use in [summarize_vars()].
|
|
574 |
#'
|
|
575 |
#' @note Since [a_summary()] is generic and we want customization of the formatting arguments
|
|
576 |
#' via [rtables::make_afun()], we need to create another temporary generic function, with
|
|
577 |
#' corresponding customized methods. Then in order for the methods to be found,
|
|
578 |
#' we need to wrap them in a combined `afun`. Since this is required by two layout creating
|
|
579 |
#' functions (and possibly others in the future), we provide a constructor that does this:
|
|
580 |
#' [create_afun_summary()].
|
|
581 |
#'
|
|
582 |
#' @examples
|
|
583 |
#' # `create_afun_summary()` to create combined `afun`
|
|
584 |
#'
|
|
585 |
#' afun <- create_afun_summary(
|
|
586 |
#' .stats = NULL,
|
|
587 |
#' .formats = c(median = "xx."),
|
|
588 |
#' .labels = c(median = "My median"),
|
|
589 |
#' .indent_mods = c(median = 1L)
|
|
590 |
#' )
|
|
591 |
#' ## Fabricated dataset.
|
|
592 |
#' dta_test <- data.frame(
|
|
593 |
#' USUBJID = rep(1:6, each = 3),
|
|
594 |
#' PARAMCD = rep("lab", 6 * 3),
|
|
595 |
#' AVISIT = rep(paste0("V", 1:3), 6),
|
|
596 |
#' ARM = rep(LETTERS[1:3], rep(6, 3)),
|
|
597 |
#' AVAL = c(9:1, rep(NA, 9))
|
|
598 |
#' )
|
|
599 |
#'
|
|
600 |
#' l <- basic_table() %>%
|
|
601 |
#' split_cols_by(var = "ARM") %>%
|
|
602 |
#' split_rows_by(var = "AVISIT") %>%
|
|
603 |
#' analyze(vars = "AVAL", afun = afun)
|
|
604 |
#'
|
|
605 |
#' build_table(l, df = dta_test)
|
|
606 |
#'
|
|
607 |
#' @export
|
|
608 |
create_afun_summary <- function(.stats, .formats, .labels, .indent_mods) { |
|
609 | 23x |
function(x, |
610 |
...,
|
|
611 | 23x |
.N_row, # nolint |
612 | 23x |
.N_col, # nolint |
613 | 23x |
.var) { |
614 | 270x |
afun <- function(x, ...) { |
615 | 270x |
UseMethod("afun", x) |
616 |
}
|
|
617 | ||
618 | 270x |
numeric_stats <- afun_selected_stats( |
619 | 270x |
.stats,
|
620 | 270x |
all_stats = names(.a_summary_numeric_formats) |
621 |
)
|
|
622 | 270x |
afun.numeric <- make_afun( # nolint |
623 | 270x |
a_summary.numeric,
|
624 | 270x |
.stats = numeric_stats, |
625 | 270x |
.formats = extract_by_name(.formats, numeric_stats), |
626 | 270x |
.labels = extract_by_name(.labels, numeric_stats), |
627 | 270x |
.indent_mods = extract_by_name(.indent_mods, numeric_stats) |
628 |
)
|
|
629 | ||
630 | 270x |
factor_stats <- afun_selected_stats(.stats, c("n", "count", "count_fraction")) |
631 | 270x |
ungroup_stats <- afun_selected_stats(.stats, c("count", "count_fraction")) |
632 | 270x |
afun.factor <- make_afun( # nolint |
633 | 270x |
a_summary.factor,
|
634 | 270x |
.stats = factor_stats, |
635 | 270x |
.formats = extract_by_name(.formats, factor_stats), |
636 | 270x |
.labels = extract_by_name(.labels, factor_stats), |
637 | 270x |
.indent_mods = extract_by_name(.indent_mods, factor_stats), |
638 | 270x |
.ungroup_stats = ungroup_stats |
639 |
)
|
|
640 | ||
641 | 270x |
afun.character <- make_afun( # nolint |
642 | 270x |
a_summary.character,
|
643 | 270x |
.stats = factor_stats, |
644 | 270x |
.formats = extract_by_name(.formats, factor_stats), |
645 | 270x |
.labels = extract_by_name(.labels, factor_stats), |
646 | 270x |
.indent_mods = extract_by_name(.indent_mods, factor_stats), |
647 | 270x |
.ungroup_stats = ungroup_stats |
648 |
)
|
|
649 | ||
650 | 270x |
afun.logical <- make_afun( # nolint |
651 | 270x |
a_summary.logical,
|
652 | 270x |
.stats = factor_stats, |
653 | 270x |
.formats = extract_by_name(.formats, factor_stats), |
654 | 270x |
.labels = extract_by_name(.labels, factor_stats), |
655 | 270x |
.indent_mods = extract_by_name(.indent_mods, factor_stats) |
656 |
)
|
|
657 | ||
658 | 270x |
afun( |
659 | 270x |
x = x, |
660 |
...,
|
|
661 | 270x |
.N_row = .N_row, |
662 | 270x |
.N_col = .N_col, |
663 | 270x |
.var = .var |
664 |
)
|
|
665 |
}
|
|
666 |
}
|
|
667 | ||
668 |
#' @describeIn summarize_variables Layout-creating function which can take statistics function arguments
|
|
669 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()].
|
|
670 |
#'
|
|
671 |
#' @param ... arguments passed to `s_summary()`.
|
|
672 |
#' @param .indent_mods (named `vector` of `integer`)\cr indent modifiers for the labels. Each element of the vector
|
|
673 |
#' should be a name-value pair with name corresponding to a statistic specified in `.stats` and value the indentation
|
|
674 |
#' for that statistic's row label.
|
|
675 |
#'
|
|
676 |
#' @return
|
|
677 |
#' * `summarize_vars()` returns a layout object suitable for passing to further layouting functions,
|
|
678 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing
|
|
679 |
#' the statistics from `s_summary()` to the table layout.
|
|
680 |
#'
|
|
681 |
#' @examples
|
|
682 |
#' ## Fabricated dataset.
|
|
683 |
#' dta_test <- data.frame(
|
|
684 |
#' USUBJID = rep(1:6, each = 3),
|
|
685 |
#' PARAMCD = rep("lab", 6 * 3),
|
|
686 |
#' AVISIT = rep(paste0("V", 1:3), 6),
|
|
687 |
#' ARM = rep(LETTERS[1:3], rep(6, 3)),
|
|
688 |
#' AVAL = c(9:1, rep(NA, 9))
|
|
689 |
#' )
|
|
690 |
#'
|
|
691 |
#' # `summarize_vars()` in `rtables` pipelines
|
|
692 |
#' ## Default output within a `rtables` pipeline.
|
|
693 |
#' l <- basic_table() %>%
|
|
694 |
#' split_cols_by(var = "ARM") %>%
|
|
695 |
#' split_rows_by(var = "AVISIT") %>%
|
|
696 |
#' summarize_vars(vars = "AVAL")
|
|
697 |
#'
|
|
698 |
#' build_table(l, df = dta_test)
|
|
699 |
#'
|
|
700 |
#' ## Select and format statistics output.
|
|
701 |
#' l <- basic_table() %>%
|
|
702 |
#' split_cols_by(var = "ARM") %>%
|
|
703 |
#' split_rows_by(var = "AVISIT") %>%
|
|
704 |
#' summarize_vars(
|
|
705 |
#' vars = "AVAL",
|
|
706 |
#' .stats = c("n", "mean_sd", "quantiles"),
|
|
707 |
#' .formats = c("mean_sd" = "xx.x, xx.x"),
|
|
708 |
#' .labels = c(n = "n", mean_sd = "Mean, SD", quantiles = c("Q1 - Q3"))
|
|
709 |
#' )
|
|
710 |
#'
|
|
711 |
#' results <- build_table(l, df = dta_test)
|
|
712 |
#' as_html(results)
|
|
713 |
#'
|
|
714 |
#' ## Use arguments interpreted by `s_summary`.
|
|
715 |
#' l <- basic_table() %>%
|
|
716 |
#' split_cols_by(var = "ARM") %>%
|
|
717 |
#' split_rows_by(var = "AVISIT") %>%
|
|
718 |
#' summarize_vars(vars = "AVAL", na.rm = FALSE)
|
|
719 |
#'
|
|
720 |
#' results <- build_table(l, df = dta_test)
|
|
721 |
#'
|
|
722 |
#' ## Handle `NA` levels first when summarizing factors.
|
|
723 |
#' dta_test$AVISIT <- NA_character_
|
|
724 |
#' dta_test <- df_explicit_na(dta_test)
|
|
725 |
#' l <- basic_table() %>%
|
|
726 |
#' split_cols_by(var = "ARM") %>%
|
|
727 |
#' summarize_vars(vars = "AVISIT", na.rm = FALSE)
|
|
728 |
#'
|
|
729 |
#' results <- build_table(l, df = dta_test)
|
|
730 |
#' \donttest{
|
|
731 |
#' Viewer(results)
|
|
732 |
#' }
|
|
733 |
#'
|
|
734 |
#' @export
|
|
735 |
summarize_vars <- function(lyt, |
|
736 |
vars,
|
|
737 |
var_labels = vars, |
|
738 |
nested = TRUE, |
|
739 |
...,
|
|
740 |
na_level = NA_character_, |
|
741 |
show_labels = "default", |
|
742 |
table_names = vars, |
|
743 |
section_div = NA_character_, |
|
744 |
.stats = c("n", "mean_sd", "median", "range", "count_fraction"), |
|
745 |
.formats = NULL, |
|
746 |
.labels = NULL, |
|
747 |
.indent_mods = NULL) { |
|
748 | 19x |
afun <- create_afun_summary(.stats, .formats, .labels, .indent_mods) |
749 | ||
750 | 19x |
analyze( |
751 | 19x |
lyt = lyt, |
752 | 19x |
vars = vars, |
753 | 19x |
var_labels = var_labels, |
754 | 19x |
afun = afun, |
755 | 19x |
nested = nested, |
756 | 19x |
extra_args = list(...), |
757 | 19x |
na_str = na_level, |
758 | 19x |
inclNAs = TRUE, |
759 | 19x |
show_labels = show_labels, |
760 | 19x |
table_names = table_names, |
761 | 19x |
section_div = section_div |
762 |
)
|
|
763 |
}
|
1 |
#' Summary numeric variables in columns
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("experimental")`
|
|
4 |
#'
|
|
5 |
#' Layout-creating function which can be used for creating column-wise summary tables.
|
|
6 |
#' This function sets the analysis methods as column labels and is a wrapper for
|
|
7 |
#' [rtables::analyze_colvars()]. It was designed principally for PK tables.
|
|
8 |
#'
|
|
9 |
#' @inheritParams argument_convention
|
|
10 |
#' @inheritParams rtables::analyze_colvars
|
|
11 |
#' @param row_labels (`character`)\cr as this function works in columns space, usual `.labels`
|
|
12 |
#' character vector applies on the column space. You can change the row labels by defining this
|
|
13 |
#' parameter to a named character vector with names corresponding to the split values. It defaults
|
|
14 |
#' to `NULL` and if it contains only one `string`, it will duplicate that as a row label.
|
|
15 |
#' @param do_summarize_row_groups (`flag`)\cr defaults to `FALSE` and applies the analysis to the current
|
|
16 |
#' label rows. This is a wrapper of [rtables::summarize_row_groups()] and it can accept `labelstr`
|
|
17 |
#' to define row labels. This behavior is not supported as we never need to overload row labels.
|
|
18 |
#' @param split_col_vars (`flag`)\cr defaults to `TRUE` and puts the analysis results onto the columns.
|
|
19 |
#' This option allows you to add multiple instances of this functions, also in a nested fashion,
|
|
20 |
#' without adding more splits. This split must happen only one time on a single layout.
|
|
21 |
#'
|
|
22 |
#' @return
|
|
23 |
#' A layout object suitable for passing to further layouting functions, or to [rtables::build_table()].
|
|
24 |
#' Adding this function to an `rtable` layout will summarize the given variables, arrange the output
|
|
25 |
#' in columns, and add it to the table layout.
|
|
26 |
#'
|
|
27 |
#' @note This is an experimental implementation of [rtables::summarize_row_groups()] and
|
|
28 |
#' [rtables::analyze_colvars()] that may be subjected to changes as `rtables` extends its
|
|
29 |
#' support to more complex analysis pipelines on the column space. For the same reasons,
|
|
30 |
#' we encourage to read the examples carefully and file issues for cases that differ from
|
|
31 |
#' them.
|
|
32 |
#'
|
|
33 |
#' Here `labelstr` behaves differently than usual. If it is not defined (default as `NULL`),
|
|
34 |
#' row labels are assigned automatically to the split values in case of `rtables::analyze_colvars`
|
|
35 |
#' (`do_summarize_row_groups = FALSE`, the default), and to the group label for
|
|
36 |
#' `do_summarize_row_groups = TRUE`.
|
|
37 |
#'
|
|
38 |
#' @seealso [summarize_vars()], [rtables::analyze_colvars()].
|
|
39 |
#'
|
|
40 |
#' @examples
|
|
41 |
#' library(dplyr)
|
|
42 |
#'
|
|
43 |
#' # Data preparation
|
|
44 |
#' adpp <- tern_ex_adpp %>% h_pkparam_sort()
|
|
45 |
#'
|
|
46 |
#' lyt <- basic_table() %>%
|
|
47 |
#' split_rows_by(var = "STRATA1", label_pos = "topleft") %>%
|
|
48 |
#' split_rows_by(
|
|
49 |
#' var = "SEX",
|
|
50 |
#' label_pos = "topleft",
|
|
51 |
#' child_label = "hidden"
|
|
52 |
#' ) %>% # Removes duplicated labels
|
|
53 |
#' analyze_vars_in_cols(vars = "AGE")
|
|
54 |
#' result <- build_table(lyt = lyt, df = adpp)
|
|
55 |
#' result
|
|
56 |
#'
|
|
57 |
#' # By selecting just some statistics and ad-hoc labels
|
|
58 |
#' lyt <- basic_table() %>%
|
|
59 |
#' split_rows_by(var = "ARM", label_pos = "topleft") %>%
|
|
60 |
#' split_rows_by(
|
|
61 |
#' var = "SEX",
|
|
62 |
#' label_pos = "topleft",
|
|
63 |
#' child_labels = "hidden",
|
|
64 |
#' split_fun = drop_split_levels
|
|
65 |
#' ) %>%
|
|
66 |
#' analyze_vars_in_cols(
|
|
67 |
#' vars = "AGE",
|
|
68 |
#' .stats = c("n", "cv", "geom_mean"),
|
|
69 |
#' .labels = c(
|
|
70 |
#' n = "aN",
|
|
71 |
#' cv = "aCV",
|
|
72 |
#' geom_mean = "aGeomMean"
|
|
73 |
#' )
|
|
74 |
#' )
|
|
75 |
#' result <- build_table(lyt = lyt, df = adpp)
|
|
76 |
#' result
|
|
77 |
#'
|
|
78 |
#' # Changing row labels
|
|
79 |
#' lyt <- basic_table() %>%
|
|
80 |
#' analyze_vars_in_cols(
|
|
81 |
#' vars = "AGE",
|
|
82 |
#' row_labels = "some custom label"
|
|
83 |
#' )
|
|
84 |
#' result <- build_table(lyt, df = adpp)
|
|
85 |
#' result
|
|
86 |
#'
|
|
87 |
#' # Pharmacokinetic parameters
|
|
88 |
#' lyt <- basic_table() %>%
|
|
89 |
#' split_rows_by(
|
|
90 |
#' var = "TLG_DISPLAY",
|
|
91 |
#' split_label = "PK Parameter",
|
|
92 |
#' label_pos = "topleft",
|
|
93 |
#' child_label = "hidden"
|
|
94 |
#' ) %>%
|
|
95 |
#' analyze_vars_in_cols(
|
|
96 |
#' vars = "AVAL"
|
|
97 |
#' )
|
|
98 |
#' result <- build_table(lyt, df = adpp)
|
|
99 |
#' result
|
|
100 |
#'
|
|
101 |
#' # Multiple calls (summarize label and analyze underneath)
|
|
102 |
#' lyt <- basic_table() %>%
|
|
103 |
#' split_rows_by(
|
|
104 |
#' var = "TLG_DISPLAY",
|
|
105 |
#' split_label = "PK Parameter",
|
|
106 |
#' label_pos = "topleft"
|
|
107 |
#' ) %>%
|
|
108 |
#' analyze_vars_in_cols(
|
|
109 |
#' vars = "AVAL",
|
|
110 |
#' do_summarize_row_groups = TRUE # does a summarize level
|
|
111 |
#' ) %>%
|
|
112 |
#' split_rows_by("SEX",
|
|
113 |
#' child_label = "hidden",
|
|
114 |
#' label_pos = "topleft"
|
|
115 |
#' ) %>%
|
|
116 |
#' analyze_vars_in_cols(
|
|
117 |
#' vars = "AVAL",
|
|
118 |
#' split_col_vars = FALSE # avoids re-splitting the columns
|
|
119 |
#' )
|
|
120 |
#' result <- build_table(lyt, df = adpp)
|
|
121 |
#' result
|
|
122 |
#'
|
|
123 |
#' @export
|
|
124 |
analyze_vars_in_cols <- function(lyt, |
|
125 |
vars,
|
|
126 |
...,
|
|
127 |
.stats = c( |
|
128 |
"n",
|
|
129 |
"mean",
|
|
130 |
"sd",
|
|
131 |
"se",
|
|
132 |
"cv",
|
|
133 |
"geom_cv"
|
|
134 |
),
|
|
135 |
.labels = c( |
|
136 |
n = "n", |
|
137 |
mean = "Mean", |
|
138 |
sd = "SD", |
|
139 |
se = "SE", |
|
140 |
cv = "CV (%)", |
|
141 |
geom_cv = "CV % Geometric Mean" |
|
142 |
),
|
|
143 |
row_labels = NULL, |
|
144 |
do_summarize_row_groups = FALSE, |
|
145 |
split_col_vars = TRUE, |
|
146 |
.indent_mods = NULL, |
|
147 |
nested = TRUE, |
|
148 |
na_level = NULL, |
|
149 |
.formats = NULL) { |
|
150 | 6x |
checkmate::assert_string(na_level, null.ok = TRUE) |
151 | 6x |
checkmate::assert_character(row_labels, null.ok = TRUE) |
152 | 6x |
checkmate::assert_int(.indent_mods, null.ok = TRUE) |
153 | 6x |
checkmate::assert_flag(nested) |
154 | 6x |
checkmate::assert_flag(split_col_vars) |
155 | 6x |
checkmate::assert_flag(do_summarize_row_groups) |
156 | ||
157 |
# Automatic assignment of formats
|
|
158 | 6x |
if (is.null(.formats)) { |
159 |
# General values
|
|
160 | 6x |
sf_numeric <- summary_formats("numeric") |
161 | 6x |
sf_counts <- summary_formats("counts")[-1] |
162 | 6x |
formats_v <- c(sf_numeric, sf_counts) |
163 |
} else { |
|
164 | ! |
formats_v <- .formats |
165 |
}
|
|
166 | ||
167 |
# Check for vars in the case that one or more are used
|
|
168 | 6x |
if (length(vars) == 1) { |
169 | 5x |
vars <- rep(vars, length(.stats)) |
170 | 1x |
} else if (length(vars) != length(.stats)) { |
171 | 1x |
stop( |
172 | 1x |
"Analyzed variables (vars) does not have the same ",
|
173 | 1x |
"number of elements of specified statistics (.stats)."
|
174 |
)
|
|
175 |
}
|
|
176 | ||
177 | 5x |
if (split_col_vars) { |
178 |
# Checking there is not a previous identical column split
|
|
179 | 4x |
clyt <- tail(clayout(lyt), 1)[[1]] |
180 | ||
181 | 4x |
dummy_lyt <- split_cols_by_multivar( |
182 | 4x |
lyt = basic_table(), |
183 | 4x |
vars = vars, |
184 | 4x |
varlabels = .labels[.stats] |
185 |
)
|
|
186 | ||
187 | 4x |
if (any(sapply(clyt, identical, y = get_last_col_split(dummy_lyt)))) { |
188 | ! |
stop( |
189 | ! |
"Column split called again with the same values. ",
|
190 | ! |
"This can create many unwanted columns. Please consider adding ",
|
191 | ! |
"split_col_vars = FALSE to the last call of ",
|
192 | ! |
deparse(sys.calls()[[sys.nframe() - 1]]), "." |
193 |
)
|
|
194 |
}
|
|
195 | ||
196 |
# Main col split
|
|
197 | 4x |
lyt <- split_cols_by_multivar( |
198 | 4x |
lyt = lyt, |
199 | 4x |
vars = vars, |
200 | 4x |
varlabels = .labels[.stats] |
201 |
)
|
|
202 |
}
|
|
203 | ||
204 | 5x |
if (do_summarize_row_groups) { |
205 | 2x |
if (length(unique(vars)) > 1) { |
206 | ! |
stop("When using do_summarize_row_groups only one label level var should be inserted.") |
207 |
}
|
|
208 | ||
209 |
# Function list for do_summarize_row_groups. Slightly different handling of labels
|
|
210 | 2x |
cfun_list <- Map( |
211 | 2x |
function(stat) { |
212 | 12x |
function(u, .spl_context, labelstr, ...) { |
213 |
# Statistic
|
|
214 | 24x |
res <- s_summary(u, ...)[[stat]] |
215 | ||
216 |
# Label check and replacement
|
|
217 | 24x |
if (length(row_labels) > 1) { |
218 | 12x |
if (!(labelstr %in% names(row_labels))) { |
219 | ! |
stop( |
220 | ! |
"Replacing the labels in do_summarize_row_groups needs a named vector",
|
221 | ! |
"that contains the split values. In the current split variable ",
|
222 | ! |
.spl_context$split[nrow(.spl_context)], |
223 | ! |
" the labelstr value (split value by default) ", labelstr, " is not in", |
224 | ! |
" row_labels names: ", names(row_labels) |
225 |
)
|
|
226 |
}
|
|
227 | 12x |
lbl <- unlist(row_labels[labelstr]) |
228 |
} else { |
|
229 | 12x |
lbl <- labelstr |
230 |
}
|
|
231 | ||
232 |
# Cell creation
|
|
233 | 24x |
rcell(res, |
234 | 24x |
label = lbl, |
235 | 24x |
format = formats_v[names(formats_v) == stat][[1]], |
236 | 24x |
format_na_str = na_level, |
237 | 24x |
indent_mod = ifelse(is.null(.indent_mods), 0L, .indent_mods) |
238 |
)
|
|
239 |
}
|
|
240 |
},
|
|
241 | 2x |
stat = .stats |
242 |
)
|
|
243 | ||
244 |
# Main call to rtables
|
|
245 | 2x |
summarize_row_groups( |
246 | 2x |
lyt = lyt, |
247 | 2x |
var = unique(vars), |
248 | 2x |
cfun = cfun_list, |
249 | 2x |
extra_args = list(...) |
250 |
)
|
|
251 |
} else { |
|
252 |
# Function list for analyze_colvars
|
|
253 | 3x |
afun_list <- Map( |
254 | 3x |
function(stat) { |
255 | 15x |
function(u, .spl_context, ...) { |
256 |
# Main statistics
|
|
257 | 78x |
res <- s_summary(u, ...)[[stat]] |
258 | ||
259 |
# Label from context
|
|
260 | 78x |
label_from_context <- .spl_context$value[nrow(.spl_context)] |
261 | ||
262 |
# Label switcher
|
|
263 | 78x |
if (is.null(row_labels)) { |
264 | 18x |
lbl <- label_from_context |
265 |
} else { |
|
266 | 60x |
if (length(row_labels) > 1) { |
267 | 48x |
if (!(label_from_context %in% names(row_labels))) { |
268 | ! |
stop( |
269 | ! |
"Replacing the labels in do_summarize_row_groups needs a named vector",
|
270 | ! |
"that contains the split values. In the current split variable ",
|
271 | ! |
.spl_context$split[nrow(.spl_context)], |
272 | ! |
" the split value ", label_from_context, " is not in", |
273 | ! |
" row_labels names: ", names(row_labels) |
274 |
)
|
|
275 |
}
|
|
276 | 48x |
lbl <- unlist(row_labels[label_from_context]) |
277 |
} else { |
|
278 | 12x |
lbl <- row_labels |
279 |
}
|
|
280 |
}
|
|
281 | ||
282 |
# Cell creation
|
|
283 | 78x |
rcell(res, |
284 | 78x |
label = lbl, |
285 | 78x |
format = formats_v[names(formats_v) == stat][[1]], |
286 | 78x |
format_na_str = na_level, |
287 | 78x |
indent_mod = ifelse(is.null(.indent_mods), 0L, .indent_mods) |
288 |
)
|
|
289 |
}
|
|
290 |
},
|
|
291 | 3x |
stat = .stats |
292 |
)
|
|
293 | ||
294 |
# Main call to rtables
|
|
295 | 3x |
analyze_colvars(lyt, |
296 | 3x |
afun = afun_list, |
297 | 3x |
nested = nested, |
298 | 3x |
extra_args = list(...) |
299 |
)
|
|
300 |
}
|
|
301 |
}
|
|
302 | ||
303 |
# Help function
|
|
304 |
get_last_col_split <- function(lyt) { |
|
305 | ! |
tail(tail(clayout(lyt), 1)[[1]], 1)[[1]] |
306 |
}
|
1 |
#' Cox Regression Helper: Interactions
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Test and estimate the effect of a treatment in interaction with a covariate.
|
|
6 |
#' The effect is estimated as the HR of the tested treatment for a given level
|
|
7 |
#' of the covariate, in comparison to the treatment control.
|
|
8 |
#'
|
|
9 |
#' @inheritParams argument_convention
|
|
10 |
#' @param x (`numeric` or `factor`)\cr the values of the covariate to be tested.
|
|
11 |
#' @param effect (`string`)\cr the name of the effect to be tested and estimated.
|
|
12 |
#' @param covar (`string`)\cr the name of the covariate in the model.
|
|
13 |
#' @param mod (`coxph`)\cr the Cox regression model.
|
|
14 |
#' @param label (`string`)\cr the label to be returned as `term_label`.
|
|
15 |
#' @param control (`list`)\cr a list of controls as returned by [control_coxreg()].
|
|
16 |
#' @param ... see methods.
|
|
17 |
#'
|
|
18 |
#' @examples
|
|
19 |
#' library(survival)
|
|
20 |
#'
|
|
21 |
#' set.seed(1, kind = "Mersenne-Twister")
|
|
22 |
#'
|
|
23 |
#' # Testing dataset [survival::bladder].
|
|
24 |
#' dta_bladder <- with(
|
|
25 |
#' data = bladder[bladder$enum < 5, ],
|
|
26 |
#' data.frame(
|
|
27 |
#' time = stop,
|
|
28 |
#' status = event,
|
|
29 |
#' armcd = as.factor(rx),
|
|
30 |
#' covar1 = as.factor(enum),
|
|
31 |
#' covar2 = factor(
|
|
32 |
#' sample(as.factor(enum)),
|
|
33 |
#' levels = 1:4,
|
|
34 |
#' labels = c("F", "F", "M", "M")
|
|
35 |
#' )
|
|
36 |
#' )
|
|
37 |
#' )
|
|
38 |
#' labels <- c("armcd" = "ARM", "covar1" = "A Covariate Label", "covar2" = "Sex (F/M)")
|
|
39 |
#' formatters::var_labels(dta_bladder)[names(labels)] <- labels
|
|
40 |
#' dta_bladder$age <- sample(20:60, size = nrow(dta_bladder), replace = TRUE)
|
|
41 |
#'
|
|
42 |
#' plot(
|
|
43 |
#' survfit(Surv(time, status) ~ armcd + covar1, data = dta_bladder),
|
|
44 |
#' lty = 2:4,
|
|
45 |
#' xlab = "Months",
|
|
46 |
#' col = c("blue1", "blue2", "blue3", "blue4", "red1", "red2", "red3", "red4")
|
|
47 |
#' )
|
|
48 |
#'
|
|
49 |
#' @name cox_regression_inter
|
|
50 |
NULL
|
|
51 | ||
52 |
#' @describeIn cox_regression_inter S3 generic helper function to determine interaction effect.
|
|
53 |
#'
|
|
54 |
#' @return
|
|
55 |
#' * `h_coxreg_inter_effect()` returns a `data.frame` of covariate interaction effects consisting of the following
|
|
56 |
#' variables: `effect`, `term`, `term_label`, `level`, `n`, `hr`, `lcl`, `ucl`, `pval`, and `pval_inter`.
|
|
57 |
#'
|
|
58 |
#' @export
|
|
59 |
h_coxreg_inter_effect <- function(x, |
|
60 |
effect,
|
|
61 |
covar,
|
|
62 |
mod,
|
|
63 |
label,
|
|
64 |
control,
|
|
65 |
...) { |
|
66 | 26x |
UseMethod("h_coxreg_inter_effect", x) |
67 |
}
|
|
68 | ||
69 |
#' @describeIn cox_regression_inter Method for `numeric` class. Estimates the interaction with a `numeric` covariate.
|
|
70 |
#'
|
|
71 |
#' @method h_coxreg_inter_effect numeric
|
|
72 |
#'
|
|
73 |
#' @param at (`list`)\cr a list with items named after the covariate, every
|
|
74 |
#' item is a vector of levels at which the interaction should be estimated.
|
|
75 |
#'
|
|
76 |
#' @export
|
|
77 |
h_coxreg_inter_effect.numeric <- function(x, |
|
78 |
effect,
|
|
79 |
covar,
|
|
80 |
mod,
|
|
81 |
label,
|
|
82 |
control,
|
|
83 |
at,
|
|
84 |
...) { |
|
85 | 7x |
betas <- stats::coef(mod) |
86 | 7x |
attrs <- attr(stats::terms(mod), "term.labels") |
87 | 7x |
term_indices <- grep( |
88 | 7x |
pattern = effect, |
89 | 7x |
x = attrs[!grepl("strata\\(", attrs)] |
90 |
)
|
|
91 | 7x |
checkmate::assert_vector(term_indices, len = 2) |
92 | 7x |
betas <- betas[term_indices] |
93 | 7x |
betas_var <- diag(stats::vcov(mod))[term_indices] |
94 | 7x |
betas_cov <- stats::vcov(mod)[term_indices[1], term_indices[2]] |
95 | 7x |
xval <- if (is.null(at[[covar]])) { |
96 | 6x |
stats::median(x) |
97 |
} else { |
|
98 | 1x |
at[[covar]] |
99 |
}
|
|
100 | 7x |
effect_index <- !grepl(covar, names(betas)) |
101 | 7x |
coef_hat <- betas[effect_index] + xval * betas[!effect_index] |
102 | 7x |
coef_se <- sqrt( |
103 | 7x |
betas_var[effect_index] + |
104 | 7x |
xval ^ 2 * betas_var[!effect_index] + # styler: off |
105 | 7x |
2 * xval * betas_cov |
106 |
)
|
|
107 | 7x |
q_norm <- stats::qnorm((1 + control$conf_level) / 2) |
108 | 7x |
data.frame( |
109 | 7x |
effect = "Covariate:", |
110 | 7x |
term = rep(covar, length(xval)), |
111 | 7x |
term_label = paste0(" ", xval), |
112 | 7x |
level = as.character(xval), |
113 | 7x |
n = NA, |
114 | 7x |
hr = exp(coef_hat), |
115 | 7x |
lcl = exp(coef_hat - q_norm * coef_se), |
116 | 7x |
ucl = exp(coef_hat + q_norm * coef_se), |
117 | 7x |
pval = NA, |
118 | 7x |
pval_inter = NA, |
119 | 7x |
stringsAsFactors = FALSE |
120 |
)
|
|
121 |
}
|
|
122 | ||
123 |
#' @describeIn cox_regression_inter Method for `factor` class. Estimate the interaction with a `factor` covariate.
|
|
124 |
#'
|
|
125 |
#' @method h_coxreg_inter_effect factor
|
|
126 |
#'
|
|
127 |
#' @param data (`data.frame`)\cr the data frame on which the model was fit.
|
|
128 |
#'
|
|
129 |
#' @export
|
|
130 |
h_coxreg_inter_effect.factor <- function(x, |
|
131 |
effect,
|
|
132 |
covar,
|
|
133 |
mod,
|
|
134 |
label,
|
|
135 |
control,
|
|
136 |
data,
|
|
137 |
...) { |
|
138 | 15x |
lvl_given <- levels(x) |
139 | 15x |
y <- h_coxreg_inter_estimations( |
140 | 15x |
variable = effect, given = covar, |
141 | 15x |
lvl_var = levels(data[[effect]]), |
142 | 15x |
lvl_given = lvl_given, |
143 | 15x |
mod = mod, |
144 | 15x |
conf_level = 0.95 |
145 | 15x |
)[[1]] |
146 | ||
147 | 15x |
data.frame( |
148 | 15x |
effect = "Covariate:", |
149 | 15x |
term = rep(covar, nrow(y)), |
150 | 15x |
term_label = paste0(" ", lvl_given), |
151 | 15x |
level = lvl_given, |
152 | 15x |
n = NA, |
153 | 15x |
hr = y[, "hr"], |
154 | 15x |
lcl = y[, "lcl"], |
155 | 15x |
ucl = y[, "ucl"], |
156 | 15x |
pval = NA, |
157 | 15x |
pval_inter = NA, |
158 | 15x |
stringsAsFactors = FALSE |
159 |
)
|
|
160 |
}
|
|
161 | ||
162 |
#' @describeIn cox_regression_inter Method for `character` class. Estimate the interaction with a `character` covariate.
|
|
163 |
#' This makes an automatic conversion to `factor` and then forwards to the method for factors.
|
|
164 |
#'
|
|
165 |
#' @method h_coxreg_inter_effect character
|
|
166 |
#'
|
|
167 |
#' @note
|
|
168 |
#' * Automatic conversion of character to factor does not guarantee results can be generated correctly. It is
|
|
169 |
#' therefore better to always pre-process the dataset such that factors are manually created from character
|
|
170 |
#' variables before passing the dataset to [rtables::build_table()].
|
|
171 |
#'
|
|
172 |
#' @export
|
|
173 |
h_coxreg_inter_effect.character <- function(x, |
|
174 |
effect,
|
|
175 |
covar,
|
|
176 |
mod,
|
|
177 |
label,
|
|
178 |
control,
|
|
179 |
data,
|
|
180 |
...) { |
|
181 | 4x |
y <- as.factor(x) |
182 | ||
183 | 4x |
h_coxreg_inter_effect( |
184 | 4x |
x = y, |
185 | 4x |
effect = effect, |
186 | 4x |
covar = covar, |
187 | 4x |
mod = mod, |
188 | 4x |
label = label, |
189 | 4x |
control = control, |
190 | 4x |
data = data, |
191 |
...
|
|
192 |
)
|
|
193 |
}
|
|
194 | ||
195 |
#' @describeIn cox_regression_inter A higher level function to get
|
|
196 |
#' the results of the interaction test and the estimated values.
|
|
197 |
#'
|
|
198 |
#' @return
|
|
199 |
#' * `h_coxreg_extract_interaction()` returns the result of an interaction test and the estimated values. If
|
|
200 |
#' no interaction, [h_coxreg_univar_extract()] is applied instead.
|
|
201 |
#'
|
|
202 |
#' @examples
|
|
203 |
#' mod <- coxph(Surv(time, status) ~ armcd * covar1, data = dta_bladder)
|
|
204 |
#' h_coxreg_extract_interaction(
|
|
205 |
#' mod = mod, effect = "armcd", covar = "covar1", data = dta_bladder,
|
|
206 |
#' control = control_coxreg()
|
|
207 |
#' )
|
|
208 |
#'
|
|
209 |
#' @export
|
|
210 |
h_coxreg_extract_interaction <- function(effect, |
|
211 |
covar,
|
|
212 |
mod,
|
|
213 |
data,
|
|
214 |
at,
|
|
215 |
control) { |
|
216 | 27x |
if (!any(attr(stats::terms(mod), "order") == 2)) { |
217 | 10x |
y <- h_coxreg_univar_extract( |
218 | 10x |
effect = effect, covar = covar, mod = mod, data = data, control = control |
219 |
)
|
|
220 | 10x |
y$pval_inter <- NA |
221 | 10x |
y
|
222 |
} else { |
|
223 | 17x |
test_statistic <- c(wald = "Wald", likelihood = "LR")[control$pval_method] |
224 | ||
225 |
# Test the main treatment effect.
|
|
226 | 17x |
mod_aov <- muffled_car_anova(mod, test_statistic) |
227 | 17x |
sum_anova <- broom::tidy(mod_aov) |
228 | 17x |
pval <- sum_anova[sum_anova$term == effect, ][["p.value"]] |
229 | ||
230 |
# Test the interaction effect.
|
|
231 | 17x |
pval_inter <- sum_anova[grep(":", sum_anova$term), ][["p.value"]] |
232 | 17x |
covar_test <- data.frame( |
233 | 17x |
effect = "Covariate:", |
234 | 17x |
term = covar, |
235 | 17x |
term_label = unname(labels_or_names(data[covar])), |
236 | 17x |
level = "", |
237 | 17x |
n = mod$n, hr = NA, lcl = NA, ucl = NA, pval = pval, |
238 | 17x |
pval_inter = pval_inter, |
239 | 17x |
stringsAsFactors = FALSE |
240 |
)
|
|
241 |
# Estimate the interaction.
|
|
242 | 17x |
y <- h_coxreg_inter_effect( |
243 | 17x |
data[[covar]], |
244 | 17x |
covar = covar, |
245 | 17x |
effect = effect, |
246 | 17x |
mod = mod, |
247 | 17x |
label = unname(labels_or_names(data[covar])), |
248 | 17x |
at = at, |
249 | 17x |
control = control, |
250 | 17x |
data = data |
251 |
)
|
|
252 | 17x |
rbind(covar_test, y) |
253 |
}
|
|
254 |
}
|
|
255 | ||
256 |
#' @describeIn cox_regression_inter Hazard ratio estimation in interactions.
|
|
257 |
#'
|
|
258 |
#' @param variable,given (`string`)\cr the name of variables in interaction. We seek the estimation
|
|
259 |
#' of the levels of `variable` given the levels of `given`.
|
|
260 |
#' @param lvl_var,lvl_given (`character`)\cr corresponding levels has given by [levels()].
|
|
261 |
#' @param mod (`coxph`)\cr a fitted Cox regression model (see [survival::coxph()]).
|
|
262 |
#'
|
|
263 |
#' @details Given the cox regression investigating the effect of Arm (A, B, C; reference A)
|
|
264 |
#' and Sex (F, M; reference Female) and the model being abbreviated: y ~ Arm + Sex + Arm:Sex.
|
|
265 |
#' The cox regression estimates the coefficients along with a variance-covariance matrix for:
|
|
266 |
#'
|
|
267 |
#' - b1 (arm b), b2 (arm c)
|
|
268 |
#' - b3 (sex m)
|
|
269 |
#' - b4 (arm b: sex m), b5 (arm c: sex m)
|
|
270 |
#'
|
|
271 |
#' The estimation of the Hazard Ratio for arm C/sex M is given in reference
|
|
272 |
#' to arm A/Sex M by exp(b2 + b3 + b5)/ exp(b3) = exp(b2 + b5).
|
|
273 |
#' The interaction coefficient is deduced by b2 + b5 while the standard error
|
|
274 |
#' is obtained as $sqrt(Var b2 + Var b5 + 2 * covariance (b2,b5))$.
|
|
275 |
#'
|
|
276 |
#' @return
|
|
277 |
#' * `h_coxreg_inter_estimations()` returns a list of matrices (one per level of variable) with rows corresponding
|
|
278 |
#' to the combinations of `variable` and `given`, with columns:
|
|
279 |
#' * `coef_hat`: Estimation of the coefficient.
|
|
280 |
#' * `coef_se`: Standard error of the estimation.
|
|
281 |
#' * `hr`: Hazard ratio.
|
|
282 |
#' * `lcl, ucl`: Lower/upper confidence limit of the hazard ratio.
|
|
283 |
#'
|
|
284 |
#' @examples
|
|
285 |
#' mod <- coxph(Surv(time, status) ~ armcd * covar1, data = dta_bladder)
|
|
286 |
#' result <- h_coxreg_inter_estimations(
|
|
287 |
#' variable = "armcd", given = "covar1",
|
|
288 |
#' lvl_var = levels(dta_bladder$armcd),
|
|
289 |
#' lvl_given = levels(dta_bladder$covar1),
|
|
290 |
#' mod = mod, conf_level = .95
|
|
291 |
#' )
|
|
292 |
#' result
|
|
293 |
#'
|
|
294 |
#' @export
|
|
295 |
h_coxreg_inter_estimations <- function(variable, |
|
296 |
given,
|
|
297 |
lvl_var,
|
|
298 |
lvl_given,
|
|
299 |
mod,
|
|
300 |
conf_level = 0.95) { |
|
301 | 16x |
var_lvl <- paste0(variable, lvl_var[-1]) # [-1]: reference level |
302 | 16x |
giv_lvl <- paste0(given, lvl_given) |
303 | 16x |
design_mat <- expand.grid(variable = var_lvl, given = giv_lvl) |
304 | 16x |
design_mat <- design_mat[order(design_mat$variable, design_mat$given), ] |
305 | 16x |
design_mat <- within( |
306 | 16x |
data = design_mat, |
307 | 16x |
expr = { |
308 | 16x |
inter <- paste0(variable, ":", given) |
309 | 16x |
rev_inter <- paste0(given, ":", variable) |
310 |
}
|
|
311 |
)
|
|
312 | 16x |
split_by_variable <- design_mat$variable |
313 | 16x |
interaction_names <- paste(design_mat$variable, design_mat$given, sep = "/") |
314 | ||
315 | 16x |
mmat <- stats::model.matrix(mod)[1, ] |
316 | 16x |
mmat[!mmat == 0] <- 0 |
317 | ||
318 | 16x |
design_mat <- apply( |
319 | 16x |
X = design_mat, MARGIN = 1, FUN = function(x) { |
320 | 46x |
mmat[names(mmat) %in% x[-which(names(x) == "given")]] <- 1 |
321 | 46x |
mmat
|
322 |
}
|
|
323 |
)
|
|
324 | 16x |
colnames(design_mat) <- interaction_names |
325 | ||
326 | 16x |
coef <- stats::coef(mod) |
327 | 16x |
vcov <- stats::vcov(mod) |
328 | 16x |
betas <- as.matrix(coef) |
329 | 16x |
coef_hat <- t(design_mat) %*% betas |
330 | 16x |
dimnames(coef_hat)[2] <- "coef" |
331 | 16x |
coef_se <- apply( |
332 | 16x |
design_mat, 2, |
333 | 16x |
function(x) { |
334 | 46x |
vcov_el <- as.logical(x) |
335 | 46x |
y <- vcov[vcov_el, vcov_el] |
336 | 46x |
y <- sum(y) |
337 | 46x |
y <- sqrt(y) |
338 | 46x |
return(y) |
339 |
}
|
|
340 |
)
|
|
341 | 16x |
q_norm <- stats::qnorm((1 + conf_level) / 2) |
342 | 16x |
y <- cbind(coef_hat, `se(coef)` = coef_se) |
343 | 16x |
y <- apply(y, 1, function(x) { |
344 | 46x |
x["hr"] <- exp(x["coef"]) |
345 | 46x |
x["lcl"] <- exp(x["coef"] - q_norm * x["se(coef)"]) |
346 | 46x |
x["ucl"] <- exp(x["coef"] + q_norm * x["se(coef)"]) |
347 | 46x |
x
|
348 |
}) |
|
349 | 16x |
y <- t(y) |
350 | 16x |
y <- by(y, split_by_variable, identity) |
351 | 16x |
y <- lapply(y, as.matrix) |
352 | 16x |
attr(y, "details") <- paste0( |
353 | 16x |
"Estimations of ", variable, |
354 | 16x |
" hazard ratio given the level of ", given, " compared to ", |
355 | 16x |
variable, " level ", lvl_var[1], "." |
356 |
)
|
|
357 | 16x |
y
|
358 |
}
|
1 |
#' Counting Patients and Events in Columns
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Counting the number of unique patients and the total number of all and specific events
|
|
6 |
#' when a column table layout is required.
|
|
7 |
#'
|
|
8 |
#' @inheritParams argument_convention
|
|
9 |
#'
|
|
10 |
#' @name count_patients_events_in_cols
|
|
11 |
NULL
|
|
12 | ||
13 |
#' @describeIn count_patients_events_in_cols Statistics function which counts numbers of patients and multiple
|
|
14 |
#' events defined by filters. Used as analysis function `afun` in `summarize_patients_events_in_cols()`.
|
|
15 |
#'
|
|
16 |
#' @param filters_list (named `list` of `character`)\cr each element in this list describes one
|
|
17 |
#' type of event describe by filters, in the same format as [s_count_patients_with_event()].
|
|
18 |
#' If it has a label, then this will be used for the column title.
|
|
19 |
#' @param empty_stats (`character`)\cr optional names of the statistics that should be returned empty such
|
|
20 |
#' that corresponding table cells will stay blank.
|
|
21 |
#' @param custom_label (`string` or `NULL`)\cr if provided and `labelstr` is empty then this will
|
|
22 |
#' be used as label.
|
|
23 |
#'
|
|
24 |
#' @return
|
|
25 |
#' * `s_count_patients_and_multiple_events()` returns a list with the statistics:
|
|
26 |
#' - `unique`: number of unique patients in `df`.
|
|
27 |
#' - `all`: number of rows in `df`.
|
|
28 |
#' - one element with the same name as in `filters_list`: number of rows in `df`,
|
|
29 |
#' i.e. events, fulfilling the filter condition.
|
|
30 |
#'
|
|
31 |
#' @examples
|
|
32 |
#' # `s_count_patients_and_multiple_events()`
|
|
33 |
#' df <- data.frame(
|
|
34 |
#' USUBJID = rep(c("id1", "id2", "id3", "id4"), c(2, 3, 1, 1)),
|
|
35 |
#' ARM = c("A", "A", "B", "B", "B", "B", "A"),
|
|
36 |
#' AESER = rep("Y", 7),
|
|
37 |
#' AESDTH = c("Y", "Y", "N", "Y", "Y", "N", "N"),
|
|
38 |
#' AEREL = c("Y", "Y", "N", "Y", "Y", "N", "Y"),
|
|
39 |
#' AEDECOD = c("A", "A", "A", "B", "B", "C", "D"),
|
|
40 |
#' AEBODSYS = rep(c("SOC1", "SOC2", "SOC3"), c(3, 3, 1))
|
|
41 |
#' )
|
|
42 |
#'
|
|
43 |
#' @keywords internal
|
|
44 |
s_count_patients_and_multiple_events <- function(df, # nolint |
|
45 |
id,
|
|
46 |
filters_list,
|
|
47 |
empty_stats = character(), |
|
48 |
labelstr = "", |
|
49 |
custom_label = NULL) { |
|
50 | 9x |
checkmate::assert_list(filters_list, names = "named") |
51 | 9x |
checkmate::assert_data_frame(df) |
52 | 9x |
checkmate::assert_string(id) |
53 | 9x |
checkmate::assert_disjunct(c("unique", "all"), names(filters_list)) |
54 | 9x |
checkmate::assert_character(empty_stats) |
55 | 9x |
checkmate::assert_string(labelstr) |
56 | 9x |
checkmate::assert_string(custom_label, null.ok = TRUE) |
57 | ||
58 |
# Below we want to count each row in `df` once, therefore introducing this helper index column.
|
|
59 | 9x |
df$.row_index <- as.character(seq_len(nrow(df))) |
60 | 9x |
y <- list() |
61 | 9x |
row_label <- if (labelstr != "") { |
62 | ! |
labelstr
|
63 | 9x |
} else if (!is.null(custom_label)) { |
64 | 2x |
custom_label
|
65 |
} else { |
|
66 | 7x |
"counts"
|
67 |
}
|
|
68 | 9x |
y$unique <- formatters::with_label( |
69 | 9x |
s_num_patients_content(df = df, .N_col = 1, .var = id, required = NULL)$unique[1L], |
70 | 9x |
row_label
|
71 |
)
|
|
72 | 9x |
y$all <- formatters::with_label( |
73 | 9x |
nrow(df), |
74 | 9x |
row_label
|
75 |
)
|
|
76 | 9x |
events <- Map( |
77 | 9x |
function(filters) { |
78 | 25x |
formatters::with_label( |
79 | 25x |
s_count_patients_with_event(df = df, .var = ".row_index", filters = filters, .N_col = 1, .N_row = 1)$count, |
80 | 25x |
row_label
|
81 |
)
|
|
82 |
},
|
|
83 | 9x |
filters = filters_list |
84 |
)
|
|
85 | 9x |
y_complete <- c(y, events) |
86 | 9x |
y <- if (length(empty_stats) > 0) { |
87 | 3x |
y_reduced <- y_complete |
88 | 3x |
for (stat in intersect(names(y_complete), empty_stats)) { |
89 | 4x |
y_reduced[[stat]] <- formatters::with_label(character(), obj_label(y_reduced[[stat]])) |
90 |
}
|
|
91 | 3x |
y_reduced
|
92 |
} else { |
|
93 | 6x |
y_complete
|
94 |
}
|
|
95 | 9x |
y
|
96 |
}
|
|
97 | ||
98 |
#' @describeIn count_patients_events_in_cols Layout-creating function which can take statistics function
|
|
99 |
#' arguments and additional format arguments. This function is a wrapper for [rtables::summarize_row_groups()].
|
|
100 |
#'
|
|
101 |
#' @param col_split (`flag`)\cr whether the columns should be split.
|
|
102 |
#' Set to `FALSE` when the required column split has been done already earlier in the layout pipe.
|
|
103 |
#'
|
|
104 |
#' @return
|
|
105 |
#' * `summarize_patients_events_in_cols()` returns a layout object suitable for passing to further layouting functions,
|
|
106 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted content rows
|
|
107 |
#' containing the statistics from `s_count_patients_and_multiple_events()` to the table layout.
|
|
108 |
#' @examples
|
|
109 |
#' # `summarize_patients_events_in_cols()`
|
|
110 |
#' basic_table() %>%
|
|
111 |
#' summarize_patients_events_in_cols(
|
|
112 |
#' filters_list = list(
|
|
113 |
#' related = formatters::with_label(c(AEREL = "Y"), "Events (Related)"),
|
|
114 |
#' fatal = c(AESDTH = "Y"),
|
|
115 |
#' fatal_related = c(AEREL = "Y", AESDTH = "Y")
|
|
116 |
#' ),
|
|
117 |
#' custom_label = "%s Total number of patients and events"
|
|
118 |
#' ) %>%
|
|
119 |
#' build_table(df)
|
|
120 |
#'
|
|
121 |
#' @export
|
|
122 |
summarize_patients_events_in_cols <- function(lyt, # nolint |
|
123 |
id = "USUBJID", |
|
124 |
filters_list = list(), |
|
125 |
...,
|
|
126 |
.stats = c( |
|
127 |
"unique",
|
|
128 |
"all",
|
|
129 |
names(filters_list) |
|
130 |
),
|
|
131 |
.labels = c( |
|
132 |
unique = "Patients (All)", |
|
133 |
all = "Events (All)", |
|
134 |
labels_or_names(filters_list) |
|
135 |
),
|
|
136 |
col_split = TRUE) { |
|
137 | 2x |
afun_list <- Map( |
138 | 2x |
function(stat) { |
139 | 7x |
make_afun( |
140 | 7x |
s_count_patients_and_multiple_events,
|
141 | 7x |
id = id, |
142 | 7x |
filters_list = filters_list, |
143 | 7x |
.stats = stat, |
144 | 7x |
.formats = "xx." |
145 |
)
|
|
146 |
},
|
|
147 | 2x |
stat = .stats |
148 |
)
|
|
149 | 2x |
if (col_split) { |
150 | 2x |
lyt <- split_cols_by_multivar( |
151 | 2x |
lyt = lyt, |
152 | 2x |
vars = rep(id, length(.stats)), |
153 | 2x |
varlabels = .labels[.stats] |
154 |
)
|
|
155 |
}
|
|
156 | 2x |
summarize_row_groups( |
157 | 2x |
lyt = lyt, |
158 | 2x |
cfun = afun_list, |
159 | 2x |
extra_args = list(...) |
160 |
)
|
|
161 |
}
|
1 |
#' Tabulate Survival Duration by Subgroup
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Tabulate statistics such as median survival time and hazard ratio for population subgroups.
|
|
6 |
#'
|
|
7 |
#' @inheritParams argument_convention
|
|
8 |
#' @inheritParams survival_coxph_pairwise
|
|
9 |
#' @param data (`data.frame`)\cr the dataset containing the variables to summarize.
|
|
10 |
#' @param groups_lists (named `list` of `list`)\cr optionally contains for each `subgroups` variable a list, which
|
|
11 |
#' specifies the new group levels via the names and the levels that belong to it in the character vectors that are
|
|
12 |
#' elements of the list.
|
|
13 |
#' @param label_all (`string`)\cr label for the total population analysis.
|
|
14 |
#' @param time_unit (`string`)\cr label with unit of median survival time. Default `NULL` skips displaying unit.
|
|
15 |
#'
|
|
16 |
#' @details These functions create a layout starting from a data frame which contains
|
|
17 |
#' the required statistics. Tables typically used as part of forest plot.
|
|
18 |
#'
|
|
19 |
#' @seealso [extract_survival_subgroups()]
|
|
20 |
#'
|
|
21 |
#' @examples
|
|
22 |
#' library(dplyr)
|
|
23 |
#' library(forcats)
|
|
24 |
#'
|
|
25 |
#' adtte <- tern_ex_adtte
|
|
26 |
#'
|
|
27 |
#' # Save variable labels before data processing steps.
|
|
28 |
#' adtte_labels <- formatters::var_labels(adtte)
|
|
29 |
#'
|
|
30 |
#' adtte_f <- adtte %>%
|
|
31 |
#' filter(
|
|
32 |
#' PARAMCD == "OS",
|
|
33 |
#' ARM %in% c("B: Placebo", "A: Drug X"),
|
|
34 |
#' SEX %in% c("M", "F")
|
|
35 |
#' ) %>%
|
|
36 |
#' mutate(
|
|
37 |
#' # Reorder levels of ARM to display reference arm before treatment arm.
|
|
38 |
#' ARM = droplevels(fct_relevel(ARM, "B: Placebo")),
|
|
39 |
#' SEX = droplevels(SEX),
|
|
40 |
#' AVALU = as.character(AVALU),
|
|
41 |
#' is_event = CNSR == 0
|
|
42 |
#' )
|
|
43 |
#' labels <- c(
|
|
44 |
#' "ARM" = adtte_labels[["ARM"]],
|
|
45 |
#' "SEX" = adtte_labels[["SEX"]],
|
|
46 |
#' "AVALU" = adtte_labels[["AVALU"]],
|
|
47 |
#' "is_event" = "Event Flag"
|
|
48 |
#' )
|
|
49 |
#' formatters::var_labels(adtte_f)[names(labels)] <- labels
|
|
50 |
#'
|
|
51 |
#' df <- extract_survival_subgroups(
|
|
52 |
#' variables = list(
|
|
53 |
#' tte = "AVAL",
|
|
54 |
#' is_event = "is_event",
|
|
55 |
#' arm = "ARM", subgroups = c("SEX", "BMRKR2")
|
|
56 |
#' ),
|
|
57 |
#' data = adtte_f
|
|
58 |
#' )
|
|
59 |
#' df
|
|
60 |
#'
|
|
61 |
#' @name survival_duration_subgroups
|
|
62 |
NULL
|
|
63 | ||
64 |
#' Prepares Survival Data for Population Subgroups in Data Frames
|
|
65 |
#'
|
|
66 |
#' @description `r lifecycle::badge("stable")`
|
|
67 |
#'
|
|
68 |
#' Prepares estimates of median survival times and treatment hazard ratios for population subgroups in
|
|
69 |
#' data frames. Simple wrapper for [h_survtime_subgroups_df()] and [h_coxph_subgroups_df()]. Result is a `list`
|
|
70 |
#' of two `data.frame`s: `survtime` and `hr`. `variables` corresponds to the names of variables found in `data`,
|
|
71 |
#' passed as a named `list` and requires elements `tte`, `is_event`, `arm` and optionally `subgroups` and `strat`.
|
|
72 |
#' `groups_lists` optionally specifies groupings for `subgroups` variables.
|
|
73 |
#'
|
|
74 |
#' @inheritParams argument_convention
|
|
75 |
#' @inheritParams survival_duration_subgroups
|
|
76 |
#' @inheritParams survival_coxph_pairwise
|
|
77 |
#'
|
|
78 |
#' @return A named `list` of two elements:
|
|
79 |
#' * `survtime`: A `data.frame` containing columns `arm`, `n`, `n_events`, `median`, `subgroup`, `var`,
|
|
80 |
#' `var_label`, and `row_type`.
|
|
81 |
#' * `hr`: A `data.frame` containing columns `arm`, `n_tot`, `n_tot_events`, `hr`, `lcl`, `ucl`, `conf_level`,
|
|
82 |
#' `pval`, `pval_label`, `subgroup`, `var`, `var_label`, and `row_type`.
|
|
83 |
#'
|
|
84 |
#' @seealso [survival_duration_subgroups]
|
|
85 |
#'
|
|
86 |
#' @examples
|
|
87 |
#' library(dplyr)
|
|
88 |
#' library(forcats)
|
|
89 |
#'
|
|
90 |
#' adtte <- tern_ex_adtte
|
|
91 |
#' adtte_labels <- formatters::var_labels(adtte)
|
|
92 |
#'
|
|
93 |
#' adtte_f <- adtte %>%
|
|
94 |
#' filter(
|
|
95 |
#' PARAMCD == "OS",
|
|
96 |
#' ARM %in% c("B: Placebo", "A: Drug X"),
|
|
97 |
#' SEX %in% c("M", "F")
|
|
98 |
#' ) %>%
|
|
99 |
#' mutate(
|
|
100 |
#' # Reorder levels of ARM to display reference arm before treatment arm.
|
|
101 |
#' ARM = droplevels(fct_relevel(ARM, "B: Placebo")),
|
|
102 |
#' SEX = droplevels(SEX),
|
|
103 |
#' AVALU = as.character(AVALU),
|
|
104 |
#' is_event = CNSR == 0
|
|
105 |
#' )
|
|
106 |
#' labels <- c(
|
|
107 |
#' "ARM" = adtte_labels[["ARM"]],
|
|
108 |
#' "SEX" = adtte_labels[["SEX"]],
|
|
109 |
#' "AVALU" = adtte_labels[["AVALU"]],
|
|
110 |
#' "is_event" = "Event Flag"
|
|
111 |
#' )
|
|
112 |
#' formatters::var_labels(adtte_f)[names(labels)] <- labels
|
|
113 |
#'
|
|
114 |
#' df <- extract_survival_subgroups(
|
|
115 |
#' variables = list(
|
|
116 |
#' tte = "AVAL",
|
|
117 |
#' is_event = "is_event",
|
|
118 |
#' arm = "ARM", subgroups = c("SEX", "BMRKR2")
|
|
119 |
#' ),
|
|
120 |
#' data = adtte_f
|
|
121 |
#' )
|
|
122 |
#' df
|
|
123 |
#'
|
|
124 |
#' df_grouped <- extract_survival_subgroups(
|
|
125 |
#' variables = list(
|
|
126 |
#' tte = "AVAL",
|
|
127 |
#' is_event = "is_event",
|
|
128 |
#' arm = "ARM", subgroups = c("SEX", "BMRKR2")
|
|
129 |
#' ),
|
|
130 |
#' data = adtte_f,
|
|
131 |
#' groups_lists = list(
|
|
132 |
#' BMRKR2 = list(
|
|
133 |
#' "low" = "LOW",
|
|
134 |
#' "low/medium" = c("LOW", "MEDIUM"),
|
|
135 |
#' "low/medium/high" = c("LOW", "MEDIUM", "HIGH")
|
|
136 |
#' )
|
|
137 |
#' )
|
|
138 |
#' )
|
|
139 |
#' df_grouped
|
|
140 |
#'
|
|
141 |
#' @export
|
|
142 |
extract_survival_subgroups <- function(variables, |
|
143 |
data,
|
|
144 |
groups_lists = list(), |
|
145 |
control = control_coxph(), |
|
146 |
label_all = "All Patients") { |
|
147 | 8x |
df_survtime <- h_survtime_subgroups_df( |
148 | 8x |
variables,
|
149 | 8x |
data,
|
150 | 8x |
groups_lists = groups_lists, |
151 | 8x |
label_all = label_all |
152 |
)
|
|
153 | 8x |
df_hr <- h_coxph_subgroups_df( |
154 | 8x |
variables,
|
155 | 8x |
data,
|
156 | 8x |
groups_lists = groups_lists, |
157 | 8x |
control = control, |
158 | 8x |
label_all = label_all |
159 |
)
|
|
160 | ||
161 | 8x |
list(survtime = df_survtime, hr = df_hr) |
162 |
}
|
|
163 | ||
164 |
#' @describeIn survival_duration_subgroups Formatted analysis function which is used as
|
|
165 |
#' `afun` in `tabulate_survival_subgroups()`.
|
|
166 |
#'
|
|
167 |
#' @return
|
|
168 |
#' * `a_survival_subgroups()` returns the corresponding list with formatted [rtables::CellValue()].
|
|
169 |
#'
|
|
170 |
#' @keywords internal
|
|
171 |
a_survival_subgroups <- function(.formats = list( |
|
172 |
n = "xx", |
|
173 |
n_events = "xx", |
|
174 |
n_tot_events = "xx", |
|
175 |
median = "xx.x", |
|
176 |
n_tot = "xx", |
|
177 |
hr = list(format_extreme_values(2L)), |
|
178 |
ci = list(format_extreme_values_ci(2L)), |
|
179 |
pval = "x.xxxx | (<0.0001)" |
|
180 |
)) { |
|
181 | 12x |
checkmate::assert_list(.formats) |
182 | 12x |
checkmate::assert_subset( |
183 | 12x |
names(.formats), |
184 | 12x |
c("n", "n_events", "median", "n_tot", "n_tot_events", "hr", "ci", "pval") |
185 |
)
|
|
186 | ||
187 | 12x |
afun_lst <- Map( |
188 | 12x |
function(stat, fmt) { |
189 | 90x |
if (stat == "ci") { |
190 | 11x |
function(df, labelstr = "", ...) { |
191 | 20x |
in_rows( |
192 | 20x |
.list = combine_vectors(df$lcl, df$ucl), |
193 | 20x |
.labels = as.character(df$subgroup), |
194 | 20x |
.formats = fmt |
195 |
)
|
|
196 |
}
|
|
197 |
} else { |
|
198 | 79x |
function(df, labelstr = "", ...) { |
199 | 111x |
in_rows( |
200 | 111x |
.list = as.list(df[[stat]]), |
201 | 111x |
.labels = as.character(df$subgroup), |
202 | 111x |
.formats = fmt |
203 |
)
|
|
204 |
}
|
|
205 |
}
|
|
206 |
},
|
|
207 | 12x |
stat = names(.formats), |
208 | 12x |
fmt = .formats |
209 |
)
|
|
210 | ||
211 | 12x |
afun_lst
|
212 |
}
|
|
213 | ||
214 |
#' @describeIn survival_duration_subgroups Table-creating function which creates a table
|
|
215 |
#' summarizing survival by subgroup. This function is a wrapper for [rtables::analyze_colvars()]
|
|
216 |
#' and [rtables::summarize_row_groups()].
|
|
217 |
#'
|
|
218 |
#' @param df (`list`)\cr of data frames containing all analysis variables. List should be
|
|
219 |
#' created using [extract_survival_subgroups()].
|
|
220 |
#' @param vars (`character`)\cr the name of statistics to be reported among:
|
|
221 |
#' * `n_tot_events`: Total number of events per group.
|
|
222 |
#' * `n_events`: Number of events per group.
|
|
223 |
#' * `n_tot`: Total number of observations per group.
|
|
224 |
#' * `n`: Number of observations per group.
|
|
225 |
#' * `median`: Median survival time.
|
|
226 |
#' * `hr`: Hazard ratio.
|
|
227 |
#' * `ci`: Confidence interval of hazard ratio.
|
|
228 |
#' * `pval`: p-value of the effect.
|
|
229 |
#' Note, one of the statistics `n_tot` and `n_tot_events`, as well as both `hr` and `ci`
|
|
230 |
#' are required.
|
|
231 |
#'
|
|
232 |
#' @return An `rtables` table summarizing survival by subgroup.
|
|
233 |
#'
|
|
234 |
#' @examples
|
|
235 |
#' ## Table with default columns.
|
|
236 |
#' basic_table() %>%
|
|
237 |
#' tabulate_survival_subgroups(df, time_unit = adtte_f$AVALU[1])
|
|
238 |
#'
|
|
239 |
#' ## Table with a manually chosen set of columns: adding "pval".
|
|
240 |
#' basic_table() %>%
|
|
241 |
#' tabulate_survival_subgroups(
|
|
242 |
#' df = df,
|
|
243 |
#' vars = c("n_tot_events", "n_events", "median", "hr", "ci", "pval"),
|
|
244 |
#' time_unit = adtte_f$AVALU[1]
|
|
245 |
#' )
|
|
246 |
#'
|
|
247 |
#' @export
|
|
248 |
tabulate_survival_subgroups <- function(lyt, |
|
249 |
df,
|
|
250 |
vars = c("n_tot_events", "n_events", "median", "hr", "ci"), |
|
251 |
time_unit = NULL) { |
|
252 | 5x |
conf_level <- df$hr$conf_level[1] |
253 | 5x |
method <- df$hr$pval_label[1] |
254 | ||
255 | 5x |
afun_lst <- a_survival_subgroups() |
256 | 5x |
colvars <- d_survival_subgroups_colvars( |
257 | 5x |
vars,
|
258 | 5x |
conf_level = conf_level, |
259 | 5x |
method = method, |
260 | 5x |
time_unit = time_unit |
261 |
)
|
|
262 | ||
263 | 5x |
colvars_survtime <- list( |
264 | 5x |
vars = colvars$vars[names(colvars$labels) %in% c("n", "n_events", "median")], |
265 | 5x |
labels = colvars$labels[names(colvars$labels) %in% c("n", "n_events", "median")] |
266 |
)
|
|
267 | 5x |
colvars_hr <- list( |
268 | 5x |
vars = colvars$vars[names(colvars$labels) %in% c("n_tot", "n_tot_events", "hr", "ci", "pval")], |
269 | 5x |
labels = colvars$labels[names(colvars$labels) %in% c("n_tot", "n_tot_events", "hr", "ci", "pval")] |
270 |
)
|
|
271 | ||
272 |
# Columns from table_survtime are optional.
|
|
273 | 5x |
if (length(colvars_survtime$vars) > 0) { |
274 | 4x |
lyt_survtime <- split_cols_by(lyt = lyt, var = "arm") |
275 | 4x |
lyt_survtime <- split_rows_by( |
276 | 4x |
lyt = lyt_survtime, |
277 | 4x |
var = "row_type", |
278 | 4x |
split_fun = keep_split_levels("content"), |
279 | 4x |
nested = FALSE |
280 |
)
|
|
281 | 4x |
lyt_survtime <- summarize_row_groups( |
282 | 4x |
lyt = lyt_survtime, |
283 | 4x |
var = "var_label", |
284 | 4x |
cfun = afun_lst[names(colvars_survtime$labels)] |
285 |
)
|
|
286 | 4x |
lyt_survtime <- split_cols_by_multivar( |
287 | 4x |
lyt = lyt_survtime, |
288 | 4x |
vars = colvars_survtime$vars, |
289 | 4x |
varlabels = colvars_survtime$labels |
290 |
)
|
|
291 | ||
292 | 4x |
if ("analysis" %in% df$survtime$row_type) { |
293 | 3x |
lyt_survtime <- split_rows_by( |
294 | 3x |
lyt = lyt_survtime, |
295 | 3x |
var = "row_type", |
296 | 3x |
split_fun = keep_split_levels("analysis"), |
297 | 3x |
nested = FALSE, |
298 | 3x |
child_labels = "hidden" |
299 |
)
|
|
300 | 3x |
lyt_survtime <- split_rows_by(lyt = lyt_survtime, var = "var_label", nested = TRUE) |
301 | 3x |
lyt_survtime <- analyze_colvars( |
302 | 3x |
lyt = lyt_survtime, |
303 | 3x |
afun = afun_lst[names(colvars_survtime$labels)], |
304 | 3x |
inclNAs = TRUE |
305 |
)
|
|
306 |
}
|
|
307 | ||
308 | 4x |
table_survtime <- build_table(lyt_survtime, df = df$survtime) |
309 |
} else { |
|
310 | 1x |
table_survtime <- NULL |
311 |
}
|
|
312 | ||
313 |
# Columns "n_tot_events" or "n_tot", and "hr", "ci" in table_hr are required.
|
|
314 | 5x |
lyt_hr <- split_cols_by(lyt = lyt, var = "arm") |
315 | 5x |
lyt_hr <- split_rows_by( |
316 | 5x |
lyt = lyt_hr, |
317 | 5x |
var = "row_type", |
318 | 5x |
split_fun = keep_split_levels("content"), |
319 | 5x |
nested = FALSE |
320 |
)
|
|
321 | 5x |
lyt_hr <- summarize_row_groups( |
322 | 5x |
lyt = lyt_hr, |
323 | 5x |
var = "var_label", |
324 | 5x |
cfun = afun_lst[names(colvars_hr$labels)] |
325 |
)
|
|
326 | 5x |
lyt_hr <- split_cols_by_multivar( |
327 | 5x |
lyt = lyt_hr, |
328 | 5x |
vars = colvars_hr$vars, |
329 | 5x |
varlabels = colvars_hr$labels |
330 |
) %>% |
|
331 | 5x |
append_topleft("Baseline Risk Factors") |
332 | ||
333 | 5x |
if ("analysis" %in% df$survtime$row_type) { |
334 | 4x |
lyt_hr <- split_rows_by( |
335 | 4x |
lyt = lyt_hr, |
336 | 4x |
var = "row_type", |
337 | 4x |
split_fun = keep_split_levels("analysis"), |
338 | 4x |
nested = FALSE, |
339 | 4x |
child_labels = "hidden" |
340 |
)
|
|
341 | 4x |
lyt_hr <- split_rows_by(lyt = lyt_hr, var = "var_label", nested = TRUE) |
342 | 4x |
lyt_hr <- analyze_colvars( |
343 | 4x |
lyt = lyt_hr, |
344 | 4x |
afun = afun_lst[names(colvars_hr$labels)], |
345 | 4x |
inclNAs = TRUE |
346 |
)
|
|
347 |
}
|
|
348 | 5x |
table_hr <- build_table(lyt_hr, df = df$hr) |
349 | ||
350 |
# There can be one or two vars starting with "n_tot".
|
|
351 | 5x |
n_tot_ids <- grep("^n_tot", colvars_hr$vars) |
352 | 5x |
if (is.null(table_survtime)) { |
353 | 1x |
result <- table_hr |
354 | 1x |
hr_id <- match("hr", colvars_hr$vars) |
355 | 1x |
ci_id <- match("lcl", colvars_hr$vars) |
356 |
} else { |
|
357 |
# Reorder the table.
|
|
358 | 4x |
result <- cbind_rtables(table_hr[, n_tot_ids], table_survtime, table_hr[, -n_tot_ids]) |
359 |
# And then calculate column indices accordingly.
|
|
360 | 4x |
hr_id <- length(n_tot_ids) + ncol(table_survtime) + match("hr", colvars_hr$vars[-n_tot_ids]) |
361 | 4x |
ci_id <- length(n_tot_ids) + ncol(table_survtime) + match("lcl", colvars_hr$vars[-n_tot_ids]) |
362 | 4x |
n_tot_ids <- seq_along(n_tot_ids) |
363 |
}
|
|
364 | ||
365 | 5x |
structure( |
366 | 5x |
result,
|
367 | 5x |
forest_header = paste0(rev(levels(df$survtime$arm)), "\nBetter"), |
368 | 5x |
col_x = hr_id, |
369 | 5x |
col_ci = ci_id, |
370 |
# Take the first one for scaling the symbol sizes in graph.
|
|
371 | 5x |
col_symbol_size = n_tot_ids[1] |
372 |
)
|
|
373 |
}
|
|
374 | ||
375 |
#' Labels for Column Variables in Survival Duration by Subgroup Table
|
|
376 |
#'
|
|
377 |
#' @description `r lifecycle::badge("stable")`
|
|
378 |
#'
|
|
379 |
#' Internal function to check variables included in [tabulate_survival_subgroups()] and create column labels.
|
|
380 |
#'
|
|
381 |
#' @inheritParams tabulate_survival_subgroups
|
|
382 |
#' @inheritParams argument_convention
|
|
383 |
#' @param method (`character`)\cr p-value method for testing hazard ratio = 1.
|
|
384 |
#'
|
|
385 |
#' @return A `list` of variables and their labels to tabulate.
|
|
386 |
#'
|
|
387 |
#' @note At least one of `n_tot` and `n_tot_events` must be provided in `vars`.
|
|
388 |
#'
|
|
389 |
#' @export
|
|
390 |
d_survival_subgroups_colvars <- function(vars, |
|
391 |
conf_level,
|
|
392 |
method,
|
|
393 |
time_unit = NULL) { |
|
394 | 12x |
checkmate::assert_character(vars) |
395 | 12x |
checkmate::assert_string(time_unit, null.ok = TRUE) |
396 | 12x |
checkmate::assert_subset(c("hr", "ci"), vars) |
397 | 12x |
checkmate::assert_true(any(c("n_tot", "n_tot_events") %in% vars)) |
398 | 12x |
checkmate::assert_subset( |
399 | 12x |
vars,
|
400 | 12x |
c("n", "n_events", "median", "n_tot", "n_tot_events", "hr", "ci", "pval") |
401 |
)
|
|
402 | ||
403 | 12x |
propcase_time_label <- if (!is.null(time_unit)) { |
404 | 11x |
paste0("Median (", time_unit, ")") |
405 |
} else { |
|
406 | 1x |
"Median"
|
407 |
}
|
|
408 | ||
409 | 12x |
varlabels <- c( |
410 | 12x |
n = "n", |
411 | 12x |
n_events = "Events", |
412 | 12x |
median = propcase_time_label, |
413 | 12x |
n_tot = "Total n", |
414 | 12x |
n_tot_events = "Total Events", |
415 | 12x |
hr = "Hazard Ratio", |
416 | 12x |
ci = paste0(100 * conf_level, "% Wald CI"), |
417 | 12x |
pval = method |
418 |
)
|
|
419 | ||
420 | 12x |
colvars <- vars |
421 | ||
422 |
# The `lcl` variable is just a placeholder available in the analysis data,
|
|
423 |
# it is not acutally used in the tabulation.
|
|
424 |
# Variables used in the tabulation are lcl and ucl, see `a_survival_subgroups` for details.
|
|
425 | 12x |
colvars[colvars == "ci"] <- "lcl" |
426 | ||
427 | 12x |
list( |
428 | 12x |
vars = colvars, |
429 | 12x |
labels = varlabels[vars] |
430 |
)
|
|
431 |
}
|
1 |
#' Patient Counts for Laboratory Events (Worsen From Baseline) by Highest Grade Post-Baseline
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Patient count and fraction for laboratory events (worsen from baseline) shift table.
|
|
6 |
#'
|
|
7 |
#' @inheritParams argument_convention
|
|
8 |
#'
|
|
9 |
#' @seealso Relevant helper functions [h_adlb_worsen()] and [h_worsen_counter()]
|
|
10 |
#'
|
|
11 |
#' @name abnormal_by_worst_grade_worsen
|
|
12 |
NULL
|
|
13 | ||
14 |
#' Helper Function to Prepare `ADLB` with Worst Labs
|
|
15 |
#'
|
|
16 |
#' @description `r lifecycle::badge("stable")`
|
|
17 |
#'
|
|
18 |
#' Helper function to prepare a `df` for generate the patient count shift table
|
|
19 |
#'
|
|
20 |
#' @param adlb (`data.frame`)\cr `ADLB` dataframe
|
|
21 |
#' @param worst_flag_low (named `vector`)\cr Worst low post-baseline lab grade flag variable
|
|
22 |
#' @param worst_flag_high (named `vector`)\cr Worst high post-baseline lab grade flag variable
|
|
23 |
#' @param direction_var (`string`)\cr Direction variable specifying the direction of the shift table of interest.
|
|
24 |
#' Only lab records flagged by `L`, `H` or `B` are included in the shift table.
|
|
25 |
#' * `L`: low direction only
|
|
26 |
#' * `H`: high direction only
|
|
27 |
#' * `B`: both low and high directions
|
|
28 |
#'
|
|
29 |
#' @return `h_adlb_worsen()` returns the `adlb` `data.frame` containing only the
|
|
30 |
#' worst labs specified according to `worst_flag_low` or `worst_flag_high` for the
|
|
31 |
#' direction specified according to `direction_var`. For instance, for a lab that is
|
|
32 |
#' needed for the low direction only, only records flagged by `worst_flag_low` are
|
|
33 |
#' selected. For a lab that is needed for both low and high directions, the worst
|
|
34 |
#' low records are selected for the low direction, and the worst high record are selected
|
|
35 |
#' for the high direction.
|
|
36 |
#'
|
|
37 |
#' @seealso [abnormal_by_worst_grade_worsen]
|
|
38 |
#'
|
|
39 |
#' @examples
|
|
40 |
#' library(dplyr)
|
|
41 |
#'
|
|
42 |
#' # The direction variable, GRADDR, is based on metadata
|
|
43 |
#' adlb <- tern_ex_adlb %>%
|
|
44 |
#' mutate(
|
|
45 |
#' GRADDR = case_when(
|
|
46 |
#' PARAMCD == "ALT" ~ "B",
|
|
47 |
#' PARAMCD == "CRP" ~ "L",
|
|
48 |
#' PARAMCD == "IGA" ~ "H"
|
|
49 |
#' )
|
|
50 |
#' ) %>%
|
|
51 |
#' filter(SAFFL == "Y" & ONTRTFL == "Y" & GRADDR != "")
|
|
52 |
#'
|
|
53 |
#' df <- h_adlb_worsen(
|
|
54 |
#' adlb,
|
|
55 |
#' worst_flag_low = c("WGRLOFL" = "Y"),
|
|
56 |
#' worst_flag_high = c("WGRHIFL" = "Y"),
|
|
57 |
#' direction_var = "GRADDR"
|
|
58 |
#' )
|
|
59 |
#'
|
|
60 |
#' @export
|
|
61 |
h_adlb_worsen <- function(adlb, |
|
62 |
worst_flag_low = NULL, |
|
63 |
worst_flag_high = NULL, |
|
64 |
direction_var) { |
|
65 | 5x |
checkmate::assert_string(direction_var) |
66 | 5x |
checkmate::assert_subset(as.character(unique(adlb[[direction_var]])), c("B", "L", "H")) |
67 | 5x |
assert_df_with_variables(adlb, list("Col" = direction_var)) |
68 | ||
69 | 5x |
if (any(unique(adlb[[direction_var]]) == "H")) { |
70 | 4x |
assert_df_with_variables(adlb, list("High" = names(worst_flag_high))) |
71 |
}
|
|
72 | ||
73 | 5x |
if (any(unique(adlb[[direction_var]]) == "L")) { |
74 | 4x |
assert_df_with_variables(adlb, list("Low" = names(worst_flag_low))) |
75 |
}
|
|
76 | ||
77 | 5x |
if (any(unique(adlb[[direction_var]]) == "B")) { |
78 | 3x |
assert_df_with_variables( |
79 | 3x |
adlb,
|
80 | 3x |
list( |
81 | 3x |
"Low" = names(worst_flag_low), |
82 | 3x |
"High" = names(worst_flag_high) |
83 |
)
|
|
84 |
)
|
|
85 |
}
|
|
86 | ||
87 |
# extract patients with worst post-baseline lab, either low or high or both
|
|
88 | 5x |
worst_flag <- c(worst_flag_low, worst_flag_high) |
89 | 5x |
col_names <- names(worst_flag) |
90 | 5x |
filter_values <- worst_flag |
91 | 5x |
temp <- Map( |
92 | 5x |
function(x, y) which(adlb[[x]] == y), |
93 | 5x |
col_names,
|
94 | 5x |
filter_values
|
95 |
)
|
|
96 | 5x |
position_satisfy_filters <- Reduce(union, temp) |
97 | ||
98 |
# select variables of interest
|
|
99 | 5x |
adlb_f <- adlb[position_satisfy_filters, ] |
100 | ||
101 |
# generate subsets for different directionality
|
|
102 | 5x |
adlb_f_h <- adlb_f[which(adlb_f[[direction_var]] == "H"), ] |
103 | 5x |
adlb_f_l <- adlb_f[which(adlb_f[[direction_var]] == "L"), ] |
104 | 5x |
adlb_f_b <- adlb_f[which(adlb_f[[direction_var]] == "B"), ] |
105 | ||
106 |
# for labs requiring both high and low, data is duplicated and will be stacked on top of each other
|
|
107 | 5x |
adlb_f_b_h <- adlb_f_b |
108 | 5x |
adlb_f_b_l <- adlb_f_b |
109 | ||
110 |
# extract data with worst lab
|
|
111 | 5x |
if (!is.null(worst_flag_high) && !is.null(worst_flag_low)) { |
112 |
# change H to High, L to Low
|
|
113 | 3x |
adlb_f_h[[direction_var]] <- rep("High", nrow(adlb_f_h)) |
114 | 3x |
adlb_f_l[[direction_var]] <- rep("Low", nrow(adlb_f_l)) |
115 | ||
116 |
# change, B to High and Low
|
|
117 | 3x |
adlb_f_b_h[[direction_var]] <- rep("High", nrow(adlb_f_b_h)) |
118 | 3x |
adlb_f_b_l[[direction_var]] <- rep("Low", nrow(adlb_f_b_l)) |
119 | ||
120 | 3x |
adlb_out_h <- adlb_f_h[which(adlb_f_h[[names(worst_flag_high)]] == worst_flag_high), ] |
121 | 3x |
adlb_out_b_h <- adlb_f_b_h[which(adlb_f_b_h[[names(worst_flag_high)]] == worst_flag_high), ] |
122 | 3x |
adlb_out_l <- adlb_f_l[which(adlb_f_l[[names(worst_flag_low)]] == worst_flag_low), ] |
123 | 3x |
adlb_out_b_l <- adlb_f_b_l[which(adlb_f_b_l[[names(worst_flag_low)]] == worst_flag_low), ] |
124 | ||
125 | 3x |
out <- rbind(adlb_out_h, adlb_out_b_h, adlb_out_l, adlb_out_b_l) |
126 | 2x |
} else if (!is.null(worst_flag_high)) { |
127 | 1x |
adlb_f_h[[direction_var]] <- rep("High", nrow(adlb_f_h)) |
128 | 1x |
adlb_f_b_h[[direction_var]] <- rep("High", nrow(adlb_f_b_h)) |
129 | ||
130 | 1x |
adlb_out_h <- adlb_f_h[which(adlb_f_h[[names(worst_flag_high)]] == worst_flag_high), ] |
131 | 1x |
adlb_out_b_h <- adlb_f_b_h[which(adlb_f_b_h[[names(worst_flag_high)]] == worst_flag_high), ] |
132 | ||
133 | 1x |
out <- rbind(adlb_out_h, adlb_out_b_h) |
134 | 1x |
} else if (!is.null(worst_flag_low)) { |
135 | 1x |
adlb_f_l[[direction_var]] <- rep("Low", nrow(adlb_f_l)) |
136 | 1x |
adlb_f_b_l[[direction_var]] <- rep("Low", nrow(adlb_f_b_l)) |
137 | ||
138 | 1x |
adlb_out_l <- adlb_f_l[which(adlb_f_l[[names(worst_flag_low)]] == worst_flag_low), ] |
139 | 1x |
adlb_out_b_l <- adlb_f_b_l[which(adlb_f_b_l[[names(worst_flag_low)]] == worst_flag_low), ] |
140 | ||
141 | 1x |
out <- rbind(adlb_out_l, adlb_out_b_l) |
142 |
}
|
|
143 | ||
144 |
# label
|
|
145 | 5x |
formatters::var_labels(out) <- formatters::var_labels(adlb_f, fill = FALSE) |
146 |
# NA
|
|
147 | 5x |
out
|
148 |
}
|
|
149 | ||
150 |
#' Helper Function to Analyze Patients for [s_count_abnormal_lab_worsen_by_baseline()]
|
|
151 |
#'
|
|
152 |
#' @description `r lifecycle::badge("stable")`
|
|
153 |
#'
|
|
154 |
#' Helper function to count the number of patients and the fraction of patients according to
|
|
155 |
#' highest post-baseline lab grade variable `.var`, baseline lab grade variable `baseline_var`,
|
|
156 |
#' and the direction of interest specified in `direction_var`.
|
|
157 |
#'
|
|
158 |
#' @inheritParams argument_convention
|
|
159 |
#' @inheritParams h_adlb_worsen
|
|
160 |
#' @param baseline_var (`string`)\cr baseline lab grade variable
|
|
161 |
#'
|
|
162 |
#' @return `h_worsen_counter()` returns the counts and fraction of patients
|
|
163 |
#' whose worst post-baseline lab grades are worse than their baseline grades, for
|
|
164 |
#' post-baseline worst grades "1", "2", "3", "4" and "Any".
|
|
165 |
#'
|
|
166 |
#' @seealso [abnormal_by_worst_grade_worsen]
|
|
167 |
#'
|
|
168 |
#' @examples
|
|
169 |
#' library(dplyr)
|
|
170 |
#'
|
|
171 |
#' # The direction variable, GRADDR, is based on metadata
|
|
172 |
#' adlb <- tern_ex_adlb %>%
|
|
173 |
#' mutate(
|
|
174 |
#' GRADDR = case_when(
|
|
175 |
#' PARAMCD == "ALT" ~ "B",
|
|
176 |
#' PARAMCD == "CRP" ~ "L",
|
|
177 |
#' PARAMCD == "IGA" ~ "H"
|
|
178 |
#' )
|
|
179 |
#' ) %>%
|
|
180 |
#' filter(SAFFL == "Y" & ONTRTFL == "Y" & GRADDR != "")
|
|
181 |
#'
|
|
182 |
#' df <- h_adlb_worsen(
|
|
183 |
#' adlb,
|
|
184 |
#' worst_flag_low = c("WGRLOFL" = "Y"),
|
|
185 |
#' worst_flag_high = c("WGRHIFL" = "Y"),
|
|
186 |
#' direction_var = "GRADDR"
|
|
187 |
#' )
|
|
188 |
#'
|
|
189 |
#' # `h_worsen_counter`
|
|
190 |
#' h_worsen_counter(
|
|
191 |
#' df %>% filter(PARAMCD == "CRP" & GRADDR == "Low"),
|
|
192 |
#' id = "USUBJID",
|
|
193 |
#' .var = "ATOXGR",
|
|
194 |
#' baseline_var = "BTOXGR",
|
|
195 |
#' direction_var = "GRADDR"
|
|
196 |
#' )
|
|
197 |
#'
|
|
198 |
#' @export
|
|
199 |
h_worsen_counter <- function(df, id, .var, baseline_var, direction_var) { |
|
200 | 17x |
checkmate::assert_string(id) |
201 | 17x |
checkmate::assert_string(.var) |
202 | 17x |
checkmate::assert_string(baseline_var) |
203 | 17x |
checkmate::assert_scalar(unique(df[[direction_var]])) |
204 | 17x |
checkmate::assert_subset(unique(df[[direction_var]]), c("High", "Low")) |
205 | 17x |
assert_df_with_variables(df, list(val = c(id, .var, baseline_var, direction_var))) |
206 | ||
207 |
# remove post-baseline missing
|
|
208 | 17x |
df <- df[df[[.var]] != "<Missing>", ] |
209 | ||
210 |
# obtain directionality
|
|
211 | 17x |
direction <- unique(df[[direction_var]]) |
212 | ||
213 | 17x |
if (direction == "Low") { |
214 | 10x |
grade <- -1:-4 |
215 | 10x |
worst_grade <- -4 |
216 | 7x |
} else if (direction == "High") { |
217 | 7x |
grade <- 1:4 |
218 | 7x |
worst_grade <- 4 |
219 |
}
|
|
220 | ||
221 | 17x |
if (nrow(df) > 0) { |
222 | 17x |
by_grade <- lapply(grade, function(i) { |
223 |
# filter baseline values that is less than i or <Missing>
|
|
224 | 68x |
df_temp <- df[df[[baseline_var]] %in% c((i + sign(i) * -1):(-1 * worst_grade), "<Missing>"), ] |
225 |
# num: number of patients with post-baseline worst lab equal to i
|
|
226 | 68x |
num <- length(unique(df_temp[df_temp[[.var]] %in% i, id, drop = TRUE])) |
227 |
# denom: number of patients with baseline values less than i or <missing> and post-baseline in the same direction
|
|
228 | 68x |
denom <- length(unique(df_temp[[id]])) |
229 | 68x |
rm(df_temp) |
230 | 68x |
c(num = num, denom = denom) |
231 |
}) |
|
232 |
} else { |
|
233 | ! |
by_grade <- lapply(1, function(i) { |
234 | ! |
c(num = 0, denom = 0) |
235 |
}) |
|
236 |
}
|
|
237 | ||
238 | 17x |
names(by_grade) <- as.character(seq_along(by_grade)) |
239 | ||
240 |
# baseline grade less 4 or missing
|
|
241 | 17x |
df_temp <- df[!df[[baseline_var]] %in% worst_grade, ] |
242 | ||
243 |
# denom: number of patients with baseline values less than 4 or <missing> and post-baseline in the same direction
|
|
244 | 17x |
denom <- length(unique(df_temp[, id, drop = TRUE])) |
245 | ||
246 |
# condition 1: missing baseline and in the direction of abnormality
|
|
247 | 17x |
con1 <- which(df_temp[[baseline_var]] == "<Missing>" & df_temp[[.var]] %in% grade) |
248 | 17x |
df_temp_nm <- df_temp[which(df_temp[[baseline_var]] != "<Missing>" & df_temp[[.var]] %in% grade), ] |
249 | ||
250 |
# condition 2: if post-baseline values are present then post-baseline values must be worse than baseline
|
|
251 | 17x |
if (direction == "Low") { |
252 | 10x |
con2 <- which(as.numeric(as.character(df_temp_nm[[.var]])) < as.numeric(as.character(df_temp_nm[[baseline_var]]))) |
253 |
} else { |
|
254 | 7x |
con2 <- which(as.numeric(as.character(df_temp_nm[[.var]])) > as.numeric(as.character(df_temp_nm[[baseline_var]]))) |
255 |
}
|
|
256 | ||
257 |
# number of patients satisfy either conditions 1 or 2
|
|
258 | 17x |
num <- length(unique(df_temp[union(con1, con2), id, drop = TRUE])) |
259 | ||
260 | 17x |
list(fraction = c(by_grade, list("Any" = c(num = num, denom = denom)))) |
261 |
}
|
|
262 | ||
263 |
#' @describeIn abnormal_by_worst_grade_worsen Statistics function for patients whose worst post-baseline
|
|
264 |
#' lab grades are worse than their baseline grades.
|
|
265 |
#'
|
|
266 |
#' @param variables (named `list` of `string`)\cr list of additional analysis variables including:
|
|
267 |
#' * `id` (`string`)\cr subject variable name.
|
|
268 |
#' * `baseline_var` (`string`)\cr name of the data column containing baseline toxicity variable.
|
|
269 |
#' * `direction_var` (`string`)\cr see `direction_var` for more details.
|
|
270 |
#'
|
|
271 |
#' @return
|
|
272 |
#' * `s_count_abnormal_lab_worsen_by_baseline()` returns the counts and fraction of patients whose worst
|
|
273 |
#' post-baseline lab grades are worse than their baseline grades, for post-baseline worst grades
|
|
274 |
#' "1", "2", "3", "4" and "Any".
|
|
275 |
#'
|
|
276 |
#' @examples
|
|
277 |
#' library(dplyr)
|
|
278 |
#'
|
|
279 |
#' # The direction variable, GRADDR, is based on metadata
|
|
280 |
#' adlb <- tern_ex_adlb %>%
|
|
281 |
#' mutate(
|
|
282 |
#' GRADDR = case_when(
|
|
283 |
#' PARAMCD == "ALT" ~ "B",
|
|
284 |
#' PARAMCD == "CRP" ~ "L",
|
|
285 |
#' PARAMCD == "IGA" ~ "H"
|
|
286 |
#' )
|
|
287 |
#' ) %>%
|
|
288 |
#' filter(SAFFL == "Y" & ONTRTFL == "Y" & GRADDR != "")
|
|
289 |
#'
|
|
290 |
#' df <- h_adlb_worsen(
|
|
291 |
#' adlb,
|
|
292 |
#' worst_flag_low = c("WGRLOFL" = "Y"),
|
|
293 |
#' worst_flag_high = c("WGRHIFL" = "Y"),
|
|
294 |
#' direction_var = "GRADDR"
|
|
295 |
#' )
|
|
296 |
#'
|
|
297 |
#' @keywords internal
|
|
298 |
s_count_abnormal_lab_worsen_by_baseline <- function(df, # nolint |
|
299 |
.var = "ATOXGR", |
|
300 |
variables = list( |
|
301 |
id = "USUBJID", |
|
302 |
baseline_var = "BTOXGR", |
|
303 |
direction_var = "GRADDR" |
|
304 |
)) { |
|
305 | 1x |
checkmate::assert_string(.var) |
306 | 1x |
checkmate::assert_set_equal(names(variables), c("id", "baseline_var", "direction_var")) |
307 | 1x |
checkmate::assert_string(variables$id) |
308 | 1x |
checkmate::assert_string(variables$baseline_var) |
309 | 1x |
checkmate::assert_string(variables$direction_var) |
310 | 1x |
assert_df_with_variables(df, c(aval = .var, variables[1:3])) |
311 | 1x |
assert_list_of_variables(variables) |
312 | ||
313 | 1x |
h_worsen_counter(df, variables$id, .var, variables$baseline_var, variables$direction_var) |
314 |
}
|
|
315 | ||
316 | ||
317 |
#' @describeIn abnormal_by_worst_grade_worsen Formatted analysis function which is used as `afun`
|
|
318 |
#' in `count_abnormal_lab_worsen_by_baseline()`.
|
|
319 |
#'
|
|
320 |
#' @return
|
|
321 |
#' * `a_count_abnormal_lab_worsen_by_baseline()` returns the corresponding list with
|
|
322 |
#' formatted [rtables::CellValue()].
|
|
323 |
#'
|
|
324 |
#' @keywords internal
|
|
325 |
a_count_abnormal_lab_worsen_by_baseline <- make_afun( # nolint |
|
326 |
s_count_abnormal_lab_worsen_by_baseline,
|
|
327 |
.formats = c(fraction = format_fraction), |
|
328 |
.ungroup_stats = "fraction" |
|
329 |
)
|
|
330 | ||
331 |
#' @describeIn abnormal_by_worst_grade_worsen Layout-creating function which can take statistics function
|
|
332 |
#' arguments and additional format arguments. This function is a wrapper for [rtables::analyze()].
|
|
333 |
#'
|
|
334 |
#' @return
|
|
335 |
#' * `count_abnormal_lab_worsen_by_baseline()` returns a layout object suitable for passing to further layouting
|
|
336 |
#' functions, or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted
|
|
337 |
#' rows containing the statistics from `s_count_abnormal_lab_worsen_by_baseline()` to the table layout.
|
|
338 |
#'
|
|
339 |
#' @examples
|
|
340 |
#' basic_table() %>%
|
|
341 |
#' split_cols_by("ARMCD") %>%
|
|
342 |
#' add_colcounts() %>%
|
|
343 |
#' split_rows_by("PARAMCD") %>%
|
|
344 |
#' split_rows_by("GRADDR") %>%
|
|
345 |
#' count_abnormal_lab_worsen_by_baseline(
|
|
346 |
#' var = "ATOXGR",
|
|
347 |
#' variables = list(
|
|
348 |
#' id = "USUBJID",
|
|
349 |
#' baseline_var = "BTOXGR",
|
|
350 |
#' direction_var = "GRADDR"
|
|
351 |
#' )
|
|
352 |
#' ) %>%
|
|
353 |
#' append_topleft("Direction of Abnormality") %>%
|
|
354 |
#' build_table(df = df, alt_counts_df = tern_ex_adsl)
|
|
355 |
#'
|
|
356 |
#' @export
|
|
357 |
count_abnormal_lab_worsen_by_baseline <- function(lyt, # nolint |
|
358 |
var,
|
|
359 |
...,
|
|
360 |
table_names = NULL, |
|
361 |
.stats = NULL, |
|
362 |
.formats = NULL, |
|
363 |
.labels = NULL, |
|
364 |
.indent_mods = NULL) { |
|
365 | 1x |
checkmate::assert_string(var) |
366 | ||
367 | 1x |
afun <- make_afun( |
368 | 1x |
a_count_abnormal_lab_worsen_by_baseline,
|
369 | 1x |
.stats = .stats, |
370 | 1x |
.formats = .formats, |
371 | 1x |
.labels = .labels, |
372 | 1x |
.indent_mods = .indent_mods |
373 |
)
|
|
374 | ||
375 | 1x |
lyt <- analyze( |
376 | 1x |
lyt = lyt, |
377 | 1x |
vars = var, |
378 | 1x |
afun = afun, |
379 | 1x |
extra_args = list(...), |
380 | 1x |
show_labels = "hidden" |
381 |
)
|
|
382 | ||
383 | 1x |
lyt
|
384 |
}
|
1 |
#' Formatting Functions
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' See below for the list of formatting functions created in `tern` to work with `rtables`.
|
|
6 |
#'
|
|
7 |
#' Other available formats can be listed via [`formatters::list_valid_format_labels()`]. Additional
|
|
8 |
#' custom formats can be created via the [`formatters::sprintf_format()`] function.
|
|
9 |
#'
|
|
10 |
#' @family formatting functions
|
|
11 |
#' @name formatting_functions
|
|
12 |
NULL
|
|
13 | ||
14 |
#' Formatting Fraction and Percentage
|
|
15 |
#'
|
|
16 |
#' @description `r lifecycle::badge("stable")`
|
|
17 |
#'
|
|
18 |
#' Formats a fraction together with ratio in percent.
|
|
19 |
#'
|
|
20 |
#' @param x (`integer`)\cr with elements `num` and `denom`.
|
|
21 |
#' @param ... required for `rtables` interface.
|
|
22 |
#'
|
|
23 |
#' @return A string in the format `num / denom (ratio %)`. If `num` is 0, the format is `num / denom`.
|
|
24 |
#'
|
|
25 |
#' @examples
|
|
26 |
#' format_fraction(x = c(num = 2L, denom = 3L))
|
|
27 |
#' format_fraction(x = c(num = 0L, denom = 3L))
|
|
28 |
#'
|
|
29 |
#' @family formatting functions
|
|
30 |
#' @export
|
|
31 |
format_fraction <- function(x, ...) { |
|
32 | 4x |
attr(x, "label") <- NULL |
33 | ||
34 | 4x |
checkmate::assert_vector(x) |
35 | 4x |
checkmate::assert_count(x["num"]) |
36 | 2x |
checkmate::assert_count(x["denom"]) |
37 | ||
38 | 2x |
result <- if (x["num"] == 0) { |
39 | 1x |
paste0(x["num"], "/", x["denom"]) |
40 |
} else { |
|
41 | 1x |
paste0( |
42 | 1x |
x["num"], "/", x["denom"], |
43 | 1x |
" (", round(x["num"] / x["denom"] * 100, 1), "%)" |
44 |
)
|
|
45 |
}
|
|
46 | ||
47 | 2x |
return(result) |
48 |
}
|
|
49 | ||
50 |
#' Formatting Fraction and Percentage with Fixed Single Decimal Place
|
|
51 |
#'
|
|
52 |
#' @description `r lifecycle::badge("stable")`
|
|
53 |
#'
|
|
54 |
#' Formats a fraction together with ratio in percent with fixed single decimal place.
|
|
55 |
#' Includes trailing zero in case of whole number percentages to always keep one decimal place.
|
|
56 |
#'
|
|
57 |
#' @param x (`integer`)\cr with elements `num` and `denom`.
|
|
58 |
#' @param ... required for `rtables` interface.
|
|
59 |
#'
|
|
60 |
#' @return A string in the format `num / denom (ratio %)`. If `num` is 0, the format is `num / denom`.
|
|
61 |
#'
|
|
62 |
#' @examples
|
|
63 |
#' format_fraction_fixed_dp(x = c(num = 1L, denom = 2L))
|
|
64 |
#' format_fraction_fixed_dp(x = c(num = 1L, denom = 4L))
|
|
65 |
#' format_fraction_fixed_dp(x = c(num = 0L, denom = 3L))
|
|
66 |
#'
|
|
67 |
#' @family formatting functions
|
|
68 |
#' @export
|
|
69 |
format_fraction_fixed_dp <- function(x, ...) { |
|
70 | 3x |
attr(x, "label") <- NULL |
71 | 3x |
checkmate::assert_vector(x) |
72 | 3x |
checkmate::assert_count(x["num"]) |
73 | 3x |
checkmate::assert_count(x["denom"]) |
74 | ||
75 | 3x |
result <- if (x["num"] == 0) { |
76 | 1x |
paste0(x["num"], "/", x["denom"]) |
77 |
} else { |
|
78 | 2x |
paste0( |
79 | 2x |
x["num"], "/", x["denom"], |
80 | 2x |
" (", sprintf("%.1f", round(x["num"] / x["denom"] * 100, 1)), "%)" |
81 |
)
|
|
82 |
}
|
|
83 | 3x |
return(result) |
84 |
}
|
|
85 | ||
86 |
#' Formatting Count and Fraction
|
|
87 |
#'
|
|
88 |
#' @description `r lifecycle::badge("stable")`
|
|
89 |
#'
|
|
90 |
#' Formats a count together with fraction with special consideration when count is `0`.
|
|
91 |
#'
|
|
92 |
#' @param x (`integer`)\cr vector of length 2, count and fraction.
|
|
93 |
#' @param ... required for `rtables` interface.
|
|
94 |
#'
|
|
95 |
#' @return A string in the format `count (fraction %)`. If `count` is 0, the format is `0`.
|
|
96 |
#'
|
|
97 |
#' @examples
|
|
98 |
#' format_count_fraction(x = c(2, 0.6667))
|
|
99 |
#' format_count_fraction(x = c(0, 0))
|
|
100 |
#'
|
|
101 |
#' @family formatting functions
|
|
102 |
#' @export
|
|
103 |
format_count_fraction <- function(x, ...) { |
|
104 | 3x |
attr(x, "label") <- NULL |
105 | ||
106 | 3x |
if (any(is.na(x))) { |
107 | 1x |
return("NA") |
108 |
}
|
|
109 | ||
110 | 2x |
checkmate::assert_vector(x) |
111 | 2x |
checkmate::assert_integerish(x[1]) |
112 | 2x |
assert_proportion_value(x[2], include_boundaries = TRUE) |
113 | ||
114 | 2x |
result <- if (x[1] == 0) { |
115 | 1x |
"0"
|
116 |
} else { |
|
117 | 1x |
paste0(x[1], " (", round(x[2] * 100, 1), "%)") |
118 |
}
|
|
119 | ||
120 | 2x |
return(result) |
121 |
}
|
|
122 | ||
123 |
#' Formatting Count and Percentage with Fixed Single Decimal Place
|
|
124 |
#'
|
|
125 |
#' @description `r lifecycle::badge("experimental")`
|
|
126 |
#'
|
|
127 |
#' Formats a count together with fraction with special consideration when count is `0`.
|
|
128 |
#'
|
|
129 |
#' @param x (`integer`)\cr vector of length 2, count and fraction.
|
|
130 |
#' @param ... required for `rtables` interface.
|
|
131 |
#'
|
|
132 |
#' @return A string in the format `count (fraction %)`. If `count` is 0, the format is `0`.
|
|
133 |
#'
|
|
134 |
#' @examples
|
|
135 |
#' format_count_fraction_fixed_dp(x = c(2, 0.6667))
|
|
136 |
#' format_count_fraction_fixed_dp(x = c(2, 0.5))
|
|
137 |
#' format_count_fraction_fixed_dp(x = c(0, 0))
|
|
138 |
#'
|
|
139 |
#' @family formatting functions
|
|
140 |
#' @export
|
|
141 |
format_count_fraction_fixed_dp <- function(x, ...) { |
|
142 | 3x |
attr(x, "label") <- NULL |
143 | ||
144 | 3x |
if (any(is.na(x))) { |
145 | ! |
return("NA") |
146 |
}
|
|
147 | ||
148 | 3x |
checkmate::assert_vector(x) |
149 | 3x |
checkmate::assert_integerish(x[1]) |
150 | 3x |
assert_proportion_value(x[2], include_boundaries = TRUE) |
151 | ||
152 | 3x |
result <- if (x[1] == 0) { |
153 | 1x |
"0"
|
154 | 3x |
} else if (x[2] == 1) { |
155 | ! |
sprintf("%d (100%%)", x[1]) |
156 |
} else { |
|
157 | 2x |
sprintf("%d (%.1f%%)", x[1], x[2] * 100) |
158 |
}
|
|
159 | ||
160 | 3x |
return(result) |
161 |
}
|
|
162 | ||
163 |
#' Formatting: XX as Formatting Function
|
|
164 |
#'
|
|
165 |
#' Translate a string where x and dots are interpreted as number place
|
|
166 |
#' holders, and others as formatting elements.
|
|
167 |
#'
|
|
168 |
#' @param str (`string`)\cr template.
|
|
169 |
#'
|
|
170 |
#' @return An `rtables` formatting function.
|
|
171 |
#'
|
|
172 |
#' @examples
|
|
173 |
#' test <- list(c(1.658, 0.5761), c(1e1, 785.6))
|
|
174 |
#'
|
|
175 |
#' z <- format_xx("xx (xx.x)")
|
|
176 |
#' sapply(test, z)
|
|
177 |
#'
|
|
178 |
#' z <- format_xx("xx.x - xx.x")
|
|
179 |
#' sapply(test, z)
|
|
180 |
#'
|
|
181 |
#' z <- format_xx("xx.x, incl. xx.x% NE")
|
|
182 |
#' sapply(test, z)
|
|
183 |
#'
|
|
184 |
#' @family formatting functions
|
|
185 |
#' @export
|
|
186 |
format_xx <- function(str) { |
|
187 |
# Find position in the string.
|
|
188 | 1x |
positions <- gregexpr(pattern = "x+\\.x+|x+", text = str, perl = TRUE) |
189 | 1x |
x_positions <- regmatches(x = str, m = positions)[[1]] |
190 | ||
191 |
# Roundings depends on the number of x behind [.].
|
|
192 | 1x |
roundings <- lapply( |
193 | 1x |
X = x_positions, |
194 | 1x |
function(x) { |
195 | 2x |
y <- strsplit(split = "\\.", x = x)[[1]] |
196 | 2x |
rounding <- function(x) { |
197 | 4x |
round(x, digits = ifelse(length(y) > 1, nchar(y[2]), 0)) |
198 |
}
|
|
199 | 2x |
return(rounding) |
200 |
}
|
|
201 |
)
|
|
202 | ||
203 | 1x |
rtable_format <- function(x, output) { |
204 | 2x |
values <- Map(y = x, fun = roundings, function(y, fun) fun(y)) |
205 | 2x |
regmatches(x = str, m = positions)[[1]] <- values |
206 | 2x |
return(str) |
207 |
}
|
|
208 | ||
209 | 1x |
return(rtable_format) |
210 |
}
|
|
211 | ||
212 |
#' Formatting Fraction with Lower Threshold
|
|
213 |
#'
|
|
214 |
#' @description `r lifecycle::badge("stable")`
|
|
215 |
#'
|
|
216 |
#' Formats a fraction when the second element of the input `x` is the fraction. It applies
|
|
217 |
#' a lower threshold, below which it is just stated that the fraction is smaller than that.
|
|
218 |
#'
|
|
219 |
#' @param threshold (`proportion`)\cr lower threshold.
|
|
220 |
#'
|
|
221 |
#' @return An `rtables` formatting function that takes numeric input `x` where the second
|
|
222 |
#' element is the fraction that is formatted. If the fraction is above or equal to the threshold,
|
|
223 |
#' then it is displayed in percentage. If it is positive but below the threshold, it returns,
|
|
224 |
#' e.g. "<1" if the threshold is `0.01`. If it is zero, then just "0" is returned.
|
|
225 |
#'
|
|
226 |
#' @examples
|
|
227 |
#' format_fun <- format_fraction_threshold(0.05)
|
|
228 |
#' format_fun(x = c(20, 0.1))
|
|
229 |
#' format_fun(x = c(2, 0.01))
|
|
230 |
#' format_fun(x = c(0, 0))
|
|
231 |
#'
|
|
232 |
#' @family formatting functions
|
|
233 |
#' @export
|
|
234 |
format_fraction_threshold <- function(threshold) { |
|
235 | 1x |
assert_proportion_value(threshold) |
236 | 1x |
string_below_threshold <- paste0("<", round(threshold * 100)) |
237 | 1x |
function(x, ...) { |
238 | 3x |
assert_proportion_value(x[2], include_boundaries = TRUE) |
239 | 3x |
ifelse( |
240 | 3x |
x[2] > 0.01, |
241 | 3x |
round(x[2] * 100), |
242 | 3x |
ifelse( |
243 | 3x |
x[2] == 0, |
244 | 3x |
"0",
|
245 | 3x |
string_below_threshold
|
246 |
)
|
|
247 |
)
|
|
248 |
}
|
|
249 |
}
|
|
250 | ||
251 |
#' Formatting Extreme Values
|
|
252 |
#'
|
|
253 |
#' @description `r lifecycle::badge("stable")`
|
|
254 |
#'
|
|
255 |
#' `rtables` formatting functions that handle extreme values.
|
|
256 |
#'
|
|
257 |
#' @param digits (`integer`)\cr number of decimal places to display.
|
|
258 |
#'
|
|
259 |
#' @details For each input, apply a format to the specified number of `digits`. If the value is
|
|
260 |
#' below a threshold, it returns "<0.01" e.g. if the number of `digits` is 2. If the value is
|
|
261 |
#' above a threshold, it returns ">999.99" e.g. if the number of `digits` is 2.
|
|
262 |
#' If it is zero, then returns "0.00".
|
|
263 |
#'
|
|
264 |
#' @family formatting functions
|
|
265 |
#' @name extreme_format
|
|
266 |
NULL
|
|
267 | ||
268 |
#' @describeIn extreme_format Internal helper function to calculate the threshold and create formatted strings
|
|
269 |
#' used in Formatting Functions. Returns a list with elements `threshold` and `format_string`.
|
|
270 |
#'
|
|
271 |
#' @return
|
|
272 |
#' * `h_get_format_threshold()` returns a `list` of 2 elements: `threshold`, with `low` and `high` thresholds,
|
|
273 |
#' and `format_string`, with thresholds formatted as strings.
|
|
274 |
#'
|
|
275 |
#' @examples
|
|
276 |
#' h_get_format_threshold(2L)
|
|
277 |
#'
|
|
278 |
#' @export
|
|
279 |
h_get_format_threshold <- function(digits = 2L) { |
|
280 | 1022x |
checkmate::assert_integerish(digits) |
281 | ||
282 | 1022x |
low_threshold <- 1 / (10 ^ digits) # styler: off |
283 | 1022x |
high_threshold <- 1000 - (1 / (10 ^ digits)) # styler: off |
284 | ||
285 | 1022x |
string_below_threshold <- paste0("<", low_threshold) |
286 | 1022x |
string_above_threshold <- paste0(">", high_threshold) |
287 | ||
288 | 1022x |
list( |
289 | 1022x |
"threshold" = c(low = low_threshold, high = high_threshold), |
290 | 1022x |
"format_string" = c(low = string_below_threshold, high = string_above_threshold) |
291 |
)
|
|
292 |
}
|
|
293 | ||
294 |
#' @describeIn extreme_format Internal helper function to apply a threshold format to a value.
|
|
295 |
#' Creates a formatted string to be used in Formatting Functions.
|
|
296 |
#'
|
|
297 |
#' @param x (`number`)\cr value to format.
|
|
298 |
#'
|
|
299 |
#' @return
|
|
300 |
#' * `h_format_threshold()` returns the given value, or if the value is not within the digit threshold the relation
|
|
301 |
#' of the given value to the digit threshold, as a formatted string.
|
|
302 |
#'
|
|
303 |
#' @examples
|
|
304 |
#' h_format_threshold(0.001)
|
|
305 |
#' h_format_threshold(1000)
|
|
306 |
#'
|
|
307 |
#' @export
|
|
308 |
h_format_threshold <- function(x, digits = 2L) { |
|
309 | 1025x |
if (is.na(x)) { |
310 | 4x |
return(x) |
311 |
}
|
|
312 | ||
313 | 1021x |
checkmate::assert_numeric(x, lower = 0) |
314 | ||
315 | 1021x |
l_fmt <- h_get_format_threshold(digits) |
316 | ||
317 | 1021x |
result <- if (x < l_fmt$threshold["low"] && 0 < x) { |
318 | 25x |
l_fmt$format_string["low"] |
319 | 1021x |
} else if (x > l_fmt$threshold["high"]) { |
320 | 72x |
l_fmt$format_string["high"] |
321 |
} else { |
|
322 | 924x |
sprintf(fmt = paste0("%.", digits, "f"), x) |
323 |
}
|
|
324 | ||
325 | 1021x |
unname(result) |
326 |
}
|
|
327 | ||
328 |
#' Formatting a Single Extreme Value
|
|
329 |
#'
|
|
330 |
#' @description `r lifecycle::badge("stable")`
|
|
331 |
#'
|
|
332 |
#' Create Formatting Function for a single extreme value.
|
|
333 |
#'
|
|
334 |
#' @inheritParams extreme_format
|
|
335 |
#'
|
|
336 |
#' @return An `rtables` formatting function that uses threshold `digits` to return a formatted extreme value.
|
|
337 |
#'
|
|
338 |
#' @examples
|
|
339 |
#' format_fun <- format_extreme_values(2L)
|
|
340 |
#' format_fun(x = 0.127)
|
|
341 |
#' format_fun(x = Inf)
|
|
342 |
#' format_fun(x = 0)
|
|
343 |
#' format_fun(x = 0.009)
|
|
344 |
#'
|
|
345 |
#' @family formatting functions
|
|
346 |
#' @export
|
|
347 |
format_extreme_values <- function(digits = 2L) { |
|
348 | 24x |
function(x, ...) { |
349 | 307x |
checkmate::assert_scalar(x, na.ok = TRUE) |
350 | ||
351 | 307x |
h_format_threshold(x = x, digits = digits) |
352 |
}
|
|
353 |
}
|
|
354 | ||
355 |
#' Formatting Extreme Values Part of a Confidence Interval
|
|
356 |
#'
|
|
357 |
#' @description `r lifecycle::badge("stable")`
|
|
358 |
#'
|
|
359 |
#' Formatting Function for extreme values part of a confidence interval. Values
|
|
360 |
#' are formatted as e.g. "(xx.xx, xx.xx)" if the number of `digits` is 2.
|
|
361 |
#'
|
|
362 |
#' @inheritParams extreme_format
|
|
363 |
#'
|
|
364 |
#' @return An `rtables` formatting function that uses threshold `digits` to return a formatted extreme
|
|
365 |
#' values confidence interval.
|
|
366 |
#'
|
|
367 |
#' @examples
|
|
368 |
#' format_fun <- format_extreme_values_ci(2L)
|
|
369 |
#' format_fun(x = c(0.127, Inf))
|
|
370 |
#' format_fun(x = c(0, 0.009))
|
|
371 |
#'
|
|
372 |
#' @family formatting functions
|
|
373 |
#' @export
|
|
374 |
format_extreme_values_ci <- function(digits = 2L) { |
|
375 | 32x |
function(x, ...) { |
376 | 356x |
checkmate::assert_vector(x, len = 2) |
377 | 356x |
l_result <- h_format_threshold(x = x[1], digits = digits) |
378 | 356x |
h_result <- h_format_threshold(x = x[2], digits = digits) |
379 | ||
380 | 356x |
paste0("(", l_result, ", ", h_result, ")") |
381 |
}
|
|
382 |
}
|
1 |
#' Missing Data
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Substitute missing data with a string or factor level.
|
|
6 |
#'
|
|
7 |
#' @param x (`factor` or `character` vector)\cr values for which any missing values should be substituted.
|
|
8 |
#' @param label (`character`)\cr string that missing data should be replaced with.
|
|
9 |
#'
|
|
10 |
#' @return `x` with any `NA` values substituted by `label`.
|
|
11 |
#'
|
|
12 |
#' @examples
|
|
13 |
#' explicit_na(c(NA, "a", "b"))
|
|
14 |
#' is.na(explicit_na(c(NA, "a", "b")))
|
|
15 |
#'
|
|
16 |
#' explicit_na(factor(c(NA, "a", "b")))
|
|
17 |
#' is.na(explicit_na(factor(c(NA, "a", "b"))))
|
|
18 |
#'
|
|
19 |
#' explicit_na(sas_na(c("a", "")))
|
|
20 |
#'
|
|
21 |
#' @export
|
|
22 |
explicit_na <- function(x, label = "<Missing>") { |
|
23 | 409x |
checkmate::assert_string(label) |
24 | ||
25 | 409x |
if (is.factor(x)) { |
26 | 307x |
x <- forcats::fct_na_value_to_level(x, label) |
27 | 307x |
forcats::fct_drop(x, only = label) |
28 | 102x |
} else if (is.character(x)) { |
29 | 102x |
x[is.na(x)] <- label |
30 | 102x |
x
|
31 |
} else { |
|
32 | ! |
stop("only factors and character vectors allowed") |
33 |
}
|
|
34 |
}
|
|
35 | ||
36 |
#' Convert Strings to `NA`
|
|
37 |
#'
|
|
38 |
#' @description `r lifecycle::badge("stable")`
|
|
39 |
#'
|
|
40 |
#' SAS imports missing data as empty strings or strings with whitespaces only. This helper function can be used to
|
|
41 |
#' convert these values to `NA`s.
|
|
42 |
#'
|
|
43 |
#' @inheritParams explicit_na
|
|
44 |
#' @param empty (`logical`)\cr if `TRUE` empty strings get replaced by `NA`.
|
|
45 |
#' @param whitespaces (`logical`)\cr if `TRUE` then strings made from whitespaces only get replaced with `NA`.
|
|
46 |
#'
|
|
47 |
#' @return `x` with `""` and/or whitespace-only values substituted by `NA`, depending on the values of
|
|
48 |
#' `empty` and `whitespaces`.
|
|
49 |
#'
|
|
50 |
#' @examples
|
|
51 |
#' sas_na(c("1", "", " ", " ", "b"))
|
|
52 |
#' sas_na(factor(c("", " ", "b")))
|
|
53 |
#'
|
|
54 |
#' is.na(sas_na(c("1", "", " ", " ", "b")))
|
|
55 |
#'
|
|
56 |
#' @export
|
|
57 |
sas_na <- function(x, empty = TRUE, whitespaces = TRUE) { |
|
58 | 406x |
checkmate::assert_flag(empty) |
59 | 406x |
checkmate::assert_flag(whitespaces) |
60 | ||
61 | 406x |
if (is.factor(x)) { |
62 | 300x |
empty_levels <- levels(x) == "" |
63 | 11x |
if (empty && any(empty_levels)) levels(x)[empty_levels] <- NA |
64 | ||
65 | 300x |
ws_levels <- grepl("^\\s+$", levels(x)) |
66 | ! |
if (whitespaces && any(ws_levels)) levels(x)[ws_levels] <- NA |
67 | ||
68 | 300x |
x
|
69 | 106x |
} else if (is.character(x)) { |
70 | 106x |
if (empty) x[x == ""] <- NA_character_ |
71 | ||
72 | 106x |
if (whitespaces) x[grepl("^\\s+$", x)] <- NA_character_ |
73 | ||
74 | 106x |
x
|
75 |
} else { |
|
76 | ! |
stop("only factors and character vectors allowed") |
77 |
}
|
|
78 |
}
|
1 |
#' Helper Functions for Tabulating Binary Response by Subgroup
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Helper functions that tabulate in a data frame statistics such as response rate
|
|
6 |
#' and odds ratio for population subgroups.
|
|
7 |
#'
|
|
8 |
#' @inheritParams argument_convention
|
|
9 |
#' @inheritParams response_subgroups
|
|
10 |
#' @param arm (`factor`)\cr the treatment group variable.
|
|
11 |
#'
|
|
12 |
#' @details Main functionality is to prepare data for use in a layout-creating function.
|
|
13 |
#'
|
|
14 |
#' @examples
|
|
15 |
#' library(dplyr)
|
|
16 |
#' library(forcats)
|
|
17 |
#'
|
|
18 |
#' adrs <- tern_ex_adrs
|
|
19 |
#' adrs_labels <- formatters::var_labels(adrs)
|
|
20 |
#'
|
|
21 |
#' adrs_f <- adrs %>%
|
|
22 |
#' filter(PARAMCD == "BESRSPI") %>%
|
|
23 |
#' filter(ARM %in% c("A: Drug X", "B: Placebo")) %>%
|
|
24 |
#' droplevels() %>%
|
|
25 |
#' mutate(
|
|
26 |
#' # Reorder levels of factor to make the placebo group the reference arm.
|
|
27 |
#' ARM = fct_relevel(ARM, "B: Placebo"),
|
|
28 |
#' rsp = AVALC == "CR"
|
|
29 |
#' )
|
|
30 |
#' formatters::var_labels(adrs_f) <- c(adrs_labels, "Response")
|
|
31 |
#'
|
|
32 |
#' @name h_response_subgroups
|
|
33 |
NULL
|
|
34 | ||
35 |
#' @describeIn h_response_subgroups helper to prepare a data frame of binary responses by arm.
|
|
36 |
#'
|
|
37 |
#' @return
|
|
38 |
#' * `h_proportion_df()` returns a `data.frame` with columns `arm`, `n`, `n_rsp`, and `prop`.
|
|
39 |
#'
|
|
40 |
#' @examples
|
|
41 |
#' h_proportion_df(
|
|
42 |
#' c(TRUE, FALSE, FALSE),
|
|
43 |
#' arm = factor(c("A", "A", "B"), levels = c("A", "B"))
|
|
44 |
#' )
|
|
45 |
#'
|
|
46 |
#' @export
|
|
47 |
h_proportion_df <- function(rsp, arm) { |
|
48 | 59x |
checkmate::assert_logical(rsp) |
49 | 58x |
assert_valid_factor(arm, len = length(rsp)) |
50 | 58x |
non_missing_rsp <- !is.na(rsp) |
51 | 58x |
rsp <- rsp[non_missing_rsp] |
52 | 58x |
arm <- arm[non_missing_rsp] |
53 | ||
54 | 58x |
lst_rsp <- split(rsp, arm) |
55 | 58x |
lst_results <- Map(function(x, arm) { |
56 | 116x |
if (length(x) > 0) { |
57 | 114x |
s_prop <- s_proportion(df = x) |
58 | 114x |
data.frame( |
59 | 114x |
arm = arm, |
60 | 114x |
n = length(x), |
61 | 114x |
n_rsp = unname(s_prop$n_prop[1]), |
62 | 114x |
prop = unname(s_prop$n_prop[2]), |
63 | 114x |
stringsAsFactors = FALSE |
64 |
)
|
|
65 |
} else { |
|
66 | 2x |
data.frame( |
67 | 2x |
arm = arm, |
68 | 2x |
n = 0L, |
69 | 2x |
n_rsp = NA, |
70 | 2x |
prop = NA, |
71 | 2x |
stringsAsFactors = FALSE |
72 |
)
|
|
73 |
}
|
|
74 | 58x |
}, lst_rsp, names(lst_rsp)) |
75 | ||
76 | 58x |
df <- do.call(rbind, args = c(lst_results, make.row.names = FALSE)) |
77 | 58x |
df$arm <- factor(df$arm, levels = levels(arm)) |
78 | 58x |
df
|
79 |
}
|
|
80 | ||
81 |
#' @describeIn h_response_subgroups summarizes proportion of binary responses by arm and across subgroups
|
|
82 |
#' in a data frame. `variables` corresponds to the names of variables found in `data`, passed as a named list and
|
|
83 |
#' requires elements `rsp`, `arm` and optionally `subgroups`. `groups_lists` optionally specifies
|
|
84 |
#' groupings for `subgroups` variables.
|
|
85 |
#'
|
|
86 |
#' @return
|
|
87 |
#' * `h_proportion_subgroups_df()` returns a `data.frame` with columns `arm`, `n`, `n_rsp`, `prop`, `subgroup`,
|
|
88 |
#' `var`, `var_label`, and `row_type`.
|
|
89 |
#'
|
|
90 |
#' @examples
|
|
91 |
#' h_proportion_subgroups_df(
|
|
92 |
#' variables = list(rsp = "rsp", arm = "ARM", subgroups = c("SEX", "BMRKR2")),
|
|
93 |
#' data = adrs_f
|
|
94 |
#' )
|
|
95 |
#'
|
|
96 |
#' # Define groupings for BMRKR2 levels.
|
|
97 |
#' h_proportion_subgroups_df(
|
|
98 |
#' variables = list(rsp = "rsp", arm = "ARM", subgroups = c("SEX", "BMRKR2")),
|
|
99 |
#' data = adrs_f,
|
|
100 |
#' groups_lists = list(
|
|
101 |
#' BMRKR2 = list(
|
|
102 |
#' "low" = "LOW",
|
|
103 |
#' "low/medium" = c("LOW", "MEDIUM"),
|
|
104 |
#' "low/medium/high" = c("LOW", "MEDIUM", "HIGH")
|
|
105 |
#' )
|
|
106 |
#' )
|
|
107 |
#' )
|
|
108 |
#'
|
|
109 |
#' @export
|
|
110 |
h_proportion_subgroups_df <- function(variables, |
|
111 |
data,
|
|
112 |
groups_lists = list(), |
|
113 |
label_all = "All Patients") { |
|
114 | 13x |
checkmate::assert_character(variables$rsp) |
115 | 13x |
checkmate::assert_character(variables$arm) |
116 | 13x |
checkmate::assert_character(variables$subgroups, null.ok = TRUE) |
117 | 13x |
assert_df_with_factors(data, list(val = variables$arm), min.levels = 2, max.levels = 2) |
118 | 13x |
assert_df_with_variables(data, variables) |
119 | 13x |
checkmate::assert_string(label_all) |
120 | ||
121 |
# Add All Patients.
|
|
122 | 13x |
result_all <- h_proportion_df(data[[variables$rsp]], data[[variables$arm]]) |
123 | 13x |
result_all$subgroup <- label_all |
124 | 13x |
result_all$var <- "ALL" |
125 | 13x |
result_all$var_label <- label_all |
126 | 13x |
result_all$row_type <- "content" |
127 | ||
128 |
# Add Subgroups.
|
|
129 | 13x |
if (is.null(variables$subgroups)) { |
130 | 3x |
result_all
|
131 |
} else { |
|
132 | 10x |
l_data <- h_split_by_subgroups(data, variables$subgroups, groups_lists = groups_lists) |
133 | ||
134 | 10x |
l_result <- lapply(l_data, function(grp) { |
135 | 42x |
result <- h_proportion_df(grp$df[[variables$rsp]], grp$df[[variables$arm]]) |
136 | 42x |
result_labels <- grp$df_labels[rep(1, times = nrow(result)), ] |
137 | 42x |
cbind(result, result_labels) |
138 |
}) |
|
139 | 10x |
result_subgroups <- do.call(rbind, args = c(l_result, make.row.names = FALSE)) |
140 | 10x |
result_subgroups$row_type <- "analysis" |
141 | ||
142 | 10x |
rbind( |
143 | 10x |
result_all,
|
144 | 10x |
result_subgroups
|
145 |
)
|
|
146 |
}
|
|
147 |
}
|
|
148 | ||
149 |
#' @describeIn h_response_subgroups helper to prepare a data frame with estimates of
|
|
150 |
#' the odds ratio between a treatment and a control arm.
|
|
151 |
#'
|
|
152 |
#' @inheritParams response_subgroups
|
|
153 |
#' @param strata_data (`factor`, `data.frame` or `NULL`)\cr required if stratified analysis is performed.
|
|
154 |
#'
|
|
155 |
#' @return
|
|
156 |
#' * `h_odds_ratio_df()` returns a `data.frame` with columns `arm`, `n_tot`, `or`, `lcl`, `ucl`, `conf_level`, and
|
|
157 |
#' optionally `pval` and `pval_label`.
|
|
158 |
#'
|
|
159 |
#' @examples
|
|
160 |
#' # Unstratatified analysis.
|
|
161 |
#' h_odds_ratio_df(
|
|
162 |
#' c(TRUE, FALSE, FALSE, TRUE),
|
|
163 |
#' arm = factor(c("A", "A", "B", "B"), levels = c("A", "B"))
|
|
164 |
#' )
|
|
165 |
#'
|
|
166 |
#' # Include p-value.
|
|
167 |
#' h_odds_ratio_df(adrs_f$rsp, adrs_f$ARM, method = "chisq")
|
|
168 |
#'
|
|
169 |
#' # Stratatified analysis.
|
|
170 |
#' h_odds_ratio_df(
|
|
171 |
#' rsp = adrs_f$rsp,
|
|
172 |
#' arm = adrs_f$ARM,
|
|
173 |
#' strata_data = adrs_f[, c("STRATA1", "STRATA2")],
|
|
174 |
#' method = "cmh"
|
|
175 |
#' )
|
|
176 |
#'
|
|
177 |
#' @export
|
|
178 |
h_odds_ratio_df <- function(rsp, arm, strata_data = NULL, conf_level = 0.95, method = NULL) { |
|
179 | 64x |
assert_valid_factor(arm, n.levels = 2, len = length(rsp)) |
180 | ||
181 | 64x |
df_rsp <- data.frame( |
182 | 64x |
rsp = rsp, |
183 | 64x |
arm = arm |
184 |
)
|
|
185 | ||
186 | 64x |
if (!is.null(strata_data)) { |
187 | 11x |
strata_var <- interaction(strata_data, drop = TRUE) |
188 | 11x |
strata_name <- "strata" |
189 | ||
190 | 11x |
assert_valid_factor(strata_var, len = nrow(df_rsp)) |
191 | ||
192 | 11x |
df_rsp[[strata_name]] <- strata_var |
193 |
} else { |
|
194 | 53x |
strata_name <- NULL |
195 |
}
|
|
196 | ||
197 | 64x |
l_df <- split(df_rsp, arm) |
198 | ||
199 | 64x |
if (nrow(l_df[[1]]) > 0 && nrow(l_df[[2]]) > 0) { |
200 |
# Odds ratio and CI.
|
|
201 | 62x |
result_odds_ratio <- s_odds_ratio( |
202 | 62x |
df = l_df[[2]], |
203 | 62x |
.var = "rsp", |
204 | 62x |
.ref_group = l_df[[1]], |
205 | 62x |
.in_ref_col = FALSE, |
206 | 62x |
.df_row = df_rsp, |
207 | 62x |
variables = list(arm = "arm", strata = strata_name), |
208 | 62x |
conf_level = conf_level |
209 |
)
|
|
210 | ||
211 | 62x |
df <- data.frame( |
212 |
# Dummy column needed downstream to create a nested header.
|
|
213 | 62x |
arm = " ", |
214 | 62x |
n_tot = unname(result_odds_ratio$n_tot["n_tot"]), |
215 | 62x |
or = unname(result_odds_ratio$or_ci["est"]), |
216 | 62x |
lcl = unname(result_odds_ratio$or_ci["lcl"]), |
217 | 62x |
ucl = unname(result_odds_ratio$or_ci["ucl"]), |
218 | 62x |
conf_level = conf_level, |
219 | 62x |
stringsAsFactors = FALSE |
220 |
)
|
|
221 | ||
222 | 62x |
if (!is.null(method)) { |
223 |
# Test for difference.
|
|
224 | 29x |
result_test <- s_test_proportion_diff( |
225 | 29x |
df = l_df[[2]], |
226 | 29x |
.var = "rsp", |
227 | 29x |
.ref_group = l_df[[1]], |
228 | 29x |
.in_ref_col = FALSE, |
229 | 29x |
variables = list(strata = strata_name), |
230 | 29x |
method = method |
231 |
)
|
|
232 | ||
233 | 29x |
df$pval <- as.numeric(result_test$pval) |
234 | 29x |
df$pval_label <- obj_label(result_test$pval) |
235 |
}
|
|
236 | ||
237 |
# In those cases cannot go through the model so will obtain n_tot from data.
|
|
238 |
} else if ( |
|
239 | 2x |
(nrow(l_df[[1]]) == 0 && nrow(l_df[[2]]) > 0) || |
240 | 2x |
(nrow(l_df[[1]]) > 0 && nrow(l_df[[2]]) == 0) |
241 |
) { |
|
242 | 2x |
df <- data.frame( |
243 |
# Dummy column needed downstream to create a nested header.
|
|
244 | 2x |
arm = " ", |
245 | 2x |
n_tot = sum(stats::complete.cases(df_rsp)), |
246 | 2x |
or = NA, |
247 | 2x |
lcl = NA, |
248 | 2x |
ucl = NA, |
249 | 2x |
conf_level = conf_level, |
250 | 2x |
stringsAsFactors = FALSE |
251 |
)
|
|
252 | 2x |
if (!is.null(method)) { |
253 | 2x |
df$pval <- NA |
254 | 2x |
df$pval_label <- NA |
255 |
}
|
|
256 |
} else { |
|
257 | ! |
df <- data.frame( |
258 |
# Dummy column needed downstream to create a nested header.
|
|
259 | ! |
arm = " ", |
260 | ! |
n_tot = 0L, |
261 | ! |
or = NA, |
262 | ! |
lcl = NA, |
263 | ! |
ucl = NA, |
264 | ! |
conf_level = conf_level, |
265 | ! |
stringsAsFactors = FALSE |
266 |
)
|
|
267 | ||
268 | ! |
if (!is.null(method)) { |
269 | ! |
df$pval <- NA |
270 | ! |
df$pval_label <- NA |
271 |
}
|
|
272 |
}
|
|
273 | ||
274 | 64x |
df
|
275 |
}
|
|
276 | ||
277 |
#' @describeIn h_response_subgroups summarizes estimates of the odds ratio between a treatment and a control
|
|
278 |
#' arm across subgroups in a data frame. `variables` corresponds to the names of variables found in
|
|
279 |
#' `data`, passed as a named list and requires elements `rsp`, `arm` and optionally `subgroups`
|
|
280 |
#' and `strat`. `groups_lists` optionally specifies groupings for `subgroups` variables.
|
|
281 |
#'
|
|
282 |
#' @return
|
|
283 |
#' * `h_odds_ratio_subgroups_df()` returns a `data.frame` with columns `arm`, `n_tot`, `or`, `lcl`, `ucl`,
|
|
284 |
#' `conf_level`, `subgroup`, `var`, `var_label`, and `row_type`.
|
|
285 |
#'
|
|
286 |
#' @examples
|
|
287 |
#' # Unstratified analysis.
|
|
288 |
#' h_odds_ratio_subgroups_df(
|
|
289 |
#' variables = list(rsp = "rsp", arm = "ARM", subgroups = c("SEX", "BMRKR2")),
|
|
290 |
#' data = adrs_f
|
|
291 |
#' )
|
|
292 |
#'
|
|
293 |
#' # Stratified analysis.
|
|
294 |
#' h_odds_ratio_subgroups_df(
|
|
295 |
#' variables = list(
|
|
296 |
#' rsp = "rsp",
|
|
297 |
#' arm = "ARM",
|
|
298 |
#' subgroups = c("SEX", "BMRKR2"),
|
|
299 |
#' strat = c("STRATA1", "STRATA2")
|
|
300 |
#' ),
|
|
301 |
#' data = adrs_f
|
|
302 |
#' )
|
|
303 |
#'
|
|
304 |
#' # Define groupings of BMRKR2 levels.
|
|
305 |
#' h_odds_ratio_subgroups_df(
|
|
306 |
#' variables = list(
|
|
307 |
#' rsp = "rsp",
|
|
308 |
#' arm = "ARM",
|
|
309 |
#' subgroups = c("SEX", "BMRKR2")
|
|
310 |
#' ),
|
|
311 |
#' data = adrs_f,
|
|
312 |
#' groups_lists = list(
|
|
313 |
#' BMRKR2 = list(
|
|
314 |
#' "low" = "LOW",
|
|
315 |
#' "low/medium" = c("LOW", "MEDIUM"),
|
|
316 |
#' "low/medium/high" = c("LOW", "MEDIUM", "HIGH")
|
|
317 |
#' )
|
|
318 |
#' )
|
|
319 |
#' )
|
|
320 |
#'
|
|
321 |
#' @export
|
|
322 |
h_odds_ratio_subgroups_df <- function(variables, |
|
323 |
data,
|
|
324 |
groups_lists = list(), |
|
325 |
conf_level = 0.95, |
|
326 |
method = NULL, |
|
327 |
label_all = "All Patients") { |
|
328 | 14x |
checkmate::assert_character(variables$rsp) |
329 | 14x |
checkmate::assert_character(variables$arm) |
330 | 14x |
checkmate::assert_character(variables$subgroups, null.ok = TRUE) |
331 | 14x |
checkmate::assert_character(variables$strat, null.ok = TRUE) |
332 | 14x |
assert_df_with_factors(data, list(val = variables$arm), min.levels = 2, max.levels = 2) |
333 | 14x |
assert_df_with_variables(data, variables) |
334 | 14x |
checkmate::assert_string(label_all) |
335 | ||
336 | 14x |
strata_data <- if (is.null(variables$strat)) { |
337 | 12x |
NULL
|
338 |
} else { |
|
339 | 2x |
data[, variables$strat, drop = FALSE] |
340 |
}
|
|
341 | ||
342 |
# Add All Patients.
|
|
343 | 14x |
result_all <- h_odds_ratio_df( |
344 | 14x |
rsp = data[[variables$rsp]], |
345 | 14x |
arm = data[[variables$arm]], |
346 | 14x |
strata_data = strata_data, |
347 | 14x |
conf_level = conf_level, |
348 | 14x |
method = method |
349 |
)
|
|
350 | 14x |
result_all$subgroup <- label_all |
351 | 14x |
result_all$var <- "ALL" |
352 | 14x |
result_all$var_label <- label_all |
353 | 14x |
result_all$row_type <- "content" |
354 | ||
355 | 14x |
if (is.null(variables$subgroups)) { |
356 | 3x |
result_all
|
357 |
} else { |
|
358 | 11x |
l_data <- h_split_by_subgroups(data, variables$subgroups, groups_lists = groups_lists) |
359 | ||
360 | 11x |
l_result <- lapply(l_data, function(grp) { |
361 | 46x |
grp_strata_data <- if (is.null(variables$strat)) { |
362 | 38x |
NULL
|
363 |
} else { |
|
364 | 8x |
grp$df[, variables$strat, drop = FALSE] |
365 |
}
|
|
366 | ||
367 | 46x |
result <- h_odds_ratio_df( |
368 | 46x |
rsp = grp$df[[variables$rsp]], |
369 | 46x |
arm = grp$df[[variables$arm]], |
370 | 46x |
strata_data = grp_strata_data, |
371 | 46x |
conf_level = conf_level, |
372 | 46x |
method = method |
373 |
)
|
|
374 | 46x |
result_labels <- grp$df_labels[rep(1, times = nrow(result)), ] |
375 | 46x |
cbind(result, result_labels) |
376 |
}) |
|
377 | ||
378 | 11x |
result_subgroups <- do.call(rbind, args = c(l_result, make.row.names = FALSE)) |
379 | 11x |
result_subgroups$row_type <- "analysis" |
380 | ||
381 | 11x |
rbind( |
382 | 11x |
result_all,
|
383 | 11x |
result_subgroups
|
384 |
)
|
|
385 |
}
|
|
386 |
}
|
1 |
#' Difference Test for Two Proportions
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Various tests were implemented to test the difference between two proportions.
|
|
6 |
#'
|
|
7 |
#' @inheritParams argument_convention
|
|
8 |
#' @param tbl (`matrix`)\cr matrix with two groups in rows and the binary response (`TRUE`/`FALSE`) in columns.
|
|
9 |
#'
|
|
10 |
#' @seealso [h_prop_diff_test]
|
|
11 |
#'
|
|
12 |
#' @name prop_diff_test
|
|
13 |
NULL
|
|
14 | ||
15 |
#' @describeIn prop_diff_test Statistics function which tests the difference between two proportions.
|
|
16 |
#'
|
|
17 |
#' @param method (`string`)\cr one of `chisq`, `cmh`, `fisher`, or `schouten`; specifies the test used
|
|
18 |
#' to calculate the p-value.
|
|
19 |
#'
|
|
20 |
#' @return
|
|
21 |
#' * `s_test_proportion_diff()` returns a named `list` with a single item `pval` with an attribute `label`
|
|
22 |
#' describing the method used. The p-value tests the null hypothesis that proportions in two groups are the same.
|
|
23 |
#'
|
|
24 |
#'
|
|
25 |
#' @keywords internal
|
|
26 |
s_test_proportion_diff <- function(df, |
|
27 |
.var,
|
|
28 |
.ref_group,
|
|
29 |
.in_ref_col,
|
|
30 |
variables = list(strata = NULL), |
|
31 |
method = c("chisq", "schouten", "fisher", "cmh")) { |
|
32 | 30x |
method <- match.arg(method) |
33 | 30x |
y <- list(pval = "") |
34 | ||
35 | 30x |
if (!.in_ref_col) { |
36 | 30x |
assert_df_with_variables(df, list(rsp = .var)) |
37 | 30x |
assert_df_with_variables(.ref_group, list(rsp = .var)) |
38 | 30x |
rsp <- factor( |
39 | 30x |
c(.ref_group[[.var]], df[[.var]]), |
40 | 30x |
levels = c("TRUE", "FALSE") |
41 |
)
|
|
42 | 30x |
grp <- factor( |
43 | 30x |
rep(c("ref", "Not-ref"), c(nrow(.ref_group), nrow(df))), |
44 | 30x |
levels = c("ref", "Not-ref") |
45 |
)
|
|
46 | ||
47 | 30x |
if (!is.null(variables$strata) || method == "cmh") { |
48 | 12x |
strata <- variables$strata |
49 | 12x |
checkmate::assert_false(is.null(strata)) |
50 | 12x |
strata_vars <- stats::setNames(as.list(strata), strata) |
51 | 12x |
assert_df_with_variables(df, strata_vars) |
52 | 12x |
assert_df_with_variables(.ref_group, strata_vars) |
53 | 12x |
strata <- c(interaction(.ref_group[strata]), interaction(df[strata])) |
54 |
}
|
|
55 | ||
56 | 30x |
tbl <- switch(method, |
57 | 30x |
cmh = table(grp, rsp, strata), |
58 | 30x |
table(grp, rsp) |
59 |
)
|
|
60 | ||
61 | 30x |
y$pval <- switch(method, |
62 | 30x |
chisq = prop_chisq(tbl), |
63 | 30x |
cmh = prop_cmh(tbl), |
64 | 30x |
fisher = prop_fisher(tbl), |
65 | 30x |
schouten = prop_schouten(tbl) |
66 |
)
|
|
67 |
}
|
|
68 | ||
69 | 30x |
y$pval <- formatters::with_label(y$pval, d_test_proportion_diff(method)) |
70 | 30x |
y
|
71 |
}
|
|
72 | ||
73 |
#' Description of the Difference Test Between Two Proportions
|
|
74 |
#'
|
|
75 |
#' @description `r lifecycle::badge("stable")`
|
|
76 |
#'
|
|
77 |
#' This is an auxiliary function that describes the analysis in `s_test_proportion_diff`.
|
|
78 |
#'
|
|
79 |
#' @inheritParams s_test_proportion_diff
|
|
80 |
#'
|
|
81 |
#' @return `string` describing the test from which the p-value is derived.
|
|
82 |
#'
|
|
83 |
#' @export
|
|
84 |
d_test_proportion_diff <- function(method) { |
|
85 | 41x |
checkmate::assert_string(method) |
86 | 41x |
meth_part <- switch(method, |
87 | 41x |
"schouten" = "Chi-Squared Test with Schouten Correction", |
88 | 41x |
"chisq" = "Chi-Squared Test", |
89 | 41x |
"cmh" = "Cochran-Mantel-Haenszel Test", |
90 | 41x |
"fisher" = "Fisher's Exact Test", |
91 | 41x |
stop(paste(method, "does not have a description")) |
92 |
)
|
|
93 | 41x |
paste0("p-value (", meth_part, ")") |
94 |
}
|
|
95 | ||
96 |
#' @describeIn prop_diff_test Formatted analysis function which is used as `afun` in `test_proportion_diff()`.
|
|
97 |
#'
|
|
98 |
#' @return
|
|
99 |
#' * `a_test_proportion_diff()` returns the corresponding list with formatted [rtables::CellValue()].
|
|
100 |
#'
|
|
101 |
#'
|
|
102 |
#' @keywords internal
|
|
103 |
a_test_proportion_diff <- make_afun( |
|
104 |
s_test_proportion_diff,
|
|
105 |
.formats = c(pval = "x.xxxx | (<0.0001)"), |
|
106 |
.indent_mods = c(pval = 1L) |
|
107 |
)
|
|
108 | ||
109 |
#' @describeIn prop_diff_test Layout-creating function which can take statistics function arguments
|
|
110 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()].
|
|
111 |
#'
|
|
112 |
#' @param ... other arguments are passed to [s_test_proportion_diff()].
|
|
113 |
#'
|
|
114 |
#' @return
|
|
115 |
#' * `test_proportion_diff()` returns a layout object suitable for passing to further layouting functions,
|
|
116 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing
|
|
117 |
#' the statistics from `s_test_proportion_diff()` to the table layout.
|
|
118 |
#'
|
|
119 |
#' @examples
|
|
120 |
#' dta <- data.frame(
|
|
121 |
#' rsp = sample(c(TRUE, FALSE), 100, TRUE),
|
|
122 |
#' grp = factor(rep(c("A", "B"), each = 50)),
|
|
123 |
#' strat = factor(rep(c("V", "W", "X", "Y", "Z"), each = 20))
|
|
124 |
#' )
|
|
125 |
#'
|
|
126 |
#' # With `rtables` pipelines.
|
|
127 |
#' l <- basic_table() %>%
|
|
128 |
#' split_cols_by(var = "grp", ref_group = "B") %>%
|
|
129 |
#' test_proportion_diff(
|
|
130 |
#' vars = "rsp",
|
|
131 |
#' method = "cmh", variables = list(strata = "strat")
|
|
132 |
#' )
|
|
133 |
#'
|
|
134 |
#' build_table(l, df = dta)
|
|
135 |
#'
|
|
136 |
#' @export
|
|
137 |
test_proportion_diff <- function(lyt, |
|
138 |
vars,
|
|
139 |
...,
|
|
140 |
var_labels = vars, |
|
141 |
show_labels = "hidden", |
|
142 |
table_names = vars, |
|
143 |
.stats = NULL, |
|
144 |
.formats = NULL, |
|
145 |
.labels = NULL, |
|
146 |
.indent_mods = NULL) { |
|
147 | 5x |
afun <- make_afun( |
148 | 5x |
a_test_proportion_diff,
|
149 | 5x |
.stats = .stats, |
150 | 5x |
.formats = .formats, |
151 | 5x |
.labels = .labels, |
152 | 5x |
.indent_mods = .indent_mods |
153 |
)
|
|
154 | 5x |
analyze( |
155 | 5x |
lyt,
|
156 | 5x |
vars,
|
157 | 5x |
afun = afun, |
158 | 5x |
var_labels = var_labels, |
159 | 5x |
extra_args = list(...), |
160 | 5x |
show_labels = show_labels, |
161 | 5x |
table_names = table_names |
162 |
)
|
|
163 |
}
|
|
164 | ||
165 |
#' Helper Functions to Test Proportion Differences
|
|
166 |
#'
|
|
167 |
#' Helper functions to implement various tests on the difference between two proportions.
|
|
168 |
#'
|
|
169 |
#' @param tbl (`matrix`)\cr matrix with two groups in rows and the binary response (`TRUE`/`FALSE`) in columns.
|
|
170 |
#'
|
|
171 |
#' @return A p-value.
|
|
172 |
#'
|
|
173 |
#' @seealso [prop_diff_test()] for implementation of these helper functions.
|
|
174 |
#'
|
|
175 |
#' @name h_prop_diff_test
|
|
176 |
NULL
|
|
177 | ||
178 |
#' @describeIn h_prop_diff_test performs Chi-Squared test. Internally calls [stats::prop.test()].
|
|
179 |
#'
|
|
180 |
#'
|
|
181 |
#' @keywords internal
|
|
182 |
prop_chisq <- function(tbl) { |
|
183 | 23x |
checkmate::assert_integer(c(ncol(tbl), nrow(tbl)), lower = 2, upper = 2) |
184 | 23x |
tbl <- tbl[, c("TRUE", "FALSE")] |
185 | 23x |
if (any(colSums(tbl) == 0)) { |
186 | 2x |
return(1) |
187 |
}
|
|
188 | 21x |
stats::prop.test(tbl, correct = FALSE)$p.value |
189 |
}
|
|
190 | ||
191 |
#' @describeIn h_prop_diff_test performs stratified Cochran-Mantel-Haenszel test. Internally calls
|
|
192 |
#' [stats::mantelhaen.test()]. Note that strata with less than two observations are automatically discarded.
|
|
193 |
#'
|
|
194 |
#' @param ary (`array`, 3 dimensions)\cr array with two groups in rows, the binary response
|
|
195 |
#' (`TRUE`/`FALSE`) in columns, and the strata in the third dimension.
|
|
196 |
#'
|
|
197 |
#'
|
|
198 |
#' @keywords internal
|
|
199 |
prop_cmh <- function(ary) { |
|
200 | 16x |
checkmate::assert_array(ary) |
201 | 16x |
checkmate::assert_integer(c(ncol(ary), nrow(ary)), lower = 2, upper = 2) |
202 | 16x |
checkmate::assert_integer(length(dim(ary)), lower = 3, upper = 3) |
203 | 16x |
strata_sizes <- apply(ary, MARGIN = 3, sum) |
204 | 16x |
if (any(strata_sizes < 5)) { |
205 | 1x |
warning("<5 data points in some strata. CMH test may be incorrect.") |
206 | 1x |
ary <- ary[, , strata_sizes > 1] |
207 |
}
|
|
208 | ||
209 | 16x |
stats::mantelhaen.test(ary, correct = FALSE)$p.value |
210 |
}
|
|
211 | ||
212 |
#' @describeIn h_prop_diff_test performs the Chi-Squared test with Schouten correction.
|
|
213 |
#'
|
|
214 |
#' @seealso For information on the Schouten correction (Schouten, 1980),
|
|
215 |
#' visit \url{https://onlinelibrary.wiley.com/doi/abs/10.1002/bimj.4710220305}.
|
|
216 |
#'
|
|
217 |
#'
|
|
218 |
#' @keywords internal
|
|
219 |
prop_schouten <- function(tbl) { |
|
220 | 100x |
checkmate::assert_integer(c(ncol(tbl), nrow(tbl)), lower = 2, upper = 2) |
221 | 100x |
tbl <- tbl[, c("TRUE", "FALSE")] |
222 | 100x |
if (any(colSums(tbl) == 0)) { |
223 | 1x |
return(1) |
224 |
}
|
|
225 | ||
226 | 99x |
n <- sum(tbl) |
227 | 99x |
n1 <- sum(tbl[1, ]) |
228 | 99x |
n2 <- sum(tbl[2, ]) |
229 | ||
230 | 99x |
ad <- diag(tbl) |
231 | 99x |
bc <- diag(apply(tbl, 2, rev)) |
232 | 99x |
ac <- tbl[, 1] |
233 | 99x |
bd <- tbl[, 2] |
234 | ||
235 | 99x |
t_schouten <- (n - 1) * |
236 | 99x |
(abs(prod(ad) - prod(bc)) - 0.5 * min(n1, n2))^2 / |
237 | 99x |
(n1 * n2 * sum(ac) * sum(bd)) |
238 | ||
239 | 99x |
1 - stats::pchisq(t_schouten, df = 1) |
240 |
}
|
|
241 | ||
242 |
#' @describeIn h_prop_diff_test performs the Fisher's exact test. Internally calls [stats::fisher.test()].
|
|
243 |
#'
|
|
244 |
#'
|
|
245 |
#' @keywords internal
|
|
246 |
prop_fisher <- function(tbl) { |
|
247 | 2x |
checkmate::assert_integer(c(ncol(tbl), nrow(tbl)), lower = 2, upper = 2) |
248 | 2x |
tbl <- tbl[, c("TRUE", "FALSE")] |
249 | 2x |
stats::fisher.test(tbl)$p.value |
250 |
}
|
1 |
#' Individual Patient Plots
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Line plot(s) displaying trend in patients' parameter values over time is rendered.
|
|
6 |
#' Patients' individual baseline values can be added to the plot(s) as reference.
|
|
7 |
#'
|
|
8 |
#' @inheritParams argument_convention
|
|
9 |
#' @param xvar (`string`)\cr time point variable to be plotted on x-axis.
|
|
10 |
#' @param yvar (`string`)\cr continuous analysis variable to be plotted on y-axis.
|
|
11 |
#' @param xlab (`string`)\cr plot label for x-axis.
|
|
12 |
#' @param ylab (`string`)\cr plot label for y-axis.
|
|
13 |
#' @param id_var (`string`)\cr variable used as patient identifier.
|
|
14 |
#' @param title (`string`)\cr title for plot.
|
|
15 |
#' @param subtitle (`string`)\cr subtitle for plot.
|
|
16 |
#' @param add_baseline_hline (`flag`)\cr adds horizontal line at baseline y-value on
|
|
17 |
#' plot when TRUE.
|
|
18 |
#' @param yvar_baseline (`string`)\cr variable with baseline values only.
|
|
19 |
#' Ignored when `add_baseline_hline` is FALSE.
|
|
20 |
#' @param ggtheme (`theme`)\cr optional graphical theme function as provided
|
|
21 |
#' by `ggplot2` to control outlook of plot. Use `ggplot2::theme()` to tweak the display.
|
|
22 |
#' @param plotting_choices (`character`)\cr specifies options for displaying
|
|
23 |
#' plots. Must be one of "all_in_one", "split_by_max_obs", "separate_by_obs".
|
|
24 |
#' @param max_obs_per_plot (`count`)\cr Number of observations to be plotted on one
|
|
25 |
#' plot. Ignored when `plotting_choices` is not "separate_by_obs".
|
|
26 |
#' @param caption (`character` scalar)\cr optional caption below the plot.
|
|
27 |
#' @param col (`character`)\cr lines colors.
|
|
28 |
#'
|
|
29 |
#' @seealso Relevant helper function [h_g_ipp()].
|
|
30 |
#'
|
|
31 |
#' @name individual_patient_plot
|
|
32 |
NULL
|
|
33 | ||
34 |
#' Helper Function To Create Simple Line Plot over Time
|
|
35 |
#'
|
|
36 |
#' @description `r lifecycle::badge("stable")`
|
|
37 |
#'
|
|
38 |
#' Function that generates a simple line plot displaying parameter trends over time.
|
|
39 |
#'
|
|
40 |
#' @inheritParams argument_convention
|
|
41 |
#' @inheritParams g_ipp
|
|
42 |
#'
|
|
43 |
#' @return A `ggplot` line plot.
|
|
44 |
#'
|
|
45 |
#' @seealso [g_ipp()] which uses this function.
|
|
46 |
#'
|
|
47 |
#' @examples
|
|
48 |
#' library(dplyr)
|
|
49 |
#' library(nestcolor)
|
|
50 |
#'
|
|
51 |
#' # Select a small sample of data to plot.
|
|
52 |
#' adlb <- tern_ex_adlb %>%
|
|
53 |
#' filter(PARAMCD == "ALT", !(AVISIT %in% c("SCREENING", "BASELINE"))) %>%
|
|
54 |
#' slice(1:36)
|
|
55 |
#'
|
|
56 |
#' p <- h_g_ipp(
|
|
57 |
#' df = adlb,
|
|
58 |
#' xvar = "AVISIT",
|
|
59 |
#' yvar = "AVAL",
|
|
60 |
#' xlab = "Visit",
|
|
61 |
#' id_var = "USUBJID",
|
|
62 |
#' ylab = "SGOT/ALT (U/L)",
|
|
63 |
#' add_baseline_hline = TRUE
|
|
64 |
#' )
|
|
65 |
#' p
|
|
66 |
#'
|
|
67 |
#' @export
|
|
68 |
h_g_ipp <- function(df, |
|
69 |
xvar,
|
|
70 |
yvar,
|
|
71 |
xlab,
|
|
72 |
ylab,
|
|
73 |
id_var,
|
|
74 |
title = "Individual Patient Plots", |
|
75 |
subtitle = "", |
|
76 |
caption = NULL, |
|
77 |
add_baseline_hline = FALSE, |
|
78 |
yvar_baseline = "BASE", |
|
79 |
ggtheme = nestcolor::theme_nest(), |
|
80 |
col = NULL) { |
|
81 | 13x |
checkmate::assert_string(xvar) |
82 | 13x |
checkmate::assert_string(yvar) |
83 | 13x |
checkmate::assert_string(yvar_baseline) |
84 | 13x |
checkmate::assert_string(id_var) |
85 | 13x |
checkmate::assert_string(xlab) |
86 | 13x |
checkmate::assert_string(ylab) |
87 | 13x |
checkmate::assert_string(title) |
88 | 13x |
checkmate::assert_string(subtitle) |
89 | 13x |
checkmate::assert_subset(c(xvar, yvar, yvar_baseline, id_var), colnames(df)) |
90 | 13x |
checkmate::assert_data_frame(df) |
91 | 13x |
checkmate::assert_flag(add_baseline_hline) |
92 | 13x |
checkmate::assert_character(col, null.ok = TRUE) |
93 | ||
94 | 13x |
p <- ggplot2::ggplot( |
95 | 13x |
data = df, |
96 | 13x |
mapping = ggplot2::aes( |
97 | 13x |
x = .data[[xvar]], |
98 | 13x |
y = .data[[yvar]], |
99 | 13x |
group = .data[[id_var]], |
100 | 13x |
colour = .data[[id_var]] |
101 |
)
|
|
102 |
) + |
|
103 | 13x |
ggplot2::geom_line(linewidth = 0.4) + |
104 | 13x |
ggplot2::geom_point(size = 2) + |
105 | 13x |
ggplot2::labs( |
106 | 13x |
x = xlab, |
107 | 13x |
y = ylab, |
108 | 13x |
title = title, |
109 | 13x |
subtitle = subtitle, |
110 | 13x |
caption = caption |
111 |
) + |
|
112 | 13x |
ggtheme
|
113 | ||
114 | 13x |
if (add_baseline_hline) { |
115 | 12x |
baseline_df <- df[, c(id_var, yvar_baseline)] |
116 | 12x |
baseline_df <- unique(baseline_df) |
117 | ||
118 | 12x |
p <- p + |
119 | 12x |
ggplot2::geom_hline( |
120 | 12x |
data = baseline_df, |
121 | 12x |
mapping = ggplot2::aes( |
122 | 12x |
yintercept = .data[[yvar_baseline]], |
123 | 12x |
colour = .data[[id_var]] |
124 |
),
|
|
125 | 12x |
linetype = "dotdash", |
126 | 12x |
linewidth = 0.4 |
127 |
) + |
|
128 | 12x |
ggplot2::geom_text( |
129 | 12x |
data = baseline_df, |
130 | 12x |
mapping = ggplot2::aes( |
131 | 12x |
x = 1, |
132 | 12x |
y = .data[[yvar_baseline]], |
133 | 12x |
label = .data[[id_var]], |
134 | 12x |
colour = .data[[id_var]] |
135 |
),
|
|
136 | 12x |
nudge_y = 0.025 * (max(df[, yvar], na.rm = TRUE) - min(df[, yvar], na.rm = TRUE)), |
137 | 12x |
vjust = "right", |
138 | 12x |
size = 2 |
139 |
)
|
|
140 | ||
141 | 12x |
if (!is.null(col)) { |
142 | 1x |
p <- p + |
143 | 1x |
ggplot2::scale_color_manual(values = col) |
144 |
}
|
|
145 |
}
|
|
146 | 13x |
p
|
147 |
}
|
|
148 | ||
149 |
#' @describeIn individual_patient_plot Plotting function for individual patient plots which, depending on user
|
|
150 |
#' preference, renders a single graphic or compiles a list of graphics that show trends in individual's parameter
|
|
151 |
#' values over time.
|
|
152 |
#'
|
|
153 |
#' @return A `ggplot` object or a list of `ggplot` objects.
|
|
154 |
#'
|
|
155 |
#' @examples
|
|
156 |
#' library(dplyr)
|
|
157 |
#' library(nestcolor)
|
|
158 |
#'
|
|
159 |
#' # Select a small sample of data to plot.
|
|
160 |
#' adlb <- tern_ex_adlb %>%
|
|
161 |
#' filter(PARAMCD == "ALT", !(AVISIT %in% c("SCREENING", "BASELINE"))) %>%
|
|
162 |
#' slice(1:36)
|
|
163 |
#'
|
|
164 |
#' plot_list <- g_ipp(
|
|
165 |
#' df = adlb,
|
|
166 |
#' xvar = "AVISIT",
|
|
167 |
#' yvar = "AVAL",
|
|
168 |
#' xlab = "Visit",
|
|
169 |
#' ylab = "SGOT/ALT (U/L)",
|
|
170 |
#' title = "Individual Patient Plots",
|
|
171 |
#' add_baseline_hline = TRUE,
|
|
172 |
#' plotting_choices = "split_by_max_obs",
|
|
173 |
#' max_obs_per_plot = 5
|
|
174 |
#' )
|
|
175 |
#' plot_list
|
|
176 |
#'
|
|
177 |
#' @export
|
|
178 |
g_ipp <- function(df, |
|
179 |
xvar,
|
|
180 |
yvar,
|
|
181 |
xlab,
|
|
182 |
ylab,
|
|
183 |
id_var = "USUBJID", |
|
184 |
title = "Individual Patient Plots", |
|
185 |
subtitle = "", |
|
186 |
caption = NULL, |
|
187 |
add_baseline_hline = FALSE, |
|
188 |
yvar_baseline = "BASE", |
|
189 |
ggtheme = nestcolor::theme_nest(), |
|
190 |
plotting_choices = c("all_in_one", "split_by_max_obs", "separate_by_obs"), |
|
191 |
max_obs_per_plot = 4, |
|
192 |
col = NULL) { |
|
193 | 3x |
checkmate::assert_count(max_obs_per_plot) |
194 | 3x |
checkmate::assert_subset(plotting_choices, c("all_in_one", "split_by_max_obs", "separate_by_obs")) |
195 | 3x |
checkmate::assert_character(col, null.ok = TRUE) |
196 | ||
197 | 3x |
plotting_choices <- match.arg(plotting_choices) |
198 | ||
199 | 3x |
if (plotting_choices == "all_in_one") { |
200 | 1x |
p <- h_g_ipp( |
201 | 1x |
df = df, |
202 | 1x |
xvar = xvar, |
203 | 1x |
yvar = yvar, |
204 | 1x |
xlab = xlab, |
205 | 1x |
ylab = ylab, |
206 | 1x |
id_var = id_var, |
207 | 1x |
title = title, |
208 | 1x |
subtitle = subtitle, |
209 | 1x |
caption = caption, |
210 | 1x |
add_baseline_hline = add_baseline_hline, |
211 | 1x |
yvar_baseline = yvar_baseline, |
212 | 1x |
ggtheme = ggtheme, |
213 | 1x |
col = col |
214 |
)
|
|
215 | ||
216 | 1x |
return(p) |
217 | 2x |
} else if (plotting_choices == "split_by_max_obs") { |
218 | 1x |
id_vec <- unique(df[[id_var]]) |
219 | 1x |
id_list <- split( |
220 | 1x |
id_vec,
|
221 | 1x |
rep(1:ceiling(length(id_vec) / max_obs_per_plot), |
222 | 1x |
each = max_obs_per_plot, |
223 | 1x |
length.out = length(id_vec) |
224 |
)
|
|
225 |
)
|
|
226 | ||
227 | 1x |
df_list <- list() |
228 | 1x |
plot_list <- list() |
229 | ||
230 | 1x |
for (i in seq_along(id_list)) { |
231 | 2x |
df_list[[i]] <- df[df[[id_var]] %in% id_list[[i]], ] |
232 | ||
233 | 2x |
plots <- h_g_ipp( |
234 | 2x |
df = df_list[[i]], |
235 | 2x |
xvar = xvar, |
236 | 2x |
yvar = yvar, |
237 | 2x |
xlab = xlab, |
238 | 2x |
ylab = ylab, |
239 | 2x |
id_var = id_var, |
240 | 2x |
title = title, |
241 | 2x |
subtitle = subtitle, |
242 | 2x |
caption = caption, |
243 | 2x |
add_baseline_hline = add_baseline_hline, |
244 | 2x |
yvar_baseline = yvar_baseline, |
245 | 2x |
ggtheme = ggtheme, |
246 | 2x |
col = col |
247 |
)
|
|
248 | ||
249 | 2x |
plot_list[[i]] <- plots |
250 |
}
|
|
251 | 1x |
return(plot_list) |
252 |
} else { |
|
253 | 1x |
ind_df <- split(df, df[[id_var]]) |
254 | 1x |
plot_list <- lapply( |
255 | 1x |
ind_df,
|
256 | 1x |
function(x) { |
257 | 8x |
h_g_ipp( |
258 | 8x |
df = x, |
259 | 8x |
xvar = xvar, |
260 | 8x |
yvar = yvar, |
261 | 8x |
xlab = xlab, |
262 | 8x |
ylab = ylab, |
263 | 8x |
id_var = id_var, |
264 | 8x |
title = title, |
265 | 8x |
subtitle = subtitle, |
266 | 8x |
caption = caption, |
267 | 8x |
add_baseline_hline = add_baseline_hline, |
268 | 8x |
yvar_baseline = yvar_baseline, |
269 | 8x |
ggtheme = ggtheme, |
270 | 8x |
col = col |
271 |
)
|
|
272 |
}
|
|
273 |
)
|
|
274 | ||
275 | 1x |
return(plot_list) |
276 |
}
|
|
277 |
}
|
1 |
#' Count the Number of Patients with a Particular Event
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' The primary analysis variable `.var` denotes the unique patient identifier.
|
|
6 |
#'
|
|
7 |
#' @inheritParams argument_convention
|
|
8 |
#'
|
|
9 |
#' @seealso [count_patients_with_flags]
|
|
10 |
#'
|
|
11 |
#' @name count_patients_with_event
|
|
12 |
NULL
|
|
13 | ||
14 |
#' @describeIn count_patients_with_event Statistics function which counts the number of patients for which
|
|
15 |
#' the defined event has occurred.
|
|
16 |
#'
|
|
17 |
#' @inheritParams summarize_variables
|
|
18 |
#' @param .var (`character`)\cr name of the column that contains the unique identifier.
|
|
19 |
#' @param filters (`character`)\cr a character vector specifying the column names and flag variables
|
|
20 |
#' to be used for counting the number of unique identifiers satisfying such conditions.
|
|
21 |
#' Multiple column names and flags are accepted in this format
|
|
22 |
#' `c("column_name1" = "flag1", "column_name2" = "flag2")`.
|
|
23 |
#' Note that only equality is being accepted as condition.
|
|
24 |
#'
|
|
25 |
#' @return
|
|
26 |
#' * `s_count_patients_with_event()` returns the count and fraction of unique identifiers with the defined event.
|
|
27 |
#'
|
|
28 |
#' @examples
|
|
29 |
#' library(dplyr)
|
|
30 |
#'
|
|
31 |
#' # `s_count_patients_with_event()`
|
|
32 |
#'
|
|
33 |
#' s_count_patients_with_event(
|
|
34 |
#' tern_ex_adae,
|
|
35 |
#' .var = "SUBJID",
|
|
36 |
#' filters = c("TRTEMFL" = "Y")
|
|
37 |
#' )
|
|
38 |
#' s_count_patients_with_event(
|
|
39 |
#' tern_ex_adae,
|
|
40 |
#' .var = "SUBJID",
|
|
41 |
#' filters = c("TRTEMFL" = "Y", "AEOUT" = "FATAL")
|
|
42 |
#' )
|
|
43 |
#' s_count_patients_with_event(
|
|
44 |
#' tern_ex_adae,
|
|
45 |
#' .var = "SUBJID",
|
|
46 |
#' filters = c("TRTEMFL" = "Y", "AEOUT" = "FATAL"),
|
|
47 |
#' denom = "N_col",
|
|
48 |
#' .N_col = 456
|
|
49 |
#' )
|
|
50 |
#'
|
|
51 |
#' @export
|
|
52 |
s_count_patients_with_event <- function(df, |
|
53 |
.var,
|
|
54 |
filters,
|
|
55 |
.N_col, # nolint |
|
56 |
.N_row, # nolint |
|
57 |
denom = c("n", "N_row", "N_col")) { |
|
58 | 28x |
col_names <- names(filters) |
59 | 28x |
filter_values <- filters |
60 | ||
61 | 28x |
checkmate::assert_subset(col_names, colnames(df)) |
62 | ||
63 | 28x |
temp <- Map( |
64 | 28x |
function(x, y) which(df[[x]] == y), |
65 | 28x |
col_names,
|
66 | 28x |
filter_values
|
67 |
)
|
|
68 | 28x |
position_satisfy_filters <- Reduce(intersect, temp) |
69 | 28x |
id_satisfy_filters <- as.character(unique(df[position_satisfy_filters, ][[.var]])) |
70 | 28x |
result <- s_count_values( |
71 | 28x |
as.character(unique(df[[.var]])), |
72 | 28x |
id_satisfy_filters,
|
73 | 28x |
denom = denom, |
74 | 28x |
.N_col = .N_col, |
75 | 28x |
.N_row = .N_row |
76 |
)
|
|
77 | 28x |
result
|
78 |
}
|
|
79 | ||
80 |
#' @describeIn count_patients_with_event Formatted analysis function which is used as `afun`
|
|
81 |
#' in `count_patients_with_event()`.
|
|
82 |
#'
|
|
83 |
#' @return
|
|
84 |
#' * `a_count_patients_with_event()` returns the corresponding list with formatted [rtables::CellValue()].
|
|
85 |
#'
|
|
86 |
#' @examples
|
|
87 |
#' # `a_count_patients_with_event()`
|
|
88 |
#'
|
|
89 |
#' a_count_patients_with_event(
|
|
90 |
#' tern_ex_adae,
|
|
91 |
#' .var = "SUBJID",
|
|
92 |
#' filters = c("TRTEMFL" = "Y"),
|
|
93 |
#' .N_col = 100,
|
|
94 |
#' .N_row = 100
|
|
95 |
#' )
|
|
96 |
#'
|
|
97 |
#' @export
|
|
98 |
a_count_patients_with_event <- make_afun( |
|
99 |
s_count_patients_with_event,
|
|
100 |
.formats = c(count_fraction = format_count_fraction_fixed_dp) |
|
101 |
)
|
|
102 | ||
103 |
#' @describeIn count_patients_with_event Layout-creating function which can take statistics function
|
|
104 |
#' arguments and additional format arguments. This function is a wrapper for [rtables::analyze()].
|
|
105 |
#'
|
|
106 |
#' @return
|
|
107 |
#' * `count_patients_with_event()` returns a layout object suitable for passing to further layouting functions,
|
|
108 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing
|
|
109 |
#' the statistics from `s_count_patients_with_event()` to the table layout.
|
|
110 |
#'
|
|
111 |
#' @examples
|
|
112 |
#' # `count_patients_with_event()`
|
|
113 |
#'
|
|
114 |
#' lyt <- basic_table() %>%
|
|
115 |
#' split_cols_by("ARM") %>%
|
|
116 |
#' add_colcounts() %>%
|
|
117 |
#' count_values(
|
|
118 |
#' "STUDYID",
|
|
119 |
#' values = "AB12345",
|
|
120 |
#' .stats = "count",
|
|
121 |
#' .labels = c(count = "Total AEs")
|
|
122 |
#' ) %>%
|
|
123 |
#' count_patients_with_event(
|
|
124 |
#' "SUBJID",
|
|
125 |
#' filters = c("TRTEMFL" = "Y"),
|
|
126 |
#' .labels = c(count_fraction = "Total number of patients with at least one adverse event"),
|
|
127 |
#' table_names = "tbl_all"
|
|
128 |
#' ) %>%
|
|
129 |
#' count_patients_with_event(
|
|
130 |
#' "SUBJID",
|
|
131 |
#' filters = c("TRTEMFL" = "Y", "AEOUT" = "FATAL"),
|
|
132 |
#' .labels = c(count_fraction = "Total number of patients with fatal AEs"),
|
|
133 |
#' table_names = "tbl_fatal"
|
|
134 |
#' ) %>%
|
|
135 |
#' count_patients_with_event(
|
|
136 |
#' "SUBJID",
|
|
137 |
#' filters = c("TRTEMFL" = "Y", "AEOUT" = "FATAL", "AEREL" = "Y"),
|
|
138 |
#' .labels = c(count_fraction = "Total number of patients with related fatal AEs"),
|
|
139 |
#' .indent_mods = c(count_fraction = 2L),
|
|
140 |
#' table_names = "tbl_rel_fatal"
|
|
141 |
#' )
|
|
142 |
#' build_table(lyt, tern_ex_adae, alt_counts_df = tern_ex_adsl)
|
|
143 |
#'
|
|
144 |
#' @export
|
|
145 |
count_patients_with_event <- function(lyt, |
|
146 |
vars,
|
|
147 |
...,
|
|
148 |
table_names = vars, |
|
149 |
.stats = "count_fraction", |
|
150 |
.formats = NULL, |
|
151 |
.labels = NULL, |
|
152 |
.indent_mods = NULL) { |
|
153 | 5x |
afun <- make_afun( |
154 | 5x |
a_count_patients_with_event,
|
155 | 5x |
.stats = .stats, |
156 | 5x |
.formats = .formats, |
157 | 5x |
.labels = .labels, |
158 | 5x |
.indent_mods = .indent_mods |
159 |
)
|
|
160 | ||
161 | 5x |
analyze( |
162 | 5x |
lyt,
|
163 | 5x |
vars,
|
164 | 5x |
afun = afun, |
165 | 5x |
extra_args = list(...), |
166 | 5x |
show_labels = ifelse(length(vars) > 1, "visible", "hidden"), |
167 | 5x |
table_names = table_names |
168 |
)
|
|
169 |
}
|
1 |
#' Cox Proportional Hazards Regression
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Fits a Cox regression model and estimates hazard ratio to describe the effect size in a survival analysis.
|
|
6 |
#'
|
|
7 |
#' @inheritParams argument_convention
|
|
8 |
#'
|
|
9 |
#' @details Cox models are the most commonly used methods to estimate the magnitude of
|
|
10 |
#' the effect in survival analysis. It assumes proportional hazards: the ratio
|
|
11 |
#' of the hazards between groups (e.g., two arms) is constant over time.
|
|
12 |
#' This ratio is referred to as the "hazard ratio" (HR) and is one of the
|
|
13 |
#' most commonly reported metrics to describe the effect size in survival
|
|
14 |
#' analysis (NEST Team, 2020).
|
|
15 |
#'
|
|
16 |
#' @seealso [fit_coxreg] for relevant fitting functions, [h_cox_regression] for relevant
|
|
17 |
#' helper functions, and [tidy_coxreg] for custom tidy methods.
|
|
18 |
#'
|
|
19 |
#' @examples
|
|
20 |
#' library(survival)
|
|
21 |
#'
|
|
22 |
#' # Testing dataset [survival::bladder].
|
|
23 |
#' set.seed(1, kind = "Mersenne-Twister")
|
|
24 |
#' dta_bladder <- with(
|
|
25 |
#' data = bladder[bladder$enum < 5, ],
|
|
26 |
#' tibble::tibble(
|
|
27 |
#' TIME = stop,
|
|
28 |
#' STATUS = event,
|
|
29 |
#' ARM = as.factor(rx),
|
|
30 |
#' COVAR1 = as.factor(enum) %>% formatters::with_label("A Covariate Label"),
|
|
31 |
#' COVAR2 = factor(
|
|
32 |
#' sample(as.factor(enum)),
|
|
33 |
#' levels = 1:4, labels = c("F", "F", "M", "M")
|
|
34 |
#' ) %>% formatters::with_label("Sex (F/M)")
|
|
35 |
#' )
|
|
36 |
#' )
|
|
37 |
#' dta_bladder$AGE <- sample(20:60, size = nrow(dta_bladder), replace = TRUE)
|
|
38 |
#' dta_bladder$STUDYID <- factor("X")
|
|
39 |
#'
|
|
40 |
#' plot(
|
|
41 |
#' survfit(Surv(TIME, STATUS) ~ ARM + COVAR1, data = dta_bladder),
|
|
42 |
#' lty = 2:4,
|
|
43 |
#' xlab = "Months",
|
|
44 |
#' col = c("blue1", "blue2", "blue3", "blue4", "red1", "red2", "red3", "red4")
|
|
45 |
#' )
|
|
46 |
#'
|
|
47 |
#' @name cox_regression
|
|
48 |
NULL
|
|
49 | ||
50 |
#' @describeIn cox_regression Statistics function that transforms results tabulated
|
|
51 |
#' from [fit_coxreg_univar()] or [fit_coxreg_multivar()] into a list.
|
|
52 |
#'
|
|
53 |
#' @param model_df (`data.frame`)\cr contains the resulting model fit from a [fit_coxreg]
|
|
54 |
#' function with tidying applied via [broom::tidy()].
|
|
55 |
#' @param .stats (`character`)\cr the name of statistics to be reported among:
|
|
56 |
#' * `n`: number of observations (univariate only)
|
|
57 |
#' * `hr`: hazard ratio
|
|
58 |
#' * `ci`: confidence interval
|
|
59 |
#' * `pval`: p-value of the treatment effect
|
|
60 |
#' * `pval_inter`: p-value of the interaction effect between the treatment and the covariate (univariate only)
|
|
61 |
#' @param .which_vars (`character`)\cr which rows should statistics be returned for from the given model.
|
|
62 |
#' Defaults to "all". Other options include "var_main" for main effects, `"inter"` for interaction effects,
|
|
63 |
#' and `"multi_lvl"` for multivariate model covariate level rows. When `.which_vars` is "all" specific
|
|
64 |
#' variables can be selected by specifying `.var_nms`.
|
|
65 |
#' @param .var_nms (`character`)\cr the `term` value of rows in `df` for which `.stats` should be returned. Typically
|
|
66 |
#' this is the name of a variable. If using variable labels, `var` should be a vector of both the desired
|
|
67 |
#' variable name and the variable label in that order to see all `.stats` related to that variable. When `.which_vars`
|
|
68 |
#' is `"var_main"` `.var_nms` should be only the variable name.
|
|
69 |
#'
|
|
70 |
#' @return
|
|
71 |
#' * `s_coxreg()` returns the selected statistic for from the Cox regression model for the selected variable(s).
|
|
72 |
#'
|
|
73 |
#' @examples
|
|
74 |
#' # s_coxreg
|
|
75 |
#'
|
|
76 |
#' # Univariate
|
|
77 |
#' u1_variables <- list(
|
|
78 |
#' time = "TIME", event = "STATUS", arm = "ARM", covariates = c("COVAR1", "COVAR2")
|
|
79 |
#' )
|
|
80 |
#' univar_model <- fit_coxreg_univar(variables = u1_variables, data = dta_bladder)
|
|
81 |
#' df1 <- broom::tidy(univar_model)
|
|
82 |
#' s_coxreg(model_df = df1, .stats = "hr")
|
|
83 |
#'
|
|
84 |
#' # Univariate with interactions
|
|
85 |
#' univar_model_inter <- fit_coxreg_univar(
|
|
86 |
#' variables = u1_variables, control = control_coxreg(interaction = TRUE), data = dta_bladder
|
|
87 |
#' )
|
|
88 |
#' df1_inter <- broom::tidy(univar_model_inter)
|
|
89 |
#' s_coxreg(model_df = df1_inter, .stats = "hr", .which_vars = "inter", .var_nms = "COVAR1")
|
|
90 |
#'
|
|
91 |
#' # Univariate without treatment arm - only "COVAR2" covariate effects
|
|
92 |
#' u2_variables <- list(time = "TIME", event = "STATUS", covariates = c("COVAR1", "COVAR2"))
|
|
93 |
#' univar_covs_model <- fit_coxreg_univar(variables = u2_variables, data = dta_bladder)
|
|
94 |
#' df1_covs <- broom::tidy(univar_covs_model)
|
|
95 |
#' s_coxreg(model_df = df1_covs, .stats = "hr", .var_nms = c("COVAR2", "Sex (F/M)"))
|
|
96 |
#'
|
|
97 |
#' # Multivariate.
|
|
98 |
#' m1_variables <- list(
|
|
99 |
#' time = "TIME", event = "STATUS", arm = "ARM", covariates = c("COVAR1", "COVAR2")
|
|
100 |
#' )
|
|
101 |
#' multivar_model <- fit_coxreg_multivar(variables = m1_variables, data = dta_bladder)
|
|
102 |
#' df2 <- broom::tidy(multivar_model)
|
|
103 |
#' s_coxreg(model_df = df2, .stats = "pval", .which_vars = "var_main", .var_nms = "COVAR1")
|
|
104 |
#' s_coxreg(
|
|
105 |
#' model_df = df2, .stats = "pval", .which_vars = "multi_lvl",
|
|
106 |
#' .var_nms = c("COVAR1", "A Covariate Label")
|
|
107 |
#' )
|
|
108 |
#'
|
|
109 |
#' # Multivariate without treatment arm - only "COVAR1" main effect
|
|
110 |
#' m2_variables <- list(time = "TIME", event = "STATUS", covariates = c("COVAR1", "COVAR2"))
|
|
111 |
#' multivar_covs_model <- fit_coxreg_multivar(variables = m2_variables, data = dta_bladder)
|
|
112 |
#' df2_covs <- broom::tidy(multivar_covs_model)
|
|
113 |
#' s_coxreg(model_df = df2_covs, .stats = "hr")
|
|
114 |
#'
|
|
115 |
#' @export
|
|
116 |
s_coxreg <- function(model_df, .stats, .which_vars = "all", .var_nms = NULL) { |
|
117 | 194x |
assert_df_with_variables(model_df, list(term = "term", stat = .stats)) |
118 | 194x |
checkmate::assert_multi_class(model_df$term, classes = c("factor", "character")) |
119 | 194x |
model_df$term <- as.character(model_df$term) |
120 | 194x |
.var_nms <- .var_nms[!is.na(.var_nms)] |
121 | ||
122 | 192x |
if (length(.var_nms) > 0) model_df <- model_df[model_df$term %in% .var_nms, ] |
123 | 39x |
if (.which_vars == "multi_lvl") model_df$term <- tail(.var_nms, 1) |
124 | ||
125 |
# We need a list with names corresponding to the stats to display of equal length to the list of stats.
|
|
126 | 194x |
y <- split(model_df, f = model_df$term, drop = FALSE) |
127 | 194x |
y <- stats::setNames(y, nm = rep(.stats, length(y))) |
128 | ||
129 | 194x |
if (.which_vars == "var_main") { |
130 | 84x |
y <- lapply(y, function(x) x[1, ]) # only main effect |
131 | 110x |
} else if (.which_vars %in% c("inter", "multi_lvl")) { |
132 | 80x |
y <- lapply(y, function(x) if (nrow(y[[1]]) > 1) x[-1, ] else x) # exclude main effect |
133 |
}
|
|
134 | ||
135 | 194x |
lapply( |
136 | 194x |
X = y, |
137 | 194x |
FUN = function(x) { |
138 | 198x |
z <- as.list(x[[.stats]]) |
139 | 198x |
stats::setNames(z, nm = x$term_label) |
140 |
}
|
|
141 |
)
|
|
142 |
}
|
|
143 | ||
144 |
#' @describeIn cox_regression Analysis function which is used as `afun` in [rtables::analyze()]
|
|
145 |
#' and `cfun` in [rtables::summarize_row_groups()] within `summarize_coxreg()`.
|
|
146 |
#'
|
|
147 |
#' @param eff (`flag`)\cr whether treatment effect should be calculated. Defaults to `FALSE`.
|
|
148 |
#' @param var_main (`flag`)\cr whether main effects should be calculated. Defaults to `FALSE`.
|
|
149 |
#' @param na_level (`string`)\cr custom string to replace all `NA` values with. Defaults to `""`.
|
|
150 |
#' @param cache_env (`environment`)\cr an environment object used to cache the regression model in order to
|
|
151 |
#' avoid repeatedly fitting the same model for every row in the table. Defaults to `NULL` (no caching).
|
|
152 |
#' @param varlabels (`list`)\cr a named list corresponds to the names of variables found in data, passed
|
|
153 |
#' as a named list and corresponding to time, event, arm, strata, and covariates terms. If arm is missing
|
|
154 |
#' from variables, then only Cox model(s) including the covariates will be fitted and the corresponding
|
|
155 |
#' effect estimates will be tabulated later.
|
|
156 |
#'
|
|
157 |
#' @return
|
|
158 |
#' * `a_coxreg()` returns formatted [rtables::CellValue()].
|
|
159 |
#'
|
|
160 |
#' @examples
|
|
161 |
#' a_coxreg(
|
|
162 |
#' df = dta_bladder,
|
|
163 |
#' labelstr = "Label 1",
|
|
164 |
#' variables = u1_variables,
|
|
165 |
#' .spl_context = list(value = "COVAR1"),
|
|
166 |
#' .stats = "n",
|
|
167 |
#' .formats = "xx"
|
|
168 |
#' )
|
|
169 |
#'
|
|
170 |
#' a_coxreg(
|
|
171 |
#' df = dta_bladder,
|
|
172 |
#' labelstr = "",
|
|
173 |
#' variables = u1_variables,
|
|
174 |
#' .spl_context = list(value = "COVAR2"),
|
|
175 |
#' .stats = "pval",
|
|
176 |
#' .formats = "xx.xxxx"
|
|
177 |
#' )
|
|
178 |
#'
|
|
179 |
#' @export
|
|
180 |
a_coxreg <- function(df, |
|
181 |
labelstr,
|
|
182 |
eff = FALSE, |
|
183 |
var_main = FALSE, |
|
184 |
multivar = FALSE, |
|
185 |
variables,
|
|
186 |
at = list(), |
|
187 |
control = control_coxreg(), |
|
188 |
.spl_context,
|
|
189 |
.stats,
|
|
190 |
.formats,
|
|
191 |
.indent_mods = NULL, |
|
192 |
na_level = "", |
|
193 |
cache_env = NULL) { |
|
194 | 191x |
cov_no_arm <- !multivar && !"arm" %in% names(variables) && control$interaction # special case: univar no arm |
195 | 191x |
cov <- tail(.spl_context$value, 1) # current variable/covariate |
196 | 191x |
var_lbl <- formatters::var_labels(df)[cov] # check for df labels |
197 | 191x |
if (length(labelstr) > 1) { |
198 | ! |
labelstr <- if (cov %in% names(labelstr)) labelstr[[cov]] else var_lbl # use df labels if none |
199 | 191x |
} else if (!is.na(var_lbl) && labelstr == cov && cov %in% variables$covariates) { |
200 | 62x |
labelstr <- var_lbl |
201 |
}
|
|
202 | 191x |
if (eff || multivar || cov_no_arm) { |
203 | 82x |
control$interaction <- FALSE |
204 |
} else { |
|
205 | 109x |
variables$covariates <- cov |
206 | 40x |
if (var_main) control$interaction <- TRUE |
207 |
}
|
|
208 | ||
209 | 191x |
if (is.null(cache_env[[cov]])) { |
210 | 30x |
if (!multivar) { |
211 | 23x |
model <- fit_coxreg_univar(variables = variables, data = df, at = at, control = control) %>% broom::tidy() |
212 |
} else { |
|
213 | 7x |
model <- fit_coxreg_multivar(variables = variables, data = df, control = control) %>% broom::tidy() |
214 |
}
|
|
215 | 30x |
cache_env[[cov]] <- model |
216 |
} else { |
|
217 | 161x |
model <- cache_env[[cov]] |
218 |
}
|
|
219 | 109x |
if (!multivar && !var_main) model[, "pval_inter"] <- NA_real_ |
220 | ||
221 | 191x |
if (cov_no_arm || (!cov_no_arm && !"arm" %in% names(variables) && is.numeric(df[[cov]]))) { |
222 | 15x |
multivar <- TRUE |
223 | 3x |
if (!cov_no_arm) var_main <- TRUE |
224 |
}
|
|
225 | ||
226 | 191x |
vars_coxreg <- list(which_vars = "all", var_nms = NULL) |
227 | 191x |
if (eff) { |
228 | 40x |
if (multivar && !var_main) { # multivar treatment level |
229 | 6x |
var_lbl_arm <- formatters::var_labels(df)[[variables$arm]] |
230 | 6x |
vars_coxreg[c("var_nms", "which_vars")] <- list(c(variables$arm, var_lbl_arm), "multi_lvl") |
231 |
} else { # treatment effect |
|
232 | 34x |
vars_coxreg["var_nms"] <- variables$arm |
233 | 6x |
if (var_main) vars_coxreg["which_vars"] <- "var_main" |
234 |
}
|
|
235 |
} else { |
|
236 | 151x |
if (!multivar || (multivar && var_main && !is.numeric(df[[cov]]))) { # covariate effect/level |
237 | 118x |
vars_coxreg[c("var_nms", "which_vars")] <- list(cov, "var_main") |
238 | 33x |
} else if (multivar) { # multivar covariate level |
239 | 33x |
vars_coxreg[c("var_nms", "which_vars")] <- list(c(cov, var_lbl), "multi_lvl") |
240 | 6x |
if (var_main) model[cov, .stats] <- NA_real_ |
241 |
}
|
|
242 | 40x |
if (!multivar && !var_main && control$interaction) vars_coxreg["which_vars"] <- "inter" # interaction effect |
243 |
}
|
|
244 | 191x |
var_vals <- s_coxreg(model, .stats, .which_vars = vars_coxreg$which_vars, .var_nms = vars_coxreg$var_nms)[[1]] |
245 | 191x |
var_names <- if (all(grepl("\\(reference = ", names(var_vals))) && labelstr != tail(.spl_context$value, 1)) { |
246 | 21x |
paste(c(labelstr, tail(strsplit(names(var_vals), " ")[[1]], 3)), collapse = " ") # "reference" main effect labels |
247 | 191x |
} else if ((!multivar && !eff && !(!var_main && control$interaction) && nchar(labelstr) > 0) || |
248 | 191x |
(multivar && var_main && is.numeric(df[[cov]]))) { |
249 | 47x |
labelstr # other main effect labels |
250 | 191x |
} else if (multivar && !eff && !var_main && is.numeric(df[[cov]])) { |
251 | 6x |
"All" # multivar numeric covariate |
252 |
} else { |
|
253 | 117x |
names(var_vals) |
254 |
}
|
|
255 | 191x |
in_rows( |
256 | 191x |
.list = var_vals, .names = var_names, .labels = var_names, .indent_mods = .indent_mods, |
257 | 191x |
.formats = stats::setNames(rep(.formats, length(var_names)), var_names), |
258 | 191x |
.format_na_strs = stats::setNames(rep(na_level, length(var_names)), var_names) |
259 |
)
|
|
260 |
}
|
|
261 | ||
262 |
#' @describeIn cox_regression Layout-creating function which creates a Cox regression summary table
|
|
263 |
#' layout. This function is a wrapper for several `rtables` layouting functions. This function
|
|
264 |
#' is a wrapper for [rtables::analyze_colvars()] and [rtables::summarize_row_groups()].
|
|
265 |
#'
|
|
266 |
#' @inheritParams fit_coxreg_univar
|
|
267 |
#' @param multivar (`flag`)\cr Defaults to `FALSE`. If `TRUE` multivariate Cox regression will run, otherwise
|
|
268 |
#' univariate Cox regression will run.
|
|
269 |
#' @param common_var (`character`)\cr the name of a factor variable in the dataset which takes the same value
|
|
270 |
#' for all rows. This should be created during pre-processing if no such variable currently exists.
|
|
271 |
#' @param .section_div (`character`)\cr string which should be repeated as a section divider between sections.
|
|
272 |
#' Defaults to `NA` for no section divider. If a vector of two strings are given, the first will be used between
|
|
273 |
#' treatment and covariate sections and the second between different covariates.
|
|
274 |
#'
|
|
275 |
#' @return
|
|
276 |
#' * `summarize_coxreg()` returns a layout object suitable for passing to further layouting functions,
|
|
277 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add a Cox regression table
|
|
278 |
#' containing the chosen statistics to the table layout.
|
|
279 |
#'
|
|
280 |
#' @seealso [fit_coxreg_univar()] and [fit_coxreg_multivar()] which also take the `variables`, `data`,
|
|
281 |
#' `at` (univariate only), and `control` arguments but return unformatted univariate and multivariate
|
|
282 |
#' Cox regression models, respectively.
|
|
283 |
#'
|
|
284 |
#' @examples
|
|
285 |
#' # summarize_coxreg
|
|
286 |
#'
|
|
287 |
#' result_univar <- basic_table() %>%
|
|
288 |
#' summarize_coxreg(variables = u1_variables) %>%
|
|
289 |
#' build_table(dta_bladder)
|
|
290 |
#' result_univar
|
|
291 |
#'
|
|
292 |
#' result_multivar <- basic_table() %>%
|
|
293 |
#' summarize_coxreg(
|
|
294 |
#' variables = m1_variables,
|
|
295 |
#' multivar = TRUE,
|
|
296 |
#' ) %>%
|
|
297 |
#' build_table(dta_bladder)
|
|
298 |
#' result_multivar
|
|
299 |
#'
|
|
300 |
#' result_univar_covs <- basic_table() %>%
|
|
301 |
#' summarize_coxreg(
|
|
302 |
#' variables = u2_variables,
|
|
303 |
#' ) %>%
|
|
304 |
#' build_table(dta_bladder)
|
|
305 |
#' result_univar_covs
|
|
306 |
#'
|
|
307 |
#' result_multivar_covs <- basic_table() %>%
|
|
308 |
#' summarize_coxreg(
|
|
309 |
#' variables = m2_variables,
|
|
310 |
#' multivar = TRUE,
|
|
311 |
#' varlabels = c("Covariate 1", "Covariate 2") # custom labels
|
|
312 |
#' ) %>%
|
|
313 |
#' build_table(dta_bladder)
|
|
314 |
#' result_multivar_covs
|
|
315 |
#'
|
|
316 |
#' @export
|
|
317 |
summarize_coxreg <- function(lyt, |
|
318 |
variables,
|
|
319 |
control = control_coxreg(), |
|
320 |
at = list(), |
|
321 |
multivar = FALSE, |
|
322 |
common_var = "STUDYID", |
|
323 |
.stats = c("n", "hr", "ci", "pval", "pval_inter"), |
|
324 |
.formats = c( |
|
325 |
n = "xx", hr = "xx.xx", ci = "(xx.xx, xx.xx)", |
|
326 |
pval = "x.xxxx | (<0.0001)", pval_inter = "x.xxxx | (<0.0001)" |
|
327 |
),
|
|
328 |
varlabels = NULL, |
|
329 |
.indent_mods = NULL, |
|
330 |
na_level = "", |
|
331 |
.section_div = NA_character_) { |
|
332 | 11x |
if (multivar && control$interaction) { |
333 | 1x |
warning(paste( |
334 | 1x |
"Interactions are not available for multivariate cox regression using summarize_coxreg.",
|
335 | 1x |
"The model will be calculated without interaction effects."
|
336 |
)) |
|
337 |
}
|
|
338 | 11x |
if (control$interaction && !"arm" %in% names(variables)) { |
339 | 1x |
stop("To include interactions please specify 'arm' in variables.") |
340 |
}
|
|
341 | ||
342 | 10x |
.stats <- if (!"arm" %in% names(variables) || multivar) { # only valid statistics |
343 | 4x |
intersect(c("hr", "ci", "pval"), .stats) |
344 | 10x |
} else if (control$interaction) { |
345 | 4x |
intersect(c("n", "hr", "ci", "pval", "pval_inter"), .stats) |
346 |
} else { |
|
347 | 2x |
intersect(c("n", "hr", "ci", "pval"), .stats) |
348 |
}
|
|
349 | 10x |
stat_labels <- c( |
350 | 10x |
n = "n", hr = "Hazard Ratio", ci = paste0(control$conf_level * 100, "% CI"), |
351 | 10x |
pval = "p-value", pval_inter = "Interaction p-value" |
352 |
)
|
|
353 | 10x |
stat_labels <- stat_labels[names(stat_labels) %in% .stats] |
354 | 10x |
.formats <- .formats[names(.formats) %in% .stats] |
355 | 10x |
env <- new.env() # create caching environment |
356 | ||
357 | 10x |
lyt <- lyt %>% |
358 | 10x |
split_cols_by_multivar( |
359 | 10x |
vars = rep(common_var, length(.stats)), |
360 | 10x |
varlabels = stat_labels, |
361 | 10x |
extra_args = list( |
362 | 10x |
.stats = .stats, .formats = .formats, .indent_mods = .indent_mods, na_level = rep(na_level, length(.stats)), |
363 | 10x |
cache_env = replicate(length(.stats), list(env)) |
364 |
)
|
|
365 |
)
|
|
366 | ||
367 | 10x |
if ("arm" %in% names(variables)) { # treatment effect |
368 | 8x |
lyt <- lyt %>% |
369 | 8x |
split_rows_by( |
370 | 8x |
common_var,
|
371 | 8x |
split_label = "Treatment:", |
372 | 8x |
label_pos = "visible", |
373 | 8x |
section_div = head(.section_div, 1) |
374 |
) %>% |
|
375 | 8x |
summarize_row_groups( |
376 | 8x |
cfun = a_coxreg, |
377 | 8x |
extra_args = list( |
378 | 8x |
variables = variables, control = control, multivar = multivar, eff = TRUE, var_main = multivar |
379 |
)
|
|
380 |
)
|
|
381 | 8x |
if (multivar) { # treatment level effects |
382 | 2x |
lyt <- lyt %>% |
383 | 2x |
analyze_colvars( |
384 | 2x |
afun = a_coxreg, |
385 | 2x |
extra_args = list(eff = TRUE, control = control, variables = variables, multivar = multivar, labelstr = "") |
386 |
)
|
|
387 |
}
|
|
388 |
}
|
|
389 | ||
390 | 10x |
if ("covariates" %in% names(variables)) { # covariate main effects |
391 | 10x |
lyt <- lyt %>% |
392 | 10x |
split_rows_by_multivar( |
393 | 10x |
vars = variables$covariates, |
394 | 10x |
varlabels = varlabels, |
395 | 10x |
split_label = "Covariate:", |
396 | 10x |
nested = FALSE, |
397 | 10x |
child_labels = if (multivar || control$interaction || !"arm" %in% names(variables)) "default" else "hidden", |
398 | 10x |
section_div = tail(.section_div, 1) |
399 |
)
|
|
400 | 10x |
if (multivar || control$interaction || !"arm" %in% names(variables)) { |
401 | 8x |
lyt <- lyt %>% |
402 | 8x |
summarize_row_groups( |
403 | 8x |
cfun = a_coxreg, |
404 | 8x |
extra_args = list( |
405 | 8x |
variables = variables, at = at, control = control, multivar = multivar, |
406 | 8x |
var_main = if (multivar) multivar else control$interaction |
407 |
)
|
|
408 |
)
|
|
409 |
} else { |
|
410 | ! |
if (!is.null(varlabels)) names(varlabels) <- variables$covariates |
411 | 2x |
lyt <- lyt %>% |
412 | 2x |
analyze_colvars( |
413 | 2x |
afun = a_coxreg, |
414 | 2x |
extra_args = list( |
415 | 2x |
variables = variables, at = at, control = control, multivar = multivar, |
416 | 2x |
var_main = if (multivar) multivar else control$interaction, |
417 | 2x |
labelstr = if (is.null(varlabels)) "" else varlabels |
418 |
)
|
|
419 |
)
|
|
420 |
}
|
|
421 | ||
422 | 2x |
if (!"arm" %in% names(variables)) control$interaction <- TRUE # special case: univar no arm |
423 | 10x |
if (multivar || control$interaction) { # covariate level effects |
424 | 8x |
lyt <- lyt %>% |
425 | 8x |
analyze_colvars( |
426 | 8x |
afun = a_coxreg, |
427 | 8x |
extra_args = list(variables = variables, at = at, control = control, multivar = multivar, labelstr = "") |
428 |
)
|
|
429 |
}
|
|
430 |
}
|
|
431 | ||
432 | 10x |
lyt
|
433 |
}
|
1 |
#' Kaplan-Meier Plot
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' From a survival model, a graphic is rendered along with tabulated annotation
|
|
6 |
#' including the number of patient at risk at given time and the median survival
|
|
7 |
#' per group.
|
|
8 |
#'
|
|
9 |
#' @inheritParams grid::gTree
|
|
10 |
#' @inheritParams argument_convention
|
|
11 |
#' @param df (`data.frame`)\cr data set containing all analysis variables.
|
|
12 |
#' @param variables (named `list`)\cr variable names. Details are:
|
|
13 |
#' * `tte` (`numeric`)\cr variable indicating time-to-event duration values.
|
|
14 |
#' * `is_event` (`logical`)\cr event variable. `TRUE` if event, `FALSE` if time to event is censored.
|
|
15 |
#' * `arm` (`factor`)\cr the treatment group variable.
|
|
16 |
#' * `strat` (`character` or `NULL`)\cr variable names indicating stratification factors.
|
|
17 |
#' @param control_surv (`list`)\cr parameters for comparison details, specified by using
|
|
18 |
#' the helper function [control_surv_timepoint()]. Some possible parameter options are:
|
|
19 |
#' * `conf_level` (`proportion`)\cr confidence level of the interval for survival rate.
|
|
20 |
#' * `conf_type` (`string`)\cr `"plain"` (default), `"log"`, `"log-log"` for confidence interval type,
|
|
21 |
#' see more in [survival::survfit()]. Note that the option "none" is no longer supported.
|
|
22 |
#' @param xticks (`numeric`, `number`, or `NULL`)\cr numeric vector of ticks or single number with spacing
|
|
23 |
#' between ticks on the x axis. If `NULL` (default), [labeling::extended()] is used to determine
|
|
24 |
#' an optimal tick position on the x axis.
|
|
25 |
#' @param yval (`string`)\cr value of y-axis. Options are `Survival` (default) and `Failure` probability.
|
|
26 |
#' @param censor_show (`flag`)\cr whether to show censored.
|
|
27 |
#' @param xlab (`string`)\cr label of x-axis.
|
|
28 |
#' @param ylab (`string`)\cr label of y-axis.
|
|
29 |
#' @param title (`string`)\cr title for plot.
|
|
30 |
#' @param footnotes (`string`)\cr footnotes for plot.
|
|
31 |
#' @param col (`character`)\cr lines colors. Length of a vector should be equal
|
|
32 |
#' to number of strata from [survival::survfit()].
|
|
33 |
#' @param lty (`numeric`)\cr line type. Length of a vector should be equal
|
|
34 |
#' to number of strata from [survival::survfit()].
|
|
35 |
#' @param lwd (`numeric`)\cr line width. Length of a vector should be equal
|
|
36 |
#' to number of strata from [survival::survfit()].
|
|
37 |
#' @param pch (`numeric`, `string`)\cr value or character of points symbol to indicate censored cases.
|
|
38 |
#' @param size (`numeric`)\cr size of censored point, a class of `unit`.
|
|
39 |
#' @param max_time (`numeric`)\cr maximum value to show on X axis. Only data values less than or up to
|
|
40 |
#' this threshold value will be plotted (defaults to `NULL`).
|
|
41 |
#' @param font_size (`number`)\cr font size to be used.
|
|
42 |
#' @param ci_ribbon (`flag`)\cr draw the confidence interval around the Kaplan-Meier curve.
|
|
43 |
#' @param ggtheme (`theme`)\cr a graphical theme as provided by `ggplot2` to control outlook of the Kaplan-Meier curve.
|
|
44 |
#' @param annot_at_risk (`flag`)\cr compute and add the annotation table reporting the number of patient at risk
|
|
45 |
#' matching the main grid of the Kaplan-Meier curve.
|
|
46 |
#' @param annot_surv_med (`flag`)\cr compute and add the annotation table on the Kaplan-Meier curve estimating the
|
|
47 |
#' median survival time per group.
|
|
48 |
#' @param annot_coxph (`flag`)\cr add the annotation table from a [survival::coxph()] model.
|
|
49 |
#' @param annot_stats (`string`)\cr statistics annotations to add to the plot. Options are
|
|
50 |
#' `median` (median survival follow-up time) and `min` (minimum survival follow-up time).
|
|
51 |
#' @param annot_stats_vlines (`flag`)\cr add vertical lines corresponding to each of the statistics
|
|
52 |
#' specified by `annot_stats`. If `annot_stats` is `NULL` no lines will be added.
|
|
53 |
#' @param control_coxph_pw (`list`)\cr parameters for comparison details, specified by using
|
|
54 |
#' the helper function [control_coxph()]. Some possible parameter options are:
|
|
55 |
#' * `pval_method` (`string`)\cr p-value method for testing hazard ratio = 1.
|
|
56 |
#' Default method is `"log-rank"`, can also be set to `"wald"` or `"likelihood"`.
|
|
57 |
#' * `ties` (`string`)\cr method for tie handling. Default is `"efron"`,
|
|
58 |
#' can also be set to `"breslow"` or `"exact"`. See more in [survival::coxph()]
|
|
59 |
#' * `conf_level` (`proportion`)\cr confidence level of the interval for HR.
|
|
60 |
#' @param position_coxph (`numeric`)\cr x and y positions for plotting [survival::coxph()] model.
|
|
61 |
#' @param position_surv_med (`numeric`)\cr x and y positions for plotting annotation table estimating median survival
|
|
62 |
#' time per group.
|
|
63 |
#' @param width_annots (named `list` of `unit`s)\cr a named list of widths for annotation tables with names `surv_med`
|
|
64 |
#' (median survival time table) and `coxph` ([survival::coxph()] model table), where each value is the width
|
|
65 |
#' (in units) to implement when printing the annotation table.
|
|
66 |
#'
|
|
67 |
#' @return A `grob` of class `gTree`.
|
|
68 |
#'
|
|
69 |
#' @examples
|
|
70 |
#' \donttest{
|
|
71 |
#' library(dplyr)
|
|
72 |
#' library(ggplot2)
|
|
73 |
#' library(survival)
|
|
74 |
#' library(grid)
|
|
75 |
#' library(nestcolor)
|
|
76 |
#'
|
|
77 |
#' df <- tern_ex_adtte %>%
|
|
78 |
#' filter(PARAMCD == "OS") %>%
|
|
79 |
#' mutate(is_event = CNSR == 0)
|
|
80 |
#' variables <- list(tte = "AVAL", is_event = "is_event", arm = "ARMCD")
|
|
81 |
#'
|
|
82 |
#' # 1. Example - basic option
|
|
83 |
#'
|
|
84 |
#' res <- g_km(df = df, variables = variables)
|
|
85 |
#' res <- g_km(df = df, variables = variables, yval = "Failure")
|
|
86 |
#' res <- g_km(
|
|
87 |
#' df = df,
|
|
88 |
#' variables = variables,
|
|
89 |
#' control_surv = control_surv_timepoint(conf_level = 0.9),
|
|
90 |
#' col = c("grey25", "grey50", "grey75")
|
|
91 |
#' )
|
|
92 |
#' res <- g_km(df = df, variables = variables, ggtheme = theme_minimal())
|
|
93 |
#' res <- g_km(df = df, variables = variables, ggtheme = theme_minimal(), lty = 1:3)
|
|
94 |
#' res <- g_km(df = df, variables = variables, max = 2000)
|
|
95 |
#' res <- g_km(
|
|
96 |
#' df = df,
|
|
97 |
#' variables = variables,
|
|
98 |
#' annot_stats = c("min", "median"),
|
|
99 |
#' annot_stats_vlines = TRUE
|
|
100 |
#' )
|
|
101 |
#'
|
|
102 |
#' # 2. Example - Arrange several KM curve on a single graph device
|
|
103 |
#'
|
|
104 |
#' # 2.1 Use case: A general graph on the top, a zoom on the bottom.
|
|
105 |
#' grid.newpage()
|
|
106 |
#' lyt <- grid.layout(nrow = 2, ncol = 1) %>%
|
|
107 |
#' viewport(layout = .) %>%
|
|
108 |
#' pushViewport()
|
|
109 |
#'
|
|
110 |
#' res <- g_km(
|
|
111 |
#' df = df, variables = variables, newpage = FALSE, annot_surv_med = FALSE,
|
|
112 |
#' vp = viewport(layout.pos.row = 1, layout.pos.col = 1)
|
|
113 |
#' )
|
|
114 |
#' res <- g_km(
|
|
115 |
#' df = df, variables = variables, max = 1000, newpage = FALSE, annot_surv_med = FALSE,
|
|
116 |
#' ggtheme = theme_dark(),
|
|
117 |
#' vp = viewport(layout.pos.row = 2, layout.pos.col = 1)
|
|
118 |
#' )
|
|
119 |
#'
|
|
120 |
#' # 2.1 Use case: No annotations on top, annotated graph on bottom
|
|
121 |
#' grid.newpage()
|
|
122 |
#' lyt <- grid.layout(nrow = 2, ncol = 1) %>%
|
|
123 |
#' viewport(layout = .) %>%
|
|
124 |
#' pushViewport()
|
|
125 |
#'
|
|
126 |
#' res <- g_km(
|
|
127 |
#' df = df, variables = variables, newpage = FALSE,
|
|
128 |
#' annot_surv_med = FALSE, annot_at_risk = FALSE,
|
|
129 |
#' vp = viewport(layout.pos.row = 1, layout.pos.col = 1)
|
|
130 |
#' )
|
|
131 |
#' res <- g_km(
|
|
132 |
#' df = df, variables = variables, max = 2000, newpage = FALSE, annot_surv_med = FALSE,
|
|
133 |
#' annot_at_risk = TRUE,
|
|
134 |
#' ggtheme = theme_dark(),
|
|
135 |
#' vp = viewport(layout.pos.row = 2, layout.pos.col = 1)
|
|
136 |
#' )
|
|
137 |
#'
|
|
138 |
#' # Add annotation from a pairwise coxph analysis
|
|
139 |
#' g_km(
|
|
140 |
#' df = df, variables = variables,
|
|
141 |
#' annot_coxph = TRUE
|
|
142 |
#' )
|
|
143 |
#'
|
|
144 |
#' # Change widths/sizes of surv_med and coxph annotation tables.
|
|
145 |
#' g_km(
|
|
146 |
#' df = df, variables = c(variables, list(strat = "SEX")),
|
|
147 |
#' annot_coxph = TRUE,
|
|
148 |
#' width_annots = list(surv_med = grid::unit(2, "in"), coxph = grid::unit(3, "in"))
|
|
149 |
#' )
|
|
150 |
#'
|
|
151 |
#' g_km(
|
|
152 |
#' df = df, variables = c(variables, list(strat = "SEX")),
|
|
153 |
#' font_size = 15,
|
|
154 |
#' annot_coxph = TRUE,
|
|
155 |
#' control_coxph = control_coxph(pval_method = "wald", ties = "exact", conf_level = 0.99),
|
|
156 |
#' position_coxph = c(0.5, 0.5)
|
|
157 |
#' )
|
|
158 |
#'
|
|
159 |
#' # Change position of the treatment group annotation table.
|
|
160 |
#' g_km(
|
|
161 |
#' df = df, variables = c(variables, list(strat = "SEX")),
|
|
162 |
#' font_size = 15,
|
|
163 |
#' annot_coxph = TRUE,
|
|
164 |
#' control_coxph = control_coxph(pval_method = "wald", ties = "exact", conf_level = 0.99),
|
|
165 |
#' position_surv_med = c(1, 0.7)
|
|
166 |
#' )
|
|
167 |
#' }
|
|
168 |
#'
|
|
169 |
#' @export
|
|
170 |
g_km <- function(df, |
|
171 |
variables,
|
|
172 |
control_surv = control_surv_timepoint(), |
|
173 |
col = NULL, |
|
174 |
lty = NULL, |
|
175 |
lwd = .5, |
|
176 |
censor_show = TRUE, |
|
177 |
pch = 3, |
|
178 |
size = 2, |
|
179 |
max_time = NULL, |
|
180 |
xticks = NULL, |
|
181 |
xlab = "Days", |
|
182 |
yval = c("Survival", "Failure"), |
|
183 |
ylab = paste(yval, "Probability"), |
|
184 |
title = NULL, |
|
185 |
footnotes = NULL, |
|
186 |
draw = TRUE, |
|
187 |
newpage = TRUE, |
|
188 |
gp = NULL, |
|
189 |
vp = NULL, |
|
190 |
name = NULL, |
|
191 |
font_size = 12, |
|
192 |
ci_ribbon = FALSE, |
|
193 |
ggtheme = nestcolor::theme_nest(), |
|
194 |
annot_at_risk = TRUE, |
|
195 |
annot_surv_med = TRUE, |
|
196 |
annot_coxph = FALSE, |
|
197 |
annot_stats = NULL, |
|
198 |
annot_stats_vlines = FALSE, |
|
199 |
control_coxph_pw = control_coxph(), |
|
200 |
position_coxph = c(-0.03, -0.02), |
|
201 |
position_surv_med = c(0.95, 0.9), |
|
202 |
width_annots = list(surv_med = grid::unit(0.3, "npc"), coxph = grid::unit(0.4, "npc"))) { |
|
203 | 6x |
checkmate::assert_list(variables) |
204 | 6x |
checkmate::assert_subset(c("tte", "arm", "is_event"), names(variables)) |
205 | 6x |
checkmate::assert_string(title, null.ok = TRUE) |
206 | 6x |
checkmate::assert_string(footnotes, null.ok = TRUE) |
207 | 6x |
checkmate::assert_character(col, null.ok = TRUE) |
208 | 6x |
checkmate::assert_subset(annot_stats, c("median", "min")) |
209 | 6x |
checkmate::assert_logical(annot_stats_vlines) |
210 | 6x |
checkmate::assert_true(all(sapply(width_annots, grid::is.unit))) |
211 | ||
212 | 6x |
tte <- variables$tte |
213 | 6x |
is_event <- variables$is_event |
214 | 6x |
arm <- variables$arm |
215 | ||
216 | 6x |
assert_valid_factor(df[[arm]]) |
217 | 6x |
assert_df_with_variables(df, list(tte = tte, is_event = is_event, arm = arm)) |
218 | 6x |
checkmate::assert_logical(df[[is_event]], min.len = 1, any.missing = FALSE) |
219 | 6x |
checkmate::assert_numeric(df[[tte]], min.len = 1, any.missing = FALSE) |
220 | ||
221 | 6x |
armval <- as.character(unique(df[[arm]])) |
222 | 6x |
if (length(armval) > 1) { |
223 | 6x |
armval <- NULL |
224 |
}
|
|
225 | 6x |
yval <- match.arg(yval) |
226 | 6x |
formula <- stats::as.formula(paste0("survival::Surv(", tte, ", ", is_event, ") ~ ", arm)) |
227 | 6x |
fit_km <- survival::survfit( |
228 | 6x |
formula = formula, |
229 | 6x |
data = df, |
230 | 6x |
conf.int = control_surv$conf_level, |
231 | 6x |
conf.type = control_surv$conf_type |
232 |
)
|
|
233 | 6x |
data_plot <- h_data_plot( |
234 | 6x |
fit_km = fit_km, |
235 | 6x |
armval = armval, |
236 | 6x |
max_time = max_time |
237 |
)
|
|
238 | ||
239 | 6x |
xticks <- h_xticks(data = data_plot, xticks = xticks, max_time = max_time) |
240 | 6x |
gg <- h_ggkm( |
241 | 6x |
data = data_plot, |
242 | 6x |
censor_show = censor_show, |
243 | 6x |
pch = pch, |
244 | 6x |
size = size, |
245 | 6x |
xticks = xticks, |
246 | 6x |
xlab = xlab, |
247 | 6x |
yval = yval, |
248 | 6x |
ylab = ylab, |
249 | 6x |
title = title, |
250 | 6x |
footnotes = footnotes, |
251 | 6x |
max_time = max_time, |
252 | 6x |
lwd = lwd, |
253 | 6x |
lty = lty, |
254 | 6x |
col = col, |
255 | 6x |
ggtheme = ggtheme, |
256 | 6x |
ci_ribbon = ci_ribbon |
257 |
)
|
|
258 | ||
259 | 6x |
if (!is.null(annot_stats)) { |
260 | ! |
if ("median" %in% annot_stats) { |
261 | ! |
fit_km_all <- survival::survfit( |
262 | ! |
formula = stats::as.formula(paste0("survival::Surv(", tte, ", ", is_event, ") ~ ", 1)), |
263 | ! |
data = df, |
264 | ! |
conf.int = control_surv$conf_level, |
265 | ! |
conf.type = control_surv$conf_type |
266 |
)
|
|
267 | ! |
gg <- gg + |
268 | ! |
geom_text( |
269 | ! |
size = 8 / ggplot2::.pt, col = 1, |
270 | ! |
x = stats::median(fit_km_all) + 0.065 * max(data_plot$time), |
271 | ! |
y = ifelse(yval == "Survival", 0.62, 0.38), |
272 | ! |
label = paste("Median F/U:\n", round(stats::median(fit_km_all), 1), tolower(df$AVALU[1])) |
273 |
)
|
|
274 | ! |
if (annot_stats_vlines) { |
275 | ! |
gg <- gg + |
276 | ! |
geom_segment(aes(x = stats::median(fit_km_all), xend = stats::median(fit_km_all), y = -Inf, yend = Inf), |
277 | ! |
linetype = 2, col = "darkgray" |
278 |
)
|
|
279 |
}
|
|
280 |
}
|
|
281 | ! |
if ("min" %in% annot_stats) { |
282 | ! |
min_fu <- min(df[[tte]]) |
283 | ! |
gg <- gg + |
284 | ! |
geom_text( |
285 | ! |
size = 8 / ggplot2::.pt, col = 1, |
286 | ! |
x = min_fu + max(data_plot$time) * ifelse(yval == "Survival", 0.05, 0.07), |
287 | ! |
y = ifelse(yval == "Survival", 1.0, 0.05), |
288 | ! |
label = paste("Min. F/U:\n", round(min_fu, 1), tolower(df$AVALU[1])) |
289 |
)
|
|
290 | ! |
if (annot_stats_vlines) { |
291 | ! |
gg <- gg + |
292 | ! |
geom_segment(aes(x = min_fu, xend = min_fu, y = Inf, yend = -Inf), linetype = 2, col = "darkgray") |
293 |
}
|
|
294 |
}
|
|
295 | ! |
gg <- gg + ggplot2::guides(fill = ggplot2::guide_legend(override.aes = list(shape = NA, label = ""))) |
296 |
}
|
|
297 | ||
298 | 6x |
g_el <- h_decompose_gg(gg) |
299 | ||
300 | 6x |
if (annot_at_risk) { |
301 |
# This is the content of the table that will be below the graph.
|
|
302 | 5x |
annot_tbl <- summary(fit_km, time = xticks) |
303 | 5x |
annot_tbl <- if (is.null(fit_km$strata)) { |
304 | ! |
data.frame( |
305 | ! |
n.risk = annot_tbl$n.risk, |
306 | ! |
time = annot_tbl$time, |
307 | ! |
strata = as.factor(armval) |
308 |
)
|
|
309 |
} else { |
|
310 | 5x |
strata_lst <- strsplit(sub("=", "equals", levels(annot_tbl$strata)), "equals") |
311 | 5x |
levels(annot_tbl$strata) <- matrix(unlist(strata_lst), ncol = 2, byrow = TRUE)[, 2] |
312 | 5x |
data.frame( |
313 | 5x |
n.risk = annot_tbl$n.risk, |
314 | 5x |
time = annot_tbl$time, |
315 | 5x |
strata = annot_tbl$strata |
316 |
)
|
|
317 |
}
|
|
318 | ||
319 | 5x |
grobs_patient <- h_grob_tbl_at_risk( |
320 | 5x |
data = data_plot, |
321 | 5x |
annot_tbl = annot_tbl, |
322 | 5x |
xlim = max(max_time, data_plot$time, xticks) |
323 |
)
|
|
324 |
}
|
|
325 | ||
326 | 6x |
if (annot_at_risk || annot_surv_med || annot_coxph) { |
327 | 5x |
lyt <- h_km_layout( |
328 | 5x |
data = data_plot, g_el = g_el, title = title, footnotes = footnotes, annot_at_risk = annot_at_risk |
329 |
)
|
|
330 | 5x |
ttl_row <- as.numeric(!is.null(title)) |
331 | 5x |
foot_row <- as.numeric(!is.null(footnotes)) |
332 | 5x |
km_grob <- grid::gTree( |
333 | 5x |
vp = grid::viewport(layout = lyt, height = .95, width = .95), |
334 | 5x |
children = grid::gList( |
335 |
# Title.
|
|
336 | 5x |
if (ttl_row == 1) { |
337 | 1x |
grid::gTree( |
338 | 1x |
vp = grid::viewport(layout.pos.row = 1, layout.pos.col = 2), |
339 | 1x |
children = grid::gList(grid::textGrob(label = title, x = grid::unit(0, "npc"), hjust = 0)) |
340 |
)
|
|
341 |
},
|
|
342 | ||
343 |
# The Kaplan - Meier curve (top-right corner).
|
|
344 | 5x |
grid::gTree( |
345 | 5x |
vp = grid::viewport(layout.pos.row = 1 + ttl_row, layout.pos.col = 2), |
346 | 5x |
children = grid::gList(g_el$panel) |
347 |
),
|
|
348 | ||
349 |
# Survfit summary table (top-right corner).
|
|
350 | 5x |
if (annot_surv_med) { |
351 | 4x |
grid::gTree( |
352 | 4x |
vp = grid::viewport(layout.pos.row = 1 + ttl_row, layout.pos.col = 2), |
353 | 4x |
children = h_grob_median_surv( |
354 | 4x |
fit_km = fit_km, |
355 | 4x |
armval = armval, |
356 | 4x |
x = position_surv_med[1], |
357 | 4x |
y = position_surv_med[2], |
358 | 4x |
width = if (!is.null(width_annots[["surv_med"]])) width_annots[["surv_med"]] else grid::unit(0.3, "npc"), |
359 | 4x |
ttheme = gridExtra::ttheme_default(base_size = font_size) |
360 |
)
|
|
361 |
)
|
|
362 |
},
|
|
363 | 5x |
if (annot_coxph) { |
364 | 1x |
grid::gTree( |
365 | 1x |
vp = grid::viewport(layout.pos.row = 1 + ttl_row, layout.pos.col = 2), |
366 | 1x |
children = h_grob_coxph( |
367 | 1x |
df = df, |
368 | 1x |
variables = variables, |
369 | 1x |
control_coxph_pw = control_coxph_pw, |
370 | 1x |
x = position_coxph[1], |
371 | 1x |
y = position_coxph[2], |
372 | 1x |
width = if (!is.null(width_annots[["coxph"]])) width_annots[["coxph"]] else grid::unit(0.4, "npc"), |
373 | 1x |
ttheme = gridExtra::ttheme_default( |
374 | 1x |
base_size = font_size, |
375 | 1x |
padding = grid::unit(c(1, .5), "lines"), |
376 | 1x |
core = list(bg_params = list(fill = c("grey95", "grey90"), alpha = .5)) |
377 |
)
|
|
378 |
)
|
|
379 |
)
|
|
380 |
},
|
|
381 | ||
382 |
# Add the y-axis annotation (top-left corner).
|
|
383 | 5x |
grid::gTree( |
384 | 5x |
vp = grid::viewport(layout.pos.row = 1 + ttl_row, layout.pos.col = 1), |
385 | 5x |
children = h_grob_y_annot(ylab = g_el$ylab, yaxis = g_el$yaxis) |
386 |
),
|
|
387 | ||
388 |
# Add the x-axis annotation (second row below the Kaplan Meier Curve).
|
|
389 | 5x |
grid::gTree( |
390 | 5x |
vp = grid::viewport(layout.pos.row = 2 + ttl_row, layout.pos.col = 2), |
391 | 5x |
children = grid::gList(rbind(g_el$xaxis, g_el$xlab)) |
392 |
),
|
|
393 | ||
394 |
# Add the legend.
|
|
395 | 5x |
grid::gTree( |
396 | 5x |
vp = grid::viewport(layout.pos.row = 3 + ttl_row, layout.pos.col = 2), |
397 | 5x |
children = grid::gList(g_el$guide) |
398 |
),
|
|
399 | ||
400 |
# Add the table with patient-at-risk numbers.
|
|
401 | 5x |
if (annot_at_risk) { |
402 | 5x |
grid::gTree( |
403 | 5x |
vp = grid::viewport(layout.pos.row = 4 + ttl_row, layout.pos.col = 2), |
404 | 5x |
children = grobs_patient$at_risk |
405 |
)
|
|
406 |
},
|
|
407 | 5x |
if (annot_at_risk) { |
408 | 5x |
grid::gTree( |
409 | 5x |
vp = grid::viewport(layout.pos.row = 4 + ttl_row, layout.pos.col = 1), |
410 | 5x |
children = grobs_patient$label |
411 |
)
|
|
412 |
},
|
|
413 | 5x |
if (annot_at_risk) { |
414 |
# Add the x-axis for the table.
|
|
415 | 5x |
grid::gTree( |
416 | 5x |
vp = grid::viewport(layout.pos.row = 5 + ttl_row, layout.pos.col = 2), |
417 | 5x |
children = grid::gList(rbind(g_el$xaxis, g_el$xlab)) |
418 |
)
|
|
419 |
},
|
|
420 | ||
421 |
# Footnotes.
|
|
422 | 5x |
if (foot_row == 1) { |
423 | 1x |
grid::gTree( |
424 | 1x |
vp = grid::viewport( |
425 | 1x |
layout.pos.row = ifelse(annot_at_risk, 6 + ttl_row, 4 + ttl_row), |
426 | 1x |
layout.pos.col = 2 |
427 |
),
|
|
428 | 1x |
children = grid::gList(grid::textGrob(label = footnotes, x = grid::unit(0, "npc"), hjust = 0)) |
429 |
)
|
|
430 |
}
|
|
431 |
)
|
|
432 |
)
|
|
433 | ||
434 | 5x |
result <- grid::gTree( |
435 | 5x |
vp = vp, |
436 | 5x |
gp = gp, |
437 | 5x |
name = name, |
438 | 5x |
children = grid::gList(km_grob) |
439 |
)
|
|
440 |
} else { |
|
441 | 1x |
result <- grid::gTree( |
442 | 1x |
vp = vp, |
443 | 1x |
gp = gp, |
444 | 1x |
name = name, |
445 | 1x |
children = grid::gList(ggplot2::ggplotGrob(gg)) |
446 |
)
|
|
447 |
}
|
|
448 | ||
449 | 3x |
if (newpage && draw) grid::grid.newpage() |
450 | 3x |
if (draw) grid::grid.draw(result) |
451 | 6x |
invisible(result) |
452 |
}
|
|
453 | ||
454 |
#' Helper function: tidy survival fit
|
|
455 |
#'
|
|
456 |
#' @description `r lifecycle::badge("stable")`
|
|
457 |
#'
|
|
458 |
#' Convert the survival fit data into a data frame designed for plotting
|
|
459 |
#' within `g_km`.
|
|
460 |
#'
|
|
461 |
#' This starts from the [broom::tidy()] result, and then:
|
|
462 |
#' * Post-processes the `strata` column into a factor.
|
|
463 |
#' * Extends each stratum by an additional first row with time 0 and probability 1 so that
|
|
464 |
#' downstream plot lines start at those coordinates.
|
|
465 |
#' * Adds a `censor` column.
|
|
466 |
#' * Filters the rows before `max_time`.
|
|
467 |
#'
|
|
468 |
#' @inheritParams g_km
|
|
469 |
#' @param fit_km (`survfit`)\cr result of [survival::survfit()].
|
|
470 |
#' @param armval (`string`)\cr used as strata name when treatment arm variable only has one level. Default is `"All"`.
|
|
471 |
#'
|
|
472 |
#' @return A `tibble` with columns `time`, `n.risk`, `n.event`, `n.censor`, `estimate`, `std.error`, `conf.high`,
|
|
473 |
#' `conf.low`, `strata`, and `censor`.
|
|
474 |
#'
|
|
475 |
#' @examples
|
|
476 |
#' \donttest{
|
|
477 |
#' library(dplyr)
|
|
478 |
#' library(survival)
|
|
479 |
#'
|
|
480 |
#' # Test with multiple arms
|
|
481 |
#' tern_ex_adtte %>%
|
|
482 |
#' filter(PARAMCD == "OS") %>%
|
|
483 |
#' survfit(form = Surv(AVAL, 1 - CNSR) ~ ARMCD, data = .) %>%
|
|
484 |
#' h_data_plot()
|
|
485 |
#'
|
|
486 |
#' # Test with single arm
|
|
487 |
#' tern_ex_adtte %>%
|
|
488 |
#' filter(PARAMCD == "OS", ARMCD == "ARM B") %>%
|
|
489 |
#' survfit(form = Surv(AVAL, 1 - CNSR) ~ ARMCD, data = .) %>%
|
|
490 |
#' h_data_plot(armval = "ARM B")
|
|
491 |
#' }
|
|
492 |
#'
|
|
493 |
#' @export
|
|
494 |
h_data_plot <- function(fit_km, |
|
495 |
armval = "All", |
|
496 |
max_time = NULL) { |
|
497 | 13x |
y <- broom::tidy(fit_km) |
498 | ||
499 | 13x |
if (!is.null(fit_km$strata)) { |
500 | 13x |
fit_km_var_level <- strsplit(sub("=", "equals", names(fit_km$strata)), "equals") |
501 | 13x |
strata_levels <- vapply(fit_km_var_level, FUN = "[", FUN.VALUE = "a", i = 2) |
502 | 13x |
strata_var_level <- strsplit(sub("=", "equals", y$strata), "equals") |
503 | 13x |
y$strata <- factor( |
504 | 13x |
vapply(strata_var_level, FUN = "[", FUN.VALUE = "a", i = 2), |
505 | 13x |
levels = strata_levels |
506 |
)
|
|
507 |
} else { |
|
508 | ! |
y$strata <- armval |
509 |
}
|
|
510 | ||
511 | 13x |
y_by_strata <- split(y, y$strata) |
512 | 13x |
y_by_strata_extended <- lapply( |
513 | 13x |
y_by_strata,
|
514 | 13x |
FUN = function(tbl) { |
515 | 38x |
first_row <- tbl[1L, ] |
516 | 38x |
first_row$time <- 0 |
517 | 38x |
first_row$n.risk <- sum(first_row[, c("n.risk", "n.event", "n.censor")]) |
518 | 38x |
first_row$n.event <- first_row$n.censor <- 0 |
519 | 38x |
first_row$estimate <- first_row$conf.high <- first_row$conf.low <- 1 |
520 | 38x |
first_row$std.error <- 0 |
521 | 38x |
rbind( |
522 | 38x |
first_row,
|
523 | 38x |
tbl
|
524 |
)
|
|
525 |
}
|
|
526 |
)
|
|
527 | 13x |
y <- do.call(rbind, y_by_strata_extended) |
528 | ||
529 | 13x |
y$censor <- ifelse(y$n.censor > 0, y$estimate, NA) |
530 | 13x |
if (!is.null(max_time)) { |
531 | 2x |
y <- y[y$time <= max(max_time), ] |
532 |
}
|
|
533 | 13x |
y
|
534 |
}
|
|
535 | ||
536 |
#' Helper function: x tick positions
|
|
537 |
#'
|
|
538 |
#' @description `r lifecycle::badge("stable")`
|
|
539 |
#'
|
|
540 |
#' Calculate the positions of ticks on the x-axis. However, if `xticks` already
|
|
541 |
#' exists it is kept as is. It is based on the same function `ggplot2` relies on,
|
|
542 |
#' and is required in the graphic and the patient-at-risk annotation table.
|
|
543 |
#'
|
|
544 |
#' @inheritParams g_km
|
|
545 |
#' @inheritParams h_ggkm
|
|
546 |
#'
|
|
547 |
#' @return A vector of positions to use for x-axis ticks on a `ggplot` object.
|
|
548 |
#'
|
|
549 |
#' @examples
|
|
550 |
#' \donttest{
|
|
551 |
#' library(dplyr)
|
|
552 |
#' library(survival)
|
|
553 |
#'
|
|
554 |
#' data <- tern_ex_adtte %>%
|
|
555 |
#' filter(PARAMCD == "OS") %>%
|
|
556 |
#' survfit(form = Surv(AVAL, 1 - CNSR) ~ ARMCD, data = .) %>%
|
|
557 |
#' h_data_plot()
|
|
558 |
#'
|
|
559 |
#' h_xticks(data)
|
|
560 |
#' h_xticks(data, xticks = seq(0, 3000, 500))
|
|
561 |
#' h_xticks(data, xticks = 500)
|
|
562 |
#' h_xticks(data, xticks = 500, max_time = 6000)
|
|
563 |
#' h_xticks(data, xticks = c(0, 500), max_time = 300)
|
|
564 |
#' h_xticks(data, xticks = 500, max_time = 300)
|
|
565 |
#' }
|
|
566 |
#'
|
|
567 |
#' @export
|
|
568 |
h_xticks <- function(data, xticks = NULL, max_time = NULL) { |
|
569 | 13x |
if (is.null(xticks)) { |
570 | 7x |
if (is.null(max_time)) { |
571 | 6x |
labeling::extended(range(data$time)[1], range(data$time)[2], m = 5) |
572 |
} else { |
|
573 | 1x |
labeling::extended(range(data$time)[1], max(range(data$time)[2], max_time), m = 5) |
574 |
}
|
|
575 | 6x |
} else if (checkmate::test_number(xticks)) { |
576 | 3x |
if (is.null(max_time)) { |
577 | 2x |
seq(0, max(data$time), xticks) |
578 |
} else { |
|
579 | 1x |
seq(0, max(data$time, max_time), xticks) |
580 |
}
|
|
581 | 3x |
} else if (is.numeric(xticks)) { |
582 | 2x |
xticks
|
583 |
} else { |
|
584 | 1x |
stop( |
585 | 1x |
paste( |
586 | 1x |
"xticks should be either `NULL`",
|
587 | 1x |
"or a single number (interval between x ticks)",
|
588 | 1x |
"or a numeric vector (position of ticks on the x axis)"
|
589 |
)
|
|
590 |
)
|
|
591 |
}
|
|
592 |
}
|
|
593 | ||
594 |
#' Helper function: KM plot
|
|
595 |
#'
|
|
596 |
#' @description `r lifecycle::badge("stable")`
|
|
597 |
#'
|
|
598 |
#' Draw the Kaplan-Meier plot using `ggplot2`.
|
|
599 |
#'
|
|
600 |
#' @inheritParams g_km
|
|
601 |
#' @param data (`data.frame`)\cr survival data as pre-processed by `h_data_plot`.
|
|
602 |
#'
|
|
603 |
#' @return A `ggplot` object.
|
|
604 |
#'
|
|
605 |
#' @examples
|
|
606 |
#' \donttest{
|
|
607 |
#' library(dplyr)
|
|
608 |
#' library(survival)
|
|
609 |
#'
|
|
610 |
#' fit_km <- tern_ex_adtte %>%
|
|
611 |
#' filter(PARAMCD == "OS") %>%
|
|
612 |
#' survfit(form = Surv(AVAL, 1 - CNSR) ~ ARMCD, data = .)
|
|
613 |
#' data_plot <- h_data_plot(fit_km = fit_km)
|
|
614 |
#' xticks <- h_xticks(data = data_plot)
|
|
615 |
#' gg <- h_ggkm(
|
|
616 |
#' data = data_plot,
|
|
617 |
#' censor_show = TRUE,
|
|
618 |
#' xticks = xticks,
|
|
619 |
#' xlab = "Days",
|
|
620 |
#' yval = "Survival",
|
|
621 |
#' ylab = "Survival Probability",
|
|
622 |
#' title = "Survival"
|
|
623 |
#' )
|
|
624 |
#' gg
|
|
625 |
#' }
|
|
626 |
#'
|
|
627 |
#' @export
|
|
628 |
h_ggkm <- function(data, |
|
629 |
xticks = NULL, |
|
630 |
yval = "Survival", |
|
631 |
censor_show,
|
|
632 |
xlab,
|
|
633 |
ylab,
|
|
634 |
title,
|
|
635 |
footnotes = NULL, |
|
636 |
max_time = NULL, |
|
637 |
lwd = 1, |
|
638 |
lty = NULL, |
|
639 |
pch = 3, |
|
640 |
size = 2, |
|
641 |
col = NULL, |
|
642 |
ci_ribbon = FALSE, |
|
643 |
ggtheme = nestcolor::theme_nest()) { |
|
644 | 6x |
checkmate::assert_numeric(lty, null.ok = TRUE) |
645 | 6x |
checkmate::assert_character(col, null.ok = TRUE) |
646 | ||
647 |
# change estimates of survival to estimates of failure (1 - survival)
|
|
648 | 6x |
if (yval == "Failure") { |
649 | 1x |
data$estimate <- 1 - data$estimate |
650 | 1x |
data[c("conf.high", "conf.low")] <- list(1 - data$conf.low, 1 - data$conf.high) |
651 | 1x |
data$censor <- 1 - data$censor |
652 |
}
|
|
653 | ||
654 | 6x |
gg <- { |
655 | 6x |
ggplot2::ggplot( |
656 | 6x |
data = data, |
657 | 6x |
mapping = ggplot2::aes( |
658 | 6x |
x = .data[["time"]], |
659 | 6x |
y = .data[["estimate"]], |
660 | 6x |
ymin = .data[["conf.low"]], |
661 | 6x |
ymax = .data[["conf.high"]], |
662 | 6x |
color = .data[["strata"]], |
663 | 6x |
fill = .data[["strata"]] |
664 |
)
|
|
665 |
) + |
|
666 | 6x |
ggplot2::geom_hline(yintercept = 0) |
667 |
}
|
|
668 | ||
669 | 6x |
if (ci_ribbon) { |
670 | 1x |
gg <- gg + ggplot2::geom_ribbon(alpha = .3, lty = 0) |
671 |
}
|
|
672 | ||
673 | 6x |
gg <- if (is.null(lty)) { |
674 | 5x |
gg + |
675 | 5x |
ggplot2::geom_step(linewidth = lwd) |
676 | 6x |
} else if (checkmate::test_number(lty)) { |
677 | 1x |
gg + |
678 | 1x |
ggplot2::geom_step(linewidth = lwd, lty = lty) |
679 | 6x |
} else if (is.numeric(lty)) { |
680 | ! |
gg + |
681 | ! |
ggplot2::geom_step(mapping = ggplot2::aes(linetype = .data[["strata"]]), linewidth = lwd) + |
682 | ! |
ggplot2::scale_linetype_manual(values = lty) |
683 |
}
|
|
684 | ||
685 | 6x |
gg <- gg + |
686 | 6x |
ggplot2::coord_cartesian(ylim = c(0, 1)) + |
687 | 6x |
ggplot2::labs(x = xlab, y = ylab, title = title, caption = footnotes) |
688 | ||
689 | 6x |
if (!is.null(col)) { |
690 | ! |
gg <- gg + |
691 | ! |
ggplot2::scale_color_manual(values = col) + |
692 | ! |
ggplot2::scale_fill_manual(values = col) |
693 |
}
|
|
694 | 6x |
if (censor_show) { |
695 | 6x |
dt <- data[data$n.censor != 0, ] |
696 | 6x |
dt$censor_lbl <- factor("Censored") |
697 | ||
698 | 6x |
gg <- gg + ggplot2::geom_point( |
699 | 6x |
data = dt, |
700 | 6x |
ggplot2::aes( |
701 | 6x |
x = .data[["time"]], |
702 | 6x |
y = .data[["censor"]], |
703 | 6x |
shape = .data[["censor_lbl"]] |
704 |
),
|
|
705 | 6x |
size = size, |
706 | 6x |
show.legend = TRUE, |
707 | 6x |
inherit.aes = TRUE |
708 |
) + |
|
709 | 6x |
ggplot2::scale_shape_manual(name = NULL, values = pch) + |
710 | 6x |
ggplot2::guides( |
711 | 6x |
shape = ggplot2::guide_legend(override.aes = list(linetype = NA)), |
712 | 6x |
fill = ggplot2::guide_legend(override.aes = list(shape = NA)) |
713 |
)
|
|
714 |
}
|
|
715 | ||
716 | 6x |
if (!is.null(max_time) && !is.null(xticks)) { |
717 | ! |
gg <- gg + ggplot2::scale_x_continuous(breaks = xticks, limits = c(min(0, xticks), max(c(xticks, max_time)))) |
718 | 6x |
} else if (!is.null(xticks)) { |
719 | 6x |
if (max(data$time) <= max(xticks)) { |
720 | 5x |
gg <- gg + ggplot2::scale_x_continuous(breaks = xticks, limits = c(min(0, min(xticks)), max(xticks))) |
721 |
} else { |
|
722 | 1x |
gg <- gg + ggplot2::scale_x_continuous(breaks = xticks) |
723 |
}
|
|
724 | ! |
} else if (!is.null(max_time)) { |
725 | ! |
gg <- gg + ggplot2::scale_x_continuous(limits = c(0, max_time)) |
726 |
}
|
|
727 | ||
728 | 6x |
if (!is.null(ggtheme)) { |
729 | 6x |
gg <- gg + ggtheme |
730 |
}
|
|
731 | ||
732 | 6x |
gg + ggplot2::theme( |
733 | 6x |
legend.position = "bottom", |
734 | 6x |
legend.title = ggplot2::element_blank(), |
735 | 6x |
legend.key.height = unit(0.02, "npc"), |
736 | 6x |
panel.grid.major.x = ggplot2::element_line(linewidth = 2) |
737 |
)
|
|
738 |
}
|
|
739 | ||
740 |
#' `ggplot` Decomposition
|
|
741 |
#'
|
|
742 |
#' @description `r lifecycle::badge("stable")`
|
|
743 |
#'
|
|
744 |
#' The elements composing the `ggplot` are extracted and organized in a `list`.
|
|
745 |
#'
|
|
746 |
#' @param gg (`ggplot`)\cr a graphic to decompose.
|
|
747 |
#'
|
|
748 |
#' @return A named `list` with elements:
|
|
749 |
#' * `panel`: The panel.
|
|
750 |
#' * `yaxis`: The y-axis.
|
|
751 |
#' * `xaxis`: The x-axis.
|
|
752 |
#' * `xlab`: The x-axis label.
|
|
753 |
#' * `ylab`: The y-axis label.
|
|
754 |
#' * `guide`: The legend.
|
|
755 |
#'
|
|
756 |
#' @examples
|
|
757 |
#' \donttest{
|
|
758 |
#' library(dplyr)
|
|
759 |
#' library(survival)
|
|
760 |
#' library(grid)
|
|
761 |
#'
|
|
762 |
#' fit_km <- tern_ex_adtte %>%
|
|
763 |
#' filter(PARAMCD == "OS") %>%
|
|
764 |
#' survfit(form = Surv(AVAL, 1 - CNSR) ~ ARMCD, data = .)
|
|
765 |
#' data_plot <- h_data_plot(fit_km = fit_km)
|
|
766 |
#' xticks <- h_xticks(data = data_plot)
|
|
767 |
#' gg <- h_ggkm(
|
|
768 |
#' data = data_plot,
|
|
769 |
#' yval = "Survival",
|
|
770 |
#' censor_show = TRUE,
|
|
771 |
#' xticks = xticks, xlab = "Days", ylab = "Survival Probability",
|
|
772 |
#' title = "tt",
|
|
773 |
#' footnotes = "ff"
|
|
774 |
#' )
|
|
775 |
#'
|
|
776 |
#' g_el <- h_decompose_gg(gg)
|
|
777 |
#' grid::grid.newpage()
|
|
778 |
#' grid.rect(gp = grid::gpar(lty = 1, col = "red", fill = "gray85", lwd = 5))
|
|
779 |
#' grid::grid.draw(g_el$panel)
|
|
780 |
#'
|
|
781 |
#' grid::grid.newpage()
|
|
782 |
#' grid.rect(gp = grid::gpar(lty = 1, col = "royalblue", fill = "gray85", lwd = 5))
|
|
783 |
#' grid::grid.draw(with(g_el, cbind(ylab, yaxis)))
|
|
784 |
#' }
|
|
785 |
#'
|
|
786 |
#' @export
|
|
787 |
h_decompose_gg <- function(gg) { |
|
788 | 6x |
g_el <- ggplot2::ggplotGrob(gg) |
789 | 6x |
y <- c( |
790 | 6x |
panel = "panel", |
791 | 6x |
yaxis = "axis-l", |
792 | 6x |
xaxis = "axis-b", |
793 | 6x |
xlab = "xlab-b", |
794 | 6x |
ylab = "ylab-l", |
795 | 6x |
guide = "guide" |
796 |
)
|
|
797 | 6x |
lapply(X = y, function(x) gtable::gtable_filter(g_el, x)) |
798 |
}
|
|
799 | ||
800 |
#' Helper: KM Layout
|
|
801 |
#'
|
|
802 |
#' @description `r lifecycle::badge("stable")`
|
|
803 |
#'
|
|
804 |
#' Prepares a (5 rows) x (2 cols) layout for the Kaplan-Meier curve.
|
|
805 |
#'
|
|
806 |
#' @inheritParams g_km
|
|
807 |
#' @inheritParams h_ggkm
|
|
808 |
#' @param g_el (`list` of `gtable`)\cr list as obtained by `h_decompose_gg()`.
|
|
809 |
#' @param annot_at_risk (`flag`)\cr compute and add the annotation table reporting the number of
|
|
810 |
#' patient at risk matching the main grid of the Kaplan-Meier curve.
|
|
811 |
#'
|
|
812 |
#' @return A grid layout.
|
|
813 |
#'
|
|
814 |
#' @details The layout corresponds to a grid of two columns and five rows of unequal dimensions. Most of the
|
|
815 |
#' dimension are fixed, only the curve is flexible and will accommodate with the remaining free space.
|
|
816 |
#' * The left column gets the annotation of the `ggplot` (y-axis) and the names of the strata for the patient
|
|
817 |
#' at risk tabulation. The main constraint is about the width of the columns which must allow the writing of
|
|
818 |
#' the strata name.
|
|
819 |
#' * The right column receive the `ggplot`, the legend, the x-axis and the patient at risk table.
|
|
820 |
#'
|
|
821 |
#' @examples
|
|
822 |
#' \donttest{
|
|
823 |
#' library(dplyr)
|
|
824 |
#' library(survival)
|
|
825 |
#' library(grid)
|
|
826 |
#'
|
|
827 |
#' fit_km <- tern_ex_adtte %>%
|
|
828 |
#' filter(PARAMCD == "OS") %>%
|
|
829 |
#' survfit(form = Surv(AVAL, 1 - CNSR) ~ ARMCD, data = .)
|
|
830 |
#' data_plot <- h_data_plot(fit_km = fit_km)
|
|
831 |
#' xticks <- h_xticks(data = data_plot)
|
|
832 |
#' gg <- h_ggkm(
|
|
833 |
#' data = data_plot,
|
|
834 |
#' censor_show = TRUE,
|
|
835 |
#' xticks = xticks, xlab = "Days", ylab = "Survival Probability",
|
|
836 |
#' title = "tt", footnotes = "ff", yval = "Survival"
|
|
837 |
#' )
|
|
838 |
#' g_el <- h_decompose_gg(gg)
|
|
839 |
#' lyt <- h_km_layout(data = data_plot, g_el = g_el, title = "t", footnotes = "f")
|
|
840 |
#' grid.show.layout(lyt)
|
|
841 |
#' }
|
|
842 |
#'
|
|
843 |
#' @export
|
|
844 |
h_km_layout <- function(data, g_el, title, footnotes, annot_at_risk = TRUE) { |
|
845 | 5x |
txtlines <- levels(as.factor(data$strata)) |
846 | 5x |
nlines <- nlevels(as.factor(data$strata)) |
847 | 5x |
col_annot_width <- max( |
848 | 5x |
c( |
849 | 5x |
as.numeric(grid::convertX(g_el$yaxis$width + g_el$ylab$width, "pt")), |
850 | 5x |
as.numeric( |
851 | 5x |
grid::convertX( |
852 | 5x |
grid::stringWidth(txtlines) + grid::unit(7, "pt"), "pt" |
853 |
)
|
|
854 |
)
|
|
855 |
)
|
|
856 |
)
|
|
857 | ||
858 | 5x |
ttl_row <- as.numeric(!is.null(title)) |
859 | 5x |
foot_row <- as.numeric(!is.null(footnotes)) |
860 | 5x |
no_tbl_ind <- c() |
861 | 5x |
ht_x <- c() |
862 | 5x |
ht_units <- c() |
863 | ||
864 | 5x |
if (ttl_row == 1) { |
865 | 1x |
no_tbl_ind <- c(no_tbl_ind, TRUE) |
866 | 1x |
ht_x <- c(ht_x, 2) |
867 | 1x |
ht_units <- c(ht_units, "lines") |
868 |
}
|
|
869 | ||
870 | 5x |
no_tbl_ind <- c(no_tbl_ind, rep(TRUE, 3), rep(FALSE, 2)) |
871 | 5x |
ht_x <- c( |
872 | 5x |
ht_x,
|
873 | 5x |
1,
|
874 | 5x |
grid::convertX(with(g_el, xaxis$height + ylab$width), "pt") + grid::unit(5, "pt"), |
875 | 5x |
grid::convertX(g_el$guide$heights, "pt") + grid::unit(2, "pt"), |
876 | 5x |
nlines + 0.5, |
877 | 5x |
grid::convertX(with(g_el, xaxis$height + ylab$width), "pt") |
878 |
)
|
|
879 | 5x |
ht_units <- c( |
880 | 5x |
ht_units,
|
881 | 5x |
"null",
|
882 | 5x |
"pt",
|
883 | 5x |
"pt",
|
884 | 5x |
"lines",
|
885 | 5x |
"pt"
|
886 |
)
|
|
887 | ||
888 | 5x |
if (foot_row == 1) { |
889 | 1x |
no_tbl_ind <- c(no_tbl_ind, TRUE) |
890 | 1x |
ht_x <- c(ht_x, 1) |
891 | 1x |
ht_units <- c(ht_units, "lines") |
892 |
}
|
|
893 | ||
894 | 5x |
no_at_risk_tbl <- if (annot_at_risk) { |
895 | 5x |
rep(TRUE, 5 + ttl_row + foot_row) |
896 |
} else { |
|
897 | ! |
no_tbl_ind
|
898 |
}
|
|
899 | ||
900 | 5x |
grid::grid.layout( |
901 | 5x |
nrow = sum(no_at_risk_tbl), ncol = 2, |
902 | 5x |
widths = grid::unit(c(col_annot_width, 1), c("pt", "null")), |
903 | 5x |
heights = grid::unit( |
904 | 5x |
x = ht_x[no_at_risk_tbl], |
905 | 5x |
units = ht_units[no_at_risk_tbl] |
906 |
)
|
|
907 |
)
|
|
908 |
}
|
|
909 | ||
910 |
#' Helper: Patient-at-Risk Grobs
|
|
911 |
#'
|
|
912 |
#' @description `r lifecycle::badge("stable")`
|
|
913 |
#'
|
|
914 |
#' Two graphical objects are obtained, one corresponding to row labeling and
|
|
915 |
#' the second to the number of patient at risk.
|
|
916 |
#'
|
|
917 |
#' @inheritParams g_km
|
|
918 |
#' @inheritParams h_ggkm
|
|
919 |
#' @param annot_tbl (`data.frame`)\cr annotation as prepared by [survival::summary.survfit()] which
|
|
920 |
#' includes the number of patients at risk at given time points.
|
|
921 |
#' @param xlim (`numeric`)\cr the maximum value on the x-axis (used to
|
|
922 |
#' ensure the at risk table aligns with the KM graph).
|
|
923 |
#'
|
|
924 |
#' @return A named `list` of two `gTree` objects: `at_risk` and `label`.
|
|
925 |
#'
|
|
926 |
#' @examples
|
|
927 |
#' \donttest{
|
|
928 |
#' library(dplyr)
|
|
929 |
#' library(survival)
|
|
930 |
#' library(grid)
|
|
931 |
#'
|
|
932 |
#' fit_km <- tern_ex_adtte %>%
|
|
933 |
#' filter(PARAMCD == "OS") %>%
|
|
934 |
#' survfit(form = Surv(AVAL, 1 - CNSR) ~ ARMCD, data = .)
|
|
935 |
#'
|
|
936 |
#' data_plot <- h_data_plot(fit_km = fit_km)
|
|
937 |
#'
|
|
938 |
#' xticks <- h_xticks(data = data_plot)
|
|
939 |
#'
|
|
940 |
#' gg <- h_ggkm(
|
|
941 |
#' data = data_plot,
|
|
942 |
#' censor_show = TRUE,
|
|
943 |
#' xticks = xticks, xlab = "Days", ylab = "Survival Probability",
|
|
944 |
#' title = "tt", footnotes = "ff", yval = "Survival"
|
|
945 |
#' )
|
|
946 |
#'
|
|
947 |
#' # The annotation table reports the patient at risk for a given strata and
|
|
948 |
#' # time (`xticks`).
|
|
949 |
#' annot_tbl <- summary(fit_km, time = xticks)
|
|
950 |
#' if (is.null(fit_km$strata)) {
|
|
951 |
#' annot_tbl <- with(annot_tbl, data.frame(n.risk = n.risk, time = time, strata = "All"))
|
|
952 |
#' } else {
|
|
953 |
#' strata_lst <- strsplit(sub("=", "equals", levels(annot_tbl$strata)), "equals")
|
|
954 |
#' levels(annot_tbl$strata) <- matrix(unlist(strata_lst), ncol = 2, byrow = TRUE)[, 2]
|
|
955 |
#' annot_tbl <- data.frame(
|
|
956 |
#' n.risk = annot_tbl$n.risk,
|
|
957 |
#' time = annot_tbl$time,
|
|
958 |
#' strata = annot_tbl$strata
|
|
959 |
#' )
|
|
960 |
#' }
|
|
961 |
#'
|
|
962 |
#' # The annotation table is transformed into a grob.
|
|
963 |
#' tbl <- h_grob_tbl_at_risk(data = data_plot, annot_tbl = annot_tbl, xlim = max(xticks))
|
|
964 |
#'
|
|
965 |
#' # For the representation, the layout is estimated for which the decomposition
|
|
966 |
#' # of the graphic element is necessary.
|
|
967 |
#' g_el <- h_decompose_gg(gg)
|
|
968 |
#' lyt <- h_km_layout(data = data_plot, g_el = g_el, title = "t", footnotes = "f")
|
|
969 |
#'
|
|
970 |
#' grid::grid.newpage()
|
|
971 |
#' pushViewport(viewport(layout = lyt, height = .95, width = .95))
|
|
972 |
#' grid.rect(gp = grid::gpar(lty = 1, col = "purple", fill = "gray85", lwd = 1))
|
|
973 |
#' pushViewport(viewport(layout.pos.row = 4, layout.pos.col = 2))
|
|
974 |
#' grid.rect(gp = grid::gpar(lty = 1, col = "orange", fill = "gray85", lwd = 1))
|
|
975 |
#' grid::grid.draw(tbl$at_risk)
|
|
976 |
#' popViewport()
|
|
977 |
#' pushViewport(viewport(layout.pos.row = 4, layout.pos.col = 1))
|
|
978 |
#' grid.rect(gp = grid::gpar(lty = 1, col = "green3", fill = "gray85", lwd = 1))
|
|
979 |
#' grid::grid.draw(tbl$label)
|
|
980 |
#' }
|
|
981 |
#'
|
|
982 |
#' @export
|
|
983 |
h_grob_tbl_at_risk <- function(data, annot_tbl, xlim) { |
|
984 | 5x |
txtlines <- levels(as.factor(data$strata)) |
985 | 5x |
nlines <- nlevels(as.factor(data$strata)) |
986 | 5x |
y_int <- annot_tbl$time[2] - annot_tbl$time[1] |
987 | 5x |
annot_tbl <- expand.grid( |
988 | 5x |
time = seq(0, xlim, y_int), |
989 | 5x |
strata = unique(annot_tbl$strata) |
990 | 5x |
) %>% dplyr::left_join(annot_tbl, by = c("time", "strata")) |
991 | 5x |
annot_tbl[is.na(annot_tbl)] <- 0 |
992 | 5x |
y_str_unit <- as.numeric(annot_tbl$strata) |
993 | 5x |
vp_table <- grid::plotViewport(margins = grid::unit(c(0, 0, 0, 0), "lines")) |
994 | 5x |
gb_table_left_annot <- grid::gList( |
995 | 5x |
grid::rectGrob( |
996 | 5x |
x = 0, y = grid::unit(c(1:nlines) - 1, "lines"), |
997 | 5x |
gp = grid::gpar(fill = c("gray95", "gray90"), alpha = 1, col = "white"), |
998 | 5x |
height = grid::unit(1, "lines"), just = "bottom", hjust = 0 |
999 |
),
|
|
1000 | 5x |
grid::textGrob( |
1001 | 5x |
label = unique(annot_tbl$strata), |
1002 | 5x |
x = 0.5, |
1003 | 5x |
y = grid::unit( |
1004 | 5x |
(max(unique(y_str_unit)) - unique(y_str_unit)) + 0.75, |
1005 | 5x |
"native"
|
1006 |
),
|
|
1007 | 5x |
gp = grid::gpar(fontface = "italic", fontsize = 10) |
1008 |
)
|
|
1009 |
)
|
|
1010 | 5x |
gb_patient_at_risk <- grid::gList( |
1011 | 5x |
grid::rectGrob( |
1012 | 5x |
x = 0, y = grid::unit(c(1:nlines) - 1, "lines"), |
1013 | 5x |
gp = grid::gpar(fill = c("gray95", "gray90"), alpha = 1, col = "white"), |
1014 | 5x |
height = grid::unit(1, "lines"), just = "bottom", hjust = 0 |
1015 |
),
|
|
1016 | 5x |
grid::textGrob( |
1017 | 5x |
label = annot_tbl$n.risk, |
1018 | 5x |
x = grid::unit(annot_tbl$time, "native"), |
1019 | 5x |
y = grid::unit( |
1020 | 5x |
(max(y_str_unit) - y_str_unit) + .5, |
1021 | 5x |
"line"
|
1022 | 5x |
) # maybe native |
1023 |
)
|
|
1024 |
)
|
|
1025 | ||
1026 | 5x |
list( |
1027 | 5x |
at_risk = grid::gList( |
1028 | 5x |
grid::gTree( |
1029 | 5x |
vp = vp_table, |
1030 | 5x |
children = grid::gList( |
1031 | 5x |
grid::gTree( |
1032 | 5x |
vp = grid::dataViewport( |
1033 | 5x |
xscale = c(0, xlim) + c(-0.05, 0.05) * xlim, |
1034 | 5x |
yscale = c(0, nlines + 1), |
1035 | 5x |
extension = c(0.05, 0) |
1036 |
),
|
|
1037 | 5x |
children = grid::gList(gb_patient_at_risk) |
1038 |
)
|
|
1039 |
)
|
|
1040 |
)
|
|
1041 |
),
|
|
1042 | 5x |
label = grid::gList( |
1043 | 5x |
grid::gTree( |
1044 | 5x |
vp = grid::viewport(width = max(grid::stringWidth(txtlines))), |
1045 | 5x |
children = grid::gList( |
1046 | 5x |
grid::gTree( |
1047 | 5x |
vp = grid::dataViewport( |
1048 | 5x |
xscale = 0:1, |
1049 | 5x |
yscale = c(0, nlines + 1), |
1050 | 5x |
extension = c(0.0, 0) |
1051 |
),
|
|
1052 | 5x |
children = grid::gList(gb_table_left_annot) |
1053 |
)
|
|
1054 |
)
|
|
1055 |
)
|
|
1056 |
)
|
|
1057 |
)
|
|
1058 |
}
|
|
1059 | ||
1060 |
#' Helper Function: Survival Estimations
|
|
1061 |
#'
|
|
1062 |
#' @description `r lifecycle::badge("stable")`
|
|
1063 |
#'
|
|
1064 |
#' Transform a survival fit to a table with groups in rows characterized by N, median and confidence interval.
|
|
1065 |
#'
|
|
1066 |
#' @inheritParams h_data_plot
|
|
1067 |
#'
|
|
1068 |
#' @return A summary table with statistics `N`, `Median`, and `XX% CI` (`XX` taken from `fit_km`).
|
|
1069 |
#'
|
|
1070 |
#' @examples
|
|
1071 |
#' \donttest{
|
|
1072 |
#' library(dplyr)
|
|
1073 |
#' library(survival)
|
|
1074 |
#'
|
|
1075 |
#' adtte <- tern_ex_adtte %>% filter(PARAMCD == "OS")
|
|
1076 |
#' fit <- survfit(
|
|
1077 |
#' form = Surv(AVAL, 1 - CNSR) ~ ARMCD,
|
|
1078 |
#' data = adtte
|
|
1079 |
#' )
|
|
1080 |
#' h_tbl_median_surv(fit_km = fit)
|
|
1081 |
#' }
|
|
1082 |
#'
|
|
1083 |
#' @export
|
|
1084 |
h_tbl_median_surv <- function(fit_km, armval = "All") { |
|
1085 | 5x |
y <- if (is.null(fit_km$strata)) { |
1086 | ! |
as.data.frame(t(summary(fit_km)$table), row.names = armval) |
1087 |
} else { |
|
1088 | 5x |
tbl <- summary(fit_km)$table |
1089 | 5x |
rownames_lst <- strsplit(sub("=", "equals", rownames(tbl)), "equals") |
1090 | 5x |
rownames(tbl) <- matrix(unlist(rownames_lst), ncol = 2, byrow = TRUE)[, 2] |
1091 | 5x |
as.data.frame(tbl) |
1092 |
}
|
|
1093 | 5x |
conf.int <- summary(fit_km)$conf.int # nolint |
1094 | 5x |
y$records <- round(y$records) |
1095 | 5x |
y$median <- signif(y$median, 4) |
1096 | 5x |
y$`CI` <- paste0( |
1097 | 5x |
"(", signif(y[[paste0(conf.int, "LCL")]], 4), ", ", signif(y[[paste0(conf.int, "UCL")]], 4), ")" |
1098 |
)
|
|
1099 | 5x |
stats::setNames( |
1100 | 5x |
y[c("records", "median", "CI")], |
1101 | 5x |
c("N", "Median", f_conf_level(conf.int)) |
1102 |
)
|
|
1103 |
}
|
|
1104 | ||
1105 |
#' Helper Function: Survival Estimation Grob
|
|
1106 |
#'
|
|
1107 |
#' @description `r lifecycle::badge("stable")`
|
|
1108 |
#'
|
|
1109 |
#' The survival fit is transformed in a grob containing a table with groups in
|
|
1110 |
#' rows characterized by N, median and 95% confidence interval.
|
|
1111 |
#'
|
|
1112 |
#' @inheritParams g_km
|
|
1113 |
#' @inheritParams h_data_plot
|
|
1114 |
#' @param ttheme (`list`)\cr see [gridExtra::ttheme_default()].
|
|
1115 |
#' @param x (`numeric`)\cr a value between 0 and 1 specifying x-location.
|
|
1116 |
#' @param y (`numeric`)\cr a value between 0 and 1 specifying y-location.
|
|
1117 |
#' @param width (`unit`)\cr width (as a unit) to use when printing the grob.
|
|
1118 |
#'
|
|
1119 |
#' @return A `grob` of a table containing statistics `N`, `Median`, and `XX% CI` (`XX` taken from `fit_km`).
|
|
1120 |
#'
|
|
1121 |
#' @examples
|
|
1122 |
#' \donttest{
|
|
1123 |
#' library(dplyr)
|
|
1124 |
#' library(survival)
|
|
1125 |
#' library(grid)
|
|
1126 |
#'
|
|
1127 |
#' grid::grid.newpage()
|
|
1128 |
#' grid.rect(gp = grid::gpar(lty = 1, col = "pink", fill = "gray85", lwd = 1))
|
|
1129 |
#' tern_ex_adtte %>%
|
|
1130 |
#' filter(PARAMCD == "OS") %>%
|
|
1131 |
#' survfit(form = Surv(AVAL, 1 - CNSR) ~ ARMCD, data = .) %>%
|
|
1132 |
#' h_grob_median_surv() %>%
|
|
1133 |
#' grid::grid.draw()
|
|
1134 |
#' }
|
|
1135 |
#'
|
|
1136 |
#' @export
|
|
1137 |
h_grob_median_surv <- function(fit_km, |
|
1138 |
armval = "All", |
|
1139 |
x = 0.9, |
|
1140 |
y = 0.9, |
|
1141 |
width = grid::unit(0.3, "npc"), |
|
1142 |
ttheme = gridExtra::ttheme_default()) { |
|
1143 | 4x |
data <- h_tbl_median_surv(fit_km, armval = armval) |
1144 | ||
1145 | 4x |
width <- grid::convertUnit(width, "in") |
1146 | 4x |
height <- width * (nrow(data) + 1) / 12 |
1147 | ||
1148 | 4x |
w <- paste(" ", c( |
1149 | 4x |
rownames(data)[which.max(nchar(rownames(data)))], |
1150 | 4x |
sapply(names(data), function(x) c(x, data[[x]])[which.max(nchar(c(x, data[[x]])))]) |
1151 |
)) |
|
1152 | 4x |
w_unit <- grid::convertWidth(grid::stringWidth(w), "in", valueOnly = TRUE) |
1153 | ||
1154 | 4x |
w_txt <- sapply(1:64, function(x) { |
1155 | 256x |
graphics::par(ps = x) |
1156 | 256x |
graphics::strwidth(w[4], units = "in") |
1157 |
}) |
|
1158 | 4x |
f_size_w <- which.max(w_txt[w_txt < as.numeric((w_unit / sum(w_unit)) * width)[4]]) |
1159 | ||
1160 | 4x |
h_txt <- sapply(1:64, function(x) { |
1161 | 256x |
graphics::par(ps = x) |
1162 | 256x |
graphics::strheight(grid::stringHeight("X"), units = "in") |
1163 |
}) |
|
1164 | 4x |
f_size_h <- which.max(h_txt[h_txt < as.numeric(grid::unit(as.numeric(height) / 4, grid::unitType(height)))]) |
1165 | ||
1166 | 4x |
if (ttheme$core$fg_params$fontsize == 12) { |
1167 | 4x |
ttheme$core$fg_params$fontsize <- min(f_size_w, f_size_h) |
1168 | 4x |
ttheme$colhead$fg_params$fontsize <- min(f_size_w, f_size_h) |
1169 | 4x |
ttheme$rowhead$fg_params$fontsize <- min(f_size_w, f_size_h) |
1170 |
}
|
|
1171 | ||
1172 | 4x |
gt <- gridExtra::tableGrob( |
1173 | 4x |
d = data, |
1174 | 4x |
theme = ttheme |
1175 |
)
|
|
1176 | 4x |
gt$widths <- ((w_unit / sum(w_unit)) * width) |
1177 | 4x |
gt$heights <- rep(grid::unit(as.numeric(height) / 4, grid::unitType(height)), nrow(gt)) |
1178 | ||
1179 | 4x |
vp <- grid::viewport( |
1180 | 4x |
x = grid::unit(x, "npc") + grid::unit(1, "lines"), |
1181 | 4x |
y = grid::unit(y, "npc") + grid::unit(1.5, "lines"), |
1182 | 4x |
height = height, |
1183 | 4x |
width = width, |
1184 | 4x |
just = c("right", "top") |
1185 |
)
|
|
1186 | ||
1187 | 4x |
grid::gList( |
1188 | 4x |
grid::gTree( |
1189 | 4x |
vp = vp, |
1190 | 4x |
children = grid::gList(gt) |
1191 |
)
|
|
1192 |
)
|
|
1193 |
}
|
|
1194 | ||
1195 |
#' Helper: Grid Object with y-axis Annotation
|
|
1196 |
#'
|
|
1197 |
#' @description `r lifecycle::badge("stable")`
|
|
1198 |
#'
|
|
1199 |
#' Build the y-axis annotation from a decomposed `ggplot`.
|
|
1200 |
#'
|
|
1201 |
#' @param ylab (`gtable`)\cr the y-lab as a graphical object derived from a `ggplot`.
|
|
1202 |
#' @param yaxis (`gtable`)\cr the y-axis as a graphical object derived from a `ggplot`.
|
|
1203 |
#'
|
|
1204 |
#' @return a `gTree` object containing the y-axis annotation from a `ggplot`.
|
|
1205 |
#'
|
|
1206 |
#' @examples
|
|
1207 |
#' \donttest{
|
|
1208 |
#' library(dplyr)
|
|
1209 |
#' library(survival)
|
|
1210 |
#' library(grid)
|
|
1211 |
#'
|
|
1212 |
#' fit_km <- tern_ex_adtte %>%
|
|
1213 |
#' filter(PARAMCD == "OS") %>%
|
|
1214 |
#' survfit(form = Surv(AVAL, 1 - CNSR) ~ ARMCD, data = .)
|
|
1215 |
#' data_plot <- h_data_plot(fit_km = fit_km)
|
|
1216 |
#' xticks <- h_xticks(data = data_plot)
|
|
1217 |
#' gg <- h_ggkm(
|
|
1218 |
#' data = data_plot,
|
|
1219 |
#' censor_show = TRUE,
|
|
1220 |
#' xticks = xticks, xlab = "Days", ylab = "Survival Probability",
|
|
1221 |
#' title = "title", footnotes = "footnotes", yval = "Survival"
|
|
1222 |
#' )
|
|
1223 |
#'
|
|
1224 |
#' g_el <- h_decompose_gg(gg)
|
|
1225 |
#'
|
|
1226 |
#' grid::grid.newpage()
|
|
1227 |
#' pvp <- grid::plotViewport(margins = c(5, 4, 2, 20))
|
|
1228 |
#' pushViewport(pvp)
|
|
1229 |
#' grid::grid.draw(h_grob_y_annot(ylab = g_el$ylab, yaxis = g_el$yaxis))
|
|
1230 |
#' grid.rect(gp = grid::gpar(lty = 1, col = "gray35", fill = NA))
|
|
1231 |
#' }
|
|
1232 |
#'
|
|
1233 |
#' @export
|
|
1234 |
h_grob_y_annot <- function(ylab, yaxis) { |
|
1235 | 5x |
grid::gList( |
1236 | 5x |
grid::gTree( |
1237 | 5x |
vp = grid::viewport( |
1238 | 5x |
width = grid::convertX(yaxis$width + ylab$width, "pt"), |
1239 | 5x |
x = grid::unit(1, "npc"), |
1240 | 5x |
just = "right" |
1241 |
),
|
|
1242 | 5x |
children = grid::gList(cbind(ylab, yaxis)) |
1243 |
)
|
|
1244 |
)
|
|
1245 |
}
|
|
1246 | ||
1247 |
#' Helper Function: Pairwise `CoxPH` table
|
|
1248 |
#'
|
|
1249 |
#' @description `r lifecycle::badge("stable")`
|
|
1250 |
#'
|
|
1251 |
#' Create a `data.frame` of pairwise stratified or unstratified `CoxPH` analysis results.
|
|
1252 |
#'
|
|
1253 |
#' @inheritParams g_km
|
|
1254 |
#'
|
|
1255 |
#' @return A `data.frame` containing statistics `HR`, `XX% CI` (`XX` taken from `control_coxph_pw`),
|
|
1256 |
#' and `p-value (log-rank)`.
|
|
1257 |
#'
|
|
1258 |
#' @examples
|
|
1259 |
#' \donttest{
|
|
1260 |
#' library(dplyr)
|
|
1261 |
#'
|
|
1262 |
#' adtte <- tern_ex_adtte %>%
|
|
1263 |
#' filter(PARAMCD == "OS") %>%
|
|
1264 |
#' mutate(is_event = CNSR == 0)
|
|
1265 |
#'
|
|
1266 |
#' h_tbl_coxph_pairwise(
|
|
1267 |
#' df = adtte,
|
|
1268 |
#' variables = list(tte = "AVAL", is_event = "is_event", arm = "ARM"),
|
|
1269 |
#' control_coxph_pw = control_coxph(conf_level = 0.9)
|
|
1270 |
#' )
|
|
1271 |
#' }
|
|
1272 |
#'
|
|
1273 |
#' @export
|
|
1274 |
h_tbl_coxph_pairwise <- function(df, |
|
1275 |
variables,
|
|
1276 |
control_coxph_pw = control_coxph()) { |
|
1277 | 3x |
assert_df_with_variables(df, variables) |
1278 | 3x |
arm <- variables$arm |
1279 | 3x |
df[[arm]] <- factor(df[[arm]]) |
1280 | 3x |
ref_group <- levels(df[[arm]])[1] |
1281 | 3x |
comp_group <- levels(df[[arm]])[-1] |
1282 | 3x |
results <- Map(function(comp) { |
1283 | 6x |
res <- s_coxph_pairwise( |
1284 | 6x |
df = df[df[[arm]] == comp, , drop = FALSE], |
1285 | 6x |
.ref_group = df[df[[arm]] == ref_group, , drop = FALSE], |
1286 | 6x |
.in_ref_col = FALSE, |
1287 | 6x |
.var = variables$tte, |
1288 | 6x |
is_event = variables$is_event, |
1289 | 6x |
strat = variables$strat, |
1290 | 6x |
control = control_coxph_pw |
1291 |
)
|
|
1292 | 6x |
res_df <- data.frame( |
1293 | 6x |
hr = format(round(res$hr, 2), nsmall = 2), |
1294 | 6x |
hr_ci = paste0( |
1295 | 6x |
"(", format(round(res$hr_ci[1], 2), nsmall = 2), ", ", |
1296 | 6x |
format(round(res$hr_ci[2], 2), nsmall = 2), ")" |
1297 |
),
|
|
1298 | 6x |
pvalue = if (res$pvalue < 0.0001) "<0.0001" else format(round(res$pvalue, 4), 4), |
1299 | 6x |
stringsAsFactors = FALSE |
1300 |
)
|
|
1301 | 6x |
colnames(res_df) <- c("HR", vapply(res[c("hr_ci", "pvalue")], obj_label, FUN.VALUE = "character")) |
1302 | 6x |
row.names(res_df) <- comp |
1303 | 6x |
res_df
|
1304 | 3x |
}, comp_group) |
1305 | 3x |
do.call(rbind, results) |
1306 |
}
|
|
1307 | ||
1308 |
#' Helper Function: `CoxPH` Grob
|
|
1309 |
#'
|
|
1310 |
#' @description `r lifecycle::badge("stable")`
|
|
1311 |
#'
|
|
1312 |
#' Grob of `rtable` output from [h_tbl_coxph_pairwise()]
|
|
1313 |
#'
|
|
1314 |
#' @inheritParams h_grob_median_surv
|
|
1315 |
#' @param ... arguments will be passed to [h_tbl_coxph_pairwise()].
|
|
1316 |
#' @param x (`numeric`)\cr a value between 0 and 1 specifying x-location.
|
|
1317 |
#' @param y (`numeric`)\cr a value between 0 and 1 specifying y-location.
|
|
1318 |
#' @param width (`unit`)\cr width (as a unit) to use when printing the grob.
|
|
1319 |
#'
|
|
1320 |
#' @return A `grob` of a table containing statistics `HR`, `XX% CI` (`XX` taken from `control_coxph_pw`),
|
|
1321 |
#' and `p-value (log-rank)`.
|
|
1322 |
#'
|
|
1323 |
#' @examples
|
|
1324 |
#' \donttest{
|
|
1325 |
#' library(dplyr)
|
|
1326 |
#' library(survival)
|
|
1327 |
#' library(grid)
|
|
1328 |
#'
|
|
1329 |
#' grid::grid.newpage()
|
|
1330 |
#' grid.rect(gp = grid::gpar(lty = 1, col = "pink", fill = "gray85", lwd = 1))
|
|
1331 |
#' data <- tern_ex_adtte %>%
|
|
1332 |
#' filter(PARAMCD == "OS") %>%
|
|
1333 |
#' mutate(is_event = CNSR == 0)
|
|
1334 |
#' tbl_grob <- h_grob_coxph(
|
|
1335 |
#' df = data,
|
|
1336 |
#' variables = list(tte = "AVAL", is_event = "is_event", arm = "ARMCD"),
|
|
1337 |
#' control_coxph_pw = control_coxph(conf_level = 0.9), x = 0.5, y = 0.5
|
|
1338 |
#' )
|
|
1339 |
#' grid::grid.draw(tbl_grob)
|
|
1340 |
#' }
|
|
1341 |
#'
|
|
1342 |
#' @export
|
|
1343 |
h_grob_coxph <- function(..., |
|
1344 |
x = 0, |
|
1345 |
y = 0, |
|
1346 |
width = grid::unit(0.4, "npc"), |
|
1347 |
ttheme = gridExtra::ttheme_default( |
|
1348 |
padding = grid::unit(c(1, .5), "lines"), |
|
1349 |
core = list(bg_params = list(fill = c("grey95", "grey90"), alpha = .5)) |
|
1350 |
)) { |
|
1351 | 2x |
data <- h_tbl_coxph_pairwise(...) |
1352 | ||
1353 | 2x |
width <- grid::convertUnit(width, "in") |
1354 | 2x |
height <- width * (nrow(data) + 1) / 12 |
1355 | ||
1356 | 2x |
w <- paste(" ", c( |
1357 | 2x |
rownames(data)[which.max(nchar(rownames(data)))], |
1358 | 2x |
sapply(names(data), function(x) c(x, data[[x]])[which.max(nchar(c(x, data[[x]])))]) |
1359 |
)) |
|
1360 | 2x |
w_unit <- grid::convertWidth(grid::stringWidth(w), "in", valueOnly = TRUE) |
1361 | ||
1362 | 2x |
w_txt <- sapply(1:64, function(x) { |
1363 | 128x |
graphics::par(ps = x) |
1364 | 128x |
graphics::strwidth(w[4], units = "in") |
1365 |
}) |
|
1366 | 2x |
f_size_w <- which.max(w_txt[w_txt < as.numeric((w_unit / sum(w_unit)) * width)[4]]) |
1367 | ||
1368 | 2x |
h_txt <- sapply(1:64, function(x) { |
1369 | 128x |
graphics::par(ps = x) |
1370 | 128x |
graphics::strheight(grid::stringHeight("X"), units = "in") |
1371 |
}) |
|
1372 | 2x |
f_size_h <- which.max(h_txt[h_txt < as.numeric(grid::unit(as.numeric(height) / 4, grid::unitType(height)))]) |
1373 | ||
1374 | 2x |
if (ttheme$core$fg_params$fontsize == 12) { |
1375 | 2x |
ttheme$core$fg_params$fontsize <- min(f_size_w, f_size_h) |
1376 | 2x |
ttheme$colhead$fg_params$fontsize <- min(f_size_w, f_size_h) |
1377 | 2x |
ttheme$rowhead$fg_params$fontsize <- min(f_size_w, f_size_h) |
1378 |
}
|
|
1379 | ||
1380 | 2x |
tryCatch( |
1381 | 2x |
expr = { |
1382 | 2x |
gt <- gridExtra::tableGrob( |
1383 | 2x |
d = data, |
1384 | 2x |
theme = ttheme |
1385 | 2x |
) # ERROR 'data' must be of a vector type, was 'NULL' |
1386 | 2x |
gt$widths <- ((w_unit / sum(w_unit)) * width) |
1387 | 2x |
gt$heights <- rep(grid::unit(as.numeric(height) / 4, grid::unitType(height)), nrow(gt)) |
1388 | 2x |
vp <- grid::viewport( |
1389 | 2x |
x = grid::unit(x, "npc") + grid::unit(1, "lines"), |
1390 | 2x |
y = grid::unit(y, "npc") + grid::unit(1.5, "lines"), |
1391 | 2x |
height = height, |
1392 | 2x |
width = width, |
1393 | 2x |
just = c("left", "bottom") |
1394 |
)
|
|
1395 | 2x |
grid::gList( |
1396 | 2x |
grid::gTree( |
1397 | 2x |
vp = vp, |
1398 | 2x |
children = grid::gList(gt) |
1399 |
)
|
|
1400 |
)
|
|
1401 |
},
|
|
1402 | 2x |
error = function(w) { |
1403 | ! |
message(paste( |
1404 | ! |
"Warning: Cox table will not be displayed as there is",
|
1405 | ! |
"not any level to be compared in the arm variable."
|
1406 |
)) |
|
1407 | ! |
return( |
1408 | ! |
grid::gList( |
1409 | ! |
grid::gTree( |
1410 | ! |
vp = NULL, |
1411 | ! |
children = NULL |
1412 |
)
|
|
1413 |
)
|
|
1414 |
)
|
|
1415 |
}
|
|
1416 |
)
|
|
1417 |
}
|
1 |
#' Confidence Interval for Mean
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Convenient function for calculating the mean confidence interval. It calculates the arithmetic as well as the
|
|
6 |
#' geometric mean. It can be used as a `ggplot` helper function for plotting.
|
|
7 |
#'
|
|
8 |
#' @inheritParams argument_convention
|
|
9 |
#' @param n_min (`number`)\cr a minimum number of non-missing `x` to estimate the confidence interval for mean.
|
|
10 |
#' @param gg_helper (`logical`)\cr `TRUE` when output should be aligned for the use with `ggplot`.
|
|
11 |
#' @param geom_mean (`logical`)\cr `TRUE` when the geometric mean should be calculated.
|
|
12 |
#'
|
|
13 |
#' @return A named `vector` of values `mean_ci_lwr` and `mean_ci_upr`.
|
|
14 |
#'
|
|
15 |
#' @examples
|
|
16 |
#' stat_mean_ci(sample(10), gg_helper = FALSE)
|
|
17 |
#'
|
|
18 |
#' p <- ggplot2::ggplot(mtcars, ggplot2::aes(cyl, mpg)) +
|
|
19 |
#' ggplot2::geom_point()
|
|
20 |
#'
|
|
21 |
#' p + ggplot2::stat_summary(
|
|
22 |
#' fun.data = stat_mean_ci,
|
|
23 |
#' geom = "errorbar"
|
|
24 |
#' )
|
|
25 |
#'
|
|
26 |
#' p + ggplot2::stat_summary(
|
|
27 |
#' fun.data = stat_mean_ci,
|
|
28 |
#' fun.args = list(conf_level = 0.5),
|
|
29 |
#' geom = "errorbar"
|
|
30 |
#' )
|
|
31 |
#'
|
|
32 |
#' p + ggplot2::stat_summary(
|
|
33 |
#' fun.data = stat_mean_ci,
|
|
34 |
#' fun.args = list(conf_level = 0.5, geom_mean = TRUE),
|
|
35 |
#' geom = "errorbar"
|
|
36 |
#' )
|
|
37 |
#'
|
|
38 |
#' @export
|
|
39 |
stat_mean_ci <- function(x, |
|
40 |
conf_level = 0.95, |
|
41 |
na.rm = TRUE, # nolint |
|
42 |
n_min = 2, |
|
43 |
gg_helper = TRUE, |
|
44 |
geom_mean = FALSE) { |
|
45 | 460x |
if (na.rm) { |
46 | 2x |
x <- stats::na.omit(x) |
47 |
}
|
|
48 | 460x |
n <- length(x) |
49 | ||
50 | 460x |
if (!geom_mean) { |
51 | 231x |
m <- mean(x) |
52 |
} else { |
|
53 | 229x |
negative_values_exist <- any(is.na(x[!is.na(x)]) <- x[!is.na(x)] <= 0) |
54 | 229x |
if (negative_values_exist) { |
55 | 18x |
m <- NA_real_ |
56 |
} else { |
|
57 | 211x |
x <- log(x) |
58 | 211x |
m <- mean(x) |
59 |
}
|
|
60 |
}
|
|
61 | ||
62 | 460x |
if (n < n_min || is.na(m)) { |
63 | 96x |
ci <- c(mean_ci_lwr = NA_real_, mean_ci_upr = NA_real_) |
64 |
} else { |
|
65 | 364x |
hci <- stats::qt((1 + conf_level) / 2, df = n - 1) * stats::sd(x) / sqrt(n) |
66 | 364x |
ci <- c(mean_ci_lwr = m - hci, mean_ci_upr = m + hci) |
67 | 364x |
if (geom_mean) { |
68 | 176x |
ci <- exp(ci) |
69 |
}
|
|
70 |
}
|
|
71 | ||
72 | 460x |
if (gg_helper) { |
73 | ! |
m <- ifelse(is.na(m), NA_real_, m) |
74 | ! |
ci <- data.frame(y = ifelse(geom_mean, exp(m), m), ymin = ci[[1]], ymax = ci[[2]]) |
75 |
}
|
|
76 | ||
77 | 460x |
return(ci) |
78 |
}
|
|
79 | ||
80 |
#' Confidence Interval for Median
|
|
81 |
#'
|
|
82 |
#' @description `r lifecycle::badge("stable")`
|
|
83 |
#'
|
|
84 |
#' Convenient function for calculating the median confidence interval. It can be used as a `ggplot` helper
|
|
85 |
#' function for plotting.
|
|
86 |
#'
|
|
87 |
#' @inheritParams argument_convention
|
|
88 |
#' @param gg_helper (`logical`)\cr `TRUE` when output should be aligned for the use with `ggplot`.
|
|
89 |
#'
|
|
90 |
#' @details The function was adapted from `DescTools/versions/0.99.35/source`
|
|
91 |
#'
|
|
92 |
#' @return A named `vector` of values `median_ci_lwr` and `median_ci_upr`.
|
|
93 |
#'
|
|
94 |
#' @examples
|
|
95 |
#' stat_median_ci(sample(10), gg_helper = FALSE)
|
|
96 |
#'
|
|
97 |
#' p <- ggplot2::ggplot(mtcars, ggplot2::aes(cyl, mpg)) +
|
|
98 |
#' ggplot2::geom_point()
|
|
99 |
#' p + ggplot2::stat_summary(
|
|
100 |
#' fun.data = stat_median_ci,
|
|
101 |
#' geom = "errorbar"
|
|
102 |
#' )
|
|
103 |
#'
|
|
104 |
#' @export
|
|
105 |
stat_median_ci <- function(x, |
|
106 |
conf_level = 0.95, |
|
107 |
na.rm = TRUE, # nolint |
|
108 |
gg_helper = TRUE) { |
|
109 | 232x |
x <- unname(x) |
110 | 232x |
if (na.rm) { |
111 | 3x |
x <- x[!is.na(x)] |
112 |
}
|
|
113 | 232x |
n <- length(x) |
114 | 232x |
med <- stats::median(x) |
115 | ||
116 | 232x |
k <- stats::qbinom(p = (1 - conf_level) / 2, size = n, prob = 0.5, lower.tail = TRUE) |
117 | ||
118 |
# k == 0 - for small samples (e.g. n <= 5) ci can be outside the observed range
|
|
119 | 232x |
if (k == 0 || is.na(med)) { |
120 | 78x |
ci <- c(median_ci_lwr = NA_real_, median_ci_upr = NA_real_) |
121 | 78x |
empir_conf_level <- NA_real_ |
122 |
} else { |
|
123 | 154x |
x_sort <- sort(x) |
124 | 154x |
ci <- c(median_ci_lwr = x_sort[k], median_ci_upr = x_sort[n - k + 1]) |
125 | 154x |
empir_conf_level <- 1 - 2 * stats::pbinom(k - 1, size = n, prob = 0.5) |
126 |
}
|
|
127 | ||
128 | 232x |
if (gg_helper) { |
129 | ! |
ci <- data.frame(y = med, ymin = ci[[1]], ymax = ci[[2]]) |
130 |
}
|
|
131 | ||
132 | 232x |
attr(ci, "conf_level") <- empir_conf_level |
133 | ||
134 | 232x |
return(ci) |
135 |
}
|
|
136 | ||
137 |
#' p-Value of the Mean
|
|
138 |
#'
|
|
139 |
#' @description `r lifecycle::badge("stable")`
|
|
140 |
#'
|
|
141 |
#' Convenient function for calculating the two-sided p-value of the mean.
|
|
142 |
#'
|
|
143 |
#' @inheritParams argument_convention
|
|
144 |
#' @param n_min (`numeric`)\cr a minimum number of non-missing `x` to estimate the p-value of the mean.
|
|
145 |
#' @param test_mean (`numeric`)\cr mean value to test under the null hypothesis.
|
|
146 |
#'
|
|
147 |
#' @return A p-value.
|
|
148 |
#'
|
|
149 |
#' @examples
|
|
150 |
#' stat_mean_pval(sample(10))
|
|
151 |
#'
|
|
152 |
#' stat_mean_pval(rnorm(10), test_mean = 0.5)
|
|
153 |
#'
|
|
154 |
#' @export
|
|
155 |
stat_mean_pval <- function(x, |
|
156 |
na.rm = TRUE, # nolint |
|
157 |
n_min = 2, |
|
158 |
test_mean = 0) { |
|
159 | 233x |
if (na.rm) { |
160 | 4x |
x <- stats::na.omit(x) |
161 |
}
|
|
162 | 233x |
n <- length(x) |
163 | ||
164 | 233x |
x_mean <- mean(x) |
165 | 233x |
x_sd <- stats::sd(x) |
166 | ||
167 | 233x |
if (n < n_min) { |
168 | 42x |
pv <- c(p_value = NA_real_) |
169 |
} else { |
|
170 | 191x |
x_se <- stats::sd(x) / sqrt(n) |
171 | 191x |
ttest <- (x_mean - test_mean) / x_se |
172 | 191x |
pv <- c(p_value = 2 * stats::pt(-abs(ttest), df = n - 1)) |
173 |
}
|
|
174 | ||
175 | 233x |
return(pv) |
176 |
}
|
1 |
#' Estimation of Proportions
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Estimate the proportion of responders within a studied population.
|
|
6 |
#'
|
|
7 |
#' @inheritParams argument_convention
|
|
8 |
#'
|
|
9 |
#' @seealso [h_proportions]
|
|
10 |
#'
|
|
11 |
#' @name estimate_proportions
|
|
12 |
NULL
|
|
13 | ||
14 |
#' @describeIn estimate_proportions Statistics function estimating a
|
|
15 |
#' proportion along with its confidence interval.
|
|
16 |
#'
|
|
17 |
#' @inheritParams prop_strat_wilson
|
|
18 |
#' @param df (`logical` or `data.frame`)\cr if only a logical vector is used,
|
|
19 |
#' it indicates whether each subject is a responder or not. `TRUE` represents
|
|
20 |
#' a successful outcome. If a `data.frame` is provided, also the `strata` variable
|
|
21 |
#' names must be provided in `variables` as a list element with the strata strings.
|
|
22 |
#' In the case of `data.frame`, the logical vector of responses must be indicated as a
|
|
23 |
#' variable name in `.var`.
|
|
24 |
#' @param method (`string`)\cr the method used to construct the confidence interval
|
|
25 |
#' for proportion of successful outcomes; one of `waldcc`, `wald`, `clopper-pearson`,
|
|
26 |
#' `wilson`, `wilsonc`, `strat_wilson`, `strat_wilsonc`, `agresti-coull` or `jeffreys`.
|
|
27 |
#' @param long (`flag`)\cr a long description is required.
|
|
28 |
#'
|
|
29 |
#' @return
|
|
30 |
#' * `s_proportion()` returns statistics `n_prop` (`n` and proportion) and `prop_ci` (proportion CI) for a
|
|
31 |
#' given variable.
|
|
32 |
#'
|
|
33 |
#' @examples
|
|
34 |
#' # Case with only logical vector.
|
|
35 |
#' rsp_v <- c(1, 0, 1, 0, 1, 1, 0, 0)
|
|
36 |
#' s_proportion(rsp_v)
|
|
37 |
#'
|
|
38 |
#' # Example for Stratified Wilson CI
|
|
39 |
#' nex <- 100 # Number of example rows
|
|
40 |
#' dta <- data.frame(
|
|
41 |
#' "rsp" = sample(c(TRUE, FALSE), nex, TRUE),
|
|
42 |
#' "grp" = sample(c("A", "B"), nex, TRUE),
|
|
43 |
#' "f1" = sample(c("a1", "a2"), nex, TRUE),
|
|
44 |
#' "f2" = sample(c("x", "y", "z"), nex, TRUE),
|
|
45 |
#' stringsAsFactors = TRUE
|
|
46 |
#' )
|
|
47 |
#'
|
|
48 |
#' s_proportion(
|
|
49 |
#' df = dta,
|
|
50 |
#' .var = "rsp",
|
|
51 |
#' variables = list(strata = c("f1", "f2")),
|
|
52 |
#' conf_level = 0.90,
|
|
53 |
#' method = "strat_wilson"
|
|
54 |
#' )
|
|
55 |
#'
|
|
56 |
#' @export
|
|
57 |
s_proportion <- function(df, |
|
58 |
.var,
|
|
59 |
conf_level = 0.95, |
|
60 |
method = c( |
|
61 |
"waldcc", "wald", "clopper-pearson", |
|
62 |
"wilson", "wilsonc", "strat_wilson", "strat_wilsonc", |
|
63 |
"agresti-coull", "jeffreys" |
|
64 |
),
|
|
65 |
weights = NULL, |
|
66 |
max_iterations = 50, |
|
67 |
variables = list(strata = NULL), |
|
68 |
long = FALSE) { |
|
69 | 125x |
method <- match.arg(method) |
70 | 125x |
checkmate::assert_flag(long) |
71 | 125x |
assert_proportion_value(conf_level) |
72 | ||
73 | 125x |
if (!is.null(variables$strata)) { |
74 |
# Checks for strata
|
|
75 | ! |
if (missing(df)) stop("When doing stratified analysis a data.frame with specific columns is needed.") |
76 | ! |
strata_colnames <- variables$strata |
77 | ! |
checkmate::assert_character(strata_colnames, null.ok = FALSE) |
78 | ! |
strata_vars <- stats::setNames(as.list(strata_colnames), strata_colnames) |
79 | ! |
assert_df_with_variables(df, strata_vars) |
80 | ||
81 | ! |
strata <- interaction(df[strata_colnames]) |
82 | ! |
strata <- as.factor(strata) |
83 | ||
84 |
# Pushing down checks to prop_strat_wilson
|
|
85 | 125x |
} else if (checkmate::test_subset(method, c("strat_wilson", "strat_wilsonc"))) { |
86 | ! |
stop("To use stratified methods you need to specify the strata variables.") |
87 |
}
|
|
88 | 125x |
if (checkmate::test_atomic_vector(df)) { |
89 | 125x |
rsp <- as.logical(df) |
90 |
} else { |
|
91 | ! |
rsp <- as.logical(df[[.var]]) |
92 |
}
|
|
93 | 125x |
n <- sum(rsp) |
94 | 125x |
p_hat <- mean(rsp) |
95 | ||
96 | 125x |
prop_ci <- switch(method, |
97 | 125x |
"clopper-pearson" = prop_clopper_pearson(rsp, conf_level), |
98 | 125x |
"wilson" = prop_wilson(rsp, conf_level), |
99 | 125x |
"wilsonc" = prop_wilson(rsp, conf_level, correct = TRUE), |
100 | 125x |
"strat_wilson" = prop_strat_wilson(rsp, |
101 | 125x |
strata,
|
102 | 125x |
weights,
|
103 | 125x |
conf_level,
|
104 | 125x |
max_iterations,
|
105 | 125x |
correct = FALSE |
106 | 125x |
)$conf_int, |
107 | 125x |
"strat_wilsonc" = prop_strat_wilson(rsp, |
108 | 125x |
strata,
|
109 | 125x |
weights,
|
110 | 125x |
conf_level,
|
111 | 125x |
max_iterations,
|
112 | 125x |
correct = TRUE |
113 | 125x |
)$conf_int, |
114 | 125x |
"wald" = prop_wald(rsp, conf_level), |
115 | 125x |
"waldcc" = prop_wald(rsp, conf_level, correct = TRUE), |
116 | 125x |
"agresti-coull" = prop_agresti_coull(rsp, conf_level), |
117 | 125x |
"jeffreys" = prop_jeffreys(rsp, conf_level) |
118 |
)
|
|
119 | ||
120 | 125x |
list( |
121 | 125x |
"n_prop" = formatters::with_label(c(n, p_hat), "Responders"), |
122 | 125x |
"prop_ci" = formatters::with_label( |
123 | 125x |
x = 100 * prop_ci, label = d_proportion(conf_level, method, long = long) |
124 |
)
|
|
125 |
)
|
|
126 |
}
|
|
127 | ||
128 |
#' @describeIn estimate_proportions Formatted analysis function which is used as `afun`
|
|
129 |
#' in `estimate_proportion()`.
|
|
130 |
#'
|
|
131 |
#' @return
|
|
132 |
#' * `a_proportion()` returns the corresponding list with formatted [rtables::CellValue()].
|
|
133 |
#'
|
|
134 |
#' @export
|
|
135 |
a_proportion <- make_afun( |
|
136 |
s_proportion,
|
|
137 |
.formats = c(n_prop = "xx (xx.x%)", prop_ci = "(xx.x, xx.x)") |
|
138 |
)
|
|
139 | ||
140 |
#' @describeIn estimate_proportions Layout-creating function which can take statistics function arguments
|
|
141 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()].
|
|
142 |
#'
|
|
143 |
#' @param ... other arguments are ultimately conveyed to [s_proportion()].
|
|
144 |
#'
|
|
145 |
#' @return
|
|
146 |
#' * `estimate_proportion()` returns a layout object suitable for passing to further layouting functions,
|
|
147 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing
|
|
148 |
#' the statistics from `s_proportion()` to the table layout.
|
|
149 |
#'
|
|
150 |
#' @examples
|
|
151 |
#' dta_test <- data.frame(
|
|
152 |
#' USUBJID = paste0("S", 1:12),
|
|
153 |
#' ARM = rep(LETTERS[1:3], each = 4),
|
|
154 |
#' AVAL = c(A = c(1, 1, 1, 1), B = c(0, 0, 1, 1), C = c(0, 0, 0, 0))
|
|
155 |
#' )
|
|
156 |
#'
|
|
157 |
#' basic_table() %>%
|
|
158 |
#' split_cols_by("ARM") %>%
|
|
159 |
#' estimate_proportion(vars = "AVAL") %>%
|
|
160 |
#' build_table(df = dta_test)
|
|
161 |
#'
|
|
162 |
#' @export
|
|
163 |
estimate_proportion <- function(lyt, |
|
164 |
vars,
|
|
165 |
...,
|
|
166 |
show_labels = "hidden", |
|
167 |
table_names = vars, |
|
168 |
.stats = NULL, |
|
169 |
.formats = NULL, |
|
170 |
.labels = NULL, |
|
171 |
.indent_mods = NULL) { |
|
172 | 3x |
afun <- make_afun( |
173 | 3x |
a_proportion,
|
174 | 3x |
.stats = .stats, |
175 | 3x |
.formats = .formats, |
176 | 3x |
.labels = .labels, |
177 | 3x |
.indent_mods = .indent_mods |
178 |
)
|
|
179 | 3x |
analyze( |
180 | 3x |
lyt,
|
181 | 3x |
vars,
|
182 | 3x |
afun = afun, |
183 | 3x |
extra_args = list(...), |
184 | 3x |
show_labels = show_labels, |
185 | 3x |
table_names = table_names |
186 |
)
|
|
187 |
}
|
|
188 | ||
189 |
#' Helper Functions for Calculating Proportion Confidence Intervals
|
|
190 |
#'
|
|
191 |
#' @description `r lifecycle::badge("stable")`
|
|
192 |
#'
|
|
193 |
#' Functions to calculate different proportion confidence intervals for use in [estimate_proportion()].
|
|
194 |
#'
|
|
195 |
#' @inheritParams argument_convention
|
|
196 |
#' @inheritParams estimate_proportions
|
|
197 |
#'
|
|
198 |
#' @return Confidence interval of a proportion.
|
|
199 |
#'
|
|
200 |
#' @seealso [estimate_proportions], descriptive function [d_proportion()],
|
|
201 |
#' and helper functions [strata_normal_quantile()] and [update_weights_strat_wilson()].
|
|
202 |
#'
|
|
203 |
#' @name h_proportions
|
|
204 |
NULL
|
|
205 | ||
206 |
#' @describeIn h_proportions Calculates the Wilson interval by calling [stats::prop.test()].
|
|
207 |
#' Also referred to as Wilson score interval.
|
|
208 |
#'
|
|
209 |
#' @examples
|
|
210 |
#' rsp <- c(
|
|
211 |
#' TRUE, TRUE, TRUE, TRUE, TRUE,
|
|
212 |
#' FALSE, FALSE, FALSE, FALSE, FALSE
|
|
213 |
#' )
|
|
214 |
#' prop_wilson(rsp, conf_level = 0.9)
|
|
215 |
#'
|
|
216 |
#' @export
|
|
217 |
prop_wilson <- function(rsp, conf_level, correct = FALSE) { |
|
218 | 5x |
y <- stats::prop.test( |
219 | 5x |
sum(rsp), |
220 | 5x |
length(rsp), |
221 | 5x |
correct = correct, |
222 | 5x |
conf.level = conf_level |
223 |
)
|
|
224 | ||
225 | 5x |
as.numeric(y$conf.int) |
226 |
}
|
|
227 | ||
228 |
#' @describeIn h_proportions Calculates the stratified Wilson confidence
|
|
229 |
#' interval for unequal proportions as described in \insertCite{Yan2010-jt;textual}{tern}
|
|
230 |
#'
|
|
231 |
#' @param strata (`factor`)\cr variable with one level per stratum and same length as `rsp`.
|
|
232 |
#' @param weights (`numeric` or `NULL`)\cr weights for each level of the strata. If `NULL`, they are
|
|
233 |
#' estimated using the iterative algorithm proposed in \insertCite{Yan2010-jt;textual}{tern} that
|
|
234 |
#' minimizes the weighted squared length of the confidence interval.
|
|
235 |
#' @param max_iterations (`count`)\cr maximum number of iterations for the iterative procedure used
|
|
236 |
#' to find estimates of optimal weights.
|
|
237 |
#' @param correct (`flag`)\cr include the continuity correction. For further information, see for example
|
|
238 |
#' [stats::prop.test()].
|
|
239 |
#'
|
|
240 |
#' @references
|
|
241 |
#' \insertRef{Yan2010-jt}{tern}
|
|
242 |
#'
|
|
243 |
#' @examples
|
|
244 |
#' # Stratified Wilson confidence interval with unequal probabilities
|
|
245 |
#'
|
|
246 |
#' set.seed(1)
|
|
247 |
#' rsp <- sample(c(TRUE, FALSE), 100, TRUE)
|
|
248 |
#' strata_data <- data.frame(
|
|
249 |
#' "f1" = sample(c("a", "b"), 100, TRUE),
|
|
250 |
#' "f2" = sample(c("x", "y", "z"), 100, TRUE),
|
|
251 |
#' stringsAsFactors = TRUE
|
|
252 |
#' )
|
|
253 |
#' strata <- interaction(strata_data)
|
|
254 |
#' n_strata <- ncol(table(rsp, strata)) # Number of strata
|
|
255 |
#'
|
|
256 |
#' prop_strat_wilson(
|
|
257 |
#' rsp = rsp, strata = strata,
|
|
258 |
#' conf_level = 0.90
|
|
259 |
#' )
|
|
260 |
#'
|
|
261 |
#' # Not automatic setting of weights
|
|
262 |
#' prop_strat_wilson(
|
|
263 |
#' rsp = rsp, strata = strata,
|
|
264 |
#' weights = rep(1 / n_strata, n_strata),
|
|
265 |
#' conf_level = 0.90
|
|
266 |
#' )
|
|
267 |
#'
|
|
268 |
#' @export
|
|
269 |
prop_strat_wilson <- function(rsp, |
|
270 |
strata,
|
|
271 |
weights = NULL, |
|
272 |
conf_level = 0.95, |
|
273 |
max_iterations = NULL, |
|
274 |
correct = FALSE) { |
|
275 | 20x |
checkmate::assert_logical(rsp, any.missing = FALSE) |
276 | 20x |
checkmate::assert_factor(strata, len = length(rsp)) |
277 | 20x |
assert_proportion_value(conf_level) |
278 | ||
279 | 20x |
tbl <- table(rsp, strata) |
280 | 20x |
n_strata <- length(unique(strata)) |
281 | ||
282 |
# Checking the weights and maximum number of iterations.
|
|
283 | 20x |
do_iter <- FALSE |
284 | 20x |
if (is.null(weights)) { |
285 | 6x |
weights <- rep(1 / n_strata, n_strata) # Initialization for iterative procedure |
286 | 6x |
do_iter <- TRUE |
287 | ||
288 |
# Iteration parameters
|
|
289 | 2x |
if (is.null(max_iterations)) max_iterations <- 10 |
290 | 6x |
checkmate::assert_int(max_iterations, na.ok = FALSE, null.ok = FALSE, lower = 1) |
291 |
}
|
|
292 | 20x |
checkmate::assert_numeric(weights, lower = 0, upper = 1, any.missing = FALSE, len = n_strata) |
293 | 20x |
sum_weights <- checkmate::assert_int(sum(weights)) |
294 | ! |
if (as.integer(sum_weights + 0.5) != 1L) stop("Sum of weights must be 1L.") |
295 | ||
296 | ||
297 | 20x |
xs <- tbl["TRUE", ] |
298 | 20x |
ns <- colSums(tbl) |
299 | 20x |
use_stratum <- (ns > 0) |
300 | 20x |
ns <- ns[use_stratum] |
301 | 20x |
xs <- xs[use_stratum] |
302 | 20x |
ests <- xs / ns |
303 | 20x |
vars <- ests * (1 - ests) / ns |
304 | ||
305 | 20x |
strata_qnorm <- strata_normal_quantile(vars, weights, conf_level) |
306 | ||
307 |
# Iterative setting of weights if they were not set externally
|
|
308 | 20x |
weights_new <- if (do_iter) { |
309 | 6x |
update_weights_strat_wilson(vars, strata_qnorm, weights, ns, max_iterations, conf_level)$weights |
310 |
} else { |
|
311 | 14x |
weights
|
312 |
}
|
|
313 | ||
314 | 20x |
strata_conf_level <- 2 * stats::pnorm(strata_qnorm) - 1 |
315 | ||
316 | 20x |
ci_by_strata <- Map( |
317 | 20x |
function(x, n) { |
318 |
# Classic Wilson's confidence interval
|
|
319 | 139x |
suppressWarnings(stats::prop.test(x, n, correct = correct, conf.level = strata_conf_level)$conf.int) |
320 |
},
|
|
321 | 20x |
x = xs, |
322 | 20x |
n = ns |
323 |
)
|
|
324 | 20x |
lower_by_strata <- sapply(ci_by_strata, "[", 1L) |
325 | 20x |
upper_by_strata <- sapply(ci_by_strata, "[", 2L) |
326 | ||
327 | 20x |
lower <- sum(weights_new * lower_by_strata) |
328 | 20x |
upper <- sum(weights_new * upper_by_strata) |
329 | ||
330 |
# Return values
|
|
331 | 20x |
if (do_iter) { |
332 | 6x |
list( |
333 | 6x |
conf_int = c( |
334 | 6x |
lower = lower, |
335 | 6x |
upper = upper |
336 |
),
|
|
337 | 6x |
weights = weights_new |
338 |
)
|
|
339 |
} else { |
|
340 | 14x |
list( |
341 | 14x |
conf_int = c( |
342 | 14x |
lower = lower, |
343 | 14x |
upper = upper |
344 |
)
|
|
345 |
)
|
|
346 |
}
|
|
347 |
}
|
|
348 | ||
349 |
#' @describeIn h_proportions Calculates the Clopper-Pearson interval by calling [stats::binom.test()].
|
|
350 |
#' Also referred to as the `exact` method.
|
|
351 |
#'
|
|
352 |
#' @examples
|
|
353 |
#' prop_clopper_pearson(rsp, conf_level = .95)
|
|
354 |
#'
|
|
355 |
#' @export
|
|
356 |
prop_clopper_pearson <- function(rsp, |
|
357 |
conf_level) { |
|
358 | 1x |
y <- stats::binom.test( |
359 | 1x |
x = sum(rsp), |
360 | 1x |
n = length(rsp), |
361 | 1x |
conf.level = conf_level |
362 |
)
|
|
363 | 1x |
as.numeric(y$conf.int) |
364 |
}
|
|
365 | ||
366 |
#' @describeIn h_proportions Calculates the Wald interval by following the usual textbook definition
|
|
367 |
#' for a single proportion confidence interval using the normal approximation.
|
|
368 |
#'
|
|
369 |
#' @param correct (`flag`)\cr apply continuity correction.
|
|
370 |
#'
|
|
371 |
#' @examples
|
|
372 |
#' prop_wald(rsp, conf_level = 0.95)
|
|
373 |
#' prop_wald(rsp, conf_level = 0.95, correct = TRUE)
|
|
374 |
#'
|
|
375 |
#' @export
|
|
376 |
prop_wald <- function(rsp, conf_level, correct = FALSE) { |
|
377 | 122x |
n <- length(rsp) |
378 | 122x |
p_hat <- mean(rsp) |
379 | 122x |
z <- stats::qnorm((1 + conf_level) / 2) |
380 | 122x |
q_hat <- 1 - p_hat |
381 | 122x |
correct <- if (correct) 1 / (2 * n) else 0 |
382 | ||
383 | 122x |
err <- z * sqrt(p_hat * q_hat) / sqrt(n) + correct |
384 | 122x |
l_ci <- max(0, p_hat - err) |
385 | 122x |
u_ci <- min(1, p_hat + err) |
386 | ||
387 | 122x |
c(l_ci, u_ci) |
388 |
}
|
|
389 | ||
390 |
#' @describeIn h_proportions Calculates the `Agresti-Coull` interval (created by `Alan Agresti` and `Brent Coull`) by
|
|
391 |
#' (for 95% CI) adding two successes and two failures to the data and then using the Wald formula to construct a CI.
|
|
392 |
#'
|
|
393 |
#' @examples
|
|
394 |
#' prop_agresti_coull(rsp, conf_level = 0.95)
|
|
395 |
#'
|
|
396 |
#' @export
|
|
397 |
prop_agresti_coull <- function(rsp, conf_level) { |
|
398 | 2x |
n <- length(rsp) |
399 | 2x |
x_sum <- sum(rsp) |
400 | 2x |
z <- stats::qnorm((1 + conf_level) / 2) |
401 | ||
402 |
# Add here both z^2 / 2 successes and failures.
|
|
403 | 2x |
x_sum_tilde <- x_sum + z^2 / 2 |
404 | 2x |
n_tilde <- n + z^2 |
405 | ||
406 |
# Then proceed as with the Wald interval.
|
|
407 | 2x |
p_tilde <- x_sum_tilde / n_tilde |
408 | 2x |
q_tilde <- 1 - p_tilde |
409 | 2x |
err <- z * sqrt(p_tilde * q_tilde) / sqrt(n_tilde) |
410 | 2x |
l_ci <- max(0, p_tilde - err) |
411 | 2x |
u_ci <- min(1, p_tilde + err) |
412 | ||
413 | 2x |
c(l_ci, u_ci) |
414 |
}
|
|
415 | ||
416 |
#' @describeIn h_proportions Calculates the Jeffreys interval, an equal-tailed interval based on the
|
|
417 |
#' non-informative Jeffreys prior for a binomial proportion.
|
|
418 |
#'
|
|
419 |
#' @examples
|
|
420 |
#' prop_jeffreys(rsp, conf_level = 0.95)
|
|
421 |
#'
|
|
422 |
#' @export
|
|
423 |
prop_jeffreys <- function(rsp, |
|
424 |
conf_level) { |
|
425 | 4x |
n <- length(rsp) |
426 | 4x |
x_sum <- sum(rsp) |
427 | ||
428 | 4x |
alpha <- 1 - conf_level |
429 | 4x |
l_ci <- ifelse( |
430 | 4x |
x_sum == 0, |
431 | 4x |
0,
|
432 | 4x |
stats::qbeta(alpha / 2, x_sum + 0.5, n - x_sum + 0.5) |
433 |
)
|
|
434 | ||
435 | 4x |
u_ci <- ifelse( |
436 | 4x |
x_sum == n, |
437 | 4x |
1,
|
438 | 4x |
stats::qbeta(1 - alpha / 2, x_sum + 0.5, n - x_sum + 0.5) |
439 |
)
|
|
440 | ||
441 | 4x |
c(l_ci, u_ci) |
442 |
}
|
|
443 | ||
444 |
#' Description of the Proportion Summary
|
|
445 |
#'
|
|
446 |
#' @description `r lifecycle::badge("stable")`
|
|
447 |
#'
|
|
448 |
#' This is a helper function that describes the analysis in [s_proportion()].
|
|
449 |
#'
|
|
450 |
#' @inheritParams s_proportion
|
|
451 |
#' @param long (`flag`)\cr whether a long or a short (default) description is required.
|
|
452 |
#'
|
|
453 |
#' @return String describing the analysis.
|
|
454 |
#'
|
|
455 |
#' @export
|
|
456 |
d_proportion <- function(conf_level, |
|
457 |
method,
|
|
458 |
long = FALSE) { |
|
459 | 137x |
label <- paste0(conf_level * 100, "% CI") |
460 | ||
461 | ! |
if (long) label <- paste(label, "for Response Rates") |
462 | ||
463 | 137x |
method_part <- switch(method, |
464 | 137x |
"clopper-pearson" = "Clopper-Pearson", |
465 | 137x |
"waldcc" = "Wald, with correction", |
466 | 137x |
"wald" = "Wald, without correction", |
467 | 137x |
"wilson" = "Wilson, without correction", |
468 | 137x |
"strat_wilson" = "Stratified Wilson, without correction", |
469 | 137x |
"wilsonc" = "Wilson, with correction", |
470 | 137x |
"strat_wilsonc" = "Stratified Wilson, with correction", |
471 | 137x |
"agresti-coull" = "Agresti-Coull", |
472 | 137x |
"jeffreys" = "Jeffreys", |
473 | 137x |
stop(paste(method, "does not have a description")) |
474 |
)
|
|
475 | ||
476 | 137x |
paste0(label, " (", method_part, ")") |
477 |
}
|
|
478 | ||
479 |
#' Helper Function for the Estimation of Stratified Quantiles
|
|
480 |
#'
|
|
481 |
#' @description `r lifecycle::badge("stable")`
|
|
482 |
#'
|
|
483 |
#' This function wraps the estimation of stratified percentiles when we assume
|
|
484 |
#' the approximation for large numbers. This is necessary only in the case
|
|
485 |
#' proportions for each strata are unequal.
|
|
486 |
#'
|
|
487 |
#' @inheritParams argument_convention
|
|
488 |
#' @inheritParams prop_strat_wilson
|
|
489 |
#'
|
|
490 |
#' @return Stratified quantile.
|
|
491 |
#'
|
|
492 |
#' @seealso [prop_strat_wilson()]
|
|
493 |
#'
|
|
494 |
#' @examples
|
|
495 |
#' strata_data <- table(data.frame(
|
|
496 |
#' "f1" = sample(c(TRUE, FALSE), 100, TRUE),
|
|
497 |
#' "f2" = sample(c("x", "y", "z"), 100, TRUE),
|
|
498 |
#' stringsAsFactors = TRUE
|
|
499 |
#' ))
|
|
500 |
#' ns <- colSums(strata_data)
|
|
501 |
#' ests <- strata_data["TRUE", ] / ns
|
|
502 |
#' vars <- ests * (1 - ests) / ns
|
|
503 |
#' weights <- rep(1 / length(ns), length(ns))
|
|
504 |
#' strata_normal_quantile(vars, weights, 0.95)
|
|
505 |
#'
|
|
506 |
#' @export
|
|
507 |
strata_normal_quantile <- function(vars, weights, conf_level) { |
|
508 | 41x |
summands <- weights^2 * vars |
509 |
# Stratified quantile
|
|
510 | 41x |
sqrt(sum(summands)) / sum(sqrt(summands)) * stats::qnorm((1 + conf_level) / 2) |
511 |
}
|
|
512 | ||
513 |
#' Helper Function for the Estimation of Weights for `prop_strat_wilson`
|
|
514 |
#'
|
|
515 |
#' @description `r lifecycle::badge("stable")`
|
|
516 |
#'
|
|
517 |
#' This function wraps the iteration procedure that allows you to estimate
|
|
518 |
#' the weights for each proportional strata. This assumes to minimize the
|
|
519 |
#' weighted squared length of the confidence interval.
|
|
520 |
#'
|
|
521 |
#' @inheritParams prop_strat_wilson
|
|
522 |
#' @param vars (`numeric`)\cr normalized proportions for each strata.
|
|
523 |
#' @param strata_qnorm (`numeric`)\cr initial estimation with identical weights of the quantiles.
|
|
524 |
#' @param initial_weights (`numeric`)\cr initial weights used to calculate `strata_qnorm`. This can
|
|
525 |
#' be optimized in the future if we need to estimate better initial weights.
|
|
526 |
#' @param n_per_strata (`numeric`)\cr number of elements in each strata.
|
|
527 |
#' @param max_iterations (`count`)\cr maximum number of iterations to be tried. Convergence is always checked.
|
|
528 |
#' @param tol (`number`)\cr tolerance threshold for convergence.
|
|
529 |
#'
|
|
530 |
#' @return A `list` of 3 elements: `n_it`, `weights`, and `diff_v`.
|
|
531 |
#'
|
|
532 |
#' @seealso For references and details see [prop_strat_wilson()].
|
|
533 |
#'
|
|
534 |
#' @examples
|
|
535 |
#' vs <- c(0.011, 0.013, 0.012, 0.014, 0.017, 0.018)
|
|
536 |
#' sq <- 0.674
|
|
537 |
#' ws <- rep(1 / length(vs), length(vs))
|
|
538 |
#' ns <- c(22, 18, 17, 17, 14, 12)
|
|
539 |
#'
|
|
540 |
#' update_weights_strat_wilson(vs, sq, ws, ns, 100, 0.95, 0.001)
|
|
541 |
#'
|
|
542 |
#' @export
|
|
543 |
update_weights_strat_wilson <- function(vars, |
|
544 |
strata_qnorm,
|
|
545 |
initial_weights,
|
|
546 |
n_per_strata,
|
|
547 |
max_iterations = 50, |
|
548 |
conf_level = 0.95, |
|
549 |
tol = 0.001) { |
|
550 | 8x |
it <- 0 |
551 | 8x |
diff_v <- NULL |
552 | ||
553 | 8x |
while (it < max_iterations) { |
554 | 19x |
it <- it + 1 |
555 | 19x |
weights_new_t <- (1 + strata_qnorm^2 / n_per_strata)^2 |
556 | 19x |
weights_new_b <- (vars + strata_qnorm^2 / (4 * n_per_strata^2)) |
557 | 19x |
weights_new <- weights_new_t / weights_new_b |
558 | 19x |
weights_new <- weights_new / sum(weights_new) |
559 | 19x |
strata_qnorm <- strata_normal_quantile(vars, weights_new, conf_level) |
560 | 19x |
diff_v <- c(diff_v, sum(abs(weights_new - initial_weights))) |
561 | 8x |
if (diff_v[length(diff_v)] < tol) break |
562 | 11x |
initial_weights <- weights_new |
563 |
}
|
|
564 | ||
565 | 8x |
if (it == max_iterations) { |
566 | ! |
warning("The heuristic to find weights did not converge with max_iterations = ", max_iterations) |
567 |
}
|
|
568 | ||
569 | 8x |
list( |
570 | 8x |
"n_it" = it, |
571 | 8x |
"weights" = weights_new, |
572 | 8x |
"diff_v" = diff_v |
573 |
)
|
|
574 |
}
|
1 |
#' Add Titles, Footnotes, Page Number, and a Bounding Box to a Grid Grob
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' This function is useful to label grid grobs (also `ggplot2`, and `lattice` plots)
|
|
6 |
#' with title, footnote, and page numbers.
|
|
7 |
#'
|
|
8 |
#' @inheritParams grid::grob
|
|
9 |
#' @param grob a grid grob object, optionally `NULL` if only a `grob` with the decoration should be shown.
|
|
10 |
#' @param titles vector of character strings. Vector elements are separated by a newline and strings are wrapped
|
|
11 |
#' according to the page width.
|
|
12 |
#' @param footnotes vector of character string. Same rules as for `titles`.
|
|
13 |
#' @param page string with page numeration, if `NULL` then no page number is displayed.
|
|
14 |
#' @param width_titles unit object
|
|
15 |
#' @param width_footnotes unit object
|
|
16 |
#' @param border boolean, whether a a border should be drawn around the plot or not.
|
|
17 |
#' @param margins unit object of length 4
|
|
18 |
#' @param padding unit object of length 4
|
|
19 |
#' @param outer_margins unit object of length 4
|
|
20 |
#' @param gp_titles a `gpar` object
|
|
21 |
#' @param gp_footnotes a `gpar` object
|
|
22 |
#'
|
|
23 |
#' @return A grid grob (`gTree`).
|
|
24 |
#'
|
|
25 |
#' @details The titles and footnotes will be ragged, i.e. each title will be wrapped individually.
|
|
26 |
#'
|
|
27 |
#' @examples
|
|
28 |
#' library(grid)
|
|
29 |
#'
|
|
30 |
#' titles <- c(
|
|
31 |
#' "Edgar Anderson's Iris Data",
|
|
32 |
#' paste(
|
|
33 |
#' "This famous (Fisher's or Anderson's) iris data set gives the measurements",
|
|
34 |
#' "in centimeters of the variables sepal length and width and petal length",
|
|
35 |
#' "and width, respectively, for 50 flowers from each of 3 species of iris."
|
|
36 |
#' )
|
|
37 |
#' )
|
|
38 |
#'
|
|
39 |
#' footnotes <- c(
|
|
40 |
#' "The species are Iris setosa, versicolor, and virginica.",
|
|
41 |
#' paste(
|
|
42 |
#' "iris is a data frame with 150 cases (rows) and 5 variables (columns) named",
|
|
43 |
#' "Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, and Species."
|
|
44 |
#' )
|
|
45 |
#' )
|
|
46 |
#'
|
|
47 |
#' ## empty plot
|
|
48 |
#' grid.newpage()
|
|
49 |
#'
|
|
50 |
#' grid.draw(
|
|
51 |
#' decorate_grob(
|
|
52 |
#' NULL,
|
|
53 |
#' titles = titles,
|
|
54 |
#' footnotes = footnotes,
|
|
55 |
#' page = "Page 4 of 10"
|
|
56 |
#' )
|
|
57 |
#' )
|
|
58 |
#'
|
|
59 |
#' # grid
|
|
60 |
#' p <- gTree(
|
|
61 |
#' children = gList(
|
|
62 |
#' rectGrob(),
|
|
63 |
#' xaxisGrob(),
|
|
64 |
#' yaxisGrob(),
|
|
65 |
#' textGrob("Sepal.Length", y = unit(-4, "lines")),
|
|
66 |
#' textGrob("Petal.Length", x = unit(-3.5, "lines"), rot = 90),
|
|
67 |
#' pointsGrob(iris$Sepal.Length, iris$Petal.Length, gp = gpar(col = iris$Species), pch = 16)
|
|
68 |
#' ),
|
|
69 |
#' vp = vpStack(plotViewport(), dataViewport(xData = iris$Sepal.Length, yData = iris$Petal.Length))
|
|
70 |
#' )
|
|
71 |
#' grid.newpage()
|
|
72 |
#' grid.draw(p)
|
|
73 |
#'
|
|
74 |
#' grid.newpage()
|
|
75 |
#' grid.draw(
|
|
76 |
#' decorate_grob(
|
|
77 |
#' grob = p,
|
|
78 |
#' titles = titles,
|
|
79 |
#' footnotes = footnotes,
|
|
80 |
#' page = "Page 6 of 129"
|
|
81 |
#' )
|
|
82 |
#' )
|
|
83 |
#'
|
|
84 |
#' ## with ggplot2
|
|
85 |
#' library(ggplot2)
|
|
86 |
#'
|
|
87 |
#' p_gg <- ggplot2::ggplot(iris, aes(Sepal.Length, Sepal.Width, col = Species)) +
|
|
88 |
#' ggplot2::geom_point()
|
|
89 |
#' p_gg
|
|
90 |
#' p <- ggplotGrob(p_gg)
|
|
91 |
#' grid.newpage()
|
|
92 |
#' grid.draw(
|
|
93 |
#' decorate_grob(
|
|
94 |
#' grob = p,
|
|
95 |
#' titles = titles,
|
|
96 |
#' footnotes = footnotes,
|
|
97 |
#' page = "Page 6 of 129"
|
|
98 |
#' )
|
|
99 |
#' )
|
|
100 |
#'
|
|
101 |
#' ## with lattice
|
|
102 |
#' library(lattice)
|
|
103 |
#'
|
|
104 |
#' xyplot(Sepal.Length ~ Petal.Length, data = iris, col = iris$Species)
|
|
105 |
#' p <- grid.grab()
|
|
106 |
#' grid.newpage()
|
|
107 |
#' grid.draw(
|
|
108 |
#' decorate_grob(
|
|
109 |
#' grob = p,
|
|
110 |
#' titles = titles,
|
|
111 |
#' footnotes = footnotes,
|
|
112 |
#' page = "Page 6 of 129"
|
|
113 |
#' )
|
|
114 |
#' )
|
|
115 |
#'
|
|
116 |
#' # with gridExtra - no borders
|
|
117 |
#' library(gridExtra)
|
|
118 |
#' grid.newpage()
|
|
119 |
#' grid.draw(
|
|
120 |
#' decorate_grob(
|
|
121 |
#' tableGrob(
|
|
122 |
#' head(mtcars)
|
|
123 |
#' ),
|
|
124 |
#' titles = "title",
|
|
125 |
#' footnotes = "footnote",
|
|
126 |
#' border = FALSE
|
|
127 |
#' )
|
|
128 |
#' )
|
|
129 |
#'
|
|
130 |
#' @export
|
|
131 |
decorate_grob <- function(grob, |
|
132 |
titles,
|
|
133 |
footnotes,
|
|
134 |
page = "", |
|
135 |
width_titles = grid::unit(1, "npc") - grid::stringWidth(page), |
|
136 |
width_footnotes = grid::unit(1, "npc") - grid::stringWidth(page), |
|
137 |
border = TRUE, |
|
138 |
margins = grid::unit(c(1, 0, 1, 0), "lines"), |
|
139 |
padding = grid::unit(rep(1, 4), "lines"), |
|
140 |
outer_margins = grid::unit(c(2, 1.5, 3, 1.5), "cm"), |
|
141 |
gp_titles = grid::gpar(), |
|
142 |
gp_footnotes = grid::gpar(fontsize = 8), |
|
143 |
name = NULL, |
|
144 |
gp = grid::gpar(), |
|
145 |
vp = NULL) { |
|
146 | 8x |
st_titles <- split_text_grob( |
147 | 8x |
titles,
|
148 | 8x |
x = 0, y = 1, |
149 | 8x |
just = c("left", "top"), |
150 | 8x |
width = width_titles, |
151 | 8x |
vp = grid::viewport(layout.pos.row = 1, layout.pos.col = 1), |
152 | 8x |
gp = gp_titles |
153 |
)
|
|
154 | ||
155 | 8x |
st_footnotes <- split_text_grob( |
156 | 8x |
footnotes,
|
157 | 8x |
x = 0, y = 1, |
158 | 8x |
just = c("left", "top"), |
159 | 8x |
width = width_footnotes, |
160 | 8x |
vp = grid::viewport(layout.pos.row = 3, layout.pos.col = 1), |
161 | 8x |
gp = gp_footnotes |
162 |
)
|
|
163 | ||
164 | 8x |
grid::gTree( |
165 | 8x |
grob = grob, |
166 | 8x |
titles = titles, |
167 | 8x |
footnotes = footnotes, |
168 | 8x |
page = page, |
169 | 8x |
width_titles = width_titles, |
170 | 8x |
width_footnotes = width_footnotes, |
171 | 8x |
border = border, |
172 | 8x |
margins = margins, |
173 | 8x |
padding = padding, |
174 | 8x |
outer_margins = outer_margins, |
175 | 8x |
gp_titles = gp_titles, |
176 | 8x |
gp_footnotes = gp_footnotes, |
177 | 8x |
children = grid::gList( |
178 | 8x |
grid::gTree( |
179 | 8x |
children = grid::gList( |
180 | 8x |
st_titles,
|
181 | 8x |
grid::gTree( |
182 | 8x |
children = grid::gList( |
183 | 8x |
if (border) grid::rectGrob(), |
184 | 8x |
grid::gTree( |
185 | 8x |
children = grid::gList( |
186 | 8x |
grob
|
187 |
),
|
|
188 | 8x |
vp = grid::plotViewport(margins = padding) |
189 |
)
|
|
190 |
),
|
|
191 | 8x |
vp = grid::vpStack( |
192 | 8x |
grid::viewport(layout.pos.row = 2, layout.pos.col = 1), |
193 | 8x |
grid::plotViewport(margins = margins) |
194 |
)
|
|
195 |
),
|
|
196 | 8x |
st_footnotes,
|
197 | 8x |
grid::textGrob( |
198 | 8x |
page,
|
199 | 8x |
x = 1, y = 0, |
200 | 8x |
just = c("right", "bottom"), |
201 | 8x |
vp = grid::viewport(layout.pos.row = 3, layout.pos.col = 1), |
202 | 8x |
gp = gp_footnotes |
203 |
)
|
|
204 |
),
|
|
205 | 8x |
childrenvp = NULL, |
206 | 8x |
name = "titles_grob_footnotes", |
207 | 8x |
vp = grid::vpStack( |
208 | 8x |
grid::plotViewport(margins = outer_margins), |
209 | 8x |
grid::viewport( |
210 | 8x |
layout = grid::grid.layout( |
211 | 8x |
nrow = 3, ncol = 1, |
212 | 8x |
heights = grid::unit.c( |
213 | 8x |
grid::grobHeight(st_titles), |
214 | 8x |
grid::unit(1, "null"), |
215 | 8x |
grid::grobHeight(st_footnotes) |
216 |
)
|
|
217 |
)
|
|
218 |
)
|
|
219 |
)
|
|
220 |
)
|
|
221 |
),
|
|
222 | 8x |
name = name, |
223 | 8x |
gp = gp, |
224 | 8x |
vp = vp, |
225 | 8x |
cl = "decoratedGrob" |
226 |
)
|
|
227 |
}
|
|
228 | ||
229 |
#' @importFrom grid validDetails
|
|
230 |
#' @noRd
|
|
231 |
validDetails.decoratedGrob <- function(x) { |
|
232 | ! |
checkmate::assert_character(x$titles) |
233 | ! |
checkmate::assert_character(x$footnotes) |
234 | ||
235 | ! |
if (!is.null(x$grob)) { |
236 | ! |
checkmate::assert_true(grid::is.grob(x$grob)) |
237 |
}
|
|
238 | ! |
if (length(x$page) == 1) { |
239 | ! |
checkmate::assert_character(x$page) |
240 |
}
|
|
241 | ! |
if (!grid::is.unit(x$outer_margins)) { |
242 | ! |
checkmate::assert_vector(x$outer_margins, len = 4) |
243 |
}
|
|
244 | ! |
if (!grid::is.unit(x$margins)) { |
245 | ! |
checkmate::assert_vector(x$margins, len = 4) |
246 |
}
|
|
247 | ! |
if (!grid::is.unit(x$padding)) { |
248 | ! |
checkmate::assert_vector(x$padding, len = 4) |
249 |
}
|
|
250 | ||
251 | ! |
x
|
252 |
}
|
|
253 | ||
254 |
#' @importFrom grid widthDetails
|
|
255 |
#' @noRd
|
|
256 |
widthDetails.decoratedGrob <- function(x) { |
|
257 | ! |
grid::unit(1, "null") |
258 |
}
|
|
259 | ||
260 |
#' @importFrom grid heightDetails
|
|
261 |
#' @noRd
|
|
262 |
heightDetails.decoratedGrob <- function(x) { |
|
263 | ! |
grid::unit(1, "null") |
264 |
}
|
|
265 | ||
266 |
# Adapted from Paul Murell R Graphics 2nd Edition
|
|
267 |
# https://www.stat.auckland.ac.nz/~paul/RG2e/interactgrid-splittext.R
|
|
268 |
split_string <- function(text, width) { |
|
269 | 17x |
strings <- strsplit(text, " ") |
270 | 17x |
out_string <- NA |
271 | 17x |
for (string_i in seq_along(strings)) { |
272 | 17x |
newline_str <- strings[[string_i]] |
273 | 6x |
if (length(newline_str) == 0) newline_str <- "" |
274 | 17x |
if (is.na(out_string[string_i])) { |
275 | 17x |
out_string[string_i] <- newline_str[[1]][[1]] |
276 | 17x |
linewidth <- grid::stringWidth(out_string[string_i]) |
277 |
}
|
|
278 | 17x |
gapwidth <- grid::stringWidth(" ") |
279 | 17x |
availwidth <- as.numeric(width) |
280 | 17x |
if (length(newline_str) > 1) { |
281 | 5x |
for (i in seq(2, length(newline_str))) { |
282 | 27x |
width_i <- grid::stringWidth(newline_str[i]) |
283 | 27x |
if (grid::convertWidth(linewidth + gapwidth + width_i, grid::unitType(width), valueOnly = TRUE) < availwidth) { |
284 | 25x |
sep <- " " |
285 | 25x |
linewidth <- linewidth + gapwidth + width_i |
286 |
} else { |
|
287 | 2x |
sep <- "\n" |
288 | 2x |
linewidth <- width_i |
289 |
}
|
|
290 | 27x |
out_string[string_i] <- paste(out_string[string_i], newline_str[i], sep = sep) |
291 |
}
|
|
292 |
}
|
|
293 |
}
|
|
294 | 17x |
paste(out_string, collapse = "\n") |
295 |
}
|
|
296 | ||
297 |
#' Split Text According To Available Text Width
|
|
298 |
#'
|
|
299 |
#' Dynamically wrap text.
|
|
300 |
#'
|
|
301 |
#' @inheritParams grid::grid.text
|
|
302 |
#' @param text character string
|
|
303 |
#' @param width a unit object specifying max width of text
|
|
304 |
#'
|
|
305 |
#' @return A text grob.
|
|
306 |
#'
|
|
307 |
#' @details This code is taken from `R Graphics by Paul Murell, 2nd edition`
|
|
308 |
#'
|
|
309 |
#' @keywords internal
|
|
310 |
split_text_grob <- function(text, |
|
311 |
x = grid::unit(0.5, "npc"), |
|
312 |
y = grid::unit(0.5, "npc"), |
|
313 |
width = grid::unit(1, "npc"), |
|
314 |
just = "centre", |
|
315 |
hjust = NULL, |
|
316 |
vjust = NULL, |
|
317 |
default.units = "npc", # nolint |
|
318 |
name = NULL, |
|
319 |
gp = grid::gpar(), |
|
320 |
vp = NULL) { |
|
321 | 16x |
if (!grid::is.unit(x)) x <- grid::unit(x, default.units) |
322 | 16x |
if (!grid::is.unit(y)) y <- grid::unit(y, default.units) |
323 | ! |
if (!grid::is.unit(width)) width <- grid::unit(width, default.units) |
324 | ! |
if (grid::unitType(x) %in% c("sum", "min", "max")) x <- grid::convertUnit(x, default.units) |
325 | ! |
if (grid::unitType(y) %in% c("sum", "min", "max")) y <- grid::convertUnit(y, default.units) |
326 | 16x |
if (grid::unitType(width) %in% c("sum", "min", "max")) width <- grid::convertUnit(width, default.units) |
327 | ||
328 |
## if it is a fixed unit then we do not need to recalculate when viewport resized
|
|
329 | 16x |
if (!inherits(width, "unit.arithmetic") && |
330 | 16x |
!is.null(attr(width, "unit")) && |
331 | 16x |
attr(width, "unit") %in% c("cm", "inches", "mm", "points", "picas", "bigpts", "dida", "cicero", "scaledpts")) { |
332 | ! |
attr(text, "fixed_text") <- paste(vapply(text, split_string, character(1), width = width), collapse = "\n") |
333 |
}
|
|
334 | ||
335 | 16x |
grid::grid.text( |
336 | 16x |
label = split_string(text, width), |
337 | 16x |
x = x, y = y, |
338 | 16x |
just = just, |
339 | 16x |
hjust = hjust, |
340 | 16x |
vjust = vjust, |
341 | 16x |
rot = 0, |
342 | 16x |
check.overlap = FALSE, |
343 | 16x |
name = name, |
344 | 16x |
gp = gp, |
345 | 16x |
vp = vp, |
346 | 16x |
draw = FALSE |
347 |
)
|
|
348 |
}
|
|
349 | ||
350 |
#' @importFrom grid validDetails
|
|
351 |
#' @noRd
|
|
352 |
validDetails.dynamicSplitText <- function(x) { |
|
353 | ! |
checkmate::assert_character(x$text) |
354 | ! |
checkmate::assert_true(grid::is.unit(x$width)) |
355 | ! |
checkmate::assert_vector(x$width, len = 1) |
356 | ! |
x
|
357 |
}
|
|
358 | ||
359 |
#' @importFrom grid heightDetails
|
|
360 |
#' @noRd
|
|
361 |
heightDetails.dynamicSplitText <- function(x) { |
|
362 | ! |
txt <- if (!is.null(attr(x$text, "fixed_text"))) { |
363 | ! |
attr(x$text, "fixed_text") |
364 |
} else { |
|
365 | ! |
paste(vapply(x$text, split_string, character(1), width = x$width), collapse = "\n") |
366 |
}
|
|
367 | ! |
grid::stringHeight(txt) |
368 |
}
|
|
369 | ||
370 |
#' @importFrom grid widthDetails
|
|
371 |
#' @noRd
|
|
372 |
widthDetails.dynamicSplitText <- function(x) { |
|
373 | ! |
x$width |
374 |
}
|
|
375 | ||
376 |
#' @importFrom grid drawDetails
|
|
377 |
#' @noRd
|
|
378 |
drawDetails.dynamicSplitText <- function(x, recording) { |
|
379 | ! |
txt <- if (!is.null(attr(x$text, "fixed_text"))) { |
380 | ! |
attr(x$text, "fixed_text") |
381 |
} else { |
|
382 | ! |
paste(vapply(x$text, split_string, character(1), width = x$width), collapse = "\n") |
383 |
}
|
|
384 | ||
385 | ! |
x$width <- NULL |
386 | ! |
x$label <- txt |
387 | ! |
x$text <- NULL |
388 | ! |
class(x) <- c("text", class(x)[-1]) |
389 | ||
390 | ! |
grid::grid.draw(x) |
391 |
}
|
|
392 | ||
393 |
#' Update Page Number
|
|
394 |
#'
|
|
395 |
#' Automatically updates page number.
|
|
396 |
#'
|
|
397 |
#' @param npages number of pages in total
|
|
398 |
#' @param ... passed on to [decorate_grob()]
|
|
399 |
#'
|
|
400 |
#' @return Closure that increments the page number.
|
|
401 |
#'
|
|
402 |
#' @keywords internal
|
|
403 |
decorate_grob_factory <- function(npages, ...) { |
|
404 | 2x |
current_page <- 0 |
405 | 2x |
function(grob) { |
406 | 7x |
current_page <<- current_page + 1 |
407 | 7x |
if (current_page > npages) { |
408 | 1x |
stop(paste("current page is", current_page, "but max.", npages, "specified.")) |
409 |
}
|
|
410 | 6x |
decorate_grob(grob = grob, page = paste("Page", current_page, "of", npages), ...) |
411 |
}
|
|
412 |
}
|
|
413 | ||
414 |
#' Decorate Set of `grobs` and Add Page Numbering
|
|
415 |
#'
|
|
416 |
#' @description `r lifecycle::badge("stable")`
|
|
417 |
#'
|
|
418 |
#' Note that this uses the [decorate_grob_factory()] function.
|
|
419 |
#'
|
|
420 |
#' @param grobs a list of grid grobs
|
|
421 |
#' @param ... arguments passed on to [decorate_grob()].
|
|
422 |
#'
|
|
423 |
#' @return A decorated grob.
|
|
424 |
#'
|
|
425 |
#' @examples
|
|
426 |
#' library(ggplot2)
|
|
427 |
#' library(grid)
|
|
428 |
#' g <- with(data = iris, {
|
|
429 |
#' list(
|
|
430 |
#' ggplot2::ggplotGrob(
|
|
431 |
#' ggplot2::ggplot(mapping = aes(Sepal.Length, Sepal.Width, col = Species)) +
|
|
432 |
#' ggplot2::geom_point()
|
|
433 |
#' ),
|
|
434 |
#' ggplot2::ggplotGrob(
|
|
435 |
#' ggplot2::ggplot(mapping = aes(Sepal.Length, Petal.Length, col = Species)) +
|
|
436 |
#' ggplot2::geom_point()
|
|
437 |
#' ),
|
|
438 |
#' ggplot2::ggplotGrob(
|
|
439 |
#' ggplot2::ggplot(mapping = aes(Sepal.Length, Petal.Width, col = Species)) +
|
|
440 |
#' ggplot2::geom_point()
|
|
441 |
#' ),
|
|
442 |
#' ggplot2::ggplotGrob(
|
|
443 |
#' ggplot2::ggplot(mapping = aes(Sepal.Width, Petal.Length, col = Species)) +
|
|
444 |
#' ggplot2::geom_point()
|
|
445 |
#' ),
|
|
446 |
#' ggplot2::ggplotGrob(
|
|
447 |
#' ggplot2::ggplot(mapping = aes(Sepal.Width, Petal.Width, col = Species)) +
|
|
448 |
#' ggplot2::geom_point()
|
|
449 |
#' ),
|
|
450 |
#' ggplot2::ggplotGrob(
|
|
451 |
#' ggplot2::ggplot(mapping = aes(Petal.Length, Petal.Width, col = Species)) +
|
|
452 |
#' ggplot2::geom_point()
|
|
453 |
#' )
|
|
454 |
#' )
|
|
455 |
#' })
|
|
456 |
#' lg <- decorate_grob_set(grobs = g, titles = "Hello\nOne\nTwo\nThree", footnotes = "")
|
|
457 |
#'
|
|
458 |
#' draw_grob(lg[[1]])
|
|
459 |
#' draw_grob(lg[[2]])
|
|
460 |
#' draw_grob(lg[[6]])
|
|
461 |
#'
|
|
462 |
#' @export
|
|
463 |
decorate_grob_set <- function(grobs, ...) { |
|
464 | 1x |
n <- length(grobs) |
465 | 1x |
lgf <- decorate_grob_factory(npages = n, ...) |
466 | 1x |
lapply(grobs, lgf) |
467 |
}
|
1 |
#' Helper Functions for Multivariate Logistic Regression
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Helper functions used in calculations for logistic regression.
|
|
6 |
#'
|
|
7 |
#' @inheritParams argument_convention
|
|
8 |
#' @param fit_glm (`glm`)\cr logistic regression model fitted by [stats::glm()] with "binomial" family.
|
|
9 |
#' Limited functionality is also available for conditional logistic regression models fitted by
|
|
10 |
#' [survival::clogit()], currently this is used only by [extract_rsp_biomarkers()].
|
|
11 |
#' @param x (`string` or `character`)\cr a variable or interaction term in `fit_glm` (depending on the
|
|
12 |
#' helper function).
|
|
13 |
#'
|
|
14 |
#' @examples
|
|
15 |
#' library(dplyr)
|
|
16 |
#' library(broom)
|
|
17 |
#'
|
|
18 |
#' adrs_f <- tern_ex_adrs %>%
|
|
19 |
#' filter(PARAMCD == "BESRSPI") %>%
|
|
20 |
#' filter(RACE %in% c("ASIAN", "WHITE", "BLACK OR AFRICAN AMERICAN")) %>%
|
|
21 |
#' mutate(
|
|
22 |
#' Response = case_when(AVALC %in% c("PR", "CR") ~ 1, TRUE ~ 0),
|
|
23 |
#' RACE = factor(RACE),
|
|
24 |
#' SEX = factor(SEX)
|
|
25 |
#' )
|
|
26 |
#' formatters::var_labels(adrs_f) <- c(formatters::var_labels(tern_ex_adrs), Response = "Response")
|
|
27 |
#' mod1 <- fit_logistic(
|
|
28 |
#' data = adrs_f,
|
|
29 |
#' variables = list(
|
|
30 |
#' response = "Response",
|
|
31 |
#' arm = "ARMCD",
|
|
32 |
#' covariates = c("AGE", "RACE")
|
|
33 |
#' )
|
|
34 |
#' )
|
|
35 |
#' mod2 <- fit_logistic(
|
|
36 |
#' data = adrs_f,
|
|
37 |
#' variables = list(
|
|
38 |
#' response = "Response",
|
|
39 |
#' arm = "ARMCD",
|
|
40 |
#' covariates = c("AGE", "RACE"),
|
|
41 |
#' interaction = "AGE"
|
|
42 |
#' )
|
|
43 |
#' )
|
|
44 |
#'
|
|
45 |
#' @name h_logistic_regression
|
|
46 |
NULL
|
|
47 | ||
48 |
#' @describeIn h_logistic_regression Helper function to extract interaction variable names from a fitted
|
|
49 |
#' model assuming only one interaction term.
|
|
50 |
#'
|
|
51 |
#' @return Vector of names of interaction variables.
|
|
52 |
#'
|
|
53 |
#' @export
|
|
54 |
h_get_interaction_vars <- function(fit_glm) { |
|
55 | 27x |
checkmate::assert_class(fit_glm, "glm") |
56 | 27x |
terms_name <- attr(stats::terms(fit_glm), "term.labels") |
57 | 27x |
terms_order <- attr(stats::terms(fit_glm), "order") |
58 | 27x |
interaction_term <- terms_name[terms_order == 2] |
59 | 27x |
checkmate::assert_string(interaction_term) |
60 | 27x |
strsplit(interaction_term, split = ":")[[1]] |
61 |
}
|
|
62 | ||
63 |
#' @describeIn h_logistic_regression Helper function to get the right coefficient name from the
|
|
64 |
#' interaction variable names and the given levels. The main value here is that the order
|
|
65 |
#' of first and second variable is checked in the `interaction_vars` input.
|
|
66 |
#'
|
|
67 |
#' @param interaction_vars (`character` of length 2)\cr interaction variable names.
|
|
68 |
#' @param first_var_with_level (`character` of length 2)\cr the first variable name with
|
|
69 |
#' the interaction level.
|
|
70 |
#' @param second_var_with_level (`character` of length 2)\cr the second variable name with
|
|
71 |
#' the interaction level.
|
|
72 |
#'
|
|
73 |
#' @return Name of coefficient.
|
|
74 |
#'
|
|
75 |
#' @export
|
|
76 |
h_interaction_coef_name <- function(interaction_vars, |
|
77 |
first_var_with_level,
|
|
78 |
second_var_with_level) { |
|
79 | 45x |
checkmate::assert_character(interaction_vars, len = 2, any.missing = FALSE) |
80 | 45x |
checkmate::assert_character(first_var_with_level, len = 2, any.missing = FALSE) |
81 | 45x |
checkmate::assert_character(second_var_with_level, len = 2, any.missing = FALSE) |
82 | 45x |
checkmate::assert_subset(c(first_var_with_level[1], second_var_with_level[1]), interaction_vars) |
83 | ||
84 | 45x |
first_name <- paste(first_var_with_level, collapse = "") |
85 | 45x |
second_name <- paste(second_var_with_level, collapse = "") |
86 | 45x |
if (first_var_with_level[1] == interaction_vars[1]) { |
87 | 34x |
paste(first_name, second_name, sep = ":") |
88 | 11x |
} else if (second_var_with_level[1] == interaction_vars[1]) { |
89 | 11x |
paste(second_name, first_name, sep = ":") |
90 |
}
|
|
91 |
}
|
|
92 | ||
93 |
#' @describeIn h_logistic_regression Helper function to calculate the odds ratio estimates
|
|
94 |
#' for the case when both the odds ratio and the interaction variable are categorical.
|
|
95 |
#'
|
|
96 |
#' @param odds_ratio_var (`string`)\cr the odds ratio variable.
|
|
97 |
#' @param interaction_var (`string`)\cr the interaction variable.
|
|
98 |
#'
|
|
99 |
#' @return Odds ratio.
|
|
100 |
#'
|
|
101 |
#' @export
|
|
102 |
h_or_cat_interaction <- function(odds_ratio_var, |
|
103 |
interaction_var,
|
|
104 |
fit_glm,
|
|
105 |
conf_level = 0.95) { |
|
106 | 7x |
interaction_vars <- h_get_interaction_vars(fit_glm) |
107 | 7x |
checkmate::assert_string(odds_ratio_var) |
108 | 7x |
checkmate::assert_string(interaction_var) |
109 | 7x |
checkmate::assert_subset(c(odds_ratio_var, interaction_var), interaction_vars) |
110 | 7x |
checkmate::assert_vector(interaction_vars, len = 2) |
111 | ||
112 | 7x |
xs_level <- fit_glm$xlevels |
113 | 7x |
xs_coef <- stats::coef(fit_glm) |
114 | 7x |
xs_vcov <- stats::vcov(fit_glm) |
115 | 7x |
y <- list() |
116 | 7x |
for (var_level in xs_level[[odds_ratio_var]][-1]) { |
117 | 12x |
x <- list() |
118 | 12x |
for (ref_level in xs_level[[interaction_var]]) { |
119 | 32x |
coef_names <- paste0(odds_ratio_var, var_level) |
120 | 32x |
if (ref_level != xs_level[[interaction_var]][1]) { |
121 | 20x |
interaction_coef_name <- h_interaction_coef_name( |
122 | 20x |
interaction_vars,
|
123 | 20x |
c(odds_ratio_var, var_level), |
124 | 20x |
c(interaction_var, ref_level) |
125 |
)
|
|
126 | 20x |
coef_names <- c( |
127 | 20x |
coef_names,
|
128 | 20x |
interaction_coef_name
|
129 |
)
|
|
130 |
}
|
|
131 | 32x |
if (length(coef_names) > 1) { |
132 | 20x |
ones <- t(c(1, 1)) |
133 | 20x |
est <- as.numeric(ones %*% xs_coef[coef_names]) |
134 | 20x |
se <- sqrt(as.numeric(ones %*% xs_vcov[coef_names, coef_names] %*% t(ones))) |
135 |
} else { |
|
136 | 12x |
est <- xs_coef[coef_names] |
137 | 12x |
se <- sqrt(as.numeric(xs_vcov[coef_names, coef_names])) |
138 |
}
|
|
139 | 32x |
or <- exp(est) |
140 | 32x |
ci <- exp(est + c(lcl = -1, ucl = 1) * stats::qnorm((1 + conf_level) / 2) * se) |
141 | 32x |
x[[ref_level]] <- list(or = or, ci = ci) |
142 |
}
|
|
143 | 12x |
y[[var_level]] <- x |
144 |
}
|
|
145 | 7x |
y
|
146 |
}
|
|
147 | ||
148 |
#' @describeIn h_logistic_regression Helper function to calculate the odds ratio estimates
|
|
149 |
#' for the case when either the odds ratio or the interaction variable is continuous.
|
|
150 |
#'
|
|
151 |
#' @param at (`NULL` or `numeric`)\cr optional values for the interaction variable. Otherwise
|
|
152 |
#' the median is used.
|
|
153 |
#'
|
|
154 |
#' @return Odds ratio.
|
|
155 |
#'
|
|
156 |
#' @note We don't provide a function for the case when both variables are continuous because
|
|
157 |
#' this does not arise in this table, as the treatment arm variable will always be involved
|
|
158 |
#' and categorical.
|
|
159 |
#'
|
|
160 |
#' @export
|
|
161 |
h_or_cont_interaction <- function(odds_ratio_var, |
|
162 |
interaction_var,
|
|
163 |
fit_glm,
|
|
164 |
at = NULL, |
|
165 |
conf_level = 0.95) { |
|
166 | 9x |
interaction_vars <- h_get_interaction_vars(fit_glm) |
167 | 9x |
checkmate::assert_string(odds_ratio_var) |
168 | 9x |
checkmate::assert_string(interaction_var) |
169 | 9x |
checkmate::assert_subset(c(odds_ratio_var, interaction_var), interaction_vars) |
170 | 9x |
checkmate::assert_vector(interaction_vars, len = 2) |
171 | 9x |
checkmate::assert_numeric(at, min.len = 1, null.ok = TRUE, any.missing = FALSE) |
172 | 9x |
xs_level <- fit_glm$xlevels |
173 | 9x |
xs_coef <- stats::coef(fit_glm) |
174 | 9x |
xs_vcov <- stats::vcov(fit_glm) |
175 | 9x |
xs_class <- attr(fit_glm$terms, "dataClasses") |
176 | 9x |
model_data <- fit_glm$model |
177 | 9x |
if (!is.null(at)) { |
178 | 2x |
checkmate::assert_set_equal(xs_class[interaction_var], "numeric") |
179 |
}
|
|
180 | 9x |
y <- list() |
181 | 9x |
if (xs_class[interaction_var] == "numeric") { |
182 | 6x |
if (is.null(at)) { |
183 | 4x |
at <- ceiling(stats::median(model_data[[interaction_var]])) |
184 |
}
|
|
185 | ||
186 | 6x |
for (var_level in xs_level[[odds_ratio_var]][-1]) { |
187 | 12x |
x <- list() |
188 | 12x |
for (increment in at) { |
189 | 18x |
coef_names <- paste0(odds_ratio_var, var_level) |
190 | 18x |
if (increment != 0) { |
191 | 18x |
interaction_coef_name <- h_interaction_coef_name( |
192 | 18x |
interaction_vars,
|
193 | 18x |
c(odds_ratio_var, var_level), |
194 | 18x |
c(interaction_var, "") |
195 |
)
|
|
196 | 18x |
coef_names <- c( |
197 | 18x |
coef_names,
|
198 | 18x |
interaction_coef_name
|
199 |
)
|
|
200 |
}
|
|
201 | 18x |
if (length(coef_names) > 1) { |
202 | 18x |
xvec <- t(c(1, increment)) |
203 | 18x |
est <- as.numeric(xvec %*% xs_coef[coef_names]) |
204 | 18x |
se <- sqrt(as.numeric(xvec %*% xs_vcov[coef_names, coef_names] %*% t(xvec))) |
205 |
} else { |
|
206 | ! |
est <- xs_coef[coef_names] |
207 | ! |
se <- sqrt(as.numeric(xs_vcov[coef_names, coef_names])) |
208 |
}
|
|
209 | 18x |
or <- exp(est) |
210 | 18x |
ci <- exp(est + c(lcl = -1, ucl = 1) * stats::qnorm((1 + conf_level) / 2) * se) |
211 | 18x |
x[[as.character(increment)]] <- list(or = or, ci = ci) |
212 |
}
|
|
213 | 12x |
y[[var_level]] <- x |
214 |
}
|
|
215 |
} else { |
|
216 | 3x |
checkmate::assert_set_equal(xs_class[odds_ratio_var], "numeric") |
217 | 3x |
checkmate::assert_set_equal(xs_class[interaction_var], "factor") |
218 | 3x |
for (var_level in xs_level[[interaction_var]]) { |
219 | 9x |
coef_names <- odds_ratio_var |
220 | 9x |
if (var_level != xs_level[[interaction_var]][1]) { |
221 | 6x |
interaction_coef_name <- h_interaction_coef_name( |
222 | 6x |
interaction_vars,
|
223 | 6x |
c(odds_ratio_var, ""), |
224 | 6x |
c(interaction_var, var_level) |
225 |
)
|
|
226 | 6x |
coef_names <- c( |
227 | 6x |
coef_names,
|
228 | 6x |
interaction_coef_name
|
229 |
)
|
|
230 |
}
|
|
231 | 9x |
if (length(coef_names) > 1) { |
232 | 6x |
xvec <- t(c(1, 1)) |
233 | 6x |
est <- as.numeric(xvec %*% xs_coef[coef_names]) |
234 | 6x |
se <- sqrt(as.numeric(xvec %*% xs_vcov[coef_names, coef_names] %*% t(xvec))) |
235 |
} else { |
|
236 | 3x |
est <- xs_coef[coef_names] |
237 | 3x |
se <- sqrt(as.numeric(xs_vcov[coef_names, coef_names])) |
238 |
}
|
|
239 | 9x |
or <- exp(est) |
240 | 9x |
ci <- exp(est + c(lcl = -1, ucl = 1) * stats::qnorm((1 + conf_level) / 2) * se) |
241 | 9x |
y[[var_level]] <- list(or = or, ci = ci) |
242 |
}
|
|
243 |
}
|
|
244 | 9x |
y
|
245 |
}
|
|
246 | ||
247 |
#' @describeIn h_logistic_regression Helper function to calculate the odds ratio estimates
|
|
248 |
#' in case of an interaction. This is a wrapper for [h_or_cont_interaction()] and
|
|
249 |
#' [h_or_cat_interaction()].
|
|
250 |
#'
|
|
251 |
#' @return Odds ratio.
|
|
252 |
#'
|
|
253 |
#' @export
|
|
254 |
h_or_interaction <- function(odds_ratio_var, |
|
255 |
interaction_var,
|
|
256 |
fit_glm,
|
|
257 |
at = NULL, |
|
258 |
conf_level = 0.95) { |
|
259 | 13x |
xs_class <- attr(fit_glm$terms, "dataClasses") |
260 | 13x |
if (any(xs_class[c(odds_ratio_var, interaction_var)] == "numeric")) { |
261 | 7x |
h_or_cont_interaction( |
262 | 7x |
odds_ratio_var,
|
263 | 7x |
interaction_var,
|
264 | 7x |
fit_glm,
|
265 | 7x |
at = at, |
266 | 7x |
conf_level = conf_level |
267 |
)
|
|
268 | 6x |
} else if (all(xs_class[c(odds_ratio_var, interaction_var)] == "factor")) { |
269 | 6x |
h_or_cat_interaction( |
270 | 6x |
odds_ratio_var,
|
271 | 6x |
interaction_var,
|
272 | 6x |
fit_glm,
|
273 | 6x |
conf_level = conf_level |
274 |
)
|
|
275 |
} else { |
|
276 | ! |
stop("wrong interaction variable class, the interaction variable is not a numeric nor a factor") |
277 |
}
|
|
278 |
}
|
|
279 | ||
280 |
#' @describeIn h_logistic_regression Helper function to construct term labels from simple terms and the table
|
|
281 |
#' of numbers of patients.
|
|
282 |
#'
|
|
283 |
#' @param terms (`character`)\cr simple terms.
|
|
284 |
#' @param table (`table`)\cr table containing numbers for terms.
|
|
285 |
#'
|
|
286 |
#' @return Term labels containing numbers of patients.
|
|
287 |
#'
|
|
288 |
#' @export
|
|
289 |
h_simple_term_labels <- function(terms, |
|
290 |
table) { |
|
291 | 45x |
checkmate::assert_true(is.table(table)) |
292 | 45x |
checkmate::assert_multi_class(terms, classes = c("factor", "character")) |
293 | 45x |
terms <- as.character(terms) |
294 | 45x |
term_n <- table[terms] |
295 | 45x |
paste0(terms, ", n = ", term_n) |
296 |
}
|
|
297 | ||
298 |
#' @describeIn h_logistic_regression Helper function to construct term labels from interaction terms and the table
|
|
299 |
#' of numbers of patients.
|
|
300 |
#'
|
|
301 |
#' @param terms1 (`character`)\cr terms for first dimension (rows).
|
|
302 |
#' @param terms2 (`character`)\cr terms for second dimension (rows).
|
|
303 |
#' @param any (`flag`)\cr whether any of `term1` and `term2` can be fulfilled to count the
|
|
304 |
#' number of patients. In that case they can only be scalar (strings).
|
|
305 |
#'
|
|
306 |
#' @return Term labels containing numbers of patients.
|
|
307 |
#'
|
|
308 |
#' @export
|
|
309 |
h_interaction_term_labels <- function(terms1, |
|
310 |
terms2,
|
|
311 |
table,
|
|
312 |
any = FALSE) { |
|
313 | 8x |
checkmate::assert_true(is.table(table)) |
314 | 8x |
checkmate::assert_flag(any) |
315 | 8x |
checkmate::assert_multi_class(terms1, classes = c("factor", "character")) |
316 | 8x |
checkmate::assert_multi_class(terms2, classes = c("factor", "character")) |
317 | 8x |
terms1 <- as.character(terms1) |
318 | 8x |
terms2 <- as.character(terms2) |
319 | 8x |
if (any) { |
320 | 4x |
checkmate::assert_scalar(terms1) |
321 | 4x |
checkmate::assert_scalar(terms2) |
322 | 4x |
paste0( |
323 | 4x |
terms1, " or ", terms2, ", n = ", |
324 |
# Note that we double count in the initial sum the cell [terms1, terms2], therefore subtract.
|
|
325 | 4x |
sum(c(table[terms1, ], table[, terms2])) - table[terms1, terms2] |
326 |
)
|
|
327 |
} else { |
|
328 | 4x |
term_n <- table[cbind(terms1, terms2)] |
329 | 4x |
paste0(terms1, " * ", terms2, ", n = ", term_n) |
330 |
}
|
|
331 |
}
|
|
332 | ||
333 |
#' @describeIn h_logistic_regression Helper function to tabulate the main effect
|
|
334 |
#' results of a (conditional) logistic regression model.
|
|
335 |
#'
|
|
336 |
#' @return Tabulated main effect results from a logistic regression model.
|
|
337 |
#'
|
|
338 |
#' @examples
|
|
339 |
#' h_glm_simple_term_extract("AGE", mod1)
|
|
340 |
#' h_glm_simple_term_extract("ARMCD", mod1)
|
|
341 |
#'
|
|
342 |
#' @export
|
|
343 |
h_glm_simple_term_extract <- function(x, fit_glm) { |
|
344 | 61x |
checkmate::assert_multi_class(fit_glm, c("glm", "clogit")) |
345 | 61x |
checkmate::assert_string(x) |
346 | ||
347 | 61x |
xs_class <- attr(fit_glm$terms, "dataClasses") |
348 | 61x |
xs_level <- fit_glm$xlevels |
349 | 61x |
xs_coef <- summary(fit_glm)$coefficients |
350 | 61x |
stats <- if (inherits(fit_glm, "glm")) { |
351 | 49x |
c("estimate" = "Estimate", "std_error" = "Std. Error", "pvalue" = "Pr(>|z|)") |
352 |
} else { |
|
353 | 12x |
c("estimate" = "coef", "std_error" = "se(coef)", "pvalue" = "Pr(>|z|)") |
354 |
}
|
|
355 |
# Make sure x is not an interaction term.
|
|
356 | 61x |
checkmate::assert_subset(x, names(xs_class)) |
357 | 61x |
x_sel <- if (xs_class[x] == "numeric") x else paste0(x, xs_level[[x]][-1]) |
358 | 61x |
x_stats <- as.data.frame(xs_coef[x_sel, stats, drop = FALSE], stringsAsFactors = FALSE) |
359 | 61x |
colnames(x_stats) <- names(stats) |
360 | 61x |
x_stats$estimate <- as.list(x_stats$estimate) |
361 | 61x |
x_stats$std_error <- as.list(x_stats$std_error) |
362 | 61x |
x_stats$pvalue <- as.list(x_stats$pvalue) |
363 | 61x |
x_stats$df <- as.list(1) |
364 | 61x |
if (xs_class[x] == "numeric") { |
365 | 46x |
x_stats$term <- x |
366 | 46x |
x_stats$term_label <- if (inherits(fit_glm, "glm")) { |
367 | 34x |
formatters::var_labels(fit_glm$data[x], fill = TRUE) |
368 |
} else { |
|
369 |
# We just fill in here with the `term` itself as we don't have the data available.
|
|
370 | 12x |
x
|
371 |
}
|
|
372 | 46x |
x_stats$is_variable_summary <- FALSE |
373 | 46x |
x_stats$is_term_summary <- TRUE |
374 |
} else { |
|
375 | 15x |
checkmate::assert_class(fit_glm, "glm") |
376 |
# The reason is that we don't have the original data set in the `clogit` object
|
|
377 |
# and therefore cannot determine the `x_numbers` here.
|
|
378 | 15x |
x_numbers <- table(fit_glm$data[[x]]) |
379 | 15x |
x_stats$term <- xs_level[[x]][-1] |
380 | 15x |
x_stats$term_label <- h_simple_term_labels(x_stats$term, x_numbers) |
381 | 15x |
x_stats$is_variable_summary <- FALSE |
382 | 15x |
x_stats$is_term_summary <- TRUE |
383 | 15x |
main_effects <- car::Anova(fit_glm, type = 3, test.statistic = "Wald") |
384 | 15x |
x_main <- data.frame( |
385 | 15x |
pvalue = main_effects[x, "Pr(>Chisq)", drop = TRUE], |
386 | 15x |
term = xs_level[[x]][1], |
387 | 15x |
term_label = paste("Reference", h_simple_term_labels(xs_level[[x]][1], x_numbers)), |
388 | 15x |
df = main_effects[x, "Df", drop = TRUE], |
389 | 15x |
stringsAsFactors = FALSE |
390 |
)
|
|
391 | 15x |
x_main$pvalue <- as.list(x_main$pvalue) |
392 | 15x |
x_main$df <- as.list(x_main$df) |
393 | 15x |
x_main$estimate <- list(numeric(0)) |
394 | 15x |
x_main$std_error <- list(numeric(0)) |
395 | 15x |
if (length(xs_level[[x]][-1]) == 1) { |
396 | 6x |
x_main$pvalue <- list(numeric(0)) |
397 | 6x |
x_main$df <- list(numeric(0)) |
398 |
}
|
|
399 | 15x |
x_main$is_variable_summary <- TRUE |
400 | 15x |
x_main$is_term_summary <- FALSE |
401 | 15x |
x_stats <- rbind(x_main, x_stats) |
402 |
}
|
|
403 | 61x |
x_stats$variable <- x |
404 | 61x |
x_stats$variable_label <- if (inherits(fit_glm, "glm")) { |
405 | 49x |
formatters::var_labels(fit_glm$data[x], fill = TRUE) |
406 |
} else { |
|
407 | 12x |
x
|
408 |
}
|
|
409 | 61x |
x_stats$interaction <- "" |
410 | 61x |
x_stats$interaction_label <- "" |
411 | 61x |
x_stats$reference <- "" |
412 | 61x |
x_stats$reference_label <- "" |
413 | 61x |
rownames(x_stats) <- NULL |
414 | 61x |
x_stats[c( |
415 | 61x |
"variable",
|
416 | 61x |
"variable_label",
|
417 | 61x |
"term",
|
418 | 61x |
"term_label",
|
419 | 61x |
"interaction",
|
420 | 61x |
"interaction_label",
|
421 | 61x |
"reference",
|
422 | 61x |
"reference_label",
|
423 | 61x |
"estimate",
|
424 | 61x |
"std_error",
|
425 | 61x |
"df",
|
426 | 61x |
"pvalue",
|
427 | 61x |
"is_variable_summary",
|
428 | 61x |
"is_term_summary"
|
429 |
)] |
|
430 |
}
|
|
431 | ||
432 |
#' @describeIn h_logistic_regression Helper function to tabulate the interaction term
|
|
433 |
#' results of a logistic regression model.
|
|
434 |
#'
|
|
435 |
#' @return Tabulated interaction term results from a logistic regression model.
|
|
436 |
#'
|
|
437 |
#' @examples
|
|
438 |
#' h_glm_interaction_extract("ARMCD:AGE", mod2)
|
|
439 |
#'
|
|
440 |
#' @export
|
|
441 |
h_glm_interaction_extract <- function(x, fit_glm) { |
|
442 | 6x |
vars <- h_get_interaction_vars(fit_glm) |
443 | 6x |
xs_class <- attr(fit_glm$terms, "dataClasses") |
444 | ||
445 | 6x |
checkmate::assert_string(x) |
446 | ||
447 |
# Only take two-way interaction
|
|
448 | 6x |
checkmate::assert_vector(vars, len = 2) |
449 | ||
450 |
# Only consider simple case: first variable in interaction is arm, a categorical variable
|
|
451 | 6x |
checkmate::assert_disjunct(xs_class[vars[1]], "numeric") |
452 | ||
453 | 6x |
xs_level <- fit_glm$xlevels |
454 | 6x |
xs_coef <- summary(fit_glm)$coefficients |
455 | 6x |
main_effects <- car::Anova(fit_glm, type = 3, test.statistic = "Wald") |
456 | 6x |
stats <- c("estimate" = "Estimate", "std_error" = "Std. Error", "pvalue" = "Pr(>|z|)") |
457 | 6x |
v1_comp <- xs_level[[vars[1]]][-1] |
458 | 6x |
if (xs_class[vars[2]] == "numeric") { |
459 | 3x |
x_stats <- as.data.frame( |
460 | 3x |
xs_coef[paste0(vars[1], v1_comp, ":", vars[2]), stats, drop = FALSE], |
461 | 3x |
stringsAsFactors = FALSE |
462 |
)
|
|
463 | 3x |
colnames(x_stats) <- names(stats) |
464 | 3x |
x_stats$term <- v1_comp |
465 | 3x |
x_numbers <- table(fit_glm$data[[vars[1]]]) |
466 | 3x |
x_stats$term_label <- h_simple_term_labels(v1_comp, x_numbers) |
467 | 3x |
v1_ref <- xs_level[[vars[1]]][1] |
468 | 3x |
term_main <- v1_ref |
469 | 3x |
ref_label <- h_simple_term_labels(v1_ref, x_numbers) |
470 | 3x |
} else if (xs_class[vars[2]] != "numeric") { |
471 | 3x |
v2_comp <- xs_level[[vars[2]]][-1] |
472 | 3x |
v1_v2_grid <- expand.grid(v1 = v1_comp, v2 = v2_comp) |
473 | 3x |
x_sel <- paste( |
474 | 3x |
paste0(vars[1], v1_v2_grid$v1), |
475 | 3x |
paste0(vars[2], v1_v2_grid$v2), |
476 | 3x |
sep = ":" |
477 |
)
|
|
478 | 3x |
x_stats <- as.data.frame(xs_coef[x_sel, stats, drop = FALSE], stringsAsFactors = FALSE) |
479 | 3x |
colnames(x_stats) <- names(stats) |
480 | 3x |
x_stats$term <- paste(v1_v2_grid$v1, "*", v1_v2_grid$v2) |
481 | 3x |
x_numbers <- table(fit_glm$data[[vars[1]]], fit_glm$data[[vars[2]]]) |
482 | 3x |
x_stats$term_label <- h_interaction_term_labels(v1_v2_grid$v1, v1_v2_grid$v2, x_numbers) |
483 | 3x |
v1_ref <- xs_level[[vars[1]]][1] |
484 | 3x |
v2_ref <- xs_level[[vars[2]]][1] |
485 | 3x |
term_main <- paste(vars[1], vars[2], sep = " * ") |
486 | 3x |
ref_label <- h_interaction_term_labels(v1_ref, v2_ref, x_numbers, any = TRUE) |
487 |
}
|
|
488 | 6x |
x_stats$df <- as.list(1) |
489 | 6x |
x_stats$pvalue <- as.list(x_stats$pvalue) |
490 | 6x |
x_stats$is_variable_summary <- FALSE |
491 | 6x |
x_stats$is_term_summary <- TRUE |
492 | 6x |
x_main <- data.frame( |
493 | 6x |
pvalue = main_effects[x, "Pr(>Chisq)", drop = TRUE], |
494 | 6x |
term = term_main, |
495 | 6x |
term_label = paste("Reference", ref_label), |
496 | 6x |
df = main_effects[x, "Df", drop = TRUE], |
497 | 6x |
stringsAsFactors = FALSE |
498 |
)
|
|
499 | 6x |
x_main$pvalue <- as.list(x_main$pvalue) |
500 | 6x |
x_main$df <- as.list(x_main$df) |
501 | 6x |
x_main$estimate <- list(numeric(0)) |
502 | 6x |
x_main$std_error <- list(numeric(0)) |
503 | 6x |
x_main$is_variable_summary <- TRUE |
504 | 6x |
x_main$is_term_summary <- FALSE |
505 | ||
506 | 6x |
x_stats <- rbind(x_main, x_stats) |
507 | 6x |
x_stats$variable <- x |
508 | 6x |
x_stats$variable_label <- paste( |
509 | 6x |
"Interaction of",
|
510 | 6x |
formatters::var_labels(fit_glm$data[vars[1]], fill = TRUE), |
511 |
"*",
|
|
512 | 6x |
formatters::var_labels(fit_glm$data[vars[2]], fill = TRUE) |
513 |
)
|
|
514 | 6x |
x_stats$interaction <- "" |
515 | 6x |
x_stats$interaction_label <- "" |
516 | 6x |
x_stats$reference <- "" |
517 | 6x |
x_stats$reference_label <- "" |
518 | 6x |
rownames(x_stats) <- NULL |
519 | 6x |
x_stats[c( |
520 | 6x |
"variable",
|
521 | 6x |
"variable_label",
|
522 | 6x |
"term",
|
523 | 6x |
"term_label",
|
524 | 6x |
"interaction",
|
525 | 6x |
"interaction_label",
|
526 | 6x |
"reference",
|
527 | 6x |
"reference_label",
|
528 | 6x |
"estimate",
|
529 | 6x |
"std_error",
|
530 | 6x |
"df",
|
531 | 6x |
"pvalue",
|
532 | 6x |
"is_variable_summary",
|
533 | 6x |
"is_term_summary"
|
534 |
)] |
|
535 |
}
|
|
536 | ||
537 |
#' @describeIn h_logistic_regression Helper function to tabulate the interaction
|
|
538 |
#' results of a logistic regression model. This basically is a wrapper for
|
|
539 |
#' [h_or_interaction()] and [h_glm_simple_term_extract()] which puts the results
|
|
540 |
#' in the right data frame format.
|
|
541 |
#'
|
|
542 |
#' @return A `data.frame` of tabulated interaction term results from a logistic regression model.
|
|
543 |
#'
|
|
544 |
#' @examples
|
|
545 |
#' h_glm_inter_term_extract("AGE", "ARMCD", mod2)
|
|
546 |
#'
|
|
547 |
#' @export
|
|
548 |
h_glm_inter_term_extract <- function(odds_ratio_var, |
|
549 |
interaction_var,
|
|
550 |
fit_glm,
|
|
551 |
...) { |
|
552 |
# First obtain the main effects.
|
|
553 | 11x |
main_stats <- h_glm_simple_term_extract(odds_ratio_var, fit_glm) |
554 | 11x |
main_stats$is_reference_summary <- FALSE |
555 | 11x |
main_stats$odds_ratio <- NA |
556 | 11x |
main_stats$lcl <- NA |
557 | 11x |
main_stats$ucl <- NA |
558 | ||
559 |
# Then we get the odds ratio estimates and put into df form.
|
|
560 | 11x |
or_numbers <- h_or_interaction(odds_ratio_var, interaction_var, fit_glm, ...) |
561 | 11x |
is_num_or_var <- attr(fit_glm$terms, "dataClasses")[odds_ratio_var] == "numeric" |
562 | ||
563 | 11x |
if (is_num_or_var) { |
564 |
# Numeric OR variable case.
|
|
565 | 3x |
references <- names(or_numbers) |
566 | 3x |
n_ref <- length(references) |
567 | ||
568 | 3x |
extract_from_list <- function(l, name, pos = 1) { |
569 | 9x |
unname(unlist( |
570 | 9x |
lapply(or_numbers, function(x) { |
571 | 27x |
x[[name]][pos] |
572 |
}) |
|
573 |
)) |
|
574 |
}
|
|
575 | 3x |
or_stats <- data.frame( |
576 | 3x |
variable = odds_ratio_var, |
577 | 3x |
variable_label = unname(formatters::var_labels(fit_glm$data[odds_ratio_var], fill = TRUE)), |
578 | 3x |
term = odds_ratio_var, |
579 | 3x |
term_label = unname(formatters::var_labels(fit_glm$data[odds_ratio_var], fill = TRUE)), |
580 | 3x |
interaction = interaction_var, |
581 | 3x |
interaction_label = unname(formatters::var_labels(fit_glm$data[interaction_var], fill = TRUE)), |
582 | 3x |
reference = references, |
583 | 3x |
reference_label = references, |
584 | 3x |
estimate = NA, |
585 | 3x |
std_error = NA, |
586 | 3x |
odds_ratio = extract_from_list(or_numbers, "or"), |
587 | 3x |
lcl = extract_from_list(or_numbers, "ci", pos = "lcl"), |
588 | 3x |
ucl = extract_from_list(or_numbers, "ci", pos = "ucl"), |
589 | 3x |
df = NA, |
590 | 3x |
pvalue = NA, |
591 | 3x |
is_variable_summary = FALSE, |
592 | 3x |
is_term_summary = FALSE, |
593 | 3x |
is_reference_summary = TRUE |
594 |
)
|
|
595 |
} else { |
|
596 |
# Categorical OR variable case.
|
|
597 | 8x |
references <- names(or_numbers[[1]]) |
598 | 8x |
n_ref <- length(references) |
599 | ||
600 | 8x |
extract_from_list <- function(l, name, pos = 1) { |
601 | 24x |
unname(unlist( |
602 | 24x |
lapply(or_numbers, function(x) { |
603 | 42x |
lapply(x, function(y) y[[name]][pos]) |
604 |
}) |
|
605 |
)) |
|
606 |
}
|
|
607 | 8x |
or_stats <- data.frame( |
608 | 8x |
variable = odds_ratio_var, |
609 | 8x |
variable_label = unname(formatters::var_labels(fit_glm$data[odds_ratio_var], fill = TRUE)), |
610 | 8x |
term = rep(names(or_numbers), each = n_ref), |
611 | 8x |
term_label = h_simple_term_labels(rep(names(or_numbers), each = n_ref), table(fit_glm$data[[odds_ratio_var]])), |
612 | 8x |
interaction = interaction_var, |
613 | 8x |
interaction_label = unname(formatters::var_labels(fit_glm$data[interaction_var], fill = TRUE)), |
614 | 8x |
reference = unlist(lapply(or_numbers, names)), |
615 | 8x |
reference_label = unlist(lapply(or_numbers, names)), |
616 | 8x |
estimate = NA, |
617 | 8x |
std_error = NA, |
618 | 8x |
odds_ratio = extract_from_list(or_numbers, "or"), |
619 | 8x |
lcl = extract_from_list(or_numbers, "ci", pos = "lcl"), |
620 | 8x |
ucl = extract_from_list(or_numbers, "ci", pos = "ucl"), |
621 | 8x |
df = NA, |
622 | 8x |
pvalue = NA, |
623 | 8x |
is_variable_summary = FALSE, |
624 | 8x |
is_term_summary = FALSE, |
625 | 8x |
is_reference_summary = TRUE |
626 |
)
|
|
627 |
}
|
|
628 | ||
629 | 11x |
df <- rbind( |
630 | 11x |
main_stats[, names(or_stats)], |
631 | 11x |
or_stats
|
632 |
)
|
|
633 | 11x |
df[order(-df$is_variable_summary, df$term, -df$is_term_summary, df$reference), ] |
634 |
}
|
|
635 | ||
636 |
#' @describeIn h_logistic_regression Helper function to tabulate the results including
|
|
637 |
#' odds ratios and confidence intervals of simple terms.
|
|
638 |
#'
|
|
639 |
#' @return Tabulated statistics for the given variable(s) from the logistic regression model.
|
|
640 |
#'
|
|
641 |
#' @examples
|
|
642 |
#' h_logistic_simple_terms("AGE", mod1)
|
|
643 |
#'
|
|
644 |
#' @export
|
|
645 |
h_logistic_simple_terms <- function(x, fit_glm, conf_level = 0.95) { |
|
646 | 40x |
checkmate::assert_multi_class(fit_glm, c("glm", "clogit")) |
647 | 40x |
if (inherits(fit_glm, "glm")) { |
648 | 29x |
checkmate::assert_set_equal(fit_glm$family$family, "binomial") |
649 |
}
|
|
650 | 40x |
terms_name <- attr(stats::terms(fit_glm), "term.labels") |
651 | 40x |
xs_class <- attr(fit_glm$terms, "dataClasses") |
652 | 40x |
interaction <- terms_name[which(!terms_name %in% names(xs_class))] |
653 | 40x |
checkmate::assert_subset(x, terms_name) |
654 | 40x |
if (length(interaction) != 0) { |
655 |
# Make sure any item in x is not part of interaction term
|
|
656 | 1x |
checkmate::assert_disjunct(x, unlist(strsplit(interaction, ":"))) |
657 |
}
|
|
658 | 40x |
x_stats <- lapply(x, h_glm_simple_term_extract, fit_glm) |
659 | 40x |
x_stats <- do.call(rbind, x_stats) |
660 | 40x |
q_norm <- stats::qnorm((1 + conf_level) / 2) |
661 | 40x |
x_stats$odds_ratio <- lapply(x_stats$estimate, exp) |
662 | 40x |
x_stats$lcl <- Map(function(or, se) exp(log(or) - q_norm * se), x_stats$odds_ratio, x_stats$std_error) |
663 | 40x |
x_stats$ucl <- Map(function(or, se) exp(log(or) + q_norm * se), x_stats$odds_ratio, x_stats$std_error) |
664 | 40x |
x_stats$ci <- Map(function(lcl, ucl) c(lcl, ucl), lcl = x_stats$lcl, ucl = x_stats$ucl) |
665 | 40x |
x_stats
|
666 |
}
|
|
667 | ||
668 |
#' @describeIn h_logistic_regression Helper function to tabulate the results including
|
|
669 |
#' odds ratios and confidence intervals of interaction terms.
|
|
670 |
#'
|
|
671 |
#' @return Tabulated statistics for the given variable(s) from the logistic regression model.
|
|
672 |
#'
|
|
673 |
#' @examples
|
|
674 |
#' h_logistic_inter_terms(c("RACE", "AGE", "ARMCD", "AGE:ARMCD"), mod2)
|
|
675 |
#'
|
|
676 |
#' @export
|
|
677 |
h_logistic_inter_terms <- function(x, |
|
678 |
fit_glm,
|
|
679 |
conf_level = 0.95, |
|
680 |
at = NULL) { |
|
681 |
# Find out the interaction variables and interaction term.
|
|
682 | 4x |
inter_vars <- h_get_interaction_vars(fit_glm) |
683 | 4x |
checkmate::assert_vector(inter_vars, len = 2) |
684 | ||
685 | ||
686 | 4x |
inter_term_index <- intersect(grep(inter_vars[1], x), grep(inter_vars[2], x)) |
687 | 4x |
inter_term <- x[inter_term_index] |
688 | ||
689 |
# For the non-interaction vars we need the standard stuff.
|
|
690 | 4x |
normal_terms <- setdiff(x, union(inter_vars, inter_term)) |
691 | ||
692 | 4x |
x_stats <- lapply(normal_terms, h_glm_simple_term_extract, fit_glm) |
693 | 4x |
x_stats <- do.call(rbind, x_stats) |
694 | 4x |
q_norm <- stats::qnorm((1 + conf_level) / 2) |
695 | 4x |
x_stats$odds_ratio <- lapply(x_stats$estimate, exp) |
696 | 4x |
x_stats$lcl <- Map(function(or, se) exp(log(or) - q_norm * se), x_stats$odds_ratio, x_stats$std_error) |
697 | 4x |
x_stats$ucl <- Map(function(or, se) exp(log(or) + q_norm * se), x_stats$odds_ratio, x_stats$std_error) |
698 | 4x |
normal_stats <- x_stats |
699 | 4x |
normal_stats$is_reference_summary <- FALSE |
700 | ||
701 |
# Now the interaction term itself.
|
|
702 | 4x |
inter_term_stats <- h_glm_interaction_extract(inter_term, fit_glm) |
703 | 4x |
inter_term_stats$odds_ratio <- NA |
704 | 4x |
inter_term_stats$lcl <- NA |
705 | 4x |
inter_term_stats$ucl <- NA |
706 | 4x |
inter_term_stats$is_reference_summary <- FALSE |
707 | ||
708 | 4x |
is_intervar1_numeric <- attr(fit_glm$terms, "dataClasses")[inter_vars[1]] == "numeric" |
709 | ||
710 |
# Interaction stuff.
|
|
711 | 4x |
inter_stats_one <- h_glm_inter_term_extract( |
712 | 4x |
inter_vars[1], |
713 | 4x |
inter_vars[2], |
714 | 4x |
fit_glm,
|
715 | 4x |
conf_level = conf_level, |
716 | 4x |
at = `if`(is_intervar1_numeric, NULL, at) |
717 |
)
|
|
718 | 4x |
inter_stats_two <- h_glm_inter_term_extract( |
719 | 4x |
inter_vars[2], |
720 | 4x |
inter_vars[1], |
721 | 4x |
fit_glm,
|
722 | 4x |
conf_level = conf_level, |
723 | 4x |
at = `if`(is_intervar1_numeric, at, NULL) |
724 |
)
|
|
725 | ||
726 |
# Now just combine everything in one data frame.
|
|
727 | 4x |
col_names <- c( |
728 | 4x |
"variable",
|
729 | 4x |
"variable_label",
|
730 | 4x |
"term",
|
731 | 4x |
"term_label",
|
732 | 4x |
"interaction",
|
733 | 4x |
"interaction_label",
|
734 | 4x |
"reference",
|
735 | 4x |
"reference_label",
|
736 | 4x |
"estimate",
|
737 | 4x |
"std_error",
|
738 | 4x |
"df",
|
739 | 4x |
"pvalue",
|
740 | 4x |
"odds_ratio",
|
741 | 4x |
"lcl",
|
742 | 4x |
"ucl",
|
743 | 4x |
"is_variable_summary",
|
744 | 4x |
"is_term_summary",
|
745 | 4x |
"is_reference_summary"
|
746 |
)
|
|
747 | 4x |
df <- rbind( |
748 | 4x |
inter_stats_one[, col_names], |
749 | 4x |
inter_stats_two[, col_names], |
750 | 4x |
inter_term_stats[, col_names] |
751 |
)
|
|
752 | 4x |
if (length(normal_terms) > 0) { |
753 | 4x |
df <- rbind( |
754 | 4x |
normal_stats[, col_names], |
755 | 4x |
df
|
756 |
)
|
|
757 |
}
|
|
758 | 4x |
df$ci <- combine_vectors(df$lcl, df$ucl) |
759 | 4x |
df
|
760 |
}
|
1 |
#' Create a Forest Plot based on a Table
|
|
2 |
#'
|
|
3 |
#' Create a forest plot from any [rtables::rtable()] object that has a
|
|
4 |
#' column with a single value and a column with 2 values.
|
|
5 |
#'
|
|
6 |
#' @description `r lifecycle::badge("stable")`
|
|
7 |
#'
|
|
8 |
#' @inheritParams argument_convention
|
|
9 |
#' @param tbl (`rtable`)
|
|
10 |
#' @param col_x (`integer`)\cr column index with estimator. By default tries to get this from
|
|
11 |
#' `tbl` attribute `col_x`, otherwise needs to be manually specified.
|
|
12 |
#' @param col_ci (`integer`)\cr column index with confidence intervals. By default tries
|
|
13 |
#' to get this from `tbl` attribute `col_ci`, otherwise needs to be manually specified.
|
|
14 |
#' @param vline (`number`)\cr x coordinate for vertical line, if `NULL` then the line is omitted.
|
|
15 |
#' @param forest_header (`character`, length 2)\cr text displayed to the left and right of `vline`, respectively.
|
|
16 |
#' If `vline = NULL` then `forest_header` needs to be `NULL` too.
|
|
17 |
#' By default tries to get this from `tbl` attribute `forest_header`.
|
|
18 |
#' @param xlim (`numeric`)\cr limits for x axis.
|
|
19 |
#' @param logx (`flag`)\cr show the x-values on logarithm scale.
|
|
20 |
#' @param x_at (`numeric`)\cr x-tick locations, if `NULL` they get automatically chosen.
|
|
21 |
#' @param width_row_names (`unit`)\cr width for row names.
|
|
22 |
#' If `NULL` the widths get automatically calculated. See [grid::unit()].
|
|
23 |
#' @param width_columns (`unit`)\cr widths for the table columns.
|
|
24 |
#' If `NULL` the widths get automatically calculated. See [grid::unit()].
|
|
25 |
#' @param width_forest (`unit`)\cr width for the forest column.
|
|
26 |
#' If `NULL` the widths get automatically calculated. See [grid::unit()].
|
|
27 |
#' @param col_symbol_size (`integer`)\cr column index from `tbl` containing data to be used
|
|
28 |
#' to determine relative size for estimator plot symbol. Typically, the symbol size is proportional
|
|
29 |
#' to the sample size used to calculate the estimator. If `NULL`, the same symbol size is used for all subgroups.
|
|
30 |
#' By default tries to get this from `tbl` attribute `col_symbol_size`, otherwise needs to be manually specified.
|
|
31 |
#' @param col (`character`)\cr color(s).
|
|
32 |
#'
|
|
33 |
#' @return `gTree` object containing the forest plot and table.
|
|
34 |
#'
|
|
35 |
#' @examples
|
|
36 |
#' \donttest{
|
|
37 |
#' library(dplyr)
|
|
38 |
#' library(forcats)
|
|
39 |
#' library(nestcolor)
|
|
40 |
#'
|
|
41 |
#' adrs <- tern_ex_adrs
|
|
42 |
#' n_records <- 20
|
|
43 |
#' adrs_labels <- formatters::var_labels(adrs, fill = TRUE)
|
|
44 |
#' adrs <- adrs %>%
|
|
45 |
#' filter(PARAMCD == "BESRSPI") %>%
|
|
46 |
#' filter(ARM %in% c("A: Drug X", "B: Placebo")) %>%
|
|
47 |
#' slice(seq_len(n_records)) %>%
|
|
48 |
#' droplevels() %>%
|
|
49 |
#' mutate(
|
|
50 |
#' # Reorder levels of factor to make the placebo group the reference arm.
|
|
51 |
#' ARM = fct_relevel(ARM, "B: Placebo"),
|
|
52 |
#' rsp = AVALC == "CR"
|
|
53 |
#' )
|
|
54 |
#' formatters::var_labels(adrs) <- c(adrs_labels, "Response")
|
|
55 |
#' df <- extract_rsp_subgroups(
|
|
56 |
#' variables = list(rsp = "rsp", arm = "ARM", subgroups = c("SEX", "STRATA2")),
|
|
57 |
#' data = adrs
|
|
58 |
#' )
|
|
59 |
#' # Full commonly used response table.
|
|
60 |
#'
|
|
61 |
#' tbl <- basic_table() %>%
|
|
62 |
#' tabulate_rsp_subgroups(df)
|
|
63 |
#' p <- g_forest(tbl)
|
|
64 |
#'
|
|
65 |
#' draw_grob(p)
|
|
66 |
#'
|
|
67 |
#' # Odds ratio only table.
|
|
68 |
#'
|
|
69 |
#' tbl_or <- basic_table() %>%
|
|
70 |
#' tabulate_rsp_subgroups(df, vars = c("n_tot", "or", "ci"))
|
|
71 |
#' tbl_or
|
|
72 |
#' p <- g_forest(
|
|
73 |
#' tbl_or,
|
|
74 |
#' forest_header = c("Comparison\nBetter", "Treatment\nBetter")
|
|
75 |
#' )
|
|
76 |
#'
|
|
77 |
#' draw_grob(p)
|
|
78 |
#'
|
|
79 |
#' # Survival forest plot example.
|
|
80 |
#' adtte <- tern_ex_adtte
|
|
81 |
#' # Save variable labels before data processing steps.
|
|
82 |
#' adtte_labels <- formatters::var_labels(adtte, fill = TRUE)
|
|
83 |
#' adtte_f <- adtte %>%
|
|
84 |
#' filter(
|
|
85 |
#' PARAMCD == "OS",
|
|
86 |
#' ARM %in% c("B: Placebo", "A: Drug X"),
|
|
87 |
#' SEX %in% c("M", "F")
|
|
88 |
#' ) %>%
|
|
89 |
#' mutate(
|
|
90 |
#' # Reorder levels of ARM to display reference arm before treatment arm.
|
|
91 |
#' ARM = droplevels(fct_relevel(ARM, "B: Placebo")),
|
|
92 |
#' SEX = droplevels(SEX),
|
|
93 |
#' AVALU = as.character(AVALU),
|
|
94 |
#' is_event = CNSR == 0
|
|
95 |
#' )
|
|
96 |
#' labels <- list(
|
|
97 |
#' "ARM" = adtte_labels["ARM"],
|
|
98 |
#' "SEX" = adtte_labels["SEX"],
|
|
99 |
#' "AVALU" = adtte_labels["AVALU"],
|
|
100 |
#' "is_event" = "Event Flag"
|
|
101 |
#' )
|
|
102 |
#' formatters::var_labels(adtte_f)[names(labels)] <- as.character(labels)
|
|
103 |
#' df <- extract_survival_subgroups(
|
|
104 |
#' variables = list(
|
|
105 |
#' tte = "AVAL",
|
|
106 |
#' is_event = "is_event",
|
|
107 |
#' arm = "ARM", subgroups = c("SEX", "BMRKR2")
|
|
108 |
#' ),
|
|
109 |
#' data = adtte_f
|
|
110 |
#' )
|
|
111 |
#' table_hr <- basic_table() %>%
|
|
112 |
#' tabulate_survival_subgroups(df, time_unit = adtte_f$AVALU[1])
|
|
113 |
#' g_forest(table_hr)
|
|
114 |
#' # Works with any `rtable`.
|
|
115 |
#' tbl <- rtable(
|
|
116 |
#' header = c("E", "CI", "N"),
|
|
117 |
#' rrow("", 1, c(.8, 1.2), 200),
|
|
118 |
#' rrow("", 1.2, c(1.1, 1.4), 50)
|
|
119 |
#' )
|
|
120 |
#' g_forest(
|
|
121 |
#' tbl = tbl,
|
|
122 |
#' col_x = 1,
|
|
123 |
#' col_ci = 2,
|
|
124 |
#' xlim = c(0.5, 2),
|
|
125 |
#' x_at = c(0.5, 1, 2),
|
|
126 |
#' col_symbol_size = 3
|
|
127 |
#' )
|
|
128 |
#' tbl <- rtable(
|
|
129 |
#' header = rheader(
|
|
130 |
#' rrow("", rcell("A", colspan = 2)),
|
|
131 |
#' rrow("", "c1", "c2")
|
|
132 |
#' ),
|
|
133 |
#' rrow("row 1", 1, c(.8, 1.2)),
|
|
134 |
#' rrow("row 2", 1.2, c(1.1, 1.4))
|
|
135 |
#' )
|
|
136 |
#' g_forest(
|
|
137 |
#' tbl = tbl,
|
|
138 |
#' col_x = 1,
|
|
139 |
#' col_ci = 2,
|
|
140 |
#' xlim = c(0.5, 2),
|
|
141 |
#' x_at = c(0.5, 1, 2),
|
|
142 |
#' vline = 1,
|
|
143 |
#' forest_header = c("Hello", "World")
|
|
144 |
#' )
|
|
145 |
#' }
|
|
146 |
#'
|
|
147 |
#' @export
|
|
148 |
g_forest <- function(tbl, |
|
149 |
col_x = attr(tbl, "col_x"), |
|
150 |
col_ci = attr(tbl, "col_ci"), |
|
151 |
vline = 1, |
|
152 |
forest_header = attr(tbl, "forest_header"), |
|
153 |
xlim = c(0.1, 10), |
|
154 |
logx = TRUE, |
|
155 |
x_at = c(0.1, 1, 10), |
|
156 |
width_row_names = NULL, |
|
157 |
width_columns = NULL, |
|
158 |
width_forest = grid::unit(1, "null"), |
|
159 |
col_symbol_size = attr(tbl, "col_symbol_size"), |
|
160 |
col = getOption("ggplot2.discrete.colour")[1], |
|
161 |
draw = TRUE, |
|
162 |
newpage = TRUE) { |
|
163 | 2x |
checkmate::assert_class(tbl, "VTableTree") |
164 | ||
165 | 2x |
nr <- nrow(tbl) |
166 | 2x |
nc <- ncol(tbl) |
167 | 2x |
if (is.null(col)) { |
168 | 2x |
col <- "blue" |
169 |
}
|
|
170 | ||
171 | 2x |
checkmate::assert_number(col_x, lower = 0, upper = nc, null.ok = FALSE) |
172 | 2x |
checkmate::assert_number(col_ci, lower = 0, upper = nc, null.ok = FALSE) |
173 | 2x |
checkmate::assert_number(col_symbol_size, lower = 0, upper = nc, null.ok = TRUE) |
174 | 2x |
checkmate::assert_true(col_x > 0) |
175 | 2x |
checkmate::assert_true(col_ci > 0) |
176 | 2x |
checkmate::assert_character(col) |
177 | 2x |
if (!is.null(col_symbol_size)) { |
178 | 1x |
checkmate::assert_true(col_symbol_size > 0) |
179 |
}
|
|
180 | ||
181 | 2x |
x_e <- vapply(seq_len(nr), function(i) { |
182 |
# If a label row is selected NULL is returned with a warning (suppressed)
|
|
183 | 9x |
xi <- suppressWarnings(as.vector(tbl[i, col_x, drop = TRUE])) |
184 | ||
185 | 9x |
if (!is.null(xi) && !(length(xi) <= 0) && is.numeric(xi)) { |
186 | 7x |
xi
|
187 |
} else { |
|
188 | 2x |
NA_real_
|
189 |
}
|
|
190 | 2x |
}, numeric(1)) |
191 | ||
192 | 2x |
x_ci <- lapply(seq_len(nr), function(i) { |
193 | 9x |
xi <- suppressWarnings(as.vector(tbl[i, col_ci, drop = TRUE])) # as above |
194 | ||
195 | 9x |
if (!is.null(xi) && !(length(xi) <= 0) && is.numeric(xi)) { |
196 | 7x |
if (length(xi) != 2) { |
197 | ! |
stop("ci column needs two elements") |
198 |
}
|
|
199 | 7x |
xi
|
200 |
} else { |
|
201 | 2x |
c(NA_real_, NA_real_) |
202 |
}
|
|
203 |
}) |
|
204 | ||
205 | 2x |
lower <- vapply(x_ci, `[`, numeric(1), 1) |
206 | 2x |
upper <- vapply(x_ci, `[`, numeric(1), 2) |
207 | ||
208 | 2x |
symbol_size <- if (!is.null(col_symbol_size)) { |
209 | 1x |
tmp_symbol_size <- vapply(seq_len(nr), function(i) { |
210 | 7x |
suppressWarnings(xi <- as.vector(tbl[i, col_symbol_size, drop = TRUE])) |
211 | ||
212 | 7x |
if (!is.null(xi) && !(length(xi) <= 0) && is.numeric(xi)) { |
213 | 5x |
xi
|
214 |
} else { |
|
215 | 1x |
NA_real_
|
216 |
}
|
|
217 | 1x |
}, numeric(1)) |
218 | ||
219 |
# Scale symbol size.
|
|
220 | 1x |
tmp_symbol_size <- sqrt(tmp_symbol_size) |
221 | 1x |
max_size <- max(tmp_symbol_size, na.rm = TRUE) |
222 |
# Biggest points have radius is 2 * (1/3.5) lines not to overlap.
|
|
223 |
# See forest_dot_line.
|
|
224 | 1x |
2 * tmp_symbol_size / max_size |
225 |
} else { |
|
226 | 1x |
NULL
|
227 |
}
|
|
228 | ||
229 | 2x |
grob_forest <- forest_grob( |
230 | 2x |
tbl,
|
231 | 2x |
x_e,
|
232 | 2x |
lower,
|
233 | 2x |
upper,
|
234 | 2x |
vline,
|
235 | 2x |
forest_header,
|
236 | 2x |
xlim,
|
237 | 2x |
logx,
|
238 | 2x |
x_at,
|
239 | 2x |
width_row_names,
|
240 | 2x |
width_columns,
|
241 | 2x |
width_forest,
|
242 | 2x |
symbol_size = symbol_size, |
243 | 2x |
col = col, |
244 | 2x |
vp = grid::plotViewport(margins = rep(1, 4)) |
245 |
)
|
|
246 | ||
247 | 2x |
if (draw) { |
248 | ! |
if (newpage) grid::grid.newpage() |
249 | ! |
grid::grid.draw(grob_forest) |
250 |
}
|
|
251 | ||
252 | 2x |
invisible(grob_forest) |
253 |
}
|
|
254 | ||
255 |
#' Forest Plot Grob
|
|
256 |
#'
|
|
257 |
#' @inheritParams g_forest
|
|
258 |
#' @param tbl ([rtables::rtable()])
|
|
259 |
#' @param x (`numeric`)\cr coordinate of point.
|
|
260 |
#' @param lower,upper (`numeric`)\cr lower/upper bound of the confidence interval.
|
|
261 |
#' @param symbol_size (`numeric`)\cr vector with relative size for plot symbol.
|
|
262 |
#' If `NULL`, the same symbol size is used.
|
|
263 |
#'
|
|
264 |
#' @details
|
|
265 |
#' The heights get automatically determined.
|
|
266 |
#'
|
|
267 |
#' @noRd
|
|
268 |
#'
|
|
269 |
#' @examples
|
|
270 |
#' tbl <- rtable(
|
|
271 |
#' header = rheader(
|
|
272 |
#' rrow("", "E", rcell("CI", colspan = 2), "N"),
|
|
273 |
#' rrow("", "A", "B", "C", "D")
|
|
274 |
#' ),
|
|
275 |
#' rrow("row 1", 1, 0.8, 1.1, 16),
|
|
276 |
#' rrow("row 2", 1.4, 0.8, 1.6, 25),
|
|
277 |
#' rrow("row 3", 1.2, 0.8, 1.6, 36)
|
|
278 |
#' )
|
|
279 |
#'
|
|
280 |
#' x <- c(1, 1.4, 1.2)
|
|
281 |
#' lower <- c(0.8, 0.8, 0.8)
|
|
282 |
#' upper <- c(1.1, 1.6, 1.6)
|
|
283 |
#' # numeric vector with multiplication factor to scale each circle radius
|
|
284 |
#' # default radius is 1/3.5 lines
|
|
285 |
#' symbol_scale <- c(1, 1.25, 1.5)
|
|
286 |
#'
|
|
287 |
#' # Internal function - forest_grob
|
|
288 |
#' \donttest{
|
|
289 |
#' p <- forest_grob(tbl, x, lower, upper,
|
|
290 |
#' vline = 1, forest_header = c("A", "B"),
|
|
291 |
#' x_at = c(.1, 1, 10), xlim = c(0.1, 10), logx = TRUE, symbol_size = symbol_scale,
|
|
292 |
#' vp = grid::plotViewport(margins = c(1, 1, 1, 1))
|
|
293 |
#' )
|
|
294 |
#'
|
|
295 |
#' draw_grob(p)
|
|
296 |
#' }
|
|
297 |
forest_grob <- function(tbl, |
|
298 |
x,
|
|
299 |
lower,
|
|
300 |
upper,
|
|
301 |
vline,
|
|
302 |
forest_header,
|
|
303 |
xlim = NULL, |
|
304 |
logx = FALSE, |
|
305 |
x_at = NULL, |
|
306 |
width_row_names = NULL, |
|
307 |
width_columns = NULL, |
|
308 |
width_forest = grid::unit(1, "null"), |
|
309 |
symbol_size = NULL, |
|
310 |
col = "blue", |
|
311 |
name = NULL, |
|
312 |
gp = NULL, |
|
313 |
vp = NULL) { |
|
314 | 2x |
nr <- nrow(tbl) |
315 | 2x |
if (is.null(vline)) { |
316 | ! |
checkmate::assert_true(is.null(forest_header)) |
317 |
} else { |
|
318 | 2x |
checkmate::assert_number(vline) |
319 | 2x |
checkmate::assert_character(forest_header, len = 2, null.ok = TRUE) |
320 |
}
|
|
321 | ||
322 | 2x |
checkmate::assert_numeric(x, len = nr) |
323 | 2x |
checkmate::assert_numeric(lower, len = nr) |
324 | 2x |
checkmate::assert_numeric(upper, len = nr) |
325 | 2x |
checkmate::assert_numeric(symbol_size, len = nr, null.ok = TRUE) |
326 | 2x |
checkmate::assert_character(col) |
327 | ||
328 | 2x |
if (is.null(symbol_size)) { |
329 | 1x |
symbol_size <- rep(1, nr) |
330 |
}
|
|
331 | ||
332 | 2x |
if (is.null(xlim)) { |
333 | ! |
r <- range(c(x, lower, upper), na.rm = TRUE) |
334 | ! |
xlim <- r + c(-0.05, 0.05) * diff(r) |
335 |
}
|
|
336 | ||
337 | 2x |
if (logx) { |
338 | 2x |
if (is.null(x_at)) { |
339 | ! |
x_at <- pretty(log(stats::na.omit(c(x, lower, upper)))) |
340 | ! |
x_labels <- exp(x_at) |
341 |
} else { |
|
342 | 2x |
x_labels <- x_at |
343 | 2x |
x_at <- log(x_at) |
344 |
}
|
|
345 | 2x |
xlim <- log(xlim) |
346 | 2x |
x <- log(x) |
347 | 2x |
lower <- log(lower) |
348 | 2x |
upper <- log(upper) |
349 | 2x |
if (!is.null(vline)) { |
350 | 2x |
vline <- log(vline) |
351 |
}
|
|
352 |
} else { |
|
353 | ! |
x_labels <- TRUE |
354 |
}
|
|
355 | ||
356 | 2x |
data_forest_vp <- grid::dataViewport(xlim, c(0, 1)) |
357 | ||
358 |
# Get table content as matrix form.
|
|
359 | 2x |
mf <- matrix_form(tbl) |
360 | ||
361 |
# Use `rtables` indent_string eventually.
|
|
362 | 2x |
mf$strings[, 1] <- paste0( |
363 | 2x |
strrep(" ", c(rep(0, attr(mf, "nrow_header")), mf$row_info$indent)), |
364 | 2x |
mf$strings[, 1] |
365 |
)
|
|
366 | ||
367 | 2x |
n_header <- attr(mf, "nrow_header") |
368 | ||
369 | ! |
if (any(mf$display[, 1] == FALSE)) stop("row names need to be always displayed") |
370 | ||
371 |
# Pre-process the data to be used in lapply and cell_in_rows.
|
|
372 | 2x |
to_args_for_cell_in_rows_fun <- function(part = c("body", "header"), |
373 | 2x |
underline_colspan = FALSE) { |
374 | 4x |
part <- match.arg(part) |
375 | 4x |
if (part == "body") { |
376 | 2x |
mat_row_indices <- seq_len(nrow(tbl)) + n_header |
377 | 2x |
row_ind_offset <- -n_header |
378 |
} else { |
|
379 | 2x |
mat_row_indices <- seq_len(n_header) |
380 | 2x |
row_ind_offset <- 0 |
381 |
}
|
|
382 | ||
383 | 4x |
lapply(mat_row_indices, function(i) { |
384 | 13x |
disp <- mf$display[i, -1] |
385 | 13x |
list( |
386 | 13x |
row_name = mf$strings[i, 1], |
387 | 13x |
cells = mf$strings[i, -1][disp], |
388 | 13x |
cell_spans = mf$spans[i, -1][disp], |
389 | 13x |
row_index = i + row_ind_offset, |
390 | 13x |
underline_colspan = underline_colspan |
391 |
)
|
|
392 |
}) |
|
393 |
}
|
|
394 | ||
395 | 2x |
args_header <- to_args_for_cell_in_rows_fun("header", underline_colspan = TRUE) |
396 | 2x |
args_body <- to_args_for_cell_in_rows_fun("body", underline_colspan = FALSE) |
397 | ||
398 | 2x |
grid::gTree( |
399 | 2x |
name = name, |
400 | 2x |
children = grid::gList( |
401 | 2x |
grid::gTree( |
402 | 2x |
children = do.call(grid::gList, lapply(args_header, do.call, what = cell_in_rows)), |
403 | 2x |
vp = grid::vpPath("vp_table_layout", "vp_header") |
404 |
),
|
|
405 | 2x |
grid::gTree( |
406 | 2x |
children = do.call(grid::gList, lapply(args_body, do.call, what = cell_in_rows)), |
407 | 2x |
vp = grid::vpPath("vp_table_layout", "vp_body") |
408 |
),
|
|
409 | 2x |
grid::linesGrob( |
410 | 2x |
grid::unit(c(0, 1), "npc"), |
411 | 2x |
y = grid::unit(c(.5, .5), "npc"), |
412 | 2x |
vp = grid::vpPath("vp_table_layout", "vp_spacer") |
413 |
),
|
|
414 |
# forest part
|
|
415 | 2x |
if (is.null(vline)) { |
416 | ! |
NULL
|
417 |
} else { |
|
418 | 2x |
grid::gTree( |
419 | 2x |
children = grid::gList( |
420 | 2x |
grid::gTree( |
421 | 2x |
children = grid::gList( |
422 |
# this may overflow, to fix, look here
|
|
423 |
# https://stackoverflow.com/questions/33623169/add-multi-line-footnote-to-tablegrob-while-using-gridextra-in-r #nolintr
|
|
424 | 2x |
grid::textGrob( |
425 | 2x |
forest_header[1], |
426 | 2x |
x = grid::unit(vline, "native") - grid::unit(1, "lines"), |
427 | 2x |
just = c("right", "center") |
428 |
),
|
|
429 | 2x |
grid::textGrob( |
430 | 2x |
forest_header[2], |
431 | 2x |
x = grid::unit(vline, "native") + grid::unit(1, "lines"), |
432 | 2x |
just = c("left", "center") |
433 |
)
|
|
434 |
),
|
|
435 | 2x |
vp = grid::vpStack(grid::viewport(layout.pos.col = ncol(tbl) + 2), data_forest_vp) |
436 |
)
|
|
437 |
),
|
|
438 | 2x |
vp = grid::vpPath("vp_table_layout", "vp_header") |
439 |
)
|
|
440 |
},
|
|
441 | 2x |
grid::gTree( |
442 | 2x |
children = grid::gList( |
443 | 2x |
grid::gTree( |
444 | 2x |
children = grid::gList( |
445 | 2x |
grid::rectGrob(gp = grid::gpar(col = "gray90", fill = "gray90")), |
446 | 2x |
if (is.null(vline)) { |
447 | ! |
NULL
|
448 |
} else { |
|
449 | 2x |
grid::linesGrob( |
450 | 2x |
x = grid::unit(rep(vline, 2), "native"), |
451 | 2x |
y = grid::unit(c(0, 1), "npc"), |
452 | 2x |
gp = grid::gpar(lwd = 2), |
453 | 2x |
vp = data_forest_vp |
454 |
)
|
|
455 |
},
|
|
456 | 2x |
grid::xaxisGrob(at = x_at, label = x_labels, vp = data_forest_vp) |
457 |
),
|
|
458 | 2x |
vp = grid::viewport(layout.pos.col = ncol(tbl) + 2) |
459 |
)
|
|
460 |
),
|
|
461 | 2x |
vp = grid::vpPath("vp_table_layout", "vp_body") |
462 |
),
|
|
463 | 2x |
grid::gTree( |
464 | 2x |
children = do.call( |
465 | 2x |
grid::gList, |
466 | 2x |
Map( |
467 | 2x |
function(xi, li, ui, row_index, size_i, col) { |
468 | 9x |
forest_dot_line( |
469 | 9x |
xi,
|
470 | 9x |
li,
|
471 | 9x |
ui,
|
472 | 9x |
row_index,
|
473 | 9x |
xlim,
|
474 | 9x |
symbol_size = size_i, |
475 | 9x |
col = col, |
476 | 9x |
datavp = data_forest_vp |
477 |
)
|
|
478 |
},
|
|
479 | 2x |
x,
|
480 | 2x |
lower,
|
481 | 2x |
upper,
|
482 | 2x |
seq_along(x), |
483 | 2x |
symbol_size,
|
484 | 2x |
col,
|
485 | 2x |
USE.NAMES = FALSE |
486 |
)
|
|
487 |
),
|
|
488 | 2x |
vp = grid::vpPath("vp_table_layout", "vp_body") |
489 |
)
|
|
490 |
),
|
|
491 | 2x |
childrenvp = forest_viewport(tbl, width_row_names, width_columns, width_forest), |
492 | 2x |
vp = vp, |
493 | 2x |
gp = gp |
494 |
)
|
|
495 |
}
|
|
496 | ||
497 | ||
498 |
cell_in_rows <- function(row_name, |
|
499 |
cells,
|
|
500 |
cell_spans,
|
|
501 |
row_index,
|
|
502 |
underline_colspan = FALSE) { |
|
503 | 13x |
checkmate::assert_string(row_name) |
504 | 13x |
checkmate::assert_character(cells, min.len = 1, any.missing = FALSE) |
505 | 13x |
checkmate::assert_numeric(cell_spans, len = length(cells), any.missing = FALSE) |
506 | 13x |
checkmate::assert_number(row_index) |
507 | 13x |
checkmate::assert_flag(underline_colspan) |
508 | ||
509 | 13x |
vp_name_rn <- paste0("rowname-", row_index) |
510 | 13x |
g_rowname <- if (!is.null(row_name) && row_name != "") { |
511 | 10x |
grid::textGrob( |
512 | 10x |
name = vp_name_rn, |
513 | 10x |
label = row_name, |
514 | 10x |
x = grid::unit(0, "npc"), |
515 | 10x |
just = c("left", "center"), |
516 | 10x |
vp = grid::vpPath(paste0("rowname-", row_index)) |
517 |
)
|
|
518 |
} else { |
|
519 | 3x |
NULL
|
520 |
}
|
|
521 | ||
522 | 13x |
gl_cols <- if (!(length(cells) > 0)) { |
523 | ! |
list(NULL) |
524 |
} else { |
|
525 | 13x |
j <- 1 # column index of cell |
526 | ||
527 | 13x |
lapply(seq_along(cells), function(k) { |
528 | 67x |
cell_ascii <- cells[[k]] |
529 | 67x |
cs <- cell_spans[[k]] |
530 | ||
531 | 67x |
if (is.na(cell_ascii) || is.null(cell_ascii)) { |
532 | ! |
cell_ascii <- "NA" |
533 |
}
|
|
534 | ||
535 | 67x |
cell_name <- paste0("g-cell-", row_index, "-", j) |
536 | ||
537 | 67x |
cell_grobs <- if (identical(cell_ascii, "")) { |
538 | 14x |
NULL
|
539 |
} else { |
|
540 | 53x |
if (cs == 1) { |
541 | 49x |
grid::textGrob( |
542 | 49x |
label = cell_ascii, |
543 | 49x |
name = cell_name, |
544 | 49x |
vp = grid::vpPath(paste0("cell-", row_index, "-", j)) |
545 |
)
|
|
546 |
} else { |
|
547 |
# +1 because of rowname
|
|
548 | 4x |
vp_joined_cols <- grid::viewport(layout.pos.row = row_index, layout.pos.col = seq(j + 1, j + cs)) |
549 | ||
550 | 4x |
lab <- grid::textGrob( |
551 | 4x |
label = cell_ascii, |
552 | 4x |
name = cell_name, |
553 | 4x |
vp = vp_joined_cols |
554 |
)
|
|
555 | ||
556 | 4x |
if (!underline_colspan || grepl("^[[:space:]]*$", cell_ascii)) { |
557 | 1x |
lab
|
558 |
} else { |
|
559 | 3x |
grid::gList( |
560 | 3x |
lab,
|
561 | 3x |
grid::linesGrob( |
562 | 3x |
x = grid::unit.c(grid::unit(.2, "lines"), grid::unit(1, "npc") - grid::unit(.2, "lines")), |
563 | 3x |
y = grid::unit(c(0, 0), "npc"), |
564 | 3x |
vp = vp_joined_cols |
565 |
)
|
|
566 |
)
|
|
567 |
}
|
|
568 |
}
|
|
569 |
}
|
|
570 | 67x |
j <<- j + cs |
571 | ||
572 | 67x |
cell_grobs
|
573 |
}) |
|
574 |
}
|
|
575 | ||
576 | 13x |
grid::gList( |
577 | 13x |
g_rowname,
|
578 | 13x |
do.call(grid::gList, gl_cols) |
579 |
)
|
|
580 |
}
|
|
581 | ||
582 |
#' Graphic Object: Forest Dot Line
|
|
583 |
#'
|
|
584 |
#' Calculate the `grob` corresponding to the dot line within the forest plot.
|
|
585 |
#'
|
|
586 |
#' @noRd
|
|
587 |
forest_dot_line <- function(x, |
|
588 |
lower,
|
|
589 |
upper,
|
|
590 |
row_index,
|
|
591 |
xlim,
|
|
592 |
symbol_size = 1, |
|
593 |
col = "blue", |
|
594 |
datavp) { |
|
595 | 9x |
ci <- c(lower, upper) |
596 | 9x |
if (any(!is.na(c(x, ci)))) { |
597 |
# line
|
|
598 | 7x |
y <- grid::unit(c(0.5, 0.5), "npc") |
599 | ||
600 | 7x |
g_line <- if (all(!is.na(ci)) && ci[2] > xlim[1] && ci[1] < xlim[2]) { |
601 |
# -
|
|
602 | 7x |
if (ci[1] >= xlim[1] && ci[2] <= xlim[2]) { |
603 | 2x |
grid::linesGrob(x = grid::unit(c(ci[1], ci[2]), "native"), y = y) |
604 | 5x |
} else if (ci[1] < xlim[1] && ci[2] > xlim[2]) { |
605 |
# <->
|
|
606 | 3x |
grid::linesGrob( |
607 | 3x |
x = grid::unit(xlim, "native"), |
608 | 3x |
y = y, |
609 | 3x |
arrow = grid::arrow(angle = 30, length = grid::unit(0.5, "lines"), ends = "both") |
610 |
)
|
|
611 | 2x |
} else if (ci[1] < xlim[1] && ci[2] <= xlim[2]) { |
612 |
# <-
|
|
613 | ! |
grid::linesGrob( |
614 | ! |
x = grid::unit(c(xlim[1], ci[2]), "native"), |
615 | ! |
y = y, |
616 | ! |
arrow = grid::arrow(angle = 30, length = grid::unit(0.5, "lines"), ends = "first") |
617 |
)
|
|
618 | 2x |
} else if (ci[1] >= xlim[1] && ci[2] > xlim[2]) { |
619 |
# ->
|
|
620 | 2x |
grid::linesGrob( |
621 | 2x |
x = grid::unit(c(ci[1], xlim[2]), "native"), |
622 | 2x |
y = y, |
623 | 2x |
arrow = grid::arrow(angle = 30, length = grid::unit(0.5, "lines"), ends = "last") |
624 |
)
|
|
625 |
}
|
|
626 |
} else { |
|
627 | ! |
NULL
|
628 |
}
|
|
629 | ||
630 | 7x |
g_circle <- if (!is.na(x) && x >= xlim[1] && x <= xlim[2]) { |
631 | 6x |
grid::circleGrob( |
632 | 6x |
x = grid::unit(x, "native"), |
633 | 6x |
y = y, |
634 | 6x |
r = grid::unit(1 / 3.5 * symbol_size, "lines"), |
635 | 6x |
name = "point" |
636 |
)
|
|
637 |
} else { |
|
638 | 1x |
NULL
|
639 |
}
|
|
640 | ||
641 | 7x |
grid::gTree( |
642 | 7x |
children = grid::gList( |
643 | 7x |
grid::gTree( |
644 | 7x |
children = grid::gList( |
645 | 7x |
grid::gList( |
646 | 7x |
g_line,
|
647 | 7x |
g_circle
|
648 |
)
|
|
649 |
),
|
|
650 | 7x |
vp = datavp, |
651 | 7x |
gp = grid::gpar(col = col, fill = col) |
652 |
)
|
|
653 |
),
|
|
654 | 7x |
vp = grid::vpPath(paste0("forest-", row_index)) |
655 |
)
|
|
656 |
} else { |
|
657 | 2x |
NULL
|
658 |
}
|
|
659 |
}
|
|
660 | ||
661 |
#' Create a Viewport Tree for the Forest Plot
|
|
662 |
#' @param tbl (`rtable`)
|
|
663 |
#' @param width_row_names (`grid::unit`)\cr Width of row names
|
|
664 |
#' @param width_columns (`grid::unit`)\cr Width of column spans
|
|
665 |
#' @param width_forest (`grid::unit`)\cr Width of the forest plot
|
|
666 |
#' @param gap_column (`grid::unit`)\cr Gap width between the columns
|
|
667 |
#' @param gap_header (`grid::unit`)\cr Gap width between the header
|
|
668 |
#' @param mat_form matrix print form of the table
|
|
669 |
#' @return A viewport tree.
|
|
670 |
#'
|
|
671 |
#' @examples
|
|
672 |
#' library(grid)
|
|
673 |
#'
|
|
674 |
#' tbl <- rtable(
|
|
675 |
#' header = rheader(
|
|
676 |
#' rrow("", "E", rcell("CI", colspan = 2)),
|
|
677 |
#' rrow("", "A", "B", "C")
|
|
678 |
#' ),
|
|
679 |
#' rrow("row 1", 1, 0.8, 1.1),
|
|
680 |
#' rrow("row 2", 1.4, 0.8, 1.6),
|
|
681 |
#' rrow("row 3", 1.2, 0.8, 1.2)
|
|
682 |
#' )
|
|
683 |
#'
|
|
684 |
#' \donttest{
|
|
685 |
#' v <- forest_viewport(tbl)
|
|
686 |
#'
|
|
687 |
#' grid::grid.newpage()
|
|
688 |
#' showViewport(v)
|
|
689 |
#' }
|
|
690 |
#'
|
|
691 |
#' @export
|
|
692 |
forest_viewport <- function(tbl, |
|
693 |
width_row_names = NULL, |
|
694 |
width_columns = NULL, |
|
695 |
width_forest = grid::unit(1, "null"), |
|
696 |
gap_column = grid::unit(1, "lines"), |
|
697 |
gap_header = grid::unit(1, "lines"), |
|
698 |
mat_form = NULL) { |
|
699 | 2x |
checkmate::assert_class(tbl, "VTableTree") |
700 | 2x |
checkmate::assert_true(grid::is.unit(width_forest)) |
701 | 2x |
if (!is.null(width_row_names)) { |
702 | ! |
checkmate::assert_true(grid::is.unit(width_row_names)) |
703 |
}
|
|
704 | 2x |
if (!is.null(width_columns)) { |
705 | ! |
checkmate::assert_true(grid::is.unit(width_columns)) |
706 |
}
|
|
707 | ||
708 | 2x |
if (is.null(mat_form)) mat_form <- matrix_form(tbl) |
709 | ||
710 | 2x |
mat_form$strings[!mat_form$display] <- "" |
711 | ||
712 | 2x |
nr <- nrow(tbl) |
713 | 2x |
nc <- ncol(tbl) |
714 | 2x |
nr_h <- attr(mat_form, "nrow_header") |
715 | ||
716 | 2x |
if (is.null(width_row_names) || is.null(width_columns)) { |
717 | 2x |
tbl_widths <- formatters::propose_column_widths(mat_form) |
718 | 2x |
strs_with_width <- strrep("x", tbl_widths) # that works for mono spaced fonts |
719 | 2x |
if (is.null(width_row_names)) width_row_names <- grid::stringWidth(strs_with_width[1]) |
720 | 2x |
if (is.null(width_columns)) width_columns <- grid::stringWidth(strs_with_width[-1]) |
721 |
}
|
|
722 | ||
723 |
# Widths for row name, cols, forest.
|
|
724 | 2x |
widths <- grid::unit.c( |
725 | 2x |
width_row_names + gap_column, |
726 | 2x |
width_columns + gap_column, |
727 | 2x |
width_forest
|
728 |
)
|
|
729 | ||
730 | 2x |
n_lines_per_row <- apply( |
731 | 2x |
X = mat_form$strings, |
732 | 2x |
MARGIN = 1, |
733 | 2x |
FUN = function(row) { |
734 | 13x |
tmp <- vapply( |
735 | 13x |
gregexpr("\n", row, fixed = TRUE), |
736 | 13x |
attr, numeric(1), |
737 | 13x |
"match.length"
|
738 | 13x |
) + 1 |
739 | 13x |
max(c(tmp, 1)) |
740 |
}
|
|
741 |
)
|
|
742 | ||
743 | 2x |
i_header <- seq_len(nr_h) |
744 | ||
745 | 2x |
height_body_rows <- grid::unit(n_lines_per_row[-i_header] * 1.2, "lines") |
746 | 2x |
height_header_rows <- grid::unit(n_lines_per_row[i_header] * 1.2, "lines") |
747 | ||
748 | 2x |
height_body <- grid::unit(sum(n_lines_per_row[-i_header]) * 1.2, "lines") |
749 | 2x |
height_header <- grid::unit(sum(n_lines_per_row[i_header]) * 1.2, "lines") |
750 | ||
751 | 2x |
nc_g <- nc + 2 # number of columns incl. row names and forest |
752 | ||
753 | 2x |
vp_tbl <- grid::vpTree( |
754 | 2x |
parent = grid::viewport( |
755 | 2x |
name = "vp_table_layout", |
756 | 2x |
layout = grid::grid.layout( |
757 | 2x |
nrow = 3, ncol = 1, |
758 | 2x |
heights = grid::unit.c(height_header, gap_header, height_body) |
759 |
)
|
|
760 |
),
|
|
761 | 2x |
children = grid::vpList( |
762 | 2x |
vp_forest_table_part(nr_h, nc_g, 1, 1, widths, height_header_rows, "vp_header"), |
763 | 2x |
vp_forest_table_part(nr, nc_g, 3, 1, widths, height_body_rows, "vp_body"), |
764 | 2x |
grid::viewport(name = "vp_spacer", layout.pos.row = 2, layout.pos.col = 1) |
765 |
)
|
|
766 |
)
|
|
767 | 2x |
vp_tbl
|
768 |
}
|
|
769 | ||
770 |
#' Viewport Forest Plot: Table Part
|
|
771 |
#'
|
|
772 |
#' Prepares a viewport for the table included in the forest plot.
|
|
773 |
#'
|
|
774 |
#' @noRd
|
|
775 |
vp_forest_table_part <- function(nrow, |
|
776 |
ncol,
|
|
777 |
l_row,
|
|
778 |
l_col,
|
|
779 |
widths,
|
|
780 |
heights,
|
|
781 |
name) { |
|
782 | 4x |
grid::vpTree( |
783 | 4x |
grid::viewport( |
784 | 4x |
name = name, |
785 | 4x |
layout.pos.row = l_row, |
786 | 4x |
layout.pos.col = l_col, |
787 | 4x |
layout = grid::grid.layout(nrow = nrow, ncol = ncol, widths = widths, heights = heights) |
788 |
),
|
|
789 | 4x |
children = grid::vpList( |
790 | 4x |
do.call( |
791 | 4x |
grid::vpList, |
792 | 4x |
lapply( |
793 | 4x |
seq_len(nrow), function(i) { |
794 | 13x |
grid::viewport(layout.pos.row = i, layout.pos.col = 1, name = paste0("rowname-", i)) |
795 |
}
|
|
796 |
)
|
|
797 |
),
|
|
798 | 4x |
do.call( |
799 | 4x |
grid::vpList, |
800 | 4x |
apply( |
801 | 4x |
expand.grid(seq_len(nrow), seq_len(ncol - 2)), |
802 | 4x |
1,
|
803 | 4x |
function(x) { |
804 | 71x |
i <- x[1] |
805 | 71x |
j <- x[2] |
806 | 71x |
grid::viewport(layout.pos.row = i, layout.pos.col = j + 1, name = paste0("cell-", i, "-", j)) |
807 |
}
|
|
808 |
)
|
|
809 |
),
|
|
810 | 4x |
do.call( |
811 | 4x |
grid::vpList, |
812 | 4x |
lapply( |
813 | 4x |
seq_len(nrow), |
814 | 4x |
function(i) { |
815 | 13x |
grid::viewport(layout.pos.row = i, layout.pos.col = ncol, name = paste0("forest-", i)) |
816 |
}
|
|
817 |
)
|
|
818 |
)
|
|
819 |
)
|
|
820 |
)
|
|
821 |
}
|
|
822 | ||
823 |
#' Forest Rendering
|
|
824 |
#'
|
|
825 |
#' Renders the forest grob.
|
|
826 |
#'
|
|
827 |
#' @noRd
|
|
828 |
grid.forest <- function(...) { # nolint |
|
829 | ! |
grid::grid.draw(forest_grob(...)) |
830 |
}
|
1 |
#' Helper Function to create a map dataframe that can be used in `trim_levels_to_map` split function.
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Helper Function to create a map dataframe from the input dataset, which can be used as an argument in the
|
|
6 |
#' `trim_levels_to_map` split function. Based on different method, the map is constructed differently.
|
|
7 |
#'
|
|
8 |
#' @inheritParams argument_convention
|
|
9 |
#' @param abnormal (named `list`)\cr identifying the abnormal range level(s) in `df`. Based on the levels of
|
|
10 |
#' abnormality of the input dataset, it can be something like `list(Low = "LOW LOW", High = "HIGH HIGH")` or
|
|
11 |
#' `abnormal = list(Low = "LOW", High = "HIGH"))`
|
|
12 |
#' @param method (`string`)\cr indicates how the returned map will be constructed. Can be `"default"` or `"range"`.
|
|
13 |
#'
|
|
14 |
#' @return A map `data.frame`.
|
|
15 |
#'
|
|
16 |
#' @note If method is `"default"`, the returned map will only have the abnormal directions that are observed in the
|
|
17 |
#' `df`, and records with all normal values will be excluded to avoid error in creating layout. If method is
|
|
18 |
#' `"range"`, the returned map will be based on the rule that at least one observation with low range > 0
|
|
19 |
#' for low direction and at least one observation with high range is not missing for high direction.
|
|
20 |
#'
|
|
21 |
#' @examples
|
|
22 |
#' adlb <- df_explicit_na(tern_ex_adlb)
|
|
23 |
#'
|
|
24 |
#' h_map_for_count_abnormal(
|
|
25 |
#' df = adlb,
|
|
26 |
#' variables = list(anl = "ANRIND", split_rows = c("LBCAT", "PARAM")),
|
|
27 |
#' abnormal = list(low = c("LOW"), high = c("HIGH")),
|
|
28 |
#' method = "default",
|
|
29 |
#' na_level = "<Missing>"
|
|
30 |
#' )
|
|
31 |
#'
|
|
32 |
#' df <- data.frame(
|
|
33 |
#' USUBJID = c(rep("1", 4), rep("2", 4), rep("3", 4)),
|
|
34 |
#' AVISIT = c(
|
|
35 |
#' rep("WEEK 1", 2),
|
|
36 |
#' rep("WEEK 2", 2),
|
|
37 |
#' rep("WEEK 1", 2),
|
|
38 |
#' rep("WEEK 2", 2),
|
|
39 |
#' rep("WEEK 1", 2),
|
|
40 |
#' rep("WEEK 2", 2)
|
|
41 |
#' ),
|
|
42 |
#' PARAM = rep(c("ALT", "CPR"), 6),
|
|
43 |
#' ANRIND = c(
|
|
44 |
#' "NORMAL", "NORMAL", "LOW",
|
|
45 |
#' "HIGH", "LOW", "LOW", "HIGH", "HIGH", rep("NORMAL", 4)
|
|
46 |
#' ),
|
|
47 |
#' ANRLO = rep(5, 12),
|
|
48 |
#' ANRHI = rep(20, 12)
|
|
49 |
#' )
|
|
50 |
#' df$ANRIND <- factor(df$ANRIND, levels = c("LOW", "HIGH", "NORMAL"))
|
|
51 |
#' h_map_for_count_abnormal(
|
|
52 |
#' df = df,
|
|
53 |
#' variables = list(
|
|
54 |
#' anl = "ANRIND",
|
|
55 |
#' split_rows = c("PARAM"),
|
|
56 |
#' range_low = "ANRLO",
|
|
57 |
#' range_high = "ANRHI"
|
|
58 |
#' ),
|
|
59 |
#' abnormal = list(low = c("LOW"), high = c("HIGH")),
|
|
60 |
#' method = "range",
|
|
61 |
#' na_level = "<Missing>"
|
|
62 |
#' )
|
|
63 |
#'
|
|
64 |
#' @export
|
|
65 |
h_map_for_count_abnormal <- function(df, |
|
66 |
variables = list( |
|
67 |
anl = "ANRIND", |
|
68 |
split_rows = c("PARAM"), |
|
69 |
range_low = "ANRLO", |
|
70 |
range_high = "ANRHI" |
|
71 |
),
|
|
72 |
abnormal = list(low = c("LOW", "LOW LOW"), high = c("HIGH", "HIGH HIGH")), |
|
73 |
method = c("default", "range"), |
|
74 |
na_level = "<Missing>") { |
|
75 | 7x |
method <- match.arg(method) |
76 | 7x |
checkmate::assert_subset(c("anl", "split_rows"), names(variables)) |
77 | 7x |
checkmate::assert_false(anyNA(df[variables$split_rows])) |
78 | 7x |
assert_df_with_variables(df, |
79 | 7x |
variables = list(anl = variables$anl, split_rows = variables$split_rows), |
80 | 7x |
na_level = na_level |
81 |
)
|
|
82 | 7x |
assert_df_with_factors(df, list(val = variables$anl)) |
83 | 7x |
assert_valid_factor(df[[variables$anl]], any.missing = FALSE) |
84 | 7x |
assert_list_of_variables(variables) |
85 | 7x |
checkmate::assert_list(abnormal, types = "character", len = 2) |
86 | ||
87 |
# Drop usued levels from df as they are not supposed to be in the final map
|
|
88 | 7x |
df <- droplevels(df) |
89 | ||
90 | 7x |
normal_value <- setdiff(levels(df[[variables$anl]]), unlist(abnormal)) |
91 | ||
92 |
# Based on the understanding of clinical data, there should only be one level of normal which is "NORMAL"
|
|
93 | 7x |
checkmate::assert_vector(normal_value, len = 1) |
94 | ||
95 |
# Default method will only have what is observed in the df, and records with all normal values will be excluded to
|
|
96 |
# avoid error in layout building.
|
|
97 | 7x |
if (method == "default") { |
98 | 3x |
df_abnormal <- subset(df, df[[variables$anl]] %in% unlist(abnormal)) |
99 | 3x |
map <- unique(df_abnormal[c(variables$split_rows, variables$anl)]) |
100 | 3x |
map_normal <- unique(subset(map, select = variables$split_rows)) |
101 | 3x |
map_normal[[variables$anl]] <- normal_value |
102 | 3x |
map <- rbind(map, map_normal) |
103 | 4x |
} else if (method == "range") { |
104 |
# range method follows the rule that at least one observation with ANRLO > 0 for low
|
|
105 |
# direction and at least one observation with ANRHI is not missing for high direction.
|
|
106 | 4x |
checkmate::assert_subset(c("range_low", "range_high"), names(variables)) |
107 | 4x |
checkmate::assert_subset(c("LOW", "HIGH"), toupper(names(abnormal))) |
108 | ||
109 | 4x |
assert_df_with_variables(df, |
110 | 4x |
variables = list( |
111 | 4x |
range_low = variables$range_low, |
112 | 4x |
range_high = variables$range_high |
113 |
)
|
|
114 |
)
|
|
115 | ||
116 |
# Define low direction of map
|
|
117 | 4x |
df_low <- subset(df, df[[variables$range_low]] > 0) |
118 | 4x |
map_low <- unique(df_low[variables$split_rows]) |
119 | 4x |
low_levels <- unname(unlist(abnormal[toupper(names(abnormal)) == "LOW"])) |
120 | 4x |
low_levels_df <- as.data.frame(low_levels) |
121 | 4x |
colnames(low_levels_df) <- variables$anl |
122 | 4x |
low_levels_df <- do.call("rbind", replicate(nrow(map_low), low_levels_df, simplify = FALSE)) |
123 | 4x |
rownames(map_low) <- NULL # Just to avoid strange row index in case upstream functions changed |
124 | 4x |
map_low <- map_low[rep(seq_len(nrow(map_low)), each = length(low_levels)), , drop = FALSE] |
125 | 4x |
map_low <- cbind(map_low, low_levels_df) |
126 | ||
127 |
# Define high direction of map
|
|
128 | 4x |
df_high <- subset(df, df[[variables$range_high]] != na_level | !is.na(df[[variables$range_high]])) |
129 | 4x |
map_high <- unique(df_high[variables$split_rows]) |
130 | 4x |
high_levels <- unname(unlist(abnormal[toupper(names(abnormal)) == "HIGH"])) |
131 | 4x |
high_levels_df <- as.data.frame(high_levels) |
132 | 4x |
colnames(high_levels_df) <- variables$anl |
133 | 4x |
high_levels_df <- do.call("rbind", replicate(nrow(map_high), high_levels_df, simplify = FALSE)) |
134 | 4x |
rownames(map_high) <- NULL |
135 | 4x |
map_high <- map_high[rep(seq_len(nrow(map_high)), each = length(high_levels)), , drop = FALSE] |
136 | 4x |
map_high <- cbind(map_high, high_levels_df) |
137 | ||
138 |
# Define normal of map
|
|
139 | 4x |
map_normal <- unique(rbind(map_low, map_high)[variables$split_rows]) |
140 | 4x |
map_normal[variables$anl] <- normal_value |
141 | ||
142 | 4x |
map <- rbind(map_low, map_high, map_normal) |
143 |
}
|
|
144 | ||
145 |
# map should be all characters
|
|
146 | 7x |
map <- data.frame(lapply(map, as.character), stringsAsFactors = FALSE) |
147 | ||
148 |
# sort the map final output by split_rows variables
|
|
149 | 7x |
for (i in rev(seq_len(length(variables$split_rows)))) { |
150 | 7x |
map <- map[order(map[[i]]), ] |
151 |
}
|
|
152 | 7x |
map
|
153 |
}
|
1 |
#' Number of Patients
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Count the number of unique and non-unique patients in a column (variable).
|
|
6 |
#'
|
|
7 |
#' @inheritParams argument_convention
|
|
8 |
#' @param x (`character` or `factor`)\cr vector of patient IDs.
|
|
9 |
#' @param count_by (`character` or `factor`)\cr optional vector to be combined with `x` when counting
|
|
10 |
#' `nonunique` records.
|
|
11 |
#' @param unique_count_suffix (`logical`)\cr should `"(n)"` suffix be added to `unique_count` labels.
|
|
12 |
#' Defaults to `TRUE`.
|
|
13 |
#'
|
|
14 |
#' @name summarize_num_patients
|
|
15 |
NULL
|
|
16 | ||
17 |
#' @describeIn summarize_num_patients Statistics function which counts the number of
|
|
18 |
#' unique patients, the corresponding percentage taken with respect to the
|
|
19 |
#' total number of patients, and the number of non-unique patients.
|
|
20 |
#'
|
|
21 |
#' @return
|
|
22 |
#' * `s_num_patients()` returns a named `list` of 3 statistics:
|
|
23 |
#' * `unique`: Vector of counts and percentages.
|
|
24 |
#' * `nonunique`: Vector of counts.
|
|
25 |
#' * `unique_count`: Counts.
|
|
26 |
#'
|
|
27 |
#' @examples
|
|
28 |
#' # Use the statistics function to count number of unique and nonunique patients.
|
|
29 |
#' s_num_patients(x = as.character(c(1, 1, 1, 2, 4, NA)), labelstr = "", .N_col = 6L)
|
|
30 |
#' s_num_patients(
|
|
31 |
#' x = as.character(c(1, 1, 1, 2, 4, NA)),
|
|
32 |
#' labelstr = "",
|
|
33 |
#' .N_col = 6L,
|
|
34 |
#' count_by = as.character(c(1, 1, 2, 1, 1, 1))
|
|
35 |
#' )
|
|
36 |
#'
|
|
37 |
#' @export
|
|
38 |
s_num_patients <- function(x, labelstr, .N_col, count_by = NULL, unique_count_suffix = TRUE) { # nolint |
|
39 | ||
40 | 84x |
checkmate::assert_string(labelstr) |
41 | 84x |
checkmate::assert_count(.N_col) |
42 | 84x |
checkmate::assert_multi_class(x, classes = c("factor", "character")) |
43 | 84x |
checkmate::assert_flag(unique_count_suffix) |
44 | ||
45 | 84x |
count1 <- n_available(unique(x)) |
46 | 84x |
count2 <- n_available(x) |
47 | ||
48 | 84x |
if (!is.null(count_by)) { |
49 | 10x |
checkmate::assert_vector(count_by, len = length(x)) |
50 | 10x |
checkmate::assert_multi_class(count_by, classes = c("factor", "character")) |
51 | 10x |
count2 <- n_available(unique(interaction(x, count_by))) |
52 |
}
|
|
53 | ||
54 | 84x |
out <- list( |
55 | 84x |
unique = formatters::with_label(c(count1, ifelse(count1 == 0 && .N_col == 0, 0, count1 / .N_col)), labelstr), |
56 | 84x |
nonunique = formatters::with_label(count2, labelstr), |
57 | 84x |
unique_count = formatters::with_label(count1, ifelse(unique_count_suffix, paste(labelstr, "(n)"), labelstr)) |
58 |
)
|
|
59 | ||
60 | 84x |
out
|
61 |
}
|
|
62 | ||
63 |
#' @describeIn summarize_num_patients Statistics function which counts the number of unique patients
|
|
64 |
#' in a column (variable), the corresponding percentage taken with respect to the total number of
|
|
65 |
#' patients, and the number of non-unique patients in the column.
|
|
66 |
#'
|
|
67 |
#' @param required (`character` or `NULL`)\cr optional name of a variable that is required to be non-missing.
|
|
68 |
#'
|
|
69 |
#' @return
|
|
70 |
#' * `s_num_patients_content()` returns the same values as `s_num_patients()`.
|
|
71 |
#'
|
|
72 |
#' @examples
|
|
73 |
#' # Count number of unique and non-unique patients.
|
|
74 |
#' df <- data.frame(
|
|
75 |
#' USUBJID = as.character(c(1, 2, 1, 4, NA)),
|
|
76 |
#' EVENT = as.character(c(10, 15, 10, 17, 8))
|
|
77 |
#' )
|
|
78 |
#' s_num_patients_content(df, .N_col = 5, .var = "USUBJID")
|
|
79 |
#'
|
|
80 |
#' df_by_event <- data.frame(
|
|
81 |
#' USUBJID = as.character(c(1, 2, 1, 4, NA)),
|
|
82 |
#' EVENT = as.character(c(10, 15, 10, 17, 8))
|
|
83 |
#' )
|
|
84 |
#' s_num_patients_content(df_by_event, .N_col = 5, .var = "USUBJID")
|
|
85 |
#' s_num_patients_content(df_by_event, .N_col = 5, .var = "USUBJID", count_by = "EVENT")
|
|
86 |
#'
|
|
87 |
#' @export
|
|
88 |
s_num_patients_content <- function(df, |
|
89 |
labelstr = "", |
|
90 |
.N_col, # nolint |
|
91 |
.var,
|
|
92 |
required = NULL, |
|
93 |
count_by = NULL, |
|
94 |
unique_count_suffix = TRUE) { |
|
95 | 36x |
checkmate::assert_string(.var) |
96 | 36x |
checkmate::assert_data_frame(df) |
97 | 36x |
if (is.null(count_by)) { |
98 | 33x |
assert_df_with_variables(df, list(id = .var)) |
99 |
} else { |
|
100 | 3x |
assert_df_with_variables(df, list(id = .var, count_by = count_by)) |
101 |
}
|
|
102 | 36x |
if (!is.null(required)) { |
103 | ! |
checkmate::assert_string(required) |
104 | ! |
assert_df_with_variables(df, list(required = required)) |
105 | ! |
df <- df[!is.na(df[[required]]), , drop = FALSE] |
106 |
}
|
|
107 | ||
108 | 36x |
x <- df[[.var]] |
109 | 36x |
y <- switch(as.numeric(!is.null(count_by)) + 1, |
110 | 36x |
NULL,
|
111 | 36x |
df[[count_by]] |
112 |
)
|
|
113 | ||
114 | 36x |
s_num_patients( |
115 | 36x |
x = x, |
116 | 36x |
labelstr = labelstr, |
117 | 36x |
.N_col = .N_col, |
118 | 36x |
count_by = y, |
119 | 36x |
unique_count_suffix = unique_count_suffix |
120 |
)
|
|
121 |
}
|
|
122 | ||
123 |
c_num_patients <- make_afun( |
|
124 |
s_num_patients_content,
|
|
125 |
.stats = c("unique", "nonunique", "unique_count"), |
|
126 |
.formats = c(unique = format_count_fraction_fixed_dp, nonunique = "xx", unique_count = "xx") |
|
127 |
)
|
|
128 | ||
129 |
#' @describeIn summarize_num_patients Layout-creating function which can take statistics function arguments
|
|
130 |
#' and additional format arguments. This function is a wrapper for [rtables::summarize_row_groups()].
|
|
131 |
#'
|
|
132 |
#' @return
|
|
133 |
#' * `summarize_num_patients()` returns a layout object suitable for passing to further layouting functions,
|
|
134 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing
|
|
135 |
#' the statistics from `s_num_patients_content()` to the table layout.
|
|
136 |
#'
|
|
137 |
#' @export
|
|
138 |
summarize_num_patients <- function(lyt, |
|
139 |
var,
|
|
140 |
.stats = NULL, |
|
141 |
.formats = NULL, |
|
142 |
.labels = c( |
|
143 |
unique = "Number of patients with at least one event", |
|
144 |
nonunique = "Number of events" |
|
145 |
),
|
|
146 |
indent_mod = lifecycle::deprecated(), |
|
147 |
.indent_mods = 0L, |
|
148 |
...) { |
|
149 | 8x |
if (lifecycle::is_present(indent_mod)) { |
150 | ! |
lifecycle::deprecate_warn("0.8.2", "summarize_num_patients(indent_mod)", "summarize_num_patients(.indent_mods)") |
151 | ! |
.indent_mods <- indent_mod |
152 |
}
|
|
153 | ||
154 | 4x |
if (is.null(.stats)) .stats <- c("unique", "nonunique", "unique_count") |
155 | 1x |
if (length(.labels) > length(.stats)) .labels <- .labels[names(.labels) %in% .stats] |
156 | ||
157 | 8x |
cfun <- make_afun( |
158 | 8x |
c_num_patients,
|
159 | 8x |
.stats = .stats, |
160 | 8x |
.formats = .formats, |
161 | 8x |
.labels = .labels |
162 |
)
|
|
163 | ||
164 | 8x |
summarize_row_groups( |
165 | 8x |
lyt = lyt, |
166 | 8x |
var = var, |
167 | 8x |
cfun = cfun, |
168 | 8x |
extra_args = list(...), |
169 | 8x |
indent_mod = .indent_mods |
170 |
)
|
|
171 |
}
|
|
172 | ||
173 |
#' @describeIn summarize_num_patients Layout-creating function which can take statistics function arguments
|
|
174 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()].
|
|
175 |
#'
|
|
176 |
#' @return
|
|
177 |
#' * `analyze_num_patients()` returns a layout object suitable for passing to further layouting functions,
|
|
178 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing
|
|
179 |
#' the statistics from `s_num_patients_content()` to the table layout.
|
|
180 |
#'
|
|
181 |
#' @details In general, functions that starts with `analyze*` are expected to
|
|
182 |
#' work like [rtables::analyze()], while functions that starts with `summarize*`
|
|
183 |
#' are based upon [rtables::summarize_row_groups()]. The latter provides a
|
|
184 |
#' value for each dividing split in the row and column space, but, being it
|
|
185 |
#' bound to the fundamental splits, it is repeated by design in every page
|
|
186 |
#' when pagination is involved.
|
|
187 |
#'
|
|
188 |
#' @note As opposed to [summarize_num_patients()], this function does not repeat the produced rows.
|
|
189 |
#'
|
|
190 |
#' @examples
|
|
191 |
#' df_tmp <- data.frame(
|
|
192 |
#' USUBJID = as.character(c(1, 2, 1, 4, NA, 6, 6, 8, 9)),
|
|
193 |
#' ARM = c("A", "A", "A", "A", "A", "B", "B", "B", "B"),
|
|
194 |
#' AGE = c(10, 15, 10, 17, 8, 11, 11, 19, 17)
|
|
195 |
#' )
|
|
196 |
#' tbl <- basic_table() %>%
|
|
197 |
#' split_cols_by("ARM") %>%
|
|
198 |
#' add_colcounts() %>%
|
|
199 |
#' analyze_num_patients("USUBJID", .stats = c("unique")) %>%
|
|
200 |
#' build_table(df_tmp)
|
|
201 |
#' tbl
|
|
202 |
#'
|
|
203 |
#' @export
|
|
204 |
analyze_num_patients <- function(lyt, |
|
205 |
vars,
|
|
206 |
.stats = NULL, |
|
207 |
.formats = NULL, |
|
208 |
.labels = c( |
|
209 |
unique = "Number of patients with at least one event", |
|
210 |
nonunique = "Number of events" |
|
211 |
),
|
|
212 |
show_labels = c("default", "visible", "hidden"), |
|
213 |
indent_mod = lifecycle::deprecated(), |
|
214 |
.indent_mods = 0L, |
|
215 |
...) { |
|
216 | 2x |
if (lifecycle::is_present(indent_mod)) { |
217 | ! |
lifecycle::deprecate_warn("0.8.2", "analyze_num_patients(indent_mod)", "analyze_num_patients(.indent_mods)") |
218 | ! |
.indent_mods <- indent_mod |
219 |
}
|
|
220 | ||
221 | ! |
if (is.null(.stats)) .stats <- c("unique", "nonunique", "unique_count") |
222 | ! |
if (length(.labels) > length(.stats)) .labels <- .labels[names(.labels) %in% .stats] |
223 | ||
224 | 2x |
afun <- make_afun( |
225 | 2x |
c_num_patients,
|
226 | 2x |
.stats = .stats, |
227 | 2x |
.formats = .formats, |
228 | 2x |
.labels = .labels |
229 |
)
|
|
230 | ||
231 | 2x |
analyze( |
232 | 2x |
afun = afun, |
233 | 2x |
lyt = lyt, |
234 | 2x |
vars = vars, |
235 | 2x |
extra_args = list(...), |
236 | 2x |
show_labels = show_labels, |
237 | 2x |
indent_mod = .indent_mods |
238 |
)
|
|
239 |
}
|
1 |
#' Additional Assertions for `checkmate`
|
|
2 |
#'
|
|
3 |
#' Additional assertion functions which can be used together with the `checkmate` package.
|
|
4 |
#'
|
|
5 |
#' @inheritParams checkmate::assert_factor
|
|
6 |
#' @param x (`any`)\cr object to test.
|
|
7 |
#' @param df (`data.frame`)\cr data set to test.
|
|
8 |
#' @param variables (named `list` of `character`)\cr list of variables to test.
|
|
9 |
#' @param include_boundaries (`logical`)\cr whether to include boundaries when testing
|
|
10 |
#' for proportions.
|
|
11 |
#' @param na_level (`character`)\cr the string you have been using to represent NA or
|
|
12 |
#' missing data. For `NA` values please consider using directly [is.na()] or
|
|
13 |
#' similar approaches.
|
|
14 |
#' @param (`integer`)\cr minimum number of factor levels. Default is `1`.
|
|
15 |
#' @param ... a collection of objects to test.
|
|
16 |
#'
|
|
17 |
#' @return Nothing if assertion passes, otherwise prints the error message.
|
|
18 |
#'
|
|
19 |
#' @name assertions
|
|
20 |
NULL
|
|
21 | ||
22 |
check_list_of_variables <- function(x) { |
|
23 |
# drop NULL elements in list
|
|
24 | 2156x |
x <- Filter(Negate(is.null), x) |
25 | ||
26 | 2156x |
res <- checkmate::check_list(x, |
27 | 2156x |
names = "named", |
28 | 2156x |
min.len = 1, |
29 | 2156x |
any.missing = FALSE, |
30 | 2156x |
types = "character" |
31 |
)
|
|
32 |
# no empty strings allowed
|
|
33 | 2156x |
if (isTRUE(res)) { |
34 | 2151x |
res <- checkmate::check_character(unlist(x), min.chars = 1) |
35 |
}
|
|
36 | 2156x |
return(res) |
37 |
}
|
|
38 |
#' @describeIn assertions Checks whether `x` is a valid list of variable names.
|
|
39 |
#' `NULL` elements of the list `x` are dropped with `Filter(Negate(is.null), x)`.
|
|
40 |
#'
|
|
41 |
#' @keywords internal
|
|
42 |
assert_list_of_variables <- checkmate::makeAssertionFunction(check_list_of_variables) |
|
43 | ||
44 |
check_df_with_variables <- function(df, variables, na_level = NULL) { |
|
45 | 1922x |
checkmate::assert_data_frame(df) |
46 | 1920x |
assert_list_of_variables(variables) |
47 | ||
48 |
# flag for equal variables and column names
|
|
49 | 1918x |
err_flag <- all(unlist(variables) %in% colnames(df)) |
50 | 1918x |
checkmate::assert_flag(err_flag) |
51 | ||
52 | 1918x |
if (isFALSE(err_flag)) { |
53 | 5x |
vars <- setdiff(unlist(variables), colnames(df)) |
54 | 5x |
return(paste( |
55 | 5x |
deparse(substitute(df)), |
56 | 5x |
"does not contain all specified variables as column names. Missing from dataframe:",
|
57 | 5x |
paste(vars, collapse = ", ") |
58 |
)) |
|
59 |
}
|
|
60 |
# checking if na_level is present and in which column
|
|
61 | 1913x |
if (!is.null(na_level)) { |
62 | 9x |
checkmate::assert_string(na_level) |
63 | 9x |
res <- unlist(lapply(as.list(df)[unlist(variables)], function(x) any(x == na_level))) |
64 | 9x |
if (any(res)) { |
65 | 1x |
return(paste0( |
66 | 1x |
deparse(substitute(df)), " contains explicit na_level (", na_level, |
67 | 1x |
") in the following columns: ", paste0(unlist(variables)[res], |
68 | 1x |
collapse = ", " |
69 |
)
|
|
70 |
)) |
|
71 |
}
|
|
72 |
}
|
|
73 | 1912x |
return(TRUE) |
74 |
}
|
|
75 |
#' @describeIn assertions Check whether `df` is a data frame with the analysis `variables`.
|
|
76 |
#' Please notice how this produces an error when not all variables are present in the
|
|
77 |
#' data.frame while the opposite is not required.
|
|
78 |
#'
|
|
79 |
#' @keywords internal
|
|
80 |
assert_df_with_variables <- checkmate::makeAssertionFunction(check_df_with_variables) |
|
81 | ||
82 |
check_valid_factor <- function(x, |
|
83 |
min.levels = 1, # nolint |
|
84 |
max.levels = NULL, # nolint |
|
85 |
null.ok = TRUE, # nolint |
|
86 |
any.missing = TRUE, # nolint |
|
87 |
n.levels = NULL, # nolint |
|
88 |
len = NULL) { |
|
89 |
# checks on levels insertion
|
|
90 | 802x |
checkmate::assert_int(min.levels, lower = 1) |
91 | ||
92 |
# main factor check
|
|
93 | 802x |
res <- checkmate::check_factor(x, |
94 | 802x |
min.levels = min.levels, |
95 | 802x |
null.ok = null.ok, |
96 | 802x |
max.levels = max.levels, |
97 | 802x |
any.missing = any.missing, |
98 | 802x |
n.levels = n.levels |
99 |
)
|
|
100 | ||
101 |
# no empty strings allowed
|
|
102 | 802x |
if (isTRUE(res)) { |
103 | 788x |
res <- checkmate::check_character(levels(x), min.chars = 1) |
104 |
}
|
|
105 | ||
106 | 802x |
return(res) |
107 |
}
|
|
108 |
#' @describeIn assertions Check whether `x` is a valid factor (i.e. has levels and no empty
|
|
109 |
#' string levels). Note that `NULL` and `NA` elements are allowed.
|
|
110 |
#'
|
|
111 |
#' @keywords internal
|
|
112 |
assert_valid_factor <- checkmate::makeAssertionFunction(check_valid_factor) |
|
113 | ||
114 | ||
115 |
check_df_with_factors <- function(df, |
|
116 |
variables,
|
|
117 |
min.levels = 1, # nolint |
|
118 |
max.levels = NULL, # nolint |
|
119 |
any.missing = TRUE, # nolint |
|
120 |
na_level = NULL) { |
|
121 | 190x |
res <- check_df_with_variables(df, variables, na_level) |
122 |
# checking if all the columns specified by variables are valid factors
|
|
123 | 189x |
if (isTRUE(res)) { |
124 |
# searching the data.frame with selected columns (variables) as a list
|
|
125 | 187x |
res <- lapply( |
126 | 187x |
X = as.list(df)[unlist(variables)], |
127 | 187x |
FUN = check_valid_factor, |
128 | 187x |
min.levels = min.levels, |
129 | 187x |
max.levels = max.levels, |
130 | 187x |
any.missing = any.missing |
131 |
)
|
|
132 | 187x |
res_lo <- unlist(vapply(res, Negate(isTRUE), logical(1))) |
133 | 187x |
if (any(res_lo)) { |
134 | 6x |
return(paste0( |
135 | 6x |
deparse(substitute(df)), " does not contain only factor variables among:", |
136 | 6x |
"\n* Column `", paste0(unlist(variables)[res_lo], |
137 | 6x |
"` of the data.frame -> ", res[res_lo], |
138 | 6x |
collapse = "\n* " |
139 |
)
|
|
140 |
)) |
|
141 |
} else { |
|
142 | 181x |
res <- TRUE |
143 |
}
|
|
144 |
}
|
|
145 | 183x |
return(res) |
146 |
}
|
|
147 |
#' @describeIn assertions Check whether `df` is a data frame where the analysis `variables`
|
|
148 |
#' are all factors. Note that the creation of `NA` by direct call of `factor()` will
|
|
149 |
#' trim `NA` levels out of the vector list itself.
|
|
150 |
#'
|
|
151 |
#' @keywords internal
|
|
152 |
assert_df_with_factors <- checkmate::makeAssertionFunction(check_df_with_factors) |
|
153 | ||
154 |
#' @describeIn assertions Check whether `x` is a proportion: number between 0 and 1.
|
|
155 |
#'
|
|
156 |
#' @keywords internal
|
|
157 |
assert_proportion_value <- function(x, include_boundaries = FALSE) { |
|
158 | 6225x |
checkmate::assert_number(x, lower = 0, upper = 1) |
159 | 6213x |
checkmate::assert_flag(include_boundaries) |
160 | 6213x |
if (isFALSE(include_boundaries)) { |
161 | 2495x |
checkmate::assert_true(x > 0) |
162 | 2493x |
checkmate::assert_true(x < 1) |
163 |
}
|
|
164 |
}
|
1 |
#' Control Function for `CoxPH` Model
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' This is an auxiliary function for controlling arguments for `CoxPH` model, typically used internally to specify
|
|
6 |
#' details of `CoxPH` model for [s_coxph_pairwise()]. `conf_level` refers to Hazard Ratio estimation.
|
|
7 |
#'
|
|
8 |
#' @inheritParams argument_convention
|
|
9 |
#' @param pval_method (`string`)\cr p-value method for testing hazard ratio = 1.
|
|
10 |
#' Default method is `"log-rank"`, can also be set to `"wald"` or `"likelihood"`.
|
|
11 |
#' @param ties (`string`)\cr specifying the method for tie handling. Default is `"efron"`,
|
|
12 |
#' can also be set to `"breslow"` or `"exact"`. See more in [survival::coxph()].
|
|
13 |
#'
|
|
14 |
#' @return A list of components with the same names as the arguments
|
|
15 |
#'
|
|
16 |
#' @export
|
|
17 |
control_coxph <- function(pval_method = c("log-rank", "wald", "likelihood"), |
|
18 |
ties = c("efron", "breslow", "exact"), |
|
19 |
conf_level = 0.95) { |
|
20 | 40x |
pval_method <- match.arg(pval_method) |
21 | 39x |
ties <- match.arg(ties) |
22 | 39x |
assert_proportion_value(conf_level) |
23 | ||
24 | 38x |
list(pval_method = pval_method, ties = ties, conf_level = conf_level) |
25 |
}
|
|
26 | ||
27 |
#' Control Function for `survfit` Model for Survival Time
|
|
28 |
#'
|
|
29 |
#' @description `r lifecycle::badge("stable")`
|
|
30 |
#'
|
|
31 |
#' This is an auxiliary function for controlling arguments for `survfit` model, typically used internally to specify
|
|
32 |
#' details of `survfit` model for [s_surv_time()]. `conf_level` refers to survival time estimation.
|
|
33 |
#'
|
|
34 |
#' @inheritParams argument_convention
|
|
35 |
#' @param conf_type (`string`)\cr confidence interval type. Options are "plain" (default), "log", "log-log",
|
|
36 |
#' see more in [survival::survfit()]. Note option "none" is no longer supported.
|
|
37 |
#' @param quantiles (`numeric`)\cr of length two to specify the quantiles of survival time.
|
|
38 |
#'
|
|
39 |
#' @return A list of components with the same names as the arguments
|
|
40 |
#'
|
|
41 |
#' @export
|
|
42 |
control_surv_time <- function(conf_level = 0.95, |
|
43 |
conf_type = c("plain", "log", "log-log"), |
|
44 |
quantiles = c(0.25, 0.75)) { |
|
45 | 154x |
conf_type <- match.arg(conf_type) |
46 | 153x |
checkmate::assert_numeric(quantiles, lower = 0, upper = 1, len = 2, unique = TRUE, sorted = TRUE) |
47 | 152x |
nullo <- lapply(quantiles, assert_proportion_value) |
48 | 152x |
assert_proportion_value(conf_level) |
49 | 151x |
list(conf_level = conf_level, conf_type = conf_type, quantiles = quantiles) |
50 |
}
|
|
51 | ||
52 |
#' Control Function for `survfit` Model for Patient's Survival Rate at time point
|
|
53 |
#'
|
|
54 |
#' @description `r lifecycle::badge("stable")`
|
|
55 |
#'
|
|
56 |
#' This is an auxiliary function for controlling arguments for `survfit` model, typically used internally to specify
|
|
57 |
#' details of `survfit` model for [s_surv_timepoint()]. `conf_level` refers to patient risk estimation at a time point.
|
|
58 |
#'
|
|
59 |
#' @inheritParams argument_convention
|
|
60 |
#' @inheritParams control_surv_time
|
|
61 |
#'
|
|
62 |
#' @return A list of components with the same names as the arguments
|
|
63 |
#'
|
|
64 |
#' @export
|
|
65 |
control_surv_timepoint <- function(conf_level = 0.95, |
|
66 |
conf_type = c("plain", "log", "log-log")) { |
|
67 | 28x |
conf_type <- match.arg(conf_type) |
68 | 27x |
assert_proportion_value(conf_level) |
69 | 26x |
list( |
70 | 26x |
conf_level = conf_level, |
71 | 26x |
conf_type = conf_type |
72 |
)
|
|
73 |
}
|
1 |
#' Helper Functions for Tabulating Survival Duration by Subgroup
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Helper functions that tabulate in a data frame statistics such as median survival
|
|
6 |
#' time and hazard ratio for population subgroups.
|
|
7 |
#'
|
|
8 |
#' @inheritParams argument_convention
|
|
9 |
#' @inheritParams survival_coxph_pairwise
|
|
10 |
#' @inheritParams survival_duration_subgroups
|
|
11 |
#' @param arm (`factor`)\cr the treatment group variable.
|
|
12 |
#'
|
|
13 |
#' @details Main functionality is to prepare data for use in a layout-creating function.
|
|
14 |
#'
|
|
15 |
#' @examples
|
|
16 |
#' library(dplyr)
|
|
17 |
#' library(forcats)
|
|
18 |
#'
|
|
19 |
#' adtte <- tern_ex_adtte
|
|
20 |
#'
|
|
21 |
#' # Save variable labels before data processing steps.
|
|
22 |
#' adtte_labels <- formatters::var_labels(adtte)
|
|
23 |
#'
|
|
24 |
#' adtte_f <- adtte %>%
|
|
25 |
#' filter(
|
|
26 |
#' PARAMCD == "OS",
|
|
27 |
#' ARM %in% c("B: Placebo", "A: Drug X"),
|
|
28 |
#' SEX %in% c("M", "F")
|
|
29 |
#' ) %>%
|
|
30 |
#' mutate(
|
|
31 |
#' # Reorder levels of ARM to display reference arm before treatment arm.
|
|
32 |
#' ARM = droplevels(fct_relevel(ARM, "B: Placebo")),
|
|
33 |
#' SEX = droplevels(SEX),
|
|
34 |
#' is_event = CNSR == 0
|
|
35 |
#' )
|
|
36 |
#' labels <- c("ARM" = adtte_labels[["ARM"]], "SEX" = adtte_labels[["SEX"]], "is_event" = "Event Flag")
|
|
37 |
#' formatters::var_labels(adtte_f)[names(labels)] <- labels
|
|
38 |
#'
|
|
39 |
#' @name h_survival_duration_subgroups
|
|
40 |
NULL
|
|
41 | ||
42 |
#' @describeIn h_survival_duration_subgroups helper to prepare a data frame of median survival times by arm.
|
|
43 |
#'
|
|
44 |
#' @return
|
|
45 |
#' * `h_survtime_df()` returns a `data.frame` with columns `arm`, `n`, `n_events`, and `median`.
|
|
46 |
#'
|
|
47 |
#' @examples
|
|
48 |
#' # Extract median survival time for one group.
|
|
49 |
#' h_survtime_df(
|
|
50 |
#' tte = adtte_f$AVAL,
|
|
51 |
#' is_event = adtte_f$is_event,
|
|
52 |
#' arm = adtte_f$ARM
|
|
53 |
#' )
|
|
54 |
#'
|
|
55 |
#' @export
|
|
56 |
h_survtime_df <- function(tte, is_event, arm) { |
|
57 | 55x |
checkmate::assert_numeric(tte) |
58 | 54x |
checkmate::assert_logical(is_event, len = length(tte)) |
59 | 54x |
assert_valid_factor(arm, len = length(tte)) |
60 | ||
61 | 54x |
df_tte <- data.frame( |
62 | 54x |
tte = tte, |
63 | 54x |
is_event = is_event, |
64 | 54x |
stringsAsFactors = FALSE |
65 |
)
|
|
66 | ||
67 |
# Delete NAs
|
|
68 | 54x |
non_missing_rows <- stats::complete.cases(df_tte) |
69 | 54x |
df_tte <- df_tte[non_missing_rows, ] |
70 | 54x |
arm <- arm[non_missing_rows] |
71 | ||
72 | 54x |
lst_tte <- split(df_tte, arm) |
73 | 54x |
lst_results <- Map(function(x, arm) { |
74 | 108x |
if (nrow(x) > 0) { |
75 | 104x |
s_surv <- s_surv_time(x, .var = "tte", is_event = "is_event") |
76 | 104x |
median_est <- unname(as.numeric(s_surv$median)) |
77 | 104x |
n_events <- sum(x$is_event) |
78 |
} else { |
|
79 | 4x |
median_est <- NA |
80 | 4x |
n_events <- NA |
81 |
}
|
|
82 | ||
83 | 108x |
data.frame( |
84 | 108x |
arm = arm, |
85 | 108x |
n = nrow(x), |
86 | 108x |
n_events = n_events, |
87 | 108x |
median = median_est, |
88 | 108x |
stringsAsFactors = FALSE |
89 |
)
|
|
90 | 54x |
}, lst_tte, names(lst_tte)) |
91 | ||
92 | 54x |
df <- do.call(rbind, args = c(lst_results, make.row.names = FALSE)) |
93 | 54x |
df$arm <- factor(df$arm, levels = levels(arm)) |
94 | 54x |
df
|
95 |
}
|
|
96 | ||
97 |
#' @describeIn h_survival_duration_subgroups summarizes median survival times by arm and across subgroups
|
|
98 |
#' in a data frame. `variables` corresponds to the names of variables found in `data`, passed as a named list and
|
|
99 |
#' requires elements `tte`, `is_event`, `arm` and optionally `subgroups`. `groups_lists` optionally specifies
|
|
100 |
#' groupings for `subgroups` variables.
|
|
101 |
#'
|
|
102 |
#' @return
|
|
103 |
#' * `h_survtime_subgroups_df()` returns a `data.frame` with columns `arm`, `n`, `n_events`, `median`, `subgroup`,
|
|
104 |
#' `var`, `var_label`, and `row_type`.
|
|
105 |
#'
|
|
106 |
#' @examples
|
|
107 |
#' # Extract median survival time for multiple groups.
|
|
108 |
#' h_survtime_subgroups_df(
|
|
109 |
#' variables = list(
|
|
110 |
#' tte = "AVAL",
|
|
111 |
#' is_event = "is_event",
|
|
112 |
#' arm = "ARM",
|
|
113 |
#' subgroups = c("SEX", "BMRKR2")
|
|
114 |
#' ),
|
|
115 |
#' data = adtte_f
|
|
116 |
#' )
|
|
117 |
#'
|
|
118 |
#' # Define groupings for BMRKR2 levels.
|
|
119 |
#' h_survtime_subgroups_df(
|
|
120 |
#' variables = list(
|
|
121 |
#' tte = "AVAL",
|
|
122 |
#' is_event = "is_event",
|
|
123 |
#' arm = "ARM",
|
|
124 |
#' subgroups = c("SEX", "BMRKR2")
|
|
125 |
#' ),
|
|
126 |
#' data = adtte_f,
|
|
127 |
#' groups_lists = list(
|
|
128 |
#' BMRKR2 = list(
|
|
129 |
#' "low" = "LOW",
|
|
130 |
#' "low/medium" = c("LOW", "MEDIUM"),
|
|
131 |
#' "low/medium/high" = c("LOW", "MEDIUM", "HIGH")
|
|
132 |
#' )
|
|
133 |
#' )
|
|
134 |
#' )
|
|
135 |
#'
|
|
136 |
#' @export
|
|
137 |
h_survtime_subgroups_df <- function(variables, |
|
138 |
data,
|
|
139 |
groups_lists = list(), |
|
140 |
label_all = "All Patients") { |
|
141 | 11x |
checkmate::assert_character(variables$tte) |
142 | 11x |
checkmate::assert_character(variables$is_event) |
143 | 11x |
checkmate::assert_character(variables$arm) |
144 | 11x |
checkmate::assert_character(variables$subgroups, null.ok = TRUE) |
145 | ||
146 | 11x |
assert_df_with_variables(data, variables) |
147 | ||
148 | 11x |
checkmate::assert_string(label_all) |
149 | ||
150 |
# Add All Patients.
|
|
151 | 11x |
result_all <- h_survtime_df(data[[variables$tte]], data[[variables$is_event]], data[[variables$arm]]) |
152 | 11x |
result_all$subgroup <- label_all |
153 | 11x |
result_all$var <- "ALL" |
154 | 11x |
result_all$var_label <- label_all |
155 | 11x |
result_all$row_type <- "content" |
156 | ||
157 |
# Add Subgroups.
|
|
158 | 11x |
if (is.null(variables$subgroups)) { |
159 | 3x |
result_all
|
160 |
} else { |
|
161 | 8x |
l_data <- h_split_by_subgroups(data, variables$subgroups, groups_lists = groups_lists) |
162 | 8x |
l_result <- lapply(l_data, function(grp) { |
163 | 40x |
result <- h_survtime_df(grp$df[[variables$tte]], grp$df[[variables$is_event]], grp$df[[variables$arm]]) |
164 | 40x |
result_labels <- grp$df_labels[rep(1, times = nrow(result)), ] |
165 | 40x |
cbind(result, result_labels) |
166 |
}) |
|
167 | 8x |
result_subgroups <- do.call(rbind, args = c(l_result, make.row.names = FALSE)) |
168 | 8x |
result_subgroups$row_type <- "analysis" |
169 | 8x |
rbind( |
170 | 8x |
result_all,
|
171 | 8x |
result_subgroups
|
172 |
)
|
|
173 |
}
|
|
174 |
}
|
|
175 | ||
176 |
#' @describeIn h_survival_duration_subgroups helper to prepare a data frame with estimates of
|
|
177 |
#' treatment hazard ratio.
|
|
178 |
#'
|
|
179 |
#' @param strata_data (`factor`, `data.frame` or `NULL`)\cr required if stratified analysis is performed.
|
|
180 |
#'
|
|
181 |
#' @return
|
|
182 |
#' * `h_coxph_df()` returns a `data.frame` with columns `arm`, `n_tot`, `n_tot_events`, `hr`, `lcl`, `ucl`,
|
|
183 |
#' `conf_level`, `pval` and `pval_label`.
|
|
184 |
#'
|
|
185 |
#' @examples
|
|
186 |
#' # Extract hazard ratio for one group.
|
|
187 |
#' h_coxph_df(adtte_f$AVAL, adtte_f$is_event, adtte_f$ARM)
|
|
188 |
#'
|
|
189 |
#' # Extract hazard ratio for one group with stratification factor.
|
|
190 |
#' h_coxph_df(adtte_f$AVAL, adtte_f$is_event, adtte_f$ARM, strata_data = adtte_f$STRATA1)
|
|
191 |
#'
|
|
192 |
#' @export
|
|
193 |
h_coxph_df <- function(tte, is_event, arm, strata_data = NULL, control = control_coxph()) { |
|
194 | 58x |
checkmate::assert_numeric(tte) |
195 | 58x |
checkmate::assert_logical(is_event, len = length(tte)) |
196 | 58x |
assert_valid_factor(arm, n.levels = 2, len = length(tte)) |
197 | ||
198 | 58x |
df_tte <- data.frame(tte = tte, is_event = is_event) |
199 | 58x |
strata_vars <- NULL |
200 | ||
201 | 58x |
if (!is.null(strata_data)) { |
202 | 5x |
if (is.data.frame(strata_data)) { |
203 | 4x |
strata_vars <- names(strata_data) |
204 | 4x |
checkmate::assert_data_frame(strata_data, nrows = nrow(df_tte)) |
205 | 4x |
assert_df_with_factors(strata_data, as.list(stats::setNames(strata_vars, strata_vars))) |
206 |
} else { |
|
207 | 1x |
assert_valid_factor(strata_data, len = nrow(df_tte)) |
208 | 1x |
strata_vars <- "strata_data" |
209 |
}
|
|
210 | 5x |
df_tte[strata_vars] <- strata_data |
211 |
}
|
|
212 | ||
213 | 58x |
l_df <- split(df_tte, arm) |
214 | ||
215 | 58x |
if (nrow(l_df[[1]]) > 0 && nrow(l_df[[2]]) > 0) { |
216 |
# Hazard ratio and CI.
|
|
217 | 54x |
result <- s_coxph_pairwise( |
218 | 54x |
df = l_df[[2]], |
219 | 54x |
.ref_group = l_df[[1]], |
220 | 54x |
.in_ref_col = FALSE, |
221 | 54x |
.var = "tte", |
222 | 54x |
is_event = "is_event", |
223 | 54x |
strat = strata_vars, |
224 | 54x |
control = control |
225 |
)
|
|
226 | ||
227 | 54x |
df <- data.frame( |
228 |
# Dummy column needed downstream to create a nested header.
|
|
229 | 54x |
arm = " ", |
230 | 54x |
n_tot = unname(as.numeric(result$n_tot)), |
231 | 54x |
n_tot_events = unname(as.numeric(result$n_tot_events)), |
232 | 54x |
hr = unname(as.numeric(result$hr)), |
233 | 54x |
lcl = unname(result$hr_ci[1]), |
234 | 54x |
ucl = unname(result$hr_ci[2]), |
235 | 54x |
conf_level = control[["conf_level"]], |
236 | 54x |
pval = as.numeric(result$pvalue), |
237 | 54x |
pval_label = obj_label(result$pvalue), |
238 | 54x |
stringsAsFactors = FALSE |
239 |
)
|
|
240 |
} else if ( |
|
241 | 4x |
(nrow(l_df[[1]]) == 0 && nrow(l_df[[2]]) > 0) || |
242 | 4x |
(nrow(l_df[[1]]) > 0 && nrow(l_df[[2]]) == 0) |
243 |
) { |
|
244 | 4x |
df_tte_complete <- df_tte[stats::complete.cases(df_tte), ] |
245 | 4x |
df <- data.frame( |
246 |
# Dummy column needed downstream to create a nested header.
|
|
247 | 4x |
arm = " ", |
248 | 4x |
n_tot = nrow(df_tte_complete), |
249 | 4x |
n_tot_events = sum(df_tte_complete$is_event), |
250 | 4x |
hr = NA, |
251 | 4x |
lcl = NA, |
252 | 4x |
ucl = NA, |
253 | 4x |
conf_level = control[["conf_level"]], |
254 | 4x |
pval = NA, |
255 | 4x |
pval_label = NA, |
256 | 4x |
stringsAsFactors = FALSE |
257 |
)
|
|
258 |
} else { |
|
259 | ! |
df <- data.frame( |
260 |
# Dummy column needed downstream to create a nested header.
|
|
261 | ! |
arm = " ", |
262 | ! |
n_tot = 0L, |
263 | ! |
n_tot_events = 0L, |
264 | ! |
hr = NA, |
265 | ! |
lcl = NA, |
266 | ! |
ucl = NA, |
267 | ! |
conf_level = control[["conf_level"]], |
268 | ! |
pval = NA, |
269 | ! |
pval_label = NA, |
270 | ! |
stringsAsFactors = FALSE |
271 |
)
|
|
272 |
}
|
|
273 | ||
274 | 58x |
df
|
275 |
}
|
|
276 | ||
277 |
#' @describeIn h_survival_duration_subgroups summarizes estimates of the treatment hazard ratio
|
|
278 |
#' across subgroups in a data frame. `variables` corresponds to the names of variables found in
|
|
279 |
#' `data`, passed as a named list and requires elements `tte`, `is_event`, `arm` and
|
|
280 |
#' optionally `subgroups` and `strat`. `groups_lists` optionally specifies
|
|
281 |
#' groupings for `subgroups` variables.
|
|
282 |
#'
|
|
283 |
#' @return
|
|
284 |
#' * `h_coxph_subgroups_df()` returns a `data.frame` with columns `arm`, `n_tot`, `n_tot_events`, `hr`,
|
|
285 |
#' `lcl`, `ucl`, `conf_level`, `pval`, `pval_label`, `subgroup`, `var`, `var_label`, and `row_type`.
|
|
286 |
#'
|
|
287 |
#' @examples
|
|
288 |
#' # Extract hazard ratio for multiple groups.
|
|
289 |
#' h_coxph_subgroups_df(
|
|
290 |
#' variables = list(
|
|
291 |
#' tte = "AVAL",
|
|
292 |
#' is_event = "is_event",
|
|
293 |
#' arm = "ARM",
|
|
294 |
#' subgroups = c("SEX", "BMRKR2")
|
|
295 |
#' ),
|
|
296 |
#' data = adtte_f
|
|
297 |
#' )
|
|
298 |
#'
|
|
299 |
#' # Define groupings of BMRKR2 levels.
|
|
300 |
#' h_coxph_subgroups_df(
|
|
301 |
#' variables = list(
|
|
302 |
#' tte = "AVAL",
|
|
303 |
#' is_event = "is_event",
|
|
304 |
#' arm = "ARM",
|
|
305 |
#' subgroups = c("SEX", "BMRKR2")
|
|
306 |
#' ),
|
|
307 |
#' data = adtte_f,
|
|
308 |
#' groups_lists = list(
|
|
309 |
#' BMRKR2 = list(
|
|
310 |
#' "low" = "LOW",
|
|
311 |
#' "low/medium" = c("LOW", "MEDIUM"),
|
|
312 |
#' "low/medium/high" = c("LOW", "MEDIUM", "HIGH")
|
|
313 |
#' )
|
|
314 |
#' )
|
|
315 |
#' )
|
|
316 |
#'
|
|
317 |
#' # Extract hazard ratio for multiple groups with stratification factors.
|
|
318 |
#' h_coxph_subgroups_df(
|
|
319 |
#' variables = list(
|
|
320 |
#' tte = "AVAL",
|
|
321 |
#' is_event = "is_event",
|
|
322 |
#' arm = "ARM",
|
|
323 |
#' subgroups = c("SEX", "BMRKR2"),
|
|
324 |
#' strat = c("STRATA1", "STRATA2")
|
|
325 |
#' ),
|
|
326 |
#' data = adtte_f
|
|
327 |
#' )
|
|
328 |
#'
|
|
329 |
#' @export
|
|
330 |
h_coxph_subgroups_df <- function(variables, |
|
331 |
data,
|
|
332 |
groups_lists = list(), |
|
333 |
control = control_coxph(), |
|
334 |
label_all = "All Patients") { |
|
335 | 12x |
checkmate::assert_character(variables$tte) |
336 | 12x |
checkmate::assert_character(variables$is_event) |
337 | 12x |
checkmate::assert_character(variables$arm) |
338 | 12x |
checkmate::assert_character(variables$subgroups, null.ok = TRUE) |
339 | 12x |
checkmate::assert_character(variables$strat, null.ok = TRUE) |
340 | 12x |
assert_df_with_factors(data, list(val = variables$arm), min.levels = 2, max.levels = 2) |
341 | 12x |
assert_df_with_variables(data, variables) |
342 | 12x |
checkmate::assert_string(label_all) |
343 | ||
344 |
# Add All Patients.
|
|
345 | 12x |
result_all <- h_coxph_df( |
346 | 12x |
tte = data[[variables$tte]], |
347 | 12x |
is_event = data[[variables$is_event]], |
348 | 12x |
arm = data[[variables$arm]], |
349 | 12x |
strata_data = if (is.null(variables$strat)) NULL else data[variables$strat], |
350 | 12x |
control = control |
351 |
)
|
|
352 | 12x |
result_all$subgroup <- label_all |
353 | 12x |
result_all$var <- "ALL" |
354 | 12x |
result_all$var_label <- label_all |
355 | 12x |
result_all$row_type <- "content" |
356 | ||
357 |
# Add Subgroups.
|
|
358 | 12x |
if (is.null(variables$subgroups)) { |
359 | 3x |
result_all
|
360 |
} else { |
|
361 | 9x |
l_data <- h_split_by_subgroups(data, variables$subgroups, groups_lists = groups_lists) |
362 | ||
363 | 9x |
l_result <- lapply(l_data, function(grp) { |
364 | 42x |
result <- h_coxph_df( |
365 | 42x |
tte = grp$df[[variables$tte]], |
366 | 42x |
is_event = grp$df[[variables$is_event]], |
367 | 42x |
arm = grp$df[[variables$arm]], |
368 | 42x |
strata_data = if (is.null(variables$strat)) NULL else grp$df[variables$strat], |
369 | 42x |
control = control |
370 |
)
|
|
371 | 42x |
result_labels <- grp$df_labels[rep(1, times = nrow(result)), ] |
372 | 42x |
cbind(result, result_labels) |
373 |
}) |
|
374 | ||
375 | 9x |
result_subgroups <- do.call(rbind, args = c(l_result, make.row.names = FALSE)) |
376 | 9x |
result_subgroups$row_type <- "analysis" |
377 | ||
378 | 9x |
rbind( |
379 | 9x |
result_all,
|
380 | 9x |
result_subgroups
|
381 |
)
|
|
382 |
}
|
|
383 |
}
|
|
384 | ||
385 |
#' Split Dataframe by Subgroups
|
|
386 |
#'
|
|
387 |
#' @description `r lifecycle::badge("stable")`
|
|
388 |
#'
|
|
389 |
#' Split a dataframe into a non-nested list of subsets.
|
|
390 |
#'
|
|
391 |
#' @inheritParams survival_duration_subgroups
|
|
392 |
#' @param data (`data.frame`)\cr dataset to split.
|
|
393 |
#' @param subgroups (`character`)\cr names of factor variables from `data` used to create subsets.
|
|
394 |
#' Unused levels not present in `data` are dropped. Note that the order in this vector
|
|
395 |
#' determines the order in the downstream table.
|
|
396 |
#'
|
|
397 |
#' @return A list with subset data (`df`) and metadata about the subset (`df_labels`).
|
|
398 |
#'
|
|
399 |
#' @details Main functionality is to prepare data for use in forest plot layouts.
|
|
400 |
#'
|
|
401 |
#' @examples
|
|
402 |
#' df <- data.frame(
|
|
403 |
#' x = c(1:5),
|
|
404 |
#' y = factor(c("A", "B", "A", "B", "A"), levels = c("A", "B", "C")),
|
|
405 |
#' z = factor(c("C", "C", "D", "D", "D"), levels = c("D", "C"))
|
|
406 |
#' )
|
|
407 |
#' formatters::var_labels(df) <- paste("label for", names(df))
|
|
408 |
#'
|
|
409 |
#' h_split_by_subgroups(
|
|
410 |
#' data = df,
|
|
411 |
#' subgroups = c("y", "z")
|
|
412 |
#' )
|
|
413 |
#'
|
|
414 |
#' h_split_by_subgroups(
|
|
415 |
#' data = df,
|
|
416 |
#' subgroups = c("y", "z"),
|
|
417 |
#' groups_lists = list(
|
|
418 |
#' y = list("AB" = c("A", "B"), "C" = "C")
|
|
419 |
#' )
|
|
420 |
#' )
|
|
421 |
#'
|
|
422 |
#' @export
|
|
423 |
h_split_by_subgroups <- function(data, |
|
424 |
subgroups,
|
|
425 |
groups_lists = list()) { |
|
426 | 46x |
checkmate::assert_character(subgroups, min.len = 1, any.missing = FALSE) |
427 | 46x |
checkmate::assert_list(groups_lists, names = "named") |
428 | 46x |
checkmate::assert_subset(names(groups_lists), subgroups) |
429 | 46x |
assert_df_with_factors(data, as.list(stats::setNames(subgroups, subgroups))) |
430 | ||
431 | 46x |
data_labels <- unname(formatters::var_labels(data)) |
432 | 46x |
df_subgroups <- data[, subgroups, drop = FALSE] |
433 | 46x |
subgroup_labels <- formatters::var_labels(df_subgroups, fill = TRUE) |
434 | ||
435 | 46x |
l_labels <- Map(function(grp_i, name_i) { |
436 | 81x |
existing_levels <- levels(droplevels(grp_i)) |
437 | 81x |
grp_levels <- if (name_i %in% names(groups_lists)) { |
438 |
# For this variable groupings are defined. We check which groups are contained in the data.
|
|
439 | 11x |
group_list_i <- groups_lists[[name_i]] |
440 | 11x |
group_has_levels <- vapply(group_list_i, function(lvls) any(lvls %in% existing_levels), TRUE) |
441 | 11x |
names(which(group_has_levels)) |
442 |
} else { |
|
443 | 70x |
existing_levels
|
444 |
}
|
|
445 | 81x |
df_labels <- data.frame( |
446 | 81x |
subgroup = grp_levels, |
447 | 81x |
var = name_i, |
448 | 81x |
var_label = unname(subgroup_labels[name_i]), |
449 | 81x |
stringsAsFactors = FALSE # Rationale is that subgroups may not be unique. |
450 |
)
|
|
451 | 46x |
}, df_subgroups, names(df_subgroups)) |
452 | ||
453 |
# Create a dataframe with one row per subgroup.
|
|
454 | 46x |
df_labels <- do.call(rbind, args = c(l_labels, make.row.names = FALSE)) |
455 | 46x |
row_label <- paste0(df_labels$var, ".", df_labels$subgroup) |
456 | 46x |
row_split_var <- factor(row_label, levels = row_label) |
457 | ||
458 |
# Create a list of data subsets.
|
|
459 | 46x |
lapply(split(df_labels, row_split_var), function(row_i) { |
460 | 205x |
which_row <- if (row_i$var %in% names(groups_lists)) { |
461 | 31x |
data[[row_i$var]] %in% groups_lists[[row_i$var]][[row_i$subgroup]] |
462 |
} else { |
|
463 | 174x |
data[[row_i$var]] == row_i$subgroup |
464 |
}
|
|
465 | 205x |
df <- data[which_row, ] |
466 | 205x |
rownames(df) <- NULL |
467 | 205x |
formatters::var_labels(df) <- data_labels |
468 | ||
469 | 205x |
list( |
470 | 205x |
df = df, |
471 | 205x |
df_labels = data.frame(row_i, row.names = NULL) |
472 |
)
|
|
473 |
}) |
|
474 |
}
|
1 |
#' Occurrence Table Pruning
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Family of constructor and condition functions to flexibly prune occurrence tables.
|
|
6 |
#' The condition functions always return whether the row result is higher than the threshold.
|
|
7 |
#' Since they are of class [CombinationFunction()] they can be logically combined with other condition
|
|
8 |
#' functions.
|
|
9 |
#'
|
|
10 |
#' @note Since most table specifications are worded positively, we name our constructor and condition
|
|
11 |
#' functions positively, too. However, note that the result of [keep_rows()] says what
|
|
12 |
#' should be pruned, to conform with the [rtables::prune_table()] interface.
|
|
13 |
#'
|
|
14 |
#' @examples
|
|
15 |
#' \donttest{
|
|
16 |
#' tab <- basic_table() %>%
|
|
17 |
#' split_cols_by("ARM") %>%
|
|
18 |
#' split_rows_by("RACE") %>%
|
|
19 |
#' split_rows_by("STRATA1") %>%
|
|
20 |
#' summarize_row_groups() %>%
|
|
21 |
#' summarize_vars("COUNTRY", .stats = "count_fraction") %>%
|
|
22 |
#' build_table(DM)
|
|
23 |
#' }
|
|
24 |
#'
|
|
25 |
#' @name prune_occurrences
|
|
26 |
NULL
|
|
27 | ||
28 |
#' @describeIn prune_occurrences Constructor for creating pruning functions based on
|
|
29 |
#' a row condition function. This removes all analysis rows (`TableRow`) that should be
|
|
30 |
#' pruned, i.e., don't fulfill the row condition. It removes the sub-tree if there are no
|
|
31 |
#' children left.
|
|
32 |
#'
|
|
33 |
#' @param row_condition (`CombinationFunction`)\cr condition function which works on individual
|
|
34 |
#' analysis rows and flags whether these should be kept in the pruned table.
|
|
35 |
#'
|
|
36 |
#' @return
|
|
37 |
#' * `keep_rows()` returns a pruning function that can be used with [rtables::prune_table()]
|
|
38 |
#' to prune an `rtables` table.
|
|
39 |
#'
|
|
40 |
#' @examples
|
|
41 |
#' \donttest{
|
|
42 |
#' # `keep_rows`
|
|
43 |
#' is_non_empty <- !CombinationFunction(all_zero_or_na)
|
|
44 |
#' prune_table(tab, keep_rows(is_non_empty))
|
|
45 |
#' }
|
|
46 |
#'
|
|
47 |
#' @export
|
|
48 |
keep_rows <- function(row_condition) { |
|
49 | 6x |
checkmate::assert_function(row_condition) |
50 | 6x |
function(table_tree) { |
51 | 2256x |
if (inherits(table_tree, "TableRow")) { |
52 | 1872x |
return(!row_condition(table_tree)) |
53 |
}
|
|
54 | 384x |
children <- tree_children(table_tree) |
55 | 384x |
identical(length(children), 0L) |
56 |
}
|
|
57 |
}
|
|
58 | ||
59 |
#' @describeIn prune_occurrences Constructor for creating pruning functions based on
|
|
60 |
#' a condition for the (first) content row in leaf tables. This removes all leaf tables where
|
|
61 |
#' the first content row does not fulfill the condition. It does not check individual rows.
|
|
62 |
#' It then proceeds recursively by removing the sub tree if there are no children left.
|
|
63 |
#'
|
|
64 |
#' @param content_row_condition (`CombinationFunction`)\cr condition function which works on individual
|
|
65 |
#' first content rows of leaf tables and flags whether these leaf tables should be kept in the pruned table.
|
|
66 |
#'
|
|
67 |
#' @return
|
|
68 |
#' * `keep_content_rows()` returns a pruning function that checks the condition on the first content
|
|
69 |
#' row of leaf tables in the table.
|
|
70 |
#'
|
|
71 |
#' @examples
|
|
72 |
#' # `keep_content_rows`
|
|
73 |
#' \donttest{
|
|
74 |
#' more_than_twenty <- has_count_in_cols(atleast = 20L, col_names = names(tab))
|
|
75 |
#' prune_table(tab, keep_content_rows(more_than_twenty))
|
|
76 |
#' }
|
|
77 |
#'
|
|
78 |
#' @export
|
|
79 |
keep_content_rows <- function(content_row_condition) { |
|
80 | 1x |
checkmate::assert_function(content_row_condition) |
81 | 1x |
function(table_tree) { |
82 | 166x |
if (is_leaf_table(table_tree)) { |
83 | 24x |
content_row <- h_content_first_row(table_tree) |
84 | 24x |
return(!content_row_condition(content_row)) |
85 |
}
|
|
86 | 142x |
if (inherits(table_tree, "DataRow")) { |
87 | 120x |
return(FALSE) |
88 |
}
|
|
89 | 22x |
children <- tree_children(table_tree) |
90 | 22x |
identical(length(children), 0L) |
91 |
}
|
|
92 |
}
|
|
93 | ||
94 |
#' @describeIn prune_occurrences Constructor for creating condition functions on total counts in the specified columns.
|
|
95 |
#'
|
|
96 |
#' @param atleast (`count` or `proportion`)\cr threshold which should be met in order to keep the row.
|
|
97 |
#' @param ... arguments for row or column access, see [`rtables_access`]: either `col_names` (`character`) including
|
|
98 |
#' the names of the columns which should be used, or alternatively `col_indices` (`integer`) giving the indices
|
|
99 |
#' directly instead.
|
|
100 |
#'
|
|
101 |
#' @return
|
|
102 |
#' * `has_count_in_cols()` returns a condition function that sums the counts in the specified column.
|
|
103 |
#'
|
|
104 |
#' @examples
|
|
105 |
#' \donttest{
|
|
106 |
#' more_than_one <- has_count_in_cols(atleast = 1L, col_names = names(tab))
|
|
107 |
#' prune_table(tab, keep_rows(more_than_one))
|
|
108 |
#' }
|
|
109 |
#'
|
|
110 |
#' @export
|
|
111 |
has_count_in_cols <- function(atleast, ...) { |
|
112 | 3x |
checkmate::assert_count(atleast) |
113 | 3x |
CombinationFunction(function(table_row) { |
114 | 334x |
row_counts <- h_row_counts(table_row, ...) |
115 | 334x |
total_count <- sum(row_counts) |
116 | 334x |
total_count >= atleast |
117 |
}) |
|
118 |
}
|
|
119 | ||
120 |
#' @describeIn prune_occurrences Constructor for creating condition functions on any of the counts in
|
|
121 |
#' the specified columns satisfying a threshold.
|
|
122 |
#'
|
|
123 |
#' @param atleast (`count` or `proportion`)\cr threshold which should be met in order to keep the row.
|
|
124 |
#'
|
|
125 |
#' @return
|
|
126 |
#' * `has_count_in_any_col()` returns a condition function that compares the counts in the
|
|
127 |
#' specified columns with the threshold.
|
|
128 |
#'
|
|
129 |
#' @examples
|
|
130 |
#' \donttest{
|
|
131 |
#' # `has_count_in_any_col`
|
|
132 |
#' any_more_than_one <- has_count_in_any_col(atleast = 1L, col_names = names(tab))
|
|
133 |
#' prune_table(tab, keep_rows(any_more_than_one))
|
|
134 |
#' }
|
|
135 |
#'
|
|
136 |
#' @export
|
|
137 |
has_count_in_any_col <- function(atleast, ...) { |
|
138 | ! |
checkmate::assert_count(atleast) |
139 | ! |
CombinationFunction(function(table_row) { |
140 | ! |
row_counts <- h_row_counts(table_row, ...) |
141 | ! |
any(row_counts >= atleast) |
142 |
}) |
|
143 |
}
|
|
144 | ||
145 |
#' @describeIn prune_occurrences Constructor for creating condition functions on total fraction in
|
|
146 |
#' the specified columns.
|
|
147 |
#'
|
|
148 |
#' @return
|
|
149 |
#' * `has_fraction_in_cols()` returns a condition function that sums the counts in the
|
|
150 |
#' specified column, and computes the fraction by dividing by the total column counts.
|
|
151 |
#'
|
|
152 |
#' @examples
|
|
153 |
#' \donttest{
|
|
154 |
#' # `has_fraction_in_cols`
|
|
155 |
#' more_than_five_percent <- has_fraction_in_cols(atleast = 0.05, col_names = names(tab))
|
|
156 |
#' prune_table(tab, keep_rows(more_than_five_percent))
|
|
157 |
#' }
|
|
158 |
#'
|
|
159 |
#' @export
|
|
160 |
has_fraction_in_cols <- function(atleast, ...) { |
|
161 | 1x |
assert_proportion_value(atleast, include_boundaries = TRUE) |
162 | 1x |
CombinationFunction(function(table_row) { |
163 | 303x |
row_counts <- h_row_counts(table_row, ...) |
164 | 303x |
total_count <- sum(row_counts) |
165 | 303x |
col_counts <- h_col_counts(table_row, ...) |
166 | 303x |
total_n <- sum(col_counts) |
167 | 303x |
total_percent <- total_count / total_n |
168 | 303x |
total_percent >= atleast |
169 |
}) |
|
170 |
}
|
|
171 | ||
172 |
#' @describeIn prune_occurrences Constructor for creating condition functions on any fraction in
|
|
173 |
#' the specified columns.
|
|
174 |
#'
|
|
175 |
#' @return
|
|
176 |
#' * `has_fraction_in_any_col()` returns a condition function that looks at the fractions
|
|
177 |
#' in the specified columns and checks whether any of them fulfill the threshold.
|
|
178 |
#'
|
|
179 |
#' @examples
|
|
180 |
#' \donttest{
|
|
181 |
#' # `has_fraction_in_any_col`
|
|
182 |
#' any_atleast_five_percent <- has_fraction_in_any_col(atleast = 0.05, col_names = names(tab))
|
|
183 |
#' prune_table(tab, keep_rows(more_than_five_percent))
|
|
184 |
#' }
|
|
185 |
#'
|
|
186 |
#' @export
|
|
187 |
has_fraction_in_any_col <- function(atleast, ...) { |
|
188 | ! |
assert_proportion_value(atleast, include_boundaries = TRUE) |
189 | ! |
CombinationFunction(function(table_row) { |
190 | ! |
row_fractions <- h_row_fractions(table_row, ...) |
191 | ! |
any(row_fractions >= atleast) |
192 |
}) |
|
193 |
}
|
|
194 | ||
195 |
#' @describeIn prune_occurrences Constructor for creating condition function that checks the difference
|
|
196 |
#' between the fractions reported in each specified column.
|
|
197 |
#'
|
|
198 |
#' @return
|
|
199 |
#' * `has_fractions_difference()` returns a condition function that extracts the fractions of each
|
|
200 |
#' specified column, and computes the difference of the minimum and maximum.
|
|
201 |
#'
|
|
202 |
#' @examples
|
|
203 |
#' \donttest{
|
|
204 |
#' # `has_fractions_difference`
|
|
205 |
#' more_than_five_percent_diff <- has_fractions_difference(atleast = 0.05, col_names = names(tab))
|
|
206 |
#' prune_table(tab, keep_rows(more_than_five_percent_diff))
|
|
207 |
#' }
|
|
208 |
#'
|
|
209 |
#' @export
|
|
210 |
has_fractions_difference <- function(atleast, ...) { |
|
211 | 1x |
assert_proportion_value(atleast, include_boundaries = TRUE) |
212 | 1x |
CombinationFunction(function(table_row) { |
213 | 243x |
fractions <- h_row_fractions(table_row, ...) |
214 | 243x |
difference <- diff(range(fractions)) |
215 | 243x |
difference >= atleast |
216 |
}) |
|
217 |
}
|
|
218 | ||
219 |
#' @describeIn prune_occurrences Constructor for creating condition function that checks the difference
|
|
220 |
#' between the counts reported in each specified column.
|
|
221 |
#'
|
|
222 |
#' @return
|
|
223 |
#' * `has_counts_difference()` returns a condition function that extracts the counts of each
|
|
224 |
#' specified column, and computes the difference of the minimum and maximum.
|
|
225 |
#'
|
|
226 |
#' @examples
|
|
227 |
#' \donttest{
|
|
228 |
#' more_than_one_diff <- has_counts_difference(atleast = 1L, col_names = names(tab))
|
|
229 |
#' prune_table(tab, keep_rows(more_than_one_diff))
|
|
230 |
#' }
|
|
231 |
#'
|
|
232 |
#' @export
|
|
233 |
has_counts_difference <- function(atleast, ...) { |
|
234 | 1x |
checkmate::assert_count(atleast) |
235 | 1x |
CombinationFunction(function(table_row) { |
236 | 27x |
counts <- h_row_counts(table_row, ...) |
237 | 27x |
difference <- diff(range(counts)) |
238 | 27x |
difference >= atleast |
239 |
}) |
|
240 |
}
|
1 |
#' Helper Functions for Cox Proportional Hazards Regression
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Helper functions used in [fit_coxreg_univar()] and [fit_coxreg_multivar()].
|
|
6 |
#'
|
|
7 |
#' @inheritParams argument_convention
|
|
8 |
#' @inheritParams h_coxreg_univar_extract
|
|
9 |
#' @inheritParams cox_regression_inter
|
|
10 |
#' @inheritParams control_coxreg
|
|
11 |
#'
|
|
12 |
#' @seealso [cox_regression]
|
|
13 |
#'
|
|
14 |
#' @name h_cox_regression
|
|
15 |
NULL
|
|
16 | ||
17 |
#' @describeIn h_cox_regression Helper for Cox regression formula. Creates a list of formulas. It is used
|
|
18 |
#' internally by [fit_coxreg_univar()] for the comparison of univariate Cox regression models.
|
|
19 |
#'
|
|
20 |
#' @return
|
|
21 |
#' * `h_coxreg_univar_formulas()` returns a `character` vector coercible into formulas (e.g [stats::as.formula()]).
|
|
22 |
#'
|
|
23 |
#' @examples
|
|
24 |
#' # `h_coxreg_univar_formulas`
|
|
25 |
#'
|
|
26 |
#' ## Simple formulas.
|
|
27 |
#' h_coxreg_univar_formulas(
|
|
28 |
#' variables = list(
|
|
29 |
#' time = "time", event = "status", arm = "armcd", covariates = c("X", "y")
|
|
30 |
#' )
|
|
31 |
#' )
|
|
32 |
#'
|
|
33 |
#' ## Addition of an optional strata.
|
|
34 |
#' h_coxreg_univar_formulas(
|
|
35 |
#' variables = list(
|
|
36 |
#' time = "time", event = "status", arm = "armcd", covariates = c("X", "y"),
|
|
37 |
#' strata = "SITE"
|
|
38 |
#' )
|
|
39 |
#' )
|
|
40 |
#'
|
|
41 |
#' ## Inclusion of the interaction term.
|
|
42 |
#' h_coxreg_univar_formulas(
|
|
43 |
#' variables = list(
|
|
44 |
#' time = "time", event = "status", arm = "armcd", covariates = c("X", "y"),
|
|
45 |
#' strata = "SITE"
|
|
46 |
#' ),
|
|
47 |
#' interaction = TRUE
|
|
48 |
#' )
|
|
49 |
#'
|
|
50 |
#' ## Only covariates fitted in separate models.
|
|
51 |
#' h_coxreg_univar_formulas(
|
|
52 |
#' variables = list(
|
|
53 |
#' time = "time", event = "status", covariates = c("X", "y")
|
|
54 |
#' )
|
|
55 |
#' )
|
|
56 |
#'
|
|
57 |
#' @export
|
|
58 |
h_coxreg_univar_formulas <- function(variables, |
|
59 |
interaction = FALSE) { |
|
60 | 41x |
checkmate::assert_list(variables, names = "named") |
61 | 41x |
has_arm <- "arm" %in% names(variables) |
62 | 41x |
arm_name <- if (has_arm) "arm" else NULL |
63 | ||
64 | 41x |
checkmate::assert_character(variables$covariates, null.ok = TRUE) |
65 | ||
66 | 41x |
checkmate::assert_flag(interaction) |
67 | ||
68 | 41x |
if (!has_arm || is.null(variables$covariates)) { |
69 | 10x |
checkmate::assert_false(interaction) |
70 |
}
|
|
71 | ||
72 | 39x |
assert_list_of_variables(variables[c(arm_name, "event", "time")]) |
73 | ||
74 | 39x |
if (!is.null(variables$covariates)) { |
75 | 38x |
forms <- paste0( |
76 | 38x |
"survival::Surv(", variables$time, ", ", variables$event, ") ~ ", |
77 | 38x |
ifelse(has_arm, variables$arm, "1"), |
78 | 38x |
ifelse(interaction, " * ", " + "), |
79 | 38x |
variables$covariates, |
80 | 38x |
ifelse( |
81 | 38x |
!is.null(variables$strata), |
82 | 38x |
paste0(" + strata(", paste0(variables$strata, collapse = ", "), ")"), |
83 |
""
|
|
84 |
)
|
|
85 |
)
|
|
86 |
} else { |
|
87 | 1x |
forms <- NULL |
88 |
}
|
|
89 | 39x |
nams <- variables$covariates |
90 | 39x |
if (has_arm) { |
91 | 32x |
ref <- paste0( |
92 | 32x |
"survival::Surv(", variables$time, ", ", variables$event, ") ~ ", |
93 | 32x |
variables$arm, |
94 | 32x |
ifelse( |
95 | 32x |
!is.null(variables$strata), |
96 | 32x |
paste0( |
97 | 32x |
" + strata(", paste0(variables$strata, collapse = ", "), ")" |
98 |
),
|
|
99 |
""
|
|
100 |
)
|
|
101 |
)
|
|
102 | 32x |
forms <- c(ref, forms) |
103 | 32x |
nams <- c("ref", nams) |
104 |
}
|
|
105 | 39x |
stats::setNames(forms, nams) |
106 |
}
|
|
107 | ||
108 |
#' @describeIn h_cox_regression Helper for multivariate Cox regression formula. Creates a formulas
|
|
109 |
#' string. It is used internally by [fit_coxreg_multivar()] for the comparison of multivariate Cox
|
|
110 |
#' regression models. Interactions will not be included in multivariate Cox regression model.
|
|
111 |
#'
|
|
112 |
#' @return
|
|
113 |
#' * `h_coxreg_multivar_formula()` returns a `string` coercible into a formula (e.g [stats::as.formula()]).
|
|
114 |
#'
|
|
115 |
#' @examples
|
|
116 |
#' # `h_coxreg_multivar_formula`
|
|
117 |
#'
|
|
118 |
#' h_coxreg_multivar_formula(
|
|
119 |
#' variables = list(
|
|
120 |
#' time = "AVAL", event = "event", arm = "ARMCD", covariates = c("RACE", "AGE")
|
|
121 |
#' )
|
|
122 |
#' )
|
|
123 |
#'
|
|
124 |
#' # Addition of an optional strata.
|
|
125 |
#' h_coxreg_multivar_formula(
|
|
126 |
#' variables = list(
|
|
127 |
#' time = "AVAL", event = "event", arm = "ARMCD", covariates = c("RACE", "AGE"),
|
|
128 |
#' strata = "SITE"
|
|
129 |
#' )
|
|
130 |
#' )
|
|
131 |
#'
|
|
132 |
#' # Example without treatment arm.
|
|
133 |
#' h_coxreg_multivar_formula(
|
|
134 |
#' variables = list(
|
|
135 |
#' time = "AVAL", event = "event", covariates = c("RACE", "AGE"),
|
|
136 |
#' strata = "SITE"
|
|
137 |
#' )
|
|
138 |
#' )
|
|
139 |
#'
|
|
140 |
#' @export
|
|
141 |
h_coxreg_multivar_formula <- function(variables) { |
|
142 | 57x |
checkmate::assert_list(variables, names = "named") |
143 | 57x |
has_arm <- "arm" %in% names(variables) |
144 | 57x |
arm_name <- if (has_arm) "arm" else NULL |
145 | ||
146 | 57x |
checkmate::assert_character(variables$covariates, null.ok = TRUE) |
147 | ||
148 | 57x |
assert_list_of_variables(variables[c(arm_name, "event", "time")]) |
149 | ||
150 | 57x |
y <- paste0( |
151 | 57x |
"survival::Surv(", variables$time, ", ", variables$event, ") ~ ", |
152 | 57x |
ifelse(has_arm, variables$arm, "1") |
153 |
)
|
|
154 | 57x |
if (length(variables$covariates) > 0) { |
155 | 18x |
y <- paste(y, paste(variables$covariates, collapse = " + "), sep = " + ") |
156 |
}
|
|
157 | 57x |
if (!is.null(variables$strata)) { |
158 | 5x |
y <- paste0(y, " + strata(", paste0(variables$strata, collapse = ", "), ")") |
159 |
}
|
|
160 | 57x |
y
|
161 |
}
|
|
162 | ||
163 |
#' @describeIn h_cox_regression Utility function to help tabulate the result of
|
|
164 |
#' a univariate Cox regression model.
|
|
165 |
#'
|
|
166 |
#' @param effect (`string`)\cr the treatment variable.
|
|
167 |
#' @param mod (`coxph`)\cr Cox regression model fitted by [survival::coxph()].
|
|
168 |
#'
|
|
169 |
#' @return
|
|
170 |
#' * `h_coxreg_univar_extract()` returns a `data.frame` with variables `effect`, `term`, `term_label`, `level`,
|
|
171 |
#' `n`, `hr`, `lcl`, `ucl`, and `pval`.
|
|
172 |
#'
|
|
173 |
#' @examples
|
|
174 |
#' library(survival)
|
|
175 |
#'
|
|
176 |
#' dta_simple <- data.frame(
|
|
177 |
#' time = c(5, 5, 10, 10, 5, 5, 10, 10),
|
|
178 |
#' status = c(0, 0, 1, 0, 0, 1, 1, 1),
|
|
179 |
#' armcd = factor(LETTERS[c(1, 1, 1, 1, 2, 2, 2, 2)], levels = c("A", "B")),
|
|
180 |
#' var1 = c(45, 55, 65, 75, 55, 65, 85, 75),
|
|
181 |
#' var2 = c("F", "M", "F", "M", "F", "M", "F", "U")
|
|
182 |
#' )
|
|
183 |
#' mod <- coxph(Surv(time, status) ~ armcd + var1, data = dta_simple)
|
|
184 |
#' result <- h_coxreg_univar_extract(
|
|
185 |
#' effect = "armcd", covar = "armcd", mod = mod, data = dta_simple
|
|
186 |
#' )
|
|
187 |
#' result
|
|
188 |
#'
|
|
189 |
#' @export
|
|
190 |
h_coxreg_univar_extract <- function(effect, |
|
191 |
covar,
|
|
192 |
data,
|
|
193 |
mod,
|
|
194 |
control = control_coxreg()) { |
|
195 | 47x |
checkmate::assert_string(covar) |
196 | 47x |
checkmate::assert_string(effect) |
197 | 47x |
checkmate::assert_class(mod, "coxph") |
198 | 47x |
test_statistic <- c(wald = "Wald", likelihood = "LR")[control$pval_method] |
199 | ||
200 | 47x |
mod_aov <- muffled_car_anova(mod, test_statistic) |
201 | 47x |
msum <- summary(mod, conf.int = control$conf_level) |
202 | 47x |
sum_cox <- broom::tidy(msum) |
203 | ||
204 |
# Combine results together.
|
|
205 | 47x |
effect_aov <- mod_aov[effect, , drop = TRUE] |
206 | 47x |
pval <- effect_aov[[grep(pattern = "Pr", x = names(effect_aov)), drop = TRUE]] |
207 | 47x |
sum_main <- sum_cox[grepl(effect, sum_cox$level), ] |
208 | ||
209 | 47x |
term_label <- if (effect == covar) { |
210 | 25x |
paste0( |
211 | 25x |
levels(data[[covar]])[2], |
212 | 25x |
" vs control (",
|
213 | 25x |
levels(data[[covar]])[1], |
214 |
")"
|
|
215 |
)
|
|
216 |
} else { |
|
217 | 22x |
unname(labels_or_names(data[covar])) |
218 |
}
|
|
219 | 47x |
data.frame( |
220 | 47x |
effect = ifelse(covar == effect, "Treatment:", "Covariate:"), |
221 | 47x |
term = covar, |
222 | 47x |
term_label = term_label, |
223 | 47x |
level = levels(data[[effect]])[2], |
224 | 47x |
n = mod[["n"]], |
225 | 47x |
hr = unname(sum_main["exp(coef)"]), |
226 | 47x |
lcl = unname(sum_main[grep("lower", names(sum_main))]), |
227 | 47x |
ucl = unname(sum_main[grep("upper", names(sum_main))]), |
228 | 47x |
pval = pval, |
229 | 47x |
stringsAsFactors = FALSE |
230 |
)
|
|
231 |
}
|
|
232 | ||
233 |
#' @describeIn h_cox_regression Tabulation of multivariate Cox regressions. Utility function to help
|
|
234 |
#' tabulate the result of a multivariate Cox regression model for a treatment/covariate variable.
|
|
235 |
#'
|
|
236 |
#' @return
|
|
237 |
#' * `h_coxreg_multivar_extract()` returns a `data.frame` with variables `pval`, `hr`, `lcl`, `ucl`, `level`,
|
|
238 |
#' `n`, `term`, and `term_label`.
|
|
239 |
#'
|
|
240 |
#' @examples
|
|
241 |
#' mod <- coxph(Surv(time, status) ~ armcd + var1, data = dta_simple)
|
|
242 |
#' result <- h_coxreg_multivar_extract(
|
|
243 |
#' var = "var1", mod = mod, data = dta_simple
|
|
244 |
#' )
|
|
245 |
#' result
|
|
246 |
#'
|
|
247 |
#' @export
|
|
248 |
h_coxreg_multivar_extract <- function(var, |
|
249 |
data,
|
|
250 |
mod,
|
|
251 |
control = control_coxreg()) { |
|
252 | 76x |
test_statistic <- c(wald = "Wald", likelihood = "LR")[control$pval_method] |
253 | 76x |
mod_aov <- muffled_car_anova(mod, test_statistic) |
254 | ||
255 | 76x |
msum <- summary(mod, conf.int = control$conf_level) |
256 | 76x |
sum_anova <- broom::tidy(mod_aov) |
257 | 76x |
sum_cox <- broom::tidy(msum) |
258 | ||
259 | 76x |
ret_anova <- sum_anova[sum_anova$term == var, c("term", "p.value")] |
260 | 76x |
names(ret_anova)[2] <- "pval" |
261 | 76x |
if (is.factor(data[[var]])) { |
262 | 29x |
ret_cox <- sum_cox[startsWith(prefix = var, x = sum_cox$level), !(names(sum_cox) %in% "exp(-coef)")] |
263 |
} else { |
|
264 | 47x |
ret_cox <- sum_cox[(var == sum_cox$level), !(names(sum_cox) %in% "exp(-coef)")] |
265 |
}
|
|
266 | 76x |
names(ret_cox)[1:4] <- c("pval", "hr", "lcl", "ucl") |
267 | 76x |
varlab <- unname(labels_or_names(data[var])) |
268 | 76x |
ret_cox$term <- varlab |
269 | ||
270 | 76x |
if (is.numeric(data[[var]])) { |
271 | 47x |
ret <- ret_cox |
272 | 47x |
ret$term_label <- ret$term |
273 | 29x |
} else if (length(levels(data[[var]])) <= 2) { |
274 | 18x |
ret_anova$pval <- NA |
275 | 18x |
ret_anova$term_label <- paste0(varlab, " (reference = ", levels(data[[var]])[1], ")") |
276 | 18x |
ret_cox$level <- gsub(var, "", ret_cox$level) |
277 | 18x |
ret_cox$term_label <- ret_cox$level |
278 | 18x |
ret <- dplyr::bind_rows(ret_anova, ret_cox) |
279 |
} else { |
|
280 | 11x |
ret_anova$term_label <- paste0(varlab, " (reference = ", levels(data[[var]])[1], ")") |
281 | 11x |
ret_cox$level <- gsub(var, "", ret_cox$level) |
282 | 11x |
ret_cox$term_label <- ret_cox$level |
283 | 11x |
ret <- dplyr::bind_rows(ret_anova, ret_cox) |
284 |
}
|
|
285 | ||
286 | 76x |
as.data.frame(ret) |
287 |
}
|
1 |
#' Helper Functions for Subgroup Treatment Effect Pattern (STEP) Calculations
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Helper functions that are used internally for the STEP calculations.
|
|
6 |
#'
|
|
7 |
#' @inheritParams argument_convention
|
|
8 |
#'
|
|
9 |
#' @name h_step
|
|
10 |
#' @include control_step.R
|
|
11 |
NULL
|
|
12 | ||
13 |
#' @describeIn h_step creates the windows for STEP, based on the control settings
|
|
14 |
#' provided.
|
|
15 |
#'
|
|
16 |
#' @param x (`numeric`)\cr biomarker value(s) to use (without `NA`).
|
|
17 |
#' @param control (named `list`)\cr output from `control_step()`.
|
|
18 |
#'
|
|
19 |
#' @return
|
|
20 |
#' * `h_step_window()` returns a list containing the window-selection matrix `sel`
|
|
21 |
#' and the interval information matrix `interval`.
|
|
22 |
#'
|
|
23 |
#' @export
|
|
24 |
h_step_window <- function(x, |
|
25 |
control = control_step()) { |
|
26 | 12x |
checkmate::assert_numeric(x, min.len = 1, any.missing = FALSE) |
27 | 12x |
checkmate::assert_list(control, names = "named") |
28 | ||
29 | 12x |
sel <- matrix(FALSE, length(x), control$num_points) |
30 | 12x |
out <- matrix(0, control$num_points, 3) |
31 | 12x |
colnames(out) <- paste("Interval", c("Center", "Lower", "Upper")) |
32 | 12x |
if (control$use_percentile) { |
33 |
# Create windows according to percentile cutoffs.
|
|
34 | 9x |
out <- cbind(out, out) |
35 | 9x |
colnames(out)[1:3] <- paste("Percentile", c("Center", "Lower", "Upper")) |
36 | 9x |
xs <- seq(0, 1, length = control$num_points + 2)[-1] |
37 | 9x |
for (i in seq_len(control$num_points)) { |
38 | 185x |
out[i, 2:3] <- c( |
39 | 185x |
max(xs[i] - control$bandwidth, 0), |
40 | 185x |
min(xs[i] + control$bandwidth, 1) |
41 |
)
|
|
42 | 185x |
out[i, 5:6] <- stats::quantile(x, out[i, 2:3]) |
43 | 185x |
sel[, i] <- x >= out[i, 5] & x <= out[i, 6] |
44 |
}
|
|
45 |
# Center is the middle point of the percentile window.
|
|
46 | 9x |
out[, 1] <- xs[-control$num_points - 1] |
47 | 9x |
out[, 4] <- stats::quantile(x, out[, 1]) |
48 |
} else { |
|
49 |
# Create windows according to cutoffs.
|
|
50 | 3x |
m <- c(min(x), max(x)) |
51 | 3x |
xs <- seq(m[1], m[2], length = control$num_points + 2)[-1] |
52 | 3x |
for (i in seq_len(control$num_points)) { |
53 | 11x |
out[i, 2:3] <- c( |
54 | 11x |
max(xs[i] - control$bandwidth, m[1]), |
55 | 11x |
min(xs[i] + control$bandwidth, m[2]) |
56 |
)
|
|
57 | 11x |
sel[, i] <- x >= out[i, 2] & x <= out[i, 3] |
58 |
}
|
|
59 |
# Center is the same as the point for predicting.
|
|
60 | 3x |
out[, 1] <- xs[-control$num_points - 1] |
61 |
}
|
|
62 | 12x |
list(sel = sel, interval = out) |
63 |
}
|
|
64 | ||
65 |
#' @describeIn h_step calculates the estimated treatment effect estimate
|
|
66 |
#' on the linear predictor scale and corresponding standard error from a STEP `model` fitted
|
|
67 |
#' on `data` given `variables` specification, for a single biomarker value `x`.
|
|
68 |
#' This works for both `coxph` and `glm` models, i.e. for calculating log hazard ratio or log odds
|
|
69 |
#' ratio estimates.
|
|
70 |
#'
|
|
71 |
#' @param model the regression model object.
|
|
72 |
#'
|
|
73 |
#' @return
|
|
74 |
#' * `h_step_trt_effect()` returns a vector with elements `est` and `se`.
|
|
75 |
#'
|
|
76 |
#' @export
|
|
77 |
h_step_trt_effect <- function(data, |
|
78 |
model,
|
|
79 |
variables,
|
|
80 |
x) { |
|
81 | 208x |
checkmate::assert_multi_class(model, c("coxph", "glm")) |
82 | 208x |
checkmate::assert_number(x) |
83 | 208x |
assert_df_with_variables(data, variables) |
84 | 208x |
checkmate::assert_factor(data[[variables$arm]], n.levels = 2) |
85 | ||
86 | 208x |
newdata <- data[c(1, 1), ] |
87 | 208x |
newdata[, variables$biomarker] <- x |
88 | 208x |
newdata[, variables$arm] <- levels(data[[variables$arm]]) |
89 | 208x |
model_terms <- stats::delete.response(stats::terms(model)) |
90 | 208x |
model_frame <- stats::model.frame(model_terms, data = newdata, xlev = model$xlevels) |
91 | 208x |
mat <- stats::model.matrix(model_terms, data = model_frame, contrasts.arg = model$contrasts) |
92 | 208x |
coefs <- stats::coef(model) |
93 |
# Note: It is important to use the coef subset from matrix, otherwise intercept and
|
|
94 |
# strata are included for coxph() models.
|
|
95 | 208x |
mat <- mat[, names(coefs)] |
96 | 208x |
mat_diff <- diff(mat) |
97 | 208x |
est <- mat_diff %*% coefs |
98 | 208x |
var <- mat_diff %*% stats::vcov(model) %*% t(mat_diff) |
99 | 208x |
se <- sqrt(var) |
100 | 208x |
c( |
101 | 208x |
est = est, |
102 | 208x |
se = se |
103 |
)
|
|
104 |
}
|
|
105 | ||
106 |
#' @describeIn h_step builds the model formula used in survival STEP calculations.
|
|
107 |
#'
|
|
108 |
#' @return
|
|
109 |
#' * `h_step_survival_formula()` returns a model formula.
|
|
110 |
#'
|
|
111 |
#' @export
|
|
112 |
h_step_survival_formula <- function(variables, |
|
113 |
control = control_step()) { |
|
114 | 10x |
checkmate::assert_character(variables$covariates, null.ok = TRUE) |
115 | ||
116 | 10x |
assert_list_of_variables(variables[c("arm", "biomarker", "event", "time")]) |
117 | 10x |
form <- paste0("Surv(", variables$time, ", ", variables$event, ") ~ ", variables$arm) |
118 | 10x |
if (control$degree > 0) { |
119 | 5x |
form <- paste0(form, " * stats::poly(", variables$biomarker, ", degree = ", control$degree, ", raw = TRUE)") |
120 |
}
|
|
121 | 10x |
if (!is.null(variables$covariates)) { |
122 | 6x |
form <- paste(form, "+", paste(variables$covariates, collapse = "+")) |
123 |
}
|
|
124 | 10x |
if (!is.null(variables$strata)) { |
125 | 2x |
form <- paste0(form, " + strata(", paste0(variables$strata, collapse = ", "), ")") |
126 |
}
|
|
127 | 10x |
stats::as.formula(form) |
128 |
}
|
|
129 | ||
130 |
#' @describeIn h_step estimates the model with `formula` built based on
|
|
131 |
#' `variables` in `data` for a given `subset` and `control` parameters for the
|
|
132 |
#' Cox regression.
|
|
133 |
#'
|
|
134 |
#' @param formula (`formula`)\cr the regression model formula.
|
|
135 |
#' @param subset (`logical`)\cr subset vector.
|
|
136 |
#'
|
|
137 |
#' @return
|
|
138 |
#' * `h_step_survival_est()` returns a matrix of number of observations `n`,
|
|
139 |
#' `events`, log hazard ratio estimates `loghr`, standard error `se`,
|
|
140 |
#' and Wald confidence interval bounds `ci_lower` and `ci_upper`. One row is
|
|
141 |
#' included for each biomarker value in `x`.
|
|
142 |
#'
|
|
143 |
#' @export
|
|
144 |
h_step_survival_est <- function(formula, |
|
145 |
data,
|
|
146 |
variables,
|
|
147 |
x,
|
|
148 |
subset = rep(TRUE, nrow(data)), |
|
149 |
control = control_coxph()) { |
|
150 | 55x |
checkmate::assert_formula(formula) |
151 | 55x |
assert_df_with_variables(data, variables) |
152 | 55x |
checkmate::assert_logical(subset, min.len = 1, any.missing = FALSE) |
153 | 55x |
checkmate::assert_numeric(x, min.len = 1, any.missing = FALSE) |
154 | 55x |
checkmate::assert_list(control, names = "named") |
155 | ||
156 |
# Note: `subset` in `coxph` needs to be an expression referring to `data` variables.
|
|
157 | 55x |
data$.subset <- subset |
158 | 55x |
coxph_warnings <- NULL |
159 | 55x |
tryCatch( |
160 | 55x |
withCallingHandlers( |
161 | 55x |
expr = { |
162 | 55x |
fit <- survival::coxph( |
163 | 55x |
formula = formula, |
164 | 55x |
data = data, |
165 | 55x |
subset = .subset, |
166 | 55x |
ties = control$ties |
167 |
)
|
|
168 |
},
|
|
169 | 55x |
warning = function(w) { |
170 | 1x |
coxph_warnings <<- c(coxph_warnings, w) |
171 | 1x |
invokeRestart("muffleWarning") |
172 |
}
|
|
173 |
),
|
|
174 | 55x |
finally = { |
175 |
}
|
|
176 |
)
|
|
177 | 55x |
if (!is.null(coxph_warnings)) { |
178 | 1x |
warning(paste( |
179 | 1x |
"Fit warnings occurred, please consider using a simpler model, or",
|
180 | 1x |
"larger `bandwidth`, less `num_points` in `control_step()` settings"
|
181 |
)) |
|
182 |
}
|
|
183 |
# Produce a matrix with one row per `x` and columns `est` and `se`.
|
|
184 | 55x |
estimates <- t(vapply( |
185 | 55x |
X = x, |
186 | 55x |
FUN = h_step_trt_effect, |
187 | 55x |
FUN.VALUE = c(1, 2), |
188 | 55x |
data = data, |
189 | 55x |
model = fit, |
190 | 55x |
variables = variables |
191 |
)) |
|
192 | 55x |
q_norm <- stats::qnorm((1 + control$conf_level) / 2) |
193 | 55x |
cbind( |
194 | 55x |
n = fit$n, |
195 | 55x |
events = fit$nevent, |
196 | 55x |
loghr = estimates[, "est"], |
197 | 55x |
se = estimates[, "se"], |
198 | 55x |
ci_lower = estimates[, "est"] - q_norm * estimates[, "se"], |
199 | 55x |
ci_upper = estimates[, "est"] + q_norm * estimates[, "se"] |
200 |
)
|
|
201 |
}
|
|
202 | ||
203 |
#' @describeIn h_step builds the model formula used in response STEP calculations.
|
|
204 |
#'
|
|
205 |
#' @return
|
|
206 |
#' * `h_step_rsp_formula()` returns a model formula.
|
|
207 |
#'
|
|
208 |
#' @export
|
|
209 |
h_step_rsp_formula <- function(variables, |
|
210 |
control = c(control_step(), control_logistic())) { |
|
211 | 14x |
checkmate::assert_character(variables$covariates, null.ok = TRUE) |
212 | 14x |
assert_list_of_variables(variables[c("arm", "biomarker", "response")]) |
213 | 14x |
response_definition <- sub( |
214 | 14x |
pattern = "response", |
215 | 14x |
replacement = variables$response, |
216 | 14x |
x = control$response_definition, |
217 | 14x |
fixed = TRUE |
218 |
)
|
|
219 | 14x |
form <- paste0(response_definition, " ~ ", variables$arm) |
220 | 14x |
if (control$degree > 0) { |
221 | 8x |
form <- paste0(form, " * stats::poly(", variables$biomarker, ", degree = ", control$degree, ", raw = TRUE)") |
222 |
}
|
|
223 | 14x |
if (!is.null(variables$covariates)) { |
224 | 8x |
form <- paste(form, "+", paste(variables$covariates, collapse = "+")) |
225 |
}
|
|
226 | 14x |
if (!is.null(variables$strata)) { |
227 | 5x |
strata_arg <- if (length(variables$strata) > 1) { |
228 | 2x |
paste0("I(interaction(", paste0(variables$strata, collapse = ", "), "))") |
229 |
} else { |
|
230 | 3x |
variables$strata |
231 |
}
|
|
232 | 5x |
form <- paste0(form, "+ strata(", strata_arg, ")") |
233 |
}
|
|
234 | 14x |
stats::as.formula(form) |
235 |
}
|
|
236 | ||
237 |
#' @describeIn h_step estimates the model with `formula` built based on
|
|
238 |
#' `variables` in `data` for a given `subset` and `control` parameters for the
|
|
239 |
#' logistic regression.
|
|
240 |
#'
|
|
241 |
#' @param formula (`formula`)\cr the regression model formula.
|
|
242 |
#' @param subset (`logical`)\cr subset vector.
|
|
243 |
#'
|
|
244 |
#' @return
|
|
245 |
#' * `h_step_rsp_est()` returns a matrix of number of observations `n`, log odds
|
|
246 |
#' ratio estimates `logor`, standard error `se`, and Wald confidence interval bounds
|
|
247 |
#' `ci_lower` and `ci_upper`. One row is included for each biomarker value in `x`.
|
|
248 |
#'
|
|
249 |
#' @export
|
|
250 |
h_step_rsp_est <- function(formula, |
|
251 |
data,
|
|
252 |
variables,
|
|
253 |
x,
|
|
254 |
subset = rep(TRUE, nrow(data)), |
|
255 |
control = control_logistic()) { |
|
256 | 58x |
checkmate::assert_formula(formula) |
257 | 58x |
assert_df_with_variables(data, variables) |
258 | 58x |
checkmate::assert_logical(subset, min.len = 1, any.missing = FALSE) |
259 | 58x |
checkmate::assert_numeric(x, min.len = 1, any.missing = FALSE) |
260 | 58x |
checkmate::assert_list(control, names = "named") |
261 |
# Note: `subset` in `glm` needs to be an expression referring to `data` variables.
|
|
262 | 58x |
data$.subset <- subset |
263 | 58x |
fit_warnings <- NULL |
264 | 58x |
tryCatch( |
265 | 58x |
withCallingHandlers( |
266 | 58x |
expr = { |
267 | 58x |
fit <- if (is.null(variables$strata)) { |
268 | 54x |
stats::glm( |
269 | 54x |
formula = formula, |
270 | 54x |
data = data, |
271 | 54x |
subset = .subset, |
272 | 54x |
family = stats::binomial("logit") |
273 |
)
|
|
274 |
} else { |
|
275 |
# clogit needs coxph and strata imported
|
|
276 | 4x |
survival::clogit( |
277 | 4x |
formula = formula, |
278 | 4x |
data = data, |
279 | 4x |
subset = .subset |
280 |
)
|
|
281 |
}
|
|
282 |
},
|
|
283 | 58x |
warning = function(w) { |
284 | 19x |
fit_warnings <<- c(fit_warnings, w) |
285 | 19x |
invokeRestart("muffleWarning") |
286 |
}
|
|
287 |
),
|
|
288 | 58x |
finally = { |
289 |
}
|
|
290 |
)
|
|
291 | 58x |
if (!is.null(fit_warnings)) { |
292 | 13x |
warning(paste( |
293 | 13x |
"Fit warnings occurred, please consider using a simpler model, or",
|
294 | 13x |
"larger `bandwidth`, less `num_points` in `control_step()` settings"
|
295 |
)) |
|
296 |
}
|
|
297 |
# Produce a matrix with one row per `x` and columns `est` and `se`.
|
|
298 | 58x |
estimates <- t(vapply( |
299 | 58x |
X = x, |
300 | 58x |
FUN = h_step_trt_effect, |
301 | 58x |
FUN.VALUE = c(1, 2), |
302 | 58x |
data = data, |
303 | 58x |
model = fit, |
304 | 58x |
variables = variables |
305 |
)) |
|
306 | 58x |
q_norm <- stats::qnorm((1 + control$conf_level) / 2) |
307 | 58x |
cbind( |
308 | 58x |
n = length(fit$y), |
309 | 58x |
logor = estimates[, "est"], |
310 | 58x |
se = estimates[, "se"], |
311 | 58x |
ci_lower = estimates[, "est"] - q_norm * estimates[, "se"], |
312 | 58x |
ci_upper = estimates[, "est"] + q_norm * estimates[, "se"] |
313 |
)
|
|
314 |
}
|
1 |
#' Helper Functions for Tabulating Biomarker Effects on Binary Response by Subgroup
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Helper functions which are documented here separately to not confuse the user
|
|
6 |
#' when reading about the user-facing functions.
|
|
7 |
#'
|
|
8 |
#' @inheritParams response_biomarkers_subgroups
|
|
9 |
#' @inheritParams extract_rsp_biomarkers
|
|
10 |
#' @inheritParams argument_convention
|
|
11 |
#'
|
|
12 |
#' @examples
|
|
13 |
#' library(dplyr)
|
|
14 |
#' library(forcats)
|
|
15 |
#'
|
|
16 |
#' adrs <- tern_ex_adrs
|
|
17 |
#' adrs_labels <- formatters::var_labels(adrs)
|
|
18 |
#'
|
|
19 |
#' adrs_f <- adrs %>%
|
|
20 |
#' filter(PARAMCD == "BESRSPI") %>%
|
|
21 |
#' mutate(rsp = AVALC == "CR")
|
|
22 |
#' formatters::var_labels(adrs_f) <- c(adrs_labels, "Response")
|
|
23 |
#'
|
|
24 |
#' @name h_response_biomarkers_subgroups
|
|
25 |
NULL
|
|
26 | ||
27 |
#' @describeIn h_response_biomarkers_subgroups helps with converting the "response" function variable list
|
|
28 |
#' to the "logistic regression" variable list. The reason is that currently there is an
|
|
29 |
#' inconsistency between the variable names accepted by `extract_rsp_subgroups()` and `fit_logistic()`.
|
|
30 |
#'
|
|
31 |
#' @param biomarker (`string`)\cr the name of the biomarker variable.
|
|
32 |
#'
|
|
33 |
#' @return
|
|
34 |
#' * `h_rsp_to_logistic_variables()` returns a named `list` of elements `response`, `arm`, `covariates`, and `strata`.
|
|
35 |
#'
|
|
36 |
#' @examples
|
|
37 |
#' # This is how the variable list is converted internally.
|
|
38 |
#' h_rsp_to_logistic_variables(
|
|
39 |
#' variables = list(
|
|
40 |
#' rsp = "RSP",
|
|
41 |
#' covariates = c("A", "B"),
|
|
42 |
#' strat = "D"
|
|
43 |
#' ),
|
|
44 |
#' biomarker = "AGE"
|
|
45 |
#' )
|
|
46 |
#'
|
|
47 |
#' @export
|
|
48 |
h_rsp_to_logistic_variables <- function(variables, biomarker) { |
|
49 | 37x |
checkmate::assert_list(variables) |
50 | 37x |
checkmate::assert_string(variables$rsp) |
51 | 37x |
checkmate::assert_string(biomarker) |
52 | 37x |
list( |
53 | 37x |
response = variables$rsp, |
54 | 37x |
arm = biomarker, |
55 | 37x |
covariates = variables$covariates, |
56 | 37x |
strata = variables$strat |
57 |
)
|
|
58 |
}
|
|
59 | ||
60 |
#' @describeIn h_response_biomarkers_subgroups prepares estimates for number of responses, patients and
|
|
61 |
#' overall response rate, as well as odds ratio estimates, confidence intervals and p-values, for multiple
|
|
62 |
#' biomarkers in a given single data set.
|
|
63 |
#' `variables` corresponds to names of variables found in `data`, passed as a named list and requires elements
|
|
64 |
#' `rsp` and `biomarkers` (vector of continuous biomarker variables) and optionally `covariates`
|
|
65 |
#' and `strat`.
|
|
66 |
#'
|
|
67 |
#' @return
|
|
68 |
#' * `h_logistic_mult_cont_df()` returns a `data.frame` containing estimates and statistics for the selected biomarkers.
|
|
69 |
#'
|
|
70 |
#' @examples
|
|
71 |
#' # For a single population, estimate separately the effects
|
|
72 |
#' # of two biomarkers.
|
|
73 |
#' df <- h_logistic_mult_cont_df(
|
|
74 |
#' variables = list(
|
|
75 |
#' rsp = "rsp",
|
|
76 |
#' biomarkers = c("BMRKR1", "AGE"),
|
|
77 |
#' covariates = "SEX"
|
|
78 |
#' ),
|
|
79 |
#' data = adrs_f
|
|
80 |
#' )
|
|
81 |
#' df
|
|
82 |
#'
|
|
83 |
#' # If the data set is empty, still the corresponding rows with missings are returned.
|
|
84 |
#' h_coxreg_mult_cont_df(
|
|
85 |
#' variables = list(
|
|
86 |
#' rsp = "rsp",
|
|
87 |
#' biomarkers = c("BMRKR1", "AGE"),
|
|
88 |
#' covariates = "SEX",
|
|
89 |
#' strat = "STRATA1"
|
|
90 |
#' ),
|
|
91 |
#' data = adrs_f[NULL, ]
|
|
92 |
#' )
|
|
93 |
#'
|
|
94 |
#' @export
|
|
95 |
h_logistic_mult_cont_df <- function(variables, |
|
96 |
data,
|
|
97 |
control = control_logistic()) { |
|
98 | 22x |
assert_df_with_variables(data, variables) |
99 | ||
100 | 22x |
checkmate::assert_character(variables$biomarkers, min.len = 1, any.missing = FALSE) |
101 | 22x |
checkmate::assert_list(control, names = "named") |
102 | ||
103 | 22x |
conf_level <- control[["conf_level"]] |
104 | 22x |
pval_label <- "p-value (Wald)" |
105 | ||
106 |
# If there is any data, run model, otherwise return empty results.
|
|
107 | 22x |
if (nrow(data) > 0) { |
108 | 21x |
bm_cols <- match(variables$biomarkers, names(data)) |
109 | 21x |
l_result <- lapply(variables$biomarkers, function(bm) { |
110 | 36x |
model_fit <- fit_logistic( |
111 | 36x |
variables = h_rsp_to_logistic_variables(variables, bm), |
112 | 36x |
data = data, |
113 | 36x |
response_definition = control$response_definition |
114 |
)
|
|
115 | 36x |
result <- h_logistic_simple_terms( |
116 | 36x |
x = bm, |
117 | 36x |
fit_glm = model_fit, |
118 | 36x |
conf_level = control$conf_level |
119 |
)
|
|
120 | 36x |
resp_vector <- if (inherits(model_fit, "glm")) { |
121 | 26x |
model_fit$model[[variables$rsp]] |
122 |
} else { |
|
123 | 10x |
as.logical(as.matrix(model_fit$y)[, "status"]) |
124 |
}
|
|
125 | 36x |
data.frame( |
126 |
# Dummy column needed downstream to create a nested header.
|
|
127 | 36x |
biomarker = bm, |
128 | 36x |
biomarker_label = formatters::var_labels(data[bm], fill = TRUE), |
129 | 36x |
n_tot = length(resp_vector), |
130 | 36x |
n_rsp = sum(resp_vector), |
131 | 36x |
prop = mean(resp_vector), |
132 | 36x |
or = as.numeric(result[1L, "odds_ratio"]), |
133 | 36x |
lcl = as.numeric(result[1L, "lcl"]), |
134 | 36x |
ucl = as.numeric(result[1L, "ucl"]), |
135 | 36x |
conf_level = conf_level, |
136 | 36x |
pval = as.numeric(result[1L, "pvalue"]), |
137 | 36x |
pval_label = pval_label, |
138 | 36x |
stringsAsFactors = FALSE |
139 |
)
|
|
140 |
}) |
|
141 | 21x |
do.call(rbind, args = c(l_result, make.row.names = FALSE)) |
142 |
} else { |
|
143 | 1x |
data.frame( |
144 | 1x |
biomarker = variables$biomarkers, |
145 | 1x |
biomarker_label = formatters::var_labels(data[variables$biomarkers], fill = TRUE), |
146 | 1x |
n_tot = 0L, |
147 | 1x |
n_rsp = 0L, |
148 | 1x |
prop = NA, |
149 | 1x |
or = NA, |
150 | 1x |
lcl = NA, |
151 | 1x |
ucl = NA, |
152 | 1x |
conf_level = conf_level, |
153 | 1x |
pval = NA, |
154 | 1x |
pval_label = pval_label, |
155 | 1x |
row.names = seq_along(variables$biomarkers), |
156 | 1x |
stringsAsFactors = FALSE |
157 |
)
|
|
158 |
}
|
|
159 |
}
|
|
160 | ||
161 |
#' @describeIn h_response_biomarkers_subgroups prepares a single sub-table given a `df_sub` containing
|
|
162 |
#' the results for a single biomarker.
|
|
163 |
#'
|
|
164 |
#' @param df (`data.frame`)\cr results for a single biomarker, as part of what is
|
|
165 |
#' returned by [extract_rsp_biomarkers()] (it needs a couple of columns which are
|
|
166 |
#' added by that high-level function relative to what is returned by [h_logistic_mult_cont_df()],
|
|
167 |
#' see the example).
|
|
168 |
#'
|
|
169 |
#' @return
|
|
170 |
#' * `h_tab_rsp_one_biomarker()` returns an `rtables` table object with the given statistics arranged in columns.
|
|
171 |
#'
|
|
172 |
#' @examples
|
|
173 |
#' # Starting from above `df`, zoom in on one biomarker and add required columns.
|
|
174 |
#' df1 <- df[1, ]
|
|
175 |
#' df1$subgroup <- "All patients"
|
|
176 |
#' df1$row_type <- "content"
|
|
177 |
#' df1$var <- "ALL"
|
|
178 |
#' df1$var_label <- "All patients"
|
|
179 |
#'
|
|
180 |
#' h_tab_rsp_one_biomarker(
|
|
181 |
#' df1,
|
|
182 |
#' vars = c("n_tot", "n_rsp", "prop", "or", "ci", "pval")
|
|
183 |
#' )
|
|
184 |
#'
|
|
185 |
#' @export
|
|
186 |
h_tab_rsp_one_biomarker <- function(df, |
|
187 |
vars,
|
|
188 |
.indent_mods = 0L) { |
|
189 | 6x |
afuns <- a_response_subgroups()[vars] |
190 | 6x |
colvars <- d_rsp_subgroups_colvars( |
191 | 6x |
vars,
|
192 | 6x |
conf_level = df$conf_level[1], |
193 | 6x |
method = df$pval_label[1] |
194 |
)
|
|
195 | 6x |
h_tab_one_biomarker( |
196 | 6x |
df = df, |
197 | 6x |
afuns = afuns, |
198 | 6x |
colvars = colvars, |
199 | 6x |
.indent_mods = .indent_mods |
200 |
)
|
|
201 |
}
|
1 |
#' Helper Function for Deriving Analysis Datasets for `LBT13` and `LBT14`
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Helper function that merges `ADSL` and `ADLB` datasets so that missing lab test records are inserted in the
|
|
6 |
#' output dataset.
|
|
7 |
#'
|
|
8 |
#' @param adsl (`data.frame`)\cr `ADSL` dataframe.
|
|
9 |
#' @param adlb (`data.frame`)\cr `ADLB` dataframe.
|
|
10 |
#' @param worst_flag (named `vector`)\cr Worst post-baseline lab flag variable.
|
|
11 |
#' @param by_visit (`logical`)\cr defaults to `FALSE` to generate worst grade per patient.
|
|
12 |
#' If worst grade per patient per visit is specified for `worst_flag`, then
|
|
13 |
#' `by_visit` should be `TRUE` to generate worst grade patient per visit.
|
|
14 |
#' @param no_fillin_visits (named `character`)\cr Visits that are not considered for post-baseline worst toxicity
|
|
15 |
#' grade. Defaults to `c("SCREENING", "BASELINE")`.
|
|
16 |
#'
|
|
17 |
#' @return `df` containing variables shared between `adlb` and `adsl` along with variables `PARAM`, `PARAMCD`,
|
|
18 |
#' `ATOXGR`, and `BTOXGR` relevant for analysis. Optionally, `AVISIT` are `AVISITN` are included when
|
|
19 |
#' `by_visit = TRUE` and `no_fillin_visits = c("SCREENING", "BASELINE")`.
|
|
20 |
#'
|
|
21 |
#' @details In the result data missing records will be created for the following situations:
|
|
22 |
#' * Patients who are present in `adsl` but have no lab data in `adlb` (both baseline and post-baseline).
|
|
23 |
#' * Patients who do not have any post-baseline lab values.
|
|
24 |
#' * Patients without any post-baseline values flagged as the worst.
|
|
25 |
#'
|
|
26 |
#' @examples
|
|
27 |
#' # `h_adsl_adlb_merge_using_worst_flag`
|
|
28 |
#' adlb_out <- h_adsl_adlb_merge_using_worst_flag(
|
|
29 |
#' tern_ex_adsl,
|
|
30 |
#' tern_ex_adlb,
|
|
31 |
#' worst_flag = c("WGRHIFL" = "Y")
|
|
32 |
#' )
|
|
33 |
#'
|
|
34 |
#' # `h_adsl_adlb_merge_using_worst_flag` by visit example
|
|
35 |
#' adlb_out_by_visit <- h_adsl_adlb_merge_using_worst_flag(
|
|
36 |
#' tern_ex_adsl,
|
|
37 |
#' tern_ex_adlb,
|
|
38 |
#' worst_flag = c("WGRLOVFL" = "Y"),
|
|
39 |
#' by_visit = TRUE
|
|
40 |
#' )
|
|
41 |
#'
|
|
42 |
#' @export
|
|
43 |
h_adsl_adlb_merge_using_worst_flag <- function(adsl, # nolint |
|
44 |
adlb,
|
|
45 |
worst_flag = c("WGRHIFL" = "Y"), |
|
46 |
by_visit = FALSE, |
|
47 |
no_fillin_visits = c("SCREENING", "BASELINE")) { |
|
48 | 5x |
col_names <- names(worst_flag) |
49 | 5x |
filter_values <- worst_flag |
50 | ||
51 | 5x |
temp <- Map( |
52 | 5x |
function(x, y) which(adlb[[x]] == y), |
53 | 5x |
col_names,
|
54 | 5x |
filter_values
|
55 |
)
|
|
56 | ||
57 | 5x |
position_satisfy_filters <- Reduce(intersect, temp) |
58 | ||
59 | 5x |
adsl_adlb_common_columns <- intersect(colnames(adsl), colnames(adlb)) |
60 | 5x |
columns_from_adlb <- c("USUBJID", "PARAM", "PARAMCD", "AVISIT", "AVISITN", "ATOXGR", "BTOXGR") |
61 | ||
62 | 5x |
adlb_f <- adlb[position_satisfy_filters, ] %>% |
63 | 5x |
dplyr::filter(!.data[["AVISIT"]] %in% no_fillin_visits) |
64 | 5x |
adlb_f <- adlb_f[, columns_from_adlb] |
65 | ||
66 | 5x |
avisits_grid <- adlb %>% |
67 | 5x |
dplyr::filter(!.data[["AVISIT"]] %in% no_fillin_visits) %>% |
68 | 5x |
dplyr::pull(.data[["AVISIT"]]) %>% |
69 | 5x |
unique() |
70 | ||
71 | 5x |
if (by_visit) { |
72 | 1x |
adsl_lb <- expand.grid( |
73 | 1x |
USUBJID = unique(adsl$USUBJID), |
74 | 1x |
AVISIT = avisits_grid, |
75 | 1x |
PARAMCD = unique(adlb$PARAMCD) |
76 |
)
|
|
77 | ||
78 | 1x |
adsl_lb <- adsl_lb %>% |
79 | 1x |
dplyr::left_join(unique(adlb[c("AVISIT", "AVISITN")]), by = "AVISIT") %>% |
80 | 1x |
dplyr::left_join(unique(adlb[c("PARAM", "PARAMCD")]), by = "PARAMCD") |
81 | ||
82 | 1x |
adsl1 <- adsl[, adsl_adlb_common_columns] |
83 | 1x |
adsl_lb <- adsl1 %>% merge(adsl_lb, by = "USUBJID") |
84 | ||
85 | 1x |
by_variables_from_adlb <- c("USUBJID", "AVISIT", "AVISITN", "PARAMCD", "PARAM") |
86 | ||
87 | 1x |
adlb_btoxgr <- adlb %>% |
88 | 1x |
dplyr::select(c("USUBJID", "PARAMCD", "BTOXGR")) %>% |
89 | 1x |
unique() %>% |
90 | 1x |
dplyr::rename("BTOXGR_MAP" = "BTOXGR") |
91 | ||
92 | 1x |
adlb_out <- merge( |
93 | 1x |
adlb_f,
|
94 | 1x |
adsl_lb,
|
95 | 1x |
by = by_variables_from_adlb, |
96 | 1x |
all = TRUE, |
97 | 1x |
sort = FALSE |
98 |
)
|
|
99 | 1x |
adlb_out <- adlb_out %>% |
100 | 1x |
dplyr::left_join(adlb_btoxgr, by = c("USUBJID", "PARAMCD")) %>% |
101 | 1x |
dplyr::mutate(BTOXGR = .data$BTOXGR_MAP) %>% |
102 | 1x |
dplyr::select(-"BTOXGR_MAP") |
103 | ||
104 | 1x |
adlb_var_labels <- c( |
105 | 1x |
formatters::var_labels(adlb[by_variables_from_adlb]), |
106 | 1x |
formatters::var_labels(adlb[columns_from_adlb[!columns_from_adlb %in% by_variables_from_adlb]]), |
107 | 1x |
formatters::var_labels(adsl[adsl_adlb_common_columns[adsl_adlb_common_columns != "USUBJID"]]) |
108 |
)
|
|
109 |
} else { |
|
110 | 4x |
adsl_lb <- expand.grid( |
111 | 4x |
USUBJID = unique(adsl$USUBJID), |
112 | 4x |
PARAMCD = unique(adlb$PARAMCD) |
113 |
)
|
|
114 | ||
115 | 4x |
adsl_lb <- adsl_lb %>% dplyr::left_join(unique(adlb[c("PARAM", "PARAMCD")]), by = "PARAMCD") |
116 | ||
117 | 4x |
adsl1 <- adsl[, adsl_adlb_common_columns] |
118 | 4x |
adsl_lb <- adsl1 %>% merge(adsl_lb, by = "USUBJID") |
119 | ||
120 | 4x |
by_variables_from_adlb <- c("USUBJID", "PARAMCD", "PARAM") |
121 | ||
122 | 4x |
adlb_out <- merge( |
123 | 4x |
adlb_f,
|
124 | 4x |
adsl_lb,
|
125 | 4x |
by = by_variables_from_adlb, |
126 | 4x |
all = TRUE, |
127 | 4x |
sort = FALSE |
128 |
)
|
|
129 | ||
130 | 4x |
adlb_var_labels <- c( |
131 | 4x |
formatters::var_labels(adlb[by_variables_from_adlb]), |
132 | 4x |
formatters::var_labels(adlb[columns_from_adlb[!columns_from_adlb %in% by_variables_from_adlb]]), |
133 | 4x |
formatters::var_labels(adsl[adsl_adlb_common_columns[adsl_adlb_common_columns != "USUBJID"]]) |
134 |
)
|
|
135 |
}
|
|
136 | ||
137 | 5x |
adlb_out$ATOXGR <- as.factor(adlb_out$ATOXGR) |
138 | 5x |
adlb_out$BTOXGR <- as.factor(adlb_out$BTOXGR) |
139 | ||
140 | 5x |
adlb_out <- df_explicit_na(adlb_out) |
141 | 5x |
formatters::var_labels(adlb_out) <- adlb_var_labels |
142 | ||
143 | 5x |
adlb_out
|
144 |
}
|
1 |
#' Patient Counts with the Most Extreme Post-baseline Toxicity Grade per Direction of Abnormality
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Primary analysis variable `.var` indicates the toxicity grade (`factor`), and additional
|
|
6 |
#' analysis variables are `id` (`character` or `factor`), `param` (`factor`) and `grade_dir` (`factor`).
|
|
7 |
#' The pre-processing steps are crucial when using this function.
|
|
8 |
#' For a certain direction (e.g. high or low) this function counts
|
|
9 |
#' patients in the denominator as number of patients with at least one valid measurement during treatment,
|
|
10 |
#' and patients in the numerator as follows:
|
|
11 |
#' * `1` to `4`: Numerator is number of patients with worst grades 1-4 respectively;
|
|
12 |
#' * `Any`: Numerator is number of patients with at least one abnormality, which means grade is different from 0.
|
|
13 |
#'
|
|
14 |
#' @inheritParams argument_convention
|
|
15 |
#'
|
|
16 |
#' @details The pre-processing steps are crucial when using this function. From the standard lab grade variable
|
|
17 |
#' `ATOXGR`, derive the following two variables:
|
|
18 |
#' * A grade direction variable (e.g. `GRADE_DIR`) is required in order to obtain
|
|
19 |
#' the correct denominators when building the layout as it is used to define row splitting.
|
|
20 |
#' * A toxicity grade variable (e.g. `GRADE_ANL`) where all negative values from
|
|
21 |
#' `ATOXGR` are replaced by their absolute values.
|
|
22 |
#'
|
|
23 |
#' @note Prior to tabulation, `df` must be filtered to include only post-baseline records with worst grade flags.
|
|
24 |
#'
|
|
25 |
#' @name abnormal_by_worst_grade
|
|
26 |
NULL
|
|
27 | ||
28 |
#' @describeIn abnormal_by_worst_grade Statistics function which counts patients by worst grade.
|
|
29 |
#'
|
|
30 |
#' @return
|
|
31 |
#' * `s_count_abnormal_by_worst_grade()` returns the single statistic `count_fraction` with grades 1 to 4 and
|
|
32 |
#' "Any" results.
|
|
33 |
#'
|
|
34 |
#' @examples
|
|
35 |
#' library(dplyr)
|
|
36 |
#' library(forcats)
|
|
37 |
#' adlb <- tern_ex_adlb
|
|
38 |
#'
|
|
39 |
#' # Data is modified in order to have some parameters with grades only in one direction
|
|
40 |
#' # and simulate the real data.
|
|
41 |
#' adlb$ATOXGR[adlb$PARAMCD == "ALT" & adlb$ATOXGR %in% c("1", "2", "3", "4")] <- "-1"
|
|
42 |
#' adlb$ANRIND[adlb$PARAMCD == "ALT" & adlb$ANRIND == "HIGH"] <- "LOW"
|
|
43 |
#' adlb$WGRHIFL[adlb$PARAMCD == "ALT"] <- ""
|
|
44 |
#'
|
|
45 |
#' adlb$ATOXGR[adlb$PARAMCD == "IGA" & adlb$ATOXGR %in% c("-1", "-2", "-3", "-4")] <- "1"
|
|
46 |
#' adlb$ANRIND[adlb$PARAMCD == "IGA" & adlb$ANRIND == "LOW"] <- "HIGH"
|
|
47 |
#' adlb$WGRLOFL[adlb$PARAMCD == "IGA"] <- ""
|
|
48 |
#'
|
|
49 |
#' # Here starts the real pre-processing.
|
|
50 |
#' adlb_f <- adlb %>%
|
|
51 |
#' filter(!AVISIT %in% c("SCREENING", "BASELINE")) %>%
|
|
52 |
#' mutate(
|
|
53 |
#' GRADE_DIR = factor(
|
|
54 |
#' case_when(
|
|
55 |
#' ATOXGR %in% c("-1", "-2", "-3", "-4") ~ "LOW",
|
|
56 |
#' ATOXGR == "0" ~ "ZERO",
|
|
57 |
#' ATOXGR %in% c("1", "2", "3", "4") ~ "HIGH"
|
|
58 |
#' ),
|
|
59 |
#' levels = c("LOW", "ZERO", "HIGH")
|
|
60 |
#' ),
|
|
61 |
#' GRADE_ANL = fct_relevel(
|
|
62 |
#' fct_recode(ATOXGR, `1` = "-1", `2` = "-2", `3` = "-3", `4` = "-4"),
|
|
63 |
#' c("0", "1", "2", "3", "4")
|
|
64 |
#' )
|
|
65 |
#' ) %>%
|
|
66 |
#' filter(WGRLOFL == "Y" | WGRHIFL == "Y") %>%
|
|
67 |
#' droplevels()
|
|
68 |
#'
|
|
69 |
#' adlb_f_alt <- adlb_f %>%
|
|
70 |
#' filter(PARAMCD == "ALT") %>%
|
|
71 |
#' droplevels()
|
|
72 |
#' full_parent_df <- list(adlb_f_alt, "not_needed")
|
|
73 |
#' cur_col_subset <- list(rep(TRUE, nrow(adlb_f_alt)), "not_needed")
|
|
74 |
#'
|
|
75 |
#' # This mimics a split structure on PARAM and GRADE_DIR for a total column
|
|
76 |
#' spl_context <- data.frame(
|
|
77 |
#' split = c("PARAM", "GRADE_DIR"),
|
|
78 |
#' full_parent_df = I(full_parent_df),
|
|
79 |
#' cur_col_subset = I(cur_col_subset)
|
|
80 |
#' )
|
|
81 |
#'
|
|
82 |
#' @keywords internal
|
|
83 |
s_count_abnormal_by_worst_grade <- function(df, # nolint |
|
84 |
.var = "GRADE_ANL", |
|
85 |
.spl_context,
|
|
86 |
variables = list( |
|
87 |
id = "USUBJID", |
|
88 |
param = "PARAM", |
|
89 |
grade_dir = "GRADE_DIR" |
|
90 |
)) { |
|
91 | 1x |
checkmate::assert_string(.var) |
92 | 1x |
assert_valid_factor(df[[.var]]) |
93 | 1x |
assert_valid_factor(df[[variables$param]]) |
94 | 1x |
assert_valid_factor(df[[variables$grade_dir]]) |
95 | 1x |
assert_df_with_variables(df, c(a = .var, variables)) |
96 | 1x |
checkmate::assert_multi_class(df[[variables$id]], classes = c("factor", "character")) |
97 | ||
98 |
# To verify that the `split_rows_by` are performed with correct variables.
|
|
99 | 1x |
checkmate::assert_subset(c(variables[["param"]], variables[["grade_dir"]]), .spl_context$split) |
100 | 1x |
first_row <- .spl_context[.spl_context$split == variables[["param"]], ] |
101 | 1x |
x_lvls <- c(setdiff(levels(df[[.var]]), "0"), "Any") |
102 | 1x |
result <- split(numeric(0), factor(x_lvls)) |
103 | ||
104 | 1x |
subj <- first_row$full_parent_df[[1]][[variables[["id"]]]] |
105 | 1x |
subj_cur_col <- subj[first_row$cur_col_subset[[1]]] |
106 |
# Some subjects may have a record for high and low directions but
|
|
107 |
# should be counted only once.
|
|
108 | 1x |
denom <- length(unique(subj_cur_col)) |
109 | ||
110 | 1x |
for (lvl in x_lvls) { |
111 | 5x |
if (lvl != "Any") { |
112 | 4x |
df_lvl <- df[df[[.var]] == lvl, ] |
113 |
} else { |
|
114 | 1x |
df_lvl <- df[df[[.var]] != 0, ] |
115 |
}
|
|
116 | 5x |
num <- length(unique(df_lvl[["USUBJID"]])) |
117 | 5x |
fraction <- ifelse(denom == 0, 0, num / denom) |
118 | 5x |
result[[lvl]] <- formatters::with_label(c(count = num, fraction = fraction), lvl) |
119 |
}
|
|
120 | ||
121 | 1x |
result <- list(count_fraction = result) |
122 | 1x |
result
|
123 |
}
|
|
124 | ||
125 |
#' @describeIn abnormal_by_worst_grade Formatted analysis function which is used as `afun`
|
|
126 |
#' in `count_abnormal_by_worst_grade()`.
|
|
127 |
#'
|
|
128 |
#' @return
|
|
129 |
#' * `a_count_abnormal_by_worst_grade()` returns the corresponding list with formatted [rtables::CellValue()].
|
|
130 |
#'
|
|
131 |
#'
|
|
132 |
#' @keywords internal
|
|
133 |
a_count_abnormal_by_worst_grade <- make_afun( # nolint |
|
134 |
s_count_abnormal_by_worst_grade,
|
|
135 |
.formats = c(count_fraction = format_count_fraction) |
|
136 |
)
|
|
137 | ||
138 |
#' @describeIn abnormal_by_worst_grade Layout-creating function which can take statistics function arguments
|
|
139 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()].
|
|
140 |
#'
|
|
141 |
#' @return
|
|
142 |
#' * `count_abnormal_by_worst_grade()` returns a layout object suitable for passing to further layouting functions,
|
|
143 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing
|
|
144 |
#' the statistics from `s_count_abnormal_by_worst_grade()` to the table layout.
|
|
145 |
#'
|
|
146 |
#' @examples
|
|
147 |
#' # Map excludes records without abnormal grade since they should not be displayed
|
|
148 |
#' # in the table.
|
|
149 |
#' map <- unique(adlb_f[adlb_f$GRADE_DIR != "ZERO", c("PARAM", "GRADE_DIR", "GRADE_ANL")]) %>%
|
|
150 |
#' lapply(as.character) %>%
|
|
151 |
#' as.data.frame() %>%
|
|
152 |
#' arrange(PARAM, desc(GRADE_DIR), GRADE_ANL)
|
|
153 |
#'
|
|
154 |
#' basic_table() %>%
|
|
155 |
#' split_cols_by("ARMCD") %>%
|
|
156 |
#' split_rows_by("PARAM") %>%
|
|
157 |
#' split_rows_by("GRADE_DIR", split_fun = trim_levels_to_map(map)) %>%
|
|
158 |
#' count_abnormal_by_worst_grade(
|
|
159 |
#' var = "GRADE_ANL",
|
|
160 |
#' variables = list(id = "USUBJID", param = "PARAM", grade_dir = "GRADE_DIR")
|
|
161 |
#' ) %>%
|
|
162 |
#' build_table(df = adlb_f)
|
|
163 |
#'
|
|
164 |
#' @export
|
|
165 |
count_abnormal_by_worst_grade <- function(lyt, |
|
166 |
var,
|
|
167 |
...,
|
|
168 |
.stats = NULL, |
|
169 |
.formats = NULL, |
|
170 |
.labels = NULL, |
|
171 |
.indent_mods = NULL) { |
|
172 | 2x |
afun <- make_afun( |
173 | 2x |
a_count_abnormal_by_worst_grade,
|
174 | 2x |
.stats = .stats, |
175 | 2x |
.formats = .formats, |
176 | 2x |
.labels = .labels, |
177 | 2x |
.indent_mods = .indent_mods, |
178 | 2x |
.ungroup_stats = "count_fraction" |
179 |
)
|
|
180 | 2x |
analyze( |
181 | 2x |
lyt = lyt, |
182 | 2x |
vars = var, |
183 | 2x |
afun = afun, |
184 | 2x |
extra_args = list(...), |
185 | 2x |
show_labels = "hidden" |
186 |
)
|
|
187 |
}
|
1 |
#' Patient Counts with Abnormal Range Values by Baseline Status
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Primary analysis variable `.var` indicates the abnormal range result (`character` or `factor`), and additional
|
|
6 |
#' analysis variables are `id` (`character` or `factor`) and `baseline` (`character` or `factor`). For each
|
|
7 |
#' direction specified in `abnormal` (e.g. high or low) we condition on baseline range result and count
|
|
8 |
#' patients in the numerator and denominator as follows:
|
|
9 |
#' * `Not <Abnormal>`
|
|
10 |
#' * `denom`: the number of patients without abnormality at baseline (excluding those with missing baseline)
|
|
11 |
#' * `num`: the number of patients in `denom` who also have at least one abnormality post-baseline
|
|
12 |
#' * `<Abnormal>`
|
|
13 |
#' * `denom`: the number of patients with abnormality at baseline
|
|
14 |
#' * `num`: the number of patients in `denom` who also have at least one abnormality post-baseline
|
|
15 |
#' * `Total`
|
|
16 |
#' * `denom`: the number of patients with at least one valid measurement post-baseline
|
|
17 |
#' * `num`: the number of patients in `denom` who also have at least one abnormality post-baseline
|
|
18 |
#'
|
|
19 |
#' @inheritParams argument_convention
|
|
20 |
#' @param abnormal (`character`)\cr identifying the abnormal range level(s) in `.var`.
|
|
21 |
#'
|
|
22 |
#' @note
|
|
23 |
#' * `df` should be filtered to include only post-baseline records.
|
|
24 |
#' * If the baseline variable or analysis variable contains `NA`, it is expected that `NA` has been
|
|
25 |
#' conveyed to `na_level` appropriately beforehand with [df_explicit_na()] or [explicit_na()].
|
|
26 |
#'
|
|
27 |
#' @seealso Relevant description function [d_count_abnormal_by_baseline()].
|
|
28 |
#'
|
|
29 |
#' @name abnormal_by_baseline
|
|
30 |
NULL
|
|
31 | ||
32 |
#' Description Function for [s_count_abnormal_by_baseline()]
|
|
33 |
#'
|
|
34 |
#' @description `r lifecycle::badge("stable")`
|
|
35 |
#'
|
|
36 |
#' Description function that produces the labels for [s_count_abnormal_by_baseline()].
|
|
37 |
#'
|
|
38 |
#' @inheritParams abnormal_by_baseline
|
|
39 |
#'
|
|
40 |
#' @return Abnormal category labels for [s_count_abnormal_by_baseline()].
|
|
41 |
#'
|
|
42 |
#' @examples
|
|
43 |
#' d_count_abnormal_by_baseline("LOW")
|
|
44 |
#'
|
|
45 |
#' @export
|
|
46 |
d_count_abnormal_by_baseline <- function(abnormal) { |
|
47 | 7x |
not_abn_name <- paste("Not", tolower(abnormal)) |
48 | 7x |
abn_name <- paste0(toupper(substr(abnormal, 1, 1)), tolower(substring(abnormal, 2))) |
49 | 7x |
total_name <- "Total" |
50 | ||
51 | 7x |
list( |
52 | 7x |
not_abnormal = not_abn_name, |
53 | 7x |
abnormal = abn_name, |
54 | 7x |
total = total_name |
55 |
)
|
|
56 |
}
|
|
57 | ||
58 |
#' @describeIn abnormal_by_baseline Statistics function for a single `abnormal` level.
|
|
59 |
#'
|
|
60 |
#' @param na_level (`string`)\cr the explicit `na_level` argument you used in the pre-processing steps (maybe with
|
|
61 |
#' [df_explicit_na()]). The default is `"<Missing>"`.
|
|
62 |
#'
|
|
63 |
#' @return
|
|
64 |
#' * `s_count_abnormal_by_baseline()` returns statistic `fraction` which is a named list with 3 labeled elements:
|
|
65 |
#' `not_abnormal`, `abnormal`, and `total`. Each element contains a vector with `num` and `denom` patient counts.
|
|
66 |
#'
|
|
67 |
#'
|
|
68 |
#' @keywords internal
|
|
69 |
s_count_abnormal_by_baseline <- function(df, |
|
70 |
.var,
|
|
71 |
abnormal,
|
|
72 |
na_level = "<Missing>", |
|
73 |
variables = list(id = "USUBJID", baseline = "BNRIND")) { |
|
74 | 5x |
checkmate::assert_string(.var) |
75 | 5x |
checkmate::assert_string(abnormal) |
76 | 5x |
checkmate::assert_string(na_level) |
77 | 5x |
assert_df_with_variables(df, c(range = .var, variables)) |
78 | 5x |
checkmate::assert_subset(names(variables), c("id", "baseline")) |
79 | 5x |
checkmate::assert_multi_class(df[[variables$id]], classes = c("factor", "character")) |
80 | 5x |
checkmate::assert_multi_class(df[[variables$baseline]], classes = c("factor", "character")) |
81 | 5x |
checkmate::assert_multi_class(df[[.var]], classes = c("factor", "character")) |
82 | ||
83 |
# If input is passed as character, changed to factor
|
|
84 | 5x |
df[[.var]] <- as_factor_keep_attributes(df[[.var]], na_level = na_level) |
85 | 5x |
df[[variables$baseline]] <- as_factor_keep_attributes(df[[variables$baseline]], na_level = na_level) |
86 | ||
87 | 5x |
assert_valid_factor(df[[.var]], any.missing = FALSE) |
88 | 4x |
assert_valid_factor(df[[variables$baseline]], any.missing = FALSE) |
89 | ||
90 |
# Keep only records with valid analysis value.
|
|
91 | 3x |
df <- df[df[[.var]] != na_level, ] |
92 | ||
93 | 3x |
anl <- data.frame( |
94 | 3x |
id = df[[variables$id]], |
95 | 3x |
var = df[[.var]], |
96 | 3x |
baseline = df[[variables$baseline]], |
97 | 3x |
stringsAsFactors = FALSE |
98 |
)
|
|
99 | ||
100 |
# Total:
|
|
101 |
# - Patients in denominator: have at least one valid measurement post-baseline.
|
|
102 |
# - Patients in numerator: have at least one abnormality.
|
|
103 | 3x |
total_denom <- length(unique(anl$id)) |
104 | 3x |
total_num <- length(unique(anl$id[anl$var == abnormal])) |
105 | ||
106 |
# Baseline NA records are counted only in total rows.
|
|
107 | 3x |
anl <- anl[anl$baseline != na_level, ] |
108 | ||
109 |
# Abnormal:
|
|
110 |
# - Patients in denominator: have abnormality at baseline.
|
|
111 |
# - Patients in numerator: have abnormality at baseline AND
|
|
112 |
# have at least one abnormality post-baseline.
|
|
113 | 3x |
abn_denom <- length(unique(anl$id[anl$baseline == abnormal])) |
114 | 3x |
abn_num <- length(unique(anl$id[anl$baseline == abnormal & anl$var == abnormal])) |
115 | ||
116 |
# Not abnormal:
|
|
117 |
# - Patients in denominator: do not have abnormality at baseline.
|
|
118 |
# - Patients in numerator: do not have abnormality at baseline AND
|
|
119 |
# have at least one abnormality post-baseline.
|
|
120 | 3x |
not_abn_denom <- length(unique(anl$id[anl$baseline != abnormal])) |
121 | 3x |
not_abn_num <- length(unique(anl$id[anl$baseline != abnormal & anl$var == abnormal])) |
122 | ||
123 | 3x |
labels <- d_count_abnormal_by_baseline(abnormal) |
124 | 3x |
list(fraction = list( |
125 | 3x |
not_abnormal = formatters::with_label(c(num = not_abn_num, denom = not_abn_denom), labels$not_abnormal), |
126 | 3x |
abnormal = formatters::with_label(c(num = abn_num, denom = abn_denom), labels$abnormal), |
127 | 3x |
total = formatters::with_label(c(num = total_num, denom = total_denom), labels$total) |
128 |
)) |
|
129 |
}
|
|
130 | ||
131 |
#' @describeIn abnormal_by_baseline Formatted analysis function which is used as `afun`
|
|
132 |
#' in `count_abnormal_by_baseline()`.
|
|
133 |
#'
|
|
134 |
#' @return
|
|
135 |
#' * `a_count_abnormal_by_baseline()` returns the corresponding list with formatted [rtables::CellValue()].
|
|
136 |
#'
|
|
137 |
#'
|
|
138 |
#' @keywords internal
|
|
139 |
a_count_abnormal_by_baseline <- make_afun( |
|
140 |
s_count_abnormal_by_baseline,
|
|
141 |
.formats = c(fraction = format_fraction) |
|
142 |
)
|
|
143 | ||
144 |
#' @describeIn abnormal_by_baseline Layout-creating function which can take statistics function arguments
|
|
145 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()].
|
|
146 |
#'
|
|
147 |
#' @return
|
|
148 |
#' * `count_abnormal_by_baseline()` returns a layout object suitable for passing to further layouting functions,
|
|
149 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing
|
|
150 |
#' the statistics from `s_count_abnormal_by_baseline()` to the table layout.
|
|
151 |
#'
|
|
152 |
#' @examples
|
|
153 |
#' df <- data.frame(
|
|
154 |
#' USUBJID = as.character(c(1:6)),
|
|
155 |
#' ANRIND = factor(c(rep("LOW", 4), "NORMAL", "HIGH")),
|
|
156 |
#' BNRIND = factor(c("LOW", "NORMAL", "HIGH", NA, "LOW", "NORMAL"))
|
|
157 |
#' )
|
|
158 |
#' df <- df_explicit_na(df)
|
|
159 |
#'
|
|
160 |
#' # Layout creating function.
|
|
161 |
#' basic_table() %>%
|
|
162 |
#' count_abnormal_by_baseline(var = "ANRIND", abnormal = c(High = "HIGH")) %>%
|
|
163 |
#' build_table(df)
|
|
164 |
#'
|
|
165 |
#' # Passing of statistics function and formatting arguments.
|
|
166 |
#' df2 <- data.frame(
|
|
167 |
#' ID = as.character(c(1, 2, 3, 4)),
|
|
168 |
#' RANGE = factor(c("NORMAL", "LOW", "HIGH", "HIGH")),
|
|
169 |
#' BLRANGE = factor(c("LOW", "HIGH", "HIGH", "NORMAL"))
|
|
170 |
#' )
|
|
171 |
#'
|
|
172 |
#' basic_table() %>%
|
|
173 |
#' count_abnormal_by_baseline(
|
|
174 |
#' var = "RANGE",
|
|
175 |
#' abnormal = c(Low = "LOW"),
|
|
176 |
#' variables = list(id = "ID", baseline = "BLRANGE"),
|
|
177 |
#' .formats = c(fraction = "xx / xx"),
|
|
178 |
#' .indent_mods = c(fraction = 2L)
|
|
179 |
#' ) %>%
|
|
180 |
#' build_table(df2)
|
|
181 |
#'
|
|
182 |
#' @export
|
|
183 |
count_abnormal_by_baseline <- function(lyt, |
|
184 |
var,
|
|
185 |
abnormal,
|
|
186 |
...,
|
|
187 |
table_names = abnormal, |
|
188 |
.stats = NULL, |
|
189 |
.formats = NULL, |
|
190 |
.labels = NULL, |
|
191 |
.indent_mods = NULL) { |
|
192 | 2x |
checkmate::assert_character(abnormal, len = length(table_names), names = "named") |
193 | 2x |
checkmate::assert_string(var) |
194 | 2x |
afun <- make_afun( |
195 | 2x |
a_count_abnormal_by_baseline,
|
196 | 2x |
.stats = .stats, |
197 | 2x |
.formats = .formats, |
198 | 2x |
.labels = .labels, |
199 | 2x |
.indent_mods = .indent_mods, |
200 | 2x |
.ungroup_stats = "fraction" |
201 |
)
|
|
202 | 2x |
for (i in seq_along(abnormal)) { |
203 | 4x |
abn <- abnormal[i] |
204 | 4x |
lyt <- analyze( |
205 | 4x |
lyt = lyt, |
206 | 4x |
vars = var, |
207 | 4x |
var_labels = names(abn), |
208 | 4x |
afun = afun, |
209 | 4x |
table_names = table_names[i], |
210 | 4x |
extra_args = c(list(abnormal = abn), list(...)), |
211 | 4x |
show_labels = "visible" |
212 |
)
|
|
213 |
}
|
|
214 | 2x |
lyt
|
215 |
}
|
1 |
#' Incidence Rate
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Estimate the event rate adjusted for person-years at risk, otherwise known
|
|
6 |
#' as incidence rate. Primary analysis variable is the person-years at risk.
|
|
7 |
#'
|
|
8 |
#' @inheritParams argument_convention
|
|
9 |
#' @param control (`list`)\cr parameters for estimation details, specified by using
|
|
10 |
#' the helper function [control_incidence_rate()]. Possible parameter options are:
|
|
11 |
#' * `conf_level` (`proportion`)\cr confidence level for the estimated incidence rate.
|
|
12 |
#' * `conf_type` (`string`)\cr `normal` (default), `normal_log`, `exact`, or `byar`
|
|
13 |
#' for confidence interval type.
|
|
14 |
#' * `input_time_unit` (`string`)\cr `day`, `week`, `month`, or `year` (default)
|
|
15 |
#' indicating time unit for data input.
|
|
16 |
#' * `num_pt_year` (`numeric`)\cr time unit for desired output (in person-years).
|
|
17 |
#' @param person_years (`numeric`)\cr total person-years at risk.
|
|
18 |
#' @param alpha (`numeric`)\cr two-sided alpha-level for confidence interval.
|
|
19 |
#' @param n_events (`integer`)\cr number of events observed.
|
|
20 |
#'
|
|
21 |
#' @seealso [control_incidence_rate()] and helper functions [h_incidence_rate].
|
|
22 |
#'
|
|
23 |
#' @name incidence_rate
|
|
24 |
NULL
|
|
25 | ||
26 |
#' @describeIn incidence_rate Statistics function which estimates the incidence rate and the
|
|
27 |
#' associated confidence interval.
|
|
28 |
#'
|
|
29 |
#' @return
|
|
30 |
#' * `s_incidence_rate()` returns the following statistics:
|
|
31 |
#' - `person_years`: Total person-years at risk.
|
|
32 |
#' - `n_events`: Total number of events observed.
|
|
33 |
#' - `rate`: Estimated incidence rate.
|
|
34 |
#' - `rate_ci`: Confidence interval for the incidence rate.
|
|
35 |
#'
|
|
36 |
#' @examples
|
|
37 |
#' library(dplyr)
|
|
38 |
#'
|
|
39 |
#' df <- data.frame(
|
|
40 |
#' USUBJID = as.character(seq(6)),
|
|
41 |
#' CNSR = c(0, 1, 1, 0, 0, 0),
|
|
42 |
#' AVAL = c(10.1, 20.4, 15.3, 20.8, 18.7, 23.4),
|
|
43 |
#' ARM = factor(c("A", "A", "A", "B", "B", "B"))
|
|
44 |
#' ) %>%
|
|
45 |
#' mutate(is_event = CNSR == 0) %>%
|
|
46 |
#' mutate(n_events = as.integer(is_event))
|
|
47 |
#'
|
|
48 |
#' @keywords internal
|
|
49 |
s_incidence_rate <- function(df, |
|
50 |
.var,
|
|
51 |
n_events,
|
|
52 |
is_event,
|
|
53 |
control = control_incidence_rate()) { |
|
54 | 1x |
if (!missing(is_event)) { |
55 | ! |
warning("argument is_event will be deprecated. Please use n_events.") |
56 | ||
57 | ! |
if (missing(n_events)) { |
58 | ! |
assert_df_with_variables(df, list(tte = .var, is_event = is_event)) |
59 | ! |
checkmate::assert_string(.var) |
60 | ! |
checkmate::assert_logical(df[[is_event]], any.missing = FALSE) |
61 | ! |
checkmate::assert_numeric(df[[.var]], any.missing = FALSE) |
62 | ! |
n_events <- is_event |
63 |
}
|
|
64 |
} else { |
|
65 | 1x |
assert_df_with_variables(df, list(tte = .var, n_events = n_events)) |
66 | 1x |
checkmate::assert_string(.var) |
67 | 1x |
checkmate::assert_numeric(df[[.var]], any.missing = FALSE) |
68 | 1x |
checkmate::assert_integer(df[[n_events]], any.missing = FALSE) |
69 |
}
|
|
70 | ||
71 | 1x |
input_time_unit <- control$input_time_unit |
72 | 1x |
num_pt_year <- control$num_pt_year |
73 | 1x |
conf_level <- control$conf_level |
74 | 1x |
person_years <- sum(df[[.var]], na.rm = TRUE) * ( |
75 | 1x |
1 * (input_time_unit == "year") + |
76 | 1x |
1 / 12 * (input_time_unit == "month") + |
77 | 1x |
1 / 52.14 * (input_time_unit == "week") + |
78 | 1x |
1 / 365.24 * (input_time_unit == "day") |
79 |
)
|
|
80 | 1x |
n_events <- sum(df[[n_events]], na.rm = TRUE) |
81 | ||
82 | 1x |
result <- h_incidence_rate( |
83 | 1x |
person_years,
|
84 | 1x |
n_events,
|
85 | 1x |
control
|
86 |
)
|
|
87 | 1x |
list( |
88 | 1x |
person_years = formatters::with_label(person_years, "Total patient-years at risk"), |
89 | 1x |
n_events = formatters::with_label(n_events, "Number of adverse events observed"), |
90 | 1x |
rate = formatters::with_label(result$rate, paste("AE rate per", num_pt_year, "patient-years")), |
91 | 1x |
rate_ci = formatters::with_label(result$rate_ci, f_conf_level(conf_level)) |
92 |
)
|
|
93 |
}
|
|
94 | ||
95 |
#' @describeIn incidence_rate Formatted analysis function which is used as `afun`
|
|
96 |
#' in `estimate_incidence_rate()`.
|
|
97 |
#'
|
|
98 |
#' @return
|
|
99 |
#' * `a_incidence_rate()` returns the corresponding list with formatted [rtables::CellValue()].
|
|
100 |
#'
|
|
101 |
#'
|
|
102 |
#' @keywords internal
|
|
103 |
a_incidence_rate <- make_afun( |
|
104 |
s_incidence_rate,
|
|
105 |
.formats = c( |
|
106 |
"person_years" = "xx.x", |
|
107 |
"n_events" = "xx", |
|
108 |
"rate" = "xx.xx", |
|
109 |
"rate_ci" = "(xx.xx, xx.xx)" |
|
110 |
)
|
|
111 |
)
|
|
112 | ||
113 |
#' @describeIn incidence_rate Layout-creating function which can take statistics function arguments
|
|
114 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()].
|
|
115 |
#'
|
|
116 |
#' @return
|
|
117 |
#' * `estimate_incidence_rate()` returns a layout object suitable for passing to further layouting functions,
|
|
118 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing
|
|
119 |
#' the statistics from `s_incidence_rate()` to the table layout.
|
|
120 |
#'
|
|
121 |
#' @examples
|
|
122 |
#' basic_table() %>%
|
|
123 |
#' split_cols_by("ARM") %>%
|
|
124 |
#' add_colcounts() %>%
|
|
125 |
#' estimate_incidence_rate(
|
|
126 |
#' vars = "AVAL",
|
|
127 |
#' n_events = "n_events",
|
|
128 |
#' control = control_incidence_rate(
|
|
129 |
#' input_time_unit = "month",
|
|
130 |
#' num_pt_year = 100
|
|
131 |
#' )
|
|
132 |
#' ) %>%
|
|
133 |
#' build_table(df)
|
|
134 |
#'
|
|
135 |
#' @export
|
|
136 |
estimate_incidence_rate <- function(lyt, |
|
137 |
vars,
|
|
138 |
...,
|
|
139 |
show_labels = "hidden", |
|
140 |
table_names = vars, |
|
141 |
.stats = NULL, |
|
142 |
.formats = NULL, |
|
143 |
.labels = NULL, |
|
144 |
.indent_mods = NULL) { |
|
145 | 1x |
afun <- make_afun( |
146 | 1x |
a_incidence_rate,
|
147 | 1x |
.stats = .stats, |
148 | 1x |
.formats = .formats, |
149 | 1x |
.labels = .labels, |
150 | 1x |
.indent_mods = .indent_mods |
151 |
)
|
|
152 | ||
153 | 1x |
analyze( |
154 | 1x |
lyt,
|
155 | 1x |
vars,
|
156 | 1x |
show_labels = show_labels, |
157 | 1x |
table_names = table_names, |
158 | 1x |
afun = afun, |
159 | 1x |
extra_args = list(...) |
160 |
)
|
|
161 |
}
|
|
162 | ||
163 |
#' Helper Functions for Incidence Rate
|
|
164 |
#'
|
|
165 |
#' @description `r lifecycle::badge("stable")`
|
|
166 |
#'
|
|
167 |
#' @param control (`list`)\cr parameters for estimation details, specified by using
|
|
168 |
#' the helper function [control_incidence_rate()]. Possible parameter options are:
|
|
169 |
#' * `conf_level`: (`proportion`)\cr confidence level for the estimated incidence rate.
|
|
170 |
#' * `conf_type`: (`string`)\cr `normal` (default), `normal_log`, `exact`, or `byar`
|
|
171 |
#' for confidence interval type.
|
|
172 |
#' * `input_time_unit`: (`string`)\cr `day`, `week`, `month`, or `year` (default)
|
|
173 |
#' indicating time unit for data input.
|
|
174 |
#' * `num_pt_year`: (`numeric`)\cr time unit for desired output (in person-years).
|
|
175 |
#' @param person_years (`numeric`)\cr total person-years at risk.
|
|
176 |
#' @param alpha (`numeric`)\cr two-sided alpha-level for confidence interval.
|
|
177 |
#' @param n_events (`integer`)\cr number of events observed.
|
|
178 |
#'
|
|
179 |
#' @return Estimated incidence rate `rate` and associated confidence interval `rate_ci`.
|
|
180 |
#'
|
|
181 |
#' @seealso [incidence_rate]
|
|
182 |
#'
|
|
183 |
#' @name h_incidence_rate
|
|
184 |
NULL
|
|
185 | ||
186 |
#' @describeIn h_incidence_rate Helper function to estimate the incidence rate and
|
|
187 |
#' associated confidence interval based on the normal approximation for the
|
|
188 |
#' incidence rate. Unit is one person-year.
|
|
189 |
#'
|
|
190 |
#' @examples
|
|
191 |
#' h_incidence_rate_normal(200, 2)
|
|
192 |
#'
|
|
193 |
#' @export
|
|
194 |
h_incidence_rate_normal <- function(person_years, |
|
195 |
n_events,
|
|
196 |
alpha = 0.05) { |
|
197 | 1x |
checkmate::assert_number(person_years) |
198 | 1x |
checkmate::assert_number(n_events) |
199 | 1x |
assert_proportion_value(alpha) |
200 | ||
201 | 1x |
est <- n_events / person_years |
202 | 1x |
se <- sqrt(est / person_years) |
203 | 1x |
ci <- est + c(-1, 1) * stats::qnorm(1 - alpha / 2) * se |
204 | ||
205 | 1x |
list(rate = est, rate_ci = ci) |
206 |
}
|
|
207 | ||
208 |
#' @describeIn h_incidence_rate Helper function to estimate the incidence rate and
|
|
209 |
#' associated confidence interval based on the normal approximation for the
|
|
210 |
#' logarithm of the incidence rate. Unit is one person-year.
|
|
211 |
#'
|
|
212 |
#' @examples
|
|
213 |
#' h_incidence_rate_normal_log(200, 2)
|
|
214 |
#'
|
|
215 |
#' @export
|
|
216 |
h_incidence_rate_normal_log <- function(person_years, |
|
217 |
n_events,
|
|
218 |
alpha = 0.05) { |
|
219 | 5x |
checkmate::assert_number(person_years) |
220 | 5x |
checkmate::assert_number(n_events) |
221 | 5x |
assert_proportion_value(alpha) |
222 | ||
223 | 5x |
rate_est <- n_events / person_years |
224 | 5x |
rate_se <- sqrt(rate_est / person_years) |
225 | 5x |
lrate_est <- log(rate_est) |
226 | 5x |
lrate_se <- rate_se / rate_est |
227 | 5x |
ci <- exp(lrate_est + c(-1, 1) * stats::qnorm(1 - alpha / 2) * lrate_se) |
228 | ||
229 | 5x |
list(rate = rate_est, rate_ci = ci) |
230 |
}
|
|
231 | ||
232 |
#' @describeIn h_incidence_rate Helper function to estimate the incidence rate and
|
|
233 |
#' associated exact confidence interval. Unit is one person-year.
|
|
234 |
#'
|
|
235 |
#' @examples
|
|
236 |
#' h_incidence_rate_exact(200, 2)
|
|
237 |
#'
|
|
238 |
#' @export
|
|
239 |
h_incidence_rate_exact <- function(person_years, |
|
240 |
n_events,
|
|
241 |
alpha = 0.05) { |
|
242 | 1x |
checkmate::assert_number(person_years) |
243 | 1x |
checkmate::assert_number(n_events) |
244 | 1x |
assert_proportion_value(alpha) |
245 | ||
246 | 1x |
est <- n_events / person_years |
247 | 1x |
lcl <- stats::qchisq(p = (alpha) / 2, df = 2 * n_events) / (2 * person_years) |
248 | 1x |
ucl <- stats::qchisq(p = 1 - (alpha) / 2, df = 2 * n_events + 2) / (2 * person_years) |
249 | ||
250 | 1x |
list(rate = est, rate_ci = c(lcl, ucl)) |
251 |
}
|
|
252 | ||
253 |
#' @describeIn h_incidence_rate Helper function to estimate the incidence rate and
|
|
254 |
#' associated `Byar`'s confidence interval. Unit is one person-year.
|
|
255 |
#'
|
|
256 |
#' @examples
|
|
257 |
#' h_incidence_rate_byar(200, 2)
|
|
258 |
#'
|
|
259 |
#' @export
|
|
260 |
h_incidence_rate_byar <- function(person_years, |
|
261 |
n_events,
|
|
262 |
alpha = 0.05) { |
|
263 | 1x |
checkmate::assert_number(person_years) |
264 | 1x |
checkmate::assert_number(n_events) |
265 | 1x |
assert_proportion_value(alpha) |
266 | ||
267 | 1x |
est <- n_events / person_years |
268 | 1x |
seg_1 <- n_events + 0.5 |
269 | 1x |
seg_2 <- 1 - 1 / (9 * (n_events + 0.5)) |
270 | 1x |
seg_3 <- stats::qnorm(1 - alpha / 2) * sqrt(1 / (n_events + 0.5)) / 3 |
271 | 1x |
lcl <- seg_1 * ((seg_2 - seg_3)^3) / person_years |
272 | 1x |
ucl <- seg_1 * ((seg_2 + seg_3) ^ 3) / person_years # styler: off |
273 | ||
274 | 1x |
list(rate = est, rate_ci = c(lcl, ucl)) |
275 |
}
|
|
276 | ||
277 |
#' @describeIn h_incidence_rate Helper function to estimate the incidence rate and
|
|
278 |
#' associated confidence interval.
|
|
279 |
#'
|
|
280 |
#'
|
|
281 |
#' @keywords internal
|
|
282 |
h_incidence_rate <- function(person_years, |
|
283 |
n_events,
|
|
284 |
control = control_incidence_rate()) { |
|
285 | 4x |
alpha <- 1 - control$conf_level |
286 | 4x |
est <- switch(control$conf_type, |
287 | 4x |
normal = h_incidence_rate_normal(person_years, n_events, alpha), |
288 | 4x |
normal_log = h_incidence_rate_normal_log(person_years, n_events, alpha), |
289 | 4x |
exact = h_incidence_rate_exact(person_years, n_events, alpha), |
290 | 4x |
byar = h_incidence_rate_byar(person_years, n_events, alpha) |
291 |
)
|
|
292 | ||
293 | 4x |
num_pt_year <- control$num_pt_year |
294 | 4x |
list( |
295 | 4x |
rate = est$rate * num_pt_year, |
296 | 4x |
rate_ci = est$rate_ci * num_pt_year |
297 |
)
|
|
298 |
}
|
1 |
#' Encode Categorical Missing Values in a Data Frame
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' This is a helper function to encode missing entries across groups of categorical
|
|
6 |
#' variables in a data frame.
|
|
7 |
#'
|
|
8 |
#' @details Missing entries are those with `NA` or empty strings and will
|
|
9 |
#' be replaced with a specified value. If factor variables include missing
|
|
10 |
#' values, the missing value will be inserted as the last level.
|
|
11 |
#' Similarly, in case character or logical variables should be converted to factors
|
|
12 |
#' with the `char_as_factor` or `logical_as_factor` options, the missing values will
|
|
13 |
#' be set as the last level.
|
|
14 |
#'
|
|
15 |
#' @param data (`data.frame`)\cr data set.
|
|
16 |
#' @param omit_columns (`character`)\cr names of variables from `data` that should
|
|
17 |
#' not be modified by this function.
|
|
18 |
#' @param char_as_factor (`flag`)\cr whether to convert character variables
|
|
19 |
#' in `data` to factors.
|
|
20 |
#' @param logical_as_factor (`flag`)\cr whether to convert logical variables
|
|
21 |
#' in `data` to factors.
|
|
22 |
#' @param na_level (`string`)\cr used to replace all `NA` or empty
|
|
23 |
#' values inside non-`omit_columns` columns.
|
|
24 |
#'
|
|
25 |
#' @return A `data.frame` with the chosen modifications applied.
|
|
26 |
#'
|
|
27 |
#' @seealso [sas_na()] and [explicit_na()] for other missing data helper functions.
|
|
28 |
#'
|
|
29 |
#' @examples
|
|
30 |
#' my_data <- data.frame(
|
|
31 |
#' u = c(TRUE, FALSE, NA, TRUE),
|
|
32 |
#' v = factor(c("A", NA, NA, NA), levels = c("Z", "A")),
|
|
33 |
#' w = c("A", "B", NA, "C"),
|
|
34 |
#' x = c("D", "E", "F", NA),
|
|
35 |
#' y = c("G", "H", "I", ""),
|
|
36 |
#' z = c(1, 2, 3, 4),
|
|
37 |
#' stringsAsFactors = FALSE
|
|
38 |
#' )
|
|
39 |
#'
|
|
40 |
#' # Example 1
|
|
41 |
#' # Encode missing values in all character or factor columns.
|
|
42 |
#' df_explicit_na(my_data)
|
|
43 |
#' # Also convert logical columns to factor columns.
|
|
44 |
#' df_explicit_na(my_data, logical_as_factor = TRUE)
|
|
45 |
#' # Encode missing values in a subset of columns.
|
|
46 |
#' df_explicit_na(my_data, omit_columns = c("x", "y"))
|
|
47 |
#'
|
|
48 |
#' # Example 2
|
|
49 |
#' # Here we purposefully convert all `M` values to `NA` in the `SEX` variable.
|
|
50 |
#' # After running `df_explicit_na` the `NA` values are encoded as `<Missing>` but they are not
|
|
51 |
#' # included when generating `rtables`.
|
|
52 |
#' adsl <- tern_ex_adsl
|
|
53 |
#' adsl$SEX[adsl$SEX == "M"] <- NA
|
|
54 |
#' adsl <- df_explicit_na(adsl)
|
|
55 |
#'
|
|
56 |
#' # If you want the `Na` values to be displayed in the table use the `na_level` argument.
|
|
57 |
#' adsl <- tern_ex_adsl
|
|
58 |
#' adsl$SEX[adsl$SEX == "M"] <- NA
|
|
59 |
#' adsl <- df_explicit_na(adsl, na_level = "Missing Values")
|
|
60 |
#'
|
|
61 |
#' # Example 3
|
|
62 |
#' # Numeric variables that have missing values are not altered. This means that any `NA` value in
|
|
63 |
#' # a numeric variable will not be included in the summary statistics, nor will they be included
|
|
64 |
#' # in the denominator value for calculating the percent values.
|
|
65 |
#' adsl <- tern_ex_adsl
|
|
66 |
#' adsl$AGE[adsl$AGE < 30] <- NA
|
|
67 |
#' adsl <- df_explicit_na(adsl)
|
|
68 |
#'
|
|
69 |
#' @export
|
|
70 |
df_explicit_na <- function(data, |
|
71 |
omit_columns = NULL, |
|
72 |
char_as_factor = TRUE, |
|
73 |
logical_as_factor = FALSE, |
|
74 |
na_level = "<Missing>") { |
|
75 | 27x |
checkmate::assert_character(omit_columns, null.ok = TRUE, min.len = 1, any.missing = FALSE) |
76 | 26x |
checkmate::assert_data_frame(data) |
77 | 25x |
checkmate::assert_flag(char_as_factor) |
78 | 24x |
checkmate::assert_flag(logical_as_factor) |
79 | 24x |
checkmate::assert_string(na_level) |
80 | ||
81 | 22x |
target_vars <- if (is.null(omit_columns)) { |
82 | 20x |
names(data) |
83 |
} else { |
|
84 | 2x |
setdiff(names(data), omit_columns) # May have duplicates. |
85 |
}
|
|
86 | 22x |
if (length(target_vars) == 0) { |
87 | 1x |
return(data) |
88 |
}
|
|
89 | ||
90 | 21x |
l_target_vars <- split(target_vars, target_vars) |
91 | ||
92 |
# Makes sure target_vars exist in data and names are not duplicated.
|
|
93 | 21x |
assert_df_with_variables(data, l_target_vars) |
94 | ||
95 | 21x |
for (x in target_vars) { |
96 | 514x |
xi <- data[[x]] |
97 | 514x |
xi_label <- obj_label(xi) |
98 | ||
99 |
# Determine whether to convert character or logical input.
|
|
100 | 514x |
do_char_conversion <- is.character(xi) && char_as_factor |
101 | 514x |
do_logical_conversion <- is.logical(xi) && logical_as_factor |
102 | ||
103 |
# Pre-convert logical to character to deal correctly with replacing NA
|
|
104 |
# values below.
|
|
105 | 514x |
if (do_logical_conversion) { |
106 | 2x |
xi <- as.character(xi) |
107 |
}
|
|
108 | ||
109 | 514x |
if (is.factor(xi) || is.character(xi)) { |
110 |
# Handle empty strings and NA values.
|
|
111 | 387x |
xi <- explicit_na(sas_na(xi), label = na_level) |
112 | ||
113 |
# Convert to factors if requested for the original type,
|
|
114 |
# set na_level as the last value.
|
|
115 | 387x |
if (do_char_conversion || do_logical_conversion) { |
116 | 81x |
levels_xi <- setdiff(sort(unique(xi)), na_level) |
117 | 81x |
if (na_level %in% unique(xi)) { |
118 | 21x |
levels_xi <- c(levels_xi, na_level) |
119 |
}
|
|
120 | ||
121 | 81x |
xi <- factor(xi, levels = levels_xi) |
122 |
}
|
|
123 | ||
124 | 387x |
data[, x] <- formatters::with_label(xi, label = xi_label) |
125 |
}
|
|
126 |
}
|
|
127 | 21x |
return(data) |
128 |
}
|
1 |
#' Proportion Difference
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' @inheritParams argument_convention
|
|
6 |
#'
|
|
7 |
#' @seealso [d_proportion_diff()]
|
|
8 |
#'
|
|
9 |
#' @name prop_diff
|
|
10 |
NULL
|
|
11 | ||
12 |
#' @describeIn prop_diff Statistics function estimating the difference
|
|
13 |
#' in terms of responder proportion.
|
|
14 |
#'
|
|
15 |
#' @inheritParams prop_diff_strat_nc
|
|
16 |
#' @param method (`string`)\cr the method used for the confidence interval estimation.
|
|
17 |
#'
|
|
18 |
#' @return
|
|
19 |
#' * `s_proportion_diff()` returns a named list of elements `diff` and `diff_ci`.
|
|
20 |
#'
|
|
21 |
#' @note When performing an unstratified analysis, methods `"cmh"`, `"strat_newcombe"`, and `"strat_newcombecc"` are
|
|
22 |
#' not permitted.
|
|
23 |
#'
|
|
24 |
#' @examples
|
|
25 |
#' # Summary
|
|
26 |
#'
|
|
27 |
#' ## "Mid" case: 4/4 respond in group A, 1/2 respond in group B.
|
|
28 |
#' nex <- 100 # Number of example rows
|
|
29 |
#' dta <- data.frame(
|
|
30 |
#' "rsp" = sample(c(TRUE, FALSE), nex, TRUE),
|
|
31 |
#' "grp" = sample(c("A", "B"), nex, TRUE),
|
|
32 |
#' "f1" = sample(c("a1", "a2"), nex, TRUE),
|
|
33 |
#' "f2" = sample(c("x", "y", "z"), nex, TRUE),
|
|
34 |
#' stringsAsFactors = TRUE
|
|
35 |
#' )
|
|
36 |
#'
|
|
37 |
#' s_proportion_diff(
|
|
38 |
#' df = subset(dta, grp == "A"),
|
|
39 |
#' .var = "rsp",
|
|
40 |
#' .ref_group = subset(dta, grp == "B"),
|
|
41 |
#' .in_ref_col = FALSE,
|
|
42 |
#' conf_level = 0.90,
|
|
43 |
#' method = "ha"
|
|
44 |
#' )
|
|
45 |
#'
|
|
46 |
#' # CMH example with strata
|
|
47 |
#' s_proportion_diff(
|
|
48 |
#' df = subset(dta, grp == "A"),
|
|
49 |
#' .var = "rsp",
|
|
50 |
#' .ref_group = subset(dta, grp == "B"),
|
|
51 |
#' .in_ref_col = FALSE,
|
|
52 |
#' variables = list(strata = c("f1", "f2")),
|
|
53 |
#' conf_level = 0.90,
|
|
54 |
#' method = "cmh"
|
|
55 |
#' )
|
|
56 |
#'
|
|
57 |
#' @export
|
|
58 |
s_proportion_diff <- function(df, |
|
59 |
.var,
|
|
60 |
.ref_group,
|
|
61 |
.in_ref_col,
|
|
62 |
variables = list(strata = NULL), |
|
63 |
conf_level = 0.95, |
|
64 |
method = c( |
|
65 |
"waldcc", "wald", "cmh", |
|
66 |
"ha", "newcombe", "newcombecc", |
|
67 |
"strat_newcombe", "strat_newcombecc" |
|
68 |
),
|
|
69 |
weights_method = "cmh") { |
|
70 | 2x |
method <- match.arg(method) |
71 | 2x |
if (is.null(variables$strata) && checkmate::test_subset(method, c("cmh", "strat_newcombe", "strat_newcombecc"))) { |
72 | ! |
stop(paste( |
73 | ! |
"When performing an unstratified analysis, methods 'cmh', 'strat_newcombe', and 'strat_newcombecc' are not",
|
74 | ! |
"permitted. Please choose a different method."
|
75 |
)) |
|
76 |
}
|
|
77 | 2x |
y <- list(diff = "", diff_ci = "") |
78 | ||
79 | 2x |
if (!.in_ref_col) { |
80 | 2x |
rsp <- c(.ref_group[[.var]], df[[.var]]) |
81 | 2x |
grp <- factor( |
82 | 2x |
rep( |
83 | 2x |
c("ref", "Not-ref"), |
84 | 2x |
c(nrow(.ref_group), nrow(df)) |
85 |
),
|
|
86 | 2x |
levels = c("ref", "Not-ref") |
87 |
)
|
|
88 | ||
89 | 2x |
if (!is.null(variables$strata)) { |
90 | 1x |
strata_colnames <- variables$strata |
91 | 1x |
checkmate::assert_character(strata_colnames, null.ok = FALSE) |
92 | 1x |
strata_vars <- stats::setNames(as.list(strata_colnames), strata_colnames) |
93 | ||
94 | 1x |
assert_df_with_variables(df, strata_vars) |
95 | 1x |
assert_df_with_variables(.ref_group, strata_vars) |
96 | ||
97 |
# Merging interaction strata for reference group rows data and remaining
|
|
98 | 1x |
strata <- c( |
99 | 1x |
interaction(.ref_group[strata_colnames]), |
100 | 1x |
interaction(df[strata_colnames]) |
101 |
)
|
|
102 | 1x |
strata <- as.factor(strata) |
103 |
}
|
|
104 | ||
105 |
# Defining the std way to calculate weights for strat_newcombe
|
|
106 | 2x |
if (!is.null(variables$weights_method)) { |
107 | ! |
weights_method <- variables$weights_method |
108 |
} else { |
|
109 | 2x |
weights_method <- "cmh" |
110 |
}
|
|
111 | ||
112 | 2x |
y <- switch(method, |
113 | 2x |
"wald" = prop_diff_wald(rsp, grp, conf_level, correct = FALSE), |
114 | 2x |
"waldcc" = prop_diff_wald(rsp, grp, conf_level, correct = TRUE), |
115 | 2x |
"ha" = prop_diff_ha(rsp, grp, conf_level), |
116 | 2x |
"newcombe" = prop_diff_nc(rsp, grp, conf_level, correct = FALSE), |
117 | 2x |
"newcombecc" = prop_diff_nc(rsp, grp, conf_level, correct = TRUE), |
118 | 2x |
"strat_newcombe" = prop_diff_strat_nc(rsp, |
119 | 2x |
grp,
|
120 | 2x |
strata,
|
121 | 2x |
weights_method,
|
122 | 2x |
conf_level,
|
123 | 2x |
correct = FALSE |
124 |
),
|
|
125 | 2x |
"strat_newcombecc" = prop_diff_strat_nc(rsp, |
126 | 2x |
grp,
|
127 | 2x |
strata,
|
128 | 2x |
weights_method,
|
129 | 2x |
conf_level,
|
130 | 2x |
correct = TRUE |
131 |
),
|
|
132 | 2x |
"cmh" = prop_diff_cmh(rsp, grp, strata, conf_level)[c("diff", "diff_ci")] |
133 |
)
|
|
134 | ||
135 | 2x |
y$diff <- y$diff * 100 |
136 | 2x |
y$diff_ci <- y$diff_ci * 100 |
137 |
}
|
|
138 | ||
139 | 2x |
attr(y$diff, "label") <- "Difference in Response rate (%)" |
140 | 2x |
attr(y$diff_ci, "label") <- d_proportion_diff( |
141 | 2x |
conf_level, method, |
142 | 2x |
long = FALSE |
143 |
)
|
|
144 | ||
145 | 2x |
y
|
146 |
}
|
|
147 | ||
148 |
#' @describeIn prop_diff Formatted analysis function which is used as `afun` in `estimate_proportion_diff()`.
|
|
149 |
#'
|
|
150 |
#' @return
|
|
151 |
#' * `a_proportion_diff()` returns the corresponding list with formatted [rtables::CellValue()].
|
|
152 |
#'
|
|
153 |
#' @examples
|
|
154 |
#' a_proportion_diff(
|
|
155 |
#' df = subset(dta, grp == "A"),
|
|
156 |
#' .var = "rsp",
|
|
157 |
#' .ref_group = subset(dta, grp == "B"),
|
|
158 |
#' .in_ref_col = FALSE,
|
|
159 |
#' conf_level = 0.90,
|
|
160 |
#' method = "ha"
|
|
161 |
#' )
|
|
162 |
#'
|
|
163 |
#' @export
|
|
164 |
a_proportion_diff <- make_afun( |
|
165 |
s_proportion_diff,
|
|
166 |
.formats = c(diff = "xx.x", diff_ci = "(xx.x, xx.x)"), |
|
167 |
.indent_mods = c(diff = 0L, diff_ci = 1L) |
|
168 |
)
|
|
169 | ||
170 |
#' @describeIn prop_diff Layout-creating function which can take statistics function arguments
|
|
171 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()].
|
|
172 |
#'
|
|
173 |
#' @param ... arguments passed to `s_proportion_diff()`.
|
|
174 |
#'
|
|
175 |
#' @return
|
|
176 |
#' * `estimate_proportion_diff()` returns a layout object suitable for passing to further layouting functions,
|
|
177 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing
|
|
178 |
#' the statistics from `s_proportion_diff()` to the table layout.
|
|
179 |
#'
|
|
180 |
#' @examples
|
|
181 |
#' l <- basic_table() %>%
|
|
182 |
#' split_cols_by(var = "grp", ref_group = "B") %>%
|
|
183 |
#' estimate_proportion_diff(
|
|
184 |
#' vars = "rsp",
|
|
185 |
#' conf_level = 0.90,
|
|
186 |
#' method = "ha"
|
|
187 |
#' )
|
|
188 |
#'
|
|
189 |
#' build_table(l, df = dta)
|
|
190 |
#'
|
|
191 |
#' @export
|
|
192 |
estimate_proportion_diff <- function(lyt, |
|
193 |
vars,
|
|
194 |
...,
|
|
195 |
var_labels = vars, |
|
196 |
show_labels = "hidden", |
|
197 |
table_names = vars, |
|
198 |
.stats = NULL, |
|
199 |
.formats = NULL, |
|
200 |
.labels = NULL, |
|
201 |
.indent_mods = NULL) { |
|
202 | 3x |
afun <- make_afun( |
203 | 3x |
a_proportion_diff,
|
204 | 3x |
.stats = .stats, |
205 | 3x |
.formats = .formats, |
206 | 3x |
.labels = .labels, |
207 | 3x |
.indent_mods = .indent_mods |
208 |
)
|
|
209 | ||
210 | 3x |
analyze( |
211 | 3x |
lyt,
|
212 | 3x |
vars,
|
213 | 3x |
afun = afun, |
214 | 3x |
var_labels = var_labels, |
215 | 3x |
extra_args = list(...), |
216 | 3x |
show_labels = show_labels, |
217 | 3x |
table_names = table_names |
218 |
)
|
|
219 |
}
|
|
220 | ||
221 |
#' Check: Proportion Difference Arguments
|
|
222 |
#'
|
|
223 |
#' Verifies that and/or convert arguments into valid values to be used in the
|
|
224 |
#' estimation of difference in responder proportions.
|
|
225 |
#'
|
|
226 |
#' @inheritParams prop_diff
|
|
227 |
#' @inheritParams prop_diff_wald
|
|
228 |
#'
|
|
229 |
#' @keywords internal
|
|
230 |
check_diff_prop_ci <- function(rsp, |
|
231 |
grp,
|
|
232 |
strata = NULL, |
|
233 |
conf_level,
|
|
234 |
correct = NULL) { |
|
235 | 17x |
checkmate::assert_logical(rsp, any.missing = FALSE) |
236 | 17x |
checkmate::assert_factor(grp, len = length(rsp), any.missing = FALSE, n.levels = 2) |
237 | 17x |
checkmate::assert_number(conf_level, lower = 0, upper = 1) |
238 | 17x |
checkmate::assert_flag(correct, null.ok = TRUE) |
239 | ||
240 | 17x |
if (!is.null(strata)) { |
241 | 11x |
checkmate::assert_factor(strata, len = length(rsp)) |
242 |
}
|
|
243 | ||
244 | 17x |
invisible() |
245 |
}
|
|
246 | ||
247 |
#' Description of Method Used for Proportion Comparison
|
|
248 |
#'
|
|
249 |
#' @description `r lifecycle::badge("stable")`
|
|
250 |
#'
|
|
251 |
#' This is an auxiliary function that describes the analysis in
|
|
252 |
#' `s_proportion_diff`.
|
|
253 |
#'
|
|
254 |
#' @inheritParams s_proportion_diff
|
|
255 |
#' @param long (`logical`)\cr Whether a long or a short (default) description is required.
|
|
256 |
#'
|
|
257 |
#' @return A `string` describing the analysis.
|
|
258 |
#'
|
|
259 |
#' @seealso [prop_diff]
|
|
260 |
#'
|
|
261 |
#' @export
|
|
262 |
d_proportion_diff <- function(conf_level, |
|
263 |
method,
|
|
264 |
long = FALSE) { |
|
265 | 8x |
label <- paste0(conf_level * 100, "% CI") |
266 | 8x |
if (long) { |
267 | ! |
label <- paste( |
268 | ! |
label,
|
269 | ! |
ifelse( |
270 | ! |
method == "cmh", |
271 | ! |
"for adjusted difference",
|
272 | ! |
"for difference"
|
273 |
)
|
|
274 |
)
|
|
275 |
}
|
|
276 | ||
277 | 8x |
method_part <- switch(method, |
278 | 8x |
"cmh" = "CMH, without correction", |
279 | 8x |
"waldcc" = "Wald, with correction", |
280 | 8x |
"wald" = "Wald, without correction", |
281 | 8x |
"ha" = "Anderson-Hauck", |
282 | 8x |
"newcombe" = "Newcombe, without correction", |
283 | 8x |
"newcombecc" = "Newcombe, with correction", |
284 | 8x |
"strat_newcombe" = "Stratified Newcombe, without correction", |
285 | 8x |
"strat_newcombecc" = "Stratified Newcombe, with correction", |
286 | 8x |
stop(paste(method, "does not have a description")) |
287 |
)
|
|
288 | 8x |
paste0(label, " (", method_part, ")") |
289 |
}
|
|
290 | ||
291 |
#' Helper Functions to Calculate Proportion Difference
|
|
292 |
#'
|
|
293 |
#' @description `r lifecycle::badge("stable")`
|
|
294 |
#'
|
|
295 |
#' @inheritParams argument_convention
|
|
296 |
#' @inheritParams prop_diff
|
|
297 |
#' @param grp (`factor`)\cr vector assigning observations to one out of two groups
|
|
298 |
#' (e.g. reference and treatment group).
|
|
299 |
#'
|
|
300 |
#' @return A named `list` of elements `diff` (proportion difference) and `diff_ci`
|
|
301 |
#' (proportion difference confidence interval).
|
|
302 |
#'
|
|
303 |
#' @seealso [prop_diff()] for implementation of these helper functions.
|
|
304 |
#'
|
|
305 |
#' @name h_prop_diff
|
|
306 |
NULL
|
|
307 | ||
308 |
#' @describeIn h_prop_diff The Wald interval follows the usual textbook
|
|
309 |
#' definition for a single proportion confidence interval using the normal
|
|
310 |
#' approximation. It is possible to include a continuity correction for Wald's
|
|
311 |
#' interval.
|
|
312 |
#'
|
|
313 |
#' @param correct (`logical`)\cr whether to include the continuity correction. For further
|
|
314 |
#' information, see [stats::prop.test()].
|
|
315 |
#'
|
|
316 |
#' @examples
|
|
317 |
#' # Wald confidence interval
|
|
318 |
#' set.seed(2)
|
|
319 |
#' rsp <- sample(c(TRUE, FALSE), replace = TRUE, size = 20)
|
|
320 |
#' grp <- factor(c(rep("A", 10), rep("B", 10)))
|
|
321 |
#' prop_diff_wald(rsp = rsp, grp = grp, conf_level = 0.95, correct = FALSE)
|
|
322 |
#'
|
|
323 |
#' @export
|
|
324 |
prop_diff_wald <- function(rsp, |
|
325 |
grp,
|
|
326 |
conf_level = 0.95, |
|
327 |
correct = FALSE) { |
|
328 | 2x |
if (isTRUE(correct)) { |
329 | 1x |
mthd <- "waldcc" |
330 |
} else { |
|
331 | 1x |
mthd <- "wald" |
332 |
}
|
|
333 | 2x |
grp <- as_factor_keep_attributes(grp) |
334 | 2x |
check_diff_prop_ci( |
335 | 2x |
rsp = rsp, grp = grp, conf_level = conf_level, correct = correct |
336 |
)
|
|
337 | ||
338 |
# check if binary response is coded as logical
|
|
339 | 2x |
checkmate::assert_logical(rsp, any.missing = FALSE) |
340 | 2x |
checkmate::assert_factor(grp, len = length(rsp), any.missing = FALSE, n.levels = 2) |
341 | ||
342 | 2x |
tbl <- table(grp, factor(rsp, levels = c(TRUE, FALSE))) |
343 |
# x1 and n1 are non-reference groups.
|
|
344 | 2x |
diff_ci <- desctools_binom( |
345 | 2x |
x1 = tbl[2], n1 = sum(tbl[2], tbl[4]), |
346 | 2x |
x2 = tbl[1], n2 = sum(tbl[1], tbl[3]), |
347 | 2x |
conf.level = conf_level, |
348 | 2x |
method = mthd |
349 |
)
|
|
350 | ||
351 | 2x |
list( |
352 | 2x |
"diff" = unname(diff_ci[, "est"]), |
353 | 2x |
"diff_ci" = unname(diff_ci[, c("lwr.ci", "upr.ci")]) |
354 |
)
|
|
355 |
}
|
|
356 | ||
357 |
#' @describeIn h_prop_diff Anderson-Hauck confidence interval.
|
|
358 |
#'
|
|
359 |
#' @examples
|
|
360 |
#' # Anderson-Hauck confidence interval
|
|
361 |
#' ## "Mid" case: 3/4 respond in group A, 1/2 respond in group B.
|
|
362 |
#' rsp <- c(TRUE, FALSE, FALSE, TRUE, TRUE, TRUE)
|
|
363 |
#' grp <- factor(c("A", "B", "A", "B", "A", "A"), levels = c("B", "A"))
|
|
364 |
#' prop_diff_ha(rsp = rsp, grp = grp, conf_level = 0.90)
|
|
365 |
#'
|
|
366 |
#' ## Edge case: Same proportion of response in A and B.
|
|
367 |
#' rsp <- c(TRUE, FALSE, TRUE, FALSE)
|
|
368 |
#' grp <- factor(c("A", "A", "B", "B"), levels = c("A", "B"))
|
|
369 |
#' prop_diff_ha(rsp = rsp, grp = grp, conf_level = 0.6)
|
|
370 |
#'
|
|
371 |
#' @export
|
|
372 |
prop_diff_ha <- function(rsp, |
|
373 |
grp,
|
|
374 |
conf_level) { |
|
375 | 3x |
grp <- as_factor_keep_attributes(grp) |
376 | 3x |
check_diff_prop_ci(rsp = rsp, grp = grp, conf_level = conf_level) |
377 | ||
378 | 3x |
tbl <- table(grp, factor(rsp, levels = c(TRUE, FALSE))) |
379 |
# x1 and n1 are non-reference groups.
|
|
380 | 3x |
ci <- desctools_binom( |
381 | 3x |
x1 = tbl[2], n1 = sum(tbl[2], tbl[4]), |
382 | 3x |
x2 = tbl[1], n2 = sum(tbl[1], tbl[3]), |
383 | 3x |
conf.level = conf_level, |
384 | 3x |
method = "ha" |
385 |
)
|
|
386 | 3x |
list( |
387 | 3x |
"diff" = unname(ci[, "est"]), |
388 | 3x |
"diff_ci" = unname(ci[, c("lwr.ci", "upr.ci")]) |
389 |
)
|
|
390 |
}
|
|
391 | ||
392 |
#' @describeIn h_prop_diff `Newcombe` confidence interval. It is based on
|
|
393 |
#' the Wilson score confidence interval for a single binomial proportion.
|
|
394 |
#'
|
|
395 |
#' @examples
|
|
396 |
#' # `Newcombe` confidence interval
|
|
397 |
#'
|
|
398 |
#' set.seed(1)
|
|
399 |
#' rsp <- c(
|
|
400 |
#' sample(c(TRUE, FALSE), size = 40, prob = c(3 / 4, 1 / 4), replace = TRUE),
|
|
401 |
#' sample(c(TRUE, FALSE), size = 40, prob = c(1 / 2, 1 / 2), replace = TRUE)
|
|
402 |
#' )
|
|
403 |
#' grp <- factor(rep(c("A", "B"), each = 40), levels = c("B", "A"))
|
|
404 |
#' table(rsp, grp)
|
|
405 |
#' prop_diff_nc(rsp = rsp, grp = grp, conf_level = 0.9)
|
|
406 |
#'
|
|
407 |
#' @export
|
|
408 |
prop_diff_nc <- function(rsp, |
|
409 |
grp,
|
|
410 |
conf_level,
|
|
411 |
correct = FALSE) { |
|
412 | 1x |
if (isTRUE(correct)) { |
413 | ! |
mthd <- "scorecc" |
414 |
} else { |
|
415 | 1x |
mthd <- "score" |
416 |
}
|
|
417 | 1x |
grp <- as_factor_keep_attributes(grp) |
418 | 1x |
check_diff_prop_ci(rsp = rsp, grp = grp, conf_level = conf_level) |
419 | ||
420 | 1x |
p_grp <- tapply(rsp, grp, mean) |
421 | 1x |
diff_p <- unname(diff(p_grp)) |
422 | 1x |
tbl <- table(grp, factor(rsp, levels = c(TRUE, FALSE))) |
423 | 1x |
ci <- desctools_binom( |
424 |
# x1 and n1 are non-reference groups.
|
|
425 | 1x |
x1 = tbl[2], n1 = sum(tbl[2], tbl[4]), |
426 | 1x |
x2 = tbl[1], n2 = sum(tbl[1], tbl[3]), |
427 | 1x |
conf.level = conf_level, |
428 | 1x |
method = mthd |
429 |
)
|
|
430 | 1x |
list( |
431 | 1x |
"diff" = unname(ci[, "est"]), |
432 | 1x |
"diff_ci" = unname(ci[, c("lwr.ci", "upr.ci")]) |
433 |
)
|
|
434 |
}
|
|
435 | ||
436 |
#' @describeIn h_prop_diff Calculates the weighted difference. This is defined as the difference in
|
|
437 |
#' response rates between the experimental treatment group and the control treatment group, adjusted
|
|
438 |
#' for stratification factors by applying `Cochran-Mantel-Haenszel` (`CMH`) weights. For the `CMH` chi-squared
|
|
439 |
#' test, use [stats::mantelhaen.test()].
|
|
440 |
#'
|
|
441 |
#' @param strata (`factor`)\cr variable with one level per stratum and same length as `rsp`.
|
|
442 |
#'
|
|
443 |
#' @examples
|
|
444 |
#' # Cochran-Mantel-Haenszel confidence interval
|
|
445 |
#'
|
|
446 |
#' set.seed(2)
|
|
447 |
#' rsp <- sample(c(TRUE, FALSE), 100, TRUE)
|
|
448 |
#' grp <- sample(c("Placebo", "Treatment"), 100, TRUE)
|
|
449 |
#' grp <- factor(grp, levels = c("Placebo", "Treatment"))
|
|
450 |
#' strata_data <- data.frame(
|
|
451 |
#' "f1" = sample(c("a", "b"), 100, TRUE),
|
|
452 |
#' "f2" = sample(c("x", "y", "z"), 100, TRUE),
|
|
453 |
#' stringsAsFactors = TRUE
|
|
454 |
#' )
|
|
455 |
#'
|
|
456 |
#' prop_diff_cmh(
|
|
457 |
#' rsp = rsp, grp = grp, strata = interaction(strata_data),
|
|
458 |
#' conf_level = 0.90
|
|
459 |
#' )
|
|
460 |
#'
|
|
461 |
#' @export
|
|
462 |
prop_diff_cmh <- function(rsp, |
|
463 |
grp,
|
|
464 |
strata,
|
|
465 |
conf_level = 0.95) { |
|
466 | 7x |
grp <- as_factor_keep_attributes(grp) |
467 | 7x |
strata <- as_factor_keep_attributes(strata) |
468 | 7x |
check_diff_prop_ci( |
469 | 7x |
rsp = rsp, grp = grp, conf_level = conf_level, strata = strata |
470 |
)
|
|
471 | ||
472 | 7x |
if (any(tapply(rsp, strata, length) < 5)) { |
473 | ! |
warning("Less than 5 observations in some strata.") |
474 |
}
|
|
475 | ||
476 |
# first dimension: FALSE, TRUE
|
|
477 |
# 2nd dimension: CONTROL, TX
|
|
478 |
# 3rd dimension: levels of strat
|
|
479 |
# rsp as factor rsp to handle edge case of no FALSE (or TRUE) rsp records
|
|
480 | 7x |
t_tbl <- table( |
481 | 7x |
factor(rsp, levels = c("FALSE", "TRUE")), |
482 | 7x |
grp,
|
483 | 7x |
strata
|
484 |
)
|
|
485 | 7x |
n1 <- colSums(t_tbl[1:2, 1, ]) |
486 | 7x |
n2 <- colSums(t_tbl[1:2, 2, ]) |
487 | 7x |
p1 <- t_tbl[2, 1, ] / n1 |
488 | 7x |
p2 <- t_tbl[2, 2, ] / n2 |
489 |
# CMH weights
|
|
490 | 7x |
use_stratum <- (n1 > 0) & (n2 > 0) |
491 | 7x |
n1 <- n1[use_stratum] |
492 | 7x |
n2 <- n2[use_stratum] |
493 | 7x |
p1 <- p1[use_stratum] |
494 | 7x |
p2 <- p2[use_stratum] |
495 | 7x |
wt <- (n1 * n2 / (n1 + n2)) |
496 | 7x |
wt_normalized <- wt / sum(wt) |
497 | 7x |
est1 <- sum(wt_normalized * p1) |
498 | 7x |
est2 <- sum(wt_normalized * p2) |
499 | 7x |
estimate <- c(est1, est2) |
500 | 7x |
names(estimate) <- levels(grp) |
501 | 7x |
se1 <- sqrt(sum(wt_normalized^2 * p1 * (1 - p1) / n1)) |
502 | 7x |
se2 <- sqrt(sum(wt_normalized^2 * p2 * (1 - p2) / n2)) |
503 | 7x |
z <- stats::qnorm((1 + conf_level) / 2) |
504 | 7x |
err1 <- z * se1 |
505 | 7x |
err2 <- z * se2 |
506 | 7x |
ci1 <- c((est1 - err1), (est1 + err1)) |
507 | 7x |
ci2 <- c((est2 - err2), (est2 + err2)) |
508 | 7x |
estimate_ci <- list(ci1, ci2) |
509 | 7x |
names(estimate_ci) <- levels(grp) |
510 | 7x |
diff_est <- est2 - est1 |
511 | 7x |
se_diff <- sqrt(sum(((p1 * (1 - p1) / n1) + (p2 * (1 - p2) / n2)) * wt_normalized^2)) |
512 | 7x |
diff_ci <- c(diff_est - z * se_diff, diff_est + z * se_diff) |
513 | ||
514 | 7x |
list( |
515 | 7x |
prop = estimate, |
516 | 7x |
prop_ci = estimate_ci, |
517 | 7x |
diff = diff_est, |
518 | 7x |
diff_ci = diff_ci, |
519 | 7x |
weights = wt_normalized, |
520 | 7x |
n1 = n1, |
521 | 7x |
n2 = n2 |
522 |
)
|
|
523 |
}
|
|
524 | ||
525 |
#' @describeIn h_prop_diff Calculates the stratified `Newcombe` confidence interval and difference in response
|
|
526 |
#' rates between the experimental treatment group and the control treatment group, adjusted for stratification
|
|
527 |
#' factors. This implementation follows closely the one proposed by \insertCite{Yan2010-jt;textual}{tern}.
|
|
528 |
#' Weights can be estimated from the heuristic proposed in [prop_strat_wilson()] or from `CMH`-derived weights
|
|
529 |
#' (see [prop_diff_cmh()]).
|
|
530 |
#'
|
|
531 |
#' @param strata (`factor`)\cr variable with one level per stratum and same length as `rsp`.
|
|
532 |
#' @param weights_method (`string`)\cr weights method. Can be either `"cmh"` or `"heuristic"`
|
|
533 |
#' and directs the way weights are estimated.
|
|
534 |
#'
|
|
535 |
#' @references
|
|
536 |
#' \insertRef{Yan2010-jt}{tern}
|
|
537 |
#'
|
|
538 |
#' @examples
|
|
539 |
#' # Stratified `Newcombe` confidence interval
|
|
540 |
#'
|
|
541 |
#' set.seed(2)
|
|
542 |
#' data_set <- data.frame(
|
|
543 |
#' "rsp" = sample(c(TRUE, FALSE), 100, TRUE),
|
|
544 |
#' "f1" = sample(c("a", "b"), 100, TRUE),
|
|
545 |
#' "f2" = sample(c("x", "y", "z"), 100, TRUE),
|
|
546 |
#' "grp" = sample(c("Placebo", "Treatment"), 100, TRUE),
|
|
547 |
#' stringsAsFactors = TRUE
|
|
548 |
#' )
|
|
549 |
#'
|
|
550 |
#' prop_diff_strat_nc(
|
|
551 |
#' rsp = data_set$rsp, grp = data_set$grp, strata = interaction(data_set[2:3]),
|
|
552 |
#' weights_method = "cmh",
|
|
553 |
#' conf_level = 0.90
|
|
554 |
#' )
|
|
555 |
#'
|
|
556 |
#' prop_diff_strat_nc(
|
|
557 |
#' rsp = data_set$rsp, grp = data_set$grp, strata = interaction(data_set[2:3]),
|
|
558 |
#' weights_method = "wilson_h",
|
|
559 |
#' conf_level = 0.90
|
|
560 |
#' )
|
|
561 |
#'
|
|
562 |
#' @export
|
|
563 |
prop_diff_strat_nc <- function(rsp, |
|
564 |
grp,
|
|
565 |
strata,
|
|
566 |
weights_method = c("cmh", "wilson_h"), |
|
567 |
conf_level = 0.95, |
|
568 |
correct = FALSE) { |
|
569 | 4x |
weights_method <- match.arg(weights_method) |
570 | 4x |
grp <- as_factor_keep_attributes(grp) |
571 | 4x |
strata <- as_factor_keep_attributes(strata) |
572 | 4x |
check_diff_prop_ci( |
573 | 4x |
rsp = rsp, grp = grp, conf_level = conf_level, strata = strata |
574 |
)
|
|
575 | 4x |
checkmate::assert_number(conf_level, lower = 0, upper = 1) |
576 | 4x |
checkmate::assert_flag(correct) |
577 | 4x |
if (any(tapply(rsp, strata, length) < 5)) { |
578 | ! |
warning("Less than 5 observations in some strata.") |
579 |
}
|
|
580 | ||
581 | 4x |
rsp_by_grp <- split(rsp, f = grp) |
582 | 4x |
strata_by_grp <- split(strata, f = grp) |
583 | ||
584 |
# Finding the weights
|
|
585 | 4x |
weights <- if (identical(weights_method, "cmh")) { |
586 | 3x |
prop_diff_cmh(rsp = rsp, grp = grp, strata = strata)$weights |
587 | 4x |
} else if (identical(weights_method, "wilson_h")) { |
588 | 1x |
prop_strat_wilson(rsp, strata, conf_level = conf_level, correct = correct)$weights |
589 |
}
|
|
590 | 4x |
weights[levels(strata)[!levels(strata) %in% names(weights)]] <- 0 |
591 | ||
592 |
# Calculating lower (`l`) and upper (`u`) confidence bounds per group.
|
|
593 | 4x |
strat_wilson_by_grp <- Map( |
594 | 4x |
prop_strat_wilson,
|
595 | 4x |
rsp = rsp_by_grp, |
596 | 4x |
strata = strata_by_grp, |
597 | 4x |
weights = list(weights, weights), |
598 | 4x |
conf_level = conf_level, |
599 | 4x |
correct = correct |
600 |
)
|
|
601 | ||
602 | 4x |
ci_ref <- strat_wilson_by_grp[[1]] |
603 | 4x |
ci_trt <- strat_wilson_by_grp[[2]] |
604 | 4x |
l_ref <- as.numeric(ci_ref$conf_int[1]) |
605 | 4x |
u_ref <- as.numeric(ci_ref$conf_int[2]) |
606 | 4x |
l_trt <- as.numeric(ci_trt$conf_int[1]) |
607 | 4x |
u_trt <- as.numeric(ci_trt$conf_int[2]) |
608 | ||
609 |
# Estimating the diff and n_ref, n_trt (it allows different weights to be used)
|
|
610 | 4x |
t_tbl <- table( |
611 | 4x |
factor(rsp, levels = c("FALSE", "TRUE")), |
612 | 4x |
grp,
|
613 | 4x |
strata
|
614 |
)
|
|
615 | 4x |
n_ref <- colSums(t_tbl[1:2, 1, ]) |
616 | 4x |
n_trt <- colSums(t_tbl[1:2, 2, ]) |
617 | 4x |
use_stratum <- (n_ref > 0) & (n_trt > 0) |
618 | 4x |
n_ref <- n_ref[use_stratum] |
619 | 4x |
n_trt <- n_trt[use_stratum] |
620 | 4x |
p_ref <- t_tbl[2, 1, use_stratum] / n_ref |
621 | 4x |
p_trt <- t_tbl[2, 2, use_stratum] / n_trt |
622 | 4x |
est1 <- sum(weights * p_ref) |
623 | 4x |
est2 <- sum(weights * p_trt) |
624 | 4x |
diff_est <- est2 - est1 |
625 | ||
626 | 4x |
lambda1 <- sum(weights^2 / n_ref) |
627 | 4x |
lambda2 <- sum(weights^2 / n_trt) |
628 | 4x |
z <- stats::qnorm((1 + conf_level) / 2) |
629 | ||
630 | 4x |
lower <- diff_est - z * sqrt(lambda2 * l_trt * (1 - l_trt) + lambda1 * u_ref * (1 - u_ref)) |
631 | 4x |
upper <- diff_est + z * sqrt(lambda1 * l_ref * (1 - l_ref) + lambda2 * u_trt * (1 - u_trt)) |
632 | ||
633 | 4x |
list( |
634 | 4x |
"diff" = diff_est, |
635 | 4x |
"diff_ci" = c("lower" = lower, "upper" = upper) |
636 |
)
|
|
637 |
}
|
1 |
#' Odds Ratio Estimation
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Compares bivariate responses between two groups in terms of odds ratios
|
|
6 |
#' along with a confidence interval.
|
|
7 |
#'
|
|
8 |
#' @inheritParams argument_convention
|
|
9 |
#'
|
|
10 |
#' @details This function uses either logistic regression for unstratified
|
|
11 |
#' analyses, or conditional logistic regression for stratified analyses.
|
|
12 |
#' The Wald confidence interval with the specified confidence level is
|
|
13 |
#' calculated.
|
|
14 |
#'
|
|
15 |
#' @note For stratified analyses, there is currently no implementation for conditional
|
|
16 |
#' likelihood confidence intervals, therefore the likelihood confidence interval is not
|
|
17 |
#' yet available as an option. Besides, when `rsp` contains only responders or non-responders,
|
|
18 |
#' then the result values will be `NA`, because no odds ratio estimation is possible.
|
|
19 |
#'
|
|
20 |
#' @seealso Relevant helper function [h_odds_ratio()].
|
|
21 |
#'
|
|
22 |
#' @name odds_ratio
|
|
23 |
NULL
|
|
24 | ||
25 |
#' @describeIn odds_ratio Statistics function which estimates the odds ratio
|
|
26 |
#' between a treatment and a control. A `variables` list with `arm` and `strata`
|
|
27 |
#' variable names must be passed if a stratified analysis is required.
|
|
28 |
#'
|
|
29 |
#' @inheritParams split_cols_by_groups
|
|
30 |
#'
|
|
31 |
#' @return
|
|
32 |
#' * `s_odds_ratio()` returns a named list with the statistics `or_ci`
|
|
33 |
#' (containing `est`, `lcl`, and `ucl`) and `n_tot`.
|
|
34 |
#'
|
|
35 |
#' @examples
|
|
36 |
#' set.seed(12)
|
|
37 |
#' dta <- data.frame(
|
|
38 |
#' rsp = sample(c(TRUE, FALSE), 100, TRUE),
|
|
39 |
#' grp = factor(rep(c("A", "B"), each = 50), levels = c("B", "A")),
|
|
40 |
#' strata = factor(sample(c("C", "D"), 100, TRUE))
|
|
41 |
#' )
|
|
42 |
#'
|
|
43 |
#' # Unstratified analysis.
|
|
44 |
#' s_odds_ratio(
|
|
45 |
#' df = subset(dta, grp == "A"),
|
|
46 |
#' .var = "rsp",
|
|
47 |
#' .ref_group = subset(dta, grp == "B"),
|
|
48 |
#' .in_ref_col = FALSE,
|
|
49 |
#' .df_row = dta
|
|
50 |
#' )
|
|
51 |
#'
|
|
52 |
#' # Stratified analysis.
|
|
53 |
#' s_odds_ratio(
|
|
54 |
#' df = subset(dta, grp == "A"),
|
|
55 |
#' .var = "rsp",
|
|
56 |
#' .ref_group = subset(dta, grp == "B"),
|
|
57 |
#' .in_ref_col = FALSE,
|
|
58 |
#' .df_row = dta,
|
|
59 |
#' variables = list(arm = "grp", strata = "strata")
|
|
60 |
#' )
|
|
61 |
#'
|
|
62 |
#' @export
|
|
63 |
s_odds_ratio <- function(df, |
|
64 |
.var,
|
|
65 |
.ref_group,
|
|
66 |
.in_ref_col,
|
|
67 |
.df_row,
|
|
68 |
variables = list(arm = NULL, strata = NULL), |
|
69 |
conf_level = 0.95, |
|
70 |
groups_list = NULL) { |
|
71 | 65x |
y <- list(or_ci = "", n_tot = "") |
72 | ||
73 | 65x |
if (!.in_ref_col) { |
74 | 65x |
assert_proportion_value(conf_level) |
75 | 65x |
assert_df_with_variables(df, list(rsp = .var)) |
76 | 65x |
assert_df_with_variables(.ref_group, list(rsp = .var)) |
77 | ||
78 | 65x |
if (is.null(variables$strata)) { |
79 | 52x |
data <- data.frame( |
80 | 52x |
rsp = c(.ref_group[[.var]], df[[.var]]), |
81 | 52x |
grp = factor( |
82 | 52x |
rep(c("ref", "Not-ref"), c(nrow(.ref_group), nrow(df))), |
83 | 52x |
levels = c("ref", "Not-ref") |
84 |
)
|
|
85 |
)
|
|
86 | 52x |
y <- or_glm(data, conf_level = conf_level) |
87 |
} else { |
|
88 | 13x |
assert_df_with_variables(.df_row, c(list(rsp = .var), variables)) |
89 | ||
90 |
# The group variable prepared for clogit must be synchronised with combination groups definition.
|
|
91 | 13x |
if (is.null(groups_list)) { |
92 | 12x |
ref_grp <- as.character(unique(.ref_group[[variables$arm]])) |
93 | 12x |
trt_grp <- as.character(unique(df[[variables$arm]])) |
94 | 12x |
grp <- stats::relevel(factor(.df_row[[variables$arm]]), ref = ref_grp) |
95 |
} else { |
|
96 |
# If more than one level in reference col.
|
|
97 | 1x |
reference <- as.character(unique(.ref_group[[variables$arm]])) |
98 | 1x |
grp_ref_flag <- vapply( |
99 | 1x |
X = groups_list, |
100 | 1x |
FUN.VALUE = TRUE, |
101 | 1x |
FUN = function(x) all(reference %in% x) |
102 |
)
|
|
103 | 1x |
ref_grp <- names(groups_list)[grp_ref_flag] |
104 | ||
105 |
# If more than one level in treatment col.
|
|
106 | 1x |
treatment <- as.character(unique(df[[variables$arm]])) |
107 | 1x |
grp_trt_flag <- vapply( |
108 | 1x |
X = groups_list, |
109 | 1x |
FUN.VALUE = TRUE, |
110 | 1x |
FUN = function(x) all(treatment %in% x) |
111 |
)
|
|
112 | 1x |
trt_grp <- names(groups_list)[grp_trt_flag] |
113 | ||
114 | 1x |
grp <- combine_levels(.df_row[[variables$arm]], levels = reference, new_level = ref_grp) |
115 | 1x |
grp <- combine_levels(grp, levels = treatment, new_level = trt_grp) |
116 |
}
|
|
117 | ||
118 |
# The reference level in `grp` must be the same as in the `rtables` column split.
|
|
119 | 13x |
data <- data.frame( |
120 | 13x |
rsp = .df_row[[.var]], |
121 | 13x |
grp = grp, |
122 | 13x |
strata = interaction(.df_row[variables$strata]) |
123 |
)
|
|
124 | 13x |
y_all <- or_clogit(data, conf_level = conf_level) |
125 | 13x |
checkmate::assert_string(trt_grp) |
126 | 13x |
checkmate::assert_subset(trt_grp, names(y_all$or_ci)) |
127 | 12x |
y$or_ci <- y_all$or_ci[[trt_grp]] |
128 | 12x |
y$n_tot <- y_all$n_tot |
129 |
}
|
|
130 |
}
|
|
131 | ||
132 | 64x |
y$or_ci <- formatters::with_label( |
133 | 64x |
x = y$or_ci, |
134 | 64x |
label = paste0("Odds Ratio (", 100 * conf_level, "% CI)") |
135 |
)
|
|
136 | ||
137 | 64x |
y$n_tot <- formatters::with_label( |
138 | 64x |
x = y$n_tot, |
139 | 64x |
label = "Total n" |
140 |
)
|
|
141 | ||
142 | 64x |
y
|
143 |
}
|
|
144 | ||
145 |
#' @describeIn odds_ratio Formatted analysis function which is used as `afun` in `estimate_odds_ratio()`.
|
|
146 |
#'
|
|
147 |
#' @return
|
|
148 |
#' * `a_odds_ratio()` returns the corresponding list with formatted [rtables::CellValue()].
|
|
149 |
#'
|
|
150 |
#' @examples
|
|
151 |
#' a_odds_ratio(
|
|
152 |
#' df = subset(dta, grp == "A"),
|
|
153 |
#' .var = "rsp",
|
|
154 |
#' .ref_group = subset(dta, grp == "B"),
|
|
155 |
#' .in_ref_col = FALSE,
|
|
156 |
#' .df_row = dta
|
|
157 |
#' )
|
|
158 |
#'
|
|
159 |
#' @export
|
|
160 |
a_odds_ratio <- make_afun( |
|
161 |
s_odds_ratio,
|
|
162 |
.formats = c(or_ci = "xx.xx (xx.xx - xx.xx)"), |
|
163 |
.indent_mods = c(or_ci = 1L) |
|
164 |
)
|
|
165 | ||
166 |
#' @describeIn odds_ratio Layout-creating function which can take statistics function arguments
|
|
167 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()].
|
|
168 |
#'
|
|
169 |
#' @param ... arguments passed to `s_odds_ratio()`.
|
|
170 |
#'
|
|
171 |
#' @return
|
|
172 |
#' * `estimate_odds_ratio()` returns a layout object suitable for passing to further layouting functions,
|
|
173 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing
|
|
174 |
#' the statistics from `s_odds_ratio()` to the table layout.
|
|
175 |
#'
|
|
176 |
#' @examples
|
|
177 |
#' dta <- data.frame(
|
|
178 |
#' rsp = sample(c(TRUE, FALSE), 100, TRUE),
|
|
179 |
#' grp = factor(rep(c("A", "B"), each = 50))
|
|
180 |
#' )
|
|
181 |
#'
|
|
182 |
#' l <- basic_table() %>%
|
|
183 |
#' split_cols_by(var = "grp", ref_group = "B") %>%
|
|
184 |
#' estimate_odds_ratio(vars = "rsp")
|
|
185 |
#'
|
|
186 |
#' build_table(l, df = dta)
|
|
187 |
#'
|
|
188 |
#' @export
|
|
189 |
estimate_odds_ratio <- function(lyt, |
|
190 |
vars,
|
|
191 |
...,
|
|
192 |
show_labels = "hidden", |
|
193 |
table_names = vars, |
|
194 |
.stats = "or_ci", |
|
195 |
.formats = NULL, |
|
196 |
.labels = NULL, |
|
197 |
.indent_mods = NULL) { |
|
198 | 3x |
afun <- make_afun( |
199 | 3x |
a_odds_ratio,
|
200 | 3x |
.stats = .stats, |
201 | 3x |
.formats = .formats, |
202 | 3x |
.labels = .labels, |
203 | 3x |
.indent_mods = .indent_mods |
204 |
)
|
|
205 | ||
206 | 3x |
analyze( |
207 | 3x |
lyt,
|
208 | 3x |
vars,
|
209 | 3x |
afun = afun, |
210 | 3x |
extra_args = list(...), |
211 | 3x |
show_labels = show_labels, |
212 | 3x |
table_names = table_names |
213 |
)
|
|
214 |
}
|
|
215 | ||
216 |
#' Helper Functions for Odds Ratio Estimation
|
|
217 |
#'
|
|
218 |
#' @description `r lifecycle::badge("stable")`
|
|
219 |
#'
|
|
220 |
#' Functions to calculate odds ratios in [estimate_odds_ratio()].
|
|
221 |
#'
|
|
222 |
#' @inheritParams argument_convention
|
|
223 |
#' @param data (`data.frame`)\cr data frame containing at least the variables `rsp` and `grp`, and optionally
|
|
224 |
#' `strata` for [or_clogit()].
|
|
225 |
#'
|
|
226 |
#' @return A named `list` of elements `or_ci` and `n_tot`.
|
|
227 |
#'
|
|
228 |
#' @seealso [odds_ratio]
|
|
229 |
#'
|
|
230 |
#' @name h_odds_ratio
|
|
231 |
NULL
|
|
232 | ||
233 |
#' @describeIn h_odds_ratio Estimates the odds ratio based on [stats::glm()]. Note that there must be
|
|
234 |
#' exactly 2 groups in `data` as specified by the `grp` variable.
|
|
235 |
#'
|
|
236 |
#' @examples
|
|
237 |
#' # Data with 2 groups.
|
|
238 |
#' data <- data.frame(
|
|
239 |
#' rsp = as.logical(c(1, 1, 0, 1, 0, 0, 1, 1)),
|
|
240 |
#' grp = letters[c(1, 1, 1, 2, 2, 2, 1, 2)],
|
|
241 |
#' strata = letters[c(1, 2, 1, 2, 2, 2, 1, 2)],
|
|
242 |
#' stringsAsFactors = TRUE
|
|
243 |
#' )
|
|
244 |
#'
|
|
245 |
#' # Odds ratio based on glm.
|
|
246 |
#' or_glm(data, conf_level = 0.95)
|
|
247 |
#'
|
|
248 |
#' @export
|
|
249 |
or_glm <- function(data, conf_level) { |
|
250 | 55x |
checkmate::assert_logical(data$rsp) |
251 | 55x |
assert_proportion_value(conf_level) |
252 | 55x |
assert_df_with_variables(data, list(rsp = "rsp", grp = "grp")) |
253 | 55x |
checkmate::assert_multi_class(data$grp, classes = c("factor", "character")) |
254 | ||
255 | 55x |
data$grp <- as_factor_keep_attributes(data$grp) |
256 | 55x |
assert_df_with_factors(data, list(val = "grp"), min.levels = 2, max.levels = 2) |
257 | 55x |
formula <- stats::as.formula("rsp ~ grp") |
258 | 55x |
model_fit <- stats::glm( |
259 | 55x |
formula = formula, data = data, |
260 | 55x |
family = stats::binomial(link = "logit") |
261 |
)
|
|
262 | ||
263 |
# Note that here we need to discard the intercept.
|
|
264 | 55x |
or <- exp(stats::coef(model_fit)[-1]) |
265 | 55x |
or_ci <- exp( |
266 | 55x |
stats::confint.default(model_fit, level = conf_level)[-1, , drop = FALSE] |
267 |
)
|
|
268 | ||
269 | 55x |
values <- stats::setNames(c(or, or_ci), c("est", "lcl", "ucl")) |
270 | 55x |
n_tot <- stats::setNames(nrow(model_fit$model), "n_tot") |
271 | ||
272 | 55x |
list(or_ci = values, n_tot = n_tot) |
273 |
}
|
|
274 | ||
275 |
#' @describeIn h_odds_ratio estimates the odds ratio based on [survival::clogit()]. This is done for
|
|
276 |
#' the whole data set including all groups, since the results are not the same as when doing
|
|
277 |
#' pairwise comparisons between the groups.
|
|
278 |
#'
|
|
279 |
#' @examples
|
|
280 |
#' # Data with 3 groups.
|
|
281 |
#' data <- data.frame(
|
|
282 |
#' rsp = as.logical(c(1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0)),
|
|
283 |
#' grp = letters[c(1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3)],
|
|
284 |
#' strata = LETTERS[c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)],
|
|
285 |
#' stringsAsFactors = TRUE
|
|
286 |
#' )
|
|
287 |
#'
|
|
288 |
#' # Odds ratio based on stratified estimation by conditional logistic regression.
|
|
289 |
#' or_clogit(data, conf_level = 0.95)
|
|
290 |
#'
|
|
291 |
#' @export
|
|
292 |
or_clogit <- function(data, conf_level) { |
|
293 | 16x |
checkmate::assert_logical(data$rsp) |
294 | 16x |
assert_proportion_value(conf_level) |
295 | 16x |
assert_df_with_variables(data, list(rsp = "rsp", grp = "grp", strata = "strata")) |
296 | 16x |
checkmate::assert_multi_class(data$grp, classes = c("factor", "character")) |
297 | 16x |
checkmate::assert_multi_class(data$strata, classes = c("factor", "character")) |
298 | ||
299 | 16x |
data$grp <- as_factor_keep_attributes(data$grp) |
300 | 16x |
data$strata <- as_factor_keep_attributes(data$strata) |
301 | ||
302 |
# Deviation from convention: `survival::strata` must be simply `strata`.
|
|
303 | 16x |
formula <- stats::as.formula("rsp ~ grp + strata(strata)") |
304 | 16x |
model_fit <- clogit_with_tryCatch(formula = formula, data = data) |
305 | ||
306 |
# Create a list with one set of OR estimates and CI per coefficient, i.e.
|
|
307 |
# comparison of one group vs. the reference group.
|
|
308 | 16x |
coef_est <- stats::coef(model_fit) |
309 | 16x |
ci_est <- stats::confint(model_fit, level = conf_level) |
310 | 16x |
or_ci <- list() |
311 | 16x |
for (coef_name in names(coef_est)) { |
312 | 18x |
grp_name <- gsub("^grp", "", x = coef_name) |
313 | 18x |
or_ci[[grp_name]] <- stats::setNames( |
314 | 18x |
object = exp(c(coef_est[coef_name], ci_est[coef_name, , drop = TRUE])), |
315 | 18x |
nm = c("est", "lcl", "ucl") |
316 |
)
|
|
317 |
}
|
|
318 | 16x |
list(or_ci = or_ci, n_tot = c(n_tot = model_fit$n)) |
319 |
}
|
1 |
#' Counting Missed Doses
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' These are specific functions to count patients with missed doses. The difference to [count_cumulative()] is
|
|
6 |
#' mainly the special labels.
|
|
7 |
#'
|
|
8 |
#' @inheritParams argument_convention
|
|
9 |
#'
|
|
10 |
#' @seealso Relevant description function [d_count_missed_doses()].
|
|
11 |
#'
|
|
12 |
#' @name count_missed_doses
|
|
13 |
NULL
|
|
14 | ||
15 |
#' @describeIn count_missed_doses Statistics function to count non-missing values.
|
|
16 |
#'
|
|
17 |
#' @return
|
|
18 |
#' * `s_count_nonmissing()` returns the statistic `n` which is the count of non-missing values in `x`.
|
|
19 |
#'
|
|
20 |
#' @examples
|
|
21 |
#' set.seed(1)
|
|
22 |
#' x <- c(sample(1:10, 10), NA)
|
|
23 |
#'
|
|
24 |
#' @keywords internal
|
|
25 |
s_count_nonmissing <- function(x) { |
|
26 | 5x |
list(n = n_available(x)) |
27 |
}
|
|
28 | ||
29 |
#' Description Function that Calculates Labels for [s_count_missed_doses()].
|
|
30 |
#'
|
|
31 |
#' @description `r lifecycle::badge("stable")`
|
|
32 |
#'
|
|
33 |
#' @inheritParams s_count_missed_doses
|
|
34 |
#'
|
|
35 |
#' @return [d_count_missed_doses()] returns a named `character` vector with the labels.
|
|
36 |
#'
|
|
37 |
#' @seealso [s_count_missed_doses()]
|
|
38 |
#'
|
|
39 |
#' @export
|
|
40 |
d_count_missed_doses <- function(thresholds) { |
|
41 | 4x |
paste0("At least ", thresholds, " missed dose", ifelse(thresholds > 1, "s", "")) |
42 |
}
|
|
43 | ||
44 |
#' @describeIn count_missed_doses Statistics function to count patients with missed doses.
|
|
45 |
#'
|
|
46 |
#' @param thresholds (vector of `count`)\cr number of missed doses the patients at least had.
|
|
47 |
#'
|
|
48 |
#' @return
|
|
49 |
#' * `s_count_missed_doses()` returns the statistics `n` and `count_fraction` with one element for each threshold.
|
|
50 |
#'
|
|
51 |
#' @keywords internal
|
|
52 |
s_count_missed_doses <- function(x, |
|
53 |
thresholds,
|
|
54 |
.N_col) { # nolint |
|
55 | 1x |
stat <- s_count_cumulative( |
56 | 1x |
x = x, |
57 | 1x |
thresholds = thresholds, |
58 | 1x |
lower_tail = FALSE, |
59 | 1x |
include_eq = TRUE, |
60 | 1x |
.N_col = .N_col |
61 |
)
|
|
62 | 1x |
labels <- d_count_missed_doses(thresholds) |
63 | 1x |
for (i in seq_along(stat$count_fraction)) { |
64 | 2x |
stat$count_fraction[[i]] <- formatters::with_label(stat$count_fraction[[i]], label = labels[i]) |
65 |
}
|
|
66 | 1x |
n_stat <- s_count_nonmissing(x) |
67 | 1x |
c(n_stat, stat) |
68 |
}
|
|
69 | ||
70 |
#' @describeIn count_missed_doses Formatted analysis function which is used as `afun`
|
|
71 |
#' in `count_missed_doses()`.
|
|
72 |
#'
|
|
73 |
#' @return
|
|
74 |
#' * `a_count_missed_doses()` returns the corresponding list with formatted [rtables::CellValue()].
|
|
75 |
#'
|
|
76 |
#' @keywords internal
|
|
77 |
a_count_missed_doses <- make_afun( |
|
78 |
s_count_missed_doses,
|
|
79 |
.formats = c(n = "xx", count_fraction = format_count_fraction) |
|
80 |
)
|
|
81 | ||
82 |
#' @describeIn count_missed_doses Layout-creating function which can take statistics function arguments
|
|
83 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()].
|
|
84 |
#'
|
|
85 |
#' @inheritParams s_count_cumulative
|
|
86 |
#'
|
|
87 |
#' @return
|
|
88 |
#' * `count_missed_doses()` returns a layout object suitable for passing to further layouting functions,
|
|
89 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing
|
|
90 |
#' the statistics from `s_count_missed_doses()` to the table layout.
|
|
91 |
#'
|
|
92 |
#' @examples
|
|
93 |
#' library(dplyr)
|
|
94 |
#'
|
|
95 |
#' anl <- tern_ex_adsl %>%
|
|
96 |
#' distinct(STUDYID, USUBJID, ARM) %>%
|
|
97 |
#' mutate(
|
|
98 |
#' PARAMCD = "TNDOSMIS",
|
|
99 |
#' PARAM = "Total number of missed doses during study",
|
|
100 |
#' AVAL = sample(0:20, size = nrow(tern_ex_adsl), replace = TRUE),
|
|
101 |
#' AVALC = ""
|
|
102 |
#' )
|
|
103 |
#'
|
|
104 |
#' basic_table() %>%
|
|
105 |
#' split_cols_by("ARM") %>%
|
|
106 |
#' add_colcounts() %>%
|
|
107 |
#' count_missed_doses("AVAL", thresholds = c(1, 5, 10, 15), var_labels = "Missed Doses") %>%
|
|
108 |
#' build_table(anl, alt_counts_df = tern_ex_adsl)
|
|
109 |
#'
|
|
110 |
#' @export
|
|
111 |
count_missed_doses <- function(lyt, |
|
112 |
vars,
|
|
113 |
var_labels = vars, |
|
114 |
show_labels = "visible", |
|
115 |
...,
|
|
116 |
table_names = vars, |
|
117 |
.stats = NULL, |
|
118 |
.formats = NULL, |
|
119 |
.labels = NULL, |
|
120 |
.indent_mods = NULL) { |
|
121 | 1x |
afun <- make_afun( |
122 | 1x |
a_count_missed_doses,
|
123 | 1x |
.stats = .stats, |
124 | 1x |
.formats = .formats, |
125 | 1x |
.labels = .labels, |
126 | 1x |
.indent_mods = .indent_mods, |
127 | 1x |
.ungroup_stats = "count_fraction" |
128 |
)
|
|
129 | 1x |
analyze( |
130 | 1x |
lyt = lyt, |
131 | 1x |
vars = vars, |
132 | 1x |
afun = afun, |
133 | 1x |
var_labels = var_labels, |
134 | 1x |
table_names = table_names, |
135 | 1x |
show_labels = show_labels, |
136 | 1x |
extra_args = list(...) |
137 |
)
|
|
138 |
}
|
1 |
#' Compare Variables Between Groups
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Comparison with a reference group for different `x` objects.
|
|
6 |
#'
|
|
7 |
#' @inheritParams argument_convention
|
|
8 |
#'
|
|
9 |
#' @note
|
|
10 |
#' * For factor variables, `denom` for factor proportions can only be `n` since the purpose is to compare proportions
|
|
11 |
#' between columns, therefore a row-based proportion would not make sense. Proportion based on `N_col` would
|
|
12 |
#' be difficult since we use counts for the chi-squared test statistic, therefore missing values should be accounted
|
|
13 |
#' for as explicit factor levels.
|
|
14 |
#' * If factor variables contain `NA`, these `NA` values are excluded by default. To include `NA` values
|
|
15 |
#' set `na.rm = FALSE` and missing values will be displayed as an `NA` level. Alternatively, an explicit
|
|
16 |
#' factor level can be defined for `NA` values during pre-processing via [df_explicit_na()] - the
|
|
17 |
#' default `na_level` (`"<Missing>"`) will also be excluded when `na.rm` is set to `TRUE`.
|
|
18 |
#' * For character variables, automatic conversion to factor does not guarantee that the table
|
|
19 |
#' will be generated correctly. In particular for sparse tables this very likely can fail.
|
|
20 |
#' Therefore it is always better to manually convert character variables to factors during pre-processing.
|
|
21 |
#' * For `compare_vars()`, the column split must define a reference group via `ref_group` so that the comparison
|
|
22 |
#' is well defined.
|
|
23 |
#'
|
|
24 |
#' @seealso Relevant constructor function [create_afun_compare()], and [s_summary()] which is used internally
|
|
25 |
#' to compute a summary within `s_compare()`.
|
|
26 |
#'
|
|
27 |
#' @name compare_variables
|
|
28 |
#' @include summarize_variables.R
|
|
29 |
NULL
|
|
30 | ||
31 |
#' @describeIn compare_variables S3 generic function to produce a comparison summary.
|
|
32 |
#'
|
|
33 |
#' @return
|
|
34 |
#' * `s_compare()` returns output of [s_summary()] and comparisons versus the reference group in the form of p-values.
|
|
35 |
#'
|
|
36 |
#' @export
|
|
37 |
s_compare <- function(x, |
|
38 |
.ref_group,
|
|
39 |
.in_ref_col,
|
|
40 |
...) { |
|
41 | 9x |
UseMethod("s_compare", x) |
42 |
}
|
|
43 | ||
44 |
#' @describeIn compare_variables Method for `numeric` class. This uses the standard t-test
|
|
45 |
#' to calculate the p-value.
|
|
46 |
#'
|
|
47 |
#' @method s_compare numeric
|
|
48 |
#'
|
|
49 |
#' @examples
|
|
50 |
#' # `s_compare.numeric`
|
|
51 |
#'
|
|
52 |
#' ## Usual case where both this and the reference group vector have more than 1 value.
|
|
53 |
#' s_compare(rnorm(10, 5, 1), .ref_group = rnorm(5, -5, 1), .in_ref_col = FALSE)
|
|
54 |
#'
|
|
55 |
#' ## If one group has not more than 1 value, then p-value is not calculated.
|
|
56 |
#' s_compare(rnorm(10, 5, 1), .ref_group = 1, .in_ref_col = FALSE)
|
|
57 |
#'
|
|
58 |
#' ## Empty numeric does not fail, it returns NA-filled items and no p-value.
|
|
59 |
#' s_compare(numeric(), .ref_group = numeric(), .in_ref_col = FALSE)
|
|
60 |
#'
|
|
61 |
#' @export
|
|
62 |
s_compare.numeric <- function(x, |
|
63 |
.ref_group,
|
|
64 |
.in_ref_col,
|
|
65 |
...) { |
|
66 | 2x |
checkmate::assert_numeric(x) |
67 | 2x |
checkmate::assert_numeric(.ref_group) |
68 | 2x |
checkmate::assert_flag(.in_ref_col) |
69 | ||
70 | 2x |
y <- s_summary.numeric(x = x, ...) |
71 | ||
72 | 2x |
y$pval <- if (!.in_ref_col && n_available(x) > 1 && n_available(.ref_group) > 1) { |
73 | 1x |
stats::t.test(x, .ref_group)$p.value |
74 |
} else { |
|
75 | 1x |
character() |
76 |
}
|
|
77 | ||
78 | 2x |
y
|
79 |
}
|
|
80 | ||
81 |
#' @describeIn compare_variables Method for `factor` class. This uses the chi-squared test
|
|
82 |
#' to calculate the p-value.
|
|
83 |
#'
|
|
84 |
#' @param denom (`string`)\cr choice of denominator for factor proportions,
|
|
85 |
#' can only be `n` (number of values in this row and column intersection).
|
|
86 |
#'
|
|
87 |
#' @method s_compare factor
|
|
88 |
#'
|
|
89 |
#' @examples
|
|
90 |
#' # `s_compare.factor`
|
|
91 |
#'
|
|
92 |
#' ## Basic usage:
|
|
93 |
#' x <- factor(c("a", "a", "b", "c", "a"))
|
|
94 |
#' y <- factor(c("a", "b", "c"))
|
|
95 |
#' s_compare(x = x, .ref_group = y, .in_ref_col = FALSE)
|
|
96 |
#'
|
|
97 |
#' ## Management of NA values.
|
|
98 |
#' x <- explicit_na(factor(c("a", "a", "b", "c", "a", NA, NA)))
|
|
99 |
#' y <- explicit_na(factor(c("a", "b", "c", NA)))
|
|
100 |
#' s_compare(x = x, .ref_group = y, .in_ref_col = FALSE, na.rm = TRUE)
|
|
101 |
#' s_compare(x = x, .ref_group = y, .in_ref_col = FALSE, na.rm = FALSE)
|
|
102 |
#'
|
|
103 |
#' @export
|
|
104 |
s_compare.factor <- function(x, |
|
105 |
.ref_group,
|
|
106 |
.in_ref_col,
|
|
107 |
denom = "n", |
|
108 |
na.rm = TRUE, # nolint |
|
109 |
...) { |
|
110 | 3x |
checkmate::assert_flag(.in_ref_col) |
111 | 3x |
assert_valid_factor(x) |
112 | 3x |
assert_valid_factor(.ref_group) |
113 | 3x |
denom <- match.arg(denom) |
114 | ||
115 | 3x |
y <- s_summary.factor( |
116 | 3x |
x = x, |
117 | 3x |
denom = denom, |
118 | 3x |
na.rm = na.rm, |
119 |
...
|
|
120 |
)
|
|
121 | ||
122 | 3x |
if (na.rm) { |
123 | 3x |
x <- x[!is.na(x)] %>% fct_discard("<Missing>") |
124 | 3x |
.ref_group <- .ref_group[!is.na(.ref_group)] %>% fct_discard("<Missing>") |
125 |
} else { |
|
126 | ! |
x <- x %>% explicit_na(label = "NA") |
127 | ! |
.ref_group <- .ref_group %>% explicit_na(label = "NA") |
128 |
}
|
|
129 | ||
130 | 3x |
checkmate::assert_factor(x, levels = levels(.ref_group), min.levels = 2) |
131 | ||
132 | 3x |
y$pval <- if (!.in_ref_col && length(x) > 0 && length(.ref_group) > 0) { |
133 | 3x |
tab <- rbind(table(x), table(.ref_group)) |
134 | 3x |
res <- suppressWarnings(stats::chisq.test(tab)) |
135 | 3x |
res$p.value |
136 |
} else { |
|
137 | ! |
character() |
138 |
}
|
|
139 | ||
140 | 3x |
y
|
141 |
}
|
|
142 | ||
143 |
#' @describeIn compare_variables Method for `character` class. This makes an automatic
|
|
144 |
#' conversion to `factor` (with a warning) and then forwards to the method for factors.
|
|
145 |
#'
|
|
146 |
#' @param verbose (`logical`)\cr Whether warnings and messages should be printed. Mainly used
|
|
147 |
#' to print out information about factor casting. Defaults to `TRUE`.
|
|
148 |
#'
|
|
149 |
#' @method s_compare character
|
|
150 |
#'
|
|
151 |
#' @examples
|
|
152 |
#' # `s_compare.character`
|
|
153 |
#'
|
|
154 |
#' ## Basic usage:
|
|
155 |
#' x <- c("a", "a", "b", "c", "a")
|
|
156 |
#' y <- c("a", "b", "c")
|
|
157 |
#' s_compare(x, .ref_group = y, .in_ref_col = FALSE, .var = "x", verbose = FALSE)
|
|
158 |
#'
|
|
159 |
#' ## Note that missing values handling can make a large difference:
|
|
160 |
#' x <- c("a", "a", "b", "c", "a", NA)
|
|
161 |
#' y <- c("a", "b", "c", rep(NA, 20))
|
|
162 |
#' s_compare(x,
|
|
163 |
#' .ref_group = y, .in_ref_col = FALSE,
|
|
164 |
#' .var = "x", verbose = FALSE
|
|
165 |
#' )
|
|
166 |
#' s_compare(x,
|
|
167 |
#' .ref_group = y, .in_ref_col = FALSE, .var = "x",
|
|
168 |
#' na.rm = FALSE, verbose = FALSE
|
|
169 |
#' )
|
|
170 |
#'
|
|
171 |
#' @export
|
|
172 |
s_compare.character <- function(x, |
|
173 |
.ref_group,
|
|
174 |
.in_ref_col,
|
|
175 |
denom = "n", |
|
176 |
na.rm = TRUE, # nolint |
|
177 |
.var,
|
|
178 |
verbose = TRUE, |
|
179 |
...) { |
|
180 | 1x |
x <- as_factor_keep_attributes(x, x_name = .var, verbose = verbose) |
181 | 1x |
.ref_group <- as_factor_keep_attributes(.ref_group, x_name = .var, verbose = verbose) |
182 | 1x |
s_compare( |
183 | 1x |
x = x, |
184 | 1x |
.ref_group = .ref_group, |
185 | 1x |
.in_ref_col = .in_ref_col, |
186 | 1x |
denom = denom, |
187 | 1x |
na.rm = na.rm, |
188 |
...
|
|
189 |
)
|
|
190 |
}
|
|
191 | ||
192 |
#' @describeIn compare_variables Method for `logical` class. A chi-squared test
|
|
193 |
#' is used. If missing values are not removed, then they are counted as `FALSE`.
|
|
194 |
#'
|
|
195 |
#' @method s_compare logical
|
|
196 |
#'
|
|
197 |
#' @examples
|
|
198 |
#' # `s_compare.logical`
|
|
199 |
#'
|
|
200 |
#' ## Basic usage:
|
|
201 |
#' x <- c(TRUE, FALSE, TRUE, TRUE)
|
|
202 |
#' y <- c(FALSE, FALSE, TRUE)
|
|
203 |
#' s_compare(x, .ref_group = y, .in_ref_col = FALSE)
|
|
204 |
#'
|
|
205 |
#' ## Management of NA values.
|
|
206 |
#' x <- c(NA, TRUE, FALSE)
|
|
207 |
#' y <- c(NA, NA, NA, NA, FALSE)
|
|
208 |
#' s_compare(x, .ref_group = y, .in_ref_col = FALSE, na.rm = TRUE)
|
|
209 |
#' s_compare(x, .ref_group = y, .in_ref_col = FALSE, na.rm = FALSE)
|
|
210 |
#'
|
|
211 |
#' @export
|
|
212 |
s_compare.logical <- function(x, |
|
213 |
.ref_group,
|
|
214 |
.in_ref_col,
|
|
215 |
na.rm = TRUE, # nolint |
|
216 |
denom = "n", |
|
217 |
...) { |
|
218 | 3x |
denom <- match.arg(denom) |
219 | ||
220 | 3x |
y <- s_summary.logical( |
221 | 3x |
x = x, |
222 | 3x |
na.rm = na.rm, |
223 | 3x |
denom = denom, |
224 |
...
|
|
225 |
)
|
|
226 | ||
227 | 3x |
if (na.rm) { |
228 | 2x |
x <- stats::na.omit(x) |
229 | 2x |
.ref_group <- stats::na.omit(.ref_group) |
230 |
} else { |
|
231 | 1x |
x[is.na(x)] <- FALSE |
232 | 1x |
.ref_group[is.na(.ref_group)] <- FALSE |
233 |
}
|
|
234 | ||
235 | 3x |
y$pval <- if (!.in_ref_col && length(x) > 0 && length(.ref_group) > 0) { |
236 | 3x |
x <- factor(x, levels = c(TRUE, FALSE)) |
237 | 3x |
.ref_group <- factor(.ref_group, levels = c(TRUE, FALSE)) |
238 | 3x |
tbl <- rbind(table(x), table(.ref_group)) |
239 | 3x |
suppressWarnings(prop_chisq(tbl)) |
240 |
} else { |
|
241 | ! |
character() |
242 |
}
|
|
243 | ||
244 | 3x |
y
|
245 |
}
|
|
246 | ||
247 |
#' @describeIn compare_variables Formatted analysis function which is used as `afun`
|
|
248 |
#' in `compare_vars()`.
|
|
249 |
#'
|
|
250 |
#' @return
|
|
251 |
#' * `a_compare()` returns the corresponding list with formatted [rtables::CellValue()].
|
|
252 |
#'
|
|
253 |
#' @export
|
|
254 |
a_compare <- function(x, |
|
255 |
.ref_group,
|
|
256 |
.in_ref_col,
|
|
257 |
...,
|
|
258 |
.var) { |
|
259 | ! |
UseMethod("a_compare", x) |
260 |
}
|
|
261 | ||
262 |
#' @describeIn compare_variables Formatted analysis function method for `numeric` class.
|
|
263 |
#'
|
|
264 |
#' @examples
|
|
265 |
#' # `a_compare.numeric`
|
|
266 |
#' a_compare(
|
|
267 |
#' rnorm(10, 5, 1),
|
|
268 |
#' .ref_group = rnorm(20, -5, 1),
|
|
269 |
#' .in_ref_col = FALSE,
|
|
270 |
#' .var = "bla"
|
|
271 |
#' )
|
|
272 |
#'
|
|
273 |
#' @export
|
|
274 |
a_compare.numeric <- make_afun( |
|
275 |
s_compare.numeric,
|
|
276 |
.formats = c( |
|
277 |
.a_summary_numeric_formats,
|
|
278 |
pval = "x.xxxx | (<0.0001)" |
|
279 |
),
|
|
280 |
.labels = c( |
|
281 |
.a_summary_numeric_labels,
|
|
282 |
pval = "p-value (t-test)" |
|
283 |
),
|
|
284 |
.null_ref_cells = FALSE |
|
285 |
)
|
|
286 | ||
287 |
.a_compare_counts_formats <- c( |
|
288 |
.a_summary_counts_formats,
|
|
289 |
pval = "x.xxxx | (<0.0001)" |
|
290 |
)
|
|
291 | ||
292 |
.a_compare_counts_labels <- c( |
|
293 |
pval = "p-value (chi-squared test)" |
|
294 |
)
|
|
295 | ||
296 |
#' @describeIn compare_variables Formatted analysis function method for `factor` class.
|
|
297 |
#'
|
|
298 |
#' @examples
|
|
299 |
#' # `a_compare.factor`
|
|
300 |
#' # We need to ungroup `count` and `count_fraction` first so that the `rtables` formatting
|
|
301 |
#' # functions can be applied correctly.
|
|
302 |
#' afun <- make_afun(
|
|
303 |
#' getS3method("a_compare", "factor"),
|
|
304 |
#' .ungroup_stats = c("count", "count_fraction")
|
|
305 |
#' )
|
|
306 |
#' x <- factor(c("a", "a", "b", "c", "a"))
|
|
307 |
#' y <- factor(c("a", "a", "b", "c"))
|
|
308 |
#' afun(x, .ref_group = y, .in_ref_col = FALSE)
|
|
309 |
#'
|
|
310 |
#' @export
|
|
311 |
a_compare.factor <- make_afun( |
|
312 |
s_compare.factor,
|
|
313 |
.formats = .a_compare_counts_formats, |
|
314 |
.labels = .a_compare_counts_labels, |
|
315 |
.null_ref_cells = FALSE |
|
316 |
)
|
|
317 | ||
318 |
#' @describeIn compare_variables Formatted analysis function method for `character` class.
|
|
319 |
#'
|
|
320 |
#' @examples
|
|
321 |
#' # `a_compare.character`
|
|
322 |
#' afun <- make_afun(
|
|
323 |
#' getS3method("a_compare", "character"),
|
|
324 |
#' .ungroup_stats = c("count", "count_fraction")
|
|
325 |
#' )
|
|
326 |
#' x <- c("A", "B", "A", "C")
|
|
327 |
#' y <- c("B", "A", "C")
|
|
328 |
#' afun(x, .ref_group = y, .in_ref_col = FALSE, .var = "x", verbose = FALSE)
|
|
329 |
#'
|
|
330 |
#' @export
|
|
331 |
a_compare.character <- make_afun( |
|
332 |
s_compare.character,
|
|
333 |
.formats = .a_compare_counts_formats, |
|
334 |
.labels = .a_compare_counts_labels, |
|
335 |
.null_ref_cells = FALSE |
|
336 |
)
|
|
337 | ||
338 |
#' @describeIn compare_variables Formatted analysis function method for `logical` class.
|
|
339 |
#'
|
|
340 |
#' @examples
|
|
341 |
#' # `a_compare.logical`
|
|
342 |
#' afun <- make_afun(
|
|
343 |
#' getS3method("a_compare", "logical")
|
|
344 |
#' )
|
|
345 |
#' x <- c(TRUE, FALSE, FALSE, TRUE, TRUE)
|
|
346 |
#' y <- c(TRUE, FALSE)
|
|
347 |
#' afun(x, .ref_group = y, .in_ref_col = FALSE)
|
|
348 |
#'
|
|
349 |
#' @export
|
|
350 |
a_compare.logical <- make_afun( |
|
351 |
s_compare.logical,
|
|
352 |
.formats = .a_compare_counts_formats, |
|
353 |
.labels = .a_compare_counts_labels, |
|
354 |
.null_ref_cells = FALSE |
|
355 |
)
|
|
356 | ||
357 |
#' Constructor Function for [compare_vars()]
|
|
358 |
#'
|
|
359 |
#' @description `r lifecycle::badge("stable")`
|
|
360 |
#'
|
|
361 |
#' Constructor function which creates a combined formatted analysis function.
|
|
362 |
#'
|
|
363 |
#' @inheritParams argument_convention
|
|
364 |
#' @param .indent_mods (named `vector` of `integer`)\cr indent modifiers for the labels. Each element of the vector
|
|
365 |
#' should be a name-value pair with name corresponding to a statistic specified in `.stats` and value the indentation
|
|
366 |
#' for that statistic's row label.
|
|
367 |
#'
|
|
368 |
#' @return Combined formatted analysis function for use in [compare_vars()].
|
|
369 |
#'
|
|
370 |
#' @note Since [a_compare()] is generic and we want customization of the formatting arguments
|
|
371 |
#' via [rtables::make_afun()], we need to create another temporary generic function, with
|
|
372 |
#' corresponding customized methods. Then in order for the methods to be found,
|
|
373 |
#' we need to wrap them in a combined `afun`. Since this is required by two layout creating
|
|
374 |
#' functions (and possibly others in the future), we provide a constructor that does this:
|
|
375 |
#' [create_afun_compare()].
|
|
376 |
#'
|
|
377 |
#' @seealso [compare_vars()]
|
|
378 |
#'
|
|
379 |
#' @examples
|
|
380 |
#' # `create_afun_compare()` to create combined `afun`
|
|
381 |
#'
|
|
382 |
#' afun <- create_afun_compare(
|
|
383 |
#' .stats = c("n", "count_fraction", "mean_sd", "pval"),
|
|
384 |
#' .indent_mods = c(pval = 1L)
|
|
385 |
#' )
|
|
386 |
#'
|
|
387 |
#' lyt <- basic_table() %>%
|
|
388 |
#' split_cols_by("ARMCD", ref_group = "ARM A") %>%
|
|
389 |
#' analyze(
|
|
390 |
#' "AGE",
|
|
391 |
#' afun = afun,
|
|
392 |
#' show_labels = "visible"
|
|
393 |
#' )
|
|
394 |
#' build_table(lyt, df = tern_ex_adsl)
|
|
395 |
#'
|
|
396 |
#' lyt <- basic_table() %>%
|
|
397 |
#' split_cols_by("ARMCD", ref_group = "ARM A") %>%
|
|
398 |
#' analyze(
|
|
399 |
#' "SEX",
|
|
400 |
#' afun = afun,
|
|
401 |
#' show_labels = "visible"
|
|
402 |
#' )
|
|
403 |
#' build_table(lyt, df = tern_ex_adsl)
|
|
404 |
#'
|
|
405 |
#' @export
|
|
406 |
create_afun_compare <- function(.stats = NULL, |
|
407 |
.formats = NULL, |
|
408 |
.labels = NULL, |
|
409 |
.indent_mods = NULL) { |
|
410 | 3x |
function(x, |
411 | 3x |
.ref_group,
|
412 | 3x |
.in_ref_col,
|
413 |
...,
|
|
414 | 3x |
.var) { |
415 | 15x |
afun <- function(x, ...) { |
416 | 15x |
UseMethod("afun", x) |
417 |
}
|
|
418 | ||
419 | 15x |
numeric_stats <- afun_selected_stats( |
420 | 15x |
.stats,
|
421 | 15x |
all_stats = c(names(.a_summary_numeric_formats), "pval") |
422 |
)
|
|
423 | 15x |
afun.numeric <- make_afun( # nolint |
424 | 15x |
a_compare.numeric,
|
425 | 15x |
.stats = numeric_stats, |
426 | 15x |
.formats = extract_by_name(.formats, numeric_stats), |
427 | 15x |
.labels = extract_by_name(.labels, numeric_stats), |
428 | 15x |
.indent_mods = extract_by_name(.indent_mods, numeric_stats), |
429 | 15x |
.null_ref_cells = FALSE |
430 |
)
|
|
431 | ||
432 | 15x |
factor_stats <- afun_selected_stats( |
433 | 15x |
.stats,
|
434 | 15x |
all_stats = names(.a_compare_counts_formats) |
435 |
)
|
|
436 | 15x |
ungroup_stats <- afun_selected_stats(.stats, c("count", "count_fraction")) |
437 | 15x |
afun.factor <- make_afun( # nolint |
438 | 15x |
a_compare.factor,
|
439 | 15x |
.stats = factor_stats, |
440 | 15x |
.formats = extract_by_name(.formats, factor_stats), |
441 | 15x |
.labels = extract_by_name(.labels, factor_stats), |
442 | 15x |
.indent_mods = extract_by_name(.indent_mods, factor_stats), |
443 | 15x |
.ungroup_stats = ungroup_stats, |
444 | 15x |
.null_ref_cells = FALSE |
445 |
)
|
|
446 | ||
447 | 15x |
afun.character <- make_afun( # nolint |
448 | 15x |
a_compare.character,
|
449 | 15x |
.stats = factor_stats, |
450 | 15x |
.formats = extract_by_name(.formats, factor_stats), |
451 | 15x |
.labels = extract_by_name(.labels, factor_stats), |
452 | 15x |
.indent_mods = extract_by_name(.indent_mods, factor_stats), |
453 | 15x |
.ungroup_stats = ungroup_stats, |
454 | 15x |
.null_ref_cells = FALSE |
455 |
)
|
|
456 | ||
457 | 15x |
afun.logical <- make_afun( # nolint |
458 | 15x |
a_compare.logical,
|
459 | 15x |
.stats = factor_stats, |
460 | 15x |
.formats = extract_by_name(.formats, factor_stats), |
461 | 15x |
.labels = extract_by_name(.labels, factor_stats), |
462 | 15x |
.indent_mods = extract_by_name(.indent_mods, factor_stats), |
463 | 15x |
.null_ref_cells = FALSE |
464 |
)
|
|
465 | ||
466 | 15x |
afun( |
467 | 15x |
x = x, |
468 | 15x |
.ref_group = .ref_group, |
469 | 15x |
.in_ref_col = .in_ref_col, |
470 |
...,
|
|
471 | 15x |
.var = .var |
472 |
)
|
|
473 |
}
|
|
474 |
}
|
|
475 | ||
476 |
#' @describeIn compare_variables Layout-creating function which can take statistics function arguments
|
|
477 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()].
|
|
478 |
#'
|
|
479 |
#' @param ... arguments passed to `s_compare()`.
|
|
480 |
#' @param .indent_mods (named `vector` of `integer`)\cr indent modifiers for the labels. Each element of the vector
|
|
481 |
#' should be a name-value pair with name corresponding to a statistic specified in `.stats` and value the indentation
|
|
482 |
#' for that statistic's row label.
|
|
483 |
#'
|
|
484 |
#' @return
|
|
485 |
#' * `compare_vars()` returns a layout object suitable for passing to further layouting functions,
|
|
486 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing
|
|
487 |
#' the statistics from `s_compare()` to the table layout.
|
|
488 |
#'
|
|
489 |
#' @examples
|
|
490 |
#' # `compare_vars()` in `rtables` pipelines
|
|
491 |
#'
|
|
492 |
#' ## Default output within a `rtables` pipeline.
|
|
493 |
#' lyt <- basic_table() %>%
|
|
494 |
#' split_cols_by("ARMCD", ref_group = "ARM B") %>%
|
|
495 |
#' compare_vars(c("AGE", "SEX"))
|
|
496 |
#' build_table(lyt, tern_ex_adsl)
|
|
497 |
#'
|
|
498 |
#' ## Select and format statistics output.
|
|
499 |
#' lyt <- basic_table() %>%
|
|
500 |
#' split_cols_by("ARMCD", ref_group = "ARM C") %>%
|
|
501 |
#' compare_vars(
|
|
502 |
#' vars = "AGE",
|
|
503 |
#' .stats = c("mean_sd", "pval"),
|
|
504 |
#' .formats = c(mean_sd = "xx.x, xx.x"),
|
|
505 |
#' .labels = c(mean_sd = "Mean, SD")
|
|
506 |
#' )
|
|
507 |
#' build_table(lyt, df = tern_ex_adsl)
|
|
508 |
#'
|
|
509 |
#' @export
|
|
510 |
compare_vars <- function(lyt, |
|
511 |
vars,
|
|
512 |
var_labels = vars, |
|
513 |
nested = TRUE, |
|
514 |
...,
|
|
515 |
na_level = NA_character_, |
|
516 |
show_labels = "default", |
|
517 |
table_names = vars, |
|
518 |
.stats = c("n", "mean_sd", "count_fraction", "pval"), |
|
519 |
.formats = NULL, |
|
520 |
.labels = NULL, |
|
521 |
.indent_mods = NULL) { |
|
522 | 3x |
afun <- create_afun_compare(.stats, .formats, .labels, .indent_mods) |
523 | ||
524 | 3x |
analyze( |
525 | 3x |
lyt = lyt, |
526 | 3x |
vars = vars, |
527 | 3x |
var_labels = var_labels, |
528 | 3x |
afun = afun, |
529 | 3x |
nested = nested, |
530 | 3x |
extra_args = list(...), |
531 | 3x |
na_str = na_level, |
532 | 3x |
inclNAs = TRUE, |
533 | 3x |
show_labels = show_labels, |
534 | 3x |
table_names = table_names |
535 |
)
|
|
536 |
}
|
1 |
#' Stack Multiple Grobs
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Stack grobs as a new grob with 1 column and multiple rows layout.
|
|
6 |
#'
|
|
7 |
#' @param ... grobs.
|
|
8 |
#' @param grobs list of grobs.
|
|
9 |
#' @param padding unit of length 1, space between each grob.
|
|
10 |
#' @param vp a [viewport()] object (or `NULL`).
|
|
11 |
#' @param name a character identifier for the grob.
|
|
12 |
#' @param gp A [gpar()] object.
|
|
13 |
#'
|
|
14 |
#' @return A `grob`.
|
|
15 |
#'
|
|
16 |
#' @examples
|
|
17 |
#' library(grid)
|
|
18 |
#'
|
|
19 |
#' g1 <- circleGrob(gp = gpar(col = "blue"))
|
|
20 |
#' g2 <- circleGrob(gp = gpar(col = "red"))
|
|
21 |
#' g3 <- textGrob("TEST TEXT")
|
|
22 |
#' grid.newpage()
|
|
23 |
#' grid.draw(stack_grobs(g1, g2, g3))
|
|
24 |
#'
|
|
25 |
#' showViewport()
|
|
26 |
#'
|
|
27 |
#' grid.newpage()
|
|
28 |
#' pushViewport(viewport(layout = grid.layout(1, 2)))
|
|
29 |
#' vp1 <- viewport(layout.pos.row = 1, layout.pos.col = 2)
|
|
30 |
#' grid.draw(stack_grobs(g1, g2, g3, vp = vp1, name = "test"))
|
|
31 |
#'
|
|
32 |
#' showViewport()
|
|
33 |
#' grid.ls(grobs = TRUE, viewports = TRUE, print = FALSE)
|
|
34 |
#'
|
|
35 |
#' @export
|
|
36 |
stack_grobs <- function(..., |
|
37 |
grobs = list(...), |
|
38 |
padding = grid::unit(2, "line"), |
|
39 |
vp = NULL, |
|
40 |
gp = NULL, |
|
41 |
name = NULL) { |
|
42 | 4x |
checkmate::assert_true( |
43 | 4x |
all(vapply(grobs, grid::is.grob, logical(1))) |
44 |
)
|
|
45 | ||
46 | 4x |
if (length(grobs) == 1) { |
47 | 1x |
return(grobs[[1]]) |
48 |
}
|
|
49 | ||
50 | 3x |
n_layout <- 2 * length(grobs) - 1 |
51 | 3x |
hts <- lapply( |
52 | 3x |
seq(1, n_layout), |
53 | 3x |
function(i) { |
54 | 39x |
if (i %% 2 != 0) { |
55 | 21x |
grid::unit(1, "null") |
56 |
} else { |
|
57 | 18x |
padding
|
58 |
}
|
|
59 |
}
|
|
60 |
)
|
|
61 | 3x |
hts <- do.call(grid::unit.c, hts) |
62 | ||
63 | 3x |
main_vp <- grid::viewport( |
64 | 3x |
layout = grid::grid.layout(nrow = n_layout, ncol = 1, heights = hts) |
65 |
)
|
|
66 | ||
67 | 3x |
nested_grobs <- Map(function(g, i) { |
68 | 21x |
grid::gTree( |
69 | 21x |
children = grid::gList(g), |
70 | 21x |
vp = grid::viewport(layout.pos.row = i, layout.pos.col = 1) |
71 |
)
|
|
72 | 3x |
}, grobs, seq_along(grobs) * 2 - 1) |
73 | ||
74 | 3x |
grobs_mainvp <- grid::gTree( |
75 | 3x |
children = do.call(grid::gList, nested_grobs), |
76 | 3x |
vp = main_vp |
77 |
)
|
|
78 | ||
79 | 3x |
grid::gTree( |
80 | 3x |
children = grid::gList(grobs_mainvp), |
81 | 3x |
vp = vp, |
82 | 3x |
gp = gp, |
83 | 3x |
name = name |
84 |
)
|
|
85 |
}
|
|
86 | ||
87 |
#' Arrange Multiple Grobs
|
|
88 |
#'
|
|
89 |
#' Arrange grobs as a new grob with \verb{n*m (rows*cols)} layout.
|
|
90 |
#'
|
|
91 |
#' @inheritParams stack_grobs
|
|
92 |
#' @param ncol number of columns in layout.
|
|
93 |
#' @param nrow number of rows in layout.
|
|
94 |
#' @param padding_ht unit of length 1, vertical space between each grob.
|
|
95 |
#' @param padding_wt unit of length 1, horizontal space between each grob.
|
|
96 |
#'
|
|
97 |
#' @return A `grob`.
|
|
98 |
#' @examples
|
|
99 |
#' library(grid)
|
|
100 |
#'
|
|
101 |
#' \donttest{
|
|
102 |
#' num <- lapply(1:9, textGrob)
|
|
103 |
#' grid::grid.newpage()
|
|
104 |
#' grid.draw(arrange_grobs(grobs = num, ncol = 2))
|
|
105 |
#'
|
|
106 |
#' showViewport()
|
|
107 |
#'
|
|
108 |
#' g1 <- circleGrob(gp = gpar(col = "blue"))
|
|
109 |
#' g2 <- circleGrob(gp = gpar(col = "red"))
|
|
110 |
#' g3 <- textGrob("TEST TEXT")
|
|
111 |
#' grid::grid.newpage()
|
|
112 |
#' grid.draw(arrange_grobs(g1, g2, g3, nrow = 2))
|
|
113 |
#'
|
|
114 |
#' showViewport()
|
|
115 |
#'
|
|
116 |
#' grid::grid.newpage()
|
|
117 |
#' grid.draw(arrange_grobs(g1, g2, g3, ncol = 3))
|
|
118 |
#'
|
|
119 |
#' grid::grid.newpage()
|
|
120 |
#' grid::pushViewport(grid::viewport(layout = grid::grid.layout(1, 2)))
|
|
121 |
#' vp1 <- grid::viewport(layout.pos.row = 1, layout.pos.col = 2)
|
|
122 |
#' grid.draw(arrange_grobs(g1, g2, g3, ncol = 2, vp = vp1))
|
|
123 |
#'
|
|
124 |
#' showViewport()
|
|
125 |
#' }
|
|
126 |
#' @export
|
|
127 |
arrange_grobs <- function(..., |
|
128 |
grobs = list(...), |
|
129 |
ncol = NULL, nrow = NULL, |
|
130 |
padding_ht = grid::unit(2, "line"), |
|
131 |
padding_wt = grid::unit(2, "line"), |
|
132 |
vp = NULL, |
|
133 |
gp = NULL, |
|
134 |
name = NULL) { |
|
135 | 5x |
checkmate::assert_true( |
136 | 5x |
all(vapply(grobs, grid::is.grob, logical(1))) |
137 |
)
|
|
138 | ||
139 | 5x |
if (length(grobs) == 1) { |
140 | 1x |
return(grobs[[1]]) |
141 |
}
|
|
142 | ||
143 | 4x |
if (is.null(ncol) && is.null(nrow)) { |
144 | 1x |
ncol <- 1 |
145 | 1x |
nrow <- ceiling(length(grobs) / ncol) |
146 | 3x |
} else if (!is.null(ncol) && is.null(nrow)) { |
147 | 1x |
nrow <- ceiling(length(grobs) / ncol) |
148 | 2x |
} else if (is.null(ncol) && !is.null(nrow)) { |
149 | ! |
ncol <- ceiling(length(grobs) / nrow) |
150 |
}
|
|
151 | ||
152 | 4x |
if (ncol * nrow < length(grobs)) { |
153 | 1x |
stop("specififed ncol and nrow are not enough for arranging the grobs ") |
154 |
}
|
|
155 | ||
156 | 3x |
if (ncol == 1) { |
157 | 2x |
return(stack_grobs(grobs = grobs, padding = padding_ht, vp = vp, gp = gp, name = name)) |
158 |
}
|
|
159 | ||
160 | 1x |
n_col <- 2 * ncol - 1 |
161 | 1x |
n_row <- 2 * nrow - 1 |
162 | 1x |
hts <- lapply( |
163 | 1x |
seq(1, n_row), |
164 | 1x |
function(i) { |
165 | 5x |
if (i %% 2 != 0) { |
166 | 3x |
grid::unit(1, "null") |
167 |
} else { |
|
168 | 2x |
padding_ht
|
169 |
}
|
|
170 |
}
|
|
171 |
)
|
|
172 | 1x |
hts <- do.call(grid::unit.c, hts) |
173 | ||
174 | 1x |
wts <- lapply( |
175 | 1x |
seq(1, n_col), |
176 | 1x |
function(i) { |
177 | 5x |
if (i %% 2 != 0) { |
178 | 3x |
grid::unit(1, "null") |
179 |
} else { |
|
180 | 2x |
padding_wt
|
181 |
}
|
|
182 |
}
|
|
183 |
)
|
|
184 | 1x |
wts <- do.call(grid::unit.c, wts) |
185 | ||
186 | 1x |
main_vp <- grid::viewport( |
187 | 1x |
layout = grid::grid.layout(nrow = n_row, ncol = n_col, widths = wts, heights = hts) |
188 |
)
|
|
189 | ||
190 | 1x |
nested_grobs <- list() |
191 | 1x |
k <- 0 |
192 | 1x |
for (i in seq(nrow) * 2 - 1) { |
193 | 3x |
for (j in seq(ncol) * 2 - 1) { |
194 | 9x |
k <- k + 1 |
195 | 9x |
if (k <= length(grobs)) { |
196 | 9x |
nested_grobs <- c( |
197 | 9x |
nested_grobs,
|
198 | 9x |
list(grid::gTree( |
199 | 9x |
children = grid::gList(grobs[[k]]), |
200 | 9x |
vp = grid::viewport(layout.pos.row = i, layout.pos.col = j) |
201 |
)) |
|
202 |
)
|
|
203 |
}
|
|
204 |
}
|
|
205 |
}
|
|
206 | 1x |
grobs_mainvp <- grid::gTree( |
207 | 1x |
children = do.call(grid::gList, nested_grobs), |
208 | 1x |
vp = main_vp |
209 |
)
|
|
210 | ||
211 | 1x |
grid::gTree( |
212 | 1x |
children = grid::gList(grobs_mainvp), |
213 | 1x |
vp = vp, |
214 | 1x |
gp = gp, |
215 | 1x |
name = name |
216 |
)
|
|
217 |
}
|
|
218 | ||
219 |
#' Draw `grob`
|
|
220 |
#'
|
|
221 |
#' @description `r lifecycle::badge("stable")`
|
|
222 |
#'
|
|
223 |
#' Draw grob on device page.
|
|
224 |
#'
|
|
225 |
#' @param grob grid object
|
|
226 |
#' @param newpage draw on a new page
|
|
227 |
#' @param vp a [viewport()] object (or `NULL`).
|
|
228 |
#'
|
|
229 |
#' @return A `grob`.
|
|
230 |
#'
|
|
231 |
#' @examples
|
|
232 |
#' library(dplyr)
|
|
233 |
#' library(grid)
|
|
234 |
#'
|
|
235 |
#' \donttest{
|
|
236 |
#' rect <- rectGrob(width = grid::unit(0.5, "npc"), height = grid::unit(0.5, "npc"))
|
|
237 |
#' rect %>% draw_grob(vp = grid::viewport(angle = 45))
|
|
238 |
#'
|
|
239 |
#' num <- lapply(1:10, textGrob)
|
|
240 |
#' num %>%
|
|
241 |
#' arrange_grobs(grobs = .) %>%
|
|
242 |
#' draw_grob()
|
|
243 |
#' showViewport()
|
|
244 |
#' }
|
|
245 |
#'
|
|
246 |
#' @export
|
|
247 |
draw_grob <- function(grob, newpage = TRUE, vp = NULL) { |
|
248 | 3x |
if (newpage) { |
249 | 3x |
grid::grid.newpage() |
250 |
}
|
|
251 | 3x |
if (!is.null(vp)) { |
252 | 1x |
grid::pushViewport(vp) |
253 |
}
|
|
254 | 3x |
grid::grid.draw(grob) |
255 |
}
|
|
256 | ||
257 |
tern_grob <- function(x) { |
|
258 | ! |
class(x) <- unique(c("ternGrob", class(x))) |
259 | ! |
x
|
260 |
}
|
|
261 | ||
262 |
print.ternGrob <- function(x, ...) { |
|
263 | ! |
grid::grid.newpage() |
264 | ! |
grid::grid.draw(x) |
265 |
}
|
1 |
#' Controls for Cox Regression
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Sets a list of parameters for Cox regression fit. Used internally.
|
|
6 |
#'
|
|
7 |
#' @inheritParams argument_convention
|
|
8 |
#' @param pval_method (`string`)\cr the method used for estimation of p.values; `wald` (default) or `likelihood`.
|
|
9 |
#' @param interaction (`flag`)\cr if `TRUE`, the model includes the interaction between the studied
|
|
10 |
#' treatment and candidate covariate. Note that for univariate models without treatment arm, and
|
|
11 |
#' multivariate models, no interaction can be used so that this needs to be `FALSE`.
|
|
12 |
#' @param ties (`string`)\cr among `exact` (equivalent to `DISCRETE` in SAS), `efron` and `breslow`,
|
|
13 |
#' see [survival::coxph()]. Note: there is no equivalent of SAS `EXACT` method in R.
|
|
14 |
#'
|
|
15 |
#' @return A `list` of items with names corresponding to the arguments.
|
|
16 |
#'
|
|
17 |
#' @seealso [fit_coxreg_univar()] and [fit_coxreg_multivar()].
|
|
18 |
#'
|
|
19 |
#' @examples
|
|
20 |
#' control_coxreg()
|
|
21 |
#'
|
|
22 |
#' @export
|
|
23 |
control_coxreg <- function(pval_method = c("wald", "likelihood"), |
|
24 |
ties = c("exact", "efron", "breslow"), |
|
25 |
conf_level = 0.95, |
|
26 |
interaction = FALSE) { |
|
27 | 43x |
pval_method <- match.arg(pval_method) |
28 | 43x |
ties <- match.arg(ties) |
29 | 43x |
checkmate::assert_flag(interaction) |
30 | 43x |
assert_proportion_value(conf_level) |
31 | 43x |
list( |
32 | 43x |
pval_method = pval_method, |
33 | 43x |
ties = ties, |
34 | 43x |
conf_level = conf_level, |
35 | 43x |
interaction = interaction |
36 |
)
|
|
37 |
}
|
|
38 | ||
39 |
#' Custom Tidy Methods for Cox Regression
|
|
40 |
#'
|
|
41 |
#' @description `r lifecycle::badge("stable")`
|
|
42 |
#'
|
|
43 |
#' @inheritParams argument_convention
|
|
44 |
#' @param x (`list`)\cr Result of the Cox regression model fitted by [fit_coxreg_univar()] (for univariate models)
|
|
45 |
#' or [fit_coxreg_multivar()] (for multivariate models).
|
|
46 |
#'
|
|
47 |
#' @return [tidy()] returns:
|
|
48 |
#' * For `summary.coxph` objects, a `data.frame` with columns: `Pr(>|z|)`, `exp(coef)`, `exp(-coef)`, `lower .95`,
|
|
49 |
#' `upper .95`, `level`, and `n`.
|
|
50 |
#' * For `coxreg.univar` objects, a `data.frame` with columns: `effect`, `term`, `term_label`, `level`, `n`, `hr`,
|
|
51 |
#' `lcl`, `ucl`, `pval`, and `ci`.
|
|
52 |
#' * For `coxreg.multivar` objects, a `data.frame` with columns: `term`, `pval`, `term_label`, `hr`, `lcl`, `ucl`,
|
|
53 |
#' `level`, and `ci`.
|
|
54 |
#'
|
|
55 |
#' @seealso [cox_regression]
|
|
56 |
#'
|
|
57 |
#' @name tidy_coxreg
|
|
58 |
NULL
|
|
59 | ||
60 |
#' @describeIn tidy_coxreg Custom tidy method for [survival::coxph()] summary results.
|
|
61 |
#'
|
|
62 |
#' Tidy the [survival::coxph()] results into a `data.frame` to extract model results.
|
|
63 |
#'
|
|
64 |
#' @method tidy summary.coxph
|
|
65 |
#'
|
|
66 |
#' @examples
|
|
67 |
#' library(survival)
|
|
68 |
#' library(broom)
|
|
69 |
#'
|
|
70 |
#' set.seed(1, kind = "Mersenne-Twister")
|
|
71 |
#'
|
|
72 |
#' dta_bladder <- with(
|
|
73 |
#' data = bladder[bladder$enum < 5, ],
|
|
74 |
#' data.frame(
|
|
75 |
#' time = stop,
|
|
76 |
#' status = event,
|
|
77 |
#' armcd = as.factor(rx),
|
|
78 |
#' covar1 = as.factor(enum),
|
|
79 |
#' covar2 = factor(
|
|
80 |
#' sample(as.factor(enum)),
|
|
81 |
#' levels = 1:4, labels = c("F", "F", "M", "M")
|
|
82 |
#' )
|
|
83 |
#' )
|
|
84 |
#' )
|
|
85 |
#' labels <- c("armcd" = "ARM", "covar1" = "A Covariate Label", "covar2" = "Sex (F/M)")
|
|
86 |
#' formatters::var_labels(dta_bladder)[names(labels)] <- labels
|
|
87 |
#' dta_bladder$age <- sample(20:60, size = nrow(dta_bladder), replace = TRUE)
|
|
88 |
#'
|
|
89 |
#' formula <- "survival::Surv(time, status) ~ armcd + covar1"
|
|
90 |
#' msum <- summary(coxph(stats::as.formula(formula), data = dta_bladder))
|
|
91 |
#' tidy(msum)
|
|
92 |
#'
|
|
93 |
#' @export
|
|
94 |
tidy.summary.coxph <- function(x, # nolint |
|
95 |
...) { |
|
96 | 124x |
checkmate::assert_class(x, "summary.coxph") |
97 | 124x |
pval <- x$coefficients |
98 | 124x |
confint <- x$conf.int |
99 | 124x |
levels <- rownames(pval) |
100 | ||
101 | 124x |
pval <- tibble::as_tibble(pval) |
102 | 124x |
confint <- tibble::as_tibble(confint) |
103 | ||
104 | 124x |
ret <- cbind(pval[, grepl("Pr", names(pval))], confint) |
105 | 124x |
ret$level <- levels |
106 | 124x |
ret$n <- x[["n"]] |
107 | 124x |
ret
|
108 |
}
|
|
109 | ||
110 |
#' @describeIn tidy_coxreg Custom tidy method for a univariate Cox regression.
|
|
111 |
#'
|
|
112 |
#' Tidy up the result of a Cox regression model fitted by [fit_coxreg_univar()].
|
|
113 |
#'
|
|
114 |
#' @method tidy coxreg.univar
|
|
115 |
#'
|
|
116 |
#' @examples
|
|
117 |
#' ## Cox regression: arm + 1 covariate.
|
|
118 |
#' mod1 <- fit_coxreg_univar(
|
|
119 |
#' variables = list(
|
|
120 |
#' time = "time", event = "status", arm = "armcd",
|
|
121 |
#' covariates = "covar1"
|
|
122 |
#' ),
|
|
123 |
#' data = dta_bladder,
|
|
124 |
#' control = control_coxreg(conf_level = 0.91)
|
|
125 |
#' )
|
|
126 |
#'
|
|
127 |
#' ## Cox regression: arm + 1 covariate + interaction, 2 candidate covariates.
|
|
128 |
#' mod2 <- fit_coxreg_univar(
|
|
129 |
#' variables = list(
|
|
130 |
#' time = "time", event = "status", arm = "armcd",
|
|
131 |
#' covariates = c("covar1", "covar2")
|
|
132 |
#' ),
|
|
133 |
#' data = dta_bladder,
|
|
134 |
#' control = control_coxreg(conf_level = 0.91, interaction = TRUE)
|
|
135 |
#' )
|
|
136 |
#'
|
|
137 |
#' tidy(mod1)
|
|
138 |
#' tidy(mod2)
|
|
139 |
#'
|
|
140 |
#' @export
|
|
141 |
tidy.coxreg.univar <- function(x, # nolint |
|
142 |
...) { |
|
143 | 29x |
checkmate::assert_class(x, "coxreg.univar") |
144 | 29x |
mod <- x$mod |
145 | 29x |
vars <- c(x$vars$arm, x$vars$covariates) |
146 | 29x |
has_arm <- "arm" %in% names(x$vars) |
147 | ||
148 | 29x |
result <- if (!has_arm) { |
149 | 5x |
Map( |
150 | 5x |
mod = mod, vars = vars, |
151 | 5x |
f = function(mod, vars) { |
152 | 6x |
h_coxreg_multivar_extract( |
153 | 6x |
var = vars, |
154 | 6x |
data = x$data, |
155 | 6x |
mod = mod, |
156 | 6x |
control = x$control |
157 |
)
|
|
158 |
}
|
|
159 |
)
|
|
160 | 29x |
} else if (x$control$interaction) { |
161 | 10x |
Map( |
162 | 10x |
mod = mod, covar = vars, |
163 | 10x |
f = function(mod, covar) { |
164 | 22x |
h_coxreg_extract_interaction( |
165 | 22x |
effect = x$vars$arm, covar = covar, mod = mod, data = x$data, |
166 | 22x |
at = x$at, control = x$control |
167 |
)
|
|
168 |
}
|
|
169 |
)
|
|
170 |
} else { |
|
171 | 14x |
Map( |
172 | 14x |
mod = mod, vars = vars, |
173 | 14x |
f = function(mod, vars) { |
174 | 36x |
h_coxreg_univar_extract( |
175 | 36x |
effect = x$vars$arm, covar = vars, data = x$data, mod = mod, |
176 | 36x |
control = x$control |
177 |
)
|
|
178 |
}
|
|
179 |
)
|
|
180 |
}
|
|
181 | 29x |
result <- do.call(rbind, result) |
182 | ||
183 | 29x |
result$ci <- Map(lcl = result$lcl, ucl = result$ucl, f = function(lcl, ucl) c(lcl, ucl)) |
184 | 29x |
result$n <- lapply(result$n, empty_vector_if_na) |
185 | 29x |
result$ci <- lapply(result$ci, empty_vector_if_na) |
186 | 29x |
result$hr <- lapply(result$hr, empty_vector_if_na) |
187 | 29x |
if (x$control$interaction) { |
188 | 10x |
result$pval_inter <- lapply(result$pval_inter, empty_vector_if_na) |
189 |
# Remove interaction p-values due to change in specifications.
|
|
190 | 10x |
result$pval[result$effect != "Treatment:"] <- NA |
191 |
}
|
|
192 | 29x |
result$pval <- lapply(result$pval, empty_vector_if_na) |
193 | 29x |
attr(result, "conf_level") <- x$control$conf_level |
194 | 29x |
result
|
195 |
}
|
|
196 | ||
197 |
#' @describeIn tidy_coxreg Custom tidy method for a multivariate Cox regression.
|
|
198 |
#'
|
|
199 |
#' Tidy up the result of a Cox regression model fitted by [fit_coxreg_multivar()].
|
|
200 |
#'
|
|
201 |
#' @method tidy coxreg.multivar
|
|
202 |
#'
|
|
203 |
#' @examples
|
|
204 |
#' multivar_model <- fit_coxreg_multivar(
|
|
205 |
#' variables = list(
|
|
206 |
#' time = "time", event = "status", arm = "armcd",
|
|
207 |
#' covariates = c("covar1", "covar2")
|
|
208 |
#' ),
|
|
209 |
#' data = dta_bladder
|
|
210 |
#' )
|
|
211 |
#' broom::tidy(multivar_model)
|
|
212 |
#'
|
|
213 |
#' @export
|
|
214 |
tidy.coxreg.multivar <- function(x, # nolint |
|
215 |
...) { |
|
216 | 8x |
checkmate::assert_class(x, "coxreg.multivar") |
217 | 8x |
vars <- c(x$vars$arm, x$vars$covariates) |
218 | ||
219 |
# Convert the model summaries to data.
|
|
220 | 8x |
result <- Map( |
221 | 8x |
vars = vars, |
222 | 8x |
f = function(vars) { |
223 | 28x |
h_coxreg_multivar_extract( |
224 | 28x |
var = vars, data = x$data, |
225 | 28x |
mod = x$mod, control = x$control |
226 |
)
|
|
227 |
}
|
|
228 |
)
|
|
229 | 8x |
result <- do.call(rbind, result) |
230 | ||
231 | 8x |
result$ci <- Map(lcl = result$lcl, ucl = result$ucl, f = function(lcl, ucl) c(lcl, ucl)) |
232 | 8x |
result$ci <- lapply(result$ci, empty_vector_if_na) |
233 | 8x |
result$hr <- lapply(result$hr, empty_vector_if_na) |
234 | 8x |
result$pval <- lapply(result$pval, empty_vector_if_na) |
235 | 8x |
result <- result[, names(result) != "n"] |
236 | 8x |
attr(result, "conf_level") <- x$control$conf_level |
237 | ||
238 | 8x |
result
|
239 |
}
|
|
240 | ||
241 |
#' Fits for Cox Proportional Hazards Regression
|
|
242 |
#'
|
|
243 |
#' @description `r lifecycle::badge("stable")`
|
|
244 |
#'
|
|
245 |
#' Fitting functions for univariate and multivariate Cox regression models.
|
|
246 |
#'
|
|
247 |
#' @param variables (`list`)\cr a named list corresponds to the names of variables found in `data`, passed as a named
|
|
248 |
#' list and corresponding to `time`, `event`, `arm`, `strata`, and `covariates` terms. If `arm` is missing from
|
|
249 |
#' `variables`, then only Cox model(s) including the `covariates` will be fitted and the corresponding effect
|
|
250 |
#' estimates will be tabulated later.
|
|
251 |
#' @param data (`data.frame`)\cr the dataset containing the variables to fit the models.
|
|
252 |
#' @param at (`list` of `numeric`)\cr when the candidate covariate is a `numeric`, use `at` to specify
|
|
253 |
#' the value of the covariate at which the effect should be estimated.
|
|
254 |
#' @param control (`list`)\cr a list of parameters as returned by the helper function [control_coxreg()].
|
|
255 |
#'
|
|
256 |
#' @seealso [h_cox_regression] for relevant helper functions, [cox_regression].
|
|
257 |
#'
|
|
258 |
#' @examples
|
|
259 |
#' library(survival)
|
|
260 |
#'
|
|
261 |
#' set.seed(1, kind = "Mersenne-Twister")
|
|
262 |
#'
|
|
263 |
#' # Testing dataset [survival::bladder].
|
|
264 |
#' dta_bladder <- with(
|
|
265 |
#' data = bladder[bladder$enum < 5, ],
|
|
266 |
#' data.frame(
|
|
267 |
#' time = stop,
|
|
268 |
#' status = event,
|
|
269 |
#' armcd = as.factor(rx),
|
|
270 |
#' covar1 = as.factor(enum),
|
|
271 |
#' covar2 = factor(
|
|
272 |
#' sample(as.factor(enum)),
|
|
273 |
#' levels = 1:4, labels = c("F", "F", "M", "M")
|
|
274 |
#' )
|
|
275 |
#' )
|
|
276 |
#' )
|
|
277 |
#' labels <- c("armcd" = "ARM", "covar1" = "A Covariate Label", "covar2" = "Sex (F/M)")
|
|
278 |
#' formatters::var_labels(dta_bladder)[names(labels)] <- labels
|
|
279 |
#' dta_bladder$age <- sample(20:60, size = nrow(dta_bladder), replace = TRUE)
|
|
280 |
#'
|
|
281 |
#' plot(
|
|
282 |
#' survfit(Surv(time, status) ~ armcd + covar1, data = dta_bladder),
|
|
283 |
#' lty = 2:4,
|
|
284 |
#' xlab = "Months",
|
|
285 |
#' col = c("blue1", "blue2", "blue3", "blue4", "red1", "red2", "red3", "red4")
|
|
286 |
#' )
|
|
287 |
#'
|
|
288 |
#' @name fit_coxreg
|
|
289 |
NULL
|
|
290 | ||
291 |
#' @describeIn fit_coxreg Fit a series of univariate Cox regression models given the inputs.
|
|
292 |
#'
|
|
293 |
#' @return
|
|
294 |
#' * `fit_coxreg_univar()` returns a `coxreg.univar` class object which is a named `list`
|
|
295 |
#' with 5 elements:
|
|
296 |
#' * `mod`: Cox regression models fitted by [survival::coxph()].
|
|
297 |
#' * `data`: The original data frame input.
|
|
298 |
#' * `control`: The original control input.
|
|
299 |
#' * `vars`: The variables used in the model.
|
|
300 |
#' * `at`: Value of the covariate at which the effect should be estimated.
|
|
301 |
#'
|
|
302 |
#' @note When using `fit_coxreg_univar` there should be two study arms.
|
|
303 |
#'
|
|
304 |
#' @examples
|
|
305 |
#' # fit_coxreg_univar
|
|
306 |
#'
|
|
307 |
#' ## Cox regression: arm + 1 covariate.
|
|
308 |
#' mod1 <- fit_coxreg_univar(
|
|
309 |
#' variables = list(
|
|
310 |
#' time = "time", event = "status", arm = "armcd",
|
|
311 |
#' covariates = "covar1"
|
|
312 |
#' ),
|
|
313 |
#' data = dta_bladder,
|
|
314 |
#' control = control_coxreg(conf_level = 0.91)
|
|
315 |
#' )
|
|
316 |
#'
|
|
317 |
#' ## Cox regression: arm + 1 covariate + interaction, 2 candidate covariates.
|
|
318 |
#' mod2 <- fit_coxreg_univar(
|
|
319 |
#' variables = list(
|
|
320 |
#' time = "time", event = "status", arm = "armcd",
|
|
321 |
#' covariates = c("covar1", "covar2")
|
|
322 |
#' ),
|
|
323 |
#' data = dta_bladder,
|
|
324 |
#' control = control_coxreg(conf_level = 0.91, interaction = TRUE)
|
|
325 |
#' )
|
|
326 |
#'
|
|
327 |
#' ## Cox regression: arm + 1 covariate, stratified analysis.
|
|
328 |
#' mod3 <- fit_coxreg_univar(
|
|
329 |
#' variables = list(
|
|
330 |
#' time = "time", event = "status", arm = "armcd", strata = "covar2",
|
|
331 |
#' covariates = c("covar1")
|
|
332 |
#' ),
|
|
333 |
#' data = dta_bladder,
|
|
334 |
#' control = control_coxreg(conf_level = 0.91)
|
|
335 |
#' )
|
|
336 |
#'
|
|
337 |
#' ## Cox regression: no arm, only covariates.
|
|
338 |
#' mod4 <- fit_coxreg_univar(
|
|
339 |
#' variables = list(
|
|
340 |
#' time = "time", event = "status",
|
|
341 |
#' covariates = c("covar1", "covar2")
|
|
342 |
#' ),
|
|
343 |
#' data = dta_bladder
|
|
344 |
#' )
|
|
345 |
#'
|
|
346 |
#' @export
|
|
347 |
fit_coxreg_univar <- function(variables, |
|
348 |
data,
|
|
349 |
at = list(), |
|
350 |
control = control_coxreg()) { |
|
351 | 34x |
checkmate::assert_list(variables, names = "named") |
352 | 34x |
has_arm <- "arm" %in% names(variables) |
353 | 34x |
arm_name <- if (has_arm) "arm" else NULL |
354 | ||
355 | 34x |
checkmate::assert_character(variables$covariates, null.ok = TRUE) |
356 | ||
357 | 34x |
assert_df_with_variables(data, variables) |
358 | 34x |
assert_list_of_variables(variables[c(arm_name, "event", "time")]) |
359 | ||
360 | 34x |
if (!is.null(variables$strata)) { |
361 | 4x |
checkmate::assert_disjunct(control$pval_method, "likelihood") |
362 |
}
|
|
363 | 33x |
if (has_arm) { |
364 | 27x |
assert_df_with_factors(data, list(val = variables$arm), min.levels = 2, max.levels = 2) |
365 |
}
|
|
366 | 32x |
vars <- unlist(variables[c(arm_name, "covariates", "strata")], use.names = FALSE) |
367 | 32x |
for (i in vars) { |
368 | 73x |
if (is.factor(data[[i]])) { |
369 | 63x |
attr(data[[i]], "levels") <- levels(droplevels(data[[i]])) |
370 |
}
|
|
371 |
}
|
|
372 | 32x |
forms <- h_coxreg_univar_formulas(variables, interaction = control$interaction) |
373 | 32x |
mod <- lapply( |
374 | 32x |
forms, function(x) { |
375 | 69x |
survival::coxph(formula = stats::as.formula(x), data = data, ties = control$ties) |
376 |
}
|
|
377 |
)
|
|
378 | 32x |
structure( |
379 | 32x |
list( |
380 | 32x |
mod = mod, |
381 | 32x |
data = data, |
382 | 32x |
control = control, |
383 | 32x |
vars = variables, |
384 | 32x |
at = at |
385 |
),
|
|
386 | 32x |
class = "coxreg.univar" |
387 |
)
|
|
388 |
}
|
|
389 | ||
390 |
#' @describeIn fit_coxreg Fit a multivariate Cox regression model.
|
|
391 |
#'
|
|
392 |
#' @return
|
|
393 |
#' * `fit_coxreg_multivar()` returns a `coxreg.multivar` class object which is a named list
|
|
394 |
#' with 4 elements:
|
|
395 |
#' * `mod`: Cox regression model fitted by [survival::coxph()].
|
|
396 |
#' * `data`: The original data frame input.
|
|
397 |
#' * `control`: The original control input.
|
|
398 |
#' * `vars`: The variables used in the model.
|
|
399 |
#'
|
|
400 |
#' @examples
|
|
401 |
#' # fit_coxreg_multivar
|
|
402 |
#'
|
|
403 |
#' ## Cox regression: multivariate Cox regression.
|
|
404 |
#' multivar_model <- fit_coxreg_multivar(
|
|
405 |
#' variables = list(
|
|
406 |
#' time = "time", event = "status", arm = "armcd",
|
|
407 |
#' covariates = c("covar1", "covar2")
|
|
408 |
#' ),
|
|
409 |
#' data = dta_bladder
|
|
410 |
#' )
|
|
411 |
#'
|
|
412 |
#' # Example without treatment arm.
|
|
413 |
#' multivar_covs_model <- fit_coxreg_multivar(
|
|
414 |
#' variables = list(
|
|
415 |
#' time = "time", event = "status",
|
|
416 |
#' covariates = c("covar1", "covar2")
|
|
417 |
#' ),
|
|
418 |
#' data = dta_bladder
|
|
419 |
#' )
|
|
420 |
#'
|
|
421 |
#' @export
|
|
422 |
fit_coxreg_multivar <- function(variables, |
|
423 |
data,
|
|
424 |
control = control_coxreg()) { |
|
425 | 51x |
checkmate::assert_list(variables, names = "named") |
426 | 51x |
has_arm <- "arm" %in% names(variables) |
427 | 51x |
arm_name <- if (has_arm) "arm" else NULL |
428 | ||
429 | 51x |
if (!is.null(variables$covariates)) { |
430 | 13x |
checkmate::assert_character(variables$covariates) |
431 |
}
|
|
432 | ||
433 | 51x |
checkmate::assert_false(control$interaction) |
434 | 51x |
assert_df_with_variables(data, variables) |
435 | 51x |
assert_list_of_variables(variables[c(arm_name, "event", "time")]) |
436 | ||
437 | 51x |
if (!is.null(variables$strata)) { |
438 | 3x |
checkmate::assert_disjunct(control$pval_method, "likelihood") |
439 |
}
|
|
440 | ||
441 | 50x |
form <- h_coxreg_multivar_formula(variables) |
442 | 50x |
mod <- survival::coxph( |
443 | 50x |
formula = stats::as.formula(form), |
444 | 50x |
data = data, |
445 | 50x |
ties = control$ties |
446 |
)
|
|
447 | 50x |
structure( |
448 | 50x |
list( |
449 | 50x |
mod = mod, |
450 | 50x |
data = data, |
451 | 50x |
control = control, |
452 | 50x |
vars = variables |
453 |
),
|
|
454 | 50x |
class = "coxreg.multivar" |
455 |
)
|
|
456 |
}
|
|
457 | ||
458 |
#' Muffled `car::Anova`
|
|
459 |
#'
|
|
460 |
#' Applied on survival models, [car::Anova()] signal that the `strata` terms is dropped from the model formula when
|
|
461 |
#' present, this function deliberately muffles this message.
|
|
462 |
#'
|
|
463 |
#' @param mod (`coxph`)\cr Cox regression model fitted by [survival::coxph()].
|
|
464 |
#' @param test_statistic (`string`)\cr the method used for estimation of p.values; `wald` (default) or `likelihood`.
|
|
465 |
#'
|
|
466 |
#' @return Returns the output of [car::Anova()], with convergence message muffled.
|
|
467 |
#'
|
|
468 |
#' @keywords internal
|
|
469 |
muffled_car_anova <- function(mod, test_statistic) { |
|
470 | 142x |
tryCatch( |
471 | 142x |
withCallingHandlers( |
472 | 142x |
expr = { |
473 | 142x |
car::Anova( |
474 | 142x |
mod,
|
475 | 142x |
test.statistic = test_statistic, |
476 | 142x |
type = "III" |
477 |
)
|
|
478 |
},
|
|
479 | 142x |
message = function(m) invokeRestart("muffleMessage"), |
480 | 142x |
error = function(e) { |
481 | 1x |
stop(paste( |
482 | 1x |
"the model seems to have convergence problems, please try to change",
|
483 | 1x |
"the configuration of covariates or strata variables, e.g.",
|
484 | 1x |
"- original error:", e |
485 |
)) |
|
486 |
}
|
|
487 |
)
|
|
488 |
)
|
|
489 |
}
|
1 |
#' Horizontal Waterfall Plot
|
|
2 |
#'
|
|
3 |
#' This basic waterfall plot visualizes a quantity `height` ordered by value with some markup.
|
|
4 |
#'
|
|
5 |
#' @description `r lifecycle::badge("stable")`
|
|
6 |
#'
|
|
7 |
#' @param height (`numeric``)\cr vector containing values to be plotted as the waterfall bars.
|
|
8 |
#' @param id (`character`)\cr vector containing IDs to use as the x-axis label for the waterfall bars.
|
|
9 |
#' @param col (`character`)\cr colors.
|
|
10 |
#' @param col_var (`factor`, `character` or `NULL`)\cr categorical variable for bar coloring. `NULL` by default.
|
|
11 |
#' @param xlab (`character`)\cr x label. Default is `"ID"`.
|
|
12 |
#' @param ylab (`character`)\cr y label. Default is `"Value"`.
|
|
13 |
#' @param title (`character`)\cr text to be displayed as plot title.
|
|
14 |
#' @param col_legend_title (`character`)\cr text to be displayed as legend title.
|
|
15 |
#'
|
|
16 |
#' @return A `ggplot` waterfall plot.
|
|
17 |
#'
|
|
18 |
#' @examples
|
|
19 |
#' library(dplyr)
|
|
20 |
#' library(nestcolor)
|
|
21 |
#'
|
|
22 |
#' g_waterfall(height = c(3, 5, -1), id = letters[1:3])
|
|
23 |
#'
|
|
24 |
#' g_waterfall(
|
|
25 |
#' height = c(3, 5, -1),
|
|
26 |
#' id = letters[1:3],
|
|
27 |
#' col_var = letters[1:3]
|
|
28 |
#' )
|
|
29 |
#'
|
|
30 |
#' adsl_f <- tern_ex_adsl %>%
|
|
31 |
#' select(USUBJID, STUDYID, ARM, ARMCD, SEX)
|
|
32 |
#'
|
|
33 |
#' adrs_f <- tern_ex_adrs %>%
|
|
34 |
#' filter(PARAMCD == "OVRINV") %>%
|
|
35 |
#' mutate(pchg = rnorm(n(), 10, 50))
|
|
36 |
#'
|
|
37 |
#' adrs_f <- head(adrs_f, 30)
|
|
38 |
#' adrs_f <- adrs_f[!duplicated(adrs_f$USUBJID), ]
|
|
39 |
#' head(adrs_f)
|
|
40 |
#'
|
|
41 |
#' g_waterfall(
|
|
42 |
#' height = adrs_f$pchg,
|
|
43 |
#' id = adrs_f$USUBJID,
|
|
44 |
#' col_var = adrs_f$AVALC
|
|
45 |
#' )
|
|
46 |
#'
|
|
47 |
#' g_waterfall(
|
|
48 |
#' height = adrs_f$pchg,
|
|
49 |
#' id = paste("asdfdsfdsfsd", adrs_f$USUBJID),
|
|
50 |
#' col_var = adrs_f$SEX
|
|
51 |
#' )
|
|
52 |
#'
|
|
53 |
#' g_waterfall(
|
|
54 |
#' height = adrs_f$pchg,
|
|
55 |
#' id = paste("asdfdsfdsfsd", adrs_f$USUBJID),
|
|
56 |
#' xlab = "ID",
|
|
57 |
#' ylab = "Percentage Change",
|
|
58 |
#' title = "Waterfall plot"
|
|
59 |
#' )
|
|
60 |
#'
|
|
61 |
#' @export
|
|
62 |
g_waterfall <- function(height, |
|
63 |
id,
|
|
64 |
col_var = NULL, |
|
65 |
col = getOption("ggplot2.discrete.colour"), |
|
66 |
xlab = NULL, |
|
67 |
ylab = NULL, |
|
68 |
col_legend_title = NULL, |
|
69 |
title = NULL) { |
|
70 | 2x |
if (!is.null(col_var)) { |
71 | 1x |
check_same_n(height = height, id = id, col_var = col_var) |
72 |
} else { |
|
73 | 1x |
check_same_n(height = height, id = id) |
74 |
}
|
|
75 | ||
76 | 2x |
checkmate::assert_multi_class(col_var, c("character", "factor"), null.ok = TRUE) |
77 | 2x |
checkmate::assert_character(col, null.ok = TRUE) |
78 | ||
79 | 2x |
xlabel <- deparse(substitute(id)) |
80 | 2x |
ylabel <- deparse(substitute(height)) |
81 | ||
82 | 2x |
col_label <- if (!missing(col_var)) { |
83 | 1x |
deparse(substitute(col_var)) |
84 |
}
|
|
85 | ||
86 | 2x |
xlab <- if (is.null(xlab)) xlabel else xlab |
87 | 2x |
ylab <- if (is.null(ylab)) ylabel else ylab |
88 | 2x |
col_legend_title <- if (is.null(col_legend_title)) col_label else col_legend_title |
89 | ||
90 | 2x |
plot_data <- data.frame( |
91 | 2x |
height = height, |
92 | 2x |
id = as.character(id), |
93 | 2x |
col_var = if (is.null(col_var)) "x" else to_n(col_var, length(height)), |
94 | 2x |
stringsAsFactors = FALSE |
95 |
)
|
|
96 | ||
97 | 2x |
plot_data_ord <- plot_data[order(plot_data$height, decreasing = TRUE), ] |
98 | ||
99 | 2x |
p <- ggplot2::ggplot(plot_data_ord, ggplot2::aes(x = factor(id, levels = id), y = height)) + |
100 | 2x |
ggplot2::geom_col() + |
101 | 2x |
ggplot2::geom_text( |
102 | 2x |
label = format(plot_data_ord$height, digits = 2), |
103 | 2x |
vjust = ifelse(plot_data_ord$height >= 0, -0.5, 1.5) |
104 |
) + |
|
105 | 2x |
ggplot2::xlab(xlab) + |
106 | 2x |
ggplot2::ylab(ylab) + |
107 | 2x |
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 90, hjust = 0, vjust = .5)) |
108 | ||
109 | 2x |
if (!is.null(col_var)) { |
110 | 1x |
p <- p + |
111 | 1x |
ggplot2::aes(fill = col_var) + |
112 | 1x |
ggplot2::labs(fill = col_legend_title) + |
113 | 1x |
ggplot2::theme( |
114 | 1x |
legend.position = "bottom", |
115 | 1x |
legend.background = ggplot2::element_blank(), |
116 | 1x |
legend.title = ggplot2::element_text(face = "bold"), |
117 | 1x |
legend.box.background = ggplot2::element_rect(colour = "black") |
118 |
)
|
|
119 |
}
|
|
120 | ||
121 | 2x |
if (!is.null(col)) { |
122 | 1x |
p <- p + |
123 | 1x |
ggplot2::scale_fill_manual(values = col) |
124 |
}
|
|
125 | ||
126 | 2x |
if (!is.null(title)) { |
127 | 1x |
p <- p + |
128 | 1x |
ggplot2::labs(title = title) + |
129 | 1x |
ggplot2::theme(plot.title = ggplot2::element_text(face = "bold")) |
130 |
}
|
|
131 | ||
132 | 2x |
p
|
133 |
}
|
1 |
#' Count Patients with Marked Laboratory Abnormalities
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Primary analysis variable `.var` indicates whether single, replicated or last marked laboratory
|
|
6 |
#' abnormality was observed (`factor`). Additional analysis variables are `id` (`character` or `factor`)
|
|
7 |
#' and `direction` (`factor`) indicating the direction of the abnormality. Denominator is number of
|
|
8 |
#' patients with at least one valid measurement during the analysis.
|
|
9 |
#' * For `Single, not last` and `Last or replicated`: Numerator is number of patients
|
|
10 |
#' with `Single, not last` and `Last or replicated` levels, respectively.
|
|
11 |
#' * For `Any`: Numerator is the number of patients with either single or
|
|
12 |
#' replicated marked abnormalities.
|
|
13 |
#'
|
|
14 |
#' @inheritParams argument_convention
|
|
15 |
#' @param category (`list`)\cr with different marked category names for single
|
|
16 |
#' and last or replicated.
|
|
17 |
#'
|
|
18 |
#' @note `Single, not last` and `Last or replicated` levels are mutually exclusive. If a patient has
|
|
19 |
#' abnormalities that meet both the `Single, not last` and `Last or replicated` criteria, then the
|
|
20 |
#' patient will be counted only under the `Last or replicated` category.
|
|
21 |
#'
|
|
22 |
#' @name abnormal_by_marked
|
|
23 |
NULL
|
|
24 | ||
25 |
#' @describeIn abnormal_by_marked Statistics function for patients with marked lab abnormalities.
|
|
26 |
#'
|
|
27 |
#' @return
|
|
28 |
#' * `s_count_abnormal_by_marked()` returns statistic `count_fraction` with `Single, not last`,
|
|
29 |
#' `Last or replicated`, and `Any` results.
|
|
30 |
#'
|
|
31 |
#' @examples
|
|
32 |
#' library(dplyr)
|
|
33 |
#'
|
|
34 |
#' df <- data.frame(
|
|
35 |
#' USUBJID = as.character(c(rep(1, 5), rep(2, 5), rep(1, 5), rep(2, 5))),
|
|
36 |
#' ARMCD = factor(c(rep("ARM A", 5), rep("ARM B", 5), rep("ARM A", 5), rep("ARM B", 5))),
|
|
37 |
#' ANRIND = factor(c(
|
|
38 |
#' "NORMAL", "HIGH", "HIGH", "HIGH HIGH", "HIGH",
|
|
39 |
#' "HIGH", "HIGH", "HIGH HIGH", "NORMAL", "HIGH HIGH", "NORMAL", "LOW", "LOW", "LOW LOW", "LOW",
|
|
40 |
#' "LOW", "LOW", "LOW LOW", "NORMAL", "LOW LOW"
|
|
41 |
#' )),
|
|
42 |
#' ONTRTFL = rep(c("", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y"), 2),
|
|
43 |
#' PARAMCD = factor(c(rep("CRP", 10), rep("ALT", 10))),
|
|
44 |
#' AVALCAT1 = factor(rep(c("", "", "", "SINGLE", "REPLICATED", "", "", "LAST", "", "SINGLE"), 2)),
|
|
45 |
#' stringsAsFactors = FALSE
|
|
46 |
#' )
|
|
47 |
#'
|
|
48 |
#' df <- df %>%
|
|
49 |
#' mutate(abn_dir = factor(
|
|
50 |
#' case_when(
|
|
51 |
#' ANRIND == "LOW LOW" ~ "Low",
|
|
52 |
#' ANRIND == "HIGH HIGH" ~ "High",
|
|
53 |
#' TRUE ~ ""
|
|
54 |
#' ),
|
|
55 |
#' levels = c("Low", "High")
|
|
56 |
#' ))
|
|
57 |
#'
|
|
58 |
#' # Select only post-baseline records.
|
|
59 |
#' df <- df %>% filter(ONTRTFL == "Y")
|
|
60 |
#' df_crp <- df %>%
|
|
61 |
#' filter(PARAMCD == "CRP") %>%
|
|
62 |
#' droplevels()
|
|
63 |
#' full_parent_df <- list(df_crp, "not_needed")
|
|
64 |
#' cur_col_subset <- list(rep(TRUE, nrow(df_crp)), "not_needed")
|
|
65 |
#' spl_context <- data.frame(
|
|
66 |
#' split = c("PARAMCD", "GRADE_DIR"),
|
|
67 |
#' full_parent_df = I(full_parent_df),
|
|
68 |
#' cur_col_subset = I(cur_col_subset)
|
|
69 |
#' )
|
|
70 |
#'
|
|
71 |
#' @keywords internal
|
|
72 |
s_count_abnormal_by_marked <- function(df, |
|
73 |
.var = "AVALCAT1", |
|
74 |
.spl_context,
|
|
75 |
category = list(single = "SINGLE", last_replicated = c("LAST", "REPLICATED")), |
|
76 |
variables = list(id = "USUBJID", param = "PARAM", direction = "abn_dir")) { |
|
77 | 3x |
checkmate::assert_string(.var) |
78 | 3x |
checkmate::assert_list(variables) |
79 | 3x |
checkmate::assert_list(category) |
80 | 3x |
checkmate::assert_subset(names(category), c("single", "last_replicated")) |
81 | 3x |
checkmate::assert_subset(names(variables), c("id", "param", "direction")) |
82 | 3x |
checkmate::assert_vector(unique(df[[variables$direction]]), max.len = 1) |
83 | ||
84 | 2x |
assert_df_with_variables(df, c(aval = .var, variables)) |
85 | 2x |
checkmate::assert_multi_class(df[[.var]], classes = c("factor", "character")) |
86 | 2x |
checkmate::assert_multi_class(df[[variables$id]], classes = c("factor", "character")) |
87 | ||
88 | ||
89 | 2x |
first_row <- .spl_context[.spl_context$split == variables[["param"]], ] |
90 |
# Patients in the denominator have at least one post-baseline visit.
|
|
91 | 2x |
subj <- first_row$full_parent_df[[1]][[variables[["id"]]]] |
92 | 2x |
subj_cur_col <- subj[first_row$cur_col_subset[[1]]] |
93 |
# Some subjects may have a record for high and low directions but
|
|
94 |
# should be counted only once.
|
|
95 | 2x |
denom <- length(unique(subj_cur_col)) |
96 | ||
97 | 2x |
if (denom != 0) { |
98 | 2x |
subjects_last_replicated <- unique( |
99 | 2x |
df[df[[.var]] %in% category[["last_replicated"]], variables$id, drop = TRUE] |
100 |
)
|
|
101 | 2x |
subjects_single <- unique( |
102 | 2x |
df[df[[.var]] %in% category[["single"]], variables$id, drop = TRUE] |
103 |
)
|
|
104 |
# Subjects who have both single and last/replicated abnormalities are counted in only the last/replicated group.
|
|
105 | 2x |
subjects_single <- setdiff(subjects_single, subjects_last_replicated) |
106 | 2x |
n_single <- length(subjects_single) |
107 | 2x |
n_last_replicated <- length(subjects_last_replicated) |
108 | 2x |
n_any <- n_single + n_last_replicated |
109 | 2x |
result <- list(count_fraction = list( |
110 | 2x |
"Single, not last" = c(n_single, n_single / denom), |
111 | 2x |
"Last or replicated" = c(n_last_replicated, n_last_replicated / denom), |
112 | 2x |
"Any Abnormality" = c(n_any, n_any / denom) |
113 |
)) |
|
114 |
} else { |
|
115 | ! |
result <- list(count_fraction = list( |
116 | ! |
"Single, not last" = c(0, 0), |
117 | ! |
"Last or replicated" = c(0, 0), |
118 | ! |
"Any Abnormality" = c(0, 0) |
119 |
)) |
|
120 |
}
|
|
121 | ||
122 | 2x |
result
|
123 |
}
|
|
124 | ||
125 |
#' @describeIn abnormal_by_marked Formatted analysis function which is used as `afun`
|
|
126 |
#' in `count_abnormal_by_marked()`.
|
|
127 |
#'
|
|
128 |
#' @return
|
|
129 |
#' * `a_count_abnormal_by_marked()` returns the corresponding list with formatted [rtables::CellValue()].
|
|
130 |
#'
|
|
131 |
#'
|
|
132 |
#' @keywords internal
|
|
133 |
a_count_abnormal_by_marked <- make_afun( |
|
134 |
s_count_abnormal_by_marked,
|
|
135 |
.formats = c(count_fraction = format_count_fraction) |
|
136 |
)
|
|
137 | ||
138 |
#' @describeIn abnormal_by_marked Layout-creating function which can take statistics function arguments
|
|
139 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()].
|
|
140 |
#'
|
|
141 |
#' @return
|
|
142 |
#' * `count_abnormal_by_marked()` returns a layout object suitable for passing to further layouting functions,
|
|
143 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing
|
|
144 |
#' the statistics from `s_count_abnormal_by_marked()` to the table layout.
|
|
145 |
#'
|
|
146 |
#' @examples
|
|
147 |
#' map <- unique(
|
|
148 |
#' df[df$abn_dir %in% c("Low", "High") & df$AVALCAT1 != "", c("PARAMCD", "abn_dir")]
|
|
149 |
#' ) %>%
|
|
150 |
#' lapply(as.character) %>%
|
|
151 |
#' as.data.frame() %>%
|
|
152 |
#' arrange(PARAMCD, abn_dir)
|
|
153 |
#'
|
|
154 |
#' basic_table() %>%
|
|
155 |
#' split_cols_by("ARMCD") %>%
|
|
156 |
#' split_rows_by("PARAMCD") %>%
|
|
157 |
#' summarize_num_patients(
|
|
158 |
#' var = "USUBJID",
|
|
159 |
#' .stats = "unique_count"
|
|
160 |
#' ) %>%
|
|
161 |
#' split_rows_by(
|
|
162 |
#' "abn_dir",
|
|
163 |
#' split_fun = trim_levels_to_map(map)
|
|
164 |
#' ) %>%
|
|
165 |
#' count_abnormal_by_marked(
|
|
166 |
#' var = "AVALCAT1",
|
|
167 |
#' variables = list(
|
|
168 |
#' id = "USUBJID",
|
|
169 |
#' param = "PARAMCD",
|
|
170 |
#' direction = "abn_dir"
|
|
171 |
#' )
|
|
172 |
#' ) %>%
|
|
173 |
#' build_table(df = df)
|
|
174 |
#'
|
|
175 |
#' basic_table() %>%
|
|
176 |
#' split_cols_by("ARMCD") %>%
|
|
177 |
#' split_rows_by("PARAMCD") %>%
|
|
178 |
#' summarize_num_patients(
|
|
179 |
#' var = "USUBJID",
|
|
180 |
#' .stats = "unique_count"
|
|
181 |
#' ) %>%
|
|
182 |
#' split_rows_by(
|
|
183 |
#' "abn_dir",
|
|
184 |
#' split_fun = trim_levels_in_group("abn_dir")
|
|
185 |
#' ) %>%
|
|
186 |
#' count_abnormal_by_marked(
|
|
187 |
#' var = "AVALCAT1",
|
|
188 |
#' variables = list(
|
|
189 |
#' id = "USUBJID",
|
|
190 |
#' param = "PARAMCD",
|
|
191 |
#' direction = "abn_dir"
|
|
192 |
#' )
|
|
193 |
#' ) %>%
|
|
194 |
#' build_table(df = df)
|
|
195 |
#'
|
|
196 |
#' @export
|
|
197 |
count_abnormal_by_marked <- function(lyt, |
|
198 |
var,
|
|
199 |
...,
|
|
200 |
.stats = NULL, |
|
201 |
.formats = NULL, |
|
202 |
.labels = NULL, |
|
203 |
.indent_mods = NULL) { |
|
204 | 1x |
checkmate::assert_string(var) |
205 | ||
206 | 1x |
afun <- make_afun( |
207 | 1x |
a_count_abnormal_by_marked,
|
208 | 1x |
.stats = .stats, |
209 | 1x |
.formats = .formats, |
210 | 1x |
.labels = .labels, |
211 | 1x |
.indent_mods = .indent_mods, |
212 | 1x |
.ungroup_stats = "count_fraction" |
213 |
)
|
|
214 | ||
215 | 1x |
lyt <- analyze( |
216 | 1x |
lyt = lyt, |
217 | 1x |
vars = var, |
218 | 1x |
afun = afun, |
219 | 1x |
show_labels = "hidden", |
220 | 1x |
extra_args = c(list(...)) |
221 |
)
|
|
222 | 1x |
lyt
|
223 |
}
|
1 |
#' Summary for analysis of covariance (`ANCOVA`).
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Summarize results of `ANCOVA`. This can be used to analyze multiple endpoints and/or
|
|
6 |
#' multiple timepoints within the same response variable `.var`.
|
|
7 |
#'
|
|
8 |
#' @inheritParams argument_convention
|
|
9 |
#'
|
|
10 |
#' @name summarize_ancova
|
|
11 |
NULL
|
|
12 | ||
13 |
#' Helper Function to Return Results of a Linear Model
|
|
14 |
#'
|
|
15 |
#' @description `r lifecycle::badge("stable")`
|
|
16 |
#'
|
|
17 |
#' @inheritParams argument_convention
|
|
18 |
#' @param .df_row (`data.frame`)\cr data set that includes all the variables that are called in `.var` and `variables`.
|
|
19 |
#' @param variables (named `list` of `strings`)\cr list of additional analysis variables, with expected elements:
|
|
20 |
#' * `arm` (`string`)\cr group variable, for which the covariate adjusted means of multiple groups will be
|
|
21 |
#' summarized. Specifically, the first level of `arm` variable is taken as the reference group.
|
|
22 |
#' * `covariates` (`character`)\cr a vector that can contain single variable names (such as `"X1"`), and/or
|
|
23 |
#' interaction terms indicated by `"X1 * X2"`.
|
|
24 |
#' @param interaction_item (`character`)\cr name of the variable that should have interactions
|
|
25 |
#' with arm. if the interaction is not needed, the default option is `NULL`.
|
|
26 |
#'
|
|
27 |
#' @return The summary of a linear model.
|
|
28 |
#'
|
|
29 |
#' @examples
|
|
30 |
#' h_ancova(
|
|
31 |
#' .var = "Sepal.Length",
|
|
32 |
#' .df_row = iris,
|
|
33 |
#' variables = list(arm = "Species", covariates = c("Petal.Length * Petal.Width", "Sepal.Width"))
|
|
34 |
#' )
|
|
35 |
#'
|
|
36 |
#' @export
|
|
37 |
h_ancova <- function(.var, |
|
38 |
.df_row,
|
|
39 |
variables,
|
|
40 |
interaction_item = NULL) { |
|
41 | 15x |
checkmate::assert_string(.var) |
42 | 15x |
checkmate::assert_list(variables) |
43 | 15x |
checkmate::assert_subset(names(variables), c("arm", "covariates")) |
44 | 15x |
assert_df_with_variables(.df_row, list(rsp = .var)) |
45 | ||
46 | 14x |
arm <- variables$arm |
47 | 14x |
covariates <- variables$covariates |
48 | 14x |
if (!is.null(covariates) && length(covariates) > 0) { |
49 |
# Get all covariate variable names in the model.
|
|
50 | 11x |
var_list <- get_covariates(covariates) |
51 | 11x |
assert_df_with_variables(.df_row, var_list) |
52 |
}
|
|
53 | ||
54 | 13x |
covariates_part <- paste(covariates, collapse = " + ") |
55 | 13x |
if (covariates_part != "") { |
56 | 10x |
formula <- stats::as.formula(paste0(.var, " ~ ", covariates_part, " + ", arm)) |
57 |
} else { |
|
58 | 3x |
formula <- stats::as.formula(paste0(.var, " ~ ", arm)) |
59 |
}
|
|
60 | ||
61 | 13x |
if (is.null(interaction_item)) { |
62 | 9x |
specs <- arm |
63 |
} else { |
|
64 | 4x |
specs <- c(arm, interaction_item) |
65 |
}
|
|
66 | ||
67 | 13x |
lm_fit <- stats::lm( |
68 | 13x |
formula = formula, |
69 | 13x |
data = .df_row |
70 |
)
|
|
71 | 13x |
emmeans_fit <- emmeans::emmeans( |
72 | 13x |
lm_fit,
|
73 |
# Specify here the group variable over which EMM are desired.
|
|
74 | 13x |
specs = specs, |
75 |
# Pass the data again so that the factor levels of the arm variable can be inferred.
|
|
76 | 13x |
data = .df_row |
77 |
)
|
|
78 | ||
79 | 13x |
emmeans_fit
|
80 |
}
|
|
81 | ||
82 |
#' @describeIn summarize_ancova Statistics function that produces a named list of results
|
|
83 |
#' of the investigated linear model.
|
|
84 |
#'
|
|
85 |
#' @inheritParams h_ancova
|
|
86 |
#' @param interaction_y (`character`)\cr a selected item inside of the interaction_item column which will be used
|
|
87 |
#' to select the specific `ANCOVA` results. if the interaction is not needed, the default option is `FALSE`.
|
|
88 |
#'
|
|
89 |
#' @return
|
|
90 |
#' * `s_ancova()` returns a named list of 5 statistics:
|
|
91 |
#' * `n`: Count of complete sample size for the group.
|
|
92 |
#' * `lsmean`: Estimated marginal means in the group.
|
|
93 |
#' * `lsmean_diff`: Difference in estimated marginal means in comparison to the reference group.
|
|
94 |
#' If working with the reference group, this will be empty.
|
|
95 |
#' * `lsmean_diff_ci`: Confidence level for difference in estimated marginal means in comparison
|
|
96 |
#' to the reference group.
|
|
97 |
#' * `pval`: p-value (not adjusted for multiple comparisons).
|
|
98 |
#'
|
|
99 |
#' @examples
|
|
100 |
#' library(dplyr)
|
|
101 |
#'
|
|
102 |
#' df <- iris %>% filter(Species == "virginica")
|
|
103 |
#' .df_row <- iris
|
|
104 |
#' .var <- "Petal.Length"
|
|
105 |
#' variables <- list(arm = "Species", covariates = "Sepal.Length * Sepal.Width")
|
|
106 |
#' .ref_group <- iris %>% filter(Species == "setosa")
|
|
107 |
#' conf_level <- 0.95
|
|
108 |
#'
|
|
109 |
#' @keywords internal
|
|
110 |
s_ancova <- function(df, |
|
111 |
.var,
|
|
112 |
.df_row,
|
|
113 |
variables,
|
|
114 |
.ref_group,
|
|
115 |
.in_ref_col,
|
|
116 |
conf_level,
|
|
117 |
interaction_y = FALSE, |
|
118 |
interaction_item = NULL) { |
|
119 | 3x |
emmeans_fit <- h_ancova(.var = .var, variables = variables, .df_row = .df_row, interaction_item = interaction_item) |
120 | ||
121 | 3x |
sum_fit <- summary( |
122 | 3x |
emmeans_fit,
|
123 | 3x |
level = conf_level |
124 |
)
|
|
125 | ||
126 | 3x |
arm <- variables$arm |
127 | ||
128 | 3x |
sum_level <- as.character(unique(df[[arm]])) |
129 | ||
130 |
# Ensure that there is only one element in sum_level.
|
|
131 | 3x |
checkmate::assert_scalar(sum_level) |
132 | ||
133 | 2x |
sum_fit_level <- sum_fit[sum_fit[[arm]] == sum_level, ] |
134 | ||
135 |
# Get the index of the ref arm
|
|
136 | 2x |
if (interaction_y != FALSE) { |
137 | 1x |
y <- unlist(df[(df[[interaction_item]] == interaction_y), .var]) |
138 |
# convert characters selected in interaction_y into the numeric order
|
|
139 | 1x |
interaction_y <- which(sum_fit_level[[interaction_item]] == interaction_y) |
140 | 1x |
sum_fit_level <- sum_fit_level[interaction_y, ] |
141 |
# if interaction is called, reset the index
|
|
142 | 1x |
ref_key <- seq(sum_fit[[arm]][unique(.ref_group[[arm]])]) |
143 | 1x |
ref_key <- tail(ref_key, n = 1) |
144 | 1x |
ref_key <- (interaction_y - 1) * length(unique(.df_row[[arm]])) + ref_key |
145 |
} else { |
|
146 | 1x |
y <- df[[.var]] |
147 |
# Get the index of the ref arm when interaction is not called
|
|
148 | 1x |
ref_key <- seq(sum_fit[[arm]][unique(.ref_group[[arm]])]) |
149 | 1x |
ref_key <- tail(ref_key, n = 1) |
150 |
}
|
|
151 | ||
152 | 2x |
if (.in_ref_col) { |
153 | 1x |
list( |
154 | 1x |
n = length(y[!is.na(y)]), |
155 | 1x |
lsmean = formatters::with_label(sum_fit_level$emmean, "Adjusted Mean"), |
156 | 1x |
lsmean_diff = formatters::with_label(character(), "Difference in Adjusted Means"), |
157 | 1x |
lsmean_diff_ci = formatters::with_label(character(), f_conf_level(conf_level)), |
158 | 1x |
pval = formatters::with_label(character(), "p-value") |
159 |
)
|
|
160 |
} else { |
|
161 |
# Estimate the differences between the marginal means.
|
|
162 | 1x |
emmeans_contrasts <- emmeans::contrast( |
163 | 1x |
emmeans_fit,
|
164 |
# Compare all arms versus the control arm.
|
|
165 | 1x |
method = "trt.vs.ctrl", |
166 |
# Take the arm factor from .ref_group as the control arm.
|
|
167 | 1x |
ref = ref_key, |
168 | 1x |
level = conf_level |
169 |
)
|
|
170 | 1x |
sum_contrasts <- summary( |
171 | 1x |
emmeans_contrasts,
|
172 |
# Derive confidence intervals, t-tests and p-values.
|
|
173 | 1x |
infer = TRUE, |
174 |
# Do not adjust the p-values for multiplicity.
|
|
175 | 1x |
adjust = "none" |
176 |
)
|
|
177 | ||
178 | 1x |
sum_contrasts_level <- sum_contrasts[grepl(sum_level, sum_contrasts$contrast), ] |
179 | 1x |
if (interaction_y != FALSE) { |
180 | ! |
sum_contrasts_level <- sum_contrasts_level[interaction_y, ] |
181 |
}
|
|
182 | ||
183 | 1x |
list( |
184 | 1x |
n = length(y[!is.na(y)]), |
185 | 1x |
lsmean = formatters::with_label(sum_fit_level$emmean, "Adjusted Mean"), |
186 | 1x |
lsmean_diff = formatters::with_label(sum_contrasts_level$estimate, "Difference in Adjusted Means"), |
187 | 1x |
lsmean_diff_ci = formatters::with_label( |
188 | 1x |
c(sum_contrasts_level$lower.CL, sum_contrasts_level$upper.CL), |
189 | 1x |
f_conf_level(conf_level) |
190 |
),
|
|
191 | 1x |
pval = formatters::with_label(sum_contrasts_level$p.value, "p-value") |
192 |
)
|
|
193 |
}
|
|
194 |
}
|
|
195 | ||
196 |
#' @describeIn summarize_ancova Formatted analysis function which is used as `afun` in `summarize_ancova()`.
|
|
197 |
#'
|
|
198 |
#' @return
|
|
199 |
#' * `a_ancova()` returns the corresponding list with formatted [rtables::CellValue()].
|
|
200 |
#'
|
|
201 |
#'
|
|
202 |
#' @keywords internal
|
|
203 |
a_ancova <- make_afun( |
|
204 |
s_ancova,
|
|
205 |
.indent_mods = c("n" = 0L, "lsmean" = 0L, "lsmean_diff" = 0L, "lsmean_diff_ci" = 1L, "pval" = 1L), |
|
206 |
.formats = c( |
|
207 |
"n" = "xx", |
|
208 |
"lsmean" = "xx.xx", |
|
209 |
"lsmean_diff" = "xx.xx", |
|
210 |
"lsmean_diff_ci" = "(xx.xx, xx.xx)", |
|
211 |
"pval" = "x.xxxx | (<0.0001)" |
|
212 |
),
|
|
213 |
.null_ref_cells = FALSE |
|
214 |
)
|
|
215 | ||
216 |
#' @describeIn summarize_ancova Layout-creating function which can take statistics function arguments
|
|
217 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()].
|
|
218 |
#'
|
|
219 |
#' @return
|
|
220 |
#' * `summarize_ancova()` returns a layout object suitable for passing to further layouting functions,
|
|
221 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing
|
|
222 |
#' the statistics from `s_ancova()` to the table layout.
|
|
223 |
#'
|
|
224 |
#' @examples
|
|
225 |
#' basic_table() %>%
|
|
226 |
#' split_cols_by("Species", ref_group = "setosa") %>%
|
|
227 |
#' add_colcounts() %>%
|
|
228 |
#' summarize_ancova(
|
|
229 |
#' vars = "Petal.Length",
|
|
230 |
#' variables = list(arm = "Species", covariates = NULL),
|
|
231 |
#' table_names = "unadj",
|
|
232 |
#' conf_level = 0.95, var_labels = "Unadjusted comparison",
|
|
233 |
#' .labels = c(lsmean = "Mean", lsmean_diff = "Difference in Means")
|
|
234 |
#' ) %>%
|
|
235 |
#' summarize_ancova(
|
|
236 |
#' vars = "Petal.Length",
|
|
237 |
#' variables = list(arm = "Species", covariates = c("Sepal.Length", "Sepal.Width")),
|
|
238 |
#' table_names = "adj",
|
|
239 |
#' conf_level = 0.95, var_labels = "Adjusted comparison (covariates: Sepal.Length and Sepal.Width)"
|
|
240 |
#' ) %>%
|
|
241 |
#' build_table(iris)
|
|
242 |
#'
|
|
243 |
#' @export
|
|
244 |
summarize_ancova <- function(lyt, |
|
245 |
vars,
|
|
246 |
var_labels,
|
|
247 |
...,
|
|
248 |
show_labels = "visible", |
|
249 |
table_names = vars, |
|
250 |
.stats = NULL, |
|
251 |
.formats = NULL, |
|
252 |
.labels = NULL, |
|
253 |
.indent_mods = NULL, |
|
254 |
interaction_y = FALSE, |
|
255 |
interaction_item = NULL) { |
|
256 | 3x |
afun <- make_afun( |
257 | 3x |
a_ancova,
|
258 | 3x |
interaction_y = interaction_y, |
259 | 3x |
interaction_item = interaction_item, |
260 | 3x |
.stats = .stats, |
261 | 3x |
.formats = .formats, |
262 | 3x |
.labels = .labels, |
263 | 3x |
.indent_mods = .indent_mods |
264 |
)
|
|
265 | ||
266 | 3x |
analyze( |
267 | 3x |
lyt,
|
268 | 3x |
vars,
|
269 | 3x |
var_labels = var_labels, |
270 | 3x |
show_labels = show_labels, |
271 | 3x |
table_names = table_names, |
272 | 3x |
afun = afun, |
273 | 3x |
extra_args = list(...) |
274 |
)
|
|
275 |
}
|
1 |
#' Tabulate Biomarker Effects on Survival by Subgroup
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Tabulate the estimated effects of multiple continuous biomarker variables
|
|
6 |
#' across population subgroups.
|
|
7 |
#'
|
|
8 |
#' @inheritParams argument_convention
|
|
9 |
#' @inheritParams fit_coxreg_multivar
|
|
10 |
#' @inheritParams survival_duration_subgroups
|
|
11 |
#'
|
|
12 |
#' @details These functions create a layout starting from a data frame which contains
|
|
13 |
#' the required statistics. The tables are then typically used as input for forest plots.
|
|
14 |
#'
|
|
15 |
#' @examples
|
|
16 |
#' library(dplyr)
|
|
17 |
#'
|
|
18 |
#' adtte <- tern_ex_adtte
|
|
19 |
#'
|
|
20 |
#' # Save variable labels before data processing steps.
|
|
21 |
#' adtte_labels <- formatters::var_labels(adtte)
|
|
22 |
#'
|
|
23 |
#' adtte_f <- adtte %>%
|
|
24 |
#' filter(PARAMCD == "OS") %>%
|
|
25 |
#' mutate(
|
|
26 |
#' AVALU = as.character(AVALU),
|
|
27 |
#' is_event = CNSR == 0
|
|
28 |
#' )
|
|
29 |
#' labels <- c("AVALU" = adtte_labels[["AVALU"]], "is_event" = "Event Flag")
|
|
30 |
#' formatters::var_labels(adtte_f)[names(labels)] <- labels
|
|
31 |
#'
|
|
32 |
#' df <- extract_survival_biomarkers(
|
|
33 |
#' variables = list(
|
|
34 |
#' tte = "AVAL",
|
|
35 |
#' is_event = "is_event",
|
|
36 |
#' biomarkers = c("BMRKR1", "AGE"),
|
|
37 |
#' strata = "STRATA1",
|
|
38 |
#' covariates = "SEX",
|
|
39 |
#' subgroups = "BMRKR2"
|
|
40 |
#' ),
|
|
41 |
#' data = adtte_f
|
|
42 |
#' )
|
|
43 |
#' df
|
|
44 |
#'
|
|
45 |
#' @name survival_biomarkers_subgroups
|
|
46 |
NULL
|
|
47 | ||
48 |
#' Prepares Survival Data Estimates for Multiple Biomarkers in a Single Data Frame
|
|
49 |
#'
|
|
50 |
#' @description `r lifecycle::badge("stable")`
|
|
51 |
#'
|
|
52 |
#' Prepares estimates for number of events, patients and median survival times, as well as hazard ratio estimates,
|
|
53 |
#' confidence intervals and p-values, for multiple biomarkers across population subgroups in a single data frame.
|
|
54 |
#' `variables` corresponds to the names of variables found in `data`, passed as a named `list` and requires elements
|
|
55 |
#' `tte`, `is_event`, `biomarkers` (vector of continuous biomarker variables), and optionally `subgroups` and `strat`.
|
|
56 |
#' `groups_lists` optionally specifies groupings for `subgroups` variables.
|
|
57 |
#'
|
|
58 |
#' @inheritParams argument_convention
|
|
59 |
#' @inheritParams fit_coxreg_multivar
|
|
60 |
#' @inheritParams survival_duration_subgroups
|
|
61 |
#'
|
|
62 |
#' @return A `data.frame` with columns `biomarker`, `biomarker_label`, `n_tot`, `n_tot_events`,
|
|
63 |
#' `median`, `hr`, `lcl`, `ucl`, `conf_level`, `pval`, `pval_label`, `subgroup`, `var`,
|
|
64 |
#' `var_label`, and `row_type`.
|
|
65 |
#'
|
|
66 |
#' @seealso [h_coxreg_mult_cont_df()] which is used internally, [tabulate_survival_biomarkers()].
|
|
67 |
#'
|
|
68 |
#' @examples
|
|
69 |
#' # Typical analysis of two continuous biomarkers `BMRKR1` and `AGE`,
|
|
70 |
#' # in multiple regression models containing one covariate `RACE`,
|
|
71 |
#' # as well as one stratification variable `STRATA1`. The subgroups
|
|
72 |
#' # are defined by the levels of `BMRKR2`.
|
|
73 |
#'
|
|
74 |
#' library(dplyr)
|
|
75 |
#'
|
|
76 |
#' adtte <- tern_ex_adtte
|
|
77 |
#' adtte_labels <- formatters::var_labels(adtte)
|
|
78 |
#'
|
|
79 |
#' adtte_f <- adtte %>%
|
|
80 |
#' filter(PARAMCD == "OS") %>%
|
|
81 |
#' mutate(
|
|
82 |
#' AVALU = as.character(AVALU),
|
|
83 |
#' is_event = CNSR == 0
|
|
84 |
#' )
|
|
85 |
#' labels <- c("AVALU" = adtte_labels[["AVALU"]], "is_event" = "Event Flag")
|
|
86 |
#' formatters::var_labels(adtte_f)[names(labels)] <- labels
|
|
87 |
#'
|
|
88 |
#' df <- extract_survival_biomarkers(
|
|
89 |
#' variables = list(
|
|
90 |
#' tte = "AVAL",
|
|
91 |
#' is_event = "is_event",
|
|
92 |
#' biomarkers = c("BMRKR1", "AGE"),
|
|
93 |
#' strata = "STRATA1",
|
|
94 |
#' covariates = "SEX",
|
|
95 |
#' subgroups = "BMRKR2"
|
|
96 |
#' ),
|
|
97 |
#' data = adtte_f
|
|
98 |
#' )
|
|
99 |
#' df
|
|
100 |
#'
|
|
101 |
#' # Here we group the levels of `BMRKR2` manually.
|
|
102 |
#' df_grouped <- extract_survival_biomarkers(
|
|
103 |
#' variables = list(
|
|
104 |
#' tte = "AVAL",
|
|
105 |
#' is_event = "is_event",
|
|
106 |
#' biomarkers = c("BMRKR1", "AGE"),
|
|
107 |
#' strata = "STRATA1",
|
|
108 |
#' covariates = "SEX",
|
|
109 |
#' subgroups = "BMRKR2"
|
|
110 |
#' ),
|
|
111 |
#' data = adtte_f,
|
|
112 |
#' groups_lists = list(
|
|
113 |
#' BMRKR2 = list(
|
|
114 |
#' "low" = "LOW",
|
|
115 |
#' "low/medium" = c("LOW", "MEDIUM"),
|
|
116 |
#' "low/medium/high" = c("LOW", "MEDIUM", "HIGH")
|
|
117 |
#' )
|
|
118 |
#' )
|
|
119 |
#' )
|
|
120 |
#' df_grouped
|
|
121 |
#'
|
|
122 |
#' @export
|
|
123 |
extract_survival_biomarkers <- function(variables, |
|
124 |
data,
|
|
125 |
groups_lists = list(), |
|
126 |
control = control_coxreg(), |
|
127 |
label_all = "All Patients") { |
|
128 | 4x |
checkmate::assert_list(variables) |
129 | 4x |
checkmate::assert_character(variables$subgroups, null.ok = TRUE) |
130 | 4x |
checkmate::assert_string(label_all) |
131 | ||
132 |
# Start with all patients.
|
|
133 | 4x |
result_all <- h_coxreg_mult_cont_df( |
134 | 4x |
variables = variables, |
135 | 4x |
data = data, |
136 | 4x |
control = control |
137 |
)
|
|
138 | 4x |
result_all$subgroup <- label_all |
139 | 4x |
result_all$var <- "ALL" |
140 | 4x |
result_all$var_label <- label_all |
141 | 4x |
result_all$row_type <- "content" |
142 | 4x |
if (is.null(variables$subgroups)) { |
143 |
# Only return result for all patients.
|
|
144 | 1x |
result_all
|
145 |
} else { |
|
146 |
# Add subgroups results.
|
|
147 | 3x |
l_data <- h_split_by_subgroups( |
148 | 3x |
data,
|
149 | 3x |
variables$subgroups, |
150 | 3x |
groups_lists = groups_lists |
151 |
)
|
|
152 | 3x |
l_result <- lapply(l_data, function(grp) { |
153 | 15x |
result <- h_coxreg_mult_cont_df( |
154 | 15x |
variables = variables, |
155 | 15x |
data = grp$df, |
156 | 15x |
control = control |
157 |
)
|
|
158 | 15x |
result_labels <- grp$df_labels[rep(1, times = nrow(result)), ] |
159 | 15x |
cbind(result, result_labels) |
160 |
}) |
|
161 | 3x |
result_subgroups <- do.call(rbind, args = c(l_result, make.row.names = FALSE)) |
162 | 3x |
result_subgroups$row_type <- "analysis" |
163 | 3x |
rbind( |
164 | 3x |
result_all,
|
165 | 3x |
result_subgroups
|
166 |
)
|
|
167 |
}
|
|
168 |
}
|
|
169 | ||
170 |
#' @describeIn survival_biomarkers_subgroups Table-creating function which creates a table
|
|
171 |
#' summarizing biomarker effects on survival by subgroup.
|
|
172 |
#'
|
|
173 |
#' @param df (`data.frame`)\cr containing all analysis variables, as returned by
|
|
174 |
#' [extract_survival_biomarkers()].
|
|
175 |
#' @param vars (`character`)\cr the names of statistics to be reported among:
|
|
176 |
#' * `n_tot_events`: Total number of events per group.
|
|
177 |
#' * `n_tot`: Total number of observations per group.
|
|
178 |
#' * `median`: Median survival time.
|
|
179 |
#' * `hr`: Hazard ratio.
|
|
180 |
#' * `ci`: Confidence interval of hazard ratio.
|
|
181 |
#' * `pval`: p-value of the effect.
|
|
182 |
#' Note, one of the statistics `n_tot` and `n_tot_events`, as well as both `hr` and `ci` are required.
|
|
183 |
#'
|
|
184 |
#' @return An `rtables` table summarizing biomarker effects on survival by subgroup.
|
|
185 |
#'
|
|
186 |
#' @note In contrast to [tabulate_survival_subgroups()] this tabulation function does
|
|
187 |
#' not start from an input layout `lyt`. This is because internally the table is
|
|
188 |
#' created by combining multiple subtables.
|
|
189 |
#'
|
|
190 |
#' @seealso [h_tab_surv_one_biomarker()] which is used internally, [extract_survival_biomarkers()].
|
|
191 |
#'
|
|
192 |
#' @examples
|
|
193 |
#' ## Table with default columns.
|
|
194 |
#' tabulate_survival_biomarkers(df)
|
|
195 |
#'
|
|
196 |
#' ## Table with a manually chosen set of columns: leave out "pval", reorder.
|
|
197 |
#' tab <- tabulate_survival_biomarkers(
|
|
198 |
#' df = df,
|
|
199 |
#' vars = c("n_tot_events", "ci", "n_tot", "median", "hr"),
|
|
200 |
#' time_unit = as.character(adtte_f$AVALU[1])
|
|
201 |
#' )
|
|
202 |
#'
|
|
203 |
#' ## Finally produce the forest plot.
|
|
204 |
#' \donttest{
|
|
205 |
#' g_forest(tab, xlim = c(0.8, 1.2))
|
|
206 |
#' }
|
|
207 |
#'
|
|
208 |
#' @export
|
|
209 |
tabulate_survival_biomarkers <- function(df, |
|
210 |
vars = c("n_tot", "n_tot_events", "median", "hr", "ci", "pval"), |
|
211 |
time_unit = NULL, |
|
212 |
.indent_mods = 0L) { |
|
213 | 3x |
checkmate::assert_data_frame(df) |
214 | 3x |
checkmate::assert_character(df$biomarker) |
215 | 3x |
checkmate::assert_character(df$biomarker_label) |
216 | 3x |
checkmate::assert_subset(vars, c("n_tot", "n_tot_events", "median", "hr", "ci", "pval")) |
217 | ||
218 | 3x |
df_subs <- split(df, f = df$biomarker) |
219 | 3x |
tabs <- lapply(df_subs, FUN = function(df_sub) { |
220 | 5x |
tab_sub <- h_tab_surv_one_biomarker( |
221 | 5x |
df = df_sub, |
222 | 5x |
vars = vars, |
223 | 5x |
time_unit = time_unit, |
224 | 5x |
.indent_mods = .indent_mods |
225 |
)
|
|
226 |
# Insert label row as first row in table.
|
|
227 | 5x |
label_at_path(tab_sub, path = row_paths(tab_sub)[[1]][1]) <- df_sub$biomarker_label[1] |
228 | 5x |
tab_sub
|
229 |
}) |
|
230 | 3x |
result <- do.call(rbind, tabs) |
231 | ||
232 | 3x |
n_tot_ids <- grep("^n_tot", vars) |
233 | 3x |
hr_id <- match("hr", vars) |
234 | 3x |
ci_id <- match("ci", vars) |
235 | 3x |
structure( |
236 | 3x |
result,
|
237 | 3x |
forest_header = paste0(c("Higher", "Lower"), "\nBetter"), |
238 | 3x |
col_x = hr_id, |
239 | 3x |
col_ci = ci_id, |
240 | 3x |
col_symbol_size = n_tot_ids[1] |
241 |
)
|
|
242 |
}
|
1 |
#' Convert List of Groups to Data Frame
|
|
2 |
#'
|
|
3 |
#' This converts a list of group levels into a data frame format which is expected by [rtables::add_combo_levels()].
|
|
4 |
#'
|
|
5 |
#' @param groups_list (named `list` of `character`)\cr specifies the new group levels via the names and the
|
|
6 |
#' levels that belong to it in the character vectors that are elements of the list.
|
|
7 |
#'
|
|
8 |
#' @return [tibble::tibble()] in the required format.
|
|
9 |
#'
|
|
10 |
#' @examples
|
|
11 |
#' grade_groups <- list(
|
|
12 |
#' "Any Grade (%)" = c("1", "2", "3", "4", "5"),
|
|
13 |
#' "Grade 3-4 (%)" = c("3", "4"),
|
|
14 |
#' "Grade 5 (%)" = "5"
|
|
15 |
#' )
|
|
16 |
#' groups_list_to_df(grade_groups)
|
|
17 |
#'
|
|
18 |
#' @export
|
|
19 |
groups_list_to_df <- function(groups_list) { |
|
20 | 5x |
checkmate::assert_list(groups_list, names = "named") |
21 | 5x |
lapply(groups_list, checkmate::assert_character) |
22 | 5x |
tibble::tibble( |
23 | 5x |
valname = make_names(names(groups_list)), |
24 | 5x |
label = names(groups_list), |
25 | 5x |
levelcombo = unname(groups_list), |
26 | 5x |
exargs = replicate(length(groups_list), list()) |
27 |
)
|
|
28 |
}
|
|
29 | ||
30 |
#' Reference and Treatment Group Combination
|
|
31 |
#'
|
|
32 |
#' @description `r lifecycle::badge("stable")`
|
|
33 |
#'
|
|
34 |
#' Facilitate the re-combination of groups divided as reference and treatment groups; it helps in arranging groups of
|
|
35 |
#' columns in the `rtables` framework and teal modules.
|
|
36 |
#'
|
|
37 |
#' @param fct (`factor`)\cr the variable with levels which needs to be grouped.
|
|
38 |
#' @param ref (`string`)\cr the reference level(s).
|
|
39 |
#' @param collapse (`string`)\cr a character string to separate `fct` and `ref`.
|
|
40 |
#'
|
|
41 |
#' @return A `list` with first item `ref` (reference) and second item `trt` (treatment).
|
|
42 |
#'
|
|
43 |
#' @examples
|
|
44 |
#' groups <- combine_groups(
|
|
45 |
#' fct = DM$ARM,
|
|
46 |
#' ref = c("B: Placebo")
|
|
47 |
#' )
|
|
48 |
#'
|
|
49 |
#' basic_table() %>%
|
|
50 |
#' split_cols_by_groups("ARM", groups) %>%
|
|
51 |
#' add_colcounts() %>%
|
|
52 |
#' summarize_vars("AGE") %>%
|
|
53 |
#' build_table(DM)
|
|
54 |
#'
|
|
55 |
#' @export
|
|
56 |
combine_groups <- function(fct, |
|
57 |
ref = NULL, |
|
58 |
collapse = "/") { |
|
59 | 10x |
checkmate::assert_string(collapse) |
60 | 10x |
checkmate::assert_character(ref, min.chars = 1, any.missing = FALSE, null.ok = TRUE) |
61 | 10x |
checkmate::assert_multi_class(fct, classes = c("factor", "character")) |
62 | ||
63 | 10x |
fct <- as_factor_keep_attributes(fct) |
64 | ||
65 | 10x |
group_levels <- levels(fct) |
66 | 10x |
if (is.null(ref)) { |
67 | 6x |
ref <- group_levels[1] |
68 |
} else { |
|
69 | 4x |
checkmate::assert_subset(ref, group_levels) |
70 |
}
|
|
71 | ||
72 | 10x |
groups <- list( |
73 | 10x |
ref = group_levels[group_levels %in% ref], |
74 | 10x |
trt = group_levels[!group_levels %in% ref] |
75 |
)
|
|
76 | 10x |
stats::setNames(groups, nm = lapply(groups, paste, collapse = collapse)) |
77 |
}
|
|
78 | ||
79 |
#' Split Columns by Groups of Levels
|
|
80 |
#'
|
|
81 |
#' @description `r lifecycle::badge("stable")`
|
|
82 |
#'
|
|
83 |
#' @inheritParams argument_convention
|
|
84 |
#' @inheritParams groups_list_to_df
|
|
85 |
#' @param ... additional arguments to [rtables::split_cols_by()] in order. For instance, to
|
|
86 |
#' control formats (`format`), add a joint column for all groups (`incl_all`).
|
|
87 |
#'
|
|
88 |
#' @return A layout object suitable for passing to further layouting functions. Adding
|
|
89 |
#' this function to an `rtable` layout will add a column split including the given
|
|
90 |
#' groups to the table layout.
|
|
91 |
#'
|
|
92 |
#' @seealso [rtables::split_cols_by()]
|
|
93 |
#'
|
|
94 |
#' @examples
|
|
95 |
#' # 1 - Basic use
|
|
96 |
#'
|
|
97 |
#' # Without group combination `split_cols_by_groups` is
|
|
98 |
#' # equivalent to [rtables::split_cols_by()].
|
|
99 |
#' basic_table() %>%
|
|
100 |
#' split_cols_by_groups("ARM") %>%
|
|
101 |
#' add_colcounts() %>%
|
|
102 |
#' analyze("AGE") %>%
|
|
103 |
#' build_table(DM)
|
|
104 |
#'
|
|
105 |
#' # Add a reference column.
|
|
106 |
#' basic_table() %>%
|
|
107 |
#' split_cols_by_groups("ARM", ref_group = "B: Placebo") %>%
|
|
108 |
#' add_colcounts() %>%
|
|
109 |
#' analyze(
|
|
110 |
#' "AGE",
|
|
111 |
#' afun = function(x, .ref_group, .in_ref_col) {
|
|
112 |
#' if (.in_ref_col) {
|
|
113 |
#' in_rows("Diff Mean" = rcell(NULL))
|
|
114 |
#' } else {
|
|
115 |
#' in_rows("Diff Mean" = rcell(mean(x) - mean(.ref_group), format = "xx.xx"))
|
|
116 |
#' }
|
|
117 |
#' }
|
|
118 |
#' ) %>%
|
|
119 |
#' build_table(DM)
|
|
120 |
#'
|
|
121 |
#' # 2 - Adding group specification
|
|
122 |
#'
|
|
123 |
#' # Manual preparation of the groups.
|
|
124 |
#' groups <- list(
|
|
125 |
#' "Arms A+B" = c("A: Drug X", "B: Placebo"),
|
|
126 |
#' "Arms A+C" = c("A: Drug X", "C: Combination")
|
|
127 |
#' )
|
|
128 |
#'
|
|
129 |
#' # Use of split_cols_by_groups without reference column.
|
|
130 |
#' basic_table() %>%
|
|
131 |
#' split_cols_by_groups("ARM", groups) %>%
|
|
132 |
#' add_colcounts() %>%
|
|
133 |
#' analyze("AGE") %>%
|
|
134 |
#' build_table(DM)
|
|
135 |
#'
|
|
136 |
#' # Including differentiated output in the reference column.
|
|
137 |
#' basic_table() %>%
|
|
138 |
#' split_cols_by_groups("ARM", groups_list = groups, ref_group = "Arms A+B") %>%
|
|
139 |
#' analyze(
|
|
140 |
#' "AGE",
|
|
141 |
#' afun = function(x, .ref_group, .in_ref_col) {
|
|
142 |
#' if (.in_ref_col) {
|
|
143 |
#' in_rows("Diff. of Averages" = rcell(NULL))
|
|
144 |
#' } else {
|
|
145 |
#' in_rows("Diff. of Averages" = rcell(mean(x) - mean(.ref_group), format = "xx.xx"))
|
|
146 |
#' }
|
|
147 |
#' }
|
|
148 |
#' ) %>%
|
|
149 |
#' build_table(DM)
|
|
150 |
#'
|
|
151 |
#' # 3 - Binary list dividing factor levels into reference and treatment
|
|
152 |
#'
|
|
153 |
#' # `combine_groups` defines reference and treatment.
|
|
154 |
#' groups <- combine_groups(
|
|
155 |
#' fct = DM$ARM,
|
|
156 |
#' ref = c("A: Drug X", "B: Placebo")
|
|
157 |
#' )
|
|
158 |
#' groups
|
|
159 |
#'
|
|
160 |
#' # Use group definition without reference column.
|
|
161 |
#' basic_table() %>%
|
|
162 |
#' split_cols_by_groups("ARM", groups_list = groups) %>%
|
|
163 |
#' add_colcounts() %>%
|
|
164 |
#' analyze("AGE") %>%
|
|
165 |
#' build_table(DM)
|
|
166 |
#'
|
|
167 |
#' # Use group definition with reference column (first item of groups).
|
|
168 |
#' basic_table() %>%
|
|
169 |
#' split_cols_by_groups("ARM", groups, ref_group = names(groups)[1]) %>%
|
|
170 |
#' add_colcounts() %>%
|
|
171 |
#' analyze(
|
|
172 |
#' "AGE",
|
|
173 |
#' afun = function(x, .ref_group, .in_ref_col) {
|
|
174 |
#' if (.in_ref_col) {
|
|
175 |
#' in_rows("Diff Mean" = rcell(NULL))
|
|
176 |
#' } else {
|
|
177 |
#' in_rows("Diff Mean" = rcell(mean(x) - mean(.ref_group), format = "xx.xx"))
|
|
178 |
#' }
|
|
179 |
#' }
|
|
180 |
#' ) %>%
|
|
181 |
#' build_table(DM)
|
|
182 |
#'
|
|
183 |
#' @export
|
|
184 |
split_cols_by_groups <- function(lyt, |
|
185 |
var,
|
|
186 |
groups_list = NULL, |
|
187 |
ref_group = NULL, |
|
188 |
...) { |
|
189 | 6x |
if (is.null(groups_list)) { |
190 | 2x |
split_cols_by( |
191 | 2x |
lyt = lyt, |
192 | 2x |
var = var, |
193 | 2x |
ref_group = ref_group, |
194 |
...
|
|
195 |
)
|
|
196 |
} else { |
|
197 | 4x |
groups_df <- groups_list_to_df(groups_list) |
198 | 4x |
if (!is.null(ref_group)) { |
199 | 3x |
ref_group <- groups_df$valname[groups_df$label == ref_group] |
200 |
}
|
|
201 | 4x |
split_cols_by( |
202 | 4x |
lyt = lyt, |
203 | 4x |
var = var, |
204 | 4x |
split_fun = add_combo_levels(groups_df, keep_levels = groups_df$valname), |
205 | 4x |
ref_group = ref_group, |
206 |
...
|
|
207 |
)
|
|
208 |
}
|
|
209 |
}
|
|
210 | ||
211 |
#' Combine Counts
|
|
212 |
#'
|
|
213 |
#' Simplifies the estimation of column counts, especially when group combination is required.
|
|
214 |
#'
|
|
215 |
#' @inheritParams combine_groups
|
|
216 |
#' @inheritParams groups_list_to_df
|
|
217 |
#'
|
|
218 |
#' @return A `vector` of column counts.
|
|
219 |
#'
|
|
220 |
#' @seealso [combine_groups()]
|
|
221 |
#'
|
|
222 |
#' @examples
|
|
223 |
#' ref <- c("A: Drug X", "B: Placebo")
|
|
224 |
#' groups <- combine_groups(fct = DM$ARM, ref = ref)
|
|
225 |
#'
|
|
226 |
#' col_counts <- combine_counts(
|
|
227 |
#' fct = DM$ARM,
|
|
228 |
#' groups_list = groups
|
|
229 |
#' )
|
|
230 |
#'
|
|
231 |
#' basic_table() %>%
|
|
232 |
#' split_cols_by_groups("ARM", groups) %>%
|
|
233 |
#' add_colcounts() %>%
|
|
234 |
#' summarize_vars("AGE") %>%
|
|
235 |
#' build_table(DM, col_counts = col_counts)
|
|
236 |
#'
|
|
237 |
#' ref <- "A: Drug X"
|
|
238 |
#' groups <- combine_groups(fct = DM$ARM, ref = ref)
|
|
239 |
#' col_counts <- combine_counts(
|
|
240 |
#' fct = DM$ARM,
|
|
241 |
#' groups_list = groups
|
|
242 |
#' )
|
|
243 |
#'
|
|
244 |
#' basic_table() %>%
|
|
245 |
#' split_cols_by_groups("ARM", groups) %>%
|
|
246 |
#' add_colcounts() %>%
|
|
247 |
#' summarize_vars("AGE") %>%
|
|
248 |
#' build_table(DM, col_counts = col_counts)
|
|
249 |
#'
|
|
250 |
#' @export
|
|
251 |
combine_counts <- function(fct, groups_list = NULL) { |
|
252 | 4x |
checkmate::assert_multi_class(fct, classes = c("factor", "character")) |
253 | ||
254 | 4x |
fct <- as_factor_keep_attributes(fct) |
255 | ||
256 | 4x |
if (is.null(groups_list)) { |
257 | 1x |
y <- table(fct) |
258 | 1x |
y <- stats::setNames(as.numeric(y), nm = dimnames(y)[[1]]) |
259 |
} else { |
|
260 | 3x |
y <- vapply( |
261 | 3x |
X = groups_list, |
262 | 3x |
FUN = function(x) sum(table(fct)[x]), |
263 | 3x |
FUN.VALUE = 1 |
264 |
)
|
|
265 |
}
|
|
266 | 4x |
y
|
267 |
}
|
1 |
#' Survival Time Point Analysis
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Summarize patients' survival rate and difference of survival rates between groups at a time point.
|
|
6 |
#'
|
|
7 |
#' @inheritParams argument_convention
|
|
8 |
#' @inheritParams s_surv_time
|
|
9 |
#' @param time_point (`number`)\cr survival time point of interest.
|
|
10 |
#' @param control (`list`)\cr parameters for comparison details, specified by using the helper function
|
|
11 |
#' [control_surv_timepoint()]. Some possible parameter options are:
|
|
12 |
#' * `conf_level` (`proportion`)\cr confidence level of the interval for survival rate.
|
|
13 |
#' * `conf_type` (`string`)\cr confidence interval type. Options are "plain" (default), "log", "log-log",
|
|
14 |
#' see more in [survival::survfit()]. Note option "none" is no longer supported.
|
|
15 |
#' * `time_point` (`number`)\cr survival time point of interest.
|
|
16 |
#'
|
|
17 |
#' @name survival_timepoint
|
|
18 |
NULL
|
|
19 | ||
20 |
#' @describeIn survival_timepoint Statistics function which analyzes survival rate.
|
|
21 |
#'
|
|
22 |
#' @return
|
|
23 |
#' * `s_surv_timepoint()` returns the statistics:
|
|
24 |
#' * `pt_at_risk`: Patients remaining at risk.
|
|
25 |
#' * `event_free_rate`: Event-free rate (%).
|
|
26 |
#' * `rate_se`: Standard error of event free rate.
|
|
27 |
#' * `rate_ci`: Confidence interval for event free rate.
|
|
28 |
#'
|
|
29 |
#' @examples
|
|
30 |
#' library(dplyr)
|
|
31 |
#'
|
|
32 |
#' adtte_f <- tern_ex_adtte %>%
|
|
33 |
#' filter(PARAMCD == "OS") %>%
|
|
34 |
#' mutate(
|
|
35 |
#' AVAL = day2month(AVAL),
|
|
36 |
#' is_event = CNSR == 0
|
|
37 |
#' )
|
|
38 |
#' df <- adtte_f %>%
|
|
39 |
#' filter(ARMCD == "ARM A")
|
|
40 |
#'
|
|
41 |
#' @keywords internal
|
|
42 |
s_surv_timepoint <- function(df, |
|
43 |
.var,
|
|
44 |
time_point,
|
|
45 |
is_event,
|
|
46 |
control = control_surv_timepoint()) { |
|
47 | 19x |
checkmate::assert_string(.var) |
48 | 19x |
assert_df_with_variables(df, list(tte = .var, is_event = is_event)) |
49 | 19x |
checkmate::assert_numeric(df[[.var]], min.len = 1, any.missing = FALSE) |
50 | 19x |
checkmate::assert_number(time_point) |
51 | 19x |
checkmate::assert_logical(df[[is_event]], min.len = 1, any.missing = FALSE) |
52 | ||
53 | 19x |
conf_type <- control$conf_type |
54 | 19x |
conf_level <- control$conf_level |
55 | ||
56 | 19x |
formula <- stats::as.formula(paste0("survival::Surv(", .var, ", ", is_event, ") ~ 1")) |
57 | 19x |
srv_fit <- survival::survfit( |
58 | 19x |
formula = formula, |
59 | 19x |
data = df, |
60 | 19x |
conf.int = conf_level, |
61 | 19x |
conf.type = conf_type |
62 |
)
|
|
63 | 19x |
s_srv_fit <- summary(srv_fit, times = time_point, extend = TRUE) |
64 | 19x |
df_srv_fit <- as.data.frame(s_srv_fit[c("time", "n.risk", "surv", "lower", "upper", "std.err")]) |
65 | 19x |
if (df_srv_fit[["n.risk"]] == 0) { |
66 | 1x |
pt_at_risk <- event_free_rate <- rate_se <- NA_real_ |
67 | 1x |
rate_ci <- c(NA_real_, NA_real_) |
68 |
} else { |
|
69 | 18x |
pt_at_risk <- df_srv_fit$n.risk |
70 | 18x |
event_free_rate <- df_srv_fit$surv |
71 | 18x |
rate_se <- df_srv_fit$std.err |
72 | 18x |
rate_ci <- c(df_srv_fit$lower, df_srv_fit$upper) |
73 |
}
|
|
74 | 19x |
list( |
75 | 19x |
pt_at_risk = formatters::with_label(pt_at_risk, "Patients remaining at risk"), |
76 | 19x |
event_free_rate = formatters::with_label(event_free_rate * 100, "Event Free Rate (%)"), |
77 | 19x |
rate_se = formatters::with_label(rate_se * 100, "Standard Error of Event Free Rate"), |
78 | 19x |
rate_ci = formatters::with_label(rate_ci * 100, f_conf_level(conf_level)) |
79 |
)
|
|
80 |
}
|
|
81 | ||
82 |
#' @describeIn survival_timepoint Formatted analysis function which is used as `afun` in `surv_timepoint()`
|
|
83 |
#' when `method = "surv"`.
|
|
84 |
#'
|
|
85 |
#' @return
|
|
86 |
#' * `a_surv_timepoint()` returns the corresponding list with formatted [rtables::CellValue()].
|
|
87 |
#'
|
|
88 |
#' @keywords internal
|
|
89 |
a_surv_timepoint <- make_afun( |
|
90 |
s_surv_timepoint,
|
|
91 |
.indent_mods = c( |
|
92 |
pt_at_risk = 0L, |
|
93 |
event_free_rate = 0L, |
|
94 |
rate_se = 1L, |
|
95 |
rate_ci = 1L |
|
96 |
),
|
|
97 |
.formats = c( |
|
98 |
pt_at_risk = "xx", |
|
99 |
event_free_rate = "xx.xx", |
|
100 |
rate_se = "xx.xx", |
|
101 |
rate_ci = "(xx.xx, xx.xx)" |
|
102 |
)
|
|
103 |
)
|
|
104 | ||
105 |
#' @describeIn survival_timepoint Statistics function which analyzes difference between two survival rates.
|
|
106 |
#'
|
|
107 |
#' @return
|
|
108 |
#' * `s_surv_timepoint_diff()` returns the statistics:
|
|
109 |
#' * `rate_diff`: Event-free rate difference between two groups.
|
|
110 |
#' * `rate_diff_ci`: Confidence interval for the difference.
|
|
111 |
#' * `ztest_pval`: p-value to test the difference is 0.
|
|
112 |
#'
|
|
113 |
#' @examples
|
|
114 |
#' df_ref_group <- adtte_f %>%
|
|
115 |
#' filter(ARMCD == "ARM B")
|
|
116 |
#'
|
|
117 |
#' @keywords internal
|
|
118 |
s_surv_timepoint_diff <- function(df, |
|
119 |
.var,
|
|
120 |
.ref_group,
|
|
121 |
.in_ref_col,
|
|
122 |
time_point,
|
|
123 |
control = control_surv_timepoint(), |
|
124 |
...) { |
|
125 | 2x |
if (.in_ref_col) { |
126 | ! |
return( |
127 | ! |
list( |
128 | ! |
rate_diff = formatters::with_label("", "Difference in Event Free Rate"), |
129 | ! |
rate_diff_ci = formatters::with_label("", f_conf_level(control$conf_level)), |
130 | ! |
ztest_pval = formatters::with_label("", "p-value (Z-test)") |
131 |
)
|
|
132 |
)
|
|
133 |
}
|
|
134 | 2x |
data <- rbind(.ref_group, df) |
135 | 2x |
group <- factor(rep(c("ref", "x"), c(nrow(.ref_group), nrow(df))), levels = c("ref", "x")) |
136 | 2x |
res_per_group <- lapply(split(data, group), function(x) { |
137 | 4x |
s_surv_timepoint(df = x, .var = .var, time_point = time_point, control = control, ...) |
138 |
}) |
|
139 | ||
140 | 2x |
res_x <- res_per_group[[2]] |
141 | 2x |
res_ref <- res_per_group[[1]] |
142 | 2x |
rate_diff <- res_x$event_free_rate - res_ref$event_free_rate |
143 | 2x |
se_diff <- sqrt(res_x$rate_se^2 + res_ref$rate_se^2) |
144 | ||
145 | 2x |
qs <- c(-1, 1) * stats::qnorm(1 - (1 - control$conf_level) / 2) |
146 | 2x |
rate_diff_ci <- rate_diff + qs * se_diff |
147 | 2x |
ztest_pval <- if (is.na(rate_diff)) { |
148 | 2x |
NA
|
149 |
} else { |
|
150 | 2x |
2 * (1 - stats::pnorm(abs(rate_diff) / se_diff)) |
151 |
}
|
|
152 | 2x |
list( |
153 | 2x |
rate_diff = formatters::with_label(rate_diff, "Difference in Event Free Rate"), |
154 | 2x |
rate_diff_ci = formatters::with_label(rate_diff_ci, f_conf_level(control$conf_level)), |
155 | 2x |
ztest_pval = formatters::with_label(ztest_pval, "p-value (Z-test)") |
156 |
)
|
|
157 |
}
|
|
158 | ||
159 |
#' @describeIn survival_timepoint Formatted analysis function which is used as `afun` in `surv_timepoint()`
|
|
160 |
#' when `method = "surv_diff"`.
|
|
161 |
#'
|
|
162 |
#' @return
|
|
163 |
#' * `a_surv_timepoint_diff()` returns the corresponding list with formatted [rtables::CellValue()].
|
|
164 |
#'
|
|
165 |
#' @keywords internal
|
|
166 |
a_surv_timepoint_diff <- make_afun( |
|
167 |
s_surv_timepoint_diff,
|
|
168 |
.formats = c( |
|
169 |
rate_diff = "xx.xx", |
|
170 |
rate_diff_ci = "(xx.xx, xx.xx)", |
|
171 |
ztest_pval = "x.xxxx | (<0.0001)" |
|
172 |
)
|
|
173 |
)
|
|
174 | ||
175 |
#' @describeIn survival_timepoint Layout-creating function which can take statistics function arguments
|
|
176 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()].
|
|
177 |
#'
|
|
178 |
#' @param method (`string`)\cr either `surv` (survival estimations),
|
|
179 |
#' `surv_diff` (difference in survival with the control) or `both`.
|
|
180 |
#' @param table_names_suffix (`string`)\cr optional suffix for the `table_names` used for the `rtables` to
|
|
181 |
#' avoid warnings from duplicate table names.
|
|
182 |
#' @param .indent_mods (named `vector` of `integer`)\cr indent modifiers for the labels. Each element of the vector
|
|
183 |
#' should be a name-value pair with name corresponding to a statistic specified in `.stats` and value the indentation
|
|
184 |
#' for that statistic's row label.
|
|
185 |
#'
|
|
186 |
#' @return
|
|
187 |
#' * `surv_timepoint()` returns a layout object suitable for passing to further layouting functions,
|
|
188 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing
|
|
189 |
#' the statistics from `s_surv_timepoint()` and/or `s_surv_timepoint_diff()` to the table layout depending on
|
|
190 |
#' the value of `method`.
|
|
191 |
#'
|
|
192 |
#' @examples
|
|
193 |
#' # Survival at given time points.
|
|
194 |
#' basic_table() %>%
|
|
195 |
#' split_cols_by(var = "ARMCD", ref_group = "ARM A") %>%
|
|
196 |
#' add_colcounts() %>%
|
|
197 |
#' surv_timepoint(
|
|
198 |
#' vars = "AVAL",
|
|
199 |
#' var_labels = "Months",
|
|
200 |
#' is_event = "is_event",
|
|
201 |
#' time_point = 7
|
|
202 |
#' ) %>%
|
|
203 |
#' build_table(df = adtte_f)
|
|
204 |
#'
|
|
205 |
#' # Difference in survival at given time points.
|
|
206 |
#' basic_table() %>%
|
|
207 |
#' split_cols_by(var = "ARMCD", ref_group = "ARM A") %>%
|
|
208 |
#' add_colcounts() %>%
|
|
209 |
#' surv_timepoint(
|
|
210 |
#' vars = "AVAL",
|
|
211 |
#' var_labels = "Months",
|
|
212 |
#' is_event = "is_event",
|
|
213 |
#' time_point = 9,
|
|
214 |
#' method = "surv_diff",
|
|
215 |
#' .indent_mods = c("rate_diff" = 0L, "rate_diff_ci" = 2L, "ztest_pval" = 2L)
|
|
216 |
#' ) %>%
|
|
217 |
#' build_table(df = adtte_f)
|
|
218 |
#'
|
|
219 |
#' # Survival and difference in survival at given time points.
|
|
220 |
#' basic_table() %>%
|
|
221 |
#' split_cols_by(var = "ARMCD", ref_group = "ARM A") %>%
|
|
222 |
#' add_colcounts() %>%
|
|
223 |
#' surv_timepoint(
|
|
224 |
#' vars = "AVAL",
|
|
225 |
#' var_labels = "Months",
|
|
226 |
#' is_event = "is_event",
|
|
227 |
#' time_point = 9,
|
|
228 |
#' method = "both"
|
|
229 |
#' ) %>%
|
|
230 |
#' build_table(df = adtte_f)
|
|
231 |
#'
|
|
232 |
#' @export
|
|
233 |
surv_timepoint <- function(lyt, |
|
234 |
vars,
|
|
235 |
...,
|
|
236 |
table_names_suffix = "", |
|
237 |
var_labels = "Time", |
|
238 |
show_labels = "visible", |
|
239 |
method = c("surv", "surv_diff", "both"), |
|
240 |
.stats = c( |
|
241 |
"pt_at_risk", "event_free_rate", "rate_ci", |
|
242 |
"rate_diff", "rate_diff_ci", "ztest_pval" |
|
243 |
),
|
|
244 |
.formats = NULL, |
|
245 |
.labels = NULL, |
|
246 |
.indent_mods = if (method == "both") { |
|
247 | 1x |
c(rate_diff = 1L, rate_diff_ci = 2L, ztest_pval = 2L) |
248 |
} else { |
|
249 | 4x |
c(rate_diff_ci = 1L, ztest_pval = 1L) |
250 |
}) { |
|
251 | 5x |
method <- match.arg(method) |
252 | 5x |
checkmate::assert_string(table_names_suffix) |
253 | ||
254 | 5x |
f <- list( |
255 | 5x |
surv = c("pt_at_risk", "event_free_rate", "rate_se", "rate_ci"), |
256 | 5x |
surv_diff = c("rate_diff", "rate_diff_ci", "ztest_pval") |
257 |
)
|
|
258 | 5x |
.stats <- h_split_param(.stats, .stats, f = f) |
259 | 5x |
.formats <- h_split_param(.formats, names(.formats), f = f) |
260 | 5x |
.labels <- h_split_param(.labels, names(.labels), f = f) |
261 | 5x |
.indent_mods <- h_split_param(.indent_mods, names(.indent_mods), f = f) |
262 | ||
263 | 5x |
afun_surv <- make_afun( |
264 | 5x |
a_surv_timepoint,
|
265 | 5x |
.stats = .stats$surv, |
266 | 5x |
.formats = .formats$surv, |
267 | 5x |
.labels = .labels$surv, |
268 | 5x |
.indent_mods = .indent_mods$surv |
269 |
)
|
|
270 | ||
271 | 5x |
afun_surv_diff <- make_afun( |
272 | 5x |
a_surv_timepoint_diff,
|
273 | 5x |
.stats = .stats$surv_diff, |
274 | 5x |
.formats = .formats$surv_diff, |
275 | 5x |
.labels = .labels$surv_diff, |
276 | 5x |
.indent_mods = .indent_mods$surv_diff |
277 |
)
|
|
278 | ||
279 | 5x |
time_point <- list(...)$time_point |
280 | ||
281 | 5x |
for (i in seq_along(time_point)) { |
282 | 5x |
tpt <- time_point[i] |
283 | ||
284 | 5x |
if (method %in% c("surv", "both")) { |
285 | 3x |
lyt <- analyze( |
286 | 3x |
lyt,
|
287 | 3x |
vars,
|
288 | 3x |
var_labels = paste(tpt, var_labels), |
289 | 3x |
table_names = paste0("surv_", tpt, table_names_suffix), |
290 | 3x |
show_labels = show_labels, |
291 | 3x |
afun = afun_surv, |
292 | 3x |
extra_args = list( |
293 | 3x |
is_event = list(...)$is_event, |
294 | 3x |
control = list(...)$control, |
295 | 3x |
time_point = tpt |
296 |
)
|
|
297 |
)
|
|
298 |
}
|
|
299 | ||
300 | 5x |
if (method %in% c("surv_diff", "both")) { |
301 | 3x |
lyt <- analyze( |
302 | 3x |
lyt,
|
303 | 3x |
vars,
|
304 | 3x |
var_labels = paste(tpt, var_labels), |
305 | 3x |
table_names = paste0("surv_diff_", tpt, table_names_suffix), |
306 | 3x |
show_labels = ifelse(method == "both", "hidden", show_labels), |
307 | 3x |
afun = afun_surv_diff, |
308 | 3x |
extra_args = list( |
309 | 3x |
is_event = list(...)$is_event, |
310 | 3x |
control = list(...)$control, |
311 | 3x |
time_point = tpt |
312 |
)
|
|
313 |
)
|
|
314 |
}
|
|
315 |
}
|
|
316 | 5x |
lyt
|
317 |
}
|
1 |
#' Tabulate Binary Response by Subgroup
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Tabulate statistics such as response rate and odds ratio for population subgroups.
|
|
6 |
#'
|
|
7 |
#' @inheritParams argument_convention
|
|
8 |
#' @param data (`data.frame`)\cr the dataset containing the variables to summarize.
|
|
9 |
#' @param groups_lists (named `list` of `list`)\cr optionally contains for each `subgroups` variable a
|
|
10 |
#' list, which specifies the new group levels via the names and the
|
|
11 |
#' levels that belong to it in the character vectors that are elements of the list.
|
|
12 |
#' @param label_all (`string`)\cr label for the total population analysis.
|
|
13 |
#' @param method (`string`)\cr specifies the test used to calculate the p-value for the difference between
|
|
14 |
#' two proportions. For options, see [s_test_proportion_diff()]. Default is `NULL` so no test is performed.
|
|
15 |
#'
|
|
16 |
#' @details These functions create a layout starting from a data frame which contains
|
|
17 |
#' the required statistics. Tables typically used as part of forest plot.
|
|
18 |
#'
|
|
19 |
#' @seealso [extract_rsp_subgroups()]
|
|
20 |
#'
|
|
21 |
#' @examples
|
|
22 |
#' library(dplyr)
|
|
23 |
#' library(forcats)
|
|
24 |
#'
|
|
25 |
#' adrs <- tern_ex_adrs
|
|
26 |
#' adrs_labels <- formatters::var_labels(adrs)
|
|
27 |
#'
|
|
28 |
#' adrs_f <- adrs %>%
|
|
29 |
#' filter(PARAMCD == "BESRSPI") %>%
|
|
30 |
#' filter(ARM %in% c("A: Drug X", "B: Placebo")) %>%
|
|
31 |
#' droplevels() %>%
|
|
32 |
#' mutate(
|
|
33 |
#' # Reorder levels of factor to make the placebo group the reference arm.
|
|
34 |
#' ARM = fct_relevel(ARM, "B: Placebo"),
|
|
35 |
#' rsp = AVALC == "CR"
|
|
36 |
#' )
|
|
37 |
#' formatters::var_labels(adrs_f) <- c(adrs_labels, "Response")
|
|
38 |
#'
|
|
39 |
#' # Unstratified analysis.
|
|
40 |
#' df <- extract_rsp_subgroups(
|
|
41 |
#' variables = list(rsp = "rsp", arm = "ARM", subgroups = c("SEX", "BMRKR2")),
|
|
42 |
#' data = adrs_f
|
|
43 |
#' )
|
|
44 |
#' df
|
|
45 |
#'
|
|
46 |
#' @name response_subgroups
|
|
47 |
NULL
|
|
48 | ||
49 |
#' Prepares Response Data for Population Subgroups in Data Frames
|
|
50 |
#'
|
|
51 |
#' @description `r lifecycle::badge("stable")`
|
|
52 |
#'
|
|
53 |
#' Prepares response rates and odds ratios for population subgroups in data frames. Simple wrapper
|
|
54 |
#' for [h_odds_ratio_subgroups_df()] and [h_proportion_subgroups_df()]. Result is a list of two
|
|
55 |
#' `data.frames`: `prop` and `or`. `variables` corresponds to the names of variables found in `data`,
|
|
56 |
#' passed as a named `list` and requires elements `rsp`, `arm` and optionally `subgroups` and `strat`.
|
|
57 |
#' `groups_lists` optionally specifies groupings for `subgroups` variables.
|
|
58 |
#'
|
|
59 |
#' @inheritParams argument_convention
|
|
60 |
#' @inheritParams response_subgroups
|
|
61 |
#' @param label_all (`string`)\cr label for the total population analysis.
|
|
62 |
#'
|
|
63 |
#' @return A named list of two elements:
|
|
64 |
#' * `prop`: A `data.frame` containing columns `arm`, `n`, `n_rsp`, `prop`, `subgroup`, `var`,
|
|
65 |
#' `var_label`, and `row_type`.
|
|
66 |
#' * `or`: A `data.frame` containing columns `arm`, `n_tot`, `or`, `lcl`, `ucl`, `conf_level`,
|
|
67 |
#' `subgroup`, `var`, `var_label`, and `row_type`.
|
|
68 |
#'
|
|
69 |
#' @seealso [response_subgroups]
|
|
70 |
#'
|
|
71 |
#' @examples
|
|
72 |
#' library(dplyr)
|
|
73 |
#' library(forcats)
|
|
74 |
#'
|
|
75 |
#' adrs <- tern_ex_adrs
|
|
76 |
#' adrs_labels <- formatters::var_labels(adrs)
|
|
77 |
#'
|
|
78 |
#' adrs_f <- adrs %>%
|
|
79 |
#' filter(PARAMCD == "BESRSPI") %>%
|
|
80 |
#' filter(ARM %in% c("A: Drug X", "B: Placebo")) %>%
|
|
81 |
#' droplevels() %>%
|
|
82 |
#' mutate(
|
|
83 |
#' # Reorder levels of factor to make the placebo group the reference arm.
|
|
84 |
#' ARM = fct_relevel(ARM, "B: Placebo"),
|
|
85 |
#' rsp = AVALC == "CR"
|
|
86 |
#' )
|
|
87 |
#' formatters::var_labels(adrs_f) <- c(adrs_labels, "Response")
|
|
88 |
#'
|
|
89 |
#' # Unstratified analysis.
|
|
90 |
#' df <- extract_rsp_subgroups(
|
|
91 |
#' variables = list(rsp = "rsp", arm = "ARM", subgroups = c("SEX", "BMRKR2")),
|
|
92 |
#' data = adrs_f
|
|
93 |
#' )
|
|
94 |
#' df
|
|
95 |
#'
|
|
96 |
#' # Stratified analysis.
|
|
97 |
#' df_strat <- extract_rsp_subgroups(
|
|
98 |
#' variables = list(rsp = "rsp", arm = "ARM", subgroups = c("SEX", "BMRKR2"), strat = "STRATA1"),
|
|
99 |
#' data = adrs_f
|
|
100 |
#' )
|
|
101 |
#' df_strat
|
|
102 |
#'
|
|
103 |
#' # Grouping of the BMRKR2 levels.
|
|
104 |
#' df_grouped <- extract_rsp_subgroups(
|
|
105 |
#' variables = list(rsp = "rsp", arm = "ARM", subgroups = c("SEX", "BMRKR2")),
|
|
106 |
#' data = adrs_f,
|
|
107 |
#' groups_lists = list(
|
|
108 |
#' BMRKR2 = list(
|
|
109 |
#' "low" = "LOW",
|
|
110 |
#' "low/medium" = c("LOW", "MEDIUM"),
|
|
111 |
#' "low/medium/high" = c("LOW", "MEDIUM", "HIGH")
|
|
112 |
#' )
|
|
113 |
#' )
|
|
114 |
#' )
|
|
115 |
#' df_grouped
|
|
116 |
#'
|
|
117 |
#' @export
|
|
118 |
extract_rsp_subgroups <- function(variables, |
|
119 |
data,
|
|
120 |
groups_lists = list(), |
|
121 |
conf_level = 0.95, |
|
122 |
method = NULL, |
|
123 |
label_all = "All Patients") { |
|
124 | 10x |
df_prop <- h_proportion_subgroups_df( |
125 | 10x |
variables,
|
126 | 10x |
data,
|
127 | 10x |
groups_lists = groups_lists, |
128 | 10x |
label_all = label_all |
129 |
)
|
|
130 | 10x |
df_or <- h_odds_ratio_subgroups_df( |
131 | 10x |
variables,
|
132 | 10x |
data,
|
133 | 10x |
groups_lists = groups_lists, |
134 | 10x |
conf_level = conf_level, |
135 | 10x |
method = method, |
136 | 10x |
label_all = label_all |
137 |
)
|
|
138 | ||
139 | 10x |
list(prop = df_prop, or = df_or) |
140 |
}
|
|
141 | ||
142 |
#' @describeIn response_subgroups Formatted analysis function which is used as `afun` in `tabulate_rsp_subgroups()`.
|
|
143 |
#'
|
|
144 |
#' @return
|
|
145 |
#' * `a_response_subgroups()` returns the corresponding list with formatted [rtables::CellValue()].
|
|
146 |
#'
|
|
147 |
#' @keywords internal
|
|
148 |
a_response_subgroups <- function(.formats = list( |
|
149 |
n = "xx", |
|
150 |
n_rsp = "xx", |
|
151 |
prop = "xx.x%", |
|
152 |
n_tot = "xx", |
|
153 |
or = list(format_extreme_values(2L)), |
|
154 |
ci = list(format_extreme_values_ci(2L)), |
|
155 |
pval = "x.xxxx | (<0.0001)" |
|
156 |
)) { |
|
157 | 13x |
checkmate::assert_list(.formats) |
158 | 13x |
checkmate::assert_subset( |
159 | 13x |
names(.formats), |
160 | 13x |
c("n", "n_rsp", "prop", "n_tot", "or", "ci", "pval") |
161 |
)
|
|
162 | ||
163 | 13x |
afun_lst <- Map( |
164 | 13x |
function(stat, fmt) { |
165 | 86x |
if (stat == "ci") { |
166 | 12x |
function(df, labelstr = "", ...) { |
167 | 24x |
in_rows(.list = combine_vectors(df$lcl, df$ucl), .labels = as.character(df$subgroup), .formats = fmt) |
168 |
}
|
|
169 |
} else { |
|
170 | 74x |
function(df, labelstr = "", ...) { |
171 | 142x |
in_rows(.list = as.list(df[[stat]]), .labels = as.character(df$subgroup), .formats = fmt) |
172 |
}
|
|
173 |
}
|
|
174 |
},
|
|
175 | 13x |
stat = names(.formats), |
176 | 13x |
fmt = .formats |
177 |
)
|
|
178 | ||
179 | 13x |
afun_lst
|
180 |
}
|
|
181 | ||
182 |
#' @describeIn response_subgroups Table-creating function which creates a table
|
|
183 |
#' summarizing binary response by subgroup. This function is a wrapper for [rtables::analyze_colvars()]
|
|
184 |
#' and [rtables::summarize_row_groups()].
|
|
185 |
#'
|
|
186 |
#' @param df (`list`)\cr of data frames containing all analysis variables. List should be
|
|
187 |
#' created using [extract_rsp_subgroups()].
|
|
188 |
#' @param vars (`character`)\cr the names of statistics to be reported among:
|
|
189 |
#' * `n`: Total number of observations per group.
|
|
190 |
#' * `n_rsp`: Number of responders per group.
|
|
191 |
#' * `prop`: Proportion of responders.
|
|
192 |
#' * `n_tot`: Total number of observations.
|
|
193 |
#' * `or`: Odds ratio.
|
|
194 |
#' * `ci` : Confidence interval of odds ratio.
|
|
195 |
#' * `pval`: p-value of the effect.
|
|
196 |
#' Note, the statistics `n_tot`, `or` and `ci` are required.
|
|
197 |
#'
|
|
198 |
#' @return An `rtables` table summarizing binary response by subgroup.
|
|
199 |
#'
|
|
200 |
#' @examples
|
|
201 |
#' ## Table with default columns.
|
|
202 |
#' basic_table() %>%
|
|
203 |
#' tabulate_rsp_subgroups(df)
|
|
204 |
#'
|
|
205 |
#' ## Table with selected columns.
|
|
206 |
#' basic_table() %>%
|
|
207 |
#' tabulate_rsp_subgroups(
|
|
208 |
#' df = df,
|
|
209 |
#' vars = c("n_tot", "n", "n_rsp", "prop", "or", "ci")
|
|
210 |
#' )
|
|
211 |
#'
|
|
212 |
#' @export
|
|
213 |
tabulate_rsp_subgroups <- function(lyt, |
|
214 |
df,
|
|
215 |
vars = c("n_tot", "n", "prop", "or", "ci")) { |
|
216 | 6x |
conf_level <- df$or$conf_level[1] |
217 | 6x |
method <- if ("pval_label" %in% names(df$or)) { |
218 | 4x |
df$or$pval_label[1] |
219 |
} else { |
|
220 | 2x |
NULL
|
221 |
}
|
|
222 | ||
223 | 6x |
afun_lst <- a_response_subgroups() |
224 | 6x |
colvars <- d_rsp_subgroups_colvars(vars, conf_level = conf_level, method = method) |
225 | ||
226 | 6x |
colvars_prop <- list( |
227 | 6x |
vars = colvars$vars[names(colvars$labels) %in% c("n", "prop", "n_rsp")], |
228 | 6x |
labels = colvars$labels[names(colvars$labels) %in% c("n", "prop", "n_rsp")] |
229 |
)
|
|
230 | 6x |
colvars_or <- list( |
231 | 6x |
vars = colvars$vars[names(colvars$labels) %in% c("n_tot", "or", "ci", "pval")], |
232 | 6x |
labels = colvars$labels[names(colvars$labels) %in% c("n_tot", "or", "ci", "pval")] |
233 |
)
|
|
234 | ||
235 |
# Columns from table_prop are optional.
|
|
236 | 6x |
if (length(colvars_prop$vars) > 0) { |
237 | 6x |
lyt_prop <- split_cols_by(lyt = lyt, var = "arm") |
238 | 6x |
lyt_prop <- split_rows_by( |
239 | 6x |
lyt = lyt_prop, |
240 | 6x |
var = "row_type", |
241 | 6x |
split_fun = keep_split_levels("content"), |
242 | 6x |
nested = FALSE |
243 |
)
|
|
244 | 6x |
lyt_prop <- summarize_row_groups( |
245 | 6x |
lyt = lyt_prop, |
246 | 6x |
var = "var_label", |
247 | 6x |
cfun = afun_lst[names(colvars_prop$labels)] |
248 |
)
|
|
249 | 6x |
lyt_prop <- split_cols_by_multivar( |
250 | 6x |
lyt = lyt_prop, |
251 | 6x |
vars = colvars_prop$vars, |
252 | 6x |
varlabels = colvars_prop$labels |
253 |
)
|
|
254 | ||
255 | 6x |
if ("analysis" %in% df$prop$row_type) { |
256 | 5x |
lyt_prop <- split_rows_by( |
257 | 5x |
lyt = lyt_prop, |
258 | 5x |
var = "row_type", |
259 | 5x |
split_fun = keep_split_levels("analysis"), |
260 | 5x |
nested = FALSE, |
261 | 5x |
child_labels = "hidden" |
262 |
)
|
|
263 | 5x |
lyt_prop <- split_rows_by(lyt = lyt_prop, var = "var_label", nested = TRUE) |
264 | 5x |
lyt_prop <- analyze_colvars( |
265 | 5x |
lyt = lyt_prop, |
266 | 5x |
afun = afun_lst[names(colvars_prop$labels)], |
267 | 5x |
inclNAs = TRUE |
268 |
)
|
|
269 |
}
|
|
270 | ||
271 | 6x |
table_prop <- build_table(lyt_prop, df = df$prop) |
272 |
} else { |
|
273 | ! |
table_prop <- NULL |
274 |
}
|
|
275 | ||
276 |
# Columns "n_tot", "or", "ci" in table_or are required.
|
|
277 | 6x |
lyt_or <- split_cols_by(lyt = lyt, var = "arm") |
278 | 6x |
lyt_or <- split_rows_by( |
279 | 6x |
lyt = lyt_or, |
280 | 6x |
var = "row_type", |
281 | 6x |
split_fun = keep_split_levels("content"), |
282 | 6x |
nested = FALSE |
283 |
)
|
|
284 | 6x |
lyt_or <- split_cols_by_multivar( |
285 | 6x |
lyt = lyt_or, |
286 | 6x |
vars = colvars_or$vars, |
287 | 6x |
varlabels = colvars_or$labels |
288 |
)
|
|
289 | 6x |
lyt_or <- summarize_row_groups( |
290 | 6x |
lyt = lyt_or, |
291 | 6x |
var = "var_label", |
292 | 6x |
cfun = afun_lst[names(colvars_or$labels)] |
293 |
) %>% |
|
294 | 6x |
append_topleft("Baseline Risk Factors") |
295 | ||
296 | 6x |
if ("analysis" %in% df$or$row_type) { |
297 | 5x |
lyt_or <- split_rows_by( |
298 | 5x |
lyt = lyt_or, |
299 | 5x |
var = "row_type", |
300 | 5x |
split_fun = keep_split_levels("analysis"), |
301 | 5x |
nested = FALSE, |
302 | 5x |
child_labels = "hidden" |
303 |
)
|
|
304 | 5x |
lyt_or <- split_rows_by(lyt = lyt_or, var = "var_label", nested = TRUE) |
305 | 5x |
lyt_or <- analyze_colvars( |
306 | 5x |
lyt = lyt_or, |
307 | 5x |
afun = afun_lst[names(colvars_or$labels)], |
308 | 5x |
inclNAs = TRUE |
309 |
)
|
|
310 |
}
|
|
311 | 6x |
table_or <- build_table(lyt_or, df = df$or) |
312 | ||
313 | 6x |
n_tot_id <- match("n_tot", colvars_or$vars) |
314 | 6x |
if (is.null(table_prop)) { |
315 | ! |
result <- table_or |
316 | ! |
or_id <- match("or", colvars_or$vars) |
317 | ! |
ci_id <- match("lcl", colvars_or$vars) |
318 |
} else { |
|
319 | 6x |
result <- cbind_rtables(table_or[, n_tot_id], table_prop, table_or[, -n_tot_id]) |
320 | 6x |
or_id <- 1L + ncol(table_prop) + match("or", colvars_or$vars[-n_tot_id]) |
321 | 6x |
ci_id <- 1L + ncol(table_prop) + match("lcl", colvars_or$vars[-n_tot_id]) |
322 | 6x |
n_tot_id <- 1L |
323 |
}
|
|
324 | 6x |
structure( |
325 | 6x |
result,
|
326 | 6x |
forest_header = paste0(levels(df$prop$arm), "\nBetter"), |
327 | 6x |
col_x = or_id, |
328 | 6x |
col_ci = ci_id, |
329 | 6x |
col_symbol_size = n_tot_id |
330 |
)
|
|
331 |
}
|
|
332 | ||
333 |
#' Labels for Column Variables in Binary Response by Subgroup Table
|
|
334 |
#'
|
|
335 |
#' @description `r lifecycle::badge("stable")`
|
|
336 |
#'
|
|
337 |
#' Internal function to check variables included in [tabulate_rsp_subgroups()] and create column labels.
|
|
338 |
#'
|
|
339 |
#' @inheritParams argument_convention
|
|
340 |
#' @inheritParams tabulate_rsp_subgroups
|
|
341 |
#'
|
|
342 |
#' @return A `list` of variables to tabulate and their labels.
|
|
343 |
#'
|
|
344 |
#' @export
|
|
345 |
d_rsp_subgroups_colvars <- function(vars, |
|
346 |
conf_level = NULL, |
|
347 |
method = NULL) { |
|
348 | 13x |
checkmate::assert_character(vars) |
349 | 13x |
checkmate::assert_subset(c("n_tot", "or", "ci"), vars) |
350 | 13x |
checkmate::assert_subset( |
351 | 13x |
vars,
|
352 | 13x |
c("n", "n_rsp", "prop", "n_tot", "or", "ci", "pval") |
353 |
)
|
|
354 | ||
355 | 13x |
varlabels <- c( |
356 | 13x |
n = "n", |
357 | 13x |
n_rsp = "Responders", |
358 | 13x |
prop = "Response (%)", |
359 | 13x |
n_tot = "Total n", |
360 | 13x |
or = "Odds Ratio" |
361 |
)
|
|
362 | 13x |
colvars <- vars |
363 | ||
364 | 13x |
if ("ci" %in% colvars) { |
365 | 13x |
checkmate::assert_false(is.null(conf_level)) |
366 | ||
367 | 13x |
varlabels <- c( |
368 | 13x |
varlabels,
|
369 | 13x |
ci = paste0(100 * conf_level, "% CI") |
370 |
)
|
|
371 | ||
372 |
# The `lcl`` variable is just a placeholder available in the analysis data,
|
|
373 |
# it is not acutally used in the tabulation.
|
|
374 |
# Variables used in the tabulation are lcl and ucl, see `a_response_subgroups` for details.
|
|
375 | 13x |
colvars[colvars == "ci"] <- "lcl" |
376 |
}
|
|
377 | ||
378 | 13x |
if ("pval" %in% colvars) { |
379 | 10x |
varlabels <- c( |
380 | 10x |
varlabels,
|
381 | 10x |
pval = method |
382 |
)
|
|
383 |
}
|
|
384 | ||
385 | 13x |
list( |
386 | 13x |
vars = colvars, |
387 | 13x |
labels = varlabels[vars] |
388 |
)
|
|
389 |
}
|
1 |
#' Combine Factor Levels
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Combine specified old factor Levels in a single new level.
|
|
6 |
#'
|
|
7 |
#' @param x factor
|
|
8 |
#' @param levels level names to be combined
|
|
9 |
#' @param new_level name of new level
|
|
10 |
#'
|
|
11 |
#' @return A `factor` with the new levels.
|
|
12 |
#'
|
|
13 |
#' @examples
|
|
14 |
#' x <- factor(letters[1:5], levels = letters[5:1])
|
|
15 |
#' combine_levels(x, levels = c("a", "b"))
|
|
16 |
#'
|
|
17 |
#' combine_levels(x, c("e", "b"))
|
|
18 |
#'
|
|
19 |
#' @export
|
|
20 |
combine_levels <- function(x, levels, new_level = paste(levels, collapse = "/")) { |
|
21 | 4x |
checkmate::assert_factor(x) |
22 | 4x |
checkmate::assert_subset(levels, levels(x)) |
23 | ||
24 | 4x |
lvls <- levels(x) |
25 | ||
26 | 4x |
lvls[lvls %in% levels] <- new_level |
27 | ||
28 | 4x |
levels(x) <- lvls |
29 | ||
30 | 4x |
x
|
31 |
}
|
|
32 | ||
33 |
#' Conversion of a Vector to a Factor
|
|
34 |
#'
|
|
35 |
#' Converts `x` to a factor and keeps its attributes. Warns appropriately such that the user
|
|
36 |
#' can decide whether they prefer converting to factor manually (e.g. for full control of
|
|
37 |
#' factor levels).
|
|
38 |
#'
|
|
39 |
#' @param x (`atomic`)\cr object to convert.
|
|
40 |
#' @param x_name (`string`)\cr name of `x`.
|
|
41 |
#' @param na_level (`string`)\cr the explicit missing level which should be used when converting a character vector.
|
|
42 |
#' @param verbose defaults to `TRUE`. It prints out warnings and messages.
|
|
43 |
#'
|
|
44 |
#' @return A `factor` with same attributes (except class) as `x`. Does not modify `x` if already a `factor`.
|
|
45 |
#'
|
|
46 |
#' @keywords internal
|
|
47 |
as_factor_keep_attributes <- function(x, |
|
48 |
x_name = deparse(substitute(x)), |
|
49 |
na_level = "<Missing>", |
|
50 |
verbose = TRUE) { |
|
51 | 159x |
checkmate::assert_atomic(x) |
52 | 159x |
checkmate::assert_string(x_name) |
53 | 159x |
checkmate::assert_string(na_level) |
54 | 159x |
checkmate::assert_flag(verbose) |
55 | 159x |
if (is.factor(x)) { |
56 | 144x |
return(x) |
57 |
}
|
|
58 | 15x |
x_class <- class(x)[1] |
59 | 15x |
if (verbose) { |
60 | 15x |
warning(paste( |
61 | 15x |
"automatically converting", x_class, "variable", x_name, |
62 | 15x |
"to factor, better manually convert to factor to avoid failures"
|
63 |
)) |
|
64 |
}
|
|
65 | 15x |
if (identical(length(x), 0L)) { |
66 | 1x |
warning(paste( |
67 | 1x |
x_name, "has length 0, this can lead to tabulation failures, better convert to factor" |
68 |
)) |
|
69 |
}
|
|
70 | 15x |
if (is.character(x)) { |
71 | 15x |
x_no_na <- explicit_na(sas_na(x), label = na_level) |
72 | 15x |
if (any(na_level %in% x_no_na)) { |
73 | 3x |
do.call( |
74 | 3x |
structure,
|
75 | 3x |
c( |
76 | 3x |
list(.Data = forcats::fct_relevel(x_no_na, na_level, after = Inf)), |
77 | 3x |
attributes(x) |
78 |
)
|
|
79 |
)
|
|
80 |
} else { |
|
81 | 12x |
do.call(structure, c(list(.Data = as.factor(x)), attributes(x))) |
82 |
}
|
|
83 |
} else { |
|
84 | ! |
do.call(structure, c(list(.Data = as.factor(x)), attributes(x))) |
85 |
}
|
|
86 |
}
|
|
87 | ||
88 |
#' Labels for Bins in Percent
|
|
89 |
#'
|
|
90 |
#' This creates labels for quantile based bins in percent. This assumes the right-closed
|
|
91 |
#' intervals as produced by [cut_quantile_bins()].
|
|
92 |
#'
|
|
93 |
#' @param probs (`proportion` vector)\cr the probabilities identifying the quantiles.
|
|
94 |
#' This is a sorted vector of unique `proportion` values, i.e. between 0 and 1, where
|
|
95 |
#' the boundaries 0 and 1 must not be included.
|
|
96 |
#' @param digits (`integer`)\cr number of decimal places to round the percent numbers.
|
|
97 |
#'
|
|
98 |
#' @return A `character` vector with labels in the format `[0%,20%]`, `(20%,50%]`, etc.
|
|
99 |
#'
|
|
100 |
#' @keywords internal
|
|
101 |
bins_percent_labels <- function(probs, |
|
102 |
digits = 0) { |
|
103 | 1x |
if (isFALSE(0 %in% probs)) probs <- c(0, probs) |
104 | 1x |
if (isFALSE(1 %in% probs)) probs <- c(probs, 1) |
105 | 8x |
checkmate::assert_numeric(probs, lower = 0, upper = 1, unique = TRUE, sorted = TRUE) |
106 | 8x |
percent <- round(probs * 100, digits = digits) |
107 | 8x |
left <- paste0(utils::head(percent, -1), "%") |
108 | 8x |
right <- paste0(utils::tail(percent, -1), "%") |
109 | 8x |
without_left_bracket <- paste0(left, ",", right, "]") |
110 | 8x |
with_left_bracket <- paste0("[", utils::head(without_left_bracket, 1)) |
111 | 8x |
if (length(without_left_bracket) > 1) { |
112 | 6x |
with_left_bracket <- c( |
113 | 6x |
with_left_bracket,
|
114 | 6x |
paste0("(", utils::tail(without_left_bracket, -1)) |
115 |
)
|
|
116 |
}
|
|
117 | 8x |
with_left_bracket
|
118 |
}
|
|
119 | ||
120 |
#' Cutting Numeric Vector into Empirical Quantile Bins
|
|
121 |
#'
|
|
122 |
#' @description `r lifecycle::badge("stable")`
|
|
123 |
#'
|
|
124 |
#' This cuts a numeric vector into sample quantile bins.
|
|
125 |
#'
|
|
126 |
#' @inheritParams bins_percent_labels
|
|
127 |
#' @param x (`numeric`)\cr the continuous variable values which should be cut into
|
|
128 |
#' quantile bins. This may contain `NA` values, which are then
|
|
129 |
#' not used for the quantile calculations, but included in the return vector.
|
|
130 |
#' @param labels (`character`)\cr the unique labels for the quantile bins. When there are `n`
|
|
131 |
#' probabilities in `probs`, then this must be `n + 1` long.
|
|
132 |
#' @param type (`integer`)\cr type of quantiles to use, see [stats::quantile()] for details.
|
|
133 |
#' @param ordered (`flag`)\cr should the result be an ordered factor.
|
|
134 |
#'
|
|
135 |
#' @return A `factor` variable with appropriately-labeled bins as levels.
|
|
136 |
#'
|
|
137 |
#' @note Intervals are closed on the right side. That is, the first bin is the interval
|
|
138 |
#' `[-Inf, q1]` where `q1` is the first quantile, the second bin is then `(q1, q2]`, etc.,
|
|
139 |
#' and the last bin is `(qn, +Inf]` where `qn` is the last quantile.
|
|
140 |
#'
|
|
141 |
#' @examples
|
|
142 |
#' # Default is to cut into quartile bins.
|
|
143 |
#' cut_quantile_bins(cars$speed)
|
|
144 |
#'
|
|
145 |
#' # Use custom quantiles.
|
|
146 |
#' cut_quantile_bins(cars$speed, probs = c(0.1, 0.2, 0.6, 0.88))
|
|
147 |
#'
|
|
148 |
#' # Use custom labels.
|
|
149 |
#' cut_quantile_bins(cars$speed, labels = paste0("Q", 1:4))
|
|
150 |
#'
|
|
151 |
#' # NAs are preserved in result factor.
|
|
152 |
#' ozone_binned <- cut_quantile_bins(airquality$Ozone)
|
|
153 |
#' which(is.na(ozone_binned))
|
|
154 |
#' # So you might want to make these explicit.
|
|
155 |
#' explicit_na(ozone_binned)
|
|
156 |
#'
|
|
157 |
#' @export
|
|
158 |
cut_quantile_bins <- function(x, |
|
159 |
probs = c(0.25, 0.5, 0.75), |
|
160 |
labels = NULL, |
|
161 |
type = 7, |
|
162 |
ordered = TRUE) { |
|
163 | 8x |
checkmate::assert_flag(ordered) |
164 | 8x |
checkmate::assert_numeric(x) |
165 | 7x |
if (isFALSE(0 %in% probs)) probs <- c(0, probs) |
166 | 7x |
if (isFALSE(1 %in% probs)) probs <- c(probs, 1) |
167 | 8x |
checkmate::assert_numeric(probs, lower = 0, upper = 1, unique = TRUE, sorted = TRUE) |
168 | 7x |
if (is.null(labels)) labels <- bins_percent_labels(probs) |
169 | 8x |
checkmate::assert_character(labels, len = length(probs) - 1, any.missing = FALSE, unique = TRUE) |
170 | ||
171 | 8x |
if (all(is.na(x))) { |
172 |
# Early return if there are only NAs in input.
|
|
173 | 1x |
return(factor(x, ordered = ordered, levels = labels)) |
174 |
}
|
|
175 | ||
176 | 7x |
quantiles <- stats::quantile( |
177 | 7x |
x,
|
178 | 7x |
probs = probs, |
179 | 7x |
type = type, |
180 | 7x |
na.rm = TRUE |
181 |
)
|
|
182 | ||
183 | 7x |
checkmate::assert_numeric(quantiles, unique = TRUE) |
184 | ||
185 | 6x |
cut( |
186 | 6x |
x,
|
187 | 6x |
breaks = quantiles, |
188 | 6x |
labels = labels, |
189 | 6x |
ordered_result = ordered, |
190 | 6x |
include.lowest = TRUE, |
191 | 6x |
right = TRUE |
192 |
)
|
|
193 |
}
|
|
194 | ||
195 |
#' Discard Certain Levels from a Factor
|
|
196 |
#'
|
|
197 |
#' @description `r lifecycle::badge("stable")`
|
|
198 |
#'
|
|
199 |
#' This discards the observations as well as the levels specified from a factor.
|
|
200 |
#'
|
|
201 |
#' @param x (`factor`)\cr the original factor.
|
|
202 |
#' @param discard (`character`)\cr which levels to discard.
|
|
203 |
#'
|
|
204 |
#' @return A modified `factor` with observations as well as levels from `discard` dropped.
|
|
205 |
#'
|
|
206 |
#' @examples
|
|
207 |
#' fct_discard(factor(c("a", "b", "c")), "c")
|
|
208 |
#'
|
|
209 |
#' @export
|
|
210 |
fct_discard <- function(x, discard) { |
|
211 | 256x |
checkmate::assert_factor(x) |
212 | 256x |
checkmate::assert_character(discard, any.missing = FALSE) |
213 | 256x |
new_obs <- x[!(x %in% discard)] |
214 | 256x |
new_levels <- setdiff(levels(x), discard) |
215 | 256x |
factor(new_obs, levels = new_levels) |
216 |
}
|
|
217 | ||
218 |
#' Insertion of Explicit Missings in a Factor
|
|
219 |
#'
|
|
220 |
#' @description `r lifecycle::badge("stable")`
|
|
221 |
#'
|
|
222 |
#' This inserts explicit missings in a factor based on a condition. Additionally,
|
|
223 |
#' existing `NA` values will be explicitly converted to given `na_level`.
|
|
224 |
#'
|
|
225 |
#' @param x (`factor`)\cr the original factor.
|
|
226 |
#' @param condition (`logical`)\cr where to insert missings.
|
|
227 |
#' @param na_level (`string`)\cr which level to use for missings.
|
|
228 |
#'
|
|
229 |
#' @return A modified `factor` with inserted and existing `NA` converted to `na_level`.
|
|
230 |
#'
|
|
231 |
#' @seealso [forcats::fct_na_value_to_level()] which is used internally.
|
|
232 |
#'
|
|
233 |
#' @examples
|
|
234 |
#' fct_explicit_na_if(factor(c("a", "b", NA)), c(TRUE, FALSE, FALSE))
|
|
235 |
#'
|
|
236 |
#' @export
|
|
237 |
fct_explicit_na_if <- function(x, condition, na_level = "<Missing>") { |
|
238 | 1x |
checkmate::assert_factor(x, len = length(condition)) |
239 | 1x |
checkmate::assert_logical(condition) |
240 | 1x |
x[condition] <- NA |
241 | 1x |
x <- forcats::fct_na_value_to_level(x, level = na_level) |
242 | 1x |
forcats::fct_drop(x, only = na_level) |
243 |
}
|
|
244 | ||
245 |
#' Collapsing of Factor Levels and Keeping Only Those New Group Levels
|
|
246 |
#'
|
|
247 |
#' @description `r lifecycle::badge("stable")`
|
|
248 |
#'
|
|
249 |
#' This collapses levels and only keeps those new group levels, in the order provided.
|
|
250 |
#' The returned factor has levels in the order given, with the possible missing level last (this will
|
|
251 |
#' only be included if there are missing values).
|
|
252 |
#'
|
|
253 |
#' @param .f (`factor` or `character`)\cr original vector.
|
|
254 |
#' @param ... (named `character` vectors)\cr levels in each vector provided will be collapsed into
|
|
255 |
#' the new level given by the respective name.
|
|
256 |
#' @param .na_level (`string`)\cr which level to use for other levels, which should be missing in the
|
|
257 |
#' new factor. Note that this level must not be contained in the new levels specified in `...`.
|
|
258 |
#'
|
|
259 |
#' @return A modified `factor` with collapsed levels. Values and levels which are not included
|
|
260 |
#' in the given `character` vector input will be set to the missing level `.na_level`.
|
|
261 |
#'
|
|
262 |
#' @note Any existing `NA`s in the input vector will not be replaced by the missing level. If needed,
|
|
263 |
#' [explicit_na()] can be called separately on the result.
|
|
264 |
#'
|
|
265 |
#' @seealso [forcats::fct_collapse()], [forcats::fct_relevel()] which are used internally.
|
|
266 |
#'
|
|
267 |
#' @examples
|
|
268 |
#' fct_collapse_only(factor(c("a", "b", "c", "d")), TRT = "b", CTRL = c("c", "d"))
|
|
269 |
#'
|
|
270 |
#' @export
|
|
271 |
fct_collapse_only <- function(.f, ..., .na_level = "<Missing>") { |
|
272 | 4x |
new_lvls <- names(list(...)) |
273 | 4x |
if (checkmate::test_subset(.na_level, new_lvls)) { |
274 | 1x |
stop(paste0(".na_level currently set to '", .na_level, "' must not be contained in the new levels")) |
275 |
}
|
|
276 | 3x |
x <- forcats::fct_collapse(.f, ..., other_level = .na_level) |
277 | 3x |
do.call(forcats::fct_relevel, args = c(list(.f = x), as.list(new_lvls))) |
278 |
}
|
1 |
#' Control Function for Subgroup Treatment Effect Pattern (STEP) Calculations
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' This is an auxiliary function for controlling arguments for STEP calculations.
|
|
6 |
#'
|
|
7 |
#' @param biomarker (`numeric` or `NULL`)\cr optional provision of the numeric biomarker variable, which
|
|
8 |
#' could be used to infer `bandwidth`, see below.
|
|
9 |
#' @param use_percentile (`flag`)\cr if `TRUE`, the running windows are created according to
|
|
10 |
#' quantiles rather than actual values, i.e. the bandwidth refers to the percentage of data
|
|
11 |
#' covered in each window. Suggest `TRUE` if the biomarker variable is not uniformly
|
|
12 |
#' distributed.
|
|
13 |
#' @param bandwidth (`number` or `NULL`)\cr indicating the bandwidth of each window.
|
|
14 |
#' Depending on the argument `use_percentile`, it can be either the length of actual-value
|
|
15 |
#' windows on the real biomarker scale, or percentage windows.
|
|
16 |
#' If `use_percentile = TRUE`, it should be a number between 0 and 1.
|
|
17 |
#' If `NULL`, treat the bandwidth to be infinity, which means only one global model will be fitted.
|
|
18 |
#' By default, `0.25` is used for percentage windows and one quarter of the range of the `biomarker`
|
|
19 |
#' variable for actual-value windows.
|
|
20 |
#' @param degree (`count`)\cr the degree of polynomial function of the biomarker as an interaction term
|
|
21 |
#' with the treatment arm fitted at each window. If 0 (default), then the biomarker variable
|
|
22 |
#' is not included in the model fitted in each biomarker window.
|
|
23 |
#' @param num_points (`count`)\cr the number of points at which the hazard ratios are estimated. The
|
|
24 |
#' smallest number is 2.
|
|
25 |
#'
|
|
26 |
#' @return A list of components with the same names as the arguments, except `biomarker` which is
|
|
27 |
#' just used to calculate the `bandwidth` in case that actual biomarker windows are requested.
|
|
28 |
#'
|
|
29 |
#' @examples
|
|
30 |
#' # Provide biomarker values and request actual values to be used,
|
|
31 |
#' # so that bandwidth is chosen from range.
|
|
32 |
#' control_step(biomarker = 1:10, use_percentile = FALSE)
|
|
33 |
#'
|
|
34 |
#' # Use a global model with quadratic biomarker interaction term.
|
|
35 |
#' control_step(bandwidth = NULL, degree = 2)
|
|
36 |
#'
|
|
37 |
#' # Reduce number of points to be used.
|
|
38 |
#' control_step(num_points = 10)
|
|
39 |
#'
|
|
40 |
#' @export
|
|
41 |
control_step <- function(biomarker = NULL, |
|
42 |
use_percentile = TRUE, |
|
43 |
bandwidth,
|
|
44 |
degree = 0L, |
|
45 |
num_points = 39L) { |
|
46 | 31x |
checkmate::assert_numeric(biomarker, null.ok = TRUE) |
47 | 30x |
checkmate::assert_flag(use_percentile) |
48 | 30x |
checkmate::assert_int(num_points, lower = 2) |
49 | 29x |
checkmate::assert_count(degree) |
50 | ||
51 | 29x |
if (missing(bandwidth)) { |
52 |
# Infer bandwidth
|
|
53 | 21x |
bandwidth <- if (use_percentile) { |
54 | 18x |
0.25
|
55 | 21x |
} else if (!is.null(biomarker)) { |
56 | 3x |
diff(range(biomarker, na.rm = TRUE)) / 4 |
57 |
} else { |
|
58 | ! |
NULL
|
59 |
}
|
|
60 |
} else { |
|
61 |
# Check bandwidth
|
|
62 | 8x |
if (!is.null(bandwidth)) { |
63 | 5x |
if (use_percentile) { |
64 | 4x |
assert_proportion_value(bandwidth) |
65 |
} else { |
|
66 | 1x |
checkmate::assert_scalar(bandwidth) |
67 | 1x |
checkmate::assert_true(bandwidth > 0) |
68 |
}
|
|
69 |
}
|
|
70 |
}
|
|
71 | 28x |
list( |
72 | 28x |
use_percentile = use_percentile, |
73 | 28x |
bandwidth = bandwidth, |
74 | 28x |
degree = as.integer(degree), |
75 | 28x |
num_points = as.integer(num_points) |
76 |
)
|
|
77 |
}
|
1 |
#' `rtables` Access Helper Functions
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' These are a couple of functions that help with accessing the data in `rtables` objects.
|
|
6 |
#' Currently these work for occurrence tables, which are defined as having a count as the first
|
|
7 |
#' element and a fraction as the second element in each cell.
|
|
8 |
#'
|
|
9 |
#' @seealso [prune_occurrences] for usage of these functions.
|
|
10 |
#'
|
|
11 |
#' @name rtables_access
|
|
12 |
NULL
|
|
13 | ||
14 |
#' @describeIn rtables_access Helper function to extract the first values from each content
|
|
15 |
#' cell and from specified columns in a `TableRow`. Defaults to all columns.
|
|
16 |
#'
|
|
17 |
#' @param table_row (`TableRow`)\cr an analysis row in a occurrence table.
|
|
18 |
#' @param col_names (`character`)\cr the names of the columns to extract from.
|
|
19 |
#' @param col_indices (`integer`)\cr the indices of the columns to extract from. If `col_names` are provided,
|
|
20 |
#' then these are inferred from the names of `table_row`. Note that this currently only works well with a single
|
|
21 |
#' column split.
|
|
22 |
#'
|
|
23 |
#' @return
|
|
24 |
#' * `h_row_first_values()` returns a `vector` of numeric values.
|
|
25 |
#'
|
|
26 |
#' @examples
|
|
27 |
#' tbl <- basic_table() %>%
|
|
28 |
#' split_cols_by("ARM") %>%
|
|
29 |
#' split_rows_by("RACE") %>%
|
|
30 |
#' analyze("AGE", function(x) {
|
|
31 |
#' list(
|
|
32 |
#' "mean (sd)" = rcell(c(mean(x), sd(x)), format = "xx.x (xx.x)"),
|
|
33 |
#' "n" = length(x),
|
|
34 |
#' "frac" = rcell(c(0.1, 0.1), format = "xx (xx)")
|
|
35 |
#' )
|
|
36 |
#' }) %>%
|
|
37 |
#' build_table(tern_ex_adsl) %>%
|
|
38 |
#' prune_table()
|
|
39 |
#' tree_row_elem <- collect_leaves(tbl[2, ])[[1]]
|
|
40 |
#' result <- max(h_row_first_values(tree_row_elem))
|
|
41 |
#' result
|
|
42 |
#'
|
|
43 |
#' @export
|
|
44 |
h_row_first_values <- function(table_row, |
|
45 |
col_names = NULL, |
|
46 |
col_indices = NULL) { |
|
47 | 727x |
col_indices <- check_names_indices(table_row, col_names, col_indices) |
48 | 727x |
checkmate::assert_integerish(col_indices) |
49 | 727x |
checkmate::assert_subset(col_indices, seq_len(ncol(table_row))) |
50 | ||
51 |
# Main values are extracted
|
|
52 | 727x |
row_vals <- row_values(table_row)[col_indices] |
53 | ||
54 |
# Main return
|
|
55 | 727x |
vapply(row_vals, function(rv) { |
56 | 2066x |
if (is.null(rv)) { |
57 | 727x |
NA_real_
|
58 |
} else { |
|
59 | 2063x |
rv[1L] |
60 |
}
|
|
61 | 727x |
}, FUN.VALUE = numeric(1)) |
62 |
}
|
|
63 | ||
64 |
#' @describeIn rtables_access Helper function that extracts row values and checks if they are
|
|
65 |
#' convertible to integers (`integerish` values).
|
|
66 |
#'
|
|
67 |
#' @return
|
|
68 |
#' * `h_row_counts()` returns a `vector` of numeric values.
|
|
69 |
#'
|
|
70 |
#' @examples
|
|
71 |
#' # Row counts (integer values)
|
|
72 |
#' # h_row_counts(tree_row_elem) # Fails because there are no integers
|
|
73 |
#' # Using values with integers
|
|
74 |
#' tree_row_elem <- collect_leaves(tbl[3, ])[[1]]
|
|
75 |
#' result <- h_row_counts(tree_row_elem)
|
|
76 |
#' # result
|
|
77 |
#'
|
|
78 |
#' @export
|
|
79 |
h_row_counts <- function(table_row, |
|
80 |
col_names = NULL, |
|
81 |
col_indices = NULL) { |
|
82 | 727x |
counts <- h_row_first_values(table_row, col_names, col_indices) |
83 | 727x |
checkmate::assert_integerish(counts) |
84 | 727x |
counts
|
85 |
}
|
|
86 | ||
87 |
#' @describeIn rtables_access helper function to extract fractions from specified columns in a `TableRow`.
|
|
88 |
#' More specifically it extracts the second values from each content cell and checks it is a fraction.
|
|
89 |
#'
|
|
90 |
#' @return
|
|
91 |
#' * `h_row_fractions()` returns a `vector` of proportions.
|
|
92 |
#'
|
|
93 |
#' @examples
|
|
94 |
#' # Row fractions
|
|
95 |
#' tree_row_elem <- collect_leaves(tbl[4, ])[[1]]
|
|
96 |
#' h_row_fractions(tree_row_elem)
|
|
97 |
#'
|
|
98 |
#' @export
|
|
99 |
h_row_fractions <- function(table_row, |
|
100 |
col_names = NULL, |
|
101 |
col_indices = NULL) { |
|
102 | 243x |
col_indices <- check_names_indices(table_row, col_names, col_indices) |
103 | 243x |
row_vals <- row_values(table_row)[col_indices] |
104 | 243x |
fractions <- sapply(row_vals, "[", 2L) |
105 | 243x |
checkmate::assert_numeric(fractions, lower = 0, upper = 1) |
106 | 243x |
fractions
|
107 |
}
|
|
108 | ||
109 |
#' @describeIn rtables_access Helper function to extract column counts from specified columns in a table.
|
|
110 |
#'
|
|
111 |
#' @param table (`VTableNodeInfo`)\cr an occurrence table or row.
|
|
112 |
#'
|
|
113 |
#' @return
|
|
114 |
#' * `h_col_counts()` returns a `vector` of column counts.
|
|
115 |
#'
|
|
116 |
#' @export
|
|
117 |
h_col_counts <- function(table, |
|
118 |
col_names = NULL, |
|
119 |
col_indices = NULL) { |
|
120 | 304x |
col_indices <- check_names_indices(table, col_names, col_indices) |
121 | 304x |
counts <- col_counts(table)[col_indices] |
122 | 304x |
stats::setNames(counts, col_names) |
123 |
}
|
|
124 | ||
125 |
#' @describeIn rtables_access Helper function to get first row of content table of current table.
|
|
126 |
#'
|
|
127 |
#' @return
|
|
128 |
#' * `h_content_first_row()` returns a row from an `rtables` table.
|
|
129 |
#'
|
|
130 |
#' @export
|
|
131 |
h_content_first_row <- function(table) { |
|
132 | 27x |
ct <- content_table(table) |
133 | 27x |
tree_children(ct)[[1]] |
134 |
}
|
|
135 | ||
136 |
#' @describeIn rtables_access Helper function which says whether current table is a leaf in the tree.
|
|
137 |
#'
|
|
138 |
#' @return
|
|
139 |
#' * `is_leaf_table()` returns a `logical` value indicating whether current table is a leaf.
|
|
140 |
#'
|
|
141 |
#' @keywords internal
|
|
142 |
is_leaf_table <- function(table) { |
|
143 | 168x |
children <- tree_children(table) |
144 | 168x |
child_classes <- unique(sapply(children, class)) |
145 | 168x |
identical(child_classes, "ElementaryTable") |
146 |
}
|
|
147 | ||
148 |
#' @describeIn rtables_access Internal helper function that tests standard inputs for column indices.
|
|
149 |
#'
|
|
150 |
#' @return
|
|
151 |
#' * `check_names_indices` returns column indices.
|
|
152 |
#'
|
|
153 |
#' @keywords internal
|
|
154 |
check_names_indices <- function(table_row, |
|
155 |
col_names = NULL, |
|
156 |
col_indices = NULL) { |
|
157 | 1274x |
if (!is.null(col_names)) { |
158 | 1231x |
if (!is.null(col_indices)) { |
159 | ! |
stop( |
160 | ! |
"Inserted both col_names and col_indices when selecting row values. ",
|
161 | ! |
"Please choose one."
|
162 |
)
|
|
163 |
}
|
|
164 | 1231x |
col_indices <- h_col_indices(table_row, col_names) |
165 |
}
|
|
166 | 1274x |
if (is.null(col_indices)) { |
167 | 37x |
ll <- ifelse(is.null(ncol(table_row)), length(table_row), ncol(table_row)) |
168 | 37x |
col_indices <- seq_len(ll) |
169 |
}
|
|
170 | ||
171 | 1274x |
return(col_indices) |
172 |
}
|
1 |
#' Line plot with the optional table
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Line plot with the optional table.
|
|
6 |
#'
|
|
7 |
#' @param df (`data.frame`)\cr data set containing all analysis variables.
|
|
8 |
#' @param alt_counts_df (`data.frame` or `NULL`)\cr data set that will be used (only) to counts objects in strata.
|
|
9 |
#' @param variables (named `character` vector) of variable names in `df` data set. Details are:
|
|
10 |
#' * `x` (`character`)\cr name of x-axis variable.
|
|
11 |
#' * `y` (`character`)\cr name of y-axis variable.
|
|
12 |
#' * `strata` (`character`)\cr name of grouping variable, i.e. treatment arm. Can be `NA` to indicate lack of groups.
|
|
13 |
#' * `paramcd` (`character`)\cr name of the variable for parameter's code. Used for y-axis label and plot's subtitle.
|
|
14 |
#' Can be `NA` if `paramcd` is not to be added to the y-axis label or subtitle.
|
|
15 |
#' * `y_unit` (`character`)\cr name of variable with units of `y`. Used for y-axis label and plot's subtitle.
|
|
16 |
#' Can be `NA` if y unit is not to be added to the y-axis label or subtitle.
|
|
17 |
#' @param mid (`character` or `NULL`)\cr names of the statistics that will be plotted as midpoints.
|
|
18 |
#' All the statistics indicated in `mid` variable must be present in the object returned by `sfun`,
|
|
19 |
#' and be of a `double` or `numeric` type vector of length one.
|
|
20 |
#' @param interval (`character` or `NULL`)\cr names of the statistics that will be plotted as intervals.
|
|
21 |
#' All the statistics indicated in `interval` variable must be present in the object returned by `sfun`,
|
|
22 |
#' and be of a `double` or `numeric` type vector of length two.
|
|
23 |
#' @param whiskers (`character`)\cr names of the interval whiskers that will be plotted. Must match the `names`
|
|
24 |
#' attribute of the `interval` element in the list returned by `sfun`. It is possible to specify one whisker only,
|
|
25 |
#' lower or upper.
|
|
26 |
#' @param table (`character` or `NULL`)\cr names of the statistics that will be displayed in the table below the plot.
|
|
27 |
#' All the statistics indicated in `table` variable must be present in the object returned by `sfun`.
|
|
28 |
#' @param sfun (`closure`)\cr the function to compute the values of required statistics. It must return a named `list`
|
|
29 |
#' with atomic vectors. The names of the `list` elements refer to the names of the statistics and are used by `mid`,
|
|
30 |
#' `interval`, `table`. It must be able to accept as input a vector with data for which statistics are computed.
|
|
31 |
#' @param ... optional arguments to `sfun`.
|
|
32 |
#' @param mid_type (`character`)\cr controls the type of the `mid` plot, it can be point (`p`), line (`l`),
|
|
33 |
#' or point and line (`pl`).
|
|
34 |
#' @param mid_point_size (`integer` or `double`)\cr controls the font size of the point for `mid` plot.
|
|
35 |
#' @param position (`character` or `call`)\cr geom element position adjustment, either as a string, or the result of
|
|
36 |
#' a call to a position adjustment function.
|
|
37 |
#' @param legend_title (`character` string)\cr legend title.
|
|
38 |
#' @param legend_position (`character`)\cr the position of the plot legend (`none`, `left`, `right`, `bottom`, `top`,
|
|
39 |
#' or two-element numeric vector).
|
|
40 |
#' @param ggtheme (`theme`)\cr a graphical theme as provided by `ggplot2` to control styling of the plot.
|
|
41 |
#' @param y_lab (`character`)\cr y-axis label. If equal to `NULL`, then no label will be added.
|
|
42 |
#' @param y_lab_add_paramcd (`logical`)\cr should `paramcd`, i.e. `unique(df[[variables["paramcd"]]])` be added to the
|
|
43 |
#' y-axis label `y_lab`?
|
|
44 |
#' @param y_lab_add_unit (`logical`)\cr should y unit, i.e. `unique(df[[variables["y_unit"]]])` be added to the y-axis
|
|
45 |
#' label `y_lab`?
|
|
46 |
#' @param title (`character`)\cr plot title.
|
|
47 |
#' @param subtitle (`character`)\cr plot subtitle.
|
|
48 |
#' @param subtitle_add_paramcd (`logical`)\cr should `paramcd`, i.e. `unique(df[[variables["paramcd"]]])` be added to
|
|
49 |
#' the plot's subtitle `subtitle`?
|
|
50 |
#' @param subtitle_add_unit (`logical`)\cr should y unit, i.e. `unique(df[[variables["y_unit"]]])` be added to the
|
|
51 |
#' plot's subtitle `subtitle`?
|
|
52 |
#' @param caption (`character`)\cr optional caption below the plot.
|
|
53 |
#' @param table_format (named `character` or `NULL`)\cr format patterns for descriptive statistics used in the
|
|
54 |
#' (optional) table appended to the plot. It is passed directly to the `h_format_row` function through the `format`
|
|
55 |
#' parameter. Names of `table_format` must match the names of statistics returned by `sfun` function.
|
|
56 |
#' @param table_labels (named `character` or `NULL`)\cr labels for descriptive statistics used in the (optional) table
|
|
57 |
#' appended to the plot. Names of `table_labels` must match the names of statistics returned by `sfun` function.
|
|
58 |
#' @param table_font_size (`integer` or `double`)\cr controls the font size of values in the table.
|
|
59 |
#' @param newpage (`logical`)\cr should plot be drawn on new page?
|
|
60 |
#' @param col (`character`)\cr colors.
|
|
61 |
#'
|
|
62 |
#' @return A `ggplot` line plot (and statistics table if applicable).
|
|
63 |
#'
|
|
64 |
#' @examples
|
|
65 |
#' library(nestcolor)
|
|
66 |
#'
|
|
67 |
#' adsl <- tern_ex_adsl
|
|
68 |
#' adlb <- tern_ex_adlb %>% dplyr::filter(ANL01FL == "Y", PARAMCD == "ALT", AVISIT != "SCREENING")
|
|
69 |
#' adlb$AVISIT <- droplevels(adlb$AVISIT)
|
|
70 |
#' adlb <- dplyr::mutate(adlb, AVISIT = forcats::fct_reorder(AVISIT, AVISITN, min))
|
|
71 |
#'
|
|
72 |
#' # Mean with CI
|
|
73 |
#' g_lineplot(adlb, adsl, subtitle = "Laboratory Test:")
|
|
74 |
#'
|
|
75 |
#' # Mean with CI, no stratification
|
|
76 |
#' g_lineplot(adlb, variables = control_lineplot_vars(strata = NA))
|
|
77 |
#'
|
|
78 |
#' # Mean, upper whisker of CI, no strata counts N
|
|
79 |
#' g_lineplot(
|
|
80 |
#' adlb,
|
|
81 |
#' whiskers = "mean_ci_upr",
|
|
82 |
#' title = "Plot of Mean and Upper 95% Confidence Limit by Visit"
|
|
83 |
#' )
|
|
84 |
#'
|
|
85 |
#' # Median with CI
|
|
86 |
#' g_lineplot(
|
|
87 |
#' adlb,
|
|
88 |
#' adsl,
|
|
89 |
#' mid = "median",
|
|
90 |
#' interval = "median_ci",
|
|
91 |
#' whiskers = c("median_ci_lwr", "median_ci_upr"),
|
|
92 |
#' title = "Plot of Median and 95% Confidence Limits by Visit"
|
|
93 |
#' )
|
|
94 |
#'
|
|
95 |
#' # Mean, +/- SD
|
|
96 |
#' g_lineplot(adlb, adsl,
|
|
97 |
#' interval = "mean_sdi",
|
|
98 |
#' whiskers = c("mean_sdi_lwr", "mean_sdi_upr"),
|
|
99 |
#' title = "Plot of Median +/- SD by Visit"
|
|
100 |
#' )
|
|
101 |
#'
|
|
102 |
#' # Mean with CI plot with stats table
|
|
103 |
#' g_lineplot(adlb, adsl, table = c("n", "mean", "mean_ci"))
|
|
104 |
#'
|
|
105 |
#' # Mean with CI, table and customized confidence level
|
|
106 |
#' g_lineplot(
|
|
107 |
#' adlb,
|
|
108 |
#' adsl,
|
|
109 |
#' table = c("n", "mean", "mean_ci"),
|
|
110 |
#' control = control_summarize_vars(conf_level = 0.80),
|
|
111 |
#' title = "Plot of Mean and 80% Confidence Limits by Visit"
|
|
112 |
#' )
|
|
113 |
#'
|
|
114 |
#' # Mean with CI, table, filtered data
|
|
115 |
#' adlb_f <- dplyr::filter(adlb, ARMCD != "ARM A" | AVISIT == "BASELINE")
|
|
116 |
#' g_lineplot(adlb_f, table = c("n", "mean"))
|
|
117 |
#'
|
|
118 |
#' @export
|
|
119 |
g_lineplot <- function(df, |
|
120 |
alt_counts_df = NULL, |
|
121 |
variables = control_lineplot_vars(), |
|
122 |
mid = "mean", |
|
123 |
interval = "mean_ci", |
|
124 |
whiskers = c("mean_ci_lwr", "mean_ci_upr"), |
|
125 |
table = NULL, |
|
126 |
sfun = tern::s_summary, |
|
127 |
...,
|
|
128 |
mid_type = "pl", |
|
129 |
mid_point_size = 2, |
|
130 |
position = ggplot2::position_dodge(width = 0.4), |
|
131 |
legend_title = NULL, |
|
132 |
legend_position = "bottom", |
|
133 |
ggtheme = nestcolor::theme_nest(), |
|
134 |
y_lab = NULL, |
|
135 |
y_lab_add_paramcd = TRUE, |
|
136 |
y_lab_add_unit = TRUE, |
|
137 |
title = "Plot of Mean and 95% Confidence Limits by Visit", |
|
138 |
subtitle = "", |
|
139 |
subtitle_add_paramcd = TRUE, |
|
140 |
subtitle_add_unit = TRUE, |
|
141 |
caption = NULL, |
|
142 |
table_format = summary_formats(), |
|
143 |
table_labels = summary_labels(), |
|
144 |
table_font_size = 3, |
|
145 |
newpage = TRUE, |
|
146 |
col = NULL) { |
|
147 | 2x |
checkmate::assert_character(variables, any.missing = TRUE) |
148 | 2x |
checkmate::assert_character(mid, null.ok = TRUE) |
149 | 2x |
checkmate::assert_character(interval, null.ok = TRUE) |
150 | 2x |
checkmate::assert_character(col, null.ok = TRUE) |
151 | ||
152 | 2x |
checkmate::assert_string(title, null.ok = TRUE) |
153 | 2x |
checkmate::assert_string(subtitle, null.ok = TRUE) |
154 | ||
155 | 2x |
if (is.character(interval)) { |
156 | 2x |
checkmate::assert_vector(whiskers, min.len = 0, max.len = 2) |
157 |
}
|
|
158 | ||
159 | 2x |
if (length(whiskers) == 1) { |
160 | ! |
checkmate::assert_character(mid) |
161 |
}
|
|
162 | ||
163 | 2x |
if (is.character(mid)) { |
164 | 2x |
checkmate::assert_scalar(mid_type) |
165 | 2x |
checkmate::assert_subset(mid_type, c("pl", "p", "l")) |
166 |
}
|
|
167 | ||
168 | 2x |
x <- variables[["x"]] |
169 | 2x |
y <- variables[["y"]] |
170 | 2x |
paramcd <- variables["paramcd"] # NA if paramcd == NA or it is not in variables |
171 | 2x |
y_unit <- variables["y_unit"] # NA if y_unit == NA or it is not in variables |
172 | 2x |
if (is.na(variables["strata"])) { |
173 | ! |
strata <- NULL # NULL if strata == NA or it is not in variables |
174 |
} else { |
|
175 | 2x |
strata <- variables[["strata"]] |
176 |
}
|
|
177 | 2x |
checkmate::assert_flag(y_lab_add_paramcd, null.ok = TRUE) |
178 | 2x |
checkmate::assert_flag(subtitle_add_paramcd, null.ok = TRUE) |
179 | 2x |
if ((!is.null(y_lab) && y_lab_add_paramcd) || (!is.null(subtitle) && subtitle_add_paramcd)) { |
180 | 2x |
checkmate::assert_false(is.na(paramcd)) |
181 | 2x |
checkmate::assert_scalar(unique(df[[paramcd]])) |
182 |
}
|
|
183 | ||
184 | 2x |
checkmate::assert_flag(y_lab_add_unit, null.ok = TRUE) |
185 | 2x |
checkmate::assert_flag(subtitle_add_unit, null.ok = TRUE) |
186 | 2x |
if ((!is.null(y_lab) && y_lab_add_unit) || (!is.null(subtitle) && subtitle_add_unit)) { |
187 | 2x |
checkmate::assert_false(is.na(y_unit)) |
188 | 2x |
checkmate::assert_scalar(unique(df[[y_unit]])) |
189 |
}
|
|
190 | ||
191 | 2x |
if (!is.null(strata) && !is.null(alt_counts_df)) { |
192 | 2x |
checkmate::assert_set_equal(unique(alt_counts_df[[strata]]), unique(df[[strata]])) |
193 |
}
|
|
194 | ||
195 |
####################################### |
|
|
196 |
# ---- Compute required statistics ----
|
|
197 |
####################################### |
|
|
198 | 2x |
if (!is.null(strata)) { |
199 | 2x |
df_grp <- tidyr::expand(df, .data[[strata]], .data[[x]]) # expand based on levels of factors |
200 |
} else { |
|
201 | ! |
df_grp <- tidyr::expand(df, NULL, .data[[x]]) |
202 |
}
|
|
203 | 2x |
df_grp <- df_grp %>% |
204 | 2x |
dplyr::full_join(y = df[, c(strata, x, y)], by = c(strata, x), multiple = "all") %>% |
205 | 2x |
dplyr::group_by_at(c(strata, x)) |
206 | ||
207 | 2x |
df_stats <- df_grp %>% |
208 | 2x |
dplyr::summarise( |
209 | 2x |
data.frame(t(do.call(c, unname(sfun(.data[[y]], ...)[c(mid, interval)])))), |
210 | 2x |
.groups = "drop" |
211 |
)
|
|
212 | ||
213 | 2x |
df_stats <- df_stats[!is.na(df_stats[[mid]]), ] |
214 | ||
215 |
# add number of objects N in strata
|
|
216 | 2x |
if (!is.null(strata) && !is.null(alt_counts_df)) { |
217 | 2x |
strata_N <- paste0(strata, "_N") # nolint |
218 | ||
219 | 2x |
df_N <- as.data.frame(table(alt_counts_df[[strata]], exclude = c(NA, NaN, Inf))) # nolint |
220 | 2x |
colnames(df_N) <- c(strata, "N") # nolint |
221 | 2x |
df_N[[strata_N]] <- paste0(df_N[[strata]], " (N = ", df_N$N, ")") # nolint |
222 | ||
223 |
# strata_N should not be in clonames(df_stats)
|
|
224 | 2x |
checkmate::assert_disjunct(strata_N, colnames(df_stats)) |
225 | ||
226 | 2x |
df_stats <- merge(x = df_stats, y = df_N[, c(strata, strata_N)], by = strata) |
227 | ! |
} else if (!is.null(strata)) { |
228 | ! |
strata_N <- strata # nolint |
229 |
} else { |
|
230 | ! |
strata_N <- NULL # nolint |
231 |
}
|
|
232 | ||
233 |
############################################### |
|
|
234 |
# ---- Prepare certain plot's properties. ----
|
|
235 |
############################################### |
|
|
236 |
# legend title
|
|
237 | 2x |
if (is.null(legend_title) && !is.null(strata) && legend_position != "none") { |
238 | 2x |
legend_title <- attr(df[[strata]], "label") |
239 |
}
|
|
240 | ||
241 |
# y label
|
|
242 | 2x |
if (!is.null(y_lab)) { |
243 | 1x |
if (y_lab_add_paramcd) { |
244 | 1x |
y_lab <- paste(y_lab, unique(df[[paramcd]])) |
245 |
}
|
|
246 | ||
247 | 1x |
if (y_lab_add_unit) { |
248 | 1x |
y_lab <- paste0(y_lab, " (", unique(df[[y_unit]]), ")") |
249 |
}
|
|
250 | ||
251 | 1x |
y_lab <- trimws(y_lab) |
252 |
}
|
|
253 | ||
254 |
# subtitle
|
|
255 | 2x |
if (!is.null(subtitle)) { |
256 | 2x |
if (subtitle_add_paramcd) { |
257 | 2x |
subtitle <- paste(subtitle, unique(df[[paramcd]])) |
258 |
}
|
|
259 | ||
260 | 2x |
if (subtitle_add_unit) { |
261 | 2x |
subtitle <- paste0(subtitle, " (", unique(df[[y_unit]]), ")") |
262 |
}
|
|
263 | ||
264 | 2x |
subtitle <- trimws(subtitle) |
265 |
}
|
|
266 | ||
267 |
############################### |
|
|
268 |
# ---- Build plot object. ----
|
|
269 |
############################### |
|
|
270 | 2x |
p <- ggplot2::ggplot( |
271 | 2x |
data = df_stats, |
272 | 2x |
mapping = ggplot2::aes( |
273 | 2x |
x = .data[[x]], y = .data[[mid]], |
274 | 2x |
color = if (is.null(strata_N)) NULL else .data[[strata_N]], |
275 | 2x |
shape = if (is.null(strata_N)) NULL else .data[[strata_N]], |
276 | 2x |
lty = if (is.null(strata_N)) NULL else .data[[strata_N]], |
277 | 2x |
group = if (is.null(strata_N)) NULL else .data[[strata_N]] |
278 |
)
|
|
279 |
)
|
|
280 | ||
281 | 2x |
if (!is.null(mid)) { |
282 |
# points
|
|
283 | 2x |
if (grepl("p", mid_type, fixed = TRUE)) { |
284 | 2x |
p <- p + ggplot2::geom_point(position = position, size = mid_point_size, na.rm = TRUE) |
285 |
}
|
|
286 | ||
287 |
# lines
|
|
288 |
# further conditions in if are to ensure that not all of the groups consist of only one observation
|
|
289 | 2x |
if (grepl("l", mid_type, fixed = TRUE) && |
290 | 2x |
!is.null(strata) && |
291 | 2x |
!all(dplyr::summarise(df_grp, count_n = dplyr::n())[["count_n"]] == 1L)) { |
292 | 2x |
p <- p + ggplot2::geom_line(position = position, na.rm = TRUE) |
293 |
}
|
|
294 |
}
|
|
295 | ||
296 |
# interval
|
|
297 | 2x |
if (!is.null(interval)) { |
298 | 2x |
p <- p + |
299 | 2x |
ggplot2::geom_errorbar( |
300 | 2x |
ggplot2::aes(ymin = .data[[whiskers[1]]], ymax = .data[[whiskers[max(1, length(whiskers))]]]), |
301 | 2x |
width = 0.45, |
302 | 2x |
position = position |
303 |
)
|
|
304 | ||
305 | 2x |
if (length(whiskers) == 1) { # lwr or upr only; mid is then required |
306 |
# workaround as geom_errorbar does not provide single-direction whiskers
|
|
307 | ! |
p <- p + |
308 | ! |
ggplot2::geom_linerange( |
309 | ! |
data = df_stats[!is.na(df_stats[[whiskers]]), ], # as na.rm =TRUE does not suppress warnings |
310 | ! |
ggplot2::aes(ymin = .data[[mid]], ymax = .data[[whiskers]]), |
311 | ! |
position = position, |
312 | ! |
na.rm = TRUE, |
313 | ! |
show.legend = FALSE |
314 |
)
|
|
315 |
}
|
|
316 |
}
|
|
317 | ||
318 | 2x |
p <- p + |
319 | 2x |
ggplot2::scale_y_continuous(labels = scales::comma, expand = ggplot2::expansion(c(0.25, .25))) + |
320 | 2x |
ggplot2::labs( |
321 | 2x |
title = title, |
322 | 2x |
subtitle = subtitle, |
323 | 2x |
caption = caption, |
324 | 2x |
color = legend_title, |
325 | 2x |
lty = legend_title, |
326 | 2x |
shape = legend_title, |
327 | 2x |
x = attr(df[[x]], "label"), |
328 | 2x |
y = y_lab |
329 |
)
|
|
330 | ||
331 | 2x |
if (!is.null(col)) { |
332 | ! |
p <- p + |
333 | ! |
ggplot2::scale_color_manual(values = col) |
334 |
}
|
|
335 | ||
336 | 2x |
if (!is.null(ggtheme)) { |
337 | 2x |
p <- p + ggtheme |
338 |
} else { |
|
339 | ! |
p <- p + |
340 | ! |
ggplot2::theme_bw() + |
341 | ! |
ggplot2::theme( |
342 | ! |
legend.key.width = grid::unit(1, "cm"), |
343 | ! |
legend.position = legend_position, |
344 | ! |
legend.direction = ifelse( |
345 | ! |
legend_position %in% c("top", "bottom"), |
346 | ! |
"horizontal",
|
347 | ! |
"vertical"
|
348 |
)
|
|
349 |
)
|
|
350 |
}
|
|
351 | ||
352 |
############################################################# |
|
|
353 |
# ---- Optionally, add table to the bottom of the plot. ----
|
|
354 |
############################################################# |
|
|
355 | 2x |
if (!is.null(table)) { |
356 | 1x |
df_stats_table <- df_grp %>% |
357 | 1x |
dplyr::summarise( |
358 | 1x |
h_format_row( |
359 | 1x |
x = sfun(.data[[y]], ...)[table], |
360 | 1x |
format = table_format, |
361 | 1x |
labels = table_labels |
362 |
),
|
|
363 | 1x |
.groups = "drop" |
364 |
)
|
|
365 | ||
366 | 1x |
stats_lev <- rev(setdiff(colnames(df_stats_table), c(strata, x))) |
367 | ||
368 | 1x |
df_stats_table <- df_stats_table %>% |
369 | 1x |
tidyr::pivot_longer( |
370 | 1x |
cols = -dplyr::all_of(c(strata, x)), |
371 | 1x |
names_to = "stat", |
372 | 1x |
values_to = "value", |
373 | 1x |
names_ptypes = list(stat = factor(levels = stats_lev)) |
374 |
)
|
|
375 | ||
376 | 1x |
tbl <- ggplot2::ggplot( |
377 | 1x |
df_stats_table,
|
378 | 1x |
ggplot2::aes(x = .data[[x]], y = .data[["stat"]], label = .data[["value"]]) |
379 |
) + |
|
380 | 1x |
ggplot2::geom_text(size = table_font_size) + |
381 | 1x |
ggplot2::theme_bw() + |
382 | 1x |
ggplot2::theme( |
383 | 1x |
panel.border = ggplot2::element_blank(), |
384 | 1x |
panel.grid.major = ggplot2::element_blank(), |
385 | 1x |
panel.grid.minor = ggplot2::element_blank(), |
386 | 1x |
axis.ticks = ggplot2::element_blank(), |
387 | 1x |
axis.title = ggplot2::element_blank(), |
388 | 1x |
axis.text.x = ggplot2::element_blank(), |
389 | 1x |
axis.text.y = ggplot2::element_text(margin = ggplot2::margin(t = 0, r = 0, b = 0, l = 5)), |
390 | 1x |
strip.text = ggplot2::element_text(hjust = 0), |
391 | 1x |
strip.text.x = ggplot2::element_text(margin = ggplot2::margin(1.5, 0, 1.5, 0, "pt")), |
392 | 1x |
strip.background = ggplot2::element_rect(fill = "grey95", color = NA), |
393 | 1x |
legend.position = "none" |
394 |
)
|
|
395 | ||
396 | 1x |
if (!is.null(strata)) { |
397 | 1x |
tbl <- tbl + ggplot2::facet_wrap(facets = strata, ncol = 1) |
398 |
}
|
|
399 | ||
400 |
# align plot and table
|
|
401 | 1x |
cowplot::plot_grid(p, tbl, ncol = 1) |
402 |
} else { |
|
403 | 1x |
p
|
404 |
}
|
|
405 |
}
|
|
406 | ||
407 |
#' Helper function to get the right formatting in the optional table in `g_lineplot`.
|
|
408 |
#'
|
|
409 |
#' @description `r lifecycle::badge("stable")`
|
|
410 |
#'
|
|
411 |
#' @param x (named `list`)\cr list of numerical values to be formatted and optionally labeled.
|
|
412 |
#' Elements of `x` must be `numeric` vectors.
|
|
413 |
#' @param format (named `character` or `NULL`)\cr format patterns for `x`. Names of the `format` must
|
|
414 |
#' match the names of `x`. This parameter is passed directly to the `rtables::format_rcell`
|
|
415 |
#' function through the `format` parameter.
|
|
416 |
#' @param labels (named `character` or `NULL`)\cr optional labels for `x`. Names of the `labels` must
|
|
417 |
#' match the names of `x`. When a label is not specified for an element of `x`,
|
|
418 |
#' then this function tries to use `label` or `names` (in this order) attribute of that element
|
|
419 |
#' (depending on which one exists and it is not `NULL` or `NA` or `NaN`). If none of these attributes
|
|
420 |
#' are attached to a given element of `x`, then the label is automatically generated.
|
|
421 |
#'
|
|
422 |
#' @return A single row `data.frame` object.
|
|
423 |
#'
|
|
424 |
#' @examples
|
|
425 |
#' mean_ci <- c(48, 51)
|
|
426 |
#' x <- list(mean = 50, mean_ci = mean_ci)
|
|
427 |
#' format <- c(mean = "xx.x", mean_ci = "(xx.xx, xx.xx)")
|
|
428 |
#' labels <- c(mean = "My Mean")
|
|
429 |
#' h_format_row(x, format, labels)
|
|
430 |
#'
|
|
431 |
#' attr(mean_ci, "label") <- "Mean 95% CI"
|
|
432 |
#' x <- list(mean = 50, mean_ci = mean_ci)
|
|
433 |
#' h_format_row(x, format, labels)
|
|
434 |
#'
|
|
435 |
#' @export
|
|
436 |
h_format_row <- function(x, format, labels = NULL) { |
|
437 |
# cell: one row, one column data.frame
|
|
438 | 19x |
format_cell <- function(x, format, label = NULL) { |
439 | 56x |
fc <- format_rcell(x = x, format = format) |
440 | 56x |
if (is.na(fc)) { |
441 | ! |
fc <- "NA" |
442 |
}
|
|
443 | 56x |
x_label <- attr(x, "label") |
444 | 56x |
if (!is.null(label) && !is.na(label)) { |
445 | 37x |
names(fc) <- label |
446 | 19x |
} else if (!is.null(x_label) && !is.na(x_label)) { |
447 | 18x |
names(fc) <- x_label |
448 | 1x |
} else if (length(x) == length(fc)) { |
449 | ! |
names(fc) <- names(x) |
450 |
}
|
|
451 | 56x |
as.data.frame(t(fc)) |
452 |
}
|
|
453 | ||
454 | 19x |
row <- do.call( |
455 | 19x |
cbind,
|
456 | 19x |
lapply( |
457 | 19x |
names(x), function(xn) format_cell(x[[xn]], format = format[xn], label = labels[xn]) |
458 |
)
|
|
459 |
)
|
|
460 | ||
461 | 19x |
row
|
462 |
}
|
|
463 | ||
464 |
#' Control Function for `g_lineplot` Function
|
|
465 |
#'
|
|
466 |
#' @description `r lifecycle::badge("stable")`
|
|
467 |
#'
|
|
468 |
#' Default values for `variables` parameter in `g_lineplot` function.
|
|
469 |
#' A variable's default value can be overwritten for any variable.
|
|
470 |
#'
|
|
471 |
#' @param x (`character`)\cr x variable name.
|
|
472 |
#' @param y (`character`)\cr y variable name.
|
|
473 |
#' @param strata (`character` or `NA`)\cr strata variable name.
|
|
474 |
#' @param paramcd (`character` or `NA`)\cr `paramcd` variable name.
|
|
475 |
#' @param y_unit (`character` or `NA`)\cr `y_unit` variable name.
|
|
476 |
#'
|
|
477 |
#' @return A named character vector of variable names.
|
|
478 |
#'
|
|
479 |
#' @examples
|
|
480 |
#' control_lineplot_vars()
|
|
481 |
#' control_lineplot_vars(strata = NA)
|
|
482 |
#'
|
|
483 |
#' @export
|
|
484 |
control_lineplot_vars <- function(x = "AVISIT", y = "AVAL", strata = "ARM", paramcd = "PARAMCD", y_unit = "AVALU") { |
|
485 | 2x |
checkmate::assert_string(x) |
486 | 2x |
checkmate::assert_string(y) |
487 | 2x |
checkmate::assert_string(strata, na.ok = TRUE) |
488 | 2x |
checkmate::assert_string(paramcd, na.ok = TRUE) |
489 | 2x |
checkmate::assert_string(y_unit, na.ok = TRUE) |
490 | ||
491 | 2x |
variables <- c(x = x, y = y, strata = strata, paramcd = paramcd, y_unit = y_unit) |
492 | 2x |
return(variables) |
493 |
}
|
1 |
#' Summary for Poisson Negative Binomial.
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("experimental")`
|
|
4 |
#'
|
|
5 |
#' Summarize results of a Poisson Negative Binomial Regression.
|
|
6 |
#' This can be used to analyze count and/or frequency data using a linear model.
|
|
7 |
#'
|
|
8 |
#' @inheritParams argument_convention
|
|
9 |
#'
|
|
10 |
#' @name summarize_glm_count
|
|
11 |
NULL
|
|
12 | ||
13 |
#' Helper Functions for Poisson Models.
|
|
14 |
#'
|
|
15 |
#' @description `r lifecycle::badge("experimental")`
|
|
16 |
#'
|
|
17 |
#' Helper functions that can be used to return the results of various Poisson models.
|
|
18 |
#'
|
|
19 |
#' @inheritParams argument_convention
|
|
20 |
#'
|
|
21 |
#' @seealso [summarize_glm_count]
|
|
22 |
#'
|
|
23 |
#' @name h_glm_count
|
|
24 |
NULL
|
|
25 | ||
26 |
#' @describeIn h_glm_count Helper function to return results of a poisson model.
|
|
27 |
#'
|
|
28 |
#' @param .df_row (`data.frame`)\cr data set that includes all the variables that are called
|
|
29 |
#' in `.var` and `variables`.
|
|
30 |
#' @param variables (named `list` of `strings`)\cr list of additional analysis variables, with
|
|
31 |
#' expected elements:
|
|
32 |
#' * `arm` (`string`)\cr group variable, for which the covariate adjusted means of multiple
|
|
33 |
#' groups will be summarized. Specifically, the first level of `arm` variable is taken as the
|
|
34 |
#' reference group.
|
|
35 |
#' * `covariates` (`character`)\cr a vector that can contain single variable names (such as
|
|
36 |
#' `"X1"`), and/or interaction terms indicated by `"X1 * X2"`.
|
|
37 |
#' * `offset` (`numeric`)\cr a numeric vector or scalar adding an offset.
|
|
38 |
#' @param `weights`(`character`)\cr a character vector specifying weights used
|
|
39 |
#' in averaging predictions. Number of weights must equal the number of levels included in the covariates.
|
|
40 |
#' Weights option passed to emmeans function (hyperlink) (link to emmeans documentation)
|
|
41 |
#'
|
|
42 |
#' @return
|
|
43 |
#' * `h_glm_poisson()` returns the results of a Poisson model.
|
|
44 |
#'
|
|
45 |
#' @keywords internal
|
|
46 |
h_glm_poisson <- function(.var, |
|
47 |
.df_row,
|
|
48 |
variables,
|
|
49 |
weights) { |
|
50 | 9x |
arm <- variables$arm |
51 | 9x |
covariates <- variables$covariates |
52 | 9x |
offset <- .df_row[[variables$offset]] |
53 | ||
54 | 7x |
formula <- stats::as.formula(paste0( |
55 | 7x |
.var, " ~ ", |
56 |
" + ",
|
|
57 | 7x |
paste(covariates, collapse = " + "), |
58 |
" + ",
|
|
59 | 7x |
arm
|
60 |
)) |
|
61 | ||
62 | 7x |
glm_fit <- stats::glm( |
63 | 7x |
formula = formula, |
64 | 7x |
offset = offset, |
65 | 7x |
data = .df_row, |
66 | 7x |
family = stats::poisson(link = "log") |
67 |
)
|
|
68 | ||
69 | 7x |
emmeans_fit <- emmeans::emmeans( |
70 | 7x |
glm_fit,
|
71 | 7x |
specs = arm, |
72 | 7x |
data = .df_row, |
73 | 7x |
type = "response", |
74 | 7x |
offset = 0, |
75 | 7x |
weights = weights |
76 |
)
|
|
77 | ||
78 | 7x |
list( |
79 | 7x |
glm_fit = glm_fit, |
80 | 7x |
emmeans_fit = emmeans_fit |
81 |
)
|
|
82 |
}
|
|
83 | ||
84 |
#' @describeIn h_glm_count Helper function to return results of a quasipoisson model.
|
|
85 |
#'
|
|
86 |
#' @inheritParams summarize_glm_count
|
|
87 |
#'
|
|
88 |
#' @return
|
|
89 |
#' * `h_glm_quasipoisson()` returns the results of a Quasi-Poisson model.
|
|
90 |
#'
|
|
91 |
#'
|
|
92 |
#' @keywords internal
|
|
93 |
h_glm_quasipoisson <- function(.var, |
|
94 |
.df_row,
|
|
95 |
variables,
|
|
96 |
weights) { |
|
97 | 7x |
arm <- variables$arm |
98 | 7x |
covariates <- variables$covariates |
99 | 7x |
offset <- .df_row[[variables$offset]] |
100 | ||
101 | 5x |
formula <- stats::as.formula(paste0( |
102 | 5x |
.var, " ~ ", |
103 |
" + ",
|
|
104 | 5x |
paste(covariates, collapse = " + "), |
105 |
" + ",
|
|
106 | 5x |
arm
|
107 |
)) |
|
108 | ||
109 | 5x |
glm_fit <- stats::glm( |
110 | 5x |
formula = formula, |
111 | 5x |
offset = offset, |
112 | 5x |
data = .df_row, |
113 | 5x |
family = stats::quasipoisson(link = "log") |
114 |
)
|
|
115 | ||
116 | 5x |
emmeans_fit <- emmeans::emmeans( |
117 | 5x |
glm_fit,
|
118 | 5x |
specs = arm, |
119 | 5x |
data = .df_row, |
120 | 5x |
type = "response", |
121 | 5x |
offset = 0, |
122 | 5x |
weights = weights |
123 |
)
|
|
124 | ||
125 | 5x |
list( |
126 | 5x |
glm_fit = glm_fit, |
127 | 5x |
emmeans_fit = emmeans_fit |
128 |
)
|
|
129 |
}
|
|
130 | ||
131 |
#' @describeIn h_glm_count Helper function to return the results of the
|
|
132 |
#' selected model (poisson, quasipoisson, negative binomial).
|
|
133 |
#'
|
|
134 |
#' @param .df_row (`data.frame`)\cr data set that includes all the variables that are called
|
|
135 |
#' in `.var` and `variables`.
|
|
136 |
#' @param variables (named `list` of `strings`)\cr list of additional analysis variables, with
|
|
137 |
#' expected elements:
|
|
138 |
#' * `arm` (`string`)\cr group variable, for which the covariate adjusted means of multiple
|
|
139 |
#' groups will be summarized. Specifically, the first level of `arm` variable is taken as the
|
|
140 |
#' reference group.
|
|
141 |
#' * `covariates` (`character`)\cr a vector that can contain single variable names (such as
|
|
142 |
#' `"X1"`), and/or interaction terms indicated by `"X1 * X2"`.
|
|
143 |
#' * `offset` (`numeric`)\cr a numeric vector or scalar adding an offset.
|
|
144 |
#' @param `weights`(`character`)\cr character vector specifying weights used in averaging predictions.
|
|
145 |
#' @param `distribution`(`character`)\cr a character value specifying the distribution
|
|
146 |
#' used in the regression (poisson, quasipoisson).
|
|
147 |
#'
|
|
148 |
#' @return
|
|
149 |
#' * `h_glm_count()` returns the results of the selected model.
|
|
150 |
#'
|
|
151 |
#'
|
|
152 |
#' @keywords internal
|
|
153 |
h_glm_count <- function(.var, |
|
154 |
.df_row,
|
|
155 |
variables,
|
|
156 |
distribution,
|
|
157 |
weights) { |
|
158 | 11x |
if (distribution == "negbin") { |
159 | ! |
stop("negative binomial distribution is not currently available.") |
160 |
}
|
|
161 | 9x |
switch(distribution, |
162 | 6x |
poisson = h_glm_poisson(.var, .df_row, variables, weights), |
163 | 3x |
quasipoisson = h_glm_quasipoisson(.var, .df_row, variables, weights), |
164 | ! |
negbin = list() # h_glm_negbin(.var, .df_row, variables, weights) # nolint |
165 |
)
|
|
166 |
}
|
|
167 | ||
168 |
#' @describeIn h_glm_count Helper function to return the estimated means.
|
|
169 |
#'
|
|
170 |
#' @param .df_row (`data.frame`)\cr data set that includes all the variables that are called in `.var` and `variables`.
|
|
171 |
#' @param conf_level (`numeric`)\cr value used to derive the confidence interval for the rate.
|
|
172 |
#' @param obj (`glm.fit`)\cr fitted model object used to derive the mean rate estimates in each treatment arm.
|
|
173 |
#' @param `arm` (`string`)\cr group variable, for which the covariate adjusted means of multiple groups will be
|
|
174 |
#' summarized. Specifically, the first level of `arm` variable is taken as the reference group.
|
|
175 |
#'
|
|
176 |
#' @return
|
|
177 |
#' * `h_ppmeans()` returns the estimated means.
|
|
178 |
#'
|
|
179 |
#'
|
|
180 |
#' @keywords internal
|
|
181 |
h_ppmeans <- function(obj, .df_row, arm, conf_level) { |
|
182 | 3x |
alpha <- 1 - conf_level |
183 | 3x |
p <- 1 - alpha / 2 |
184 | ||
185 | 3x |
arm_levels <- levels(.df_row[[arm]]) |
186 | ||
187 | 3x |
out <- lapply(arm_levels, function(lev) { |
188 | 9x |
temp <- .df_row |
189 | 9x |
temp[[arm]] <- factor(lev, levels = arm_levels) |
190 | ||
191 | 9x |
mf <- stats::model.frame(obj$formula, data = temp) |
192 | 9x |
X <- stats::model.matrix(obj$formula, data = mf) # nolint |
193 | ||
194 | 9x |
rate <- stats::predict(obj, newdata = mf, type = "response") |
195 | 9x |
rate_hat <- mean(rate) |
196 | ||
197 | 9x |
zz <- colMeans(rate * X) |
198 | 9x |
se <- sqrt(as.numeric(t(zz) %*% stats::vcov(obj) %*% zz)) |
199 | 9x |
rate_lwr <- rate_hat * exp(-stats::qnorm(p) * se / rate_hat) |
200 | 9x |
rate_upr <- rate_hat * exp(stats::qnorm(p) * se / rate_hat) |
201 | ||
202 | 9x |
c(rate_hat, rate_lwr, rate_upr) |
203 |
}) |
|
204 | ||
205 | 3x |
names(out) <- arm_levels |
206 | 3x |
out <- do.call(rbind, out) |
207 | 3x |
if ("negbin" %in% class(obj)) { |
208 | ! |
colnames(out) <- c("response", "asymp.LCL", "asymp.UCL") |
209 |
} else { |
|
210 | 3x |
colnames(out) <- c("rate", "asymp.LCL", "asymp.UCL") |
211 |
}
|
|
212 | 3x |
out <- as.data.frame(out) |
213 | 3x |
out[[arm]] <- rownames(out) |
214 | 3x |
out
|
215 |
}
|
|
216 | ||
217 |
#' @describeIn summarize_glm_count Statistics function that produces a named list of results
|
|
218 |
#' of the investigated Poisson model.
|
|
219 |
#'
|
|
220 |
#' @inheritParams h_glm_count
|
|
221 |
#'
|
|
222 |
#' @return
|
|
223 |
#' * `s_glm_count()` returns a named `list` of 5 statistics:
|
|
224 |
#' * `n`: Count of complete sample size for the group.
|
|
225 |
#' * `rate`: Estimated event rate per follow-up time.
|
|
226 |
#' * `rate_ci`: Confidence level for estimated rate per follow-up time.
|
|
227 |
#' * `rate_ratio`: Ratio of event rates in each treatment arm to the reference arm.
|
|
228 |
#' * `rate_ratio_ci`: Confidence level for the rate ratio.
|
|
229 |
#' * `pval`: p-value.
|
|
230 |
#'
|
|
231 |
#'
|
|
232 |
#' @keywords internal
|
|
233 |
s_glm_count <- function(df, |
|
234 |
.var,
|
|
235 |
.df_row,
|
|
236 |
variables,
|
|
237 |
.ref_group,
|
|
238 |
.in_ref_col,
|
|
239 |
distribution,
|
|
240 |
conf_level,
|
|
241 |
rate_mean_method,
|
|
242 |
weights,
|
|
243 |
scale = 1) { |
|
244 | 3x |
arm <- variables$arm |
245 | ||
246 | 3x |
y <- df[[.var]] |
247 | 2x |
smry_level <- as.character(unique(df[[arm]])) |
248 | ||
249 |
# ensure there is only 1 value
|
|
250 | 2x |
checkmate::assert_scalar(smry_level) |
251 | ||
252 | 2x |
results <- h_glm_count( |
253 | 2x |
.var = .var, |
254 | 2x |
.df_row = .df_row, |
255 | 2x |
variables = variables, |
256 | 2x |
distribution = distribution, |
257 | 2x |
weights
|
258 |
)
|
|
259 | ||
260 | 2x |
if (rate_mean_method == "emmeans") { |
261 | ! |
emmeans_smry <- summary(results$emmeans_fit, level = conf_level) |
262 | 2x |
} else if (rate_mean_method == "ppmeans") { |
263 | 2x |
emmeans_smry <- h_ppmeans(results$glm_fit, .df_row, arm, conf_level) |
264 |
}
|
|
265 | ||
266 | 2x |
emmeans_smry_level <- emmeans_smry[emmeans_smry[[arm]] == smry_level, ] |
267 | ||
268 | 2x |
if (.in_ref_col) { |
269 | 1x |
list( |
270 | 1x |
n = length(y[!is.na(y)]), |
271 | 1x |
rate = formatters::with_label( |
272 | 1x |
ifelse(distribution == "negbin", emmeans_smry_level$response * scale, emmeans_smry_level$rate), |
273 | 1x |
"Adjusted Rate"
|
274 |
),
|
|
275 | 1x |
rate_ci = formatters::with_label( |
276 | 1x |
c(emmeans_smry_level$asymp.LCL * scale, emmeans_smry_level$asymp.UCL * scale), |
277 | 1x |
f_conf_level(conf_level) |
278 |
),
|
|
279 | 1x |
rate_ratio = formatters::with_label(character(), "Adjusted Rate Ratio"), |
280 | 1x |
rate_ratio_ci = formatters::with_label(character(), f_conf_level(conf_level)), |
281 | 1x |
pval = formatters::with_label(character(), "p-value") |
282 |
)
|
|
283 |
} else { |
|
284 | 1x |
emmeans_contrasts <- emmeans::contrast( |
285 | 1x |
results$emmeans_fit, |
286 | 1x |
method = "trt.vs.ctrl", |
287 | 1x |
ref = grep( |
288 | 1x |
as.character(unique(.ref_group[[arm]])), |
289 | 1x |
as.data.frame(results$emmeans_fit)[[arm]] |
290 |
)
|
|
291 |
)
|
|
292 | ||
293 | 1x |
contrasts_smry <- summary( |
294 | 1x |
emmeans_contrasts,
|
295 | 1x |
infer = TRUE, |
296 | 1x |
adjust = "none" |
297 |
)
|
|
298 | ||
299 | 1x |
smry_contrasts_level <- contrasts_smry[grepl(smry_level, contrasts_smry$contrast), ] |
300 | ||
301 | 1x |
list( |
302 | 1x |
n = length(y[!is.na(y)]), |
303 | 1x |
rate = formatters::with_label( |
304 | 1x |
ifelse(distribution == "negbin", emmeans_smry_level$response * scale, emmeans_smry_level$rate), |
305 | 1x |
"Adjusted Rate"
|
306 |
),
|
|
307 | 1x |
rate_ci = formatters::with_label( |
308 | 1x |
c(emmeans_smry_level$asymp.LCL * scale, emmeans_smry_level$asymp.UCL * scale), |
309 | 1x |
f_conf_level(conf_level) |
310 |
),
|
|
311 | 1x |
rate_ratio = formatters::with_label(smry_contrasts_level$ratio, "Adjusted Rate Ratio"), |
312 | 1x |
rate_ratio_ci = formatters::with_label( |
313 | 1x |
c(smry_contrasts_level$asymp.LCL, smry_contrasts_level$asymp.UCL), |
314 | 1x |
f_conf_level(conf_level) |
315 |
),
|
|
316 | 1x |
pval = formatters::with_label(smry_contrasts_level$p.value, "p-value") |
317 |
)
|
|
318 |
}
|
|
319 |
}
|
|
320 | ||
321 |
#' @describeIn summarize_glm_count Formatted analysis function which is used as `afun` in `summarize_glm_count()`.
|
|
322 |
#'
|
|
323 |
#' @return
|
|
324 |
#' * `a_glm_count()` returns the corresponding list with formatted [rtables::CellValue()].
|
|
325 |
#'
|
|
326 |
#'
|
|
327 |
#' @keywords internal
|
|
328 |
a_glm_count <- make_afun( |
|
329 |
s_glm_count,
|
|
330 |
.indent_mods = c( |
|
331 |
"n" = 0L, |
|
332 |
"rate" = 0L, |
|
333 |
"rate_ci" = 1L, |
|
334 |
"rate_ratio" = 0L, |
|
335 |
"rate_ratio_ci" = 1L, |
|
336 |
"pval" = 1L |
|
337 |
),
|
|
338 |
.formats = c( |
|
339 |
"n" = "xx", |
|
340 |
"rate" = "xx.xxxx", |
|
341 |
"rate_ci" = "(xx.xxxx, xx.xxxx)", |
|
342 |
"rate_ratio" = "xx.xxxx", |
|
343 |
"rate_ratio_ci" = "(xx.xxxx, xx.xxxx)", |
|
344 |
"pval" = "x.xxxx | (<0.0001)" |
|
345 |
),
|
|
346 |
.null_ref_cells = FALSE |
|
347 |
)
|
|
348 | ||
349 |
#' @describeIn summarize_glm_count Layout-creating function which can take statistics function arguments
|
|
350 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()].
|
|
351 |
#'
|
|
352 |
#' @return
|
|
353 |
#' * `summarize_glm_count()` returns a layout object suitable for passing to further layouting functions,
|
|
354 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing
|
|
355 |
#' the statistics from `s_glm_count()` to the table layout.
|
|
356 |
#'
|
|
357 |
#' @examples
|
|
358 |
#' library(dplyr)
|
|
359 |
#' anl <- tern_ex_adtte %>% filter(PARAMCD == "TNE")
|
|
360 |
#' anl$AVAL_f <- as.factor(anl$AVAL)
|
|
361 |
#'
|
|
362 |
#' lyt <- basic_table() %>%
|
|
363 |
#' split_cols_by("ARM", ref_group = "B: Placebo") %>%
|
|
364 |
#' add_colcounts() %>%
|
|
365 |
#' summarize_vars(
|
|
366 |
#' "AVAL_f",
|
|
367 |
#' var_labels = "Number of exacerbations per patient",
|
|
368 |
#' .stats = c("count_fraction"),
|
|
369 |
#' .formats = c("count_fraction" = "xx (xx.xx%)"),
|
|
370 |
#' .label = c("Number of exacerbations per patient")
|
|
371 |
#' ) %>%
|
|
372 |
#' summarize_glm_count(
|
|
373 |
#' vars = "AVAL",
|
|
374 |
#' variables = list(arm = "ARM", offset = "lgTMATRSK", covariates = NULL),
|
|
375 |
#' conf_level = 0.95,
|
|
376 |
#' distribution = "poisson",
|
|
377 |
#' rate_mean_method = "emmeans",
|
|
378 |
#' var_labels = "Unadjusted exacerbation rate (per year)",
|
|
379 |
#' table_names = "unadj",
|
|
380 |
#' .stats = c("rate"),
|
|
381 |
#' .labels = c(rate = "Rate")
|
|
382 |
#' ) %>%
|
|
383 |
#' summarize_glm_count(
|
|
384 |
#' vars = "AVAL",
|
|
385 |
#' variables = list(arm = "ARM", offset = "lgTMATRSK", covariates = c("REGION1")),
|
|
386 |
#' conf_level = 0.95,
|
|
387 |
#' distribution = "quasipoisson",
|
|
388 |
#' rate_mean_method = "ppmeans",
|
|
389 |
#' var_labels = "Adjusted (QP) exacerbation rate (per year)",
|
|
390 |
#' table_names = "adj",
|
|
391 |
#' .stats = c("rate", "rate_ci", "rate_ratio", "rate_ratio_ci", "pval"),
|
|
392 |
#' .labels = c(
|
|
393 |
#' rate = "Rate", rate_ci = "Rate CI", rate_ratio = "Rate Ratio",
|
|
394 |
#' rate_ratio_ci = "Rate Ratio CI", pval = "p value"
|
|
395 |
#' )
|
|
396 |
#' )
|
|
397 |
#' build_table(lyt = lyt, df = anl)
|
|
398 |
#'
|
|
399 |
#' @export
|
|
400 |
summarize_glm_count <- function(lyt, |
|
401 |
vars,
|
|
402 |
var_labels,
|
|
403 |
...,
|
|
404 |
show_labels = "visible", |
|
405 |
table_names = vars, |
|
406 |
.stats = NULL, |
|
407 |
.formats = NULL, |
|
408 |
.labels = NULL, |
|
409 |
.indent_mods = NULL) { |
|
410 | 1x |
afun <- make_afun( |
411 | 1x |
a_glm_count,
|
412 | 1x |
.stats = .stats, |
413 | 1x |
.formats = .formats, |
414 | 1x |
.labels = .labels, |
415 | 1x |
.indent_mods = .indent_mods |
416 |
)
|
|
417 | ||
418 | 1x |
analyze( |
419 | 1x |
lyt,
|
420 | 1x |
vars,
|
421 | 1x |
var_labels = var_labels, |
422 | 1x |
show_labels = show_labels, |
423 | 1x |
table_names = table_names, |
424 | 1x |
afun = afun, |
425 | 1x |
extra_args = list(...) |
426 |
)
|
|
427 |
}
|
1 |
#' Count the Number of Patients with Particular Flags
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' The primary analysis variable `.var` denotes the unique patient identifier.
|
|
6 |
#'
|
|
7 |
#' @inheritParams argument_convention
|
|
8 |
#'
|
|
9 |
#' @seealso [count_patients_with_event]
|
|
10 |
#'
|
|
11 |
#' @name count_patients_with_flags
|
|
12 |
NULL
|
|
13 | ||
14 |
#' @describeIn count_patients_with_flags Statistics function which counts the number of patients for which
|
|
15 |
#' a particular flag variable is `TRUE`.
|
|
16 |
#'
|
|
17 |
#' @inheritParams summarize_variables
|
|
18 |
#' @param .var (`character`)\cr name of the column that contains the unique identifier.
|
|
19 |
#' @param flag_variables (`character`)\cr a character vector specifying the names of `logical`
|
|
20 |
#' variables from analysis dataset used for counting the number of unique identifiers.
|
|
21 |
#'
|
|
22 |
#' @return
|
|
23 |
#' * `s_count_patients_with_flags()` returns the count and the fraction of unique identifiers with each particular
|
|
24 |
#' flag as a list of statistics `n`, `count`, `count_fraction`, and `n_blq`, with one element per flag.
|
|
25 |
#'
|
|
26 |
#' @examples
|
|
27 |
#' library(dplyr)
|
|
28 |
#'
|
|
29 |
#' # `s_count_patients_with_flags()`
|
|
30 |
#'
|
|
31 |
#' # Add labelled flag variables to analysis dataset.
|
|
32 |
#' adae <- tern_ex_adae %>%
|
|
33 |
#' mutate(
|
|
34 |
#' fl1 = TRUE,
|
|
35 |
#' fl2 = TRTEMFL == "Y",
|
|
36 |
#' fl3 = TRTEMFL == "Y" & AEOUT == "FATAL",
|
|
37 |
#' fl4 = TRTEMFL == "Y" & AEOUT == "FATAL" & AEREL == "Y"
|
|
38 |
#' )
|
|
39 |
#' labels <- c(
|
|
40 |
#' "fl1" = "Total AEs",
|
|
41 |
#' "fl2" = "Total number of patients with at least one adverse event",
|
|
42 |
#' "fl3" = "Total number of patients with fatal AEs",
|
|
43 |
#' "fl4" = "Total number of patients with related fatal AEs"
|
|
44 |
#' )
|
|
45 |
#' formatters::var_labels(adae)[names(labels)] <- labels
|
|
46 |
#'
|
|
47 |
#' s_count_patients_with_flags(
|
|
48 |
#' adae,
|
|
49 |
#' "SUBJID",
|
|
50 |
#' flag_variables = c("fl1", "fl2", "fl3", "fl4"),
|
|
51 |
#' denom = "N_col",
|
|
52 |
#' .N_col = 1000
|
|
53 |
#' )
|
|
54 |
#'
|
|
55 |
#' @export
|
|
56 |
s_count_patients_with_flags <- function(df, |
|
57 |
.var,
|
|
58 |
flag_variables,
|
|
59 |
.N_col, # nolint |
|
60 |
.N_row, # nolint |
|
61 |
denom = c("n", "N_row", "N_col")) { |
|
62 | 2x |
if (is.null(names(flag_variables))) flag_variables <- stats::setNames(flag_variables, flag_variables) |
63 | 2x |
flag_names <- unname(flag_variables) |
64 | 2x |
flag_variables <- names(flag_variables) |
65 | ||
66 | 2x |
checkmate::assert_subset(flag_variables, colnames(df)) |
67 | 2x |
temp <- sapply(flag_variables, function(x) { |
68 | 3x |
tmp <- Map(function(y) which(df[[y]]), x) |
69 | 3x |
position_satisfy_flags <- Reduce(intersect, tmp) |
70 | 3x |
id_satisfy_flags <- as.character(unique(df[position_satisfy_flags, ][[.var]])) |
71 | 3x |
s_count_values( |
72 | 3x |
as.character(unique(df[[.var]])), |
73 | 3x |
id_satisfy_flags,
|
74 | 3x |
denom = denom, |
75 | 3x |
.N_col = .N_col, |
76 | 3x |
.N_row = .N_row |
77 |
)
|
|
78 |
}) |
|
79 | 2x |
colnames(temp) <- flag_names |
80 | 2x |
temp <- data.frame(t(temp)) |
81 | 2x |
result <- temp %>% as.list() |
82 | 2x |
if (length(flag_variables) == 1) { |
83 | 1x |
for (i in 1:3) names(result[[i]]) <- flag_names[1] |
84 |
}
|
|
85 | 2x |
result
|
86 |
}
|
|
87 | ||
88 |
#' @describeIn count_patients_with_flags Formatted analysis function which is used as `afun`
|
|
89 |
#' in `count_patients_with_flags()`.
|
|
90 |
#'
|
|
91 |
#' @return
|
|
92 |
#' * `a_count_patients_with_flags()` returns the corresponding list with formatted [rtables::CellValue()].
|
|
93 |
#'
|
|
94 |
#' @examples
|
|
95 |
#' # We need to ungroup `count_fraction` first so that the `rtables` formatting
|
|
96 |
#' # function `format_count_fraction()` can be applied correctly.
|
|
97 |
#'
|
|
98 |
#' # `a_count_patients_with_flags()`
|
|
99 |
#'
|
|
100 |
#' afun <- make_afun(a_count_patients_with_flags,
|
|
101 |
#' .stats = "count_fraction",
|
|
102 |
#' .ungroup_stats = "count_fraction"
|
|
103 |
#' )
|
|
104 |
#' afun(
|
|
105 |
#' adae,
|
|
106 |
#' .N_col = 10L,
|
|
107 |
#' .N_row = 10L,
|
|
108 |
#' .var = "USUBJID",
|
|
109 |
#' flag_variables = c("fl1", "fl2", "fl3", "fl4")
|
|
110 |
#' )
|
|
111 |
#'
|
|
112 |
#' @export
|
|
113 |
a_count_patients_with_flags <- make_afun( |
|
114 |
s_count_patients_with_flags,
|
|
115 |
.formats = c("count_fraction" = format_count_fraction_fixed_dp) |
|
116 |
)
|
|
117 | ||
118 |
#' @describeIn count_patients_with_flags Layout-creating function which can take statistics function
|
|
119 |
#' arguments and additional format arguments. This function is a wrapper for [rtables::analyze()].
|
|
120 |
#'
|
|
121 |
#' @return
|
|
122 |
#' * `count_patients_with_flags()` returns a layout object suitable for passing to further layouting functions,
|
|
123 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing
|
|
124 |
#' the statistics from `s_count_patients_with_flags()` to the table layout.
|
|
125 |
#'
|
|
126 |
#' @examples
|
|
127 |
#' # `count_patients_with_flags()`
|
|
128 |
#'
|
|
129 |
#' lyt2 <- basic_table() %>%
|
|
130 |
#' split_cols_by("ARM") %>%
|
|
131 |
#' add_colcounts() %>%
|
|
132 |
#' count_patients_with_flags(
|
|
133 |
#' "SUBJID",
|
|
134 |
#' flag_variables = formatters::var_labels(adae[, c("fl1", "fl2", "fl3", "fl4")]),
|
|
135 |
#' denom = "N_col"
|
|
136 |
#' )
|
|
137 |
#' build_table(lyt2, adae, alt_counts_df = tern_ex_adsl)
|
|
138 |
#'
|
|
139 |
#' @export
|
|
140 |
count_patients_with_flags <- function(lyt, |
|
141 |
var,
|
|
142 |
var_labels = var, |
|
143 |
show_labels = "hidden", |
|
144 |
...,
|
|
145 |
table_names = paste0("tbl_flags_", var), |
|
146 |
.stats = "count_fraction", |
|
147 |
.formats = NULL, |
|
148 |
.indent_mods = NULL) { |
|
149 | 4x |
afun <- make_afun( |
150 | 4x |
a_count_patients_with_flags,
|
151 | 4x |
.stats = .stats, |
152 | 4x |
.formats = .formats, |
153 | 4x |
.indent_mods = .indent_mods, |
154 | 4x |
.ungroup_stats = .stats |
155 |
)
|
|
156 | ||
157 | 4x |
lyt <- analyze( |
158 | 4x |
lyt = lyt, |
159 | 4x |
vars = var, |
160 | 4x |
var_labels = var_labels, |
161 | 4x |
show_labels = show_labels, |
162 | 4x |
afun = afun, |
163 | 4x |
table_names = table_names, |
164 | 4x |
extra_args = list(...) |
165 |
)
|
|
166 | ||
167 | 4x |
lyt
|
168 |
}
|
1 |
#' Occurrence Counts by Grade
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Functions for analyzing frequencies and fractions of occurrences by grade for patients
|
|
6 |
#' with occurrence data. Multiple occurrences within one individual are counted once at the
|
|
7 |
#' greatest intensity/highest grade level.
|
|
8 |
#'
|
|
9 |
#' @inheritParams argument_convention
|
|
10 |
#' @param grade_groups (named `list` of `character`)\cr containing groupings of grades.
|
|
11 |
#' @param remove_single (`logical`)\cr `TRUE` to not include the elements of one-element grade groups
|
|
12 |
#' in the the output list; in this case only the grade groups names will be included in the output.
|
|
13 |
#'
|
|
14 |
#' @seealso Relevant helper function [h_append_grade_groups()].
|
|
15 |
#'
|
|
16 |
#' @name count_occurrences_by_grade
|
|
17 |
NULL
|
|
18 | ||
19 |
#' Helper function for [s_count_occurrences_by_grade()]
|
|
20 |
#'
|
|
21 |
#' @description `r lifecycle::badge("stable")`
|
|
22 |
#'
|
|
23 |
#' Helper function for [s_count_occurrences_by_grade()] to insert grade groupings into list with
|
|
24 |
#' individual grade frequencies. The order of the final result follows the order of `grade_groups`.
|
|
25 |
#' The elements under any-grade group (if any), i.e. the grade group equal to `refs` will be moved to
|
|
26 |
#' the end. Grade groups names must be unique.
|
|
27 |
#'
|
|
28 |
#' @inheritParams count_occurrences_by_grade
|
|
29 |
#' @param refs (named `list` of `numeric`)\cr where each name corresponds to a reference grade level
|
|
30 |
#' and each entry represents a count.
|
|
31 |
#'
|
|
32 |
#' @return Formatted list of grade groupings.
|
|
33 |
#'
|
|
34 |
#' @examples
|
|
35 |
#' h_append_grade_groups(
|
|
36 |
#' list(
|
|
37 |
#' "Any Grade" = as.character(1:5),
|
|
38 |
#' "Grade 1-2" = c("1", "2"),
|
|
39 |
#' "Grade 3-4" = c("3", "4")
|
|
40 |
#' ),
|
|
41 |
#' list("1" = 10, "2" = 20, "3" = 30, "4" = 40, "5" = 50)
|
|
42 |
#' )
|
|
43 |
#'
|
|
44 |
#' h_append_grade_groups(
|
|
45 |
#' list(
|
|
46 |
#' "Any Grade" = as.character(5:1),
|
|
47 |
#' "Grade A" = "5",
|
|
48 |
#' "Grade B" = c("4", "3")
|
|
49 |
#' ),
|
|
50 |
#' list("1" = 10, "2" = 20, "3" = 30, "4" = 40, "5" = 50)
|
|
51 |
#' )
|
|
52 |
#'
|
|
53 |
#' h_append_grade_groups(
|
|
54 |
#' list(
|
|
55 |
#' "Any Grade" = as.character(1:5),
|
|
56 |
#' "Grade 1-2" = c("1", "2"),
|
|
57 |
#' "Grade 3-4" = c("3", "4")
|
|
58 |
#' ),
|
|
59 |
#' list("1" = 10, "2" = 5, "3" = 0)
|
|
60 |
#' )
|
|
61 |
#'
|
|
62 |
#' @export
|
|
63 |
h_append_grade_groups <- function(grade_groups, refs, remove_single = TRUE) { |
|
64 | 13x |
checkmate::assert_list(grade_groups) |
65 | 13x |
checkmate::assert_list(refs) |
66 | 13x |
refs_orig <- refs |
67 | 13x |
elements <- unique(unlist(grade_groups)) |
68 | ||
69 |
### compute sums in groups
|
|
70 | 13x |
grp_sum <- lapply(grade_groups, function(i) do.call(sum, refs[i])) |
71 | 13x |
if (!checkmate::test_subset(elements, names(refs))) { |
72 | 2x |
padding_el <- setdiff(elements, names(refs)) |
73 | 2x |
refs[padding_el] <- 0 |
74 |
}
|
|
75 | 13x |
result <- c(grp_sum, refs) |
76 | ||
77 |
### order result while keeping grade_groups's ordering
|
|
78 | 13x |
ordr <- grade_groups |
79 | ||
80 |
# elements of any-grade group (if any) will be moved to the end
|
|
81 | 13x |
is_any <- sapply(grade_groups, setequal, y = names(refs)) |
82 | 13x |
ordr[is_any] <- list(character(0)) # hide elements under any-grade group |
83 | ||
84 |
# groups-elements combined sequence
|
|
85 | 13x |
ordr <- c(lapply(names(ordr), function(g) c(g, ordr[[g]])), recursive = TRUE, use.names = FALSE) |
86 | 13x |
ordr <- ordr[!duplicated(ordr)] |
87 | ||
88 |
# append remaining elements (if any)
|
|
89 | 13x |
ordr <- union(ordr, unlist(grade_groups[is_any])) # from any-grade group |
90 | 13x |
ordr <- union(ordr, names(refs)) # from refs |
91 | ||
92 |
# remove elements of single-element groups, if any
|
|
93 | 13x |
if (remove_single) { |
94 | 13x |
is_single <- sapply(grade_groups, length) == 1L |
95 | 13x |
ordr <- setdiff(ordr, unlist(grade_groups[is_single])) |
96 |
}
|
|
97 | ||
98 |
# apply the order
|
|
99 | 13x |
result <- result[ordr] |
100 | ||
101 |
# remove groups without any elements in the original refs
|
|
102 |
# note: it's OK if groups have 0 value
|
|
103 | 13x |
keep_grp <- vapply(grade_groups, function(x, rf) { |
104 | 37x |
any(x %in% rf) |
105 | 13x |
}, rf = names(refs_orig), logical(1)) |
106 | ||
107 | 13x |
keep_el <- names(result) %in% names(refs_orig) | names(result) %in% names(keep_grp)[keep_grp] |
108 | 13x |
result <- result[keep_el] |
109 | ||
110 | 13x |
result
|
111 |
}
|
|
112 | ||
113 |
#' @describeIn count_occurrences_by_grade Statistics function which counts the
|
|
114 |
#' number of patients by highest grade.
|
|
115 |
#'
|
|
116 |
#' @return
|
|
117 |
#' * `s_count_occurrences_by_grade()` returns a list of counts and fractions with one element per grade level or
|
|
118 |
#' grade level grouping.
|
|
119 |
#'
|
|
120 |
#' @examples
|
|
121 |
#' library(dplyr)
|
|
122 |
#' df <- data.frame(
|
|
123 |
#' USUBJID = as.character(c(1:6, 1)),
|
|
124 |
#' ARM = factor(c("A", "A", "A", "B", "B", "B", "A"), levels = c("A", "B")),
|
|
125 |
#' AETOXGR = factor(c(1, 2, 3, 4, 1, 2, 3), levels = c(1:5)),
|
|
126 |
#' AESEV = factor(
|
|
127 |
#' x = c("MILD", "MODERATE", "SEVERE", "MILD", "MILD", "MODERATE", "SEVERE"),
|
|
128 |
#' levels = c("MILD", "MODERATE", "SEVERE")
|
|
129 |
#' ),
|
|
130 |
#' stringsAsFactors = FALSE
|
|
131 |
#' )
|
|
132 |
#' df_adsl <- df %>%
|
|
133 |
#' select(USUBJID, ARM) %>%
|
|
134 |
#' unique()
|
|
135 |
#'
|
|
136 |
#' s_count_occurrences_by_grade(
|
|
137 |
#' df,
|
|
138 |
#' .N_col = 10L,
|
|
139 |
#' .var = "AETOXGR",
|
|
140 |
#' id = "USUBJID",
|
|
141 |
#' grade_groups = list("ANY" = levels(df$AETOXGR))
|
|
142 |
#' )
|
|
143 |
#'
|
|
144 |
#' @export
|
|
145 |
s_count_occurrences_by_grade <- function(df, |
|
146 |
.var,
|
|
147 |
.N_col, # nolint |
|
148 |
id = "USUBJID", |
|
149 |
grade_groups = list(), |
|
150 |
remove_single = TRUE, |
|
151 |
labelstr = "") { |
|
152 | 3x |
assert_valid_factor(df[[.var]]) |
153 | 3x |
assert_df_with_variables(df, list(grade = .var, id = id)) |
154 | ||
155 | 3x |
if (nrow(df) < 1) { |
156 | ! |
grade_levels <- levels(df[[.var]]) |
157 | ! |
l_count <- as.list(rep(0, length(grade_levels))) |
158 | ! |
names(l_count) <- grade_levels |
159 |
} else { |
|
160 | 3x |
if (isTRUE(is.factor(df[[id]]))) { |
161 | ! |
assert_valid_factor(df[[id]], any.missing = FALSE) |
162 |
} else { |
|
163 | 3x |
checkmate::assert_character(df[[id]], min.chars = 1, any.missing = FALSE) |
164 |
}
|
|
165 | 3x |
checkmate::assert_count(.N_col) |
166 | ||
167 | 3x |
id <- df[[id]] |
168 | 3x |
grade <- df[[.var]] |
169 | ||
170 | 3x |
if (!is.ordered(grade)) { |
171 | 3x |
grade_lbl <- obj_label(grade) |
172 | 3x |
lvls <- levels(grade) |
173 | 3x |
if (sum(grepl("^\\d+$", lvls)) %in% c(0, length(lvls))) { |
174 | 3x |
lvl_ord <- lvls |
175 |
} else { |
|
176 | ! |
lvls[!grepl("^\\d+$", lvls)] <- min(as.numeric(lvls[grepl("^\\d+$", lvls)])) - 1 |
177 | ! |
lvl_ord <- levels(grade)[order(as.numeric(lvls))] |
178 |
}
|
|
179 | 3x |
grade <- formatters::with_label(factor(grade, levels = lvl_ord, ordered = TRUE), grade_lbl) |
180 |
}
|
|
181 | ||
182 | 3x |
df_max <- stats::aggregate(grade ~ id, FUN = max, drop = FALSE) |
183 | 3x |
l_count <- as.list(table(df_max$grade)) |
184 |
}
|
|
185 | ||
186 | 3x |
if (length(grade_groups) > 0) { |
187 | 2x |
l_count <- h_append_grade_groups(grade_groups, l_count, remove_single) |
188 |
}
|
|
189 | ||
190 | 3x |
l_count_fraction <- lapply(l_count, function(i, denom) c(i, i / denom), denom = .N_col) |
191 | ||
192 | 3x |
list( |
193 | 3x |
count_fraction = l_count_fraction |
194 |
)
|
|
195 |
}
|
|
196 | ||
197 |
#' @describeIn count_occurrences_by_grade Formatted analysis function which is used as `afun`
|
|
198 |
#' in `count_occurrences_by_grade()`.
|
|
199 |
#'
|
|
200 |
#' @return
|
|
201 |
#' * `a_count_occurrences_by_grade()` returns the corresponding list with formatted [rtables::CellValue()].
|
|
202 |
#'
|
|
203 |
#' @examples
|
|
204 |
#' # We need to ungroup `count_fraction` first so that the `rtables` formatting
|
|
205 |
#' # function `format_count_fraction()` can be applied correctly.
|
|
206 |
#' afun <- make_afun(a_count_occurrences_by_grade, .ungroup_stats = "count_fraction")
|
|
207 |
#' afun(
|
|
208 |
#' df,
|
|
209 |
#' .N_col = 10L,
|
|
210 |
#' .var = "AETOXGR",
|
|
211 |
#' id = "USUBJID",
|
|
212 |
#' grade_groups = list("ANY" = levels(df$AETOXGR))
|
|
213 |
#' )
|
|
214 |
#'
|
|
215 |
#' @export
|
|
216 |
a_count_occurrences_by_grade <- make_afun( |
|
217 |
s_count_occurrences_by_grade,
|
|
218 |
.formats = c("count_fraction" = format_count_fraction_fixed_dp) |
|
219 |
)
|
|
220 | ||
221 |
#' @describeIn count_occurrences_by_grade Layout-creating function which can take statistics function
|
|
222 |
#' arguments and additional format arguments. This function is a wrapper for [rtables::analyze()].
|
|
223 |
#'
|
|
224 |
#' @param var_labels (`character`)\cr labels to show in the result table.
|
|
225 |
#'
|
|
226 |
#' @return
|
|
227 |
#' * `count_occurrences_by_grade()` returns a layout object suitable for passing to further layouting functions,
|
|
228 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing
|
|
229 |
#' the statistics from `s_count_occurrences_by_grade()` to the table layout.
|
|
230 |
#'
|
|
231 |
#' @examples
|
|
232 |
#' # Layout creating function with custom format.
|
|
233 |
#' basic_table() %>%
|
|
234 |
#' split_cols_by("ARM") %>%
|
|
235 |
#' add_colcounts() %>%
|
|
236 |
#' count_occurrences_by_grade(
|
|
237 |
#' var = "AESEV",
|
|
238 |
#' .formats = c("count_fraction" = "xx.xx (xx.xx%)")
|
|
239 |
#' ) %>%
|
|
240 |
#' build_table(df, alt_counts_df = df_adsl)
|
|
241 |
#'
|
|
242 |
#' # Define additional grade groupings.
|
|
243 |
#' grade_groups <- list(
|
|
244 |
#' "-Any-" = c("1", "2", "3", "4", "5"),
|
|
245 |
#' "Grade 1-2" = c("1", "2"),
|
|
246 |
#' "Grade 3-5" = c("3", "4", "5")
|
|
247 |
#' )
|
|
248 |
#'
|
|
249 |
#' basic_table() %>%
|
|
250 |
#' split_cols_by("ARM") %>%
|
|
251 |
#' add_colcounts() %>%
|
|
252 |
#' count_occurrences_by_grade(
|
|
253 |
#' var = "AETOXGR",
|
|
254 |
#' grade_groups = grade_groups
|
|
255 |
#' ) %>%
|
|
256 |
#' build_table(df, alt_counts_df = df_adsl)
|
|
257 |
#'
|
|
258 |
#' @export
|
|
259 |
count_occurrences_by_grade <- function(lyt, |
|
260 |
var,
|
|
261 |
var_labels = var, |
|
262 |
show_labels = "default", |
|
263 |
...,
|
|
264 |
table_names = var, |
|
265 |
.stats = NULL, |
|
266 |
.formats = NULL, |
|
267 |
.indent_mods = NULL, |
|
268 |
.labels = NULL) { |
|
269 | 7x |
afun <- make_afun( |
270 | 7x |
a_count_occurrences_by_grade,
|
271 | 7x |
.stats = .stats, |
272 | 7x |
.formats = .formats, |
273 | 7x |
.indent_mods = .indent_mods, |
274 | 7x |
.ungroup_stats = "count_fraction" |
275 |
)
|
|
276 | ||
277 | 7x |
analyze( |
278 | 7x |
lyt = lyt, |
279 | 7x |
vars = var, |
280 | 7x |
var_labels = var_labels, |
281 | 7x |
show_labels = show_labels, |
282 | 7x |
afun = afun, |
283 | 7x |
table_names = table_names, |
284 | 7x |
extra_args = list(...) |
285 |
)
|
|
286 |
}
|
|
287 | ||
288 |
#' @describeIn count_occurrences_by_grade Layout-creating function which can take content function arguments
|
|
289 |
#' and additional format arguments. This function is a wrapper for [rtables::summarize_row_groups()].
|
|
290 |
#'
|
|
291 |
#' @return
|
|
292 |
#' * `summarize_occurrences_by_grade()` returns a layout object suitable for passing to further layouting functions,
|
|
293 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted content rows
|
|
294 |
#' containing the statistics from `s_count_occurrences_by_grade()` to the table layout.
|
|
295 |
#'
|
|
296 |
#' @examples
|
|
297 |
#' # Layout creating function with custom format.
|
|
298 |
#' basic_table() %>%
|
|
299 |
#' add_colcounts() %>%
|
|
300 |
#' split_rows_by("ARM", child_labels = "visible", nested = TRUE) %>%
|
|
301 |
#' summarize_occurrences_by_grade(
|
|
302 |
#' var = "AESEV",
|
|
303 |
#' .formats = c("count_fraction" = "xx.xx (xx.xx%)")
|
|
304 |
#' ) %>%
|
|
305 |
#' build_table(df, alt_counts_df = df_adsl)
|
|
306 |
#'
|
|
307 |
#' basic_table() %>%
|
|
308 |
#' add_colcounts() %>%
|
|
309 |
#' split_rows_by("ARM", child_labels = "visible", nested = TRUE) %>%
|
|
310 |
#' summarize_occurrences_by_grade(
|
|
311 |
#' var = "AETOXGR",
|
|
312 |
#' grade_groups = grade_groups
|
|
313 |
#' ) %>%
|
|
314 |
#' build_table(df, alt_counts_df = df_adsl)
|
|
315 |
#'
|
|
316 |
#' @export
|
|
317 |
summarize_occurrences_by_grade <- function(lyt, |
|
318 |
var,
|
|
319 |
...,
|
|
320 |
.stats = NULL, |
|
321 |
.formats = NULL, |
|
322 |
.indent_mods = NULL, |
|
323 |
.labels = NULL) { |
|
324 | 2x |
cfun <- make_afun( |
325 | 2x |
a_count_occurrences_by_grade,
|
326 | 2x |
.stats = .stats, |
327 | 2x |
.formats = .formats, |
328 | 2x |
.labels = .labels, |
329 | 2x |
.indent_mods = .indent_mods, |
330 | 2x |
.ungroup_stats = "count_fraction" |
331 |
)
|
|
332 | ||
333 | 2x |
summarize_row_groups( |
334 | 2x |
lyt = lyt, |
335 | 2x |
var = var, |
336 | 2x |
cfun = cfun, |
337 | 2x |
extra_args = list(...) |
338 |
)
|
|
339 |
}
|
1 |
#' Helper Functions for Tabulating Biomarker Effects on Survival by Subgroup
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Helper functions which are documented here separately to not confuse the user
|
|
6 |
#' when reading about the user-facing functions.
|
|
7 |
#'
|
|
8 |
#' @inheritParams survival_biomarkers_subgroups
|
|
9 |
#' @inheritParams argument_convention
|
|
10 |
#' @inheritParams fit_coxreg_multivar
|
|
11 |
#'
|
|
12 |
#' @examples
|
|
13 |
#' library(dplyr)
|
|
14 |
#' library(forcats)
|
|
15 |
#'
|
|
16 |
#' adtte <- tern_ex_adtte
|
|
17 |
#'
|
|
18 |
#' # Save variable labels before data processing steps.
|
|
19 |
#' adtte_labels <- formatters::var_labels(adtte, fill = FALSE)
|
|
20 |
#'
|
|
21 |
#' adtte_f <- adtte %>%
|
|
22 |
#' filter(PARAMCD == "OS") %>%
|
|
23 |
#' mutate(
|
|
24 |
#' AVALU = as.character(AVALU),
|
|
25 |
#' is_event = CNSR == 0
|
|
26 |
#' )
|
|
27 |
#' labels <- c("AVALU" = adtte_labels[["AVALU"]], "is_event" = "Event Flag")
|
|
28 |
#' formatters::var_labels(adtte_f)[names(labels)] <- labels
|
|
29 |
#'
|
|
30 |
#' @name h_survival_biomarkers_subgroups
|
|
31 |
NULL
|
|
32 | ||
33 |
#' @describeIn h_survival_biomarkers_subgroups helps with converting the "survival" function variable list
|
|
34 |
#' to the "Cox regression" variable list. The reason is that currently there is an inconsistency between the variable
|
|
35 |
#' names accepted by `extract_survival_subgroups()` and `fit_coxreg_multivar()`.
|
|
36 |
#'
|
|
37 |
#' @param biomarker (`string`)\cr the name of the biomarker variable.
|
|
38 |
#'
|
|
39 |
#' @return
|
|
40 |
#' * `h_surv_to_coxreg_variables()` returns a named `list` of elements `time`, `event`, `arm`,
|
|
41 |
#' `covariates`, and `strata`.
|
|
42 |
#'
|
|
43 |
#' @examples
|
|
44 |
#' # This is how the variable list is converted internally.
|
|
45 |
#' h_surv_to_coxreg_variables(
|
|
46 |
#' variables = list(
|
|
47 |
#' tte = "AVAL",
|
|
48 |
#' is_event = "EVNT",
|
|
49 |
#' covariates = c("A", "B"),
|
|
50 |
#' strata = "D"
|
|
51 |
#' ),
|
|
52 |
#' biomarker = "AGE"
|
|
53 |
#' )
|
|
54 |
#'
|
|
55 |
#' @export
|
|
56 |
h_surv_to_coxreg_variables <- function(variables, biomarker) { |
|
57 | 41x |
checkmate::assert_list(variables) |
58 | 41x |
checkmate::assert_string(variables$tte) |
59 | 41x |
checkmate::assert_string(variables$is_event) |
60 | 41x |
checkmate::assert_string(biomarker) |
61 | 41x |
list( |
62 | 41x |
time = variables$tte, |
63 | 41x |
event = variables$is_event, |
64 | 41x |
arm = biomarker, |
65 | 41x |
covariates = variables$covariates, |
66 | 41x |
strata = variables$strata |
67 |
)
|
|
68 |
}
|
|
69 | ||
70 |
#' @describeIn h_survival_biomarkers_subgroups prepares estimates for number of events, patients and median survival
|
|
71 |
#' times, as well as hazard ratio estimates, confidence intervals and p-values, for multiple biomarkers
|
|
72 |
#' in a given single data set.
|
|
73 |
#' `variables` corresponds to names of variables found in `data`, passed as a named list and requires elements
|
|
74 |
#' `tte`, `is_event`, `biomarkers` (vector of continuous biomarker variables) and optionally `subgroups` and `strat`.
|
|
75 |
#'
|
|
76 |
#' @return
|
|
77 |
#' * `h_coxreg_mult_cont_df()` returns a `data.frame` containing estimates and statistics for the selected biomarkers.
|
|
78 |
#'
|
|
79 |
#' @examples
|
|
80 |
#' # For a single population, estimate separately the effects
|
|
81 |
#' # of two biomarkers.
|
|
82 |
#' df <- h_coxreg_mult_cont_df(
|
|
83 |
#' variables = list(
|
|
84 |
#' tte = "AVAL",
|
|
85 |
#' is_event = "is_event",
|
|
86 |
#' biomarkers = c("BMRKR1", "AGE"),
|
|
87 |
#' covariates = "SEX",
|
|
88 |
#' strata = c("STRATA1", "STRATA2")
|
|
89 |
#' ),
|
|
90 |
#' data = adtte_f
|
|
91 |
#' )
|
|
92 |
#' df
|
|
93 |
#'
|
|
94 |
#' # If the data set is empty, still the corresponding rows with missings are returned.
|
|
95 |
#' h_coxreg_mult_cont_df(
|
|
96 |
#' variables = list(
|
|
97 |
#' tte = "AVAL",
|
|
98 |
#' is_event = "is_event",
|
|
99 |
#' biomarkers = c("BMRKR1", "AGE"),
|
|
100 |
#' covariates = "REGION1",
|
|
101 |
#' strata = c("STRATA1", "STRATA2")
|
|
102 |
#' ),
|
|
103 |
#' data = adtte_f[NULL, ]
|
|
104 |
#' )
|
|
105 |
#'
|
|
106 |
#' @export
|
|
107 |
h_coxreg_mult_cont_df <- function(variables, |
|
108 |
data,
|
|
109 |
control = control_coxreg()) { |
|
110 | 21x |
assert_df_with_variables(data, variables) |
111 | 21x |
checkmate::assert_list(control, names = "named") |
112 | 21x |
checkmate::assert_character(variables$biomarkers, min.len = 1, any.missing = FALSE) |
113 | 21x |
conf_level <- control[["conf_level"]] |
114 | 21x |
pval_label <- paste0( |
115 |
# the regex capitalizes the first letter of the string / senetence.
|
|
116 | 21x |
"p-value (", gsub("(^[a-z])", "\\U\\1", trimws(control[["pval_method"]]), perl = TRUE), ")" |
117 |
)
|
|
118 |
# If there is any data, run model, otherwise return empty results.
|
|
119 | 21x |
if (nrow(data) > 0) { |
120 | 20x |
bm_cols <- match(variables$biomarkers, names(data)) |
121 | 20x |
l_result <- lapply(variables$biomarkers, function(bm) { |
122 | 40x |
coxreg_list <- fit_coxreg_multivar( |
123 | 40x |
variables = h_surv_to_coxreg_variables(variables, bm), |
124 | 40x |
data = data, |
125 | 40x |
control = control |
126 |
)
|
|
127 | 40x |
result <- do.call( |
128 | 40x |
h_coxreg_multivar_extract,
|
129 | 40x |
c(list(var = bm), coxreg_list[c("mod", "data", "control")]) |
130 |
)
|
|
131 | 40x |
data_fit <- as.data.frame(as.matrix(coxreg_list$mod$y)) |
132 | 40x |
data_fit$status <- as.logical(data_fit$status) |
133 | 40x |
median <- s_surv_time( |
134 | 40x |
df = data_fit, |
135 | 40x |
.var = "time", |
136 | 40x |
is_event = "status" |
137 | 40x |
)$median |
138 | 40x |
data.frame( |
139 |
# Dummy column needed downstream to create a nested header.
|
|
140 | 40x |
biomarker = bm, |
141 | 40x |
biomarker_label = formatters::var_labels(data[bm], fill = TRUE), |
142 | 40x |
n_tot = coxreg_list$mod$n, |
143 | 40x |
n_tot_events = coxreg_list$mod$nevent, |
144 | 40x |
median = as.numeric(median), |
145 | 40x |
result[1L, c("hr", "lcl", "ucl")], |
146 | 40x |
conf_level = conf_level, |
147 | 40x |
pval = result[1L, "pval"], |
148 | 40x |
pval_label = pval_label, |
149 | 40x |
stringsAsFactors = FALSE |
150 |
)
|
|
151 |
}) |
|
152 | 20x |
do.call(rbind, args = c(l_result, make.row.names = FALSE)) |
153 |
} else { |
|
154 | 1x |
data.frame( |
155 | 1x |
biomarker = variables$biomarkers, |
156 | 1x |
biomarker_label = formatters::var_labels(data[variables$biomarkers], fill = TRUE), |
157 | 1x |
n_tot = 0L, |
158 | 1x |
n_tot_events = 0L, |
159 | 1x |
median = NA, |
160 | 1x |
hr = NA, |
161 | 1x |
lcl = NA, |
162 | 1x |
ucl = NA, |
163 | 1x |
conf_level = conf_level, |
164 | 1x |
pval = NA, |
165 | 1x |
pval_label = pval_label, |
166 | 1x |
row.names = seq_along(variables$biomarkers), |
167 | 1x |
stringsAsFactors = FALSE |
168 |
)
|
|
169 |
}
|
|
170 |
}
|
|
171 | ||
172 |
#' @describeIn h_survival_biomarkers_subgroups prepares a single sub-table given a `df_sub` containing
|
|
173 |
#' the results for a single biomarker.
|
|
174 |
#'
|
|
175 |
#' @param df (`data.frame`)\cr results for a single biomarker, as part of what is
|
|
176 |
#' returned by [extract_survival_biomarkers()] (it needs a couple of columns which are
|
|
177 |
#' added by that high-level function relative to what is returned by [h_coxreg_mult_cont_df()],
|
|
178 |
#' see the example).
|
|
179 |
#'
|
|
180 |
#' @return
|
|
181 |
#' * `h_tab_surv_one_biomarker()` returns an `rtables` table object with the given statistics arranged in columns.
|
|
182 |
#'
|
|
183 |
#' @examples
|
|
184 |
#' # Starting from above `df`, zoom in on one biomarker and add required columns.
|
|
185 |
#' df1 <- df[1, ]
|
|
186 |
#' df1$subgroup <- "All patients"
|
|
187 |
#' df1$row_type <- "content"
|
|
188 |
#' df1$var <- "ALL"
|
|
189 |
#' df1$var_label <- "All patients"
|
|
190 |
#' h_tab_surv_one_biomarker(
|
|
191 |
#' df1,
|
|
192 |
#' vars = c("n_tot", "n_tot_events", "median", "hr", "ci", "pval"),
|
|
193 |
#' time_unit = "days"
|
|
194 |
#' )
|
|
195 |
#'
|
|
196 |
#' @export
|
|
197 |
h_tab_surv_one_biomarker <- function(df, |
|
198 |
vars,
|
|
199 |
time_unit,
|
|
200 |
.indent_mods = 0L) { |
|
201 | 6x |
afuns <- a_survival_subgroups()[vars] |
202 | 6x |
colvars <- d_survival_subgroups_colvars( |
203 | 6x |
vars,
|
204 | 6x |
conf_level = df$conf_level[1], |
205 | 6x |
method = df$pval_label[1], |
206 | 6x |
time_unit = time_unit |
207 |
)
|
|
208 | 6x |
h_tab_one_biomarker( |
209 | 6x |
df = df, |
210 | 6x |
afuns = afuns, |
211 | 6x |
colvars = colvars, |
212 | 6x |
.indent_mods = .indent_mods |
213 |
)
|
|
214 |
}
|
1 |
#' Occurrence Counts
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Functions for analyzing frequencies and fractions of occurrences for patients with occurrence
|
|
6 |
#' data. Primary analysis variables are the dictionary terms. All occurrences are counted for total
|
|
7 |
#' counts. Multiple occurrences within patient at the lowest term level displayed in the table are
|
|
8 |
#' counted only once.
|
|
9 |
#'
|
|
10 |
#' @inheritParams argument_convention
|
|
11 |
#'
|
|
12 |
#' @note By default, occurrences which don't appear in a given row split are dropped from the table and
|
|
13 |
#' the occurrences in the table are sorted alphabetically per row split. Therefore, the corresponding layout
|
|
14 |
#' needs to use `split_fun = drop_split_levels` in the `split_rows_by` calls. Use `drop = FALSE` if you would
|
|
15 |
#' like to show all occurrences.
|
|
16 |
#'
|
|
17 |
#' @name count_occurrences
|
|
18 |
NULL
|
|
19 | ||
20 |
#' @describeIn count_occurrences Statistics function which counts number of patients that report an
|
|
21 |
#' occurrence.
|
|
22 |
#'
|
|
23 |
#' @param denom (`string`)\cr choice of denominator for patient proportions. Can be:
|
|
24 |
#' - `N_col`: total number of patients in this column across rows
|
|
25 |
#' - `n`: number of patients with any occurrences
|
|
26 |
#'
|
|
27 |
#' @return
|
|
28 |
#' * `s_count_occurrences()` returns a list with:
|
|
29 |
#' * `count`: list of counts with one element per occurrence.
|
|
30 |
#' * `count_fraction`: list of counts and fractions with one element per occurrence.
|
|
31 |
#' * `fraction`: list of numerators and denominators with one element per occurrence.
|
|
32 |
#'
|
|
33 |
#' @examples
|
|
34 |
#' df <- data.frame(
|
|
35 |
#' USUBJID = as.character(c(1, 1, 2, 4, 4, 4)),
|
|
36 |
#' MHDECOD = c("MH1", "MH2", "MH1", "MH1", "MH1", "MH3")
|
|
37 |
#' )
|
|
38 |
#'
|
|
39 |
#' N_per_col <- 4L
|
|
40 |
#'
|
|
41 |
#' # Count unique occurrences per subject.
|
|
42 |
#' s_count_occurrences(
|
|
43 |
#' df,
|
|
44 |
#' .N_col = N_per_col,
|
|
45 |
#' .df_row = df,
|
|
46 |
#' .var = "MHDECOD",
|
|
47 |
#' id = "USUBJID"
|
|
48 |
#' )
|
|
49 |
#'
|
|
50 |
#' @export
|
|
51 |
s_count_occurrences <- function(df, |
|
52 |
denom = c("N_col", "n"), |
|
53 |
.N_col, # nolint |
|
54 |
.df_row,
|
|
55 |
drop = TRUE, |
|
56 |
.var = "MHDECOD", |
|
57 |
id = "USUBJID") { |
|
58 | 5x |
checkmate::assert_flag(drop) |
59 | 5x |
assert_df_with_variables(df, list(range = .var, id = id)) |
60 | 5x |
checkmate::assert_count(.N_col) |
61 | 5x |
checkmate::assert_multi_class(df[[.var]], classes = c("factor", "character")) |
62 | 5x |
checkmate::assert_multi_class(df[[id]], classes = c("factor", "character")) |
63 | 5x |
denom <- match.arg(denom) |
64 | ||
65 | 5x |
occurrences <- if (drop) { |
66 |
# Note that we don't try to preserve original level order here since a) that would required
|
|
67 |
# more time to look up in large original levels and b) that would fail for character input variable.
|
|
68 | 4x |
occurrence_levels <- sort(unique(.df_row[[.var]])) |
69 | 4x |
if (length(occurrence_levels) == 0) { |
70 | 1x |
stop( |
71 | 1x |
"no empty `.df_row` input allowed when `drop = TRUE`,",
|
72 | 1x |
" please use `split_fun = drop_split_levels` in the `rtables` `split_rows_by` calls"
|
73 |
)
|
|
74 |
}
|
|
75 | 3x |
factor(df[[.var]], levels = occurrence_levels) |
76 |
} else { |
|
77 | 1x |
df[[.var]] |
78 |
}
|
|
79 | 4x |
ids <- factor(df[[id]]) |
80 | 4x |
dn <- switch(denom, |
81 | 4x |
n = nlevels(ids), |
82 | 4x |
N_col = .N_col |
83 |
)
|
|
84 | 4x |
has_occurrence_per_id <- table(occurrences, ids) > 0 |
85 | 4x |
n_ids_per_occurrence <- as.list(rowSums(has_occurrence_per_id)) |
86 | 4x |
list( |
87 | 4x |
count = n_ids_per_occurrence, |
88 | 4x |
count_fraction = lapply( |
89 | 4x |
n_ids_per_occurrence,
|
90 | 4x |
function(i, denom) { |
91 | 13x |
if (i == 0 && denom == 0) { |
92 | ! |
c(0, 0) |
93 |
} else { |
|
94 | 13x |
c(i, i / denom) |
95 |
}
|
|
96 |
},
|
|
97 | 4x |
denom = dn |
98 |
),
|
|
99 | 4x |
fraction = lapply( |
100 | 4x |
n_ids_per_occurrence,
|
101 | 4x |
function(i, denom) c("num" = i, "denom" = denom), |
102 | 4x |
denom = dn |
103 |
)
|
|
104 |
)
|
|
105 |
}
|
|
106 | ||
107 |
#' @describeIn count_occurrences Formatted analysis function which is used as `afun`
|
|
108 |
#' in `count_occurrences()`.
|
|
109 |
#'
|
|
110 |
#' @return
|
|
111 |
#' * `a_count_occurrences()` returns the corresponding list with formatted [rtables::CellValue()].
|
|
112 |
#'
|
|
113 |
#' @examples
|
|
114 |
#' # We need to ungroup `count_fraction` first so that the `rtables` formatting
|
|
115 |
#' # function `format_count_fraction()` can be applied correctly.
|
|
116 |
#' afun <- make_afun(a_count_occurrences, .ungroup_stats = c("count", "count_fraction", "fraction"))
|
|
117 |
#' afun(
|
|
118 |
#' df,
|
|
119 |
#' .N_col = N_per_col,
|
|
120 |
#' .df_row = df,
|
|
121 |
#' .var = "MHDECOD",
|
|
122 |
#' id = "USUBJID"
|
|
123 |
#' )
|
|
124 |
#'
|
|
125 |
#' @export
|
|
126 |
a_count_occurrences <- make_afun( |
|
127 |
s_count_occurrences,
|
|
128 |
.formats = c(count = "xx", count_fraction = format_count_fraction_fixed_dp, fraction = format_fraction_fixed_dp) |
|
129 |
)
|
|
130 | ||
131 |
#' @describeIn count_occurrences Layout-creating function which can take statistics function arguments
|
|
132 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()].
|
|
133 |
#'
|
|
134 |
#' @return
|
|
135 |
#' * `count_occurrences()` returns a layout object suitable for passing to further layouting functions,
|
|
136 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing
|
|
137 |
#' the statistics from `s_count_occurrences()` to the table layout.
|
|
138 |
#'
|
|
139 |
#' @examples
|
|
140 |
#' library(dplyr)
|
|
141 |
#' df <- data.frame(
|
|
142 |
#' USUBJID = as.character(c(
|
|
143 |
#' 1, 1, 2, 4, 4, 4,
|
|
144 |
#' 6, 6, 6, 7, 7, 8
|
|
145 |
#' )),
|
|
146 |
#' MHDECOD = c(
|
|
147 |
#' "MH1", "MH2", "MH1", "MH1", "MH1", "MH3",
|
|
148 |
#' "MH2", "MH2", "MH3", "MH1", "MH2", "MH4"
|
|
149 |
#' ),
|
|
150 |
#' ARM = rep(c("A", "B"), each = 6)
|
|
151 |
#' )
|
|
152 |
#' df_adsl <- df %>%
|
|
153 |
#' select(USUBJID, ARM) %>%
|
|
154 |
#' unique()
|
|
155 |
#'
|
|
156 |
#' # Create table layout
|
|
157 |
#' lyt <- basic_table() %>%
|
|
158 |
#' split_cols_by("ARM") %>%
|
|
159 |
#' add_colcounts() %>%
|
|
160 |
#' count_occurrences(vars = "MHDECOD", .stats = c("count_fraction"))
|
|
161 |
#'
|
|
162 |
#' # Apply table layout to data and produce `rtable` object
|
|
163 |
#' lyt %>%
|
|
164 |
#' build_table(df, alt_counts_df = df_adsl) %>%
|
|
165 |
#' prune_table()
|
|
166 |
#'
|
|
167 |
#' @export
|
|
168 |
count_occurrences <- function(lyt, |
|
169 |
vars,
|
|
170 |
var_labels = vars, |
|
171 |
show_labels = "hidden", |
|
172 |
...,
|
|
173 |
table_names = vars, |
|
174 |
.stats = "count_fraction", |
|
175 |
.formats = NULL, |
|
176 |
.labels = NULL, |
|
177 |
.indent_mods = NULL) { |
|
178 | 6x |
afun <- make_afun( |
179 | 6x |
a_count_occurrences,
|
180 | 6x |
.stats = .stats, |
181 | 6x |
.formats = .formats, |
182 | 6x |
.labels = .labels, |
183 | 6x |
.indent_mods = .indent_mods, |
184 | 6x |
.ungroup_stats = .stats |
185 |
)
|
|
186 | ||
187 | 6x |
analyze( |
188 | 6x |
lyt = lyt, |
189 | 6x |
vars = vars, |
190 | 6x |
afun = afun, |
191 | 6x |
var_labels = var_labels, |
192 | 6x |
show_labels = show_labels, |
193 | 6x |
table_names = table_names, |
194 | 6x |
extra_args = list(...) |
195 |
)
|
|
196 |
}
|
1 |
#' Create a STEP Graph
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Based on the STEP results, creates a `ggplot` graph showing the estimated HR or OR
|
|
6 |
#' along the continuous biomarker value subgroups.
|
|
7 |
#'
|
|
8 |
#' @param df (`tibble`)\cr result of [tidy.step()].
|
|
9 |
#' @param use_percentile (`flag`)\cr whether to use percentiles for the x axis or actual
|
|
10 |
#' biomarker values.
|
|
11 |
#' @param est (named `list`)\cr `col` and `lty` settings for estimate line.
|
|
12 |
#' @param ci_ribbon (named `list` or `NULL`)\cr `fill` and `alpha` settings for the confidence interval
|
|
13 |
#' ribbon area, or `NULL` to not plot a CI ribbon.
|
|
14 |
#' @param col (`character`)\cr colors.
|
|
15 |
#'
|
|
16 |
#' @return A `ggplot` STEP graph.
|
|
17 |
#'
|
|
18 |
#' @seealso Custom tidy method [tidy.step()].
|
|
19 |
#'
|
|
20 |
#' @examples
|
|
21 |
#' library(nestcolor)
|
|
22 |
#' library(survival)
|
|
23 |
#' lung$sex <- factor(lung$sex)
|
|
24 |
#'
|
|
25 |
#' # Survival example.
|
|
26 |
#' vars <- list(
|
|
27 |
#' time = "time",
|
|
28 |
#' event = "status",
|
|
29 |
#' arm = "sex",
|
|
30 |
#' biomarker = "age"
|
|
31 |
#' )
|
|
32 |
#'
|
|
33 |
#' step_matrix <- fit_survival_step(
|
|
34 |
#' variables = vars,
|
|
35 |
#' data = lung,
|
|
36 |
#' control = c(control_coxph(), control_step(num_points = 10, degree = 2))
|
|
37 |
#' )
|
|
38 |
#' step_data <- broom::tidy(step_matrix)
|
|
39 |
#'
|
|
40 |
#' # Default plot.
|
|
41 |
#' g_step(step_data)
|
|
42 |
#'
|
|
43 |
#' # Add the reference 1 horizontal line.
|
|
44 |
#' library(ggplot2)
|
|
45 |
#' g_step(step_data) +
|
|
46 |
#' ggplot2::geom_hline(ggplot2::aes(yintercept = 1), linetype = 2)
|
|
47 |
#'
|
|
48 |
#' # Use actual values instead of percentiles, different color for estimate and no CI,
|
|
49 |
#' # use log scale for y axis.
|
|
50 |
#' g_step(
|
|
51 |
#' step_data,
|
|
52 |
#' use_percentile = FALSE,
|
|
53 |
#' est = list(col = "blue", lty = 1),
|
|
54 |
#' ci_ribbon = NULL
|
|
55 |
#' ) + scale_y_log10()
|
|
56 |
#'
|
|
57 |
#' # Adding another curve based on additional column.
|
|
58 |
#' step_data$extra <- exp(step_data$`Percentile Center`)
|
|
59 |
#' g_step(step_data) +
|
|
60 |
#' ggplot2::geom_line(ggplot2::aes(y = extra), linetype = 2, color = "green")
|
|
61 |
#'
|
|
62 |
#' # Response example.
|
|
63 |
#' vars <- list(
|
|
64 |
#' response = "status",
|
|
65 |
#' arm = "sex",
|
|
66 |
#' biomarker = "age"
|
|
67 |
#' )
|
|
68 |
#'
|
|
69 |
#' step_matrix <- fit_rsp_step(
|
|
70 |
#' variables = vars,
|
|
71 |
#' data = lung,
|
|
72 |
#' control = c(
|
|
73 |
#' control_logistic(response_definition = "I(response == 2)"),
|
|
74 |
#' control_step()
|
|
75 |
#' )
|
|
76 |
#' )
|
|
77 |
#' step_data <- broom::tidy(step_matrix)
|
|
78 |
#' g_step(step_data)
|
|
79 |
#'
|
|
80 |
#' @export
|
|
81 |
g_step <- function(df, |
|
82 |
use_percentile = "Percentile Center" %in% names(df), |
|
83 |
est = list(col = "blue", lty = 1), |
|
84 |
ci_ribbon = list(fill = getOption("ggplot2.discrete.colour")[1], alpha = 0.5), |
|
85 |
col = getOption("ggplot2.discrete.colour")) { |
|
86 | 2x |
checkmate::assert_tibble(df) |
87 | 2x |
checkmate::assert_flag(use_percentile) |
88 | 2x |
checkmate::assert_character(col, null.ok = TRUE) |
89 | 2x |
checkmate::assert_list(est, names = "named") |
90 | 2x |
checkmate::assert_list(ci_ribbon, names = "named", null.ok = TRUE) |
91 | ||
92 | 2x |
x_var <- ifelse(use_percentile, "Percentile Center", "Interval Center") |
93 | 2x |
df$x <- df[[x_var]] |
94 | 2x |
attrs <- attributes(df) |
95 | 2x |
df$y <- df[[attrs$estimate]] |
96 | ||
97 |
# Set legend names. To be modified also at call level
|
|
98 | 2x |
legend_names <- c("Estimate", "CI 95%") |
99 | ||
100 | 2x |
p <- ggplot2::ggplot(df, ggplot2::aes(x = .data[["x"]], y = .data[["y"]])) |
101 | ||
102 | 2x |
if (!is.null(col)) { |
103 | 2x |
p <- p + |
104 | 2x |
ggplot2::scale_color_manual(values = col) |
105 |
}
|
|
106 | ||
107 | 2x |
if (!is.null(ci_ribbon)) { |
108 | 1x |
if (is.null(ci_ribbon$fill)) { |
109 | ! |
ci_ribbon$fill <- "lightblue" |
110 |
}
|
|
111 | 1x |
p <- p + ggplot2::geom_ribbon( |
112 | 1x |
ggplot2::aes( |
113 | 1x |
ymin = .data[["ci_lower"]], ymax = .data[["ci_upper"]], |
114 | 1x |
fill = legend_names[2] |
115 |
),
|
|
116 | 1x |
alpha = ci_ribbon$alpha |
117 |
) + |
|
118 | 1x |
scale_fill_manual( |
119 | 1x |
name = "", values = c("CI 95%" = ci_ribbon$fill) |
120 |
)
|
|
121 |
}
|
|
122 | 2x |
suppressMessages(p <- p + |
123 | 2x |
ggplot2::geom_line( |
124 | 2x |
ggplot2::aes(y = .data[["y"]], color = legend_names[1]), |
125 | 2x |
linetype = est$lty |
126 |
) + |
|
127 | 2x |
scale_colour_manual( |
128 | 2x |
name = "", values = c("Estimate" = "blue") |
129 |
)) |
|
130 | ||
131 | 2x |
p <- p + ggplot2::labs(x = attrs$biomarker, y = attrs$estimate) |
132 | 2x |
if (use_percentile) { |
133 | 1x |
p <- p + ggplot2::scale_x_continuous(labels = scales::percent) |
134 |
}
|
|
135 | 2x |
p
|
136 |
}
|
|
137 | ||
138 |
#' Custom Tidy Method for STEP Results
|
|
139 |
#'
|
|
140 |
#' @description `r lifecycle::badge("stable")`
|
|
141 |
#'
|
|
142 |
#' Tidy the STEP results into a `tibble` format ready for plotting.
|
|
143 |
#'
|
|
144 |
#' @param x (`step` matrix)\cr results from [fit_survival_step()].
|
|
145 |
#' @param ... not used here.
|
|
146 |
#'
|
|
147 |
#' @return A `tibble` with one row per STEP subgroup. The estimates and CIs are on the HR or OR scale,
|
|
148 |
#' respectively. Additional attributes carry metadata also used for plotting.
|
|
149 |
#'
|
|
150 |
#' @seealso [g_step()] which consumes the result from this function.
|
|
151 |
#'
|
|
152 |
#' @method tidy step
|
|
153 |
#'
|
|
154 |
#' @examples
|
|
155 |
#' library(survival)
|
|
156 |
#' lung$sex <- factor(lung$sex)
|
|
157 |
#' vars <- list(
|
|
158 |
#' time = "time",
|
|
159 |
#' event = "status",
|
|
160 |
#' arm = "sex",
|
|
161 |
#' biomarker = "age"
|
|
162 |
#' )
|
|
163 |
#' step_matrix <- fit_survival_step(
|
|
164 |
#' variables = vars,
|
|
165 |
#' data = lung,
|
|
166 |
#' control = c(control_coxph(), control_step(num_points = 10, degree = 2))
|
|
167 |
#' )
|
|
168 |
#' broom::tidy(step_matrix)
|
|
169 |
#'
|
|
170 |
#' @export
|
|
171 |
tidy.step <- function(x, ...) { # nolint |
|
172 | 7x |
checkmate::assert_class(x, "step") |
173 | 7x |
dat <- as.data.frame(x) |
174 | 7x |
nams <- names(dat) |
175 | 7x |
is_surv <- "loghr" %in% names(dat) |
176 | 7x |
est_var <- ifelse(is_surv, "loghr", "logor") |
177 | 7x |
new_est_var <- ifelse(is_surv, "Hazard Ratio", "Odds Ratio") |
178 | 7x |
new_y_vars <- c(new_est_var, c("ci_lower", "ci_upper")) |
179 | 7x |
names(dat)[match(est_var, nams)] <- new_est_var |
180 | 7x |
dat[, new_y_vars] <- exp(dat[, new_y_vars]) |
181 | 7x |
any_is_na <- any(is.na(dat[, new_y_vars])) |
182 | 7x |
any_is_very_large <- any(abs(dat[, new_y_vars]) > 1e10, na.rm = TRUE) |
183 | 7x |
if (any_is_na) { |
184 | 2x |
warning(paste( |
185 | 2x |
"Missing values in the point estimate or CI columns,",
|
186 | 2x |
"this will lead to holes in the `g_step()` plot"
|
187 |
)) |
|
188 |
}
|
|
189 | 7x |
if (any_is_very_large) { |
190 | 2x |
warning(paste( |
191 | 2x |
"Very large absolute values in the point estimate or CI columns,",
|
192 | 2x |
"consider adding `scale_y_log10()` to the `g_step()` result for plotting"
|
193 |
)) |
|
194 |
}
|
|
195 | 7x |
if (any_is_na || any_is_very_large) { |
196 | 4x |
warning("Consider using larger `bandwidth`, less `num_points` in `control_step()` settings for fitting") |
197 |
}
|
|
198 | 7x |
structure( |
199 | 7x |
tibble::as_tibble(dat), |
200 | 7x |
estimate = new_est_var, |
201 | 7x |
biomarker = attr(x, "variables")$biomarker, |
202 | 7x |
ci = f_conf_level(attr(x, "control")$conf_level) |
203 |
)
|
|
204 |
}
|
1 |
#' Convert Table into Matrix of Strings
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Helper function to use mostly within tests. `with_spaces`parameter allows
|
|
6 |
#' to test not only for content but also indentation and table structure.
|
|
7 |
#' `print_txt_to_copy` instead facilitate the testing development by returning a well
|
|
8 |
#' formatted text that needs only to be copied and pasted in the expected output.
|
|
9 |
#'
|
|
10 |
#' @param x `rtables` table.
|
|
11 |
#' @param with_spaces Should the tested table keep the indentation and other relevant spaces?
|
|
12 |
#' @param print_txt_to_copy Utility to have a way to copy the input table directly
|
|
13 |
#' into the expected variable instead of copying it too manually.
|
|
14 |
#'
|
|
15 |
#' @return A `matrix` of `string`s.
|
|
16 |
#'
|
|
17 |
#' @export
|
|
18 |
to_string_matrix <- function(x, with_spaces = FALSE, print_txt_to_copy = FALSE) { |
|
19 | 5x |
checkmate::assert_flag(with_spaces) |
20 | 5x |
checkmate::assert_flag(print_txt_to_copy) |
21 | ||
22 |
# Producing the matrix to test
|
|
23 | 5x |
if (with_spaces) { |
24 | ! |
out <- strsplit(toString(matrix_form(x, TRUE)), "\\n")[[1]] |
25 |
} else { |
|
26 | 5x |
out <- matrix_form(x)$string |
27 |
}
|
|
28 | ||
29 |
# Printing to console formatted output that needs to be copied in "expected"
|
|
30 | 5x |
if (print_txt_to_copy) { |
31 | ! |
out_tmp <- out |
32 | ! |
if (!with_spaces) { |
33 | ! |
out_tmp <- apply(out, 1, paste0, collapse = '", "') |
34 |
}
|
|
35 | ! |
cat(paste0('c(\n "', paste0(out_tmp, collapse = '",\n "'), '"\n)')) |
36 |
}
|
|
37 | ||
38 |
# Return values
|
|
39 | 5x |
return(out) |
40 |
}
|
|
41 | ||
42 |
#' Blank for Missing Input
|
|
43 |
#'
|
|
44 |
#' Helper function to use in tabulating model results.
|
|
45 |
#'
|
|
46 |
#' @param x (`vector`)\cr input for a cell.
|
|
47 |
#'
|
|
48 |
#' @return An empty `character` vector if all entries in `x` are missing (`NA`), otherwise
|
|
49 |
#' the unlisted version of `x`.
|
|
50 |
#'
|
|
51 |
#' @keywords internal
|
|
52 |
unlist_and_blank_na <- function(x) { |
|
53 | 267x |
unl <- unlist(x) |
54 | 267x |
if (all(is.na(unl))) { |
55 | 161x |
character() |
56 |
} else { |
|
57 | 106x |
unl
|
58 |
}
|
|
59 |
}
|
|
60 | ||
61 |
#' Constructor for Content Functions given Data Frame with Flag Input
|
|
62 |
#'
|
|
63 |
#' This can be useful for tabulating model results.
|
|
64 |
#'
|
|
65 |
#' @param analysis_var (`string`)\cr variable name for the column containing values to be returned by the
|
|
66 |
#' content function.
|
|
67 |
#' @param flag_var (`string`)\cr variable name for the logical column identifying which row should be returned.
|
|
68 |
#' @param format (`string`)\cr `rtables` format to use.
|
|
69 |
#'
|
|
70 |
#' @return A content function which gives `df$analysis_var` at the row identified by
|
|
71 |
#' `.df_row$flag` in the given format.
|
|
72 |
#'
|
|
73 |
#' @keywords internal
|
|
74 |
cfun_by_flag <- function(analysis_var, |
|
75 |
flag_var,
|
|
76 |
format = "xx", |
|
77 |
.indent_mods = NULL) { |
|
78 | 61x |
checkmate::assert_string(analysis_var) |
79 | 61x |
checkmate::assert_string(flag_var) |
80 | 61x |
function(df, labelstr) { |
81 | 265x |
row_index <- which(df[[flag_var]]) |
82 | 265x |
x <- unlist_and_blank_na(df[[analysis_var]][row_index]) |
83 | 265x |
formatters::with_label( |
84 | 265x |
rcell(x, format = format, indent_mod = .indent_mods), |
85 | 265x |
labelstr
|
86 |
)
|
|
87 |
}
|
|
88 |
}
|
|
89 | ||
90 |
#' Content Row Function to Add Row Total to Labels
|
|
91 |
#'
|
|
92 |
#' This takes the label of the latest row split level and adds the row total in parentheses.
|
|
93 |
#'
|
|
94 |
#' @inheritParams argument_convention
|
|
95 |
#'
|
|
96 |
#' @return A `list` containing "row_count" with the row count value and the correct label.
|
|
97 |
#'
|
|
98 |
#' @note It is important here to not use `df` but rather `.N_row` in the implementation, because
|
|
99 |
#' the former is already split by columns and will refer to the first column of the data only.
|
|
100 |
#'
|
|
101 |
#' @keywords internal
|
|
102 |
c_label_n <- function(df, |
|
103 |
labelstr,
|
|
104 |
.N_row) { # nolint |
|
105 | 270x |
label <- paste0(labelstr, " (N=", .N_row, ")") |
106 | 270x |
list(row_count = formatters::with_label(c(.N_row, .N_row), label)) |
107 |
}
|
|
108 | ||
109 |
#' Layout Creating Function to Add Row Total Counts
|
|
110 |
#'
|
|
111 |
#' @description `r lifecycle::badge("stable")`
|
|
112 |
#'
|
|
113 |
#' This works analogously to [rtables::add_colcounts()] but on the rows. This function
|
|
114 |
#' is a wrapper for [rtables::summarize_row_groups()].
|
|
115 |
#'
|
|
116 |
#' @inheritParams argument_convention
|
|
117 |
#'
|
|
118 |
#' @return A modified layout where the latest row split labels now have the row-wise
|
|
119 |
#' total counts (i.e. without column-based subsetting) attached in parentheses.
|
|
120 |
#'
|
|
121 |
#' @note Row count values are contained in these row count rows but are not displayed
|
|
122 |
#' so that they are not considered zero rows by default when pruning.
|
|
123 |
#'
|
|
124 |
#' @examples
|
|
125 |
#' basic_table() %>%
|
|
126 |
#' split_cols_by("ARM") %>%
|
|
127 |
#' add_colcounts() %>%
|
|
128 |
#' split_rows_by("RACE", split_fun = drop_split_levels) %>%
|
|
129 |
#' add_rowcounts() %>%
|
|
130 |
#' analyze("AGE", afun = list_wrap_x(summary), format = "xx.xx") %>%
|
|
131 |
#' build_table(DM)
|
|
132 |
#'
|
|
133 |
#' @export
|
|
134 |
add_rowcounts <- function(lyt) { |
|
135 | 5x |
c_lbl_n_fun <- make_afun( |
136 | 5x |
c_label_n,
|
137 | 5x |
.stats = c("row_count"), |
138 | 5x |
.formats = c(row_count = function(x, ...) "") |
139 |
)
|
|
140 | 5x |
summarize_row_groups( |
141 | 5x |
lyt,
|
142 | 5x |
cfun = c_lbl_n_fun |
143 |
)
|
|
144 |
}
|
|
145 | ||
146 |
#' Obtain Column Indices
|
|
147 |
#'
|
|
148 |
#' @description `r lifecycle::badge("stable")`
|
|
149 |
#'
|
|
150 |
#' Helper function to extract column indices from a `VTableTree` for a given
|
|
151 |
#' vector of column names.
|
|
152 |
#'
|
|
153 |
#' @param table_tree (`VTableTree`)\cr table to extract the indices from.
|
|
154 |
#' @param col_names (`character`)\cr vector of column names.
|
|
155 |
#'
|
|
156 |
#' @return A vector of column indices.
|
|
157 |
#'
|
|
158 |
#' @export
|
|
159 |
h_col_indices <- function(table_tree, col_names) { |
|
160 | 1232x |
checkmate::assert_class(table_tree, "VTableNodeInfo") |
161 | 1232x |
checkmate::assert_subset(col_names, names(attr(col_info(table_tree), "cextra_args")), empty.ok = FALSE) |
162 | 1232x |
match(col_names, names(attr(col_info(table_tree), "cextra_args"))) |
163 |
}
|
|
164 | ||
165 |
#' Labels or Names of List Elements
|
|
166 |
#'
|
|
167 |
#' Internal helper function for working with nested statistic function results which typically
|
|
168 |
#' don't have labels but names that we can use.
|
|
169 |
#'
|
|
170 |
#' @param x a list
|
|
171 |
#'
|
|
172 |
#' @return A `character` vector with the labels or names for the list elements.
|
|
173 |
#'
|
|
174 |
#' @keywords internal
|
|
175 |
labels_or_names <- function(x) { |
|
176 | 119x |
checkmate::assert_multi_class(x, c("data.frame", "list")) |
177 | 119x |
labs <- sapply(x, obj_label) |
178 | 119x |
nams <- rlang::names2(x) |
179 | 119x |
label_is_null <- sapply(labs, is.null) |
180 | 119x |
result <- unlist(ifelse(label_is_null, nams, labs)) |
181 | 119x |
return(result) |
182 |
}
|
|
183 | ||
184 |
#' Convert to `rtable`
|
|
185 |
#'
|
|
186 |
#' @description `r lifecycle::badge("stable")`
|
|
187 |
#'
|
|
188 |
#' This is a new generic function to convert objects to `rtable` tables.
|
|
189 |
#'
|
|
190 |
#' @param x the object which should be converted to an `rtable`.
|
|
191 |
#' @param ... additional arguments for methods.
|
|
192 |
#'
|
|
193 |
#' @return An `rtables` table object. Note that the concrete class will depend on the method used.
|
|
194 |
#'
|
|
195 |
#' @export
|
|
196 |
as.rtable <- function(x, ...) { # nolint |
|
197 | 3x |
UseMethod("as.rtable", x) |
198 |
}
|
|
199 | ||
200 |
#' @describeIn as.rtable method for converting `data.frame` that contain numeric columns to `rtable`.
|
|
201 |
#'
|
|
202 |
#' @param format the format which should be used for the columns.
|
|
203 |
#'
|
|
204 |
#' @method as.rtable data.frame
|
|
205 |
#'
|
|
206 |
#' @examples
|
|
207 |
#' x <- data.frame(
|
|
208 |
#' a = 1:10,
|
|
209 |
#' b = rnorm(10)
|
|
210 |
#' )
|
|
211 |
#' as.rtable(x)
|
|
212 |
#'
|
|
213 |
#' @export
|
|
214 |
as.rtable.data.frame <- function(x, format = "xx.xx", ...) { |
|
215 | 3x |
checkmate::assert_numeric(unlist(x)) |
216 | 2x |
do.call( |
217 | 2x |
rtable,
|
218 | 2x |
c( |
219 | 2x |
list( |
220 | 2x |
header = labels_or_names(x), |
221 | 2x |
format = format |
222 |
),
|
|
223 | 2x |
Map( |
224 | 2x |
function(row, row_name) { |
225 | 20x |
do.call( |
226 | 20x |
rrow,
|
227 | 20x |
c(as.list(unname(row)), |
228 | 20x |
row.name = row_name |
229 |
)
|
|
230 |
)
|
|
231 |
},
|
|
232 | 2x |
row = as.data.frame(t(x)), |
233 | 2x |
row_name = rownames(x) |
234 |
)
|
|
235 |
)
|
|
236 |
)
|
|
237 |
}
|
|
238 | ||
239 |
#' Split parameters
|
|
240 |
#'
|
|
241 |
#' @description `r lifecycle::badge("stable")`
|
|
242 |
#'
|
|
243 |
#' It divides the data in the vector `param` into the groups defined by `f` based on specified `values`. It is relevant
|
|
244 |
#' in `rtables` layers so as to distribute parameters `.stats` or' `.formats` into lists with items corresponding to
|
|
245 |
#' specific analysis function.
|
|
246 |
#'
|
|
247 |
#' @param param (`vector`)\cr the parameter to be split.
|
|
248 |
#' @param value (`vector`)\cr the value used to split.
|
|
249 |
#' @param f (`list` of `vectors`)\cr the reference to make the split
|
|
250 |
#'
|
|
251 |
#' @return A named `list` with the same element names as `f`, each containing the elements specified in `.stats`.
|
|
252 |
#'
|
|
253 |
#' @examples
|
|
254 |
#' f <- list(
|
|
255 |
#' surv = c("pt_at_risk", "event_free_rate", "rate_se", "rate_ci"),
|
|
256 |
#' surv_diff = c("rate_diff", "rate_diff_ci", "ztest_pval")
|
|
257 |
#' )
|
|
258 |
#'
|
|
259 |
#' .stats <- c("pt_at_risk", "rate_diff")
|
|
260 |
#' h_split_param(.stats, .stats, f = f)
|
|
261 |
#'
|
|
262 |
#' # $surv
|
|
263 |
#' # [1] "pt_at_risk"
|
|
264 |
#' #
|
|
265 |
#' # $surv_diff
|
|
266 |
#' # [1] "rate_diff"
|
|
267 |
#'
|
|
268 |
#' .formats <- c("pt_at_risk" = "xx", "event_free_rate" = "xxx")
|
|
269 |
#' h_split_param(.formats, names(.formats), f = f)
|
|
270 |
#'
|
|
271 |
#' # $surv
|
|
272 |
#' # pt_at_risk event_free_rate
|
|
273 |
#' # "xx" "xxx"
|
|
274 |
#' #
|
|
275 |
#' # $surv_diff
|
|
276 |
#' # NULL
|
|
277 |
#'
|
|
278 |
#' @export
|
|
279 |
h_split_param <- function(param, |
|
280 |
value,
|
|
281 |
f) { |
|
282 | 21x |
y <- lapply(f, function(x) param[value %in% x]) |
283 | 21x |
lapply(y, function(x) if (length(x) == 0) NULL else x) |
284 |
}
|
|
285 | ||
286 |
#' Get Selected Statistics Names
|
|
287 |
#'
|
|
288 |
#' Helper function to be used for creating `afun`.
|
|
289 |
#'
|
|
290 |
#' @param .stats (`vector` or `NULL`)\cr input to the layout creating function. Note that `NULL` means
|
|
291 |
#' in this context that all default statistics should be used.
|
|
292 |
#' @param all_stats (`character`)\cr all statistics which can be selected here potentially.
|
|
293 |
#'
|
|
294 |
#' @return A `character` vector with the selected statistics.
|
|
295 |
#'
|
|
296 |
#' @keywords internal
|
|
297 |
afun_selected_stats <- function(.stats, all_stats) { |
|
298 | 857x |
checkmate::assert_character(.stats, null.ok = TRUE) |
299 | 857x |
checkmate::assert_character(all_stats) |
300 | 857x |
if (is.null(.stats)) { |
301 | 1x |
all_stats
|
302 |
} else { |
|
303 | 856x |
intersect(.stats, all_stats) |
304 |
}
|
|
305 |
}
|
|
306 | ||
307 |
#' Add Variable Labels to Top Left Corner in Table
|
|
308 |
#'
|
|
309 |
#' @description `r lifecycle::badge("stable")`
|
|
310 |
#'
|
|
311 |
#' Helper layout creating function to just append the variable labels of a given variables vector
|
|
312 |
#' from a given dataset in the top left corner. If a variable label is not found then the
|
|
313 |
#' variable name itself is used instead. Multiple variable labels are concatenated with slashes.
|
|
314 |
#'
|
|
315 |
#' @inheritParams argument_convention
|
|
316 |
#' @param vars (`character`)\cr variable names of which the labels are to be looked up in `df`.
|
|
317 |
#' @param indent (`integer`)\cr non-negative number of nested indent space, default to 0L which means no indent.
|
|
318 |
#' 1L means two spaces indent, 2L means four spaces indent and so on.
|
|
319 |
#'
|
|
320 |
#' @return A modified layout with the new variable label(s) added to the top-left material.
|
|
321 |
#'
|
|
322 |
#' @note This is not an optimal implementation of course, since we are using here the data set
|
|
323 |
#' itself during the layout creation. When we have a more mature `rtables` implementation then
|
|
324 |
#' this will also be improved or not necessary anymore.
|
|
325 |
#'
|
|
326 |
#' @examples
|
|
327 |
#' lyt <- basic_table() %>%
|
|
328 |
#' split_cols_by("ARM") %>%
|
|
329 |
#' add_colcounts() %>%
|
|
330 |
#' split_rows_by("SEX") %>%
|
|
331 |
#' append_varlabels(DM, "SEX") %>%
|
|
332 |
#' analyze("AGE", afun = mean) %>%
|
|
333 |
#' append_varlabels(DM, "AGE", indent = 1)
|
|
334 |
#' build_table(lyt, DM)
|
|
335 |
#'
|
|
336 |
#' lyt <- basic_table() %>%
|
|
337 |
#' split_cols_by("ARM") %>%
|
|
338 |
#' split_rows_by("SEX") %>%
|
|
339 |
#' analyze("AGE", afun = mean) %>%
|
|
340 |
#' append_varlabels(DM, c("SEX", "AGE"))
|
|
341 |
#' build_table(lyt, DM)
|
|
342 |
#'
|
|
343 |
#' @export
|
|
344 |
append_varlabels <- function(lyt, df, vars, indent = 0L) { |
|
345 | 3x |
if (checkmate::test_flag(indent)) { |
346 | ! |
warning("indent argument is now accepting integers. Boolean indent will be converted to integers.") |
347 | ! |
indent <- as.integer(indent) |
348 |
}
|
|
349 | ||
350 | 3x |
checkmate::assert_data_frame(df) |
351 | 3x |
checkmate::assert_character(vars) |
352 | 3x |
checkmate::assert_count(indent) |
353 | ||
354 | 3x |
lab <- formatters::var_labels(df[vars], fill = TRUE) |
355 | 3x |
lab <- paste(lab, collapse = " / ") |
356 | 3x |
space <- paste(rep(" ", indent * 2), collapse = "") |
357 | 3x |
lab <- paste0(space, lab) |
358 | ||
359 | 3x |
append_topleft(lyt, lab) |
360 |
}
|
1 |
#' Tabulate Biomarker Effects on Binary Response by Subgroup
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Tabulate the estimated effects of multiple continuous biomarker variables
|
|
6 |
#' on a binary response endpoint across population subgroups.
|
|
7 |
#'
|
|
8 |
#' @inheritParams argument_convention
|
|
9 |
#' @param df (`data.frame`)\cr containing all analysis variables, as returned by
|
|
10 |
#' [extract_rsp_biomarkers()].
|
|
11 |
#' @param vars (`character`)\cr the names of statistics to be reported among:
|
|
12 |
#' * `n_tot`: Total number of patients per group.
|
|
13 |
#' * `n_rsp`: Total number of responses per group.
|
|
14 |
#' * `prop`: Total response proportion per group.
|
|
15 |
#' * `or`: Odds ratio.
|
|
16 |
#' * `ci`: Confidence interval of odds ratio.
|
|
17 |
#' * `pval`: p-value of the effect.
|
|
18 |
#' Note, the statistics `n_tot`, `or` and `ci` are required.
|
|
19 |
#'
|
|
20 |
#' @return An `rtables` table summarizing biomarker effects on binary response by subgroup.
|
|
21 |
#'
|
|
22 |
#' @details These functions create a layout starting from a data frame which contains
|
|
23 |
#' the required statistics. The tables are then typically used as input for forest plots.
|
|
24 |
#'
|
|
25 |
#' @note In contrast to [tabulate_rsp_subgroups()] this tabulation function does
|
|
26 |
#' not start from an input layout `lyt`. This is because internally the table is
|
|
27 |
#' created by combining multiple subtables.
|
|
28 |
#'
|
|
29 |
#' @seealso [h_tab_rsp_one_biomarker()] which is used internally, [extract_rsp_biomarkers()].
|
|
30 |
#'
|
|
31 |
#' @examples
|
|
32 |
#' library(dplyr)
|
|
33 |
#' library(forcats)
|
|
34 |
#'
|
|
35 |
#' adrs <- tern_ex_adrs
|
|
36 |
#' adrs_labels <- formatters::var_labels(adrs)
|
|
37 |
#'
|
|
38 |
#' adrs_f <- adrs %>%
|
|
39 |
#' filter(PARAMCD == "BESRSPI") %>%
|
|
40 |
#' mutate(rsp = AVALC == "CR")
|
|
41 |
#' formatters::var_labels(adrs_f) <- c(adrs_labels, "Response")
|
|
42 |
#'
|
|
43 |
#' df <- extract_rsp_biomarkers(
|
|
44 |
#' variables = list(
|
|
45 |
#' rsp = "rsp",
|
|
46 |
#' biomarkers = c("BMRKR1", "AGE"),
|
|
47 |
#' covariates = "SEX",
|
|
48 |
#' subgroups = "BMRKR2"
|
|
49 |
#' ),
|
|
50 |
#' data = adrs_f
|
|
51 |
#' )
|
|
52 |
#'
|
|
53 |
#' \donttest{
|
|
54 |
#' ## Table with default columns.
|
|
55 |
#' tabulate_rsp_biomarkers(df)
|
|
56 |
#'
|
|
57 |
#' ## Table with a manually chosen set of columns: leave out "pval", reorder.
|
|
58 |
#' tab <- tabulate_rsp_biomarkers(
|
|
59 |
#' df = df,
|
|
60 |
#' vars = c("n_rsp", "ci", "n_tot", "prop", "or")
|
|
61 |
#' )
|
|
62 |
#'
|
|
63 |
#' ## Finally produce the forest plot.
|
|
64 |
#' g_forest(tab, xlim = c(0.7, 1.4))
|
|
65 |
#' }
|
|
66 |
#'
|
|
67 |
#' @export
|
|
68 |
#' @name response_biomarkers_subgroups
|
|
69 |
tabulate_rsp_biomarkers <- function(df, |
|
70 |
vars = c("n_tot", "n_rsp", "prop", "or", "ci", "pval"), |
|
71 |
.indent_mods = 0L) { |
|
72 | 3x |
checkmate::assert_data_frame(df) |
73 | 3x |
checkmate::assert_character(df$biomarker) |
74 | 3x |
checkmate::assert_character(df$biomarker_label) |
75 | 3x |
checkmate::assert_subset(vars, c("n_tot", "n_rsp", "prop", "or", "ci", "pval")) |
76 | ||
77 | 3x |
df_subs <- split(df, f = df$biomarker) |
78 | 3x |
tabs <- lapply(df_subs, FUN = function(df_sub) { |
79 | 5x |
tab_sub <- h_tab_rsp_one_biomarker( |
80 | 5x |
df = df_sub, |
81 | 5x |
vars = vars, |
82 | 5x |
.indent_mods = .indent_mods |
83 |
)
|
|
84 |
# Insert label row as first row in table.
|
|
85 | 5x |
label_at_path(tab_sub, path = row_paths(tab_sub)[[1]][1]) <- df_sub$biomarker_label[1] |
86 | 5x |
tab_sub
|
87 |
}) |
|
88 | 3x |
result <- do.call(rbind, tabs) |
89 | ||
90 | 3x |
n_id <- grep("n_tot", vars) |
91 | 3x |
or_id <- match("or", vars) |
92 | 3x |
ci_id <- match("ci", vars) |
93 | 3x |
structure( |
94 | 3x |
result,
|
95 | 3x |
forest_header = paste0(c("Lower", "Higher"), "\nBetter"), |
96 | 3x |
col_x = or_id, |
97 | 3x |
col_ci = ci_id, |
98 | 3x |
col_symbol_size = n_id |
99 |
)
|
|
100 |
}
|
|
101 | ||
102 |
#' Prepares Response Data Estimates for Multiple Biomarkers in a Single Data Frame
|
|
103 |
#'
|
|
104 |
#' @description `r lifecycle::badge("stable")`
|
|
105 |
#'
|
|
106 |
#' Prepares estimates for number of responses, patients and overall response rate,
|
|
107 |
#' as well as odds ratio estimates, confidence intervals and p-values,
|
|
108 |
#' for multiple biomarkers across population subgroups in a single data frame.
|
|
109 |
#' `variables` corresponds to the names of variables found in `data`, passed as a
|
|
110 |
#' named list and requires elements `rsp` and `biomarkers` (vector of continuous
|
|
111 |
#' biomarker variables) and optionally `covariates`, `subgroups` and `strat`.
|
|
112 |
#' `groups_lists` optionally specifies groupings for `subgroups` variables.
|
|
113 |
#'
|
|
114 |
#' @inheritParams argument_convention
|
|
115 |
#' @inheritParams response_subgroups
|
|
116 |
#' @param control (named `list`)\cr controls for the response definition and the
|
|
117 |
#' confidence level produced by [control_logistic()].
|
|
118 |
#'
|
|
119 |
#' @return A `data.frame` with columns `biomarker`, `biomarker_label`, `n_tot`, `n_rsp`,
|
|
120 |
#' `prop`, `or`, `lcl`, `ucl`, `conf_level`, `pval`, `pval_label`, `subgroup`, `var`,
|
|
121 |
#' `var_label`, and `row_type`.
|
|
122 |
#'
|
|
123 |
#' @note You can also specify a continuous variable in `rsp` and then use the
|
|
124 |
#' `response_definition` control to convert that internally to a logical
|
|
125 |
#' variable reflecting binary response.
|
|
126 |
#'
|
|
127 |
#' @seealso [h_logistic_mult_cont_df()] which is used internally.
|
|
128 |
#'
|
|
129 |
#' @examples
|
|
130 |
#' library(dplyr)
|
|
131 |
#' library(forcats)
|
|
132 |
#'
|
|
133 |
#' adrs <- tern_ex_adrs
|
|
134 |
#' adrs_labels <- formatters::var_labels(adrs)
|
|
135 |
#'
|
|
136 |
#' adrs_f <- adrs %>%
|
|
137 |
#' filter(PARAMCD == "BESRSPI") %>%
|
|
138 |
#' mutate(rsp = AVALC == "CR")
|
|
139 |
#'
|
|
140 |
#' # Typical analysis of two continuous biomarkers `BMRKR1` and `AGE`,
|
|
141 |
#' # in logistic regression models with one covariate `RACE`. The subgroups
|
|
142 |
#' # are defined by the levels of `BMRKR2`.
|
|
143 |
#' df <- extract_rsp_biomarkers(
|
|
144 |
#' variables = list(
|
|
145 |
#' rsp = "rsp",
|
|
146 |
#' biomarkers = c("BMRKR1", "AGE"),
|
|
147 |
#' covariates = "SEX",
|
|
148 |
#' subgroups = "BMRKR2"
|
|
149 |
#' ),
|
|
150 |
#' data = adrs_f
|
|
151 |
#' )
|
|
152 |
#' df
|
|
153 |
#'
|
|
154 |
#' # Here we group the levels of `BMRKR2` manually, and we add a stratification
|
|
155 |
#' # variable `STRATA1`. We also here use a continuous variable `EOSDY`
|
|
156 |
#' # which is then binarized internally (response is defined as this variable
|
|
157 |
#' # being larger than 500).
|
|
158 |
#' df_grouped <- extract_rsp_biomarkers(
|
|
159 |
#' variables = list(
|
|
160 |
#' rsp = "EOSDY",
|
|
161 |
#' biomarkers = c("BMRKR1", "AGE"),
|
|
162 |
#' covariates = "SEX",
|
|
163 |
#' subgroups = "BMRKR2",
|
|
164 |
#' strat = "STRATA1"
|
|
165 |
#' ),
|
|
166 |
#' data = adrs_f,
|
|
167 |
#' groups_lists = list(
|
|
168 |
#' BMRKR2 = list(
|
|
169 |
#' "low" = "LOW",
|
|
170 |
#' "low/medium" = c("LOW", "MEDIUM"),
|
|
171 |
#' "low/medium/high" = c("LOW", "MEDIUM", "HIGH")
|
|
172 |
#' )
|
|
173 |
#' ),
|
|
174 |
#' control = control_logistic(
|
|
175 |
#' response_definition = "I(response > 500)"
|
|
176 |
#' )
|
|
177 |
#' )
|
|
178 |
#' df_grouped
|
|
179 |
#'
|
|
180 |
#' @export
|
|
181 |
extract_rsp_biomarkers <- function(variables, |
|
182 |
data,
|
|
183 |
groups_lists = list(), |
|
184 |
control = control_logistic(), |
|
185 |
label_all = "All Patients") { |
|
186 | 4x |
assert_list_of_variables(variables) |
187 | 4x |
checkmate::assert_string(variables$rsp) |
188 | 4x |
checkmate::assert_character(variables$subgroups, null.ok = TRUE) |
189 | 4x |
checkmate::assert_string(label_all) |
190 | ||
191 |
# Start with all patients.
|
|
192 | 4x |
result_all <- h_logistic_mult_cont_df( |
193 | 4x |
variables = variables, |
194 | 4x |
data = data, |
195 | 4x |
control = control |
196 |
)
|
|
197 | 4x |
result_all$subgroup <- label_all |
198 | 4x |
result_all$var <- "ALL" |
199 | 4x |
result_all$var_label <- label_all |
200 | 4x |
result_all$row_type <- "content" |
201 | 4x |
if (is.null(variables$subgroups)) { |
202 |
# Only return result for all patients.
|
|
203 | 1x |
result_all
|
204 |
} else { |
|
205 |
# Add subgroups results.
|
|
206 | 3x |
l_data <- h_split_by_subgroups( |
207 | 3x |
data,
|
208 | 3x |
variables$subgroups, |
209 | 3x |
groups_lists = groups_lists |
210 |
)
|
|
211 | 3x |
l_result <- lapply(l_data, function(grp) { |
212 | 15x |
result <- h_logistic_mult_cont_df( |
213 | 15x |
variables = variables, |
214 | 15x |
data = grp$df, |
215 | 15x |
control = control |
216 |
)
|
|
217 | 15x |
result_labels <- grp$df_labels[rep(1, times = nrow(result)), ] |
218 | 15x |
cbind(result, result_labels) |
219 |
}) |
|
220 | 3x |
result_subgroups <- do.call(rbind, args = c(l_result, make.row.names = FALSE)) |
221 | 3x |
result_subgroups$row_type <- "analysis" |
222 | 3x |
rbind( |
223 | 3x |
result_all,
|
224 | 3x |
result_subgroups
|
225 |
)
|
|
226 |
}
|
|
227 |
}
|
1 |
#' Combination Functions Class
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' `CombinationFunction` is an S4 class which extends standard functions. These are special functions that
|
|
6 |
#' can be combined and negated with the logical operators.
|
|
7 |
#'
|
|
8 |
#' @param e1 (`CombinationFunction`)\cr left hand side of logical operator.
|
|
9 |
#' @param e2 (`CombinationFunction`)\cr right hand side of logical operator.
|
|
10 |
#' @param x (`CombinationFunction`)\cr the function which should be negated.
|
|
11 |
#'
|
|
12 |
#' @return Returns a logical value indicating whether the left hand side of the equation equals the right hand side.
|
|
13 |
#'
|
|
14 |
#' @exportClass CombinationFunction
|
|
15 |
#' @export CombinationFunction
|
|
16 |
#'
|
|
17 |
#' @examples
|
|
18 |
#' higher <- function(a) {
|
|
19 |
#' force(a)
|
|
20 |
#' CombinationFunction(
|
|
21 |
#' function(x) {
|
|
22 |
#' x > a
|
|
23 |
#' }
|
|
24 |
#' )
|
|
25 |
#' }
|
|
26 |
#'
|
|
27 |
#' lower <- function(b) {
|
|
28 |
#' force(b)
|
|
29 |
#' CombinationFunction(
|
|
30 |
#' function(x) {
|
|
31 |
#' x < b
|
|
32 |
#' }
|
|
33 |
#' )
|
|
34 |
#' }
|
|
35 |
#'
|
|
36 |
#' c1 <- higher(5)
|
|
37 |
#' c2 <- lower(10)
|
|
38 |
#' c3 <- higher(5) & lower(10)
|
|
39 |
#' c3(7)
|
|
40 |
#'
|
|
41 |
#' @aliases CombinationFunction-class
|
|
42 |
#' @name combination_function
|
|
43 |
CombinationFunction <- methods::setClass("CombinationFunction", contains = "function") # nolint |
|
44 | ||
45 |
#' @describeIn combination_function Logical "AND" combination of `CombinationFunction` functions.
|
|
46 |
#' The resulting object is of the same class, and evaluates the two argument functions. The result
|
|
47 |
#' is then the "AND" of the two individual results.
|
|
48 |
#'
|
|
49 |
#' @export
|
|
50 |
methods::setMethod( |
|
51 |
"&",
|
|
52 |
signature = c(e1 = "CombinationFunction", e2 = "CombinationFunction"), |
|
53 |
definition = function(e1, e2) { |
|
54 | 4x |
CombinationFunction(function(...) { |
55 | 490x |
e1(...) && e2(...) |
56 |
}) |
|
57 |
}
|
|
58 |
)
|
|
59 | ||
60 |
#' @describeIn combination_function Logical "OR" combination of `CombinationFunction` functions.
|
|
61 |
#' The resulting object is of the same class, and evaluates the two argument functions. The result
|
|
62 |
#' is then the "OR" of the two individual results.
|
|
63 |
#'
|
|
64 |
#' @export
|
|
65 |
methods::setMethod( |
|
66 |
"|",
|
|
67 |
signature = c(e1 = "CombinationFunction", e2 = "CombinationFunction"), |
|
68 |
definition = function(e1, e2) { |
|
69 | 2x |
CombinationFunction(function(...) { |
70 | 4x |
e1(...) || e2(...) |
71 |
}) |
|
72 |
}
|
|
73 |
)
|
|
74 | ||
75 |
#' @describeIn combination_function Logical negation of `CombinationFunction` functions.
|
|
76 |
#' The resulting object is of the same class, and evaluates the original function. The result
|
|
77 |
#' is then the opposite of this results.
|
|
78 |
#'
|
|
79 |
#' @export
|
|
80 |
methods::setMethod( |
|
81 |
"!",
|
|
82 |
signature = c(x = "CombinationFunction"), |
|
83 |
definition = function(x) { |
|
84 | 2x |
CombinationFunction(function(...) { |
85 | 305x |
!x(...) |
86 |
}) |
|
87 |
}
|
|
88 |
)
|
1 |
#' Estimation of Proportions per Level of Factor
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Estimate the proportion along with confidence interval of a proportion
|
|
6 |
#' regarding the level of a factor.
|
|
7 |
#'
|
|
8 |
#' @inheritParams argument_convention
|
|
9 |
#'
|
|
10 |
#' @seealso Relevant description function [d_onco_rsp_label()].
|
|
11 |
#'
|
|
12 |
#' @name estimate_multinomial_rsp
|
|
13 |
NULL
|
|
14 | ||
15 |
#' Description of Standard Oncology Response
|
|
16 |
#'
|
|
17 |
#' @description `r lifecycle::badge("stable")`
|
|
18 |
#'
|
|
19 |
#' Describe the oncology response in a standard way.
|
|
20 |
#'
|
|
21 |
#' @param x (`character`)\cr the standard oncology code to be described.
|
|
22 |
#'
|
|
23 |
#' @return Response labels.
|
|
24 |
#'
|
|
25 |
#' @seealso [estimate_multinomial_rsp()]
|
|
26 |
#'
|
|
27 |
#' @examples
|
|
28 |
#' d_onco_rsp_label(
|
|
29 |
#' c("CR", "PR", "SD", "NON CR/PD", "PD", "NE", "Missing", "<Missing>", "NE/Missing")
|
|
30 |
#' )
|
|
31 |
#'
|
|
32 |
#' # Adding some values not considered in d_onco_rsp_label
|
|
33 |
#'
|
|
34 |
#' d_onco_rsp_label(
|
|
35 |
#' c("CR", "PR", "hello", "hi")
|
|
36 |
#' )
|
|
37 |
#'
|
|
38 |
#' @export
|
|
39 |
d_onco_rsp_label <- function(x) { |
|
40 | 2x |
x <- as.character(x) |
41 | 2x |
desc <- c( |
42 | 2x |
CR = "Complete Response (CR)", |
43 | 2x |
PR = "Partial Response (PR)", |
44 | 2x |
MR = "Minimal/Minor Response (MR)", |
45 | 2x |
MRD = "Minimal Residual Disease (MRD)", |
46 | 2x |
SD = "Stable Disease (SD)", |
47 | 2x |
PD = "Progressive Disease (PD)", |
48 | 2x |
`NON CR/PD` = "Non-CR or Non-PD (NON CR/PD)", |
49 | 2x |
NE = "Not Evaluable (NE)", |
50 | 2x |
`NE/Missing` = "Missing or unevaluable", |
51 | 2x |
Missing = "Missing", |
52 | 2x |
`NA` = "Not Applicable (NA)", |
53 | 2x |
ND = "Not Done (ND)" |
54 |
)
|
|
55 | ||
56 | 2x |
values_label <- vapply( |
57 | 2x |
X = x, |
58 | 2x |
FUN.VALUE = character(1), |
59 | 2x |
function(val) { |
60 | ! |
if (val %in% names(desc)) desc[val] else val |
61 |
}
|
|
62 |
)
|
|
63 | ||
64 | 2x |
return(factor(values_label, levels = c(intersect(desc, values_label), setdiff(values_label, desc)))) |
65 |
}
|
|
66 | ||
67 |
#' @describeIn estimate_multinomial_rsp Statistics function which feeds the length of `x` as number
|
|
68 |
#' of successes, and `.N_col` as total number of successes and failures into [s_proportion()].
|
|
69 |
#'
|
|
70 |
#' @return
|
|
71 |
#' * `s_length_proportion()` returns statistics from [s_proportion()].
|
|
72 |
#'
|
|
73 |
#' @examples
|
|
74 |
#' s_length_proportion(rep("CR", 10), .N_col = 100)
|
|
75 |
#' s_length_proportion(factor(character(0)), .N_col = 100)
|
|
76 |
#'
|
|
77 |
#' @export
|
|
78 |
s_length_proportion <- function(x, |
|
79 |
.N_col, # nolint |
|
80 |
...) { |
|
81 | 4x |
checkmate::assert_multi_class(x, classes = c("factor", "character")) |
82 | 3x |
checkmate::assert_vector(x, min.len = 0, max.len = .N_col) |
83 | 2x |
checkmate::assert_vector(unique(x), min.len = 0, max.len = 1) |
84 | ||
85 | 1x |
n_true <- length(x) |
86 | 1x |
n_false <- .N_col - n_true |
87 | 1x |
x_logical <- rep(c(TRUE, FALSE), c(n_true, n_false)) |
88 | 1x |
s_proportion(df = x_logical, ...) |
89 |
}
|
|
90 | ||
91 |
#' @describeIn estimate_multinomial_rsp Formatted analysis function which is used as `afun`
|
|
92 |
#' in `estimate_multinomial_response()`.
|
|
93 |
#'
|
|
94 |
#' @return
|
|
95 |
#' * `a_length_proportion()` returns the corresponding list with formatted [rtables::CellValue()].
|
|
96 |
#'
|
|
97 |
#' @examples
|
|
98 |
#' a_length_proportion(rep("CR", 10), .N_col = 100)
|
|
99 |
#' a_length_proportion(factor(character(0)), .N_col = 100)
|
|
100 |
#'
|
|
101 |
#' @export
|
|
102 |
a_length_proportion <- make_afun( |
|
103 |
s_length_proportion,
|
|
104 |
.formats = c( |
|
105 |
n_prop = "xx (xx.x%)", |
|
106 |
prop_ci = "(xx.xx, xx.xx)" |
|
107 |
)
|
|
108 |
)
|
|
109 | ||
110 |
#' @describeIn estimate_multinomial_rsp Layout-creating function which can take statistics function arguments
|
|
111 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()] and
|
|
112 |
#' [rtables::summarize_row_groups()].
|
|
113 |
#'
|
|
114 |
#' @return
|
|
115 |
#' * `estimate_multinomial_response()` returns a layout object suitable for passing to further layouting functions,
|
|
116 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing
|
|
117 |
#' the statistics from `s_length_proportion()` to the table layout.
|
|
118 |
#'
|
|
119 |
#' @examples
|
|
120 |
#' library(dplyr)
|
|
121 |
#'
|
|
122 |
#' # Use of the layout creating function.
|
|
123 |
#' dta_test <- data.frame(
|
|
124 |
#' USUBJID = paste0("S", 1:12),
|
|
125 |
#' ARM = factor(rep(LETTERS[1:3], each = 4)),
|
|
126 |
#' AVAL = c(A = c(1, 1, 1, 1), B = c(0, 0, 1, 1), C = c(0, 0, 0, 0))
|
|
127 |
#' ) %>% mutate(
|
|
128 |
#' AVALC = factor(AVAL,
|
|
129 |
#' levels = c(0, 1),
|
|
130 |
#' labels = c("Complete Response (CR)", "Partial Response (PR)")
|
|
131 |
#' )
|
|
132 |
#' )
|
|
133 |
#'
|
|
134 |
#' lyt <- basic_table() %>%
|
|
135 |
#' split_cols_by("ARM") %>%
|
|
136 |
#' estimate_multinomial_response(var = "AVALC")
|
|
137 |
#'
|
|
138 |
#' tbl <- build_table(lyt, dta_test)
|
|
139 |
#'
|
|
140 |
#' html <- as_html(tbl)
|
|
141 |
#' html
|
|
142 |
#' \donttest{
|
|
143 |
#' Viewer(html)
|
|
144 |
#' }
|
|
145 |
#'
|
|
146 |
#' @export
|
|
147 |
estimate_multinomial_response <- function(lyt, |
|
148 |
var,
|
|
149 |
...,
|
|
150 |
show_labels = "hidden", |
|
151 |
table_names = var, |
|
152 |
.stats = "prop_ci", |
|
153 |
.formats = NULL, |
|
154 |
.labels = NULL, |
|
155 |
.indent_mods = NULL) { |
|
156 | 1x |
afun <- make_afun( |
157 | 1x |
a_length_proportion,
|
158 | 1x |
.stats = .stats, |
159 | 1x |
.formats = .formats, |
160 | 1x |
.labels = .labels, |
161 | 1x |
.indent_mods = .indent_mods |
162 |
)
|
|
163 | 1x |
lyt <- split_rows_by(lyt, var = var) |
164 | 1x |
lyt <- summarize_row_groups(lyt) |
165 | ||
166 | 1x |
analyze( |
167 | 1x |
lyt,
|
168 | 1x |
vars = var, |
169 | 1x |
afun = afun, |
170 | 1x |
show_labels = show_labels, |
171 | 1x |
table_names = table_names, |
172 | 1x |
extra_args = list(...) |
173 |
)
|
|
174 |
}
|
1 |
#' Survival Time Analysis
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Summarize median survival time and CIs, percentiles of survival times, survival
|
|
6 |
#' time range of censored/event patients.
|
|
7 |
#'
|
|
8 |
#' @inheritParams argument_convention
|
|
9 |
#' @param control (`list`)\cr parameters for comparison details, specified by using the helper function
|
|
10 |
#' [control_surv_time()]. Some possible parameter options are:
|
|
11 |
#' * `conf_level` (`proportion`)\cr confidence level of the interval for survival time.
|
|
12 |
#' * `conf_type` (`string`)\cr confidence interval type. Options are "plain" (default), "log", or "log-log",
|
|
13 |
#' see more in [survival::survfit()]. Note option "none" is not supported.
|
|
14 |
#' * `quantiles` (`numeric`)\cr vector of length two to specify the quantiles of survival time.
|
|
15 |
#'
|
|
16 |
#' @name survival_time
|
|
17 |
NULL
|
|
18 | ||
19 |
#' @describeIn survival_time Statistics function which analyzes survival times.
|
|
20 |
#'
|
|
21 |
#' @return
|
|
22 |
#' * `s_surv_time()` returns the statistics:
|
|
23 |
#' * `median`: Median survival time.
|
|
24 |
#' * `median_ci`: Confidence interval for median time.
|
|
25 |
#' * `quantiles`: Survival time for two specified quantiles.
|
|
26 |
#' * `range_censor`: Survival time range for censored observations.
|
|
27 |
#' * `range_event`: Survival time range for observations with events.
|
|
28 |
#' * `range`: Survival time range for all observations.
|
|
29 |
#'
|
|
30 |
#' @examples
|
|
31 |
#' library(dplyr)
|
|
32 |
#'
|
|
33 |
#' adtte_f <- tern_ex_adtte %>%
|
|
34 |
#' filter(PARAMCD == "OS") %>%
|
|
35 |
#' mutate(
|
|
36 |
#' AVAL = day2month(AVAL),
|
|
37 |
#' is_event = CNSR == 0
|
|
38 |
#' )
|
|
39 |
#' df <- adtte_f %>% filter(ARMCD == "ARM A")
|
|
40 |
#'
|
|
41 |
#' @keywords internal
|
|
42 |
s_surv_time <- function(df, |
|
43 |
.var,
|
|
44 |
is_event,
|
|
45 |
control = control_surv_time()) { |
|
46 | 146x |
checkmate::assert_string(.var) |
47 | 146x |
assert_df_with_variables(df, list(tte = .var, is_event = is_event)) |
48 | 146x |
checkmate::assert_numeric(df[[.var]], min.len = 1, any.missing = FALSE) |
49 | 146x |
checkmate::assert_logical(df[[is_event]], min.len = 1, any.missing = FALSE) |
50 | ||
51 | 146x |
conf_type <- control$conf_type |
52 | 146x |
conf_level <- control$conf_level |
53 | 146x |
quantiles <- control$quantiles |
54 | ||
55 | 146x |
formula <- stats::as.formula(paste0("survival::Surv(", .var, ", ", is_event, ") ~ 1")) |
56 | 146x |
srv_fit <- survival::survfit( |
57 | 146x |
formula = formula, |
58 | 146x |
data = df, |
59 | 146x |
conf.int = conf_level, |
60 | 146x |
conf.type = conf_type |
61 |
)
|
|
62 | 146x |
srv_tab <- summary(srv_fit, extend = TRUE)$table |
63 | 146x |
srv_qt_tab <- stats::quantile(srv_fit, probs = quantiles)$quantile |
64 | 146x |
range_censor <- range_noinf(df[[.var]][!df[[is_event]]], na.rm = TRUE) |
65 | 146x |
range_event <- range_noinf(df[[.var]][df[[is_event]]], na.rm = TRUE) |
66 | 146x |
range <- range_noinf(df[[.var]], na.rm = TRUE) |
67 | 146x |
list( |
68 | 146x |
median = formatters::with_label(unname(srv_tab["median"]), "Median"), |
69 | 146x |
median_ci = formatters::with_label( |
70 | 146x |
unname(srv_tab[paste0(srv_fit$conf.int, c("LCL", "UCL"))]), f_conf_level(conf_level) |
71 |
),
|
|
72 | 146x |
quantiles = formatters::with_label( |
73 | 146x |
unname(srv_qt_tab), paste0(quantiles[1] * 100, "% and ", quantiles[2] * 100, "%-ile") |
74 |
),
|
|
75 | 146x |
range_censor = formatters::with_label(range_censor, "Range (censored)"), |
76 | 146x |
range_event = formatters::with_label(range_event, "Range (event)"), |
77 | 146x |
range = formatters::with_label(range, "Range") |
78 |
)
|
|
79 |
}
|
|
80 | ||
81 |
#' @describeIn survival_time Formatted analysis function which is used as `afun` in `surv_time()`.
|
|
82 |
#'
|
|
83 |
#' @return
|
|
84 |
#' * `a_surv_time()` returns the corresponding list with formatted [rtables::CellValue()].
|
|
85 |
#'
|
|
86 |
#' @keywords internal
|
|
87 |
a_surv_time <- make_afun( |
|
88 |
s_surv_time,
|
|
89 |
.formats = c( |
|
90 |
"median" = "xx.x", |
|
91 |
"median_ci" = "(xx.x, xx.x)", |
|
92 |
"quantiles" = "xx.x, xx.x", |
|
93 |
"range_censor" = "xx.x to xx.x", |
|
94 |
"range_event" = "xx.x to xx.x", |
|
95 |
"range" = "xx.x to xx.x" |
|
96 |
)
|
|
97 |
)
|
|
98 | ||
99 |
#' @describeIn survival_time Layout-creating function which can take statistics function arguments
|
|
100 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()].
|
|
101 |
#'
|
|
102 |
#' @param .indent_mods (named `vector` of `integer`)\cr indent modifiers for the labels. Each element of the vector
|
|
103 |
#' should be a name-value pair with name corresponding to a statistic specified in `.stats` and value the indentation
|
|
104 |
#' for that statistic's row label.
|
|
105 |
#'
|
|
106 |
#' @return
|
|
107 |
#' * `surv_time()` returns a layout object suitable for passing to further layouting functions,
|
|
108 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing
|
|
109 |
#' the statistics from `s_surv_time()` to the table layout.
|
|
110 |
#'
|
|
111 |
#' @examples
|
|
112 |
#' basic_table() %>%
|
|
113 |
#' split_cols_by(var = "ARMCD") %>%
|
|
114 |
#' add_colcounts() %>%
|
|
115 |
#' surv_time(
|
|
116 |
#' vars = "AVAL",
|
|
117 |
#' var_labels = "Survival Time (Months)",
|
|
118 |
#' is_event = "is_event",
|
|
119 |
#' control = control_surv_time(conf_level = 0.9, conf_type = "log-log")
|
|
120 |
#' ) %>%
|
|
121 |
#' build_table(df = adtte_f)
|
|
122 |
#'
|
|
123 |
#' @export
|
|
124 |
surv_time <- function(lyt, |
|
125 |
vars,
|
|
126 |
...,
|
|
127 |
var_labels = "Time to Event", |
|
128 |
table_names = vars, |
|
129 |
.stats = c("median", "median_ci", "quantiles", "range_censor", "range_event"), |
|
130 |
.formats = NULL, |
|
131 |
.labels = NULL, |
|
132 |
.indent_mods = c( |
|
133 |
"median" = 0L, "median_ci" = 1L, "quantiles" = 0L, |
|
134 |
"range_censor" = 0L, "range_event" = 0L, "range" = 0L |
|
135 |
)) { |
|
136 | 2x |
afun <- make_afun( |
137 | 2x |
a_surv_time,
|
138 | 2x |
.stats = .stats, |
139 | 2x |
.formats = .formats, |
140 | 2x |
.labels = .labels, |
141 | 2x |
.indent_mods = extract_by_name(.indent_mods, .stats) |
142 |
)
|
|
143 | 2x |
analyze( |
144 | 2x |
lyt,
|
145 | 2x |
vars,
|
146 | 2x |
var_labels = var_labels, |
147 | 2x |
show_labels = "visible", |
148 | 2x |
table_names = table_names, |
149 | 2x |
afun = afun, |
150 | 2x |
extra_args = list(...) |
151 |
)
|
|
152 |
}
|
1 |
#' Subgroup Treatment Effect Pattern (STEP) Fit for Binary (Response) Outcome
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' This fits the Subgroup Treatment Effect Pattern logistic regression models for a binary
|
|
6 |
#' (response) outcome. The treatment arm variable must have exactly 2 levels,
|
|
7 |
#' where the first one is taken as reference and the estimated odds ratios are
|
|
8 |
#' for the comparison of the second level vs. the first one.
|
|
9 |
#'
|
|
10 |
#' The (conditional) logistic regression model which is fit is:
|
|
11 |
#'
|
|
12 |
#' `response ~ arm * poly(biomarker, degree) + covariates + strata(strata)`
|
|
13 |
#'
|
|
14 |
#' where `degree` is specified by `control_step()`.
|
|
15 |
#'
|
|
16 |
#' @inheritParams argument_convention
|
|
17 |
#' @param variables (named `list` of `character`)\cr list of analysis variables:
|
|
18 |
#' needs `response`, `arm`, `biomarker`, and optional `covariates` and `strata`.
|
|
19 |
#' @param control (named `list`)\cr combined control list from [control_step()]
|
|
20 |
#' and [control_logistic()].
|
|
21 |
#'
|
|
22 |
#' @return A matrix of class `step`. The first part of the columns describe the
|
|
23 |
#' subgroup intervals used for the biomarker variable, including where the
|
|
24 |
#' center of the intervals are and their bounds. The second part of the
|
|
25 |
#' columns contain the estimates for the treatment arm comparison.
|
|
26 |
#'
|
|
27 |
#' @note For the default degree 0 the `biomarker` variable is not included in the model.
|
|
28 |
#'
|
|
29 |
#' @seealso [control_step()] and [control_logistic()] for the available
|
|
30 |
#' customization options.
|
|
31 |
#'
|
|
32 |
#' @examples
|
|
33 |
#' # Testing dataset with just two treatment arms.
|
|
34 |
#' library(survival)
|
|
35 |
#' library(dplyr)
|
|
36 |
#'
|
|
37 |
#' adrs_f <- tern_ex_adrs %>%
|
|
38 |
#' filter(
|
|
39 |
#' PARAMCD == "BESRSPI",
|
|
40 |
#' ARM %in% c("B: Placebo", "A: Drug X")
|
|
41 |
#' ) %>%
|
|
42 |
#' mutate(
|
|
43 |
#' # Reorder levels of ARM to have Placebo as reference arm for Odds Ratio calculations.
|
|
44 |
#' ARM = droplevels(forcats::fct_relevel(ARM, "B: Placebo")),
|
|
45 |
#' RSP = case_when(AVALC %in% c("PR", "CR") ~ 1, TRUE ~ 0),
|
|
46 |
#' SEX = factor(SEX)
|
|
47 |
#' )
|
|
48 |
#'
|
|
49 |
#' variables <- list(
|
|
50 |
#' arm = "ARM",
|
|
51 |
#' biomarker = "BMRKR1",
|
|
52 |
#' covariates = "AGE",
|
|
53 |
#' response = "RSP"
|
|
54 |
#' )
|
|
55 |
#'
|
|
56 |
#' # Fit default STEP models: Here a constant treatment effect is estimated in each subgroup.
|
|
57 |
#' # We use a large enough bandwidth to avoid too small subgroups and linear separation in those.
|
|
58 |
#' step_matrix <- fit_rsp_step(
|
|
59 |
#' variables = variables,
|
|
60 |
#' data = adrs_f,
|
|
61 |
#' control = c(control_logistic(), control_step(bandwidth = 0.5))
|
|
62 |
#' )
|
|
63 |
#' dim(step_matrix)
|
|
64 |
#' head(step_matrix)
|
|
65 |
#'
|
|
66 |
#' # Specify different polynomial degree for the biomarker interaction to use more flexible local
|
|
67 |
#' # models. Or specify different logistic regression options, including confidence level.
|
|
68 |
#' step_matrix2 <- fit_rsp_step(
|
|
69 |
#' variables = variables,
|
|
70 |
#' data = adrs_f,
|
|
71 |
#' control = c(control_logistic(conf_level = 0.9), control_step(bandwidth = 0.6, degree = 1))
|
|
72 |
#' )
|
|
73 |
#'
|
|
74 |
#' # Use a global constant model. This is helpful as a reference for the subgroup models.
|
|
75 |
#' step_matrix3 <- fit_rsp_step(
|
|
76 |
#' variables = variables,
|
|
77 |
#' data = adrs_f,
|
|
78 |
#' control = c(control_logistic(), control_step(bandwidth = NULL, num_points = 2L))
|
|
79 |
#' )
|
|
80 |
#'
|
|
81 |
#' # It is also possible to use strata, i.e. use conditional logistic regression models.
|
|
82 |
#' variables2 <- list(
|
|
83 |
#' arm = "ARM",
|
|
84 |
#' biomarker = "BMRKR1",
|
|
85 |
#' covariates = "AGE",
|
|
86 |
#' response = "RSP",
|
|
87 |
#' strata = c("STRATA1", "STRATA2")
|
|
88 |
#' )
|
|
89 |
#'
|
|
90 |
#' step_matrix4 <- fit_rsp_step(
|
|
91 |
#' variables = variables2,
|
|
92 |
#' data = adrs_f,
|
|
93 |
#' control = c(control_logistic(), control_step(bandwidth = 0.6))
|
|
94 |
#' )
|
|
95 |
#'
|
|
96 |
#' @export
|
|
97 |
fit_rsp_step <- function(variables, |
|
98 |
data,
|
|
99 |
control = c(control_step(), control_logistic())) { |
|
100 | 5x |
assert_df_with_variables(data, variables) |
101 | 5x |
checkmate::assert_list(control, names = "named") |
102 | 5x |
data <- data[!is.na(data[[variables$biomarker]]), ] |
103 | 5x |
window_sel <- h_step_window(x = data[[variables$biomarker]], control = control) |
104 | 5x |
interval_center <- window_sel$interval[, "Interval Center"] |
105 | 5x |
form <- h_step_rsp_formula(variables = variables, control = control) |
106 | 5x |
estimates <- if (is.null(control$bandwidth)) { |
107 | 1x |
h_step_rsp_est( |
108 | 1x |
formula = form, |
109 | 1x |
data = data, |
110 | 1x |
variables = variables, |
111 | 1x |
x = interval_center, |
112 | 1x |
control = control |
113 |
)
|
|
114 |
} else { |
|
115 | 4x |
tmp <- mapply( |
116 | 4x |
FUN = h_step_rsp_est, |
117 | 4x |
x = interval_center, |
118 | 4x |
subset = as.list(as.data.frame(window_sel$sel)), |
119 | 4x |
MoreArgs = list( |
120 | 4x |
formula = form, |
121 | 4x |
data = data, |
122 | 4x |
variables = variables, |
123 | 4x |
control = control |
124 |
)
|
|
125 |
)
|
|
126 |
# Maybe we find a more elegant solution than this.
|
|
127 | 4x |
rownames(tmp) <- c("n", "logor", "se", "ci_lower", "ci_upper") |
128 | 4x |
t(tmp) |
129 |
}
|
|
130 | 5x |
result <- cbind(window_sel$interval, estimates) |
131 | 5x |
structure( |
132 | 5x |
result,
|
133 | 5x |
class = c("step", "matrix"), |
134 | 5x |
variables = variables, |
135 | 5x |
control = control |
136 |
)
|
|
137 |
}
|
1 |
#' Counting Patients Summing Exposure Across All Patients in Columns
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Counting the number of patients and summing analysis value (i.e exposure values) across all patients
|
|
6 |
#' when a column table layout is required.
|
|
7 |
#'
|
|
8 |
#' @inheritParams argument_convention
|
|
9 |
#'
|
|
10 |
#' @name summarize_patients_exposure_in_cols
|
|
11 |
NULL
|
|
12 | ||
13 |
#' @describeIn summarize_patients_exposure_in_cols Statistics function which counts numbers
|
|
14 |
#' of patients and the sum of exposure across all patients.
|
|
15 |
#'
|
|
16 |
#' @param ex_var (`character`)\cr name of the variable within `df` containing exposure values.
|
|
17 |
#' @param custom_label (`string` or `NULL`)\cr if provided and `labelstr` is empty then this will be used as label.
|
|
18 |
#'
|
|
19 |
#' @return
|
|
20 |
#' * `s_count_patients_sum_exposure()` returns a named `list` with the statistics:
|
|
21 |
#' * `n_patients`: Number of unique patients in `df`.
|
|
22 |
#' * `sum_exposure`: Sum of `ex_var` across all patients in `df`.
|
|
23 |
#'
|
|
24 |
#' @examples
|
|
25 |
#' set.seed(1)
|
|
26 |
#' df <- data.frame(
|
|
27 |
#' USUBJID = c(paste("id", seq(1, 12), sep = "")),
|
|
28 |
#' ARMCD = c(rep("ARM A", 6), rep("ARM B", 6)),
|
|
29 |
#' SEX = c(rep("Female", 6), rep("Male", 6)),
|
|
30 |
#' AVAL = as.numeric(sample(seq(1, 20), 12)),
|
|
31 |
#' stringsAsFactors = TRUE
|
|
32 |
#' )
|
|
33 |
#' adsl <- data.frame(
|
|
34 |
#' USUBJID = c(paste("id", seq(1, 12), sep = "")),
|
|
35 |
#' ARMCD = c(rep("ARM A", 2), rep("ARM B", 2)),
|
|
36 |
#' SEX = c(rep("Female", 2), rep("Male", 2)),
|
|
37 |
#' stringsAsFactors = TRUE
|
|
38 |
#' )
|
|
39 |
#'
|
|
40 |
#' @keywords internal
|
|
41 |
s_count_patients_sum_exposure <- function(df, |
|
42 |
ex_var = "AVAL", |
|
43 |
id = "USUBJID", |
|
44 |
labelstr = "", |
|
45 |
.stats = c("n_patients", "sum_exposure"), |
|
46 |
.N_col, # nolint |
|
47 |
custom_label = NULL) { |
|
48 | 56x |
assert_df_with_variables(df, list(ex_var = ex_var, id = id)) |
49 | 56x |
checkmate::assert_string(id) |
50 | 56x |
checkmate::assert_string(labelstr) |
51 | 56x |
checkmate::assert_string(custom_label, null.ok = TRUE) |
52 | 56x |
checkmate::assert_numeric(df[[ex_var]]) |
53 | 56x |
checkmate::assert_true(all(.stats %in% c("n_patients", "sum_exposure"))) |
54 | ||
55 | 56x |
row_label <- if (labelstr != "") { |
56 | ! |
labelstr
|
57 | 56x |
} else if (!is.null(custom_label)) { |
58 | 48x |
custom_label
|
59 |
} else { |
|
60 | 8x |
"Total patients numbers/person time"
|
61 |
}
|
|
62 | ||
63 | 56x |
y <- list() |
64 | ||
65 | 56x |
if ("n_patients" %in% .stats) { |
66 | 23x |
y$n_patients <- |
67 | 23x |
formatters::with_label( |
68 | 23x |
s_num_patients_content( |
69 | 23x |
df = df, |
70 | 23x |
.N_col = .N_col, # nolint |
71 | 23x |
.var = id, |
72 | 23x |
labelstr = "" |
73 | 23x |
)$unique, |
74 | 23x |
row_label
|
75 |
)
|
|
76 |
}
|
|
77 | 56x |
if ("sum_exposure" %in% .stats) { |
78 | 34x |
y$sum_exposure <- formatters::with_label(sum(df[[ex_var]]), row_label) |
79 |
}
|
|
80 | 56x |
y
|
81 |
}
|
|
82 | ||
83 |
#' @describeIn summarize_patients_exposure_in_cols Analysis function which is used as `afun` in
|
|
84 |
#' [rtables::analyze_colvars()] within `analyze_patients_exposure_in_cols()` and as `cfun` in
|
|
85 |
#' [rtables::summarize_row_groups()] within `summarize_patients_exposure_in_cols()`.
|
|
86 |
#'
|
|
87 |
#' @return
|
|
88 |
#' * `a_count_patients_sum_exposure()` returns formatted [rtables::CellValue()].
|
|
89 |
#'
|
|
90 |
#' @examples
|
|
91 |
#' a_count_patients_sum_exposure(
|
|
92 |
#' df = df,
|
|
93 |
#' var = "SEX",
|
|
94 |
#' .N_col = nrow(df),
|
|
95 |
#' .stats = "n_patients"
|
|
96 |
#' )
|
|
97 |
#'
|
|
98 |
#' @export
|
|
99 |
a_count_patients_sum_exposure <- function(df, |
|
100 |
var = NULL, |
|
101 |
ex_var = "AVAL", |
|
102 |
id = "USUBJID", |
|
103 |
labelstr = "", |
|
104 |
add_total_level = FALSE, |
|
105 |
.N_col, # nolint |
|
106 |
.stats,
|
|
107 |
.formats = list(n_patients = "xx (xx.x%)", sum_exposure = "xx"), |
|
108 |
custom_label = NULL) { |
|
109 | 32x |
checkmate::assert_flag(add_total_level) |
110 | ||
111 | 32x |
if (!is.null(var)) { |
112 | 21x |
assert_df_with_variables(df, list(var = var)) |
113 | 21x |
df[[var]] <- as.factor(df[[var]]) |
114 |
}
|
|
115 | ||
116 | 32x |
y <- list() |
117 | 32x |
if (is.null(var)) { |
118 | 11x |
y[[.stats]] <- list(Total = s_count_patients_sum_exposure( |
119 | 11x |
df = df, |
120 | 11x |
ex_var = ex_var, |
121 | 11x |
id = id, |
122 | 11x |
labelstr = labelstr, |
123 | 11x |
.N_col = .N_col, |
124 | 11x |
.stats = .stats, |
125 | 11x |
custom_label = custom_label |
126 | 11x |
)[[.stats]]) |
127 |
} else { |
|
128 | 21x |
for (lvl in levels(df[[var]])) { |
129 | 42x |
y[[.stats]][[lvl]] <- s_count_patients_sum_exposure( |
130 | 42x |
df = subset(df, get(var) == lvl), |
131 | 42x |
ex_var = ex_var, |
132 | 42x |
id = id, |
133 | 42x |
labelstr = labelstr, |
134 | 42x |
.N_col = .N_col, |
135 | 42x |
.stats = .stats, |
136 | 42x |
custom_label = lvl |
137 | 42x |
)[[.stats]] |
138 |
}
|
|
139 | 21x |
if (add_total_level) { |
140 | 2x |
y[[.stats]][["Total"]] <- s_count_patients_sum_exposure( |
141 | 2x |
df = df, |
142 | 2x |
ex_var = ex_var, |
143 | 2x |
id = id, |
144 | 2x |
labelstr = labelstr, |
145 | 2x |
.N_col = .N_col, |
146 | 2x |
.stats = .stats, |
147 | 2x |
custom_label = custom_label |
148 | 2x |
)[[.stats]] |
149 |
}
|
|
150 |
}
|
|
151 | ||
152 | 32x |
in_rows(.list = y[[.stats]], .formats = .formats[[.stats]]) |
153 |
}
|
|
154 | ||
155 |
#' @describeIn summarize_patients_exposure_in_cols Layout-creating function which can take statistics
|
|
156 |
#' function arguments and additional format arguments. This function is a wrapper for
|
|
157 |
#' [rtables::split_cols_by_multivar()] and [rtables::summarize_row_groups()].
|
|
158 |
#'
|
|
159 |
#' @return
|
|
160 |
#' * `summarize_patients_exposure_in_cols()` returns a layout object suitable for passing to further
|
|
161 |
#' layouting functions, or to [rtables::build_table()]. Adding this function to an `rtable` layout will
|
|
162 |
#' add formatted content rows, with the statistics from `s_count_patients_sum_exposure()` arranged in
|
|
163 |
#' columns, to the table layout.
|
|
164 |
#'
|
|
165 |
#' @examples
|
|
166 |
#' lyt <- basic_table() %>%
|
|
167 |
#' summarize_patients_exposure_in_cols(var = "AVAL", col_split = TRUE)
|
|
168 |
#' result <- build_table(lyt, df = df, alt_counts_df = adsl)
|
|
169 |
#' result
|
|
170 |
#'
|
|
171 |
#' lyt2 <- basic_table() %>%
|
|
172 |
#' summarize_patients_exposure_in_cols(var = "AVAL", col_split = TRUE, .stats = "sum_exposure")
|
|
173 |
#' result2 <- build_table(lyt2, df = df, alt_counts_df = adsl)
|
|
174 |
#' result2
|
|
175 |
#'
|
|
176 |
#' @export
|
|
177 |
summarize_patients_exposure_in_cols <- function(lyt, # nolint |
|
178 |
var,
|
|
179 |
...,
|
|
180 |
.stats = c("n_patients", "sum_exposure"), |
|
181 |
.labels = c(n_patients = "Patients", sum_exposure = "Person time"), |
|
182 |
.indent_mods = NULL, |
|
183 |
col_split = TRUE) { |
|
184 | 3x |
if (col_split) { |
185 | 3x |
lyt <- split_cols_by_multivar( |
186 | 3x |
lyt = lyt, |
187 | 3x |
vars = rep(var, length(.stats)), |
188 | 3x |
varlabels = .labels[.stats], |
189 | 3x |
extra_args = list(.stats = .stats) |
190 |
)
|
|
191 |
}
|
|
192 | 3x |
summarize_row_groups( |
193 | 3x |
lyt = lyt, |
194 | 3x |
var = var, |
195 | 3x |
cfun = a_count_patients_sum_exposure, |
196 | 3x |
extra_args = list(...) |
197 |
)
|
|
198 |
}
|
|
199 | ||
200 |
#' @describeIn summarize_patients_exposure_in_cols Layout-creating function which can take statistics
|
|
201 |
#' function arguments and additional format arguments. This function is a wrapper for
|
|
202 |
#' [rtables::split_cols_by_multivar()] and [rtables::analyze_colvars()].
|
|
203 |
#'
|
|
204 |
#' @param col_split (`flag`)\cr whether the columns should be split. Set to `FALSE` when the required
|
|
205 |
#' column split has been done already earlier in the layout pipe.
|
|
206 |
#'
|
|
207 |
#' @return
|
|
208 |
#' * `analyze_patients_exposure_in_cols()` returns a layout object suitable for passing to further
|
|
209 |
#' layouting functions, or to [rtables::build_table()]. Adding this function to an `rtable` layout will
|
|
210 |
#' add formatted data rows, with the statistics from `s_count_patients_sum_exposure()` arranged in
|
|
211 |
#' columns, to the table layout.
|
|
212 |
#'
|
|
213 |
#' @note As opposed to [summarize_patients_exposure_in_cols()] which generates content rows,
|
|
214 |
#' `analyze_patients_exposure_in_cols()` generates data rows which will _not_ be repeated on multiple
|
|
215 |
#' pages when pagination is used.
|
|
216 |
#'
|
|
217 |
#' @examples
|
|
218 |
#' lyt3 <- basic_table() %>%
|
|
219 |
#' split_cols_by("ARMCD", split_fun = add_overall_level("Total", first = FALSE)) %>%
|
|
220 |
#' summarize_patients_exposure_in_cols(var = "AVAL", col_split = TRUE) %>%
|
|
221 |
#' analyze_patients_exposure_in_cols(var = "SEX", col_split = FALSE)
|
|
222 |
#' result3 <- build_table(lyt3, df = df, alt_counts_df = adsl)
|
|
223 |
#' result3
|
|
224 |
#'
|
|
225 |
#' lyt4 <- basic_table() %>%
|
|
226 |
#' split_cols_by("ARMCD", split_fun = add_overall_level("Total", first = FALSE)) %>%
|
|
227 |
#' summarize_patients_exposure_in_cols(
|
|
228 |
#' var = "AVAL", col_split = TRUE,
|
|
229 |
#' .stats = "n_patients", custom_label = "some custom label"
|
|
230 |
#' ) %>%
|
|
231 |
#' analyze_patients_exposure_in_cols(var = "SEX", col_split = FALSE, ex_var = "AVAL")
|
|
232 |
#' result4 <- build_table(lyt4, df = df, alt_counts_df = adsl)
|
|
233 |
#' result4
|
|
234 |
#'
|
|
235 |
#' lyt5 <- basic_table() %>%
|
|
236 |
#' analyze_patients_exposure_in_cols(var = "SEX", col_split = TRUE, ex_var = "AVAL")
|
|
237 |
#' result5 <- build_table(lyt5, df = df, alt_counts_df = adsl)
|
|
238 |
#' result5
|
|
239 |
#'
|
|
240 |
#' # Adding total levels and custom label
|
|
241 |
#' lyt <- basic_table(
|
|
242 |
#' show_colcounts = TRUE
|
|
243 |
#' ) %>%
|
|
244 |
#' analyze_patients_exposure_in_cols(
|
|
245 |
#' var = "ARMCD",
|
|
246 |
#' col_split = TRUE,
|
|
247 |
#' add_total_level = TRUE,
|
|
248 |
#' custom_label = "TOTAL"
|
|
249 |
#' ) %>%
|
|
250 |
#' append_topleft(c("", "Sex"))
|
|
251 |
#'
|
|
252 |
#' tbl <- build_table(lyt, df = df, alt_counts_df = adsl)
|
|
253 |
#' tbl
|
|
254 |
#'
|
|
255 |
#' @export
|
|
256 |
analyze_patients_exposure_in_cols <- function(lyt, # nolint |
|
257 |
var = NULL, |
|
258 |
ex_var = "AVAL", |
|
259 |
col_split = TRUE, |
|
260 |
add_total_level = FALSE, |
|
261 |
.stats = c("n_patients", "sum_exposure"), |
|
262 |
.labels = c(n_patients = "Patients", sum_exposure = "Person time"), |
|
263 |
.indent_mods = 0L, |
|
264 |
...) { |
|
265 | 6x |
if (col_split) { |
266 | 4x |
lyt <- split_cols_by_multivar( |
267 | 4x |
lyt = lyt, |
268 | 4x |
vars = rep(ex_var, length(.stats)), |
269 | 4x |
varlabels = .labels[.stats], |
270 | 4x |
extra_args = list(.stats = .stats) |
271 |
)
|
|
272 |
}
|
|
273 | 6x |
lyt <- lyt %>% analyze_colvars( |
274 | 6x |
afun = a_count_patients_sum_exposure, |
275 | 6x |
indent_mod = .indent_mods, |
276 | 6x |
extra_args = c( |
277 | 6x |
list( |
278 | 6x |
var = var, |
279 | 6x |
ex_var = ex_var, |
280 | 6x |
add_total_level = add_total_level |
281 |
),
|
|
282 |
...
|
|
283 |
)
|
|
284 |
)
|
|
285 | 6x |
lyt
|
286 |
}
|
1 |
#' Sort Data by `PK PARAM` Variable
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' @param pk_data (`data.frame`)\cr `Pharmacokinetics` dataframe
|
|
6 |
#' @param key_var (`character`)\cr key variable used to merge pk_data and metadata created by `d_pkparam()`
|
|
7 |
#'
|
|
8 |
#' @return A PK `data.frame` sorted by a `PARAM` variable.
|
|
9 |
#'
|
|
10 |
#' @examples
|
|
11 |
#' library(dplyr)
|
|
12 |
#'
|
|
13 |
#' adpp <- tern_ex_adpp %>% mutate(PKPARAM = factor(paste0(PARAM, " (", AVALU, ")")))
|
|
14 |
#' pk_ordered_data <- h_pkparam_sort(adpp)
|
|
15 |
#'
|
|
16 |
#' @export
|
|
17 |
h_pkparam_sort <- function(pk_data, key_var = "PARAMCD") { |
|
18 | 3x |
assert_df_with_variables(pk_data, list(key_var = key_var)) |
19 | 3x |
pk_data$PARAMCD <- pk_data[[key_var]] |
20 | ||
21 | 3x |
ordered_pk_data <- d_pkparam() |
22 | ||
23 |
# Add the numeric values from ordered_pk_data to pk_data
|
|
24 | 3x |
joined_data <- merge(pk_data, ordered_pk_data, by = "PARAMCD", suffix = c("", ".y")) |
25 | ||
26 | 3x |
joined_data <- joined_data[, -grep(".*.y$", colnames(joined_data))] |
27 | ||
28 | 3x |
joined_data$TLG_ORDER <- as.numeric(joined_data$TLG_ORDER) |
29 | ||
30 |
# Then order PARAM based on this column
|
|
31 | 3x |
joined_data$PARAM <- factor(joined_data$PARAM, |
32 | 3x |
levels = unique(joined_data$PARAM[order(joined_data$TLG_ORDER)]), |
33 | 3x |
ordered = TRUE |
34 |
)
|
|
35 | ||
36 | 3x |
joined_data$TLG_DISPLAY <- factor(joined_data$TLG_DISPLAY, |
37 | 3x |
levels = unique(joined_data$TLG_DISPLAY[order(joined_data$TLG_ORDER)]), |
38 | 3x |
ordered = TRUE |
39 |
)
|
|
40 | ||
41 | 3x |
joined_data
|
42 |
}
|
1 |
#' Counting Specific Values
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' We can count the occurrence of specific values in a variable of interest.
|
|
6 |
#'
|
|
7 |
#' @inheritParams argument_convention
|
|
8 |
#'
|
|
9 |
#' @note
|
|
10 |
#' * For `factor` variables, `s_count_values` checks whether `values` are all included in the levels of `x`
|
|
11 |
#' and fails otherwise.
|
|
12 |
#' * For `count_values()`, variable labels are shown when there is more than one element in `vars`,
|
|
13 |
#' otherwise they are hidden.
|
|
14 |
#'
|
|
15 |
#' @name count_values_funs
|
|
16 |
NULL
|
|
17 | ||
18 |
#' @describeIn count_values_funs S3 generic function to count values.
|
|
19 |
#'
|
|
20 |
#' @inheritParams s_summary.logical
|
|
21 |
#' @param values (`character`)\cr specific values that should be counted.
|
|
22 |
#'
|
|
23 |
#' @return
|
|
24 |
#' * `s_count_values()` returns output of [s_summary()] for specified values of a non-numeric variable.
|
|
25 |
#'
|
|
26 |
#' @export
|
|
27 |
s_count_values <- function(x, |
|
28 |
values,
|
|
29 |
na.rm = TRUE, # nolint |
|
30 |
.N_col, # nolint |
|
31 |
.N_row, # nolint |
|
32 |
denom = c("n", "N_row", "N_col")) { |
|
33 | 79x |
UseMethod("s_count_values", x) |
34 |
}
|
|
35 | ||
36 |
#' @describeIn count_values_funs Method for `character` class.
|
|
37 |
#'
|
|
38 |
#' @method s_count_values character
|
|
39 |
#'
|
|
40 |
#' @examples
|
|
41 |
#' # `s_count_values.character`
|
|
42 |
#' s_count_values(x = c("a", "b", "a"), values = "a")
|
|
43 |
#' s_count_values(x = c("a", "b", "a", NA, NA), values = "b", na.rm = FALSE)
|
|
44 |
#'
|
|
45 |
#' @export
|
|
46 |
s_count_values.character <- function(x, |
|
47 |
values = "Y", |
|
48 |
na.rm = TRUE, # nolint |
|
49 |
...) { |
|
50 | 77x |
checkmate::assert_character(values) |
51 | ||
52 | 77x |
if (na.rm) { |
53 | 77x |
x <- x[!is.na(x)] |
54 |
}
|
|
55 | ||
56 | 77x |
is_in_values <- x %in% values |
57 | ||
58 | 77x |
s_summary(is_in_values, ...) |
59 |
}
|
|
60 | ||
61 |
#' @describeIn count_values_funs Method for `factor` class. This makes an automatic
|
|
62 |
#' conversion to `character` and then forwards to the method for characters.
|
|
63 |
#'
|
|
64 |
#' @method s_count_values factor
|
|
65 |
#'
|
|
66 |
#' @examples
|
|
67 |
#' # `s_count_values.factor`
|
|
68 |
#' s_count_values(x = factor(c("a", "b", "a")), values = "a")
|
|
69 |
#'
|
|
70 |
#' @export
|
|
71 |
s_count_values.factor <- function(x, |
|
72 |
values = "Y", |
|
73 |
...) { |
|
74 | 3x |
s_count_values(as.character(x), values = as.character(values), ...) |
75 |
}
|
|
76 | ||
77 |
#' @describeIn count_values_funs Method for `logical` class.
|
|
78 |
#'
|
|
79 |
#' @method s_count_values logical
|
|
80 |
#'
|
|
81 |
#' @examples
|
|
82 |
#' # `s_count_values.logical`
|
|
83 |
#' s_count_values(x = c(TRUE, FALSE, TRUE))
|
|
84 |
#'
|
|
85 |
#' @export
|
|
86 |
s_count_values.logical <- function(x, values = TRUE, ...) { |
|
87 | 3x |
checkmate::assert_logical(values) |
88 | 3x |
s_count_values(as.character(x), values = as.character(values), ...) |
89 |
}
|
|
90 | ||
91 |
#' @describeIn count_values_funs Formatted analysis function which is used as `afun`
|
|
92 |
#' in `count_values()`.
|
|
93 |
#'
|
|
94 |
#' @return
|
|
95 |
#' * `a_count_values()` returns the corresponding list with formatted [rtables::CellValue()].
|
|
96 |
#'
|
|
97 |
#' @examples
|
|
98 |
#' # `a_count_values`
|
|
99 |
#' a_count_values(x = factor(c("a", "b", "a")), values = "a", .N_col = 10, .N_row = 10)
|
|
100 |
#'
|
|
101 |
#' @export
|
|
102 |
a_count_values <- make_afun( |
|
103 |
s_count_values,
|
|
104 |
.formats = c(count_fraction = "xx (xx.xx%)", count = "xx") |
|
105 |
)
|
|
106 | ||
107 |
#' @describeIn count_values_funs Layout-creating function which can take statistics function arguments
|
|
108 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()].
|
|
109 |
#'
|
|
110 |
#' @return
|
|
111 |
#' * `count_values()` returns a layout object suitable for passing to further layouting functions,
|
|
112 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing
|
|
113 |
#' the statistics from `s_count_values()` to the table layout.
|
|
114 |
#'
|
|
115 |
#' @examples
|
|
116 |
#' # `count_values`
|
|
117 |
#' basic_table() %>%
|
|
118 |
#' count_values("Species", values = "setosa") %>%
|
|
119 |
#' build_table(iris)
|
|
120 |
#'
|
|
121 |
#' @export
|
|
122 |
count_values <- function(lyt, |
|
123 |
vars,
|
|
124 |
values,
|
|
125 |
...,
|
|
126 |
table_names = vars, |
|
127 |
.stats = "count_fraction", |
|
128 |
.formats = NULL, |
|
129 |
.labels = c(count_fraction = paste(values, collapse = ", ")), |
|
130 |
.indent_mods = NULL) { |
|
131 | 3x |
afun <- make_afun( |
132 | 3x |
a_count_values,
|
133 | 3x |
.stats = .stats, |
134 | 3x |
.formats = .formats, |
135 | 3x |
.labels = .labels, |
136 | 3x |
.indent_mods = .indent_mods |
137 |
)
|
|
138 | 3x |
analyze( |
139 | 3x |
lyt,
|
140 | 3x |
vars,
|
141 | 3x |
afun = afun, |
142 | 3x |
extra_args = c(list(values = values), list(...)), |
143 | 3x |
show_labels = ifelse(length(vars) > 1, "visible", "hidden"), |
144 | 3x |
table_names = table_names |
145 |
)
|
|
146 |
}
|
1 |
#' Control function for incidence rate
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' This is an auxiliary function for controlling arguments for the incidence rate, used
|
|
6 |
#' internally to specify details in `s_incidence_rate()`.
|
|
7 |
#'
|
|
8 |
#' @inheritParams argument_convention
|
|
9 |
#' @param conf_type (`string`)\cr `normal` (default), `normal_log`, `exact`, or `byar`
|
|
10 |
#' for confidence interval type.
|
|
11 |
#' @param input_time_unit (`string`)\cr `day`, `week`, `month`, or `year` (default)
|
|
12 |
#' indicating time unit for data input.
|
|
13 |
#' @param num_pt_year (`numeric`)\cr number of patient-years to use when calculating adverse event rates.
|
|
14 |
#' @param time_unit_input `r lifecycle::badge("deprecated")` Please use the `input_time_unit` argument instead.
|
|
15 |
#' @param time_unit_output `r lifecycle::badge("deprecated")` Please use the `num_pt_year` argument instead.
|
|
16 |
#'
|
|
17 |
#' @return A list of components with the same names as the arguments.
|
|
18 |
#'
|
|
19 |
#' @seealso [incidence_rate]
|
|
20 |
#'
|
|
21 |
#' @examples
|
|
22 |
#' control_incidence_rate(0.9, "exact", "month", 100)
|
|
23 |
#'
|
|
24 |
#' @export
|
|
25 |
control_incidence_rate <- function(conf_level = 0.95, |
|
26 |
conf_type = c("normal", "normal_log", "exact", "byar"), |
|
27 |
input_time_unit = c("year", "day", "week", "month"), |
|
28 |
num_pt_year = 100, |
|
29 |
time_unit_input = lifecycle::deprecated(), |
|
30 |
time_unit_output = lifecycle::deprecated()) { |
|
31 | 8x |
if (lifecycle::is_present(time_unit_input)) { |
32 | ! |
lifecycle::deprecate_warn( |
33 | ! |
"0.8.3", "control_incidence_rate(time_unit_input)", "control_incidence_rate(input_time_unit)" |
34 |
)
|
|
35 | ! |
input_time_unit <- time_unit_input |
36 |
}
|
|
37 | 8x |
if (lifecycle::is_present(time_unit_output)) { |
38 | ! |
lifecycle::deprecate_warn( |
39 | ! |
"0.8.3", "control_incidence_rate(time_unit_output)", "control_incidence_rate(num_pt_year)" |
40 |
)
|
|
41 | ! |
num_pt_year <- time_unit_output |
42 |
}
|
|
43 | ||
44 | 8x |
conf_type <- match.arg(conf_type) |
45 | 7x |
input_time_unit <- match.arg(input_time_unit) |
46 | 6x |
checkmate::assert_number(num_pt_year) |
47 | 5x |
assert_proportion_value(conf_level) |
48 | ||
49 | 4x |
list( |
50 | 4x |
conf_level = conf_level, |
51 | 4x |
conf_type = conf_type, |
52 | 4x |
input_time_unit = input_time_unit, |
53 | 4x |
num_pt_year = num_pt_year |
54 |
)
|
|
55 |
}
|
1 |
#' Occurrence Table Sorting
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Functions to score occurrence table subtables and rows which can be used in the
|
|
6 |
#' sorting of occurrence tables.
|
|
7 |
#'
|
|
8 |
#' @name score_occurrences
|
|
9 |
NULL
|
|
10 | ||
11 |
#' @describeIn score_occurrences Scoring function which sums the counts across all
|
|
12 |
#' columns. It will fail if anything else but counts are used.
|
|
13 |
#'
|
|
14 |
#' @inheritParams rtables_access
|
|
15 |
#'
|
|
16 |
#' @return
|
|
17 |
#' * `score_occurrences()` returns the sum of counts across all columns of a table row.
|
|
18 |
#'
|
|
19 |
#' @seealso [h_row_first_values()]
|
|
20 |
#'
|
|
21 |
#' @examples
|
|
22 |
#' lyt <- basic_table() %>%
|
|
23 |
#' split_cols_by("ARM") %>%
|
|
24 |
#' add_colcounts() %>%
|
|
25 |
#' analyze_num_patients(
|
|
26 |
#' vars = "USUBJID",
|
|
27 |
#' .stats = c("unique"),
|
|
28 |
#' .labels = c("Total number of patients with at least one event")
|
|
29 |
#' ) %>%
|
|
30 |
#' split_rows_by("AEBODSYS", child_labels = "visible", nested = FALSE) %>%
|
|
31 |
#' summarize_num_patients(
|
|
32 |
#' var = "USUBJID",
|
|
33 |
#' .stats = c("unique", "nonunique"),
|
|
34 |
#' .labels = c(
|
|
35 |
#' "Total number of patients with at least one event",
|
|
36 |
#' "Total number of events"
|
|
37 |
#' )
|
|
38 |
#' ) %>%
|
|
39 |
#' count_occurrences(vars = "AEDECOD")
|
|
40 |
#'
|
|
41 |
#' tbl <- build_table(lyt, tern_ex_adae, alt_counts_df = tern_ex_adsl) %>%
|
|
42 |
#' prune_table()
|
|
43 |
#'
|
|
44 |
#' tbl_sorted <- tbl %>%
|
|
45 |
#' sort_at_path(path = c("AEBODSYS", "*", "AEDECOD"), scorefun = score_occurrences)
|
|
46 |
#'
|
|
47 |
#' tbl_sorted
|
|
48 |
#'
|
|
49 |
#' @export
|
|
50 |
score_occurrences <- function(table_row) { |
|
51 | 37x |
row_counts <- h_row_counts(table_row) |
52 | 37x |
sum(row_counts) |
53 |
}
|
|
54 | ||
55 |
#' @describeIn score_occurrences Scoring functions can be produced by this constructor to only include
|
|
56 |
#' specific columns in the scoring. See [h_row_counts()] for further information.
|
|
57 |
#'
|
|
58 |
#' @inheritParams has_count_in_cols
|
|
59 |
#'
|
|
60 |
#' @return
|
|
61 |
#' * `score_occurrences_cols()` returns a function that sums counts across all specified columns
|
|
62 |
#' of a table row.
|
|
63 |
#'
|
|
64 |
#' @seealso [h_row_counts()]
|
|
65 |
#'
|
|
66 |
#' @examples
|
|
67 |
#' score_cols_a_and_b <- score_occurrences_cols(col_names = c("A: Drug X", "B: Placebo"))
|
|
68 |
#'
|
|
69 |
#' # Note that this here just sorts the AEDECOD inside the AEBODSYS. The AEBODSYS are not sorted.
|
|
70 |
#' # That would require a second pass of `sort_at_path`.
|
|
71 |
#' tbl_sorted <- tbl %>%
|
|
72 |
#' sort_at_path(path = c("AEBODSYS", "*", "AEDECOD"), scorefun = score_cols_a_and_b)
|
|
73 |
#'
|
|
74 |
#' tbl_sorted
|
|
75 |
#'
|
|
76 |
#' @export
|
|
77 |
score_occurrences_cols <- function(...) { |
|
78 | 4x |
function(table_row) { |
79 | 20x |
row_counts <- h_row_counts(table_row, ...) |
80 | 20x |
sum(row_counts) |
81 |
}
|
|
82 |
}
|
|
83 | ||
84 |
#' @describeIn score_occurrences Scoring functions produced by this constructor can be used on
|
|
85 |
#' subtables: They sum up all specified column counts in the subtable. This is useful when
|
|
86 |
#' there is no available content row summing up these counts.
|
|
87 |
#'
|
|
88 |
#' @return
|
|
89 |
#' * `score_occurrences_subtable()` returns a function that sums counts in each subtable
|
|
90 |
#' across all specified columns.
|
|
91 |
#'
|
|
92 |
#' @examples
|
|
93 |
#' score_subtable_all <- score_occurrences_subtable(col_names = names(tbl))
|
|
94 |
#'
|
|
95 |
#' # Note that this code just sorts the AEBODSYS, not the AEDECOD within AEBODSYS. That
|
|
96 |
#' # would require a second pass of `sort_at_path`.
|
|
97 |
#' tbl_sorted <- tbl %>%
|
|
98 |
#' sort_at_path(path = c("AEBODSYS"), scorefun = score_subtable_all, decreasing = FALSE)
|
|
99 |
#'
|
|
100 |
#' tbl_sorted
|
|
101 |
#'
|
|
102 |
#' @export
|
|
103 |
score_occurrences_subtable <- function(...) { |
|
104 | 1x |
score_table_row <- score_occurrences_cols(...) |
105 | 1x |
function(table_tree) { |
106 | 2x |
table_rows <- collect_leaves(table_tree) |
107 | 2x |
counts <- vapply(table_rows, score_table_row, numeric(1)) |
108 | 2x |
sum(counts) |
109 |
}
|
|
110 |
}
|
|
111 | ||
112 |
#' @describeIn score_occurrences Produce score function for sorting table by summing the first content row in
|
|
113 |
#' specified columns. Note that this is extending [rtables::cont_n_onecol()] and [rtables::cont_n_allcols()].
|
|
114 |
#'
|
|
115 |
#' @return
|
|
116 |
#' * `score_occurrences_cont_cols()` returns a function that sums counts in the first content row in
|
|
117 |
#' specified columns.
|
|
118 |
#'
|
|
119 |
#' @export
|
|
120 |
score_occurrences_cont_cols <- function(...) { |
|
121 | 1x |
score_table_row <- score_occurrences_cols(...) |
122 | 1x |
function(table_tree) { |
123 | 2x |
if (inherits(table_tree, "ContentRow")) { |
124 | ! |
return(NA) |
125 |
}
|
|
126 | 2x |
content_row <- h_content_first_row(table_tree) |
127 | 2x |
score_table_row(content_row) |
128 |
}
|
|
129 |
}
|
1 |
#' Pairwise `CoxPH` model
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Summarize p-value, HR and CIs from stratified or unstratified `CoxPH` model.
|
|
6 |
#'
|
|
7 |
#' @inheritParams argument_convention
|
|
8 |
#' @inheritParams s_surv_time
|
|
9 |
#' @param strat (`character` or `NULL`)\cr variable names indicating stratification factors.
|
|
10 |
#' @param control (`list`)\cr parameters for comparison details, specified by using the helper function
|
|
11 |
#' [control_coxph()]. Some possible parameter options are:
|
|
12 |
#' * `pval_method` (`string`)\cr p-value method for testing hazard ratio = 1. Default method is `"log-rank"` which
|
|
13 |
#' comes from [survival::survdiff()], can also be set to `"wald"` or `"likelihood"` (from [survival::coxph()]).
|
|
14 |
#' * `ties` (`string`)\cr specifying the method for tie handling. Default is `"efron"`,
|
|
15 |
#' can also be set to `"breslow"` or `"exact"`. See more in [survival::coxph()]
|
|
16 |
#' * `conf_level` (`proportion`)\cr confidence level of the interval for HR.
|
|
17 |
#'
|
|
18 |
#' @name survival_coxph_pairwise
|
|
19 |
NULL
|
|
20 | ||
21 |
#' @describeIn survival_coxph_pairwise Statistics function which analyzes HR, CIs of HR and p-value of a `coxph` model.
|
|
22 |
#'
|
|
23 |
#' @return
|
|
24 |
#' * `s_coxph_pairwise()` returns the statistics:
|
|
25 |
#' * `pvalue`: p-value to test HR = 1.
|
|
26 |
#' * `hr`: Hazard ratio.
|
|
27 |
#' * `hr_ci`: Confidence interval for hazard ratio.
|
|
28 |
#' * `n_tot`: Total number of observations.
|
|
29 |
#' * `n_tot_events`: Total number of events.
|
|
30 |
#'
|
|
31 |
#' @examples
|
|
32 |
#' library(dplyr)
|
|
33 |
#'
|
|
34 |
#' adtte_f <- tern_ex_adtte %>%
|
|
35 |
#' filter(PARAMCD == "OS") %>%
|
|
36 |
#' mutate(is_event = CNSR == 0)
|
|
37 |
#' df <- adtte_f %>%
|
|
38 |
#' filter(ARMCD == "ARM A")
|
|
39 |
#' df_ref_group <- adtte_f %>%
|
|
40 |
#' filter(ARMCD == "ARM B")
|
|
41 |
#'
|
|
42 |
#' @keywords internal
|
|
43 |
s_coxph_pairwise <- function(df, |
|
44 |
.ref_group,
|
|
45 |
.in_ref_col,
|
|
46 |
.var,
|
|
47 |
is_event,
|
|
48 |
strat = NULL, |
|
49 |
control = control_coxph()) { |
|
50 | 65x |
checkmate::assert_string(.var) |
51 | 65x |
checkmate::assert_numeric(df[[.var]]) |
52 | 65x |
checkmate::assert_logical(df[[is_event]]) |
53 | 65x |
assert_df_with_variables(df, list(tte = .var, is_event = is_event)) |
54 | 65x |
pval_method <- control$pval_method |
55 | 65x |
ties <- control$ties |
56 | 65x |
conf_level <- control$conf_level |
57 | ||
58 | 65x |
if (.in_ref_col) { |
59 | ! |
return( |
60 | ! |
list( |
61 | ! |
pvalue = formatters::with_label("", paste0("p-value (", pval_method, ")")), |
62 | ! |
hr = formatters::with_label("", "Hazard Ratio"), |
63 | ! |
hr_ci = formatters::with_label("", f_conf_level(conf_level)), |
64 | ! |
n_tot = formatters::with_label("", "Total n"), |
65 | ! |
n_tot_events = formatters::with_label("", "Total events") |
66 |
)
|
|
67 |
)
|
|
68 |
}
|
|
69 | 65x |
data <- rbind(.ref_group, df) |
70 | 65x |
group <- factor(rep(c("ref", "x"), c(nrow(.ref_group), nrow(df))), levels = c("ref", "x")) |
71 | ||
72 | 65x |
df_cox <- data.frame( |
73 | 65x |
tte = data[[.var]], |
74 | 65x |
is_event = data[[is_event]], |
75 | 65x |
arm = group |
76 |
)
|
|
77 | 65x |
if (is.null(strat)) { |
78 | 58x |
formula_cox <- survival::Surv(tte, is_event) ~ arm |
79 |
} else { |
|
80 | 7x |
formula_cox <- stats::as.formula( |
81 | 7x |
paste0( |
82 | 7x |
"survival::Surv(tte, is_event) ~ arm + strata(",
|
83 | 7x |
paste(strat, collapse = ","), |
84 |
")"
|
|
85 |
)
|
|
86 |
)
|
|
87 | 7x |
df_cox <- cbind(df_cox, data[strat]) |
88 |
}
|
|
89 | 65x |
cox_fit <- survival::coxph( |
90 | 65x |
formula = formula_cox, |
91 | 65x |
data = df_cox, |
92 | 65x |
ties = ties |
93 |
)
|
|
94 | 65x |
sum_cox <- summary(cox_fit, conf.int = conf_level, extend = TRUE) |
95 | 65x |
orginal_survdiff <- survival::survdiff( |
96 | 65x |
formula_cox,
|
97 | 65x |
data = df_cox |
98 |
)
|
|
99 | 65x |
log_rank_pvalue <- 1 - pchisq(orginal_survdiff$chisq, length(orginal_survdiff$n) - 1) |
100 | ||
101 | 65x |
pval <- switch(pval_method, |
102 | 65x |
"wald" = sum_cox$waldtest["pvalue"], |
103 | 65x |
"log-rank" = log_rank_pvalue, # pvalue from original log-rank test survival::survdiff() |
104 | 65x |
"likelihood" = sum_cox$logtest["pvalue"] |
105 |
)
|
|
106 | 65x |
list( |
107 | 65x |
pvalue = formatters::with_label(unname(pval), paste0("p-value (", pval_method, ")")), |
108 | 65x |
hr = formatters::with_label(sum_cox$conf.int[1, 1], "Hazard Ratio"), |
109 | 65x |
hr_ci = formatters::with_label(unname(sum_cox$conf.int[1, 3:4]), f_conf_level(conf_level)), |
110 | 65x |
n_tot = formatters::with_label(sum_cox$n, "Total n"), |
111 | 65x |
n_tot_events = formatters::with_label(sum_cox$nevent, "Total events") |
112 |
)
|
|
113 |
}
|
|
114 | ||
115 |
#' @describeIn survival_coxph_pairwise Formatted analysis function which is used as `afun` in `coxph_pairwise()`.
|
|
116 |
#'
|
|
117 |
#' @return
|
|
118 |
#' * `a_coxph_pairwise()` returns the corresponding list with formatted [rtables::CellValue()].
|
|
119 |
#'
|
|
120 |
#'
|
|
121 |
#' @keywords internal
|
|
122 |
a_coxph_pairwise <- make_afun( |
|
123 |
s_coxph_pairwise,
|
|
124 |
.indent_mods = c(pvalue = 0L, hr = 0L, hr_ci = 1L, n_tot = 0L, n_tot_events = 0L), |
|
125 |
.formats = c( |
|
126 |
pvalue = "x.xxxx | (<0.0001)", |
|
127 |
hr = "xx.xx", |
|
128 |
hr_ci = "(xx.xx, xx.xx)", |
|
129 |
n_tot = "xx.xx", |
|
130 |
n_tot_events = "xx.xx" |
|
131 |
)
|
|
132 |
)
|
|
133 | ||
134 |
#' @describeIn survival_coxph_pairwise Layout-creating function which can take statistics function arguments
|
|
135 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()].
|
|
136 |
#'
|
|
137 |
#' @return
|
|
138 |
#' * `coxph_pairwise()` returns a layout object suitable for passing to further layouting functions,
|
|
139 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing
|
|
140 |
#' the statistics from `s_coxph_pairwise()` to the table layout.
|
|
141 |
#'
|
|
142 |
#' @examples
|
|
143 |
#' basic_table() %>%
|
|
144 |
#' split_cols_by(var = "ARMCD", ref_group = "ARM A") %>%
|
|
145 |
#' add_colcounts() %>%
|
|
146 |
#' coxph_pairwise(
|
|
147 |
#' vars = "AVAL",
|
|
148 |
#' is_event = "is_event",
|
|
149 |
#' var_labels = "Unstratified Analysis"
|
|
150 |
#' ) %>%
|
|
151 |
#' build_table(df = adtte_f)
|
|
152 |
#'
|
|
153 |
#' basic_table() %>%
|
|
154 |
#' split_cols_by(var = "ARMCD", ref_group = "ARM A") %>%
|
|
155 |
#' add_colcounts() %>%
|
|
156 |
#' coxph_pairwise(
|
|
157 |
#' vars = "AVAL",
|
|
158 |
#' is_event = "is_event",
|
|
159 |
#' var_labels = "Stratified Analysis",
|
|
160 |
#' strat = "SEX",
|
|
161 |
#' control = control_coxph(pval_method = "wald")
|
|
162 |
#' ) %>%
|
|
163 |
#' build_table(df = adtte_f)
|
|
164 |
#'
|
|
165 |
#' @export
|
|
166 |
coxph_pairwise <- function(lyt, |
|
167 |
vars,
|
|
168 |
...,
|
|
169 |
var_labels = "CoxPH", |
|
170 |
show_labels = "visible", |
|
171 |
table_names = vars, |
|
172 |
.stats = c("pvalue", "hr", "hr_ci"), |
|
173 |
.formats = NULL, |
|
174 |
.labels = NULL, |
|
175 |
.indent_mods = NULL) { |
|
176 | 4x |
afun <- make_afun( |
177 | 4x |
a_coxph_pairwise,
|
178 | 4x |
.stats = .stats, |
179 | 4x |
.formats = .formats, |
180 | 4x |
.labels = .labels, |
181 | 4x |
.indent_mods = .indent_mods |
182 |
)
|
|
183 | 4x |
analyze( |
184 | 4x |
lyt,
|
185 | 4x |
vars,
|
186 | 4x |
var_labels = var_labels, |
187 | 4x |
show_labels = show_labels, |
188 | 4x |
table_names = table_names, |
189 | 4x |
afun = afun, |
190 | 4x |
extra_args = list(...) |
191 |
)
|
|
192 |
}
|
1 |
#' Summarize the Change from Baseline or Absolute Baseline Values
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' The primary analysis variable `.var` indicates the numerical change from baseline results,
|
|
6 |
#' and additional required secondary analysis variables are `value` and `baseline_flag`.
|
|
7 |
#' Depending on the baseline flag, either the absolute baseline values (at baseline)
|
|
8 |
#' or the change from baseline values (post-baseline) are then summarized.
|
|
9 |
#'
|
|
10 |
#' @inheritParams argument_convention
|
|
11 |
#'
|
|
12 |
#' @name summarize_change
|
|
13 |
NULL
|
|
14 | ||
15 |
#' @describeIn summarize_change Statistics function that summarizes baseline or post-baseline visits.
|
|
16 |
#'
|
|
17 |
#' @return
|
|
18 |
#' * `s_change_from_baseline()` returns the same values returned by [s_summary.numeric()].
|
|
19 |
#'
|
|
20 |
#' @note The data in `df` must be either all be from baseline or post-baseline visits. Otherwise
|
|
21 |
#' an error will be thrown.
|
|
22 |
#'
|
|
23 |
#' @examples
|
|
24 |
#' df <- data.frame(
|
|
25 |
#' chg = c(1, 2, 3),
|
|
26 |
#' is_bl = c(TRUE, TRUE, TRUE),
|
|
27 |
#' val = c(4, 5, 6)
|
|
28 |
#' )
|
|
29 |
#'
|
|
30 |
#' @keywords internal
|
|
31 |
s_change_from_baseline <- function(df, |
|
32 |
.var,
|
|
33 |
variables,
|
|
34 |
na.rm = TRUE, # nolint |
|
35 |
...) { |
|
36 | 4x |
checkmate::assert_numeric(df[[variables$value]]) |
37 | 4x |
checkmate::assert_numeric(df[[.var]]) |
38 | 4x |
checkmate::assert_logical(df[[variables$baseline_flag]]) |
39 | 4x |
checkmate::assert_vector(unique(df[[variables$baseline_flag]]), max.len = 1) |
40 | 4x |
assert_df_with_variables(df, c(variables, list(chg = .var))) |
41 | ||
42 | 4x |
combined <- ifelse( |
43 | 4x |
df[[variables$baseline_flag]], |
44 | 4x |
df[[variables$value]], |
45 | 4x |
df[[.var]] |
46 |
)
|
|
47 | 4x |
if (is.logical(combined) && identical(length(combined), 0L)) { |
48 | 1x |
combined <- numeric(0) |
49 |
}
|
|
50 | 4x |
s_summary(combined, na.rm = na.rm, ...) |
51 |
}
|
|
52 | ||
53 |
#' @describeIn summarize_change Formatted analysis function which is used as `afun` in `summarize_change()`.
|
|
54 |
#'
|
|
55 |
#' @return
|
|
56 |
#' * `a_change_from_baseline()` returns the corresponding list with formatted [rtables::CellValue()].
|
|
57 |
#'
|
|
58 |
#'
|
|
59 |
#' @keywords internal
|
|
60 |
a_change_from_baseline <- make_afun( |
|
61 |
s_change_from_baseline,
|
|
62 |
.formats = c( |
|
63 |
n = "xx", |
|
64 |
mean_sd = "xx.xx (xx.xx)", |
|
65 |
mean_se = "xx.xx (xx.xx)", |
|
66 |
median = "xx.xx", |
|
67 |
range = "xx.xx - xx.xx", |
|
68 |
mean_ci = "(xx.xx, xx.xx)", |
|
69 |
median_ci = "(xx.xx, xx.xx)", |
|
70 |
mean_pval = "xx.xx" |
|
71 |
),
|
|
72 |
.labels = c( |
|
73 |
mean_sd = "Mean (SD)", |
|
74 |
mean_se = "Mean (SE)", |
|
75 |
median = "Median", |
|
76 |
range = "Min - Max" |
|
77 |
)
|
|
78 |
)
|
|
79 | ||
80 |
#' @describeIn summarize_change Layout-creating function which can take statistics function arguments
|
|
81 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()].
|
|
82 |
#'
|
|
83 |
#' @return
|
|
84 |
#' * `summarize_change()` returns a layout object suitable for passing to further layouting functions,
|
|
85 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing
|
|
86 |
#' the statistics from `s_change_from_baseline()` to the table layout.
|
|
87 |
#'
|
|
88 |
#' @note To be used after a split on visits in the layout, such that each data subset only contains
|
|
89 |
#' either baseline or post-baseline data.
|
|
90 |
#'
|
|
91 |
#' @examples
|
|
92 |
#' # `summarize_change()`
|
|
93 |
#'
|
|
94 |
#' ## Fabricated dataset.
|
|
95 |
#' library(dplyr)
|
|
96 |
#'
|
|
97 |
#' dta_test <- data.frame(
|
|
98 |
#' USUBJID = rep(1:6, each = 3),
|
|
99 |
#' AVISIT = rep(paste0("V", 1:3), 6),
|
|
100 |
#' ARM = rep(LETTERS[1:3], rep(6, 3)),
|
|
101 |
#' AVAL = c(9:1, rep(NA, 9))
|
|
102 |
#' ) %>%
|
|
103 |
#' mutate(ABLFLL = AVISIT == "V1") %>%
|
|
104 |
#' group_by(USUBJID) %>%
|
|
105 |
#' mutate(
|
|
106 |
#' BLVAL = AVAL[ABLFLL],
|
|
107 |
#' CHG = AVAL - BLVAL
|
|
108 |
#' ) %>%
|
|
109 |
#' ungroup()
|
|
110 |
#'
|
|
111 |
#' results <- basic_table() %>%
|
|
112 |
#' split_cols_by("ARM") %>%
|
|
113 |
#' split_rows_by("AVISIT") %>%
|
|
114 |
#' summarize_change("CHG", variables = list(value = "AVAL", baseline_flag = "ABLFLL")) %>%
|
|
115 |
#' build_table(dta_test)
|
|
116 |
#' \donttest{
|
|
117 |
#' Viewer(results)
|
|
118 |
#' }
|
|
119 |
#'
|
|
120 |
#' @export
|
|
121 |
summarize_change <- function(lyt, |
|
122 |
vars,
|
|
123 |
...,
|
|
124 |
table_names = vars, |
|
125 |
.stats = c("n", "mean_sd", "median", "range"), |
|
126 |
.formats = NULL, |
|
127 |
.labels = NULL, |
|
128 |
.indent_mods = NULL) { |
|
129 | 1x |
afun <- make_afun( |
130 | 1x |
a_change_from_baseline,
|
131 | 1x |
.stats = .stats, |
132 | 1x |
.formats = .formats, |
133 | 1x |
.labels = .labels, |
134 | 1x |
.indent_mods = .indent_mods |
135 |
)
|
|
136 | ||
137 | 1x |
analyze( |
138 | 1x |
lyt,
|
139 | 1x |
vars,
|
140 | 1x |
afun = afun, |
141 | 1x |
extra_args = list(...), |
142 | 1x |
table_names = table_names |
143 |
)
|
|
144 |
}
|
1 |
#' Patient Counts with Abnormal Range Values
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Primary analysis variable `.var` indicates the abnormal range result (`character` or `factor`)
|
|
6 |
#' and additional analysis variables are `id` (`character` or `factor`) and `baseline` (`character` or
|
|
7 |
#' `factor`). For each direction specified in `abnormal` (e.g. high or low) count patients in the
|
|
8 |
#' numerator and denominator as follows:
|
|
9 |
#' * `num` : The number of patients with this abnormality recorded while on treatment.
|
|
10 |
#' * `denom`: The number of patients with at least one post-baseline assessment.
|
|
11 |
#'
|
|
12 |
#' @inheritParams argument_convention
|
|
13 |
#' @param abnormal (named `list`)\cr list identifying the abnormal range level(s) in `var`. Defaults to
|
|
14 |
#' `list(Low = "LOW", High = "HIGH")` but you can also group different levels into the named list,
|
|
15 |
#' for example, `abnormal = list(Low = c("LOW", "LOW LOW"), High = c("HIGH", "HIGH HIGH"))`.
|
|
16 |
#'
|
|
17 |
#' @note
|
|
18 |
#' * `count_abnormal()` only works with a single variable containing multiple abnormal levels.
|
|
19 |
#' * `df` should be filtered to include only post-baseline records.
|
|
20 |
#' * the denominator includes patients that might have other abnormal levels at baseline,
|
|
21 |
#' and patients with missing baseline. Patients with these abnormalities at
|
|
22 |
#' baseline can be optionally excluded from numerator and denominator.
|
|
23 |
#'
|
|
24 |
#' @name abnormal
|
|
25 |
#' @include formatting_functions.R
|
|
26 |
NULL
|
|
27 | ||
28 |
#' @describeIn abnormal Statistics function which counts patients with abnormal range values
|
|
29 |
#' for a single `abnormal` level.
|
|
30 |
#'
|
|
31 |
#' @param exclude_base_abn (`flag`)\cr whether to exclude subjects with baseline abnormality
|
|
32 |
#' from numerator and denominator.
|
|
33 |
#'
|
|
34 |
#' @return
|
|
35 |
#' * `s_count_abnormal()` returns the statistic `fraction` which is a vector with `num` and `denom` counts of patients.
|
|
36 |
#' @examples
|
|
37 |
#' library(dplyr)
|
|
38 |
#'
|
|
39 |
#' df <- data.frame(
|
|
40 |
#' USUBJID = as.character(c(1, 1, 2, 2)),
|
|
41 |
#' ANRIND = factor(c("NORMAL", "LOW", "HIGH", "HIGH")),
|
|
42 |
#' BNRIND = factor(c("NORMAL", "NORMAL", "HIGH", "HIGH")),
|
|
43 |
#' ONTRTFL = c("", "Y", "", "Y"),
|
|
44 |
#' stringsAsFactors = FALSE
|
|
45 |
#' )
|
|
46 |
#'
|
|
47 |
#' # Select only post-baseline records.
|
|
48 |
#' df <- df %>%
|
|
49 |
#' filter(ONTRTFL == "Y")
|
|
50 |
#' @keywords internal
|
|
51 |
s_count_abnormal <- function(df, |
|
52 |
.var,
|
|
53 |
abnormal = list(Low = "LOW", High = "HIGH"), |
|
54 |
variables = list(id = "USUBJID", baseline = "BNRIND"), |
|
55 |
exclude_base_abn = FALSE) { |
|
56 | 4x |
checkmate::assert_list(abnormal, types = "character", names = "named", len = 2, any.missing = FALSE) |
57 | 4x |
checkmate::assert_true(any(unlist(abnormal) %in% levels(df[[.var]]))) |
58 | 4x |
checkmate::assert_factor(df[[.var]]) |
59 | 4x |
checkmate::assert_flag(exclude_base_abn) |
60 | 4x |
assert_df_with_variables(df, c(range = .var, variables)) |
61 | 4x |
checkmate::assert_multi_class(df[[variables$baseline]], classes = c("factor", "character")) |
62 | 4x |
checkmate::assert_multi_class(df[[variables$id]], classes = c("factor", "character")) |
63 | ||
64 | 4x |
count_abnormal_single <- function(abn_name, abn) { |
65 |
# Patients in the denominator fulfill:
|
|
66 |
# - have at least one post-baseline visit
|
|
67 |
# - their baseline must not be abnormal if `exclude_base_abn`.
|
|
68 | 8x |
if (exclude_base_abn) { |
69 | 4x |
denom_select <- !(df[[variables$baseline]] %in% abn) |
70 |
} else { |
|
71 | 4x |
denom_select <- TRUE |
72 |
}
|
|
73 | 8x |
denom <- length(unique(df[denom_select, variables$id, drop = TRUE])) |
74 | ||
75 |
# Patients in the numerator fulfill:
|
|
76 |
# - have at least one post-baseline visit with the required abnormality level
|
|
77 |
# - are part of the denominator patients.
|
|
78 | 8x |
num_select <- (df[[.var]] %in% abn) & denom_select |
79 | 8x |
num <- length(unique(df[num_select, variables$id, drop = TRUE])) |
80 | ||
81 | 8x |
formatters::with_label(c(num = num, denom = denom), abn_name) |
82 |
}
|
|
83 | ||
84 |
# This will define the abnormal levels theoretically possible for a specific lab parameter
|
|
85 |
# within a split level of a layout.
|
|
86 | 4x |
abnormal_lev <- lapply(abnormal, intersect, levels(df[[.var]])) |
87 | 4x |
abnormal_lev <- abnormal_lev[vapply(abnormal_lev, function(x) length(x) > 0, logical(1))] |
88 | ||
89 | 4x |
result <- sapply(names(abnormal_lev), function(i) count_abnormal_single(i, abnormal_lev[[i]]), simplify = FALSE) |
90 | 4x |
result <- list(fraction = result) |
91 | 4x |
result
|
92 |
}
|
|
93 | ||
94 |
#' @describeIn abnormal Formatted analysis function which is used as `afun` in `count_abnormal()`.
|
|
95 |
#'
|
|
96 |
#' @return
|
|
97 |
#' * `a_count_abnormal()` returns the corresponding list with formatted [rtables::CellValue()].
|
|
98 |
#'
|
|
99 |
#' @keywords internal
|
|
100 |
a_count_abnormal <- make_afun( |
|
101 |
s_count_abnormal,
|
|
102 |
.formats = c(fraction = format_fraction) |
|
103 |
)
|
|
104 | ||
105 |
#' @describeIn abnormal Layout-creating function which can take statistics function arguments
|
|
106 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()].
|
|
107 |
#'
|
|
108 |
#' @return
|
|
109 |
#' * `count_abnormal()` returns a layout object suitable for passing to further layouting functions,
|
|
110 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing
|
|
111 |
#' the statistics from `s_count_abnormal()` to the table layout.
|
|
112 |
#'
|
|
113 |
#' @examples
|
|
114 |
#' # Layout creating function.
|
|
115 |
#' basic_table() %>%
|
|
116 |
#' count_abnormal(var = "ANRIND", abnormal = list(high = "HIGH", low = "LOW")) %>%
|
|
117 |
#' build_table(df)
|
|
118 |
#'
|
|
119 |
#' # Passing of statistics function and formatting arguments.
|
|
120 |
#' df2 <- data.frame(
|
|
121 |
#' ID = as.character(c(1, 1, 2, 2)),
|
|
122 |
#' RANGE = factor(c("NORMAL", "LOW", "HIGH", "HIGH")),
|
|
123 |
#' BL_RANGE = factor(c("NORMAL", "NORMAL", "HIGH", "HIGH")),
|
|
124 |
#' ONTRTFL = c("", "Y", "", "Y"),
|
|
125 |
#' stringsAsFactors = FALSE
|
|
126 |
#' )
|
|
127 |
#'
|
|
128 |
#' # Select only post-baseline records.
|
|
129 |
#' df2 <- df2 %>%
|
|
130 |
#' filter(ONTRTFL == "Y")
|
|
131 |
#'
|
|
132 |
#' basic_table() %>%
|
|
133 |
#' count_abnormal(
|
|
134 |
#' var = "RANGE",
|
|
135 |
#' abnormal = list(low = "LOW", high = "HIGH"),
|
|
136 |
#' variables = list(id = "ID", baseline = "BL_RANGE")
|
|
137 |
#' ) %>%
|
|
138 |
#' build_table(df2)
|
|
139 |
#'
|
|
140 |
#' @export
|
|
141 |
count_abnormal <- function(lyt, |
|
142 |
var,
|
|
143 |
...,
|
|
144 |
table_names = var, |
|
145 |
.stats = NULL, |
|
146 |
.formats = NULL, |
|
147 |
.labels = NULL, |
|
148 |
.indent_mods = NULL) { |
|
149 | 3x |
afun <- make_afun( |
150 | 3x |
a_count_abnormal,
|
151 | 3x |
.stats = .stats, |
152 | 3x |
.formats = .formats, |
153 | 3x |
.labels = .labels, |
154 | 3x |
.indent_mods = .indent_mods, |
155 | 3x |
.ungroup_stats = "fraction" |
156 |
)
|
|
157 | ||
158 | 3x |
checkmate::assert_string(var) |
159 | ||
160 | 3x |
analyze( |
161 | 3x |
lyt = lyt, |
162 | 3x |
vars = var, |
163 | 3x |
afun = afun, |
164 | 3x |
table_names = table_names, |
165 | 3x |
extra_args = list(...), |
166 | 3x |
show_labels = "hidden" |
167 |
)
|
|
168 |
}
|
1 |
#' Control Function for Logistic Regression Model Fitting
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' This is an auxiliary function for controlling arguments for logistic regression models.
|
|
6 |
#' `conf_level` refers to the confidence level used for the Odds Ratio CIs.
|
|
7 |
#'
|
|
8 |
#' @inheritParams argument_convention
|
|
9 |
#' @param response_definition (`string`)\cr the definition of what an event is in terms of `response`.
|
|
10 |
#' This will be used when fitting the logistic regression model on the left hand side of the formula.
|
|
11 |
#' Note that the evaluated expression should result in either a logical vector or a factor with 2
|
|
12 |
#' levels. By default this is just `"response"` such that the original response variable is used
|
|
13 |
#' and not modified further.
|
|
14 |
#'
|
|
15 |
#' @return A list of components with the same names as the arguments.
|
|
16 |
#'
|
|
17 |
#' @examples
|
|
18 |
#' # Standard options.
|
|
19 |
#' control_logistic()
|
|
20 |
#'
|
|
21 |
#' # Modify confidence level.
|
|
22 |
#' control_logistic(conf_level = 0.9)
|
|
23 |
#'
|
|
24 |
#' # Use a different response definition.
|
|
25 |
#' control_logistic(response_definition = "I(response %in% c('CR', 'PR'))")
|
|
26 |
#'
|
|
27 |
#' @export
|
|
28 |
control_logistic <- function(response_definition = "response", |
|
29 |
conf_level = 0.95) { |
|
30 | 28x |
checkmate::assert_true(grepl("response", response_definition)) |
31 | 27x |
checkmate::assert_string(response_definition) |
32 | 27x |
assert_proportion_value(conf_level) |
33 | 26x |
list( |
34 | 26x |
response_definition = response_definition, |
35 | 26x |
conf_level = conf_level |
36 |
)
|
|
37 |
}
|
1 |
#' Helper Function for Tabulation of a Single Biomarker Result
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' Please see [h_tab_surv_one_biomarker()] and [h_tab_rsp_one_biomarker()], which use this function for examples.
|
|
6 |
#' This function is a wrapper for [rtables::summarize_row_groups()].
|
|
7 |
#'
|
|
8 |
#' @inheritParams argument_convention
|
|
9 |
#' @param df (`data.frame`)\cr results for a single biomarker.
|
|
10 |
#' @param afuns (named `list` of `function`)\cr analysis functions.
|
|
11 |
#' @param colvars (`list` with `vars` and `labels`)\cr variables to tabulate and their labels.
|
|
12 |
#'
|
|
13 |
#' @return An `rtables` table object with statistics in columns.
|
|
14 |
#'
|
|
15 |
#' @export
|
|
16 |
h_tab_one_biomarker <- function(df, |
|
17 |
afuns,
|
|
18 |
colvars,
|
|
19 |
.indent_mods = 0L) { |
|
20 | 12x |
lyt <- basic_table() |
21 | ||
22 |
# Row split by row type - only keep the content rows here.
|
|
23 | 12x |
lyt <- split_rows_by( |
24 | 12x |
lyt = lyt, |
25 | 12x |
var = "row_type", |
26 | 12x |
split_fun = keep_split_levels("content"), |
27 | 12x |
nested = FALSE |
28 |
)
|
|
29 | ||
30 |
# Summarize rows with all patients.
|
|
31 | 12x |
lyt <- summarize_row_groups( |
32 | 12x |
lyt = lyt, |
33 | 12x |
var = "var_label", |
34 | 12x |
cfun = afuns, |
35 | 12x |
indent_mod = .indent_mods |
36 |
)
|
|
37 | ||
38 |
# Split cols by the multiple variables to populate into columns.
|
|
39 | 12x |
lyt <- split_cols_by_multivar( |
40 | 12x |
lyt = lyt, |
41 | 12x |
vars = colvars$vars, |
42 | 12x |
varlabels = colvars$labels |
43 |
)
|
|
44 | ||
45 |
# If there is any subgroup variables, we extend the layout accordingly.
|
|
46 | 12x |
if ("analysis" %in% df$row_type) { |
47 |
# Now only continue with the subgroup rows.
|
|
48 | 4x |
lyt <- split_rows_by( |
49 | 4x |
lyt = lyt, |
50 | 4x |
var = "row_type", |
51 | 4x |
split_fun = keep_split_levels("analysis"), |
52 | 4x |
nested = FALSE, |
53 | 4x |
child_labels = "hidden" |
54 |
)
|
|
55 | ||
56 |
# Split by the subgroup variable.
|
|
57 | 4x |
lyt <- split_rows_by( |
58 | 4x |
lyt = lyt, |
59 | 4x |
var = "var", |
60 | 4x |
labels_var = "var_label", |
61 | 4x |
nested = TRUE, |
62 | 4x |
child_labels = "visible", |
63 | 4x |
indent_mod = .indent_mods * 2 |
64 |
)
|
|
65 | ||
66 |
# Then analyze colvars for each subgroup.
|
|
67 | 4x |
lyt <- summarize_row_groups( |
68 | 4x |
lyt = lyt, |
69 | 4x |
cfun = afuns, |
70 | 4x |
var = "subgroup" |
71 |
)
|
|
72 |
}
|
|
73 | 12x |
build_table(lyt, df = df) |
74 |
}
|
1 |
#' Subgroup Treatment Effect Pattern (STEP) Fit for Survival Outcome
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' This fits the Subgroup Treatment Effect Pattern models for a survival outcome. The treatment arm
|
|
6 |
#' variable must have exactly 2 levels, where the first one is taken as reference and the estimated
|
|
7 |
#' hazard ratios are for the comparison of the second level vs. the first one.
|
|
8 |
#'
|
|
9 |
#' The model which is fit is:
|
|
10 |
#'
|
|
11 |
#' `Surv(time, event) ~ arm * poly(biomarker, degree) + covariates + strata(strata)`
|
|
12 |
#'
|
|
13 |
#' where `degree` is specified by `control_step()`.
|
|
14 |
#'
|
|
15 |
#' @inheritParams argument_convention
|
|
16 |
#' @param variables (named `list` of `character`)\cr list of analysis variables: needs `time`, `event`,
|
|
17 |
#' `arm`, `biomarker`, and optional `covariates` and `strata`.
|
|
18 |
#' @param control (named `list`)\cr combined control list from [control_step()] and [control_coxph()].
|
|
19 |
#'
|
|
20 |
#' @return A matrix of class `step`. The first part of the columns describe the subgroup intervals used
|
|
21 |
#' for the biomarker variable, including where the center of the intervals are and their bounds. The
|
|
22 |
#' second part of the columns contain the estimates for the treatment arm comparison.
|
|
23 |
#'
|
|
24 |
#' @note For the default degree 0 the `biomarker` variable is not included in the model.
|
|
25 |
#'
|
|
26 |
#' @seealso [control_step()] and [control_coxph()] for the available customization options.
|
|
27 |
#'
|
|
28 |
#' @examples
|
|
29 |
#' # Testing dataset with just two treatment arms.
|
|
30 |
#' library(dplyr)
|
|
31 |
#'
|
|
32 |
#' adtte_f <- tern_ex_adtte %>%
|
|
33 |
#' filter(
|
|
34 |
#' PARAMCD == "OS",
|
|
35 |
#' ARM %in% c("B: Placebo", "A: Drug X")
|
|
36 |
#' ) %>%
|
|
37 |
#' mutate(
|
|
38 |
#' # Reorder levels of ARM to display reference arm before treatment arm.
|
|
39 |
#' ARM = droplevels(forcats::fct_relevel(ARM, "B: Placebo")),
|
|
40 |
#' is_event = CNSR == 0
|
|
41 |
#' )
|
|
42 |
#' labels <- c("ARM" = "Treatment Arm", "is_event" = "Event Flag")
|
|
43 |
#' formatters::var_labels(adtte_f)[names(labels)] <- labels
|
|
44 |
#'
|
|
45 |
#' variables <- list(
|
|
46 |
#' arm = "ARM",
|
|
47 |
#' biomarker = "BMRKR1",
|
|
48 |
#' covariates = c("AGE", "BMRKR2"),
|
|
49 |
#' event = "is_event",
|
|
50 |
#' time = "AVAL"
|
|
51 |
#' )
|
|
52 |
#'
|
|
53 |
#' # Fit default STEP models: Here a constant treatment effect is estimated in each subgroup.
|
|
54 |
#' step_matrix <- fit_survival_step(
|
|
55 |
#' variables = variables,
|
|
56 |
#' data = adtte_f
|
|
57 |
#' )
|
|
58 |
#' dim(step_matrix)
|
|
59 |
#' head(step_matrix)
|
|
60 |
#'
|
|
61 |
#' # Specify different polynomial degree for the biomarker interaction to use more flexible local
|
|
62 |
#' # models. Or specify different Cox regression options.
|
|
63 |
#' step_matrix2 <- fit_survival_step(
|
|
64 |
#' variables = variables,
|
|
65 |
#' data = adtte_f,
|
|
66 |
#' control = c(control_coxph(conf_level = 0.9), control_step(degree = 2))
|
|
67 |
#' )
|
|
68 |
#'
|
|
69 |
#' # Use a global model with cubic interaction and only 5 points.
|
|
70 |
#' step_matrix3 <- fit_survival_step(
|
|
71 |
#' variables = variables,
|
|
72 |
#' data = adtte_f,
|
|
73 |
#' control = c(control_coxph(), control_step(bandwidth = NULL, degree = 3, num_points = 5L))
|
|
74 |
#' )
|
|
75 |
#'
|
|
76 |
#' @export
|
|
77 |
fit_survival_step <- function(variables, |
|
78 |
data,
|
|
79 |
control = c(control_step(), control_coxph())) { |
|
80 | 4x |
checkmate::assert_list(control) |
81 | 4x |
assert_df_with_variables(data, variables) |
82 | 4x |
data <- data[!is.na(data[[variables$biomarker]]), ] |
83 | 4x |
window_sel <- h_step_window(x = data[[variables$biomarker]], control = control) |
84 | 4x |
interval_center <- window_sel$interval[, "Interval Center"] |
85 | 4x |
form <- h_step_survival_formula(variables = variables, control = control) |
86 | 4x |
estimates <- if (is.null(control$bandwidth)) { |
87 | 1x |
h_step_survival_est( |
88 | 1x |
formula = form, |
89 | 1x |
data = data, |
90 | 1x |
variables = variables, |
91 | 1x |
x = interval_center, |
92 | 1x |
control = control |
93 |
)
|
|
94 |
} else { |
|
95 | 3x |
tmp <- mapply( |
96 | 3x |
FUN = h_step_survival_est, |
97 | 3x |
x = interval_center, |
98 | 3x |
subset = as.list(as.data.frame(window_sel$sel)), |
99 | 3x |
MoreArgs = list( |
100 | 3x |
formula = form, |
101 | 3x |
data = data, |
102 | 3x |
variables = variables, |
103 | 3x |
control = control |
104 |
)
|
|
105 |
)
|
|
106 |
# Maybe we find a more elegant solution than this.
|
|
107 | 3x |
rownames(tmp) <- c("n", "events", "loghr", "se", "ci_lower", "ci_upper") |
108 | 3x |
t(tmp) |
109 |
}
|
|
110 | 4x |
result <- cbind(window_sel$interval, estimates) |
111 | 4x |
structure( |
112 | 4x |
result,
|
113 | 4x |
class = c("step", "matrix"), |
114 | 4x |
variables = variables, |
115 | 4x |
control = control |
116 |
)
|
|
117 |
}
|
1 |
#' Generate PK reference dataset
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' @return `data.frame` of PK parameters
|
|
6 |
#'
|
|
7 |
#' @examples
|
|
8 |
#' pk_reference_dataset <- d_pkparam()
|
|
9 |
#'
|
|
10 |
#' @export
|
|
11 |
d_pkparam <- function() { |
|
12 | 3x |
pk_dataset <- as.data.frame(matrix( |
13 | 3x |
c( |
14 | 3x |
"TMAX", "Time of CMAX", "Tmax", "Plasma/Blood/Serum", "1", |
15 | 3x |
"CMAX", "Max Conc", "Cmax", "Plasma/Blood/Serum", "2", |
16 | 3x |
"CMAXD", "Max Conc Norm by Dose", "Cmax/D", "Plasma/Blood/Serum", "3", |
17 | 3x |
"AUCIFO", "AUC Infinity Obs", "AUCinf obs", "Plasma/Blood/Serum", "4", |
18 | 3x |
"AUCIFP", "AUC Infinity Pred", "AUCinf pred", "Plasma/Blood/Serum", "5", |
19 | 3x |
"AUCIFOD", "AUC Infinity Obs Norm by Dose", "AUCinf/D obs", "Plasma/Blood/Serum", "6", |
20 | 3x |
"AUCIFD", "AUC Infinity Pred Norm by Dose", "AUCinf/D pred", "Plasma/Blood/Serum", "7", |
21 | 3x |
"AUCPEO", "AUC %Extrapolation Obs", "AUCinf extrap obs", "Plasma/Blood/Serum", "8", |
22 | 3x |
"AUCPEP", "AUC %Extrapolation Pred", "AUCinf extrap pred", "Plasma/Blood/Serum", "9", |
23 | 3x |
"AUCINT", "AUC from T1 to T2", "AUCupper-lower ", "Plasma/Blood/Serum", "10", |
24 | 3x |
"AUCTAU", "AUC Over Dosing Interval", "AUCtau", "Plasma/Blood/Serum", "11", |
25 | 3x |
"AUCLST", "AUC to Last Nonzero Conc", "AUClast", "Plasma/Blood/Serum", "12", |
26 | 3x |
"AUCALL", "AUC All", "AUCall", "Plasma/Blood/Serum", "13", |
27 | 3x |
"AUMCIFO", "AUMC Infinity Obs", "AUMCinf obs", "Plasma/Blood/Serum", "14", |
28 | 3x |
"AUMCIFP", "AUMC Infinity Pred", "AUMCinf pred", "Plasma/Blood/Serum", "15", |
29 | 3x |
"AUMCPEO", "AUMC % Extrapolation Obs", "AUMC extrap obs", "Plasma/Blood/Serum", "16", |
30 | 3x |
"AUMCPEP", "AUMC % Extrapolation Pred", "AUMC extrap pred", "Plasma/Blood/Serum", "17", |
31 | 3x |
"AUMCTAU", "AUMC Over Dosing Interval", "AUMCtau", "Plasma/Blood/Serum", "18", |
32 | 3x |
"AUMCLST", "AUMC to Last Nonzero Conc", "AUMClast", "Plasma/Blood/Serum", "19", |
33 | 3x |
"AURCIFO", "AURC Infinity Obs", "AURCinf obs", "Plasma/Blood/Serum", "20", |
34 | 3x |
"AURCIFP", "AURC Infinity Pred", "AURCinf pred", "Plasma/Blood/Serum", "21", |
35 | 3x |
"AURCPEO", "AURC % Extrapolation Obs", "AURC extrap obs", "Plasma/Blood/Serum", "22", |
36 | 3x |
"AURCPEP", "AURC % Extrapolation Pred", "AURC extrap pred", "Plasma/Blood/Serum", "23", |
37 | 3x |
"AURCLST", "AURC Dosing to Last Conc", "AURClast", "Plasma/Blood/Serum", "24", |
38 | 3x |
"AURCALL", "AURC All", "AURCall", "Plasma/Blood/Serum", "25", |
39 | 3x |
"TLST", "Time of Last Nonzero Conc", "Tlast", "Plasma/Blood/Serum", "26", |
40 | 3x |
"CO", "Initial Conc", "CO", "Plasma/Blood/Serum", "27", |
41 | 3x |
"C0", "Initial Conc", "C0", "Plasma/Blood/Serum", "28", |
42 | 3x |
"CAVG", "Average Conc", "Cavg", "Plasma/Blood/Serum", "29", |
43 | 3x |
"CLST", "Last Nonzero Conc", "Clast", "Plasma/Blood/Serum", "30", |
44 | 3x |
"CMIN", "Min Conc", "Cmin", "Plasma/Blood/Serum", "31", |
45 | 3x |
"LAMZHL", "Half-Life Lambda z", "t1/2", "Plasma/Blood/Serum", "32", |
46 | 3x |
"CLFO", "Total CL Obs by F", "CL/F obs", "Plasma/Blood/Serum", "33", |
47 | 3x |
"CLFP", "Total CL Pred by F", "CL/F pred", "Plasma/Blood/Serum", "34", |
48 | 3x |
"CLO", "Total CL Obs", "CL obs", "Plasma/Blood/Serum", "35", |
49 | 3x |
"CLP", "Total CL Pred", "CL pred", "Plasma/Blood/Serum", "36", |
50 | 3x |
"CLSS", "Total CL Steady State Pred", "CLss", "Plasma/Blood/Serum", "37", |
51 | 3x |
"CLSSF", "Total CL Steady State Pred by F", "CLss/F", "Plasma/Blood/Serum", "38", |
52 | 3x |
"VZFO", "Vz Obs by F", "Vz/F obs", "Plasma/Blood/Serum", "39", |
53 | 3x |
"VZFP", "Vz Pred by F", "Vz/F pred", "Plasma/Blood/Serum", "40", |
54 | 3x |
"VZO", "Vz Obs", "Vz obs", "Plasma/Blood/Serum", "41", |
55 | 3x |
"VZP", "Vz Pred", "Vz pred", "Plasma/Blood/Serum", "42", |
56 | 3x |
"VSSO", "Vol Dist Steady State Obs", "Vss obs", "Plasma/Blood/Serum", "43", |
57 | 3x |
"VSSP", "Vol Dist Steady State Pred", "Vss pred", "Plasma/Blood/Serum", "44", |
58 | 3x |
"LAMZ", "Lambda z", "Lambda z", "Plasma/Blood/Serum", "45", |
59 | 3x |
"LAMZLL", "Lambda z Lower Limit", "Lambda z lower", "Plasma/Blood/Serum", "46", |
60 | 3x |
"LAMZUL", "Lambda z Upper Limit", "Lambda z upper", "Plasma/Blood/Serum", "47", |
61 | 3x |
"LAMZNPT", "Number of Points for Lambda z", "No points Lambda z", "Plasma/Blood/Serum", "48", |
62 | 3x |
"MRTIFO", "MRT Infinity Obs", "MRTinf obs", "Plasma/Blood/Serum", "49", |
63 | 3x |
"MRTIFP", "MRT Infinity Pred", "MRTinf pred", "Plasma/Blood/Serum", "50", |
64 | 3x |
"MRTLST", "MRT to Last Nonzero Conc", "MRTlast", "Plasma/Blood/Serum", "51", |
65 | 3x |
"R2", "R Squared", "Rsq", "Plasma/Blood/Serum", "52", |
66 | 3x |
"R2ADJ", "R Squared Adjusted", "Rsq adjusted", "Plasma/Blood/Serum", "53", |
67 | 3x |
"TLAG", "Time Until First Nonzero Conc", "TIag", "Plasma/Blood/Serum", "54", |
68 | 3x |
"TMIN", "Time of CMIN Observation", "Tmin", "Plasma/Blood/Serum", "55", |
69 | 3x |
"ACCI", "Accumulation Index", "Accumulation Index", "Plasma/Blood/Serum/Urine", "56", |
70 | 3x |
"FLUCP", "Fluctuation%", "Fluctuation", "Plasma/Blood/Serum", "57", |
71 | 3x |
"CORRXY", "Correlation Between TimeX and Log ConcY", "Corr xy", "Plasma/Blood/Serum", "58", |
72 | 3x |
"RCAMINT", "Amt Rec from T1 to T2", "Ae", "Urine", "59", |
73 | 3x |
"RCPCINT", "Pct Rec from T1 to T2", "Fe", "Urine", "60", |
74 | 3x |
"VOLPK", "Sum of Urine Vol", "Urine volume", "Urine", "61", |
75 | 3x |
"RENALCL", "Renal CL", "CLR", "Plasma/Blood/Serum/Urine", "62", |
76 | 3x |
"ERTMAX", "Time of Max Excretion Rate", "Tmax Rate", "Urine", "63", |
77 | 3x |
"RMAX", "Time of Maximum Response", "Rmax", "Matrix of PD", "64", |
78 | 3x |
"RMIN", "Time of Minimum Response", "Rmin", "Matrix of PD", "65", |
79 | 3x |
"ERMAX", "Max Excretion Rate", "Max excretion rate", "Urine", "66", |
80 | 3x |
"MIDPTLST", "Midpoint of Collection Interval", "Midpoint last", "Urine", "67", |
81 | 3x |
"ERLST", "Last Meas Excretion Rate", "Rate last", "Urine", "68", |
82 | 3x |
"TON", "Time to Onset", "Tonset", "Matrix of PD", "69", |
83 | 3x |
"TOFF", "Time to Offset", "Toffset", "Matrix of PD", "70", |
84 | 3x |
"TBBLP", "Time Below Baseline %", "Time %Below Baseline", "Matrix of PD", "71", |
85 | 3x |
"TBTP", "Time Below Threshold %", "Time %Below Threshold", "Matrix of PD", "72", |
86 | 3x |
"TABL", "Time Above Baseline", "Time Above Baseline", "Matrix of PD", "73", |
87 | 3x |
"TAT", "Time Above Threshold", "Time Above Threshold", "Matrix of PD", "74", |
88 | 3x |
"TBT", "Time Below Threshold", "Time Below Threshold", "Matrix of PD", "75", |
89 | 3x |
"TBLT", "Time Between Baseline and Threshold", "Time Between Baseline Threshold", "Matrix of PD", "76", |
90 | 3x |
"BLRSP", "Baseline Response", "Baseline", "Matrix of PD", "77", |
91 | 3x |
"TSHDRSP", "Response Threshold", "Threshold", "Matrix of PD", "78", |
92 | 3x |
"AUCABL", "AUC Above Baseline", "AUC above baseline", "Matrix of PD", "79", |
93 | 3x |
"AUCAT", "AUC Above Threshold", "AUC above threshold", "Matrix of PD", "80", |
94 | 3x |
"AUCBBL", "AUC Below Baseline", "AUC below baseline", "Matrix of PD", "81", |
95 | 3x |
"AUCBT", "AUC Below Threshold", "AUC below threshold", "Matrix of PD", "82", |
96 | 3x |
"AUCBLDIF", "Diff AUC Above Base and AUC Below Base", "AUC diff baseline", "Matrix of PD", "83", |
97 | 3x |
"AUCTDIF", "Diff AUC Above Thr and AUC Below Thr", "AUCnet threshold", "Matrix of PD", "84", |
98 | 3x |
"TDIFF", "Diff Time to Offset and Time to Onset", "Diff toffset-tonset", "Matrix of PD", "85", |
99 | 3x |
"AUCPBEO", "AUC %Back Extrapolation Obs", "AUC%Back extrap obs", "Plasma/Blood/Serum", "86", |
100 | 3x |
"AUCPBEP", "AUC %Back Extrapolation Pred", "AUC%Back extrap pred", "Plasma/Blood/Serum", "87", |
101 | 3x |
"TSLP1L", "Lower Time Limit Slope 1st", "Slope1 lower", "Matrix of PD", "88", |
102 | 3x |
"TSLP1U", "Upper Time Limit Slope 1st Segment", "Slope1 upper", "Matrix of PD", "89", |
103 | 3x |
"TSLP2L", "Lower Time Limit Slope 2nd Segment", "Slope2 lower", "Matrix of PD", "90", |
104 | 3x |
"TSLP2U", "Upper Time Limit Slope 2nd Segment", "Slope2 upper", "Matrix of PD", "91", |
105 | 3x |
"SLP1", "Slope, 1st Segment", "Slope1", "Matrix of PD", "92", |
106 | 3x |
"SLP2", "Slope, 2nd Segment", "Slope2", "Matrix of PD", "93", |
107 | 3x |
"SLP1PT", "Number of Points for Slope 1st Segment", "No points slope1", "Matrix of PD", "94", |
108 | 3x |
"SLP2PT", "Number of Points for Slope 2nd Segment", "No points slope2", "Matrix of PD", "95", |
109 | 3x |
"R2ADJS1", "R-Squared Adjusted Slope, 1st Segment", "Rsq adjusted slope1", "Matrix of PD", "96", |
110 | 3x |
"R2ADJS2", "R-Squared Adjusted Slope, 2nd Segment", "Rsq adjusted slope2", "Matrix of PD", "97", |
111 | 3x |
"R2SLP1", "R Squared, Slope, 1st Segment", "Rsq slope1", "Matrix of PD", "98", |
112 | 3x |
"R2SLP2", "R Squared, Slope, 2nd Segment", "Rsq slope2", "Matrix of PD", "99", |
113 | 3x |
"CORRXYS1", "Corr Btw TimeX and Log ConcY, Slope 1st", "Corr xy slope1", "Plasma/Blood/Serum", "100", |
114 | 3x |
"CORRXYS2", "Corr Btw TimeX and Log ConcY, Slope 1st Slope 2nd", "Corr xy slope2", "Plasma/Blood/Serum", "101", |
115 | 3x |
"AILAMZ", "Accumulation Index using Lambda z", "AILAMZ", "Plasma/Blood/Serum", "102", |
116 | 3x |
"ARAUC", "Accumulation Ratio AUCTAU", "ARAUC", "Plasma/Blood/Serum", "103", |
117 | 3x |
"ARAUCD", "Accum Ratio AUCTAU norm by dose", "ARAUCD", "Plasma/Blood/Serum", "104", |
118 | 3x |
"ARAUCIFO", "Accum Ratio AUC Infinity Obs", "ARAUCIFO", "Plasma/Blood/Serum", "105", |
119 | 3x |
"ARAUCIFP", "Accum Ratio AUC Infinity Pred", "ARAUCIFP", "Plasma/Blood/Serum", "106", |
120 | 3x |
"ARAUCIND", "Accum Ratio AUC T1 to T2 norm by dose", "ARAUCIND_T1_T2_UNIT", "Plasma/Blood/Serum", "107", |
121 | 3x |
"ARAUCINT", "Accumulation Ratio AUC from T1 to T2", "ARAUCINT_T1_T2_UNIT", "Plasma/Blood/Serum", "108", |
122 | 3x |
"ARAUCIOD", "Accum Ratio AUCIFO Norm by Dose", "ARAUCIOD", "Plasma/Blood/Serum", "109", |
123 | 3x |
"ARAUCIPD", "Accum Ratio AUCIFP Norm by Dose", "ARAUCIPD", "Plasma/Blood/Serum", "110", |
124 | 3x |
"ARAUCLST", "Accum Ratio AUC to Last Nonzero Conc", "ARAUCLST", "Plasma/Blood/Serum", "111", |
125 | 3x |
"ARCMAX", "Accumulation Ratio Cmax", "ARCMAX", "Plasma/Blood/Serum", "112", |
126 | 3x |
"ARCMAXD", "Accum Ratio Cmax norm by dose", "ARCMAXD", "Plasma/Blood/Serum", "113", |
127 | 3x |
"ARCMIN", "Accumulation Ratio Cmin", "ARCMIN", "Plasma/Blood/Serum", "114", |
128 | 3x |
"ARCMIND", "Accum Ratio Cmin norm by dose", "ARCMIND", "Plasma/Blood/Serum", "115", |
129 | 3x |
"ARCTROUD", "Accum Ratio Ctrough norm by dose", "ARCTROUD", "Plasma/Blood/Serum", "116", |
130 | 3x |
"ARCTROUG", "Accumulation Ratio Ctrough", "ARCTROUG", "Plasma/Blood/Serum", "117", |
131 | 3x |
"AUCALLB", "AUC All Norm by BMI", "AUCall_B", "Plasma/Blood/Serum", "118", |
132 | 3x |
"AUCALLD", "AUC All Norm by Dose", "AUCall_D", "Plasma/Blood/Serum", "119", |
133 | 3x |
"AUCALLS", "AUC All Norm by SA", "AUCall_S", "Plasma/Blood/Serum", "120", |
134 | 3x |
"AUCALLW", "AUC All Norm by WT", "AUCall_W", "Plasma/Blood/Serum", "121", |
135 | 3x |
"AUCIFOB", "AUC Infinity Obs Norm by BMI", "AUCINF_obs_B", "Plasma/Blood/Serum", "122", |
136 | 3x |
"AUCIFOLN", "AUC Infinity Obs LN Transformed", "AUCIFOLN", "Plasma/Blood/Serum", "123", |
137 | 3x |
"AUCIFOS", "AUC Infinity Obs Norm by SA", "AUCINF_obs_S", "Plasma/Blood/Serum", "124", |
138 | 3x |
"AUCIFOUB", "AUC Infinity Obs, Unbound Drug", "AUCIFOUB", "Plasma/Blood/Serum", "125", |
139 | 3x |
"AUCIFOW", "AUC Infinity Obs Norm by WT", "AUCINF_obs_W", "Plasma/Blood/Serum", "126", |
140 | 3x |
"AUCIFPB", "AUC Infinity Pred Norm by BMI", "AUCINF_pred_B", "Plasma/Blood/Serum", "127", |
141 | 3x |
"AUCIFPD", "AUC Infinity Pred Norm by Dose", "AUCINF_pred_D", "Plasma/Blood/Serum", "128", |
142 | 3x |
"AUCIFPS", "AUC Infinity Pred Norm by SA", "AUCINF_pred_S", "Plasma/Blood/Serum", "129", |
143 | 3x |
"AUCIFPUB", "AUC Infinity Pred, Unbound Drug", "AUCIFPUB", "Plasma/Blood/Serum", "130", |
144 | 3x |
"AUCIFPW", "AUC Infinity Pred Norm by WT", "AUCINF_pred_W", "Plasma/Blood/Serum", "131", |
145 | 3x |
"AUCINTB", "AUC from T1 to T2 Norm by BMI", "AUC_B_T1_T2_UNIT", "Plasma/Blood/Serum", "132", |
146 | 3x |
"AUCINTD", "AUC from T1 to T2 Norm by Dose", "AUC_D_T1_T2_UNIT", "Plasma/Blood/Serum", "133", |
147 | 3x |
"AUCINTS", "AUC from T1 to T2 Norm by SA", "AUC_S_T1_T2_UNIT", "Plasma/Blood/Serum", "134", |
148 | 3x |
"AUCINTW", "AUC from T1 to T2 Norm by WT", "AUC_W_T1_T2_UNIT", "Plasma/Blood/Serum", "135", |
149 | 3x |
"AUCLSTB", "AUC to Last Nonzero Conc Norm by BMI", "AUClast_B", "Plasma/Blood/Serum", "136", |
150 | 3x |
"AUCLSTD", "AUC to Last Nonzero Conc Norm by Dose", "AUClast_D", "Plasma/Blood/Serum", "137", |
151 | 3x |
"AUCLSTLN", "AUC to Last Nonzero Conc LN Transformed", "AUCLSTLN", "Plasma/Blood/Serum", "138", |
152 | 3x |
"AUCLSTS", "AUC to Last Nonzero Conc Norm by SA", "AUClast_S", "Plasma/Blood/Serum", "139", |
153 | 3x |
"AUCLSTUB", "AUC to Last Nonzero Conc, Unbound Drug", "AUCLSTUB", "Plasma/Blood/Serum", "140", |
154 | 3x |
"AUCLSTW", "AUC to Last Nonzero Conc Norm by WT", "AUClast_W", "Plasma/Blood/Serum", "141", |
155 | 3x |
"AUCTAUB", "AUC Over Dosing Interval Norm by BMI", "AUC_TAU_B", "Plasma/Blood/Serum", "142", |
156 | 3x |
"AUCTAUD", "AUC Over Dosing Interval Norm by Dose", "AUC_TAU_D", "Plasma/Blood/Serum", "143", |
157 | 3x |
"AUCTAUS", "AUC Over Dosing Interval Norm by SA", "AUC_TAU_S", "Plasma/Blood/Serum", "144", |
158 | 3x |
"AUCTAUW", "AUC Over Dosing Interval Norm by WT", "AUC_TAU_W", "Plasma/Blood/Serum", "145", |
159 | 3x |
"AUMCIFOB", "AUMC Infinity Obs Norm by BMI", "AUMCINF_obs_B", "Plasma/Blood/Serum", "146", |
160 | 3x |
"AUMCIFOD", "AUMC Infinity Obs Norm by Dose", "AUMCINF_obs_D", "Plasma/Blood/Serum", "147", |
161 | 3x |
"AUMCIFOS", "AUMC Infinity Obs Norm by SA", "AUMCINF_obs_S", "Plasma/Blood/Serum", "148", |
162 | 3x |
"AUMCIFOW", "AUMC Infinity Obs Norm by WT", "AUMCINF_obs_W", "Plasma/Blood/Serum", "149", |
163 | 3x |
"AUMCIFPB", "AUMC Infinity Pred Norm by BMI", "AUMCINF_pred_B", "Plasma/Blood/Serum", "150", |
164 | 3x |
"AUMCIFPD", "AUMC Infinity Pred Norm by Dose", "AUMCINF_pred_D", "Plasma/Blood/Serum", "151", |
165 | 3x |
"AUMCIFPS", "AUMC Infinity Pred Norm by SA", "AUMCINF_pred_S", "Plasma/Blood/Serum", "152", |
166 | 3x |
"AUMCIFPW", "AUMC Infinity Pred Norm by WT", "AUMCINF_pred_W", "Plasma/Blood/Serum", "153", |
167 | 3x |
"AUMCLSTB", "AUMC to Last Nonzero Conc Norm by BMI", "AUMClast_B", "Plasma/Blood/Serum", "154", |
168 | 3x |
"AUMCLSTD", "AUMC to Last Nonzero Conc Norm by Dose", "AUMClast_D", "Plasma/Blood/Serum", "155", |
169 | 3x |
"AUMCLSTS", "AUMC to Last Nonzero Conc Norm by SA", "AUMClast_S", "Plasma/Blood/Serum", "156", |
170 | 3x |
"AUMCLSTW", "AUMC to Last Nonzero Conc Norm by WT", "AUMClast_W", "Plasma/Blood/Serum", "157", |
171 | 3x |
"AUMCTAUB", "AUMC Over Dosing Interval Norm by BMI", "AUMCTAUB", "Plasma/Blood/Serum", "158", |
172 | 3x |
"AUMCTAUD", "AUMC Over Dosing Interval Norm by Dose", "AUMCTAUD", "Plasma/Blood/Serum", "159", |
173 | 3x |
"AUMCTAUS", "AUMC Over Dosing Interval Norm by SA", "AUMCTAUS", "Plasma/Blood/Serum", "160", |
174 | 3x |
"AUMCTAUW", "AUMC Over Dosing Interval Norm by WT", "AUMCTAUW", "Plasma/Blood/Serum", "161", |
175 | 3x |
"AURCALLB", "AURC All Norm by BMI", "AURCALLB", "Plasma/Blood/Serum", "162", |
176 | 3x |
"AURCALLD", "AURC All Norm by Dose", "AURCALLD", "Plasma/Blood/Serum", "163", |
177 | 3x |
"AURCALLS", "AURC All Norm by SA", "AURCALLS", "Plasma/Blood/Serum", "164", |
178 | 3x |
"AURCALLW", "AURC All Norm by WT", "AURCALLW", "Plasma/Blood/Serum", "165", |
179 | 3x |
"AURCIFOB", "AURC Infinity Obs Norm by BMI", "AURCIFOB", "Plasma/Blood/Serum", "166", |
180 | 3x |
"AURCIFOD", "AURC Infinity Obs Norm by Dose", "AURCIFOD", "Plasma/Blood/Serum", "167", |
181 | 3x |
"AURCIFOS", "AURC Infinity Obs Norm by SA", "AURCIFOS", "Plasma/Blood/Serum", "168", |
182 | 3x |
"AURCIFOW", "AURC Infinity Obs Norm by WT", "AURCIFOW", "Plasma/Blood/Serum", "169", |
183 | 3x |
"AURCIFPB", "AURC Infinity Pred Norm by BMI", "AURCIFPB", "Plasma/Blood/Serum", "170", |
184 | 3x |
"AURCIFPD", "AURC Infinity Pred Norm by Dose", "AURCIFPD", "Plasma/Blood/Serum", "171", |
185 | 3x |
"AURCIFPS", "AURC Infinity Pred Norm by SA", "AURCIFPS", "Plasma/Blood/Serum", "172", |
186 | 3x |
"AURCIFPW", "AURC Infinity Pred Norm by WT", "AURCIFPW", "Plasma/Blood/Serum", "173", |
187 | 3x |
"AURCINT", "AURC from T1 to T2", "AURCINT_T1_T2_UNIT", "Plasma/Blood/Serum", "174", |
188 | 3x |
"AURCINTB", "AURC from T1 to T2 Norm by BMI", "AURCINTB_T1_T2_UNIT", "Plasma/Blood/Serum", "175", |
189 | 3x |
"AURCINTD", "AURC from T1 to T2 Norm by Dose", "AURCINTD_T1_T2_UNIT", "Plasma/Blood/Serum", "176", |
190 | 3x |
"AURCINTS", "AURC from T1 to T2 Norm by SA", "AURCINTS_T1_T2_UNIT", "Plasma/Blood/Serum", "177", |
191 | 3x |
"AURCINTW", "AURC from T1 to T2 Norm by WT", "AURCINTW_T1_T2_UNIT", "Plasma/Blood/Serum", "178", |
192 | 3x |
"AURCLSTB", "AURC to Last Nonzero Rate Norm by BMI", "AURCLSTB", "Plasma/Blood/Serum", "179", |
193 | 3x |
"AURCLSTD", "AURC to Last Nonzero Rate Norm by Dose", "AURCLSTD", "Plasma/Blood/Serum", "180", |
194 | 3x |
"AURCLSTS", "AURC to Last Nonzero Rate Norm by SA", "AURCLSTS", "Plasma/Blood/Serum", "181", |
195 | 3x |
"AURCLSTW", "AURC to Last Nonzero Rate Norm by WT", "AURCLSTW", "Plasma/Blood/Serum", "182", |
196 | 3x |
"C0B", "Initial Conc Norm by BMI", "C0B", "Plasma/Blood/Serum", "183", |
197 | 3x |
"C0D", "Initial Conc Norm by Dose", "C0D", "Plasma/Blood/Serum", "184", |
198 | 3x |
"C0S", "Initial Conc Norm by SA", "C0S", "Plasma/Blood/Serum", "185", |
199 | 3x |
"C0W", "Initial Conc Norm by WT", "C0W", "Plasma/Blood/Serum", "186", |
200 | 3x |
"CAVGB", "Average Conc Norm by BMI", "CAVGB", "Plasma/Blood/Serum", "187", |
201 | 3x |
"CAVGD", "Average Conc Norm by Dose", "CAVGD", "Plasma/Blood/Serum", "188", |
202 | 3x |
"CAVGINT", "Average Conc from T1 to T2", "CAVGINT_T1_T2_UNIT", "Plasma/Blood/Serum", "189", |
203 | 3x |
"CAVGINTB", "Average Conc from T1 to T2 Norm by BMI", "CAVGINTB_T1_T2_UNIT", "Plasma/Blood/Serum", "190", |
204 | 3x |
"CAVGINTD", "Average Conc from T1 to T2 Norm by Dose", "CAVGINTD_T1_T2_UNIT", "Plasma/Blood/Serum", "191", |
205 | 3x |
"CAVGINTS", "Average Conc from T1 to T2 Norm by SA", "CAVGINTS_T1_T2_UNIT", "Plasma/Blood/Serum", "192", |
206 | 3x |
"CAVGINTW", "Average Conc from T1 to T2 Norm by WT", "CAVGINTW_T1_T2_UNIT", "Plasma/Blood/Serum", "193", |
207 | 3x |
"CAVGS", "Average Conc Norm by SA", "CAVGS", "Plasma/Blood/Serum", "194", |
208 | 3x |
"CAVGW", "Average Conc Norm by WT", "CAVGW", "Plasma/Blood/Serum", "195", |
209 | 3x |
"CHTMAX", "Concentration at Half Tmax", "CHTMAX", "Plasma/Blood/Serum", "196", |
210 | 3x |
"CLFOB", "Total CL Obs by F Norm by BMI", "CLFOB", "Plasma/Blood/Serum", "197", |
211 | 3x |
"CLFOD", "Total CL Obs by F Norm by Dose", "CLFOD", "Plasma/Blood/Serum", "198", |
212 | 3x |
"CLFOS", "Total CL Obs by F Norm by SA", "CLFOS", "Plasma/Blood/Serum", "199", |
213 | 3x |
"CLFOW", "Total CL Obs by F Norm by WT", "CLFOW", "Plasma/Blood/Serum", "200", |
214 | 3x |
"CLFPB", "Total CL Pred by F Norm by BMI", "CLFPB", "Plasma/Blood/Serum", "201", |
215 | 3x |
"CLFPD", "Total CL Pred by F Norm by Dose", "CLFPD", "Plasma/Blood/Serum", "202", |
216 | 3x |
"CLFPS", "Total CL Pred by F Norm by SA", "CLFPS", "Plasma/Blood/Serum", "203", |
217 | 3x |
"CLFPW", "Total CL Pred by F Norm by WT", "CLFPW", "Plasma/Blood/Serum", "204", |
218 | 3x |
"CLFTAU", "Total CL by F for Dose Int", "CLFTAU", "Plasma/Blood/Serum", "205", |
219 | 3x |
"CLFTAUB", "Total CL by F for Dose Int Norm by BMI", "CLFTAUB", "Plasma/Blood/Serum", "206", |
220 | 3x |
"CLFTAUD", "Total CL by F for Dose Int Norm by Dose", "CLFTAUD", "Plasma/Blood/Serum", "207", |
221 | 3x |
"CLFTAUS", "Total CL by F for Dose Int Norm by SA", "CLFTAUS", "Plasma/Blood/Serum", "208", |
222 | 3x |
"CLFTAUW", "Total CL by F for Dose Int Norm by WT", "CLFTAUW", "Plasma/Blood/Serum", "209", |
223 | 3x |
"CLFUB", "Apparent CL for Unbound Drug", "CLFUB", "Plasma/Blood/Serum", "210", |
224 | 3x |
"CLOB", "Total CL Obs Norm by BMI", "CLOB", "Plasma/Blood/Serum", "211", |
225 | 3x |
"CLOD", "Total CL Obs Norm by Dose", "CLOD", "Plasma/Blood/Serum", "212", |
226 | 3x |
"CLOS", "Total CL Obs Norm by SA", "CLOS", "Plasma/Blood/Serum", "213", |
227 | 3x |
"CLOUB", "Total CL Obs for Unbound Drug", "CLOUB", "Plasma/Blood/Serum", "214", |
228 | 3x |
"CLOW", "Total CL Obs Norm by WT", "CLOW", "Plasma/Blood/Serum", "215", |
229 | 3x |
"CLPB", "Total CL Pred Norm by BMI", "CLPB", "Plasma/Blood/Serum", "216", |
230 | 3x |
"CLPD", "Total CL Pred Norm by Dose", "CLPD", "Plasma/Blood/Serum", "217", |
231 | 3x |
"CLPS", "Total CL Pred Norm by SA", "CLPS", "Plasma/Blood/Serum", "218", |
232 | 3x |
"CLPUB", "Total CL Pred for Unbound Drug", "CLPUB", "Plasma/Blood/Serum", "219", |
233 | 3x |
"CLPW", "Total CL Pred Norm by WT", "CLPW", "Plasma/Blood/Serum", "220", |
234 | 3x |
"CLRPCLEV", "Renal CL as Pct CL EV", "CLRPCLEV", "Urine", "221", |
235 | 3x |
"CLRPCLIV", "Renal CL as Pct CL IV", "CLRPCLIV", "Urine", "222", |
236 | 3x |
"CLSTB", "Last Nonzero Conc Norm by BMI", "CLSTB", "Plasma/Blood/Serum", "223", |
237 | 3x |
"CLSTD", "Last Nonzero Conc Norm by Dose", "CLSTD", "Plasma/Blood/Serum", "224", |
238 | 3x |
"CLSTS", "Last Nonzero Conc Norm by SA", "CLSTS", "Plasma/Blood/Serum", "225", |
239 | 3x |
"CLSTW", "Last Nonzero Conc Norm by WT", "CLSTW", "Plasma/Blood/Serum", "226", |
240 | 3x |
"CLTAU", "Total CL for Dose Int", "CLTAU", "Plasma/Blood/Serum", "227", |
241 | 3x |
"CLTAUB", "Total CL for Dose Int Norm by BMI", "CLTAUB", "Plasma/Blood/Serum", "228", |
242 | 3x |
"CLTAUD", "Total CL for Dose Int Norm by Dose", "CLTAUD", "Plasma/Blood/Serum", "229", |
243 | 3x |
"CLTAUS", "Total CL for Dose Int Norm by SA", "CLTAUS", "Plasma/Blood/Serum", "230", |
244 | 3x |
"CLTAUW", "Total CL for Dose Int Norm by WT", "CLTAUW", "Plasma/Blood/Serum", "231", |
245 | 3x |
"CMAXB", "Max Conc Norm by BMI", "CMAX_B", "Plasma/Blood/Serum", "232", |
246 | 3x |
"CMAXLN", "Max Conc LN Transformed", "CMAXLN", "Plasma/Blood/Serum", "233", |
247 | 3x |
"CMAXS", "Max Conc Norm by SA", "CMAXS", "Plasma/Blood/Serum", "234", |
248 | 3x |
"CMAXUB", "Max Conc, Unbound Drug", "CMAXUB", "Plasma/Blood/Serum", "235", |
249 | 3x |
"CMAXW", "Max Conc Norm by WT", "CMAXW", "Plasma/Blood/Serum", "236", |
250 | 3x |
"CMINB", "Min Conc Norm by BMI", "CMINB", "Plasma/Blood/Serum", "237", |
251 | 3x |
"CMIND", "Min Conc Norm by Dose", "CMIND", "Plasma/Blood/Serum", "238", |
252 | 3x |
"CMINS", "Min Conc Norm by SA", "CMINS", "Plasma/Blood/Serum", "239", |
253 | 3x |
"CMINW", "Min Conc Norm by WT", "CMINW", "Plasma/Blood/Serum", "240", |
254 | 3x |
"CONC", "Concentration", "CONC", "Plasma/Blood/Serum", "241", |
255 | 3x |
"CONCB", "Conc by BMI", "CONCB", "Plasma/Blood/Serum", "242", |
256 | 3x |
"CONCD", "Conc by Dose", "CONCD", "Plasma/Blood/Serum", "243", |
257 | 3x |
"CONCS", "Conc by SA", "CONCS", "Plasma/Blood/Serum", "244", |
258 | 3x |
"CONCW", "Conc by WT", "CONCW", "Plasma/Blood/Serum", "245", |
259 | 3x |
"CTROUGH", "Conc Trough", "CTROUGH", "Plasma/Blood/Serum", "246", |
260 | 3x |
"CTROUGHB", "Conc Trough by BMI", "CTROUGHB", "Plasma/Blood/Serum", "247", |
261 | 3x |
"CTROUGHD", "Conc Trough by Dose", "CTROUGHD", "Plasma/Blood/Serum", "248", |
262 | 3x |
"CTROUGHS", "Conc Trough by SA", "CTROUGHS", "Plasma/Blood/Serum", "249", |
263 | 3x |
"CTROUGHW", "Conc Trough by WT", "CTROUGHW", "Plasma/Blood/Serum", "250", |
264 | 3x |
"EFFHL", "Effective Half-Life", "EFFHL", "Plasma/Blood/Serum", "251", |
265 | 3x |
"ERINT", "Excret Rate from T1 to T2", "ERINT_T1_T2_UNIT", "Plasma/Blood/Serum", "252", |
266 | 3x |
"ERINTB", "Excret Rate from T1 to T2 Norm by BMI", "ERINTB_T1_T2_UNIT", "Plasma/Blood/Serum", "253", |
267 | 3x |
"ERINTD", "Excret Rate from T1 to T2 Norm by Dose", "ERINTD_T1_T2_UNIT", "Plasma/Blood/Serum", "254", |
268 | 3x |
"ERINTS", "Excret Rate from T1 to T2 Norm by SA", "ERINTS_T1_T2_UNIT", "Plasma/Blood/Serum", "255", |
269 | 3x |
"ERINTW", "Excret Rate from T1 to T2 Norm by WT", "ERINTW_T1_T2_UNIT", "Plasma/Blood/Serum", "256", |
270 | 3x |
"ERLSTB", "Last Meas Excretion Rate Norm by BMI", "ERLSTB", "Plasma/Blood/Serum", "257", |
271 | 3x |
"ERLSTD", "Last Meas Excretion Rate Norm by Dose", "ERLSTD", "Plasma/Blood/Serum", "258", |
272 | 3x |
"ERLSTS", "Last Meas Excretion Rate Norm by SA", "ERLSTS", "Plasma/Blood/Serum", "259", |
273 | 3x |
"ERLSTW", "Last Meas Excretion Rate Norm by WT", "ERLSTW", "Plasma/Blood/Serum", "260", |
274 | 3x |
"ERMAXB", "Max Excretion Rate Norm by BMI", "ERMAXB", "Plasma/Blood/Serum", "261", |
275 | 3x |
"ERMAXD", "Max Excretion Rate Norm by Dose", "ERMAXD", "Plasma/Blood/Serum", "262", |
276 | 3x |
"ERMAXS", "Max Excretion Rate Norm by SA", "ERMAXS", "Plasma/Blood/Serum", "263", |
277 | 3x |
"ERMAXW", "Max Excretion Rate Norm by WT", "ERMAXW", "Plasma/Blood/Serum", "264", |
278 | 3x |
"ERTLST", "Midpoint of Interval of Last Nonzero ER", "ERTLST", "Plasma/Blood/Serum", "265", |
279 | 3x |
"FABS", "Absolute Bioavailability", "FABS", "Plasma/Blood/Serum", "266", |
280 | 3x |
"FB", "Fraction Bound", "FB", "Plasma/Blood/Serum", "267", |
281 | 3x |
"FREL", "Relative Bioavailability", "FREL", "Plasma/Blood/Serum", "268", |
282 | 3x |
"FREXINT", "Fract Excr from T1 to T2", "FREXINT_T1_T2_UNIT", "Plasma/Blood/Serum", "269", |
283 | 3x |
"FU", "Fraction Unbound", "FU", "Plasma/Blood/Serum", "270", |
284 | 3x |
"HDCL", "Hemodialysis Clearance", "HDCL", "Plasma/Blood/Serum", "271", |
285 | 3x |
"HDER", "Hemodialysis Extraction Ratio", "HDER", "Plasma/Blood/Serum", "272", |
286 | 3x |
"HTMAX", "Half Tmax", "HTMAX", "Plasma/Blood/Serum", "273", |
287 | 3x |
"LAMZLTAU", "Lambda z Lower Limit TAU", "LAMZLTAU", "Plasma/Blood/Serum", "274", |
288 | 3x |
"LAMZNTAU", "Number of Points for Lambda z TAU", "LAMZNTAU", "Plasma/Blood/Serum", "275", |
289 | 3x |
"LAMZSPN", "Lambda z Span", "LAMZSPN", "Plasma/Blood/Serum", "276", |
290 | 3x |
"LAMZTAU", "Lambda z TAU", "LAMZTAU", "Plasma/Blood/Serum", "277", |
291 | 3x |
"LAMZUTAU", "Lambda z Upper Limit TAU", "LAMZUTAU", "Plasma/Blood/Serum", "278", |
292 | 3x |
"MAT", "Mean Absorption Time", "MAT", "Plasma/Blood/Serum", "279", |
293 | 3x |
"MRAUCIFO", "Metabolite Ratio for AUC Infinity Obs", "MRAUCIFO", "Plasma/Blood/Serum", "280", |
294 | 3x |
"MRAUCIFP", "Metabolite Ratio for AUC Infinity Pred", "MRAUCIFP", "Plasma/Blood/Serum", "281", |
295 | 3x |
"MRAUCINT", "Metabolite Ratio AUC from T1 to T2", "MRAUCINT_T1_T2_UNIT", "Plasma/Blood/Serum", "282", |
296 | 3x |
"MRAUCLST", "Metabolite Ratio AUC Last Nonzero Conc", "MRAUCLST", "Plasma/Blood/Serum", "283", |
297 | 3x |
"MRAUCTAU", "Metabolite Ratio for AUC Dosing Interval", "MRAUCTAU", "Plasma/Blood/Serum", "284", |
298 | 3x |
"MRCMAX", "Metabolite Ratio for Max Conc", "MRCMAX", "Plasma/Blood/Serum", "285", |
299 | 3x |
"MRTEVIFO", "MRT Extravasc Infinity Obs", "MRTEVIFO", "Plasma/Blood/Serum", "286", |
300 | 3x |
"MRTEVIFP", "MRT Extravasc Infinity Pred", "MRTEVIFP", "Plasma/Blood/Serum", "287", |
301 | 3x |
"MRTEVLST", "MRT Extravasc to Last Nonzero Conc", "MRTEVLST", "Plasma/Blood/Serum", "288", |
302 | 3x |
"MRTIVIFO", "MRT Intravasc Infinity Obs", "MRTIVIFO", "Plasma/Blood/Serum", "289", |
303 | 3x |
"MRTIVIFP", "MRT Intravasc Infinity Pred", "MRTIVIFP", "Plasma/Blood/Serum", "290", |
304 | 3x |
"MRTIVLST", "MRT Intravasc to Last Nonzero Conc", "MRTIVLST", "Plasma/Blood/Serum", "291", |
305 | 3x |
"NRENALCL", "Nonrenal CL", "NRENALCL", "Urine", "292", |
306 | 3x |
"NRENLCLB", "Nonrenal CL Norm by BMI", "NRENLCLB", "Urine", "293", |
307 | 3x |
"NRENLCLD", "Nonrenal CL Norm by Dose", "NRENLCLD", "Urine", "294", |
308 | 3x |
"NRENLCLS", "Nonrenal CL Norm by SA", "NRENLCLS", "Urine", "295", |
309 | 3x |
"NRENLCLW", "Nonrenal CL Norm by WT", "NRENLCLW", "Urine", "296", |
310 | 3x |
"PTROUGHR", "Peak Trough Ratio", "PTROUGHR", "Plasma/Blood/Serum", "297", |
311 | 3x |
"RAAUC", "Ratio AUC", "RAAUC", "Plasma/Blood/Serum", "298", |
312 | 3x |
"RAAUCIFO", "Ratio AUC Infinity Obs", "RAAUCIFO", "Plasma/Blood/Serum", "299", |
313 | 3x |
"RAAUCIFP", "Ratio AUC Infinity Pred", "RAAUCIFP", "Plasma/Blood/Serum", "300", |
314 | 3x |
"RACMAX", "Ratio CMAX", "RACMAX", "Plasma/Blood/Serum", "301", |
315 | 3x |
"RAMAXMIN", "Ratio of CMAX to CMIN", "RAMAXMIN", "Plasma/Blood/Serum", "302", |
316 | 3x |
"RCAMIFO", "Amt Rec Infinity Obs", "RCAMIFO", "Plasma/Blood/Serum", "303", |
317 | 3x |
"RCAMIFOB", "Amt Rec Infinity Obs Norm by BMI", "RCAMIFOB", "Plasma/Blood/Serum", "304", |
318 | 3x |
"RCAMIFOS", "Amt Rec Infinity Obs Norm by SA", "RCAMIFOS", "Plasma/Blood/Serum", "305", |
319 | 3x |
"RCAMIFOW", "Amt Rec Infinity Obs Norm by WT", "RCAMIFOW", "Plasma/Blood/Serum", "306", |
320 | 3x |
"RCAMIFP", "Amt Rec Infinity Pred", "RCAMIFP", "Plasma/Blood/Serum", "307", |
321 | 3x |
"RCAMIFPB", "Amt Rec Infinity Pred Norm by BMI", "RCAMIFPB", "Plasma/Blood/Serum", "308", |
322 | 3x |
"RCAMIFPS", "Amt Rec Infinity Pred Norm by SA", "RCAMIFPS", "Plasma/Blood/Serum", "309", |
323 | 3x |
"RCAMIFPW", "Amt Rec Infinity Pred Norm by WT", "RCAMIFPW", "Plasma/Blood/Serum", "310", |
324 | 3x |
"RCAMINTB", "Amt Rec from T1 to T2 Norm by BMI", "RCAMINTB_T1_T2_UNIT", "Plasma/Blood/Serum", "311", |
325 | 3x |
"RCAMINTS", "Amt Rec from T1 to T2 Norm by SA", "RCAMINTS_T1_T2_UNIT", "Plasma/Blood/Serum", "312", |
326 | 3x |
"RCAMINTW", "Amt Rec from T1 to T2 Norm by WT", "RCAMINTW_T1_T2_UNIT", "Plasma/Blood/Serum", "313", |
327 | 3x |
"RCAMTAU", "Amt Rec Over Dosing Interval", "RCAMTAU", "Plasma/Blood/Serum", "314", |
328 | 3x |
"RCAMTAUB", "Amt Rec Over Dosing Interval Norm by BMI", "RCAMTAUB", "Plasma/Blood/Serum", "315", |
329 | 3x |
"RCAMTAUS", "Amt Rec Over Dosing Interval Norm by SA", "RCAMTAUS", "Plasma/Blood/Serum", "316", |
330 | 3x |
"RCAMTAUW", "Amt Rec Over Dosing Interval Norm by WT", "RCAMTAUW", "Plasma/Blood/Serum", "317", |
331 | 3x |
"RCPCIFO", "Pct Rec Infinity Obs", "RCPCIFO", "Plasma/Blood/Serum", "318", |
332 | 3x |
"RCPCIFOB", "Pct Rec Infinity Obs Norm by BMI", "RCPCIFOB", "Plasma/Blood/Serum", "319", |
333 | 3x |
"RCPCIFOS", "Pct Rec Infinity Obs Norm by SA", "RCPCIFOS", "Plasma/Blood/Serum", "320", |
334 | 3x |
"RCPCIFOW", "Pct Rec Infinity Obs Norm by WT", "RCPCIFOW", "Plasma/Blood/Serum", "321", |
335 | 3x |
"RCPCIFP", "Pct Rec Infinity Pred", "RCPCIFP", "Plasma/Blood/Serum", "322", |
336 | 3x |
"RCPCIFPB", "Pct Rec Infinity Pred Norm by BMI", "RCPCIFPB", "Plasma/Blood/Serum", "323", |
337 | 3x |
"RCPCIFPS", "Pct Rec Infinity Pred Norm by SA", "RCPCIFPS", "Plasma/Blood/Serum", "324", |
338 | 3x |
"RCPCIFPW", "Pct Rec Infinity Pred Norm by WT", "RCPCIFPW", "Plasma/Blood/Serum", "325", |
339 | 3x |
"RCPCINTB", "Pct Rec from T1 to T2 Norm by BMI", "RCPCINTB_T1_T2_UNIT", "Plasma/Blood/Serum", "326", |
340 | 3x |
"RCPCINTS", "Pct Rec from T1 to T2 Norm by SA", "RCPCINTS_T1_T2_UNIT", "Plasma/Blood/Serum", "327", |
341 | 3x |
"RCPCINTW", "Pct Rec from T1 to T2 Norm by WT", "RCPCINTW_T1_T2_UNIT", "Plasma/Blood/Serum", "328", |
342 | 3x |
"RCPCLST", "Pct Rec to Last Nonzero Conc", "RCPCLST", "Plasma/Blood/Serum", "329", |
343 | 3x |
"RCPCTAU", "Pct Rec Over Dosing Interval", "RCPCTAU", "Plasma/Blood/Serum", "330", |
344 | 3x |
"RCPCTAUB", "Pct Rec Over Dosing Interval Norm by BMI", "RCPCTAUB", "Plasma/Blood/Serum", "331", |
345 | 3x |
"RCPCTAUS", "Pct Rec Over Dosing Interval Norm by SA", "RCPCTAUS", "Plasma/Blood/Serum", "332", |
346 | 3x |
"RCPCTAUW", "Pct Rec Over Dosing Interval Norm by WT", "RCPCTAUW", "Plasma/Blood/Serum", "333", |
347 | 3x |
"RENALCLB", "Renal CL Norm by BMI", "RENALCLB", "Urine", "334", |
348 | 3x |
"RENALCLD", "Renal CL Norm by Dose", "RENALCLD", "Urine", "335", |
349 | 3x |
"RENALCLS", "Renal CL Norm by SA", "RENALCLS", "Urine", "336", |
350 | 3x |
"RENALCLW", "Renal CL Norm by WT", "RENALCLW", "Urine", "337", |
351 | 3x |
"RENCLTAU", "Renal CL for Dose Int", "RENCLTAU", "Urine", "338", |
352 | 3x |
"RNCLINT", "Renal CL from T1 to T2", "RNCLINT_T1_T2_UNIT", "Urine", "339", |
353 | 3x |
"RNCLINTB", "Renal CL from T1 to T2 Norm by BMI", "RNCLINTB_T1_T2_UNIT", "Urine", "340", |
354 | 3x |
"RNCLINTD", "Renal CL from T1 to T2 Norm by Dose", "RNCLINTD_T1_T2_UNIT", "Urine", "341", |
355 | 3x |
"RNCLINTS", "Renal CL from T1 to T2 Norm by SA", "RNCLINTS_T1_T2_UNIT", "Urine", "342", |
356 | 3x |
"RNCLINTW", "Renal CL from T1 to T2 Norm by WT", "RNCLINTW_T1_T2_UNIT", "Urine", "343", |
357 | 3x |
"RNCLTAUB", "Renal CL for Dose Int Norm by BMI", "RNCLTAUB", "Urine", "344", |
358 | 3x |
"RNCLTAUD", "Renal CL for Dose Int Norm by Dose", "RNCLTAUD", "Urine", "345", |
359 | 3x |
"RNCLTAUS", "Renal CL for Dose Int Norm by SA", "RNCLTAUS", "Urine", "346", |
360 | 3x |
"RNCLTAUW", "Renal CL for Dose Int Norm by WT", "RNCLTAUW", "Urine", "347", |
361 | 3x |
"RNCLUB", "Renal CL for Unbound Drug", "RNCLUB", "Urine", "348", |
362 | 3x |
"SRAUC", "Stationarity Ratio AUC", "SRAUC", "Plasma/Blood/Serum", "349", |
363 | 3x |
"SWING", "Swing", "SWING", "Plasma/Blood/Serum", "350", |
364 | 3x |
"TAUHL", "Half-Life TAU", "TAUHL", "Plasma/Blood/Serum", "351", |
365 | 3x |
"TBBL", "Time Below Baseline", "Time_Below_B", "Plasma/Blood/Serum", "352", |
366 | 3x |
"TROUGHPR", "Trough Peak Ratio", "TROUGHPR", "Plasma/Blood/Serum", "353", |
367 | 3x |
"V0", "Vol Dist Initial", "V0", "Plasma/Blood/Serum", "354", |
368 | 3x |
"V0B", "Vol Dist Initial Norm by BMI", "V0B", "Plasma/Blood/Serum", "355", |
369 | 3x |
"V0D", "Vol Dist Initial Norm by Dose", "V0D", "Plasma/Blood/Serum", "356", |
370 | 3x |
"V0S", "Vol Dist Initial Norm by SA", "V0S", "Plasma/Blood/Serum", "357", |
371 | 3x |
"V0W", "Vol Dist Initial Norm by WT", "V0W", "Plasma/Blood/Serum", "358", |
372 | 3x |
"VSSOB", "Vol Dist Steady State Obs Norm by BMI", "VSSOB", "Plasma/Blood/Serum", "359", |
373 | 3x |
"VSSOBD", "Vol Dist Steady State Obs by B", "VSSOBD", "Plasma/Blood/Serum", "360", |
374 | 3x |
"VSSOD", "Vol Dist Steady State Obs Norm by Dose", "VSSOD", "Plasma/Blood/Serum", "361", |
375 | 3x |
"VSSOF", "Vol Dist Steady State Obs by F", "VSSOF", "Plasma/Blood/Serum", "362", |
376 | 3x |
"VSSOS", "Vol Dist Steady State Obs Norm by SA", "VSSOS", "Plasma/Blood/Serum", "363", |
377 | 3x |
"VSSOUB", "Vol Dist Steady State Obs by UB", "VSSOUB", "Plasma/Blood/Serum", "364", |
378 | 3x |
"VSSOW", "Vol Dist Steady State Obs Norm by WT", "VSSOW", "Plasma/Blood/Serum", "365", |
379 | 3x |
"VSSPB", "Vol Dist Steady State Pred Norm by BMI", "VSSPB", "Plasma/Blood/Serum", "366", |
380 | 3x |
"VSSPBD", "Vol Dist Steady State Pred by B", "VSSPBD", "Plasma/Blood/Serum", "367", |
381 | 3x |
"VSSPD", "Vol Dist Steady State Pred Norm by Dose", "VSSPD", "Plasma/Blood/Serum", "368", |
382 | 3x |
"VSSPF", "Vol Dist Steady State Pred by F", "VSSPF", "Plasma/Blood/Serum", "369", |
383 | 3x |
"VSSPS", "Vol Dist Steady State Pred Norm by SA", "VSSPS", "Plasma/Blood/Serum", "370", |
384 | 3x |
"VSSPUB", "Vol Dist Steady State Pred by UB", "VSSPUB", "Plasma/Blood/Serum", "371", |
385 | 3x |
"VSSPW", "Vol Dist Steady State Pred Norm by WT", "VSSPW", "Plasma/Blood/Serum", "372", |
386 | 3x |
"VZ", "Vol Z", "Vz", "Plasma/Blood/Serum", "373", |
387 | 3x |
"VZF", "Vol Z by F", "Vz_F", "Plasma/Blood/Serum", "374", |
388 | 3x |
"VZFOB", "Vz Obs by F Norm by BMI", "VZFOB", "Plasma/Blood/Serum", "375", |
389 | 3x |
"VZFOD", "Vz Obs by F Norm by Dose", "VZFOD", "Plasma/Blood/Serum", "376", |
390 | 3x |
"VZFOS", "Vz Obs by F Norm by SA", "VZFOS", "Plasma/Blood/Serum", "377", |
391 | 3x |
"VZFOUB", "Vz Obs by F for UB", "VZFOUB", "Plasma/Blood/Serum", "378", |
392 | 3x |
"VZFOW", "Vz Obs by F Norm by WT", "VZFOW", "Plasma/Blood/Serum", "379", |
393 | 3x |
"VZFPB", "Vz Pred by F Norm by BMI", "VZFPB", "Plasma/Blood/Serum", "380", |
394 | 3x |
"VZFPD", "Vz Pred by F Norm by Dose", "VZFPD", "Plasma/Blood/Serum", "381", |
395 | 3x |
"VZFPS", "Vz Pred by F Norm by SA", "VZFPS", "Plasma/Blood/Serum", "382", |
396 | 3x |
"VZFPUB", "Vz Pred by F for UB", "VZFPUB", "Plasma/Blood/Serum", "383", |
397 | 3x |
"VZFPW", "Vz Pred by F Norm by WT", "VZFPW", "Plasma/Blood/Serum", "384", |
398 | 3x |
"VZFTAU", "Vz for Dose Int by F", "VZFTAU", "Plasma/Blood/Serum", "385", |
399 | 3x |
"VZFTAUB", "Vz for Dose Int by F Norm by BMI", "VZFTAUB", "Plasma/Blood/Serum", "386", |
400 | 3x |
"VZFTAUD", "Vz for Dose Int by F Norm by Dose", "VZFTAUD", "Plasma/Blood/Serum", "387", |
401 | 3x |
"VZFTAUS", "Vz for Dose Int by F Norm by SA", "VZFTAUS", "Plasma/Blood/Serum", "388", |
402 | 3x |
"VZFTAUW", "Vz for Dose Int by F Norm by WT", "VZFTAUW", "Plasma/Blood/Serum", "389", |
403 | 3x |
"VZOB", "Vz Obs Norm by BMI", "VZOB", "Plasma/Blood/Serum", "390", |
404 | 3x |
"VZOD", "Vz Obs Norm by Dose", "VZOD", "Plasma/Blood/Serum", "391", |
405 | 3x |
"VZOS", "Vz Obs Norm by SA", "VZOS", "Plasma/Blood/Serum", "392", |
406 | 3x |
"VZOUB", "Vz Obs for UB", "VZOUB", "Plasma/Blood/Serum", "393", |
407 | 3x |
"VZOW", "Vz Obs Norm by WT", "VZOW", "Plasma/Blood/Serum", "394", |
408 | 3x |
"VZPB", "Vz Pred Norm by BMI", "VZPB", "Plasma/Blood/Serum", "395", |
409 | 3x |
"VZPD", "Vz Pred Norm by Dose", "VZPD", "Plasma/Blood/Serum", "396", |
410 | 3x |
"VZPS", "Vz Pred Norm by SA", "VZPS", "Plasma/Blood/Serum", "397", |
411 | 3x |
"VZPUB", "Vz Pred for UB", "VZPUB", "Plasma/Blood/Serum", "398" |
412 |
),
|
|
413 | 3x |
ncol = 5, |
414 | 3x |
byrow = TRUE |
415 |
)) |
|
416 | 3x |
colnames(pk_dataset) <- c("PARAMCD", "PARAM", "TLG_DISPLAY", "MATRIX", "TLG_ORDER") |
417 | 3x |
pk_dataset
|
418 |
}
|
1 |
#' Summarize Variables in Columns
|
|
2 |
#'
|
|
3 |
#' @description `r lifecycle::badge("stable")`
|
|
4 |
#'
|
|
5 |
#' This analyze function uses the S3 generic function [s_summary()] to summarize different variables
|
|
6 |
#' that are arranged in columns. Additional standard formatting arguments are available. It is a
|
|
7 |
#' minimal wrapper for [rtables::analyze_colvars()]. The latter function is meant to add different
|
|
8 |
#' analysis methods for each column variables as different rows. To have the analysis methods as
|
|
9 |
#' column labels, please refer to [analyze_vars_in_cols()].
|
|
10 |
#'
|
|
11 |
#' @inheritParams argument_convention
|
|
12 |
#' @param ... arguments passed to `s_summary()`.
|
|
13 |
#' @param .indent_mods (named `vector` of `integer`)\cr indent modifiers for the labels. Each element of the vector
|
|
14 |
#' should be a name-value pair with name corresponding to a statistic specified in `.stats` and value the indentation
|
|
15 |
#' for that statistic's row label.
|
|
16 |
#'
|
|
17 |
#' @return
|
|
18 |
#' A layout object suitable for passing to further layouting functions, or to [rtables::build_table()].
|
|
19 |
#' Adding this function to an `rtable` layout will summarize the given variables, arrange the output
|
|
20 |
#' in columns, and add it to the table layout.
|
|
21 |
#'
|
|
22 |
#' @seealso [rtables::split_cols_by_multivar()] and [`analyze_colvars_functions`].
|
|
23 |
#'
|
|
24 |
#' @examples
|
|
25 |
#' dta_test <- data.frame(
|
|
26 |
#' USUBJID = rep(1:6, each = 3),
|
|
27 |
#' PARAMCD = rep("lab", 6 * 3),
|
|
28 |
#' AVISIT = rep(paste0("V", 1:3), 6),
|
|
29 |
#' ARM = rep(LETTERS[1:3], rep(6, 3)),
|
|
30 |
#' AVAL = c(9:1, rep(NA, 9)),
|
|
31 |
#' CHG = c(1:9, rep(NA, 9))
|
|
32 |
#' )
|
|
33 |
#'
|
|
34 |
#' ## Default output within a `rtables` pipeline.
|
|
35 |
#' basic_table() %>%
|
|
36 |
#' split_cols_by("ARM") %>%
|
|
37 |
#' split_rows_by("AVISIT") %>%
|
|
38 |
#' split_cols_by_multivar(vars = c("AVAL", "CHG")) %>%
|
|
39 |
#' summarize_colvars() %>%
|
|
40 |
#' build_table(dta_test)
|
|
41 |
#'
|
|
42 |
#' ## Selection of statistics, formats and labels also work.
|
|
43 |
#' basic_table() %>%
|
|
44 |
#' split_cols_by("ARM") %>%
|
|
45 |
#' split_rows_by("AVISIT") %>%
|
|
46 |
#' split_cols_by_multivar(vars = c("AVAL", "CHG")) %>%
|
|
47 |
#' summarize_colvars(
|
|
48 |
#' .stats = c("n", "mean_sd"),
|
|
49 |
#' .formats = c("mean_sd" = "xx.x, xx.x"),
|
|
50 |
#' .labels = c(n = "n", mean_sd = "Mean, SD")
|
|
51 |
#' ) %>%
|
|
52 |
#' build_table(dta_test)
|
|
53 |
#'
|
|
54 |
#' ## Use arguments interpreted by `s_summary`.
|
|
55 |
#' basic_table() %>%
|
|
56 |
#' split_cols_by("ARM") %>%
|
|
57 |
#' split_rows_by("AVISIT") %>%
|
|
58 |
#' split_cols_by_multivar(vars = c("AVAL", "CHG")) %>%
|
|
59 |
#' summarize_colvars(na.rm = FALSE) %>%
|
|
60 |
#' build_table(dta_test)
|
|
61 |
#'
|
|
62 |
#' @export
|
|
63 |
summarize_colvars <- function(lyt, |
|
64 |
...,
|
|
65 |
.stats = c("n", "mean_sd", "median", "range", "count_fraction"), |
|
66 |
.formats = NULL, |
|
67 |
.labels = NULL, |
|
68 |
.indent_mods = NULL) { |
|
69 | 3x |
afun <- create_afun_summary(.stats, .formats, .labels, .indent_mods) |
70 | ||
71 | 3x |
analyze_colvars( |
72 | 3x |
lyt,
|
73 | 3x |
afun = afun, |
74 | 3x |
extra_args = list(...) |
75 |
)
|
|
76 |
}
|