-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ard_categorical.survey.design.R
599 lines (554 loc) · 20.8 KB
/
ard_categorical.survey.design.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
#' ARD Categorical Survey Statistics
#'
#' @description
#' Compute tabulations on survey-weighted data.
#'
#' The counts and proportion (`"N"`, `"n"`, `"p"`) are calculated using `survey::svytable()`,
#' and the standard errors and design effect (`"p.std.error"`, `"deff"`) are
#' calculated using `survey::svymean()`.
#'
#' The unweighted statistics are calculated with `cards::ard_categorical.data.frame()`.
#'
#' @param data (`survey.design`)\cr
#' a design object often created with [`survey::svydesign()`].
#' @param variables ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
#' columns to include in summaries.
#' @param by ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
#' results are calculated for **all combinations** of the column specified
#' and the variables. A single column may be specified.
#' @param denominator (`string`)\cr
#' a string indicating the type proportions to calculate. Must be one of
#' `"column"` (the default), `"row"`, and `"cell"`.
#' @param statistic ([`formula-list-selector`][cards::syntax])\cr
#' a named list, a list of formulas,
#' or a single formula where the list element is a character vector of
#' statistic names to include. See default value for options.
#' @param fmt_fn ([`formula-list-selector`][cards::syntax])\cr
#' a named list, a list of formulas,
#' or a single formula where the list element is a named list of functions
#' (or the RHS of a formula),
#' e.g. `list(mpg = list(mean = \(x) round(x, digits = 2) |> as.character()))`.
#' @param stat_label ([`formula-list-selector`][cards::syntax])\cr
#' a named list, a list of formulas, or a single formula where
#' the list element is either a named list or a list of formulas defining the
#' statistic labels, e.g. `everything() ~ list(mean = "Mean", sd = "SD")` or
#' `everything() ~ list(mean ~ "Mean", sd ~ "SD")`.
#' @inheritParams rlang::args_dots_empty
#'
#' @return an ARD data frame of class 'card'
#' @export
#'
#' @examplesIf cardx:::is_pkg_installed("survey")
#' svy_titanic <- survey::svydesign(~1, data = as.data.frame(Titanic), weights = ~Freq)
#'
#' ard_categorical(svy_titanic, variables = c(Class, Age), by = Survived)
ard_categorical.survey.design <- function(data,
variables,
by = NULL,
statistic = everything() ~ c("n", "N", "p", "p.std.error", "deff", "n_unweighted", "N_unweighted", "p_unweighted"),
denominator = c("column", "row", "cell"),
fmt_fn = NULL,
stat_label = everything() ~ list(
p = "%",
p.std.error = "SE(%)",
deff = "Design Effect",
"n_unweighted" = "Unweighted n",
"N_unweighted" = "Unweighted N",
"p_unweighted" = "Unweighted %"
),
...) {
set_cli_abort_call()
check_pkg_installed(pkg = "survey")
check_dots_empty()
deff <- TRUE # we may update in the future to make this an argument for users
# process arguments ----------------------------------------------------------
check_not_missing(variables)
cards::process_selectors(
data = data$variables,
variables = {{ variables }},
by = {{ by }}
)
variables <- setdiff(variables, by)
check_scalar(by, allow_empty = TRUE)
# return empty ARD if no variables selected ----------------------------------
if (is_empty(variables)) {
return(dplyr::tibble() |> cards::as_card())
}
check_na_factor_levels(data$variables, c(by, variables))
cards::process_formula_selectors(
data = data$variables[variables],
statistic = statistic,
fmt_fn = fmt_fn,
stat_label = stat_label
)
cards::fill_formula_selectors(
data = data$variables[variables],
statistic = formals(asNamespace("cardx")[["ard_categorical.survey.design"]])[["statistic"]] |> eval(),
)
accepted_svy_stats <- c("n", "N", "p", "p.std.error", "deff", "n_unweighted", "N_unweighted", "p_unweighted")
cards::check_list_elements(
x = statistic,
predicate = \(x) all(x %in% accepted_svy_stats),
error_msg = c("Error in the values of the {.arg statistic} argument.",
i = "Values must be in {.val {accepted_svy_stats}}"
)
)
denominator <- arg_match(denominator)
# check the missingness
walk(
variables,
\(.x) {
if (all(is.na(data$variables[[.x]])) &&
!inherits(data$variables[[.x]], "factor")) {
cli::cli_abort(
c("Column {.val {.x}} is all missing and cannot be tabulated.",
i = "Only columns of class {.cls factor} can be tabulated when all values are missing."
),
call = get_cli_abort_call()
)
}
}
)
# return note about column names that result in errors -----------------------
if (any(by %in% c("variable", "variable_level", "group1_level", "p", "n"))) {
cli::cli_abort(
"The {.arg by} argument cannot include variables named {.val {c('variable', 'variable_level', 'group1_level', 'p', 'n')}}.",
call = get_cli_abort_call()
)
}
if (any(variables %in% c("by", "name", "n", "p", "p.std.error"))) {
cli::cli_abort(
"The {.arg variables} argument cannot include variables named {.val {c('by', 'name', 'n', 'p', 'p.std.error')}}.",
call = get_cli_abort_call()
)
}
# calculate counts -----------------------------------------------------------
# this tabulation accounts for unobserved combinations
svytable_counts <- .svytable_counts(data, variables, by, denominator)
# calculate rate SE and DEFF -------------------------------------------------
svytable_rates <- .svytable_rate_stats(data, variables, by, denominator, deff)
# convert results into a proper ARD object -----------------------------------
cards <-
svytable_counts |>
# merge in the SE(p) and DEFF
dplyr::left_join(
svytable_rates |> dplyr::select(-"p"),
by = intersect(c("group1", "group1_level", "variable", "variable_level"), names(svytable_counts))
) |>
# make columns list columns
dplyr::mutate(across(-any_of(c("group1", "variable")), as.list)) |>
tidyr::pivot_longer(
cols = -c(cards::all_ard_groups(), cards::all_ard_variables()),
names_to = "stat_name",
values_to = "stat"
) |>
# keep statistics requested by user
dplyr::inner_join(
statistic |> enframe("variable", "stat_name") |> tidyr::unnest(cols = "stat_name"),
by = c("variable", "stat_name")
)
# add unweighted statistics --------------------------------------------------
statistic_unweighted <- statistic |>
lapply(\(x) keep(x, ~ endsWith(.x, "_unweighted")) |> str_remove("_unweighted$")) |>
compact()
if (!is_empty(statistic_unweighted)) {
cards_unweighted <-
ard_categorical(
data = data[["variables"]],
variables = all_of(names(statistic_unweighted)),
by = any_of(by),
statistic = statistic_unweighted,
denominator = denominator
) |>
# all the survey levels are reported as character, so we do the same here.
dplyr::mutate(
across(
c(cards::all_ard_groups("levels"), cards::all_ard_variables("levels")),
~ map(.x, as.character)
)
) |>
dplyr::select(-c("stat_label", "fmt_fn", "warning", "error")) |>
dplyr::mutate(
stat_name =
dplyr::case_match(.data$stat_name, "n" ~ "n_unweighted", "N" ~ "N_unweighted", "p" ~ "p_unweighted")
)
cards <- cards |> dplyr::bind_rows(cards_unweighted) # styler: off
}
# final processing of fmt_fn -------------------------------------------------
cards <- cards |>
.process_nested_list_as_df(
arg = fmt_fn,
new_column = "fmt_fn"
) |>
.default_svy_cat_fmt_fn()
# merge in statistic labels --------------------------------------------------
cards <- cards |>
.process_nested_list_as_df(
arg = stat_label,
new_column = "stat_label",
unlist = TRUE
) |>
dplyr::mutate(stat_label = dplyr::coalesce(.data$stat_label, .data$stat_name))
# return final object --------------------------------------------------------
cards |>
.restore_original_column_types(data = data$variables) |>
dplyr::mutate(
context = "categorical",
warning = list(NULL),
error = list(NULL),
) |>
cards::as_card() |>
cards::tidy_ard_column_order() |>
cards::tidy_ard_row_order()
}
# check for functions with NA factor levels (these are not allowed)
check_na_factor_levels <- function(data, variables) {
walk(
variables,
\(variable) {
if (is.factor(data[[variable]]) && any(is.na(levels(data[[variable]])))) {
cli::cli_abort(
"Column {.val {variable}} is a factor with {.val {NA}} levels, which are not allowed.",
call = get_cli_abort_call()
)
}
}
)
}
# this function returns a tibble with the SE(p) and DEFF
.svytable_rate_stats <- function(data, variables, by, denominator, deff) {
if (!is_empty(by)) by_lvls <- .unique_values_sort(data$variables, by) # styler: off
if (!is_empty(by) && length(by_lvls) == 1L) {
data$variables[[by]] <-
case_switch(
inherits(data$variables[[by]], "factor") ~ fct_expand(data$variables[[by]], paste("not", by_lvls)),
.default = factor(data$variables[[by]], levels = c(by_lvls, paste("not", by_lvls)))
)
}
if (!is_empty(by) && inherits(data$variables[[by]], "logical")) {
data$variables[[by]] <- factor(data$variables[[by]], levels = c(TRUE, FALSE))
}
if (!is_empty(by) && !inherits(data$variables[[by]], "factor")) {
data$variables[[by]] <- factor(data$variables[[by]])
}
lapply(
variables,
\(variable) {
# convert the variable to a factor if not already one or a lgl, so we get the correct rate stats from svymean
if (!inherits(data$variables[[variable]], c("factor", "logical"))) {
data$variables[[variable]] <- factor(data$variables[[variable]])
}
# there are issues with svymean() when a variable has only one level. adding a second as needed
variable_lvls <- .unique_values_sort(data$variables, variable)
if (length(variable_lvls) == 1L) {
data$variables[[variable]] <-
case_switch(
inherits(data$variables[[variable]], "factor") ~ fct_expand(data$variables[[variable]], paste("not", variable_lvls)),
.default = factor(data$variables[[variable]], levels = c(variable_lvls, paste("not", variable_lvls)))
)
}
if (inherits(data$variables[[variable]], "logical")) {
data$variables[[variable]] <- factor(data$variables[[variable]], levels = c(TRUE, FALSE))
}
if (!inherits(data$variables[[variable]], "factor")) {
data$variables[[variable]] <- factor(data$variables[[variable]])
}
# each combination of denominator and whether there is a by variable is handled separately
result <-
case_switch(
# by variable and column percentages
!is_empty(by) && denominator == "column" ~
.one_svytable_rates_by_column(data, variable, by, deff),
# by variable and row percentages
!is_empty(by) && denominator == "row" ~
.one_svytable_rates_by_row(data, variable, by, deff),
# by variable and cell percentages
!is_empty(by) && denominator == "cell" ~
.one_svytable_rates_by_cell(data, variable, by, deff),
# no by variable and column/cell percentages
denominator %in% c("column", "cell") ~
.one_svytable_rates_no_by_column_and_cell(data, variable, deff),
# no by variable and row percentages
denominator == "row" ~
.one_svytable_rates_no_by_row(data, variable, deff)
)
# if a level was added, remove the fake level
if (length(variable_lvls) == 1L) {
result <- result |> dplyr::filter(.data$variable_level %in% variable_lvls)
}
if (!is_empty(by) && length(by_lvls) == 1L) {
result <- result |> dplyr::filter(.data$group1_level %in% by_lvls)
}
result
}
) |>
dplyr::bind_rows()
}
.one_svytable_rates_no_by_row <- function(data, variable, deff) {
dplyr::tibble(
variable = .env$variable,
variable_level = unique(data$variables[[variable]]) |> sort() |> as.character(),
p = 1,
p.std.error = 0,
deff = NaN
)
}
.one_svytable_rates_no_by_column_and_cell <- function(data, variable, deff) {
survey::svymean(reformulate2(variable), design = data, na.rm = TRUE, deff = deff) |>
dplyr::as_tibble(rownames = "var_level") |>
dplyr::mutate(
variable_level = str_remove(.data$var_level, pattern = paste0("^", .env$variable)),
variable = .env$variable
) |>
dplyr::select("variable", "variable_level", p = "mean", p.std.error = "SE", any_of("deff"))
}
.one_svytable_rates_by_cell <- function(data, variable, by, deff) {
df_interaction_id <-
.df_all_combos(data, variable, by) |>
dplyr::mutate(
var_level =
glue::glue("interaction({.env$by}, {.env$variable}){.data$group1_level}.{.data$variable_level}")
)
survey::svymean(
x = inject(~ interaction(!!sym(bt(by)), !!sym(bt(variable)))),
design = data,
na.rm = TRUE,
deff = deff
) |>
dplyr::as_tibble(rownames = "var_level") |>
dplyr::left_join(df_interaction_id, by = "var_level") |>
dplyr::select(
cards::all_ard_groups(), cards::all_ard_variables(),
p = "mean", p.std.error = "SE", any_of("deff")
)
}
.one_svytable_rates_by_row <- function(data, variable, by, deff) {
survey::svyby(
formula = reformulate2(by),
by = reformulate2(variable),
design = data,
FUN = survey::svymean,
na.rm = TRUE,
deff = deff
) |>
dplyr::as_tibble() |>
tidyr::pivot_longer(-all_of(variable)) |>
dplyr::mutate(
stat =
dplyr::case_when(
startsWith(.data$name, paste0("se.", by)) | startsWith(.data$name, paste0("se.`", by, "`")) ~ "p.std.error",
startsWith(.data$name, paste0("DEff.", by)) | startsWith(.data$name, paste0("DEff.`", by, "`")) ~ "deff",
TRUE ~ "p"
),
name =
str_remove_all(.data$name, "se\\.") %>%
str_remove_all("DEff\\.") %>%
str_remove_all(by) %>%
str_remove_all("`")
) |>
tidyr::pivot_wider(names_from = "stat", values_from = "value") |>
set_names(c("variable_level", "group1_level", "p", "p.std.error", "deff")) |>
dplyr::mutate(
group1 = .env$by,
variable = .env$variable,
across(c("group1_level", "variable_level"), as.character)
)
}
.one_svytable_rates_by_column <- function(data, variable, by, deff) {
survey::svyby(
formula = reformulate2(variable),
by = reformulate2(by),
design = data,
FUN = survey::svymean,
na.rm = TRUE,
deff = deff
) |>
dplyr::as_tibble() |>
tidyr::pivot_longer(-all_of(by)) |>
dplyr::mutate(
stat =
dplyr::case_when(
startsWith(.data$name, paste0("se.", variable)) | startsWith(.data$name, paste0("se.`", variable, "`")) ~ "p.std.error",
startsWith(.data$name, paste0("DEff.", variable)) | startsWith(.data$name, paste0("DEff.`", variable, "`")) ~ "deff",
TRUE ~ "p"
),
name =
str_remove_all(.data$name, "se\\.") %>%
str_remove_all("DEff\\.") %>%
str_remove_all(variable) %>%
str_remove_all("`")
) |>
tidyr::pivot_wider(names_from = "stat", values_from = "value") |>
set_names(c("group1_level", "variable_level", "p", "p.std.error", "deff")) |>
dplyr::mutate(
group1 = .env$by,
variable = .env$variable,
across(c("group1_level", "variable_level"), as.character)
)
}
.svytable_counts <- function(data, variables, by, denominator) {
df_counts <-
lapply(
variables,
\(variable) {
# perform weighted tabulation
df_count <-
survey::svytable(formula = reformulate2(c(by, variable)), design = data) |>
dplyr::as_tibble()
if (is_empty(by)) {
names(df_count) <- c("variable_level", "n")
df_count$variable <- variable
} else {
names(df_count) <- c("group1_level", "variable_level", "n")
df_count$variable <- variable
df_count$group1 <- by
}
# adding unobserved levels
.df_all_combos(data, variable, by) %>%
dplyr::left_join(
df_count,
by = names(.)
) |>
tidyr::replace_na(list(n = 0)) # unobserved levels assigned zero count
}
) |>
dplyr::bind_rows()
# add big N and p, then return data frame of results
switch(denominator,
"column" =
df_counts |>
dplyr::mutate(
.by = c(cards::all_ard_groups(), cards::all_ard_variables("names")),
N = sum(.data$n),
p = .data$n / .data$N
),
"row" =
df_counts |>
dplyr::mutate(
.by = cards::all_ard_variables(),
N = sum(.data$n),
p = .data$n / .data$N
),
"cell" =
df_counts |>
dplyr::mutate(
.by = c(cards::all_ard_groups("names"), cards::all_ard_variables("names")),
N = sum(.data$n),
p = .data$n / .data$N
)
)
}
.df_all_combos <- function(data, variable, by) {
df <-
tidyr::expand_grid(
group1_level = switch(!is_empty(by),
.unique_and_sorted(data$variables[[by]])
),
variable_level = .unique_and_sorted(data$variables[[variable]])
) |>
dplyr::mutate(variable = .env$variable)
if (!is_empty(by)) df$group1 <- by
df <- dplyr::relocate(df, any_of(c("group1", "group1_level", "variable", "variable_level")))
# convert levels to character for merging later
df |>
dplyr::mutate(
across(
c(cards::all_ard_groups("levels"), cards::all_ard_variables("levels")),
as.character
)
)
}
case_switch <- function(..., .default = NULL) {
dots <- dots_list(...)
for (f in dots) {
if (isTRUE(eval(f_lhs(f), envir = attr(f, ".Environment")))) {
return(eval(f_rhs(f), envir = attr(f, ".Environment")))
}
}
return(.default)
}
.default_svy_cat_fmt_fn <- function(x) {
x |>
dplyr::mutate(
fmt_fn =
pmap(
list(.data$stat_name, .data$stat, .data$fmt_fn),
function(stat_name, stat, fmt_fn) {
if (!is_empty(fmt_fn)) {
return(fmt_fn)
}
if (stat_name %in% c("p", "p_miss", "p_nonmiss", "p_unweighted")) {
return(cards::label_cards(digits = 1, scale = 100))
}
if (stat_name %in% c("n", "N", "N_miss", "N_nonmiss", "N_obs", "n_unweighted", "N_unweighted")) {
return(cards::label_cards(digits = 0))
}
if (is.integer(stat)) {
return(0L)
}
if (is.numeric(stat)) {
return(1L)
}
return(as.character)
}
)
)
}
#' Convert Nested Lists to Column
#'
#' Some arguments, such as `stat_label`, are passed as nested lists. This
#' function properly unnests these lists and adds them to the results data frame.
#'
#' @param x (`data.frame`)\cr
#' result data frame
#' @param arg (`list`)\cr
#' the nested list
#' @param new_column (`string`)\cr
#' new column name
#' @param unlist (`logical`)\cr
#' whether to fully unlist final results
#'
#' @return a data frame
#' @keywords internal
#'
#' @examples
#' ard <- ard_categorical(cards::ADSL, by = "ARM", variables = "AGEGR1")
#'
#' cardx:::.process_nested_list_as_df(ard, NULL, "new_col")
.process_nested_list_as_df <- function(x, arg, new_column, unlist = FALSE) {
# add fmt_fn column if not already present
if (!new_column %in% names(x)) {
x[[new_column]] <- list(NULL)
}
# process argument if not NULL, and update new column
if (!is_empty(arg)) {
df_argument <-
imap(
arg,
function(enlst_arg, variable) {
lst_stat_names <-
x[c("variable", "stat_name")] |>
dplyr::filter(.data$variable %in% .env$variable) |>
unique() %>%
{stats::setNames(as.list(.[["stat_name"]]), .[["stat_name"]])} # styler: off
cards::compute_formula_selector(
data = lst_stat_names,
x = enlst_arg
) %>%
# styler: off
{dplyr::tibble(
variable = variable,
stat_name = names(.),
"{new_column}" := unname(.)
)}
# styler: on
}
) |>
dplyr::bind_rows()
x <- x |> dplyr::rows_update(df_argument, by = c("variable", "stat_name"), unmatched = "ignore")
}
if (isTRUE(unlist)) {
x[[new_column]] <- lapply(x[[new_column]], function(x) x %||% NA) |> unlist()
}
x
}