Skip to contents

[Stable]

Usage

h_append_grade_groups(grade_groups, refs, remove_single = TRUE)

s_count_occurrences_by_grade(
  df,
  .var,
  .N_col,
  id = "USUBJID",
  grade_groups = list(),
  remove_single = TRUE,
  labelstr = ""
)

a_count_occurrences_by_grade(
  df,
  .var,
  .N_col,
  id = "USUBJID",
  grade_groups = list(),
  remove_single = TRUE,
  labelstr = ""
)

count_occurrences_by_grade(
  lyt,
  var,
  var_labels = var,
  show_labels = "default",
  ...,
  table_names = var,
  .stats = NULL,
  .formats = NULL,
  .indent_mods = NULL,
  .labels = NULL
)

summarize_occurrences_by_grade(
  lyt,
  var,
  ...,
  .stats = NULL,
  .formats = NULL,
  .indent_mods = NULL,
  .labels = NULL
)

Arguments

grade_groups

(named list of character)
containing groupings of grades.

refs

(named list of numeric)
where each name corresponds to a reference grade level and each entry represents a count.

remove_single

(logical)
TRUE to not include the elements of one-element grade groups in the the output list; in this case only the grade groups names will be included in the output.

df

(data frame)
data set containing all analysis variables.

.var, var

(string)
single variable name that is passed by rtables when requested by a statistics function.

.N_col

(count)
row-wise N (row group count) for the group of observations being analyzed (i.e. with no column-based subsetting) that is passed by rtables.

id

(string)
subject variable name.

labelstr

(character)
label of the level of the parent split currently being summarized (must be present as second argument in Content Row Functions).

lyt

(layout)
input layout where analyses will be added to.

var_labels

(character)
labels to show in the result table.

show_labels

label visibility: one of "default", "visible" and "hidden".

...

additional arguments for the lower level functions.

table_names

(character)
this can be customized in case that the same vars are analyzed multiple times, to avoid warnings from rtables.

.stats

(character)
statistics to select for the table.

.formats

(named character or list)
formats for the statistics.

.indent_mods

(named integer)
indent modifiers for the labels.

.labels

(named character)
labels for the statistics (without indent).

Details

Functions for analyzing frequencies and fractions of occurrences by grade for patients with occurrence data. Multiple occurrences within one individual are counted once at the greatest intensity/highest grade level.

Functions

  • h_append_grade_groups(): Helper function for s_count_occurrences_by_grade() to insert grade groupings into list with individual grade frequencies. The order of the final result follows the order of grade_groups. The elements under any-grade group (if any), i.e. the grade group equal to refs will be moved to the end. Grade groups names must be unique.

  • s_count_occurrences_by_grade(): Statistics function which given occurrence data counts the number of patients by highest grade. Returns a list of counts and fractions with one element per grade level or grade level grouping.

  • a_count_occurrences_by_grade(): Formatted Analysis function which can be further customized by calling rtables::make_afun() on it. It is used as afun in rtables::analyze().

  • count_occurrences_by_grade(): Layout creating function which can be used for creating tables, which can take statistics function arguments and additional format arguments (see below).

  • summarize_occurrences_by_grade(): Layout creating function which adds content rows using the statistics function and additional format arguments (see below).

Examples

h_append_grade_groups(
  list(
    "Any Grade" = as.character(1:5),
    "Grade 1-2" = c("1", "2"),
    "Grade 3-4" = c("3", "4")
  ),
  list("1" = 10, "2" = 20, "3" = 30, "4" = 40, "5" = 50)
)
#> $`Any Grade`
#> [1] 150
#> 
#> $`Grade 1-2`
#> [1] 30
#> 
#> $`1`
#> [1] 10
#> 
#> $`2`
#> [1] 20
#> 
#> $`Grade 3-4`
#> [1] 70
#> 
#> $`3`
#> [1] 30
#> 
#> $`4`
#> [1] 40
#> 
#> $`5`
#> [1] 50
#> 

h_append_grade_groups(
  list(
    "Any Grade" = as.character(5:1),
    "Grade A" = "5",
    "Grade B" = c("4", "3")
  ),
  list("1" = 10, "2" = 20, "3" = 30, "4" = 40, "5" = 50)
)
#> $`Any Grade`
#> [1] 150
#> 
#> $`Grade A`
#> [1] 50
#> 
#> $`Grade B`
#> [1] 70
#> 
#> $`4`
#> [1] 40
#> 
#> $`3`
#> [1] 30
#> 
#> $`2`
#> [1] 20
#> 
#> $`1`
#> [1] 10
#> 

h_append_grade_groups(
  list(
    "Any Grade" = as.character(1:5),
    "Grade 1-2" = c("1", "2"),
    "Grade 3-4" = c("3", "4")
  ),
  list("1" = 10, "2" = 5, "3" = 0)
)
#> $`Any Grade`
#> [1] 15
#> 
#> $`Grade 1-2`
#> [1] 15
#> 
#> $`1`
#> [1] 10
#> 
#> $`2`
#> [1] 5
#> 
#> $`Grade 3-4`
#> [1] 0
#> 
#> $`3`
#> [1] 0
#> 

