Skip to contents

[Stable]

Primary analysis variable .var indicates whether single, replicated or last marked laboratory abnormality was observed (factor). Additional analysis variables are id (character or factor) and direction indicating the direction of the abnormality (factor). Denominator is number of patients with at least one valid measurement during

  • For Single, not last and Last or replicated: Numerator is number of patients with Single, not last and Last or replicated levels, respectively.

  • For Any: Numerator is the number of patients with either single or replicated marked abnormalities.

Usage

s_count_abnormal_by_marked(
  df,
  .var = "AVALCAT1",
  .spl_context,
  category = list(single = "SINGLE", last_replicated = c("LAST", "REPLICATED")),
  variables = list(id = "USUBJID", param = "PARAM", direction = "abn_dir")
)

a_count_abnormal_by_marked(
  df,
  .var = "AVALCAT1",
  .spl_context,
  category = list(single = "SINGLE", last_replicated = c("LAST", "REPLICATED")),
  variables = list(id = "USUBJID", param = "PARAM", direction = "abn_dir")
)

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

Arguments

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.

.spl_context

(data.frame)
gives information about ancestor split states that is passed by rtables.

category

(list)
with different marked category names for single and last or replicated.

variables

(named list of string)
list of additional analysis variables.

lyt

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

...

additional arguments for the lower level functions.

.stats

(character)
statistics to select for the table.

.formats

(named character or list)
formats for the statistics.

.labels

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

.indent_mods

(named integer)
indent modifiers for the labels.

Value

s_count_abnormal_by_marked() the single statistic count_fraction

with Single, not last, Last or replicated and Any results.

a_count_abnormal_by_marked() returns the corresponding list with formatted rtables::CellValue().

Details

Note that Single, not last and Last or replicated levels are mutually exclusive. If a patient has abnormalities that meet both the Single, not last and Last or replicated criteria, then the patient will be counted only under the Last or replicated category.

Functions

  • s_count_abnormal_by_marked(): Statistics function which returns the counts and fractions of patients with Single, not last, Last or replicated and Any marked laboratory abnormalities for a single abnormal level.

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

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

Examples

library(dplyr)

df <- data.frame(
  USUBJID = as.character(c(rep(1, 5), rep(2, 5), rep(1, 5), rep(2, 5))),
  ARMCD = factor(c(rep("ARM A", 5), rep("ARM B", 5), rep("ARM A", 5), rep("ARM B", 5))),
  ANRIND = factor(c(
    "NORMAL", "HIGH", "HIGH", "HIGH HIGH", "HIGH",
    "HIGH", "HIGH", "HIGH HIGH", "NORMAL", "HIGH HIGH", "NORMAL", "LOW", "LOW", "LOW LOW", "LOW",
    "LOW", "LOW", "LOW LOW", "NORMAL", "LOW LOW"
  )),
  ONTRTFL = rep(c("", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y"), 2),
  PARAMCD = factor(c(rep("CRP", 10), rep("ALT", 10))),
  AVALCAT1 = factor(rep(c("", "", "", "SINGLE", "REPLICATED", "", "", "LAST", "", "SINGLE"), 2)),
  stringsAsFactors = FALSE
)

df <- df %>%
  mutate(abn_dir = factor(
    case_when(
      ANRIND == "LOW LOW" ~ "Low",
      ANRIND == "HIGH HIGH" ~ "High",
      TRUE ~ ""
    ),
    levels = c("Low", "High")
  ))

# Select only post-baseline records.
df <- df %>% filter(ONTRTFL == "Y")
df_crp <- df %>%
  filter(PARAMCD == "CRP") %>%
  droplevels()
full_parent_df <- list(df_crp, "not_needed")
cur_col_subset <- list(rep(TRUE, nrow(df_crp)), "not_needed")
spl_context <- data.frame(
  split = c("PARAMCD", "GRADE_DIR"),
  full_parent_df = I(full_parent_df),
  cur_col_subset = I(cur_col_subset)
)
# Internal function - s_count_abnormal_by_marked
if (FALSE) {
s_count_abnormal_by_marked(
  df = df_crp %>% filter(abn_dir == "High"),
  .spl_context = spl_context,
  .var = "AVALCAT1",
  variables = list(id = "USUBJID", param = "PARAMCD", direction = "abn_dir")
)
}

# Internal function - a_count_abnormal_by_marked
if (FALSE) {
# Use the Formatted Analysis function for `analyze()`. 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_abnormal_by_marked, .ungroup_stats = "count_fraction")
afun(
  df = df_crp %>% filter(abn_dir == "High"),
  .spl_context = spl_context,
  variables = list(id = "USUBJID", param = "PARAMCD", direction = "abn_dir")
)
}

map <- unique(
  df[df$abn_dir %in% c("Low", "High") & df$AVALCAT1 != "", c("PARAMCD", "abn_dir")]
) %>%
  lapply(as.character) %>%
  as.data.frame() %>%
  arrange(PARAMCD, abn_dir)

basic_table() %>%
  split_cols_by("ARMCD") %>%
  split_rows_by("PARAMCD") %>%
  summarize_num_patients(
    var = "USUBJID",
    .stats = "unique_count"
  ) %>%
  split_rows_by(
    "abn_dir",
    split_fun = trim_levels_to_map(map)
  ) %>%
  count_abnormal_by_marked(
    var = "AVALCAT1",
    variables = list(
      id = "USUBJID",
      param = "PARAMCD",
      direction = "abn_dir"
    )
  ) %>%
  build_table(df = df)
#>                           ARM A      ARM B  
#> ————————————————————————————————————————————
#> ALT (n)                     1          1    
#>   Low                                       
#>     Single, not last     1 (100%)      0    
#>     Last or replicated      0       1 (100%)
#>     Any Abnormality      1 (100%)   1 (100%)
#> CRP (n)                     1          1    
#>   High                                      
#>     Single, not last     1 (100%)      0    
#>     Last or replicated      0       1 (100%)
#>     Any Abnormality      1 (100%)   1 (100%)


basic_table() %>%
  split_cols_by("ARMCD") %>%
  split_rows_by("PARAMCD") %>%
  summarize_num_patients(
    var = "USUBJID",
    .stats = "unique_count"
  ) %>%
  split_rows_by(
    "abn_dir",
    split_fun = trim_levels_in_group("abn_dir")
  ) %>%
  count_abnormal_by_marked(
    var = "AVALCAT1",
    variables = list(
      id = "USUBJID",
      param = "PARAMCD",
      direction = "abn_dir"
    )
  ) %>%
  build_table(df = df)
#>                           ARM A      ARM B  
#> ————————————————————————————————————————————
#> ALT (n)                     1          1    
#>   Low                                       
#>     Single, not last     1 (100%)      0    
#>     Last or replicated      0       1 (100%)
#>     Any Abnormality      1 (100%)   1 (100%)
#> CRP (n)                     1          1    
#>   High                                      
#>     Single, not last     1 (100%)      0    
#>     Last or replicated      0       1 (100%)
#>     Any Abnormality      1 (100%)   1 (100%)