tern R package provides functions to create common
analyses from clinical trials in
R. The core functionality
for tabulation is built on the more general purpose
package. New users should first begin by reading the “Introduction
to tern” and “Introduction
The packages used in this vignette are:
The datasets used in this vignette are:
Analyze functions are used in combination with the
rtables layout functions, in the pipeline which creates the
rtables table. They apply some statistical logic to the
layout of the
rtables table. The table layout is
materialized with the
rtables::build_table function and the
tern analyze functions are wrappers around
rtables::analyze function, they offer various methods
useful from the perspective of clinical trials and other statistical
Examples of the
tern analyze functions are
As there is no one prefix to identify all
functions it is recommended to use the the
tern website functions reference.
Please skip this subsection if you are not interested in the
tern analyze functions.
tern analyze functions like
tern::summarize_ancova are mainly built in the 4 elements
h_ancova() -> tern:::s_ancova() -> tern:::a_ancova() -> summarize_ancova()
The descriptions for each function type:
h_*. These functions are useful to help define the analysis.
s_*. Statistics functions should do the computation of the numbers that are tabulated later. In order to separate computation from formatting, they should not take care of
rcelltype formatting themselves.
a_*. These have the same arguments as the corresponding statistics functions, and can be further customized by calling
rtables::make_afun()on them. They are used as
rtables::analyze(..., afun = make_afun(tern::a_*)). Analyze functions are used in combination with the
rtableslayout functions, in the pipeline which creates the table. They are the last element of the chain.
We will use the native
rtables::analyze function with
tern formatted analysis functions as a
l <- basic_table() %>% split_cols_by(var = "ARM") %>% split_rows_by(var = "AVISIT") %>% analyze(vars = "AVAL", afun = a_summary) build_table(l, df = adrs)
rtables::make_afun function is helpful when somebody
wants to attach some format to the formatted analysis function.
afun <- make_afun( a_summary, .stats = NULL, .formats = c(median = "xx."), .labels = c(median = "My median"), .indent_mods = c(median = 1L) ) l2 <- basic_table() %>% split_cols_by(var = "ARM") %>% split_rows_by(var = "AVISIT") %>% analyze(vars = "AVAL", afun = afun) build_table(l2, df = adrs)
We are going to create 3 different tables using
analyze functions and the
|Adverse event Table||
Demographic tables provide a summary of the characteristics of patients enrolled in a clinical trial. Typically the table columns represent treatment arms and variables summarized in the table are demographic properties such as age, sex, race, etc.
In the example below the only function from
analyze_vars() and the remaining layout functions are from
# Select variables to include in table. vars <- c("AGE", "SEX") var_labels <- c("Age (yr)", "Sex") basic_table() %>% split_cols_by(var = "ARM") %>% add_overall_col("All Patients") %>% add_colcounts() %>% analyze_vars( vars = vars, var_labels = var_labels ) %>% build_table(adsl) #> A: Drug X B: Placebo C: Combination All Patients #> (N=134) (N=134) (N=132) (N=400) #> —————————————————————————————————————————————————————————————————————————————— #> Age (yr) #> n 134 134 132 400 #> Mean (SD) 33.8 (6.6) 35.4 (7.9) 35.4 (7.7) 34.9 (7.4) #> Median 33.0 35.0 35.0 34.0 #> Min - Max 21.0 - 50.0 21.0 - 62.0 20.0 - 69.0 20.0 - 69.0 #> Sex #> n 134 134 132 400 #> F 79 (59%) 77 (57.5%) 66 (50%) 222 (55.5%) #> M 51 (38.1%) 55 (41%) 60 (45.5%) 166 (41.5%) #> U 3 (2.2%) 2 (1.5%) 4 (3%) 9 (2.2%) #> UNDIFFERENTIATED 1 (0.7%) 0 2 (1.5%) 3 (0.8%)
To change the display order of categorical variables in a table use
factor variables and explicitly set the order of the levels. This is the
case for the display order in columns and rows. Note that the
forcats package has many useful functions to help with
these types of data processing steps (not used below).
# Reorder the levels in the ARM variable. adsl$ARM <- factor(adsl$ARM, levels = c("B: Placebo", "A: Drug X", "C: Combination")) # nolint # Reorder the levels in the SEX variable. adsl$SEX <- factor(adsl$SEX, levels = c("M", "F", "U", "UNDIFFERENTIATED")) # nolint basic_table() %>% split_cols_by(var = "ARM")