library(dplyr)
df <- data.frame(
  USUBJID = as.character(c(1:6, 1)),
  ARM = factor(c("A", "A", "A", "B", "B", "B", "A"), levels = c("A", "B")),
  AETOXGR = factor(c(1, 2, 3, 4, 1, 2, 3), levels = c(1:5)),
  AESEV = factor(
    x = c("MILD", "MODERATE", "SEVERE", "MILD", "MILD", "MODERATE", "SEVERE"),
    levels = c("MILD", "MODERATE", "SEVERE")
  ),
  stringsAsFactors = FALSE
)
df_adsl <- df %>%
  select(USUBJID, ARM) %>%
  unique()

s_count_occurrences_by_grade(
  df,
  .N_col = 10L, # nolint
  .var = "AETOXGR",
  id = "USUBJID",
  grade_groups = list("ANY" = levels(df$AETOXGR))
)
#> $count_fraction
#> $count_fraction$ANY
#> [1] 6.0 0.6
#> 
#> $count_fraction$`1`
#> [1] 1.0 0.1
#> 
#> $count_fraction$`2`
#> [1] 2.0 0.2
#> 
#> $count_fraction$`3`
#> [1] 2.0 0.2
#> 
#> $count_fraction$`4`
#> [1] 1.0 0.1
#> 
#> $count_fraction$`5`
#> [1] 0 0
#> 
#> 

#  We need to ungroup `count_fraction` first so that the `rtables` formatting
# function `format_count_fraction()` can be applied correctly.
afun <- make_afun(a_count_occurrences_by_grade, .ungroup_stats = "count_fraction")
afun(
  df,
  .N_col = 10L, # nolint
  .var = "AETOXGR",
  id = "USUBJID",
  grade_groups = list("ANY" = levels(df$AETOXGR))
)
#> RowsVerticalSection (in_rows) object print method:
#> ----------------------------
#>   row_name formatted_cell indent_mod row_label
#> 1      ANY        6 (60%)          0       ANY
#> 2        1        1 (10%)          0         1
#> 3        2        2 (20%)          0         2
#> 4        3        2 (20%)          0         3
#> 5        4        1 (10%)          0         4
#> 6        5              0          0         5

# Layout creating function with custom format.
basic_table() %>%
  split_cols_by("ARM") %>%
  add_colcounts() %>%
  count_occurrences_by_grade(
    var = "AESEV",
    .formats = c("count_fraction" = "xx.xx (xx.xx%)")
  ) %>%
  build_table(df, alt_counts_df = df_adsl)
#>                  A               B      
#>                (N=3)           (N=3)    
#> ————————————————————————————————————————
#> MILD       0.00 (0.00%)    2.00 (66.67%)
#> MODERATE   1.00 (33.33%)   1.00 (33.33%)
#> SEVERE     2.00 (66.67%)   0.00 (0.00%) 

# Define additional grade groupings.
grade_groups <- list(
  "-Any-" = c("1", "2", "3", "4", "5"),
  "Grade 1-2" = c("1", "2"),
  "Grade 3-5" = c("3", "4", "5")
)

basic_table() %>%
  split_cols_by("ARM") %>%
  add_colcounts() %>%
  count_occurrences_by_grade(
    var = "AETOXGR",
    grade_groups = grade_groups
  ) %>%
  build_table(df, alt_counts_df = df_adsl)
#>                 A           B    
#>               (N=3)       (N=3)  
#> —————————————————————————————————
#> -Any-       3 (100%)    3 (100%) 
#> Grade 1-2   1 (33.3%)   2 (66.7%)
#> 1               0       1 (33.3%)
#> 2           1 (33.3%)   1 (33.3%)
#> Grade 3-5   2 (66.7%)   1 (33.3%)
#> 3           2 (66.7%)       0    
#> 4               0       1 (33.3%)
#> 5               0           0    

# Layout creating function with custom format.
basic_table() %>%
  add_colcounts() %>%
  split_rows_by("ARM", child_labels = "visible", nested = TRUE) %>%
  summarize_occurrences_by_grade(
    var = "AESEV",
    .formats = c("count_fraction" = "xx.xx (xx.xx%)")
  ) %>%
  build_table(df, alt_counts_df = df_adsl)
#>                 all obs   
#>                  (N=6)    
#> ——————————————————————————
#> A                         
#>   MILD       0.00 (0.00%) 
#>   MODERATE   1.00 (16.67%)
#>   SEVERE     2.00 (33.33%)
#> B                         
#>   MILD       2.00 (33.33%)
#>   MODERATE   1.00 (16.67%)
#>   SEVERE     0.00 (0.00%) 

basic_table() %>%
  add_colcounts() %>%
  split_rows_by("ARM", child_labels = "visible", nested = TRUE) %>%
  summarize_occurrences_by_grade(
    var = "AETOXGR",
    grade_groups = grade_groups
  ) %>%
  build_table(df, alt_counts_df = df_adsl)
#>                all obs 
#>                 (N=6)  
#> ———————————————————————
#> A                      
#>   -Any-        3 (50%) 
#>   Grade 1-2   1 (16.7%)
#>   1               0    
#>   2           1 (16.7%)
#>   Grade 3-5   2 (33.3%)
#>   3           2 (33.3%)
#>   4               0    
#>   5               0    
#> B                      
#>   -Any-        3 (50%) 
#>   Grade 1-2   2 (33.3%)
#>   1           1 (16.7%)
#>   2           1 (16.7%)
#>   Grade 3-5   1 (16.7%)
#>   3               0    
#>   4           1 (16.7%)
#>   5               0