Case Study: Clinical Tables
The gt package contains the rx_adsl dataset, which resembles the structure of a common ADSL ADaM dataset for clinical trial data. Each record refers to demographic information for a single subject in the fictional trial. Every column is equipped with a label attribute allowing the users to get familiar with the data.
import std/[json, math, sequtils, streams, strutils]
import nanuq
import tabelline, tabelline/nanuq, gtdata
# gt's `rx_adsl`; `rxMeta()` has the labels, units and levels R keeps with its columns
let rxAdsl = readCsv(newStringStream(rxAdslCsv))
echo rxAdsl.glimpse
Rows: 182 Columns: 14 $ STUDYID'GT01', 'GT01', 'GT01', 'GT01', 'GT01', 'GT01', 'GT01', 'GT01', 'GT01', 'GT01' $ STUDYIDN 4001, 4001, 4001, 4001, 4001, 4001, 4001, 4001, 4001, 4001 $ USUBJID 'GT1000', 'GT1001', 'GT1002', 'GT1003', 'GT1004', 'GT1005', 'GT1006', 'GT1007', 'GT1008', 'GT1009' $ TRTA null, 'Placebo', 'Placebo', 'Placebo', 'Placebo', 'Placebo', 'Placebo', 'Placebo', 'Placebo', 'Placebo' $ TRTAN 3, 1, 1, 1, 1, 1, 1, 1, 1, 1 $ ITTFL 'N', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y' $ RANDFL 'N', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y' $ SCRFREAS 'WITHDRAWAL BY SUBJECT', '', '', '', '', '', '', '', '', '' $ AGE 37, 41, 39, 38, 45, 35, 42, 35, 42, 38 $ AAGEGR1 '<40', '>=40', '<40', '<40', '>=40', '<40', '>=40', '<40', '>=40', '<40' $ SEX 'Male', 'Male', 'Female', 'Male', 'Male', 'Female', 'Female', 'Male', 'Male', 'Female' $ ETHNIC 'Hispanic or Latino', 'Not Hispanic or Latino', 'Not Hispanic or Latino', 'Not Hispanic or Latino', 'Not Hispanic or Latino', 'Hispanic or Latino', 'Not Hispanic or Latino', 'Not Hispanic or Latino', 'Not Hispanic or Latino', 'Hispanic or Latino' $ BLBMI 33.7672320231795, 33.3507320063654, 30.4586162811611, 22.8598554597702, 23.8971279682592, 29.0985568843316, 28.8476343760267, 32.5534083573148, 32.6204051985405, 34.4888631361537 $ EVNTFL '', 'Y', 'Y', 'N', 'Y', 'Y', 'N', 'N', 'N', 'N'
import json
from math import exp, lgamma, log, sqrt
import polars as pl
import polars.selectors as cs
from great_tables import GT, loc, md, style
# gt's `rx_adsl`, and the labels, units and levels R keeps with its columns
rx_meta = json.load(open("../data/gt/rx_meta.json"))
rx_adsl = pl.read_csv("../data/gt/rx_adsl.csv")
rx_adsl.glimpse()Rows: 182 Columns: 14 $ STUDYID <str> 'GT01', 'GT01', 'GT01', 'GT01', 'GT01', 'GT01', 'GT01', 'GT01', 'GT01', 'GT01' $ STUDYIDN <i64> 4001, 4001, 4001, 4001, 4001, 4001, 4001, 4001, 4001, 4001 $ USUBJID <str> 'GT1000', 'GT1001', 'GT1002', 'GT1003', 'GT1004', 'GT1005', 'GT1006', 'GT1007', 'GT1008', 'GT1009' $ TRTA <str> null, 'Placebo', 'Placebo', 'Placebo', 'Placebo', 'Placebo', 'Placebo', 'Placebo', 'Placebo', 'Placebo' $ TRTAN <i64> 3, 1, 1, 1, 1, 1, 1, 1, 1, 1 $ ITTFL <str> 'N', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y' $ RANDFL <str> 'N', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y' $ SCRFREAS <str> 'WITHDRAWAL BY SUBJECT', '', '', '', '', '', '', '', '', '' $ AGE <i64> 37, 41, 39, 38, 45, 35, 42, 35, 42, 38 $ AAGEGR1 <str> '<40', '>=40', '<40', '<40', '>=40', '<40', '>=40', '<40', '>=40', '<40' $ SEX <str> 'Male', 'Male', 'Female', 'Male', 'Male', 'Female', 'Female', 'Male', 'Male', 'Female' $ ETHNIC <str> 'Hispanic or Latino', 'Not Hispanic or Latino', 'Not Hispanic or Latino', 'Not Hispanic or Latino', 'Not Hispanic or Latino', 'Hispanic or Latino', 'Not Hispanic or Latino', 'Not Hispanic or Latino', 'Not Hispanic or Latino', 'Hispanic or Latino' $ BLBMI <f64> 33.7672320231795, 33.3507320063654, 30.4586162811611, 22.8598554597702, 23.8971279682592, 29.0985568843316, 28.8476343760267, 32.5534083573148, 32.6204051985405, 34.4888631361537 $ EVNTFL <str> '', 'Y', 'Y', 'N', 'Y', 'Y', 'N', 'N', 'N', 'N'
library(gt)
library(dplyr)
library(tidyr)
library(rlang)
library(purrr)
rx_adsl |> str()tibble [182 x 14] (S3: tbl_df/tbl/data.frame) $ STUDYID : chr [1:182] "GT01" "GT01" "GT01" "GT01" ... ..- attr(*, "label")= chr "Unique Study Identifier" $ STUDYIDN: chr [1:182] "4001" "4001" "4001" "4001" ... ..- attr(*, "label")= chr "Unique Study Identifier (N)" $ USUBJID : chr [1:182] "GT1000" "GT1001" "GT1002" "GT1003" ... ..- attr(*, "label")= chr "Unique Subject Identifier" $ TRTA : Factor w/ 2 levels "Placebo","Drug 1": NA 1 1 1 1 1 1 1 1 1 ... ..- attr(*, "label")= chr "Actual Treatment" $ TRTAN : num [1:182] 3 1 1 1 1 1 1 1 1 1 ... ..- attr(*, "label")= chr "Actual Treatment (N)" $ ITTFL : chr [1:182] "N" "Y" "Y" "Y" ... ..- attr(*, "label")= chr "ITT Population Flag" $ RANDFL : chr [1:182] "N" "Y" "Y" "Y" ... ..- attr(*, "label")= chr "Randomization Flag" $ SCRFREAS: chr [1:182] "WITHDRAWAL BY SUBJECT" "" "" "" ... ..- attr(*, "label")= chr "Reason for Screen Failure" $ AGE : int [1:182] 37 41 39 38 45 35 42 35 42 38 ... ..- attr(*, "label")= chr "Age" ..- attr(*, "units")= chr "Years" $ AAGEGR1 : Factor w/ 2 levels "<40",">=40": 1 2 1 1 2 1 2 1 2 1 ... ..- attr(*, "label")= chr "Age Group" $ SEX : Factor w/ 3 levels "Male","Female",..: 1 1 2 1 1 2 2 1 1 2 ... ..- attr(*, "label")= chr "Sex" $ ETHNIC : Factor w/ 3 levels "Hispanic or Latino",..: 1 2 2 2 2 1 2 2 2 1 ... ..- attr(*, "label")= chr "Ethnicity" $ BLBMI : num [1:182] 33.8 33.4 30.5 22.9 23.9 ... ..- attr(*, "label")= chr "Body Mass Index" ..- attr(*, "units")= chr "kg/m2" $ EVNTFL : chr [1:182] "" "Y" "Y" "N" ... ..- attr(*, "label")= chr "Event Flag"
Demographic Summary Tables
Let's start with an example of a basic demographic summary table. In a first step, we use dplyr and tidyr to create a tibble with the shape of our desired table and then use gt functions to create the output table:
let meta = rxMeta()["rx_adsl"]
let arms = meta["TRTA"]["levels"].to(seq[string])
proc customSummary(df: DataFrame, groupVar, sumVar: string): DataFrame =
let info = meta[sumVar]
if info.hasKey("levels"):
let categoryLbl = info["label"].getStr & ", n (%)"
df.groupBy(groupVar, sumVar)
.agg(len().alias("val"))
.withColumns((col("val") / col("val").sum.over(groupVar)).alias("pct"))
.pivot(groupVar, index = sumVar, values = ["val", "pct"])
.fillNull(0)
.sort([col(sumVar).cast(Enum(info["levels"].to(seq[string])))])
.rename({sumVar: "label"})
.withColumns(lit(categoryLbl).alias("category"))
else:
let categoryLbl = info["label"].getStr & " (" & info["units"].getStr & ")"
let x = col(sumVar).cast(float)
df.groupBy(groupVar)
.agg(
x.isNotNull.sum.cast(float).alias("n"),
x.mean.alias("mean"),
x.std.alias("sd"),
x.median.alias("median"),
x.min.alias("min"),
x.max.alias("max"),
lit(nil).cast(float).alias("min_max"))
.unpivot(["n", "mean", "median", "min_max"], index = [groupVar, "sd", "min", "max"],
variableName = "label", valueName = "val")
.withColumns(
`when`(col("label") == "mean").then(col("sd")).otherwise(nil).alias("sd"),
`when`(col("label") == "min_max").then(col("max")).otherwise(nil).alias("max"),
`when`(col("label") == "min_max").then(col("min")).otherwise(nil).alias("min"),
col("label").replace({"mean": "Mean (SD)", "min_max": "Min - Max", "median": "Median"}))
.pivot(groupVar, index = "label", values = ["val", "sd", "min", "max"])
.withColumns(lit(categoryLbl).alias("category"))
let adslItt = rxAdsl.filter(col("ITTFL") == "Y")
let adslSummary = concat(
["AGE", "AAGEGR1", "SEX", "ETHNIC", "BLBMI"].mapIt(customSummary(adslItt, "TRTA", it)),
how = ConcatMethod.DiagonalRelaxed)arms = rx_meta["rx_adsl"]["TRTA"]["levels"]
def custom_summary(df, group_var, sum_var):
info = rx_meta["rx_adsl"][sum_var]
if "levels" in info:
category_lbl = f"{info['label']}, n (%)"
df_out = (
df.group_by(group_var, sum_var)
.agg(val=pl.len())
.with_columns(pct=pl.col("val") / pl.col("val").sum().over(group_var))
.pivot(on=group_var, index=sum_var, values=["val", "pct"])
.fill_null(0)
.sort(pl.col(sum_var).cast(pl.Enum(info["levels"])))
.rename({sum_var: "label"})
.with_columns(category=pl.lit(category_lbl))
)
else:
category_lbl = f"{info['label']} ({info['units']})"
rows = {"n": {}, "Mean (SD)": {}, "Median": {}, "Min - Max": {}}
for arm in arms:
x = df.filter(pl.col(group_var) == arm)[sum_var].drop_nulls().cast(pl.Float64)
rows["n"][f"val_{arm}"] = float(x.len())
rows["Mean (SD)"] |= {f"val_{arm}": x.mean(), f"sd_{arm}": x.std()}
rows["Median"][f"val_{arm}"] = x.median()
rows["Min - Max"] |= {f"min_{arm}": x.min(), f"max_{arm}": x.max()}
df_out = pl.DataFrame(
[{"label": label, **values} for label, values in rows.items()],
infer_schema_length=None,
).with_columns(category=pl.lit(category_lbl))
return df_out
adsl_summary = pl.concat(
[
custom_summary(rx_adsl.filter(pl.col("ITTFL") == "Y"), "TRTA", sum_var)
for sum_var in ["AGE", "AAGEGR1", "SEX", "ETHNIC", "BLBMI"]
],
how="diagonal_relaxed",
)custom_summary <- function(df, group_var, sum_var) {
group_var <- rlang::ensym(group_var)
sum_var <- rlang::ensym(sum_var)
is_categorical <-
is.character(eval(expr(`$`(df, !!sum_var)))) |
is.factor(eval(expr(`$`(df, !!sum_var))))
if (is_categorical) {
category_lbl <-
sprintf("%s, n (%%)", attr(eval(expr(`$`(df, !!sum_var))), "label"))
df_out <-
df |>
dplyr::group_by(!!group_var) |>
dplyr::mutate(N = dplyr::n()) |>
dplyr::ungroup() |>
dplyr::group_by(!!group_var, !!sum_var) |>
dplyr::summarize(
val = dplyr::n(),
pct = dplyr::n() / mean(N),
.groups = "drop"
) |>
tidyr::pivot_wider(
id_cols = !!sum_var, names_from = !!group_var,
values_from = c(val, pct)
) |>
dplyr::rename(label = !!sum_var) |>
dplyr::mutate(
across(where(is.numeric), ~ ifelse(is.na(.), 0, .)),
category = category_lbl
)
} else {
category_lbl <-
sprintf(
"%s (%s)",
attr(eval(expr(`$`(df, !!sum_var))), "label"),
attr(eval(expr(`$`(df, !!sum_var))), "units")
)
df_out <-
df |>
dplyr::group_by(!!group_var) |>
dplyr::summarize(
n = sum(!is.na(!!sum_var)),
mean = mean(!!sum_var, na.rm = TRUE),
sd = sd(!!sum_var, na.rm = TRUE),
median = median(!!sum_var, na.rm = TRUE),
min = min(!!sum_var, na.rm = TRUE),
max = max(!!sum_var, na.rm = TRUE),
min_max = NA,
.groups = "drop"
) |>
tidyr::pivot_longer(
cols = c(n, mean, median, min_max),
names_to = "label",
values_to = "val"
) |>
dplyr::mutate(
sd = ifelse(label == "mean", sd, NA),
max = ifelse(label == "min_max", max, NA),
min = ifelse(label == "min_max", min, NA),
label = dplyr::recode(
label,
"mean" = "Mean (SD)",
"min_max" = "Min - Max",
"median" = "Median"
)
) |>
tidyr::pivot_wider(
id_cols = label,
names_from = !!group_var,
values_from = c(val, sd, min, max)
) |>
dplyr::mutate(category = category_lbl)
}
return(df_out)
}
adsl_summary <-
dplyr::filter(rx_adsl, ITTFL == "Y") |>
(\(data) purrr::map_df(
.x = dplyr::vars(AGE, AAGEGR1, SEX, ETHNIC, BLBMI),
.f = \(x) custom_summary(df = data, group_var = TRTA, sum_var = !!x)
))()We can now start to expose our tibble with the summary of adsl variables to gt using gt(). Values should be grouped by category, with labels as rownames. In addition, we can give our table a nice title and subtitle.
let rxAdslTbl = tabella(adslSummary, rownameCol = "label", groupnameCol = "category"):
tabHeader "x.x: Demographic Characteristics",
"x.x.x: Demographic Characteristics - ITT Analysis Set"
nbTable rxAdslTbl
| x.x: Demographic Characteristics | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| x.x.x: Demographic Characteristics - ITT Analysis Set | ||||||||||
| val_Placebo | val_Drug 1 | sd_Placebo | sd_Drug 1 | min_Placebo | min_Drug 1 | max_Placebo | max_Drug 1 | pct_Placebo | pct_Drug 1 | |
| Age (Years) | ||||||||||
| n | 90.0 | 90.0 | ||||||||
| Mean (SD) | 41.166666666666664 | 39.17777777777778 | 6.1374701079388325 | 5.620143901907878 | ||||||
| Median | 41.0 | 38.5 | ||||||||
| Min - Max | 27.0 | 29.0 | 56.0 | 55.0 | ||||||
| Age Group, n (%) | ||||||||||
| <40 | 36.0 | 49.0 | 0.4 | 0.5444444444444444 | ||||||
| >=40 | 54.0 | 41.0 | 0.6 | 0.45555555555555555 | ||||||
| Sex, n (%) | ||||||||||
| Male | 59.0 | 57.0 | 0.6555555555555556 | 0.6333333333333333 | ||||||
| Female | 25.0 | 32.0 | 0.2777777777777778 | 0.35555555555555557 | ||||||
| Undifferentiated | 6.0 | 1.0 | 0.06666666666666667 | 0.011111111111111112 | ||||||
| Ethnicity, n (%) | ||||||||||
| Hispanic or Latino | 38.0 | 29.0 | 0.4222222222222222 | 0.32222222222222224 | ||||||
| Not Hispanic or Latino | 46.0 | 50.0 | 0.5111111111111111 | 0.5555555555555556 | ||||||
| Missing | 6.0 | 11.0 | 0.06666666666666667 | 0.12222222222222222 | ||||||
| Body Mass Index (kg/m2) | ||||||||||
| n | 90.0 | 90.0 | ||||||||
| Mean (SD) | 26.171611831034532 | 27.168969924046866 | 4.884630675893921 | 4.973972205868662 | ||||||
| Median | 26.042854781379 | 26.8970486705657 | ||||||||
| Min - Max | 18.0886122109368 | 18.0274835294113 | 34.4888631361537 | 34.8055275594816 | ||||||
rx_adsl_tbl = GT(
adsl_summary, rowname_col="label", groupname_col="category"
).tab_header(
title="x.x: Demographic Characteristics",
subtitle="x.x.x: Demographic Characteristics - ITT Analysis Set",
)
rx_adsl_tbl| x.x: Demographic Characteristics | ||||||||||
| x.x.x: Demographic Characteristics - ITT Analysis Set | ||||||||||
| val_Placebo | val_Drug 1 | sd_Placebo | sd_Drug 1 | min_Placebo | max_Placebo | min_Drug 1 | max_Drug 1 | pct_Placebo | pct_Drug 1 | |
|---|---|---|---|---|---|---|---|---|---|---|
| Age (Years) | ||||||||||
| n | 90.0 | 90.0 | None | None | None | None | None | None | None | None |
| Mean (SD) | 41.166666666666664 | 39.17777777777778 | 6.1374701079388325 | 5.620143901907878 | None | None | None | None | None | None |
| Median | 41.0 | 38.5 | None | None | None | None | None | None | None | None |
| Min - Max | None | None | None | None | 27.0 | 56.0 | 29.0 | 55.0 | None | None |
| Age Group, n (%) | ||||||||||
| <40 | 36.0 | 49.0 | None | None | None | None | None | None | 0.4 | 0.5444444444444444 |
| >=40 | 54.0 | 41.0 | None | None | None | None | None | None | 0.6 | 0.45555555555555555 |
| Sex, n (%) | ||||||||||
| Male | 59.0 | 57.0 | None | None | None | None | None | None | 0.6555555555555556 | 0.6333333333333333 |
| Female | 25.0 | 32.0 | None | None | None | None | None | None | 0.2777777777777778 | 0.35555555555555557 |
| Undifferentiated | 6.0 | 1.0 | None | None | None | None | None | None | 0.06666666666666667 | 0.011111111111111112 |
| Ethnicity, n (%) | ||||||||||
| Hispanic or Latino | 38.0 | 29.0 | None | None | None | None | None | None | 0.4222222222222222 | 0.32222222222222224 |
| Not Hispanic or Latino | 46.0 | 50.0 | None | None | None | None | None | None | 0.5111111111111111 | 0.5555555555555556 |
| Missing | 6.0 | 11.0 | None | None | None | None | None | None | 0.06666666666666667 | 0.12222222222222222 |
| Body Mass Index (kg/m2) | ||||||||||
| n | 90.0 | 90.0 | None | None | None | None | None | None | None | None |
| Mean (SD) | 26.171611831034532 | 27.168969924046866 | 4.884630675893921 | 4.973972205868662 | None | None | None | None | None | None |
| Median | 26.042854781379 | 26.8970486705657 | None | None | None | None | None | None | None | None |
| Min - Max | None | None | None | None | 18.0886122109368 | 34.4888631361537 | 18.0274835294113 | 34.8055275594816 | None | None |
rx_adsl_tbl <-
adsl_summary |>
gt(
rowname_col = "label",
groupname_col = "category"
) |>
tab_header(
title = "x.x: Demographic Characteristics",
subtitle = "x.x.x: Demographic Characteristics - ITT Analysis Set"
)
rx_adsl_tbl| x.x: Demographic Characteristics | ||||||||||
| x.x.x: Demographic Characteristics - ITT Analysis Set | ||||||||||
| val_Placebo | val_Drug 1 | sd_Placebo | sd_Drug 1 | min_Placebo | min_Drug 1 | max_Placebo | max_Drug 1 | pct_Placebo | pct_Drug 1 | |
|---|---|---|---|---|---|---|---|---|---|---|
| Age (Years) | ||||||||||
| n | 90.00000 | 90.00000 | NA | NA | NA | NA | NA | NA | NA | NA |
| Mean (SD) | 41.16667 | 39.17778 | 6.137470 | 5.620144 | NA | NA | NA | NA | NA | NA |
| Median | 41.00000 | 38.50000 | NA | NA | NA | NA | NA | NA | NA | NA |
| Min - Max | NA | NA | NA | NA | 27.00000 | 29.00000 | 56.00000 | 55.00000 | NA | NA |
| Age Group, n (%) | ||||||||||
| <40 | 36.00000 | 49.00000 | NA | NA | NA | NA | NA | NA | 0.40000000 | 0.54444444 |
| >=40 | 54.00000 | 41.00000 | NA | NA | NA | NA | NA | NA | 0.60000000 | 0.45555556 |
| Sex, n (%) | ||||||||||
| Male | 59.00000 | 57.00000 | NA | NA | NA | NA | NA | NA | 0.65555556 | 0.63333333 |
| Female | 25.00000 | 32.00000 | NA | NA | NA | NA | NA | NA | 0.27777778 | 0.35555556 |
| Undifferentiated | 6.00000 | 1.00000 | NA | NA | NA | NA | NA | NA | 0.06666667 | 0.01111111 |
| Ethnicity, n (%) | ||||||||||
| Hispanic or Latino | 38.00000 | 29.00000 | NA | NA | NA | NA | NA | NA | 0.42222222 | 0.32222222 |
| Not Hispanic or Latino | 46.00000 | 50.00000 | NA | NA | NA | NA | NA | NA | 0.51111111 | 0.55555556 |
| Missing | 6.00000 | 11.00000 | NA | NA | NA | NA | NA | NA | 0.06666667 | 0.12222222 |
| Body Mass Index (kg/m2) | ||||||||||
| n | 90.00000 | 90.00000 | NA | NA | NA | NA | NA | NA | NA | NA |
| Mean (SD) | 26.17161 | 27.16897 | 4.884631 | 4.973972 | NA | NA | NA | NA | NA | NA |
| Median | 26.04285 | 26.89705 | NA | NA | NA | NA | NA | NA | NA | NA |
| Min - Max | NA | NA | NA | NA | 18.08861 | 18.02748 | 34.48886 | 34.80553 | NA | NA |
As a first step, let's try to format the columns, formatting counts, min, max and medians with fmt_integer(), percentages with fmt_percent(), and mean and sd values with fmt_number() using 1 and 2 decimals, respectively. We are intentionally keeping the NA values for now, as these will be needed in the cols_merge() pattern in the next step.
let rxAdslFmt = tabella(rxAdslTbl):
fmtInteger startsWith(["val_", "min_", "max_"]),
rows = where($it.label in ["n", "Median", "Min - Max"] or ($it.category).endsWith(", n (%)"))
fmtPercent startsWith("pct_"), decimals = 1
fmtNumber startsWith("val_"), rows = where(it.label == "Mean (SD)"), decimals = 1
fmtNumber startsWith("sd_"), rows = where(it.label == "Mean (SD)"), decimals = 2
nbTable rxAdslFmt
| x.x: Demographic Characteristics | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| x.x.x: Demographic Characteristics - ITT Analysis Set | ||||||||||
| val_Placebo | val_Drug 1 | sd_Placebo | sd_Drug 1 | min_Placebo | min_Drug 1 | max_Placebo | max_Drug 1 | pct_Placebo | pct_Drug 1 | |
| Age (Years) | ||||||||||
| n | 90 | 90 | ||||||||
| Mean (SD) | 41.2 | 39.2 | 6.14 | 5.62 | ||||||
| Median | 41 | 38 | ||||||||
| Min - Max | 27 | 29 | 56 | 55 | ||||||
| Age Group, n (%) | ||||||||||
| <40 | 36 | 49 | 40.0% | 54.4% | ||||||
| >=40 | 54 | 41 | 60.0% | 45.6% | ||||||
| Sex, n (%) | ||||||||||
| Male | 59 | 57 | 65.6% | 63.3% | ||||||
| Female | 25 | 32 | 27.8% | 35.6% | ||||||
| Undifferentiated | 6 | 1 | 6.7% | 1.1% | ||||||
| Ethnicity, n (%) | ||||||||||
| Hispanic or Latino | 38 | 29 | 42.2% | 32.2% | ||||||
| Not Hispanic or Latino | 46 | 50 | 51.1% | 55.6% | ||||||
| Missing | 6 | 11 | 6.7% | 12.2% | ||||||
| Body Mass Index (kg/m2) | ||||||||||
| n | 90 | 90 | ||||||||
| Mean (SD) | 26.2 | 27.2 | 4.88 | 4.97 | ||||||
| Median | 26 | 27 | ||||||||
| Min - Max | 18 | 18 | 34 | 35 | ||||||
rx_adsl_tbl = (
rx_adsl_tbl
.fmt_integer(
columns=cs.starts_with("val_", "min_", "max_"),
# (and the counts, which R shows as 36 unformatted, Polars as 36.0)
rows=pl.col("label").is_in(["n", "Median", "Min - Max"])
| pl.col("category").str.ends_with(", n (%)"),
)
.fmt_percent(columns=cs.starts_with("pct_"), decimals=1)
.fmt_number(
columns=cs.starts_with("val_"), rows=pl.col("label") == "Mean (SD)", decimals=1
)
.fmt_number(
columns=cs.starts_with("sd_"), rows=pl.col("label") == "Mean (SD)", decimals=2
)
)
rx_adsl_tbl| x.x: Demographic Characteristics | ||||||||||
| x.x.x: Demographic Characteristics - ITT Analysis Set | ||||||||||
| val_Placebo | val_Drug 1 | sd_Placebo | sd_Drug 1 | min_Placebo | max_Placebo | min_Drug 1 | max_Drug 1 | pct_Placebo | pct_Drug 1 | |
|---|---|---|---|---|---|---|---|---|---|---|
| Age (Years) | ||||||||||
| n | 90 | 90 | None | None | None | None | None | None | None | None |
| Mean (SD) | 41.2 | 39.2 | 6.14 | 5.62 | None | None | None | None | None | None |
| Median | 41 | 38 | None | None | None | None | None | None | None | None |
| Min - Max | None | None | None | None | 27 | 56 | 29 | 55 | None | None |
| Age Group, n (%) | ||||||||||
| <40 | 36 | 49 | None | None | None | None | None | None | 40.0% | 54.4% |
| >=40 | 54 | 41 | None | None | None | None | None | None | 60.0% | 45.6% |
| Sex, n (%) | ||||||||||
| Male | 59 | 57 | None | None | None | None | None | None | 65.6% | 63.3% |
| Female | 25 | 32 | None | None | None | None | None | None | 27.8% | 35.6% |
| Undifferentiated | 6 | 1 | None | None | None | None | None | None | 6.7% | 1.1% |
| Ethnicity, n (%) | ||||||||||
| Hispanic or Latino | 38 | 29 | None | None | None | None | None | None | 42.2% | 32.2% |
| Not Hispanic or Latino | 46 | 50 | None | None | None | None | None | None | 51.1% | 55.6% |
| Missing | 6 | 11 | None | None | None | None | None | None | 6.7% | 12.2% |
| Body Mass Index (kg/m2) | ||||||||||
| n | 90 | 90 | None | None | None | None | None | None | None | None |
| Mean (SD) | 26.2 | 27.2 | 4.88 | 4.97 | None | None | None | None | None | None |
| Median | 26 | 27 | None | None | None | None | None | None | None | None |
| Min - Max | None | None | None | None | 18 | 34 | 18 | 35 | None | None |
rx_adsl_tbl <-
rx_adsl_tbl |>
fmt_integer(
columns = starts_with(c("val_", "min_", "max_")),
rows = label %in% c("n", "Median", "Min - Max")
) |>
fmt_percent(
columns = starts_with("pct_"),
decimals = 1
) |>
fmt_number(
columns = starts_with("val_"),
rows = label == "Mean (SD)",
decimals = 1
) |>
fmt_number(
columns = starts_with("sd_"),
rows = label == "Mean (SD)",
decimals = 2
)
rx_adsl_tbl| x.x: Demographic Characteristics | ||||||||||
| x.x.x: Demographic Characteristics - ITT Analysis Set | ||||||||||
| val_Placebo | val_Drug 1 | sd_Placebo | sd_Drug 1 | min_Placebo | min_Drug 1 | max_Placebo | max_Drug 1 | pct_Placebo | pct_Drug 1 | |
|---|---|---|---|---|---|---|---|---|---|---|
| Age (Years) | ||||||||||
| n | 90 | 90 | NA | NA | NA | NA | NA | NA | NA | NA |
| Mean (SD) | 41.2 | 39.2 | 6.14 | 5.62 | NA | NA | NA | NA | NA | NA |
| Median | 41 | 38 | NA | NA | NA | NA | NA | NA | NA | NA |
| Min - Max | NA | NA | NA | NA | 27 | 29 | 56 | 55 | NA | NA |
| Age Group, n (%) | ||||||||||
| <40 | 36 | 49 | NA | NA | NA | NA | NA | NA | 40.0% | 54.4% |
| >=40 | 54 | 41 | NA | NA | NA | NA | NA | NA | 60.0% | 45.6% |
| Sex, n (%) | ||||||||||
| Male | 59 | 57 | NA | NA | NA | NA | NA | NA | 65.6% | 63.3% |
| Female | 25 | 32 | NA | NA | NA | NA | NA | NA | 27.8% | 35.6% |
| Undifferentiated | 6 | 1 | NA | NA | NA | NA | NA | NA | 6.7% | 1.1% |
| Ethnicity, n (%) | ||||||||||
| Hispanic or Latino | 38 | 29 | NA | NA | NA | NA | NA | NA | 42.2% | 32.2% |
| Not Hispanic or Latino | 46 | 50 | NA | NA | NA | NA | NA | NA | 51.1% | 55.6% |
| Missing | 6 | 11 | NA | NA | NA | NA | NA | NA | 6.7% | 12.2% |
| Body Mass Index (kg/m2) | ||||||||||
| n | 90 | 90 | NA | NA | NA | NA | NA | NA | NA | NA |
| Mean (SD) | 26.2 | 27.2 | 4.88 | 4.97 | NA | NA | NA | NA | NA | NA |
| Median | 26 | 27 | NA | NA | NA | NA | NA | NA | NA | NA |
| Min - Max | NA | NA | NA | NA | 18 | 18 | 34 | 35 | NA | NA |
This looks way better but our table still has a rather wide style. To collapse the columns appropriately, we will use cols_merge(), combining mean and SD, min and max, as well as n and percentages, respectively. We will use the pattern argument to specify our custom merging pattern.
let rxAdslMerged = tabella(rxAdslFmt):
for arm in arms:
colsMerge ["val_", "pct_", "sd_", "min_", "max_"].mapIt(it & arm),
pattern = "<<{1}>><< ({2})>><< ({3})>><<{4} - {5}>>"
nbTable rxAdslMerged
| x.x: Demographic Characteristics | ||
|---|---|---|
| x.x.x: Demographic Characteristics - ITT Analysis Set | ||
| val_Placebo | val_Drug 1 | |
| Age (Years) | ||
| n | 90 | 90 |
| Mean (SD) | 41.2 (6.14) | 39.2 (5.62) |
| Median | 41 | 38 |
| Min - Max | 27 - 56 | 29 - 55 |
| Age Group, n (%) | ||
| <40 | 36 (40.0%) | 49 (54.4%) |
| >=40 | 54 (60.0%) | 41 (45.6%) |
| Sex, n (%) | ||
| Male | 59 (65.6%) | 57 (63.3%) |
| Female | 25 (27.8%) | 32 (35.6%) |
| Undifferentiated | 6 (6.7%) | 1 (1.1%) |
| Ethnicity, n (%) | ||
| Hispanic or Latino | 38 (42.2%) | 29 (32.2%) |
| Not Hispanic or Latino | 46 (51.1%) | 50 (55.6%) |
| Missing | 6 (6.7%) | 11 (12.2%) |
| Body Mass Index (kg/m2) | ||
| n | 90 | 90 |
| Mean (SD) | 26.2 (4.88) | 27.2 (4.97) |
| Median | 26 | 27 |
| Min - Max | 18 - 34 | 18 - 35 |
rx_adsl_tbl = rx_adsl_tbl.cols_merge(
columns=["val_Placebo", "pct_Placebo", "sd_Placebo", "min_Placebo", "max_Placebo"],
pattern="<<{0}>><< ({1})>><< ({2})>><<{3} - {4}>>",
).cols_merge(
columns=["val_Drug 1", "pct_Drug 1", "sd_Drug 1", "min_Drug 1", "max_Drug 1"],
pattern="<<{0}>><< ({1})>><< ({2})>><<{3} - {4}>>",
)
rx_adsl_tbl| x.x: Demographic Characteristics | ||
| x.x.x: Demographic Characteristics - ITT Analysis Set | ||
| val_Placebo | val_Drug 1 | |
|---|---|---|
| Age (Years) | ||
| n | 90 | 90 |
| Mean (SD) | 41.2 (6.14) | 39.2 (5.62) |
| Median | 41 | 38 |
| Min - Max | 27 - 56 | 29 - 55 |
| Age Group, n (%) | ||
| <40 | 36 (40.0%) | 49 (54.4%) |
| >=40 | 54 (60.0%) | 41 (45.6%) |
| Sex, n (%) | ||
| Male | 59 (65.6%) | 57 (63.3%) |
| Female | 25 (27.8%) | 32 (35.6%) |
| Undifferentiated | 6 (6.7%) | 1 (1.1%) |
| Ethnicity, n (%) | ||
| Hispanic or Latino | 38 (42.2%) | 29 (32.2%) |
| Not Hispanic or Latino | 46 (51.1%) | 50 (55.6%) |
| Missing | 6 (6.7%) | 11 (12.2%) |
| Body Mass Index (kg/m2) | ||
| n | 90 | 90 |
| Mean (SD) | 26.2 (4.88) | 27.2 (4.97) |
| Median | 26 | 27 |
| Min - Max | 18 - 34 | 18 - 35 |
rx_adsl_tbl <-
rx_adsl_tbl |>
cols_merge(
columns = c("val_Placebo", "pct_Placebo", "sd_Placebo", "min_Placebo", "max_Placebo"),
pattern = "<<{1}>><< ({2})>><< ({3})>><<{4} - {5}>>"
) |>
cols_merge(
columns = c("val_Drug 1", "pct_Drug 1", "sd_Drug 1", "min_Drug 1", "max_Drug 1"),
pattern = "<<{1}>><< ({2})>><< ({3})>><<{4} - {5}>>"
)
rx_adsl_tbl| x.x: Demographic Characteristics | ||
| x.x.x: Demographic Characteristics - ITT Analysis Set | ||
| val_Placebo | val_Drug 1 | |
|---|---|---|
| Age (Years) | ||
| n | 90 | 90 |
| Mean (SD) | 41.2 (6.14) | 39.2 (5.62) |
| Median | 41 | 38 |
| Min - Max | 27 - 56 | 29 - 55 |
| Age Group, n (%) | ||
| <40 | 36 (40.0%) | 49 (54.4%) |
| >=40 | 54 (60.0%) | 41 (45.6%) |
| Sex, n (%) | ||
| Male | 59 (65.6%) | 57 (63.3%) |
| Female | 25 (27.8%) | 32 (35.6%) |
| Undifferentiated | 6 (6.7%) | 1 (1.1%) |
| Ethnicity, n (%) | ||
| Hispanic or Latino | 38 (42.2%) | 29 (32.2%) |
| Not Hispanic or Latino | 46 (51.1%) | 50 (55.6%) |
| Missing | 6 (6.7%) | 11 (12.2%) |
| Body Mass Index (kg/m2) | ||
| n | 90 | 90 |
| Mean (SD) | 26.2 (4.88) | 27.2 (4.97) |
| Median | 26 | 27 |
| Min - Max | 18 - 34 | 18 - 35 |
Now that looks more like a demographic table. But let's take a step back and understand the merging pattern. { } are used to arrange the single column values in a row-wise fashion. The number in curly braces corresponds to the order specified in the columns = argument. We use << >> to surround spans of text that will be omitted if any of the values within contain missing values. Our first column in the call to cols_merge() contains values for n's, means and medians and is printed if the values are not missing (meaning for the cells for numeric and categorical n's, means and medians but not for min and max). The SD and percentages for categorical grouping variables are then appended to the cells for mean and categorical n's, because these are the only rows that contain non-missing values. All of the previous aspects are ignored for the min and max row, as n's, percentages, SD's and medians are missing. Here, only min and max are arranged.
We can now start to look in to style features. Let us indent the values in the stub using tab_stub_indent() and left-align the title with opt_align_table_header().
let rxAdslIndented = tabella(rxAdslMerged):
tabStubIndent allRows(), 5
optAlignTableHeader "left"
nbTable rxAdslIndented
| x.x: Demographic Characteristics | ||
|---|---|---|
| x.x.x: Demographic Characteristics - ITT Analysis Set | ||
| val_Placebo | val_Drug 1 | |
| Age (Years) | ||
| n | 90 | 90 |
| Mean (SD) | 41.2 (6.14) | 39.2 (5.62) |
| Median | 41 | 38 |
| Min - Max | 27 - 56 | 29 - 55 |
| Age Group, n (%) | ||
| <40 | 36 (40.0%) | 49 (54.4%) |
| >=40 | 54 (60.0%) | 41 (45.6%) |
| Sex, n (%) | ||
| Male | 59 (65.6%) | 57 (63.3%) |
| Female | 25 (27.8%) | 32 (35.6%) |
| Undifferentiated | 6 (6.7%) | 1 (1.1%) |
| Ethnicity, n (%) | ||
| Hispanic or Latino | 38 (42.2%) | 29 (32.2%) |
| Not Hispanic or Latino | 46 (51.1%) | 50 (55.6%) |
| Missing | 6 (6.7%) | 11 (12.2%) |
| Body Mass Index (kg/m2) | ||
| n | 90 | 90 |
| Mean (SD) | 26.2 (4.88) | 27.2 (4.97) |
| Median | 26 | 27 |
| Min - Max | 18 - 34 | 18 - 35 |
rx_adsl_tbl = rx_adsl_tbl.tab_stub_indent(
rows=list(range(len(adsl_summary))), indent=5
).opt_align_table_header(align="left")
rx_adsl_tbl| x.x: Demographic Characteristics | ||
| x.x.x: Demographic Characteristics - ITT Analysis Set | ||
| val_Placebo | val_Drug 1 | |
|---|---|---|
| Age (Years) | ||
| n | 90 | 90 |
| Mean (SD) | 41.2 (6.14) | 39.2 (5.62) |
| Median | 41 | 38 |
| Min - Max | 27 - 56 | 29 - 55 |
| Age Group, n (%) | ||
| <40 | 36 (40.0%) | 49 (54.4%) |
| >=40 | 54 (60.0%) | 41 (45.6%) |
| Sex, n (%) | ||
| Male | 59 (65.6%) | 57 (63.3%) |
| Female | 25 (27.8%) | 32 (35.6%) |
| Undifferentiated | 6 (6.7%) | 1 (1.1%) |
| Ethnicity, n (%) | ||
| Hispanic or Latino | 38 (42.2%) | 29 (32.2%) |
| Not Hispanic or Latino | 46 (51.1%) | 50 (55.6%) |
| Missing | 6 (6.7%) | 11 (12.2%) |
| Body Mass Index (kg/m2) | ||
| n | 90 | 90 |
| Mean (SD) | 26.2 (4.88) | 27.2 (4.97) |
| Median | 26 | 27 |
| Min - Max | 18 - 34 | 18 - 35 |
rx_adsl_tbl <-
rx_adsl_tbl |>
tab_stub_indent(
rows = everything(),
indent = 5
) |>
opt_align_table_header(align = "left")
rx_adsl_tbl| x.x: Demographic Characteristics | ||
| x.x.x: Demographic Characteristics - ITT Analysis Set | ||
| val_Placebo | val_Drug 1 | |
|---|---|---|
| Age (Years) | ||
| n | 90 | 90 |
| Mean (SD) | 41.2 (6.14) | 39.2 (5.62) |
| Median | 41 | 38 |
| Min - Max | 27 - 56 | 29 - 55 |
| Age Group, n (%) | ||
| <40 | 36 (40.0%) | 49 (54.4%) |
| >=40 | 54 (60.0%) | 41 (45.6%) |
| Sex, n (%) | ||
| Male | 59 (65.6%) | 57 (63.3%) |
| Female | 25 (27.8%) | 32 (35.6%) |
| Undifferentiated | 6 (6.7%) | 1 (1.1%) |
| Ethnicity, n (%) | ||
| Hispanic or Latino | 38 (42.2%) | 29 (32.2%) |
| Not Hispanic or Latino | 46 (51.1%) | 50 (55.6%) |
| Missing | 6 (6.7%) | 11 (12.2%) |
| Body Mass Index (kg/m2) | ||
| n | 90 | 90 |
| Mean (SD) | 26.2 (4.88) | 27.2 (4.97) |
| Median | 26 | 27 |
| Min - Max | 18 - 34 | 18 - 35 |
Let's now change the column width of our Placebo and Drug 1 columns and align all values to the center, making use of cols_width() and cols_align().
let rxAdslAligned = tabella(rxAdslIndented):
colsWidth @[("label", "250px")] & arms.mapIt(("val_" & it, "200px"))
colsAlign alCenter, startsWith("val_")
nbTable rxAdslAligned
| x.x: Demographic Characteristics | ||
|---|---|---|
| x.x.x: Demographic Characteristics - ITT Analysis Set | ||
| val_Placebo | val_Drug 1 | |
| Age (Years) | ||
| n | 90 | 90 |
| Mean (SD) | 41.2 (6.14) | 39.2 (5.62) |
| Median | 41 | 38 |
| Min - Max | 27 - 56 | 29 - 55 |
| Age Group, n (%) | ||
| <40 | 36 (40.0%) | 49 (54.4%) |
| >=40 | 54 (60.0%) | 41 (45.6%) |
| Sex, n (%) | ||
| Male | 59 (65.6%) | 57 (63.3%) |
| Female | 25 (27.8%) | 32 (35.6%) |
| Undifferentiated | 6 (6.7%) | 1 (1.1%) |
| Ethnicity, n (%) | ||
| Hispanic or Latino | 38 (42.2%) | 29 (32.2%) |
| Not Hispanic or Latino | 46 (51.1%) | 50 (55.6%) |
| Missing | 6 (6.7%) | 11 (12.2%) |
| Body Mass Index (kg/m2) | ||
| n | 90 | 90 |
| Mean (SD) | 26.2 (4.88) | 27.2 (4.97) |
| Median | 26 | 27 |
| Min - Max | 18 - 34 | 18 - 35 |
rx_adsl_tbl = rx_adsl_tbl.cols_width(
{"label": "250px"} | {f"val_{arm}": "200px" for arm in arms}
).cols_align(align="center", columns=cs.starts_with("val_"))
rx_adsl_tbl| x.x: Demographic Characteristics | ||
| x.x.x: Demographic Characteristics - ITT Analysis Set | ||
| val_Placebo | val_Drug 1 | |
|---|---|---|
| Age (Years) | ||
| n | 90 | 90 |
| Mean (SD) | 41.2 (6.14) | 39.2 (5.62) |
| Median | 41 | 38 |
| Min - Max | 27 - 56 | 29 - 55 |
| Age Group, n (%) | ||
| <40 | 36 (40.0%) | 49 (54.4%) |
| >=40 | 54 (60.0%) | 41 (45.6%) |
| Sex, n (%) | ||
| Male | 59 (65.6%) | 57 (63.3%) |
| Female | 25 (27.8%) | 32 (35.6%) |
| Undifferentiated | 6 (6.7%) | 1 (1.1%) |
| Ethnicity, n (%) | ||
| Hispanic or Latino | 38 (42.2%) | 29 (32.2%) |
| Not Hispanic or Latino | 46 (51.1%) | 50 (55.6%) |
| Missing | 6 (6.7%) | 11 (12.2%) |
| Body Mass Index (kg/m2) | ||
| n | 90 | 90 |
| Mean (SD) | 26.2 (4.88) | 27.2 (4.97) |
| Median | 26 | 27 |
| Min - Max | 18 - 34 | 18 - 35 |
rx_adsl_tbl <-
rx_adsl_tbl |>
cols_width(
starts_with("val_") ~ px(200),
1 ~ px(250)
) |>
cols_align(
align = "center",
columns = starts_with("val_")
)
rx_adsl_tbl| x.x: Demographic Characteristics | ||
| x.x.x: Demographic Characteristics - ITT Analysis Set | ||
| val_Placebo | val_Drug 1 | |
|---|---|---|
| Age (Years) | ||
| n | 90 | 90 |
| Mean (SD) | 41.2 (6.14) | 39.2 (5.62) |
| Median | 41 | 38 |
| Min - Max | 27 - 56 | 29 - 55 |
| Age Group, n (%) | ||
| <40 | 36 (40.0%) | 49 (54.4%) |
| >=40 | 54 (60.0%) | 41 (45.6%) |
| Sex, n (%) | ||
| Male | 59 (65.6%) | 57 (63.3%) |
| Female | 25 (27.8%) | 32 (35.6%) |
| Undifferentiated | 6 (6.7%) | 1 (1.1%) |
| Ethnicity, n (%) | ||
| Hispanic or Latino | 38 (42.2%) | 29 (32.2%) |
| Not Hispanic or Latino | 46 (51.1%) | 50 (55.6%) |
| Missing | 6 (6.7%) | 11 (12.2%) |
| Body Mass Index (kg/m2) | ||
| n | 90 | 90 |
| Mean (SD) | 26.2 (4.88) | 27.2 (4.97) |
| Median | 26 | 27 |
| Min - Max | 18 - 34 | 18 - 35 |
In a final step we can now take care of the column names and assign something more meaningful. Out column header should be the name of the study intervention together with the respective subject count. To make use of cols_label()'s ability to handle lists, we summarize our new column labels in a named list.
# Count subjects per arm and make the labels of the columns
var collblList: seq[(string, string)]
for arm in arms:
let n = adslItt.filter(col("TRTA") == arm).height
collblList.add ("val_" & arm, arm & " N=" & $n & " (100%)")
let rxAdslLabelled = tabella(rxAdslAligned):
colsLabel collblList
nbTable rxAdslLabelled
| x.x: Demographic Characteristics | ||
|---|---|---|
| x.x.x: Demographic Characteristics - ITT Analysis Set | ||
| Placebo N=90 (100%) | Drug 1 N=90 (100%) | |
| Age (Years) | ||
| n | 90 | 90 |
| Mean (SD) | 41.2 (6.14) | 39.2 (5.62) |
| Median | 41 | 38 |
| Min - Max | 27 - 56 | 29 - 55 |
| Age Group, n (%) | ||
| <40 | 36 (40.0%) | 49 (54.4%) |
| >=40 | 54 (60.0%) | 41 (45.6%) |
| Sex, n (%) | ||
| Male | 59 (65.6%) | 57 (63.3%) |
| Female | 25 (27.8%) | 32 (35.6%) |
| Undifferentiated | 6 (6.7%) | 1 (1.1%) |
| Ethnicity, n (%) | ||
| Hispanic or Latino | 38 (42.2%) | 29 (32.2%) |
| Not Hispanic or Latino | 46 (51.1%) | 50 (55.6%) |
| Missing | 6 (6.7%) | 11 (12.2%) |
| Body Mass Index (kg/m2) | ||
| n | 90 | 90 |
| Mean (SD) | 26.2 (4.88) | 27.2 (4.97) |
| Median | 26 | 27 |
| Min - Max | 18 - 34 | 18 - 35 |
# Count subjects per arm and make the labels of the columns
arm_n = (
rx_adsl.filter(pl.col("ITTFL") == "Y")
.group_by("TRTA")
.len()
.sort(pl.col("TRTA").cast(pl.Enum(arms)))
)
collbl_list = {
f"val_{arm}": f"{arm} N={n} (100%)" for arm, n in arm_n.iter_rows()
}
rx_adsl_tbl = rx_adsl_tbl.cols_label(collbl_list)
rx_adsl_tbl| x.x: Demographic Characteristics | ||
| x.x.x: Demographic Characteristics - ITT Analysis Set | ||
| Placebo N=90 (100%) | Drug 1 N=90 (100%) | |
|---|---|---|
| Age (Years) | ||
| n | 90 | 90 |
| Mean (SD) | 41.2 (6.14) | 39.2 (5.62) |
| Median | 41 | 38 |
| Min - Max | 27 - 56 | 29 - 55 |
| Age Group, n (%) | ||
| <40 | 36 (40.0%) | 49 (54.4%) |
| >=40 | 54 (60.0%) | 41 (45.6%) |
| Sex, n (%) | ||
| Male | 59 (65.6%) | 57 (63.3%) |
| Female | 25 (27.8%) | 32 (35.6%) |
| Undifferentiated | 6 (6.7%) | 1 (1.1%) |
| Ethnicity, n (%) | ||
| Hispanic or Latino | 38 (42.2%) | 29 (32.2%) |
| Not Hispanic or Latino | 46 (51.1%) | 50 (55.6%) |
| Missing | 6 (6.7%) | 11 (12.2%) |
| Body Mass Index (kg/m2) | ||
| n | 90 | 90 |
| Mean (SD) | 26.2 (4.88) | 27.2 (4.97) |
| Median | 26 | 27 |
| Min - Max | 18 - 34 | 18 - 35 |
### Count subjects per arm and summarize values in a list
arm_n <-
rx_adsl |>
dplyr::filter(ITTFL == "Y") |>
dplyr::group_by(TRTA) |>
dplyr::summarize(
lbl = sprintf("%s N=%i (100%%)", unique(TRTA), dplyr::n()),
.groups = "drop"
) |>
dplyr::arrange(TRTA)
collbl_list <- as.list(arm_n$lbl)
names(collbl_list) <- paste0("val_", arm_n$TRTA)
rx_adsl_tbl <-
rx_adsl_tbl |>
cols_label(.list = collbl_list)
rx_adsl_tbl| x.x: Demographic Characteristics | ||
| x.x.x: Demographic Characteristics - ITT Analysis Set | ||
| Placebo N=90 (100%) | Drug 1 N=90 (100%) | |
|---|---|---|
| Age (Years) | ||
| n | 90 | 90 |
| Mean (SD) | 41.2 (6.14) | 39.2 (5.62) |
| Median | 41 | 38 |
| Min - Max | 27 - 56 | 29 - 55 |
| Age Group, n (%) | ||
| <40 | 36 (40.0%) | 49 (54.4%) |
| >=40 | 54 (60.0%) | 41 (45.6%) |
| Sex, n (%) | ||
| Male | 59 (65.6%) | 57 (63.3%) |
| Female | 25 (27.8%) | 32 (35.6%) |
| Undifferentiated | 6 (6.7%) | 1 (1.1%) |
| Ethnicity, n (%) | ||
| Hispanic or Latino | 38 (42.2%) | 29 (32.2%) |
| Not Hispanic or Latino | 46 (51.1%) | 50 (55.6%) |
| Missing | 6 (6.7%) | 11 (12.2%) |
| Body Mass Index (kg/m2) | ||
| n | 90 | 90 |
| Mean (SD) | 26.2 (4.88) | 27.2 (4.97) |
| Median | 26 | 27 |
| Min - Max | 18 - 34 | 18 - 35 |
Response / Event Rate Analysis Tables
In another table, we can summarize the number of subjects with an event per intervention in the subgroup defined by the age groups. Within each intervention group we are counting the number and percentage of participants with an event (EVNTFL == "Y") as well as the total number of participants. The number of participants with an event divided by the number without an event are the odds of experiencing the event per study intervention. The odds ratio is then computed as the odds under Drug 1 divided by the odds under Placebo.
The below code performs the calculation outlined above within the subgroup defined by AAGEGR1, where confidence intervals around the event rates are computed using the Clopper Pearson method.
# The probability of at most `k` events in `n`, each with probability `p`
proc binomCdf(k, n: int, p: float): float =
for i in 0 .. k:
result += exp(lgamma(float(n + 1)) - lgamma(float(i + 1)) - lgamma(float(n - i + 1)) +
float(i) * ln(p) + float(n - i) * ln(1 - p))
# The `p` in (0, 1) where `f`, increasing, is zero
proc bisect(f: proc (p: float): float): float =
var (lo, hi) = (0.0, 1.0)
for _ in 1 .. 60:
let mid = (lo + hi) / 2
if f(mid) < 0: lo = mid else: hi = mid
(lo + hi) / 2
# The Clopper-Pearson interval of `x` events in `n` (R's `qf()` gives the same)
proc clopperPearson(x, n: int, level = 0.95): (float, float) =
let a = (1 - level) / 2
let low = if x == 0: 0.0 else: bisect(proc (p: float): float = 1 - binomCdf(x - 1, n, p) - a)
let up = if x == n: 1.0 else: bisect(proc (p: float): float = a - binomCdf(x, n, p))
(low, up)
const z = 1.959963984540054 # R's qnorm(0.975)
let counts = adslItt
.groupBy("TRTA", "AAGEGR1")
.agg((col("EVNTFL") == "Y").sum.alias("n_resp"), len().alias("n_total"))
.sort([col("TRTA").cast(Enum(arms)),
col("AAGEGR1").cast(Enum(meta["AAGEGR1"]["levels"].to(seq[string])))])
# the intervals in Nim, one for each row: nanuq has no quantiles of the F distribution
let intervals = zip(counts.toList("n_resp", int), counts.toList("n_total", int))
.mapIt(clopperPearson(it[0], it[1]))
let (resp, total) = (col("n_resp"), col("n_total"))
proc byArm(name, arm: string): Expr = col(name & "_" & arm)
let rxResponders = counts
.withColumns(
series("ci_up", intervals.mapIt(100 * it[1])),
series("ci_low", intervals.mapIt(100 * it[0])))
.withColumns((100 * resp / total).alias("pct"), (resp / (total - resp)).alias("odds"))
.pivot("TRTA", index = "AAGEGR1",
values = ["n_resp", "n_total", "pct", "ci_up", "ci_low", "odds"])
.withColumns(
`when`(byArm("odds", "Placebo") == 0).then(nil)
.otherwise(byArm("odds", "Drug 1") / byArm("odds", "Placebo")).alias("or"),
sqrt(1 / byArm("n_resp", "Placebo") + 1 / byArm("n_resp", "Drug 1") +
1 / (byArm("n_total", "Placebo") - byArm("n_resp", "Placebo")) +
1 / (byArm("n_total", "Drug 1") - byArm("n_resp", "Drug 1"))).alias("se"))
.withColumns(
exp(log(col("or")) - z * col("se")).alias("or_ci_low"),
exp(log(col("or")) + z * col("se")).alias("or_ci_up"))
.drop(["odds_Placebo", "odds_Drug 1", "se"])# The probability of at most `k` events in `n`, each with probability `p`
def binom_cdf(k, n, p):
return sum(
exp(lgamma(n + 1) - lgamma(i + 1) - lgamma(n - i + 1) + i * log(p) + (n - i) * log(1 - p))
for i in range(k + 1)
)
# The `p` in (0, 1) where `f`, increasing, is zero
def bisect(f):
lo, hi = 0.0, 1.0
for _ in range(60):
mid = (lo + hi) / 2
lo, hi = (mid, hi) if f(mid) < 0 else (lo, mid)
return (lo + hi) / 2
# The Clopper-Pearson interval of `x` events in `n` (R's `qf()` gives the same)
def clopper_pearson(x, n, level=0.95):
a = (1 - level) / 2
low = 0.0 if x == 0 else bisect(lambda p: 1 - binom_cdf(x - 1, n, p) - a)
up = 1.0 if x == n else bisect(lambda p: a - binom_cdf(x, n, p))
return low, up
z = 1.959963984540054 # R's qnorm(0.975)
rows = []
for age in rx_meta["rx_adsl"]["AAGEGR1"]["levels"]:
row = {"AAGEGR1": age}
for arm in arms:
subjects = rx_adsl.filter(
(pl.col("ITTFL") == "Y") & (pl.col("TRTA") == arm) & (pl.col("AAGEGR1") == age)
)
x = (subjects["EVNTFL"] == "Y").sum()
n = len(subjects)
low, up = clopper_pearson(x, n)
row |= {
f"n_resp_{arm}": x, f"n_total_{arm}": n, f"pct_{arm}": 100 * x / n,
f"ci_up_{arm}": 100 * up, f"ci_low_{arm}": 100 * low, f"odds_{arm}": x / (n - x),
}
rows.append(row)
rx_responders = pl.DataFrame(rows)
rx_responders = (
rx_responders.with_columns(
**{
"or": pl.when(pl.col("odds_Placebo") == 0)
.then(None)
.otherwise(pl.col("odds_Drug 1") / pl.col("odds_Placebo"))
}
)
.with_columns(
se=(
1 / pl.col("n_resp_Placebo")
+ 1 / pl.col("n_resp_Drug 1")
+ 1 / (pl.col("n_total_Placebo") - pl.col("n_resp_Placebo"))
+ 1 / (pl.col("n_total_Drug 1") - pl.col("n_resp_Drug 1"))
).sqrt()
)
.with_columns(
or_ci_low=(pl.col("or").log() - z * pl.col("se")).exp(),
or_ci_up=(pl.col("or").log() + z * pl.col("se")).exp(),
)
.select(
"AAGEGR1",
*[f"{v}_{arm}" for v in ["n_resp", "n_total", "pct", "ci_up", "ci_low"] for arm in arms],
"or", "or_ci_low", "or_ci_up",
)
)rx_responders <-
rx_adsl |>
dplyr::filter(ITTFL == "Y") |>
dplyr::group_by(TRTA, AAGEGR1) |>
dplyr::summarize(
n_resp = sum(EVNTFL == "Y"),
n_total = dplyr::n(),
pct = 100 * sum(EVNTFL == "Y") / dplyr::n(),
ci_up = 100 * (
1 + (dplyr::n() - sum(EVNTFL == "Y")) / (
(sum(EVNTFL == "Y") + 1) * qf(
0.975,
2 * (sum(EVNTFL == "Y") + 1),
2 * (dplyr::n() - sum(EVNTFL == "Y"))
)
)
)^(-1),
ci_low = ifelse(
sum(EVNTFL == "Y") == 0,
0,
100 * (
1 + (dplyr::n() - sum(EVNTFL == "Y") + 1) /
(sum(EVNTFL == "Y") * qf(
0.025,
2 * sum(EVNTFL == "Y"),
2 * (dplyr::n() - sum(EVNTFL == "Y") + 1)
)
)
)^(-1)
),
odds = sum(EVNTFL == "Y") / (dplyr::n() - sum(EVNTFL == "Y")),
.groups = "drop"
) |>
tidyr::pivot_wider(
id_cols = AAGEGR1,
names_from = TRTA,
values_from = c(n_resp, n_total, pct, ci_up, ci_low, odds)
) |>
dplyr::mutate(
or = ifelse(
odds_Placebo == 0,
NA_real_,
!!sym("odds_Drug 1") / odds_Placebo
),
or_ci_low = exp(
log(or) - qnorm(0.975) * sqrt(
1 / n_resp_Placebo +
1 / !!sym("n_resp_Drug 1") +
1 / (n_total_Placebo - n_resp_Placebo) +
1 / (!!sym("n_total_Drug 1") - !!sym("n_resp_Drug 1"))
)
),
or_ci_up = exp(
log(or) + qnorm(0.975) * sqrt(
1 / n_resp_Placebo +
1 / !!sym("n_resp_Drug 1") +
1 / (n_total_Placebo - n_resp_Placebo) +
1 / (!!sym("n_total_Drug 1") - !!sym("n_resp_Drug 1"))
)
)
) |>
dplyr::select(-tidyselect::starts_with("odds_"))Let's first create a basic gt table with a left-aligned table title and subtitle. Here we are using tab_header() and opt_align_table_header() again.
let rxRespTbl = tabella(rxResponders):
tabHeader "x.x: Efficacy Data",
"x.x.x: Occurence of Event per Subgroup - {gt} Analysis Set"
optAlignTableHeader "left"
nbTable rxRespTbl
| x.x: Efficacy Data | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| x.x.x: Occurence of Event per Subgroup - {gt} Analysis Set | |||||||||||||
| AAGEGR1 | n_resp_Placebo | n_resp_Drug 1 | n_total_Placebo | n_total_Drug 1 | pct_Placebo | pct_Drug 1 | ci_up_Placebo | ci_up_Drug 1 | ci_low_Placebo | ci_low_Drug 1 | or | or_ci_low | or_ci_up |
| <40 | 17 | 33 | 36 | 49 | 47.22222222222222 | 67.34693877551021 | 64.51362014626898 | 80.05146619538974 | 30.405058871626157 | 52.45992945096554 | 2.3051470588235294 | 0.9506020017324771 | 5.58982934300424 |
| >=40 | 23 | 29 | 54 | 41 | 42.592592592592595 | 70.73170731707317 | 56.791735010475584 | 83.87014302856923 | 29.234726623708994 | 54.462601683463795 | 3.257246376811594 | 1.375206634801842 | 7.714952568405137 |
rx_resp_tbl = GT(rx_responders).tab_header(
title="x.x: Efficacy Data",
subtitle="x.x.x: Occurence of Event per Subgroup - {gt} Analysis Set",
).opt_align_table_header(align="left")
rx_resp_tbl| x.x: Efficacy Data | |||||||||||||
| x.x.x: Occurence of Event per Subgroup - {gt} Analysis Set | |||||||||||||
| AAGEGR1 | n_resp_Placebo | n_resp_Drug 1 | n_total_Placebo | n_total_Drug 1 | pct_Placebo | pct_Drug 1 | ci_up_Placebo | ci_up_Drug 1 | ci_low_Placebo | ci_low_Drug 1 | or | or_ci_low | or_ci_up |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| <40 | 17 | 33 | 36 | 49 | 47.22222222222222 | 67.34693877551021 | 64.51362014626898 | 80.05146619538984 | 30.405058871626444 | 52.459929450969376 | 2.3051470588235294 | 0.9506020017324771 | 5.58982934300424 |
| >=40 | 23 | 29 | 54 | 41 | 42.592592592592595 | 70.73170731707317 | 56.79173501047543 | 83.87014302856922 | 29.23472662370371 | 54.46260168346257 | 3.257246376811594 | 1.375206634801842 | 7.714952568405137 |
rx_resp_tbl <- rx_responders |>
gt() |>
tab_header(
title = "x.x: Efficacy Data",
subtitle = "x.x.x: Occurence of Event per Subgroup - {gt} Analysis Set"
) |>
opt_align_table_header(align = "left")
rx_resp_tbl| x.x: Efficacy Data | |||||||||||||
| x.x.x: Occurence of Event per Subgroup - {gt} Analysis Set | |||||||||||||
| Age Group | n_resp_Placebo | n_resp_Drug 1 | n_total_Placebo | n_total_Drug 1 | pct_Placebo | pct_Drug 1 | ci_up_Placebo | ci_up_Drug 1 | ci_low_Placebo | ci_low_Drug 1 | or | or_ci_low | or_ci_up |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| <40 | 17 | 33 | 36 | 49 | 47.22222 | 67.34694 | 64.51362 | 80.05147 | 30.40506 | 52.45993 | 2.305147 | 0.950602 | 5.589829 |
| >=40 | 23 | 29 | 54 | 41 | 42.59259 | 70.73171 | 56.79174 | 83.87014 | 29.23473 | 54.46260 | 3.257246 | 1.375207 | 7.714953 |
Next, we are formatting the columns for counts to integers with fmt_integer(), percentages and CI's around percentages as numbers with one decimal and odds ratio and the CI around the odds ratio as numbers with two decimals, in both cases using fmt_number().
let rxRespFmt = tabella(rxRespTbl):
fmtInteger startsWith("n_")
fmtNumber startsWith(["pct_", "ci_"]), decimals = 1
fmtNumber startsWith("or"), decimals = 2
nbTable rxRespFmt
| x.x: Efficacy Data | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| x.x.x: Occurence of Event per Subgroup - {gt} Analysis Set | |||||||||||||
| AAGEGR1 | n_resp_Placebo | n_resp_Drug 1 | n_total_Placebo | n_total_Drug 1 | pct_Placebo | pct_Drug 1 | ci_up_Placebo | ci_up_Drug 1 | ci_low_Placebo | ci_low_Drug 1 | or | or_ci_low | or_ci_up |
| <40 | 17 | 33 | 36 | 49 | 47.2 | 67.3 | 64.5 | 80.1 | 30.4 | 52.5 | 2.31 | 0.95 | 5.59 |
| >=40 | 23 | 29 | 54 | 41 | 42.6 | 70.7 | 56.8 | 83.9 | 29.2 | 54.5 | 3.26 | 1.38 | 7.71 |
rx_resp_tbl = (
rx_resp_tbl
.fmt_integer(columns=cs.starts_with("n_"))
.fmt_number(columns=cs.starts_with("pct_", "ci_"), decimals=1)
.fmt_number(columns=cs.starts_with("or"), decimals=2)
)
rx_resp_tbl| x.x: Efficacy Data | |||||||||||||
| x.x.x: Occurence of Event per Subgroup - {gt} Analysis Set | |||||||||||||
| AAGEGR1 | n_resp_Placebo | n_resp_Drug 1 | n_total_Placebo | n_total_Drug 1 | pct_Placebo | pct_Drug 1 | ci_up_Placebo | ci_up_Drug 1 | ci_low_Placebo | ci_low_Drug 1 | or | or_ci_low | or_ci_up |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| <40 | 17 | 33 | 36 | 49 | 47.2 | 67.3 | 64.5 | 80.1 | 30.4 | 52.5 | 2.31 | 0.95 | 5.59 |
| >=40 | 23 | 29 | 54 | 41 | 42.6 | 70.7 | 56.8 | 83.9 | 29.2 | 54.5 | 3.26 | 1.38 | 7.71 |
rx_resp_tbl <-
rx_resp_tbl |>
fmt_integer(columns = starts_with("n_")) |>
fmt_number(columns = starts_with(c("pct_", "ci_")), decimals = 1) |>
fmt_number(columns = starts_with("or"), decimals = 2)
rx_resp_tbl| x.x: Efficacy Data | |||||||||||||
| x.x.x: Occurence of Event per Subgroup - {gt} Analysis Set | |||||||||||||
| Age Group | n_resp_Placebo | n_resp_Drug 1 | n_total_Placebo | n_total_Drug 1 | pct_Placebo | pct_Drug 1 | ci_up_Placebo | ci_up_Drug 1 | ci_low_Placebo | ci_low_Drug 1 | or | or_ci_low | or_ci_up |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| <40 | 17 | 33 | 36 | 49 | 47.2 | 67.3 | 64.5 | 80.1 | 30.4 | 52.5 | 2.31 | 0.95 | 5.59 |
| >=40 | 23 | 29 | 54 | 41 | 42.6 | 70.7 | 56.8 | 83.9 | 29.2 | 54.5 | 3.26 | 1.38 | 7.71 |
We can now merge the columns for participants with events, total number of participants and percentage of participants with events, as well as the 95% CI's around the event rate using cols_merge(). To indicate the intervention group we are adding tab spanners with tab_spanner().
let rxRespMerged = tabella(rxRespFmt):
for arm in arms:
colsMerge ["n_resp_" & arm, "n_total_" & arm, "pct_" & arm], pattern = "{1}/{2} ({3})"
colsMerge ["ci_low_" & arm, "ci_up_" & arm], pattern = "[{1}, {2}]"
colsMerge ["or_ci_low", "or_ci_up"], pattern = "[{1}, {2}]"
tabSpanner "Drug 1", cols("n_resp_Drug 1", "ci_low_Drug 1")
tabSpanner "Placebo", cols("n_resp_Placebo", "ci_low_Placebo")
nbTable rxRespMerged
| x.x: Efficacy Data | ||||||
|---|---|---|---|---|---|---|
| x.x.x: Occurence of Event per Subgroup - {gt} Analysis Set | ||||||
| AAGEGR1 | Placebo | Drug 1 | or | or_ci_low | ||
| n_resp_Placebo | ci_low_Placebo | n_resp_Drug 1 | ci_low_Drug 1 | |||
| <40 | 17/36 (47.2) | [30.4, 64.5] | 33/49 (67.3) | [52.5, 80.1] | 2.31 | [0.95, 5.59] |
| >=40 | 23/54 (42.6) | [29.2, 56.8] | 29/41 (70.7) | [54.5, 83.9] | 3.26 | [1.38, 7.71] |
rx_resp_tbl = (
rx_resp_tbl
.cols_merge(
columns=["n_resp_Placebo", "n_total_Placebo", "pct_Placebo"], pattern="{0}/{1} ({2})"
)
.cols_merge(
columns=["n_resp_Drug 1", "n_total_Drug 1", "pct_Drug 1"], pattern="{0}/{1} ({2})"
)
.cols_merge(columns=["ci_low_Placebo", "ci_up_Placebo"], pattern="[{0}, {1}]")
.cols_merge(columns=["ci_low_Drug 1", "ci_up_Drug 1"], pattern="[{0}, {1}]")
.cols_merge(columns=["or_ci_low", "or_ci_up"], pattern="[{0}, {1}]")
.tab_spanner(label="Drug 1", columns=["n_resp_Drug 1", "ci_low_Drug 1"])
.tab_spanner(label="Placebo", columns=["n_resp_Placebo", "ci_low_Placebo"])
)
rx_resp_tbl| x.x: Efficacy Data | ||||||
| x.x.x: Occurence of Event per Subgroup - {gt} Analysis Set | ||||||
| AAGEGR1 | Placebo | Drug 1 | or | or_ci_low | ||
|---|---|---|---|---|---|---|
| n_resp_Placebo | ci_low_Placebo | n_resp_Drug 1 | ci_low_Drug 1 | |||
| <40 | 17/36 (47.2) | [30.4, 64.5] | 33/49 (67.3) | [52.5, 80.1] | 2.31 | [0.95, 5.59] |
| >=40 | 23/54 (42.6) | [29.2, 56.8] | 29/41 (70.7) | [54.5, 83.9] | 3.26 | [1.38, 7.71] |
rx_resp_tbl <-
rx_resp_tbl |>
cols_merge(
columns = c("n_resp_Placebo", "n_total_Placebo", "pct_Placebo"),
pattern = "{1}/{2} ({3})"
) |>
cols_merge(
columns = c("n_resp_Drug 1", "n_total_Drug 1", "pct_Drug 1"),
pattern = "{1}/{2} ({3})"
) |>
cols_merge(
columns = c("ci_low_Placebo", "ci_up_Placebo"),
pattern = "[{1}, {2}]"
) |>
cols_merge(
columns = c("ci_low_Drug 1", "ci_up_Drug 1"),
pattern = "[{1}, {2}]"
) |>
cols_merge(
columns = c("or_ci_low", "or_ci_up"),
pattern = "[{1}, {2}]"
) |>
tab_spanner(
label = "Drug 1",
columns = c("n_resp_Drug 1", "ci_low_Drug 1")
) |>
tab_spanner(
label = "Placebo",
columns = c("n_resp_Placebo", "ci_low_Placebo")
)
rx_resp_tbl| x.x: Efficacy Data | ||||||
| x.x.x: Occurence of Event per Subgroup - {gt} Analysis Set | ||||||
| Age Group |
Placebo
|
Drug 1
|
or | or_ci_low | ||
|---|---|---|---|---|---|---|
| n_resp_Placebo | ci_low_Placebo | n_resp_Drug 1 | ci_low_Drug 1 | |||
| <40 | 17/36 (47.2) | [30.4, 64.5] | 33/49 (67.3) | [52.5, 80.1] | 2.31 | [0.95, 5.59] |
| >=40 | 23/54 (42.6) | [29.2, 56.8] | 29/41 (70.7) | [54.5, 83.9] | 3.26 | [1.38, 7.71] |
The table is looking way better now. Let's now group the two categories and highlight the fact that these are actually age subgroups. We are using tab_row_group() to manually add a row group label Age.
let rxRespGrouped = tabella(rxRespMerged):
tabRowGroup "Age", allRows()
nbTable rxRespGrouped
| x.x: Efficacy Data | ||||||
|---|---|---|---|---|---|---|
| x.x.x: Occurence of Event per Subgroup - {gt} Analysis Set | ||||||
| AAGEGR1 | Placebo | Drug 1 | or | or_ci_low | ||
| n_resp_Placebo | ci_low_Placebo | n_resp_Drug 1 | ci_low_Drug 1 | |||
| Age | ||||||
| <40 | 17/36 (47.2) | [30.4, 64.5] | 33/49 (67.3) | [52.5, 80.1] | 2.31 | [0.95, 5.59] |
| >=40 | 23/54 (42.6) | [29.2, 56.8] | 29/41 (70.7) | [54.5, 83.9] | 3.26 | [1.38, 7.71] |
Great Tables has no version of this example.
rx_resp_tbl <-
rx_resp_tbl |>
tab_row_group(
label = "Age",
rows = everything()
)
rx_resp_tbl| x.x: Efficacy Data | ||||||
| x.x.x: Occurence of Event per Subgroup - {gt} Analysis Set | ||||||
| Age Group |
Placebo
|
Drug 1
|
or | or_ci_low | ||
|---|---|---|---|---|---|---|
| n_resp_Placebo | ci_low_Placebo | n_resp_Drug 1 | ci_low_Drug 1 | |||
| Age | ||||||
| <40 | 17/36 (47.2) | [30.4, 64.5] | 33/49 (67.3) | [52.5, 80.1] | 2.31 | [0.95, 5.59] |
| >=40 | 23/54 (42.6) | [29.2, 56.8] | 29/41 (70.7) | [54.5, 83.9] | 3.26 | [1.38, 7.71] |
Next, we'll take care of the column labels. As we now have the tab_row_group() label in place, we no longer need the label for the first column and can assign an empty string. Also, because of the two tab spanners, we can assign equal column labels for event rates and 95% CI's in both intervention groups.
Using cols_width() and cols_align() we can apply a more convenient column width and left-align the first column.
let rxRespLabelled = tabella(rxRespGrouped):
colsAlign alCenter, startsWith(["n_", "ci", "or"])
colsLabel:
AAGEGR1: ""
n_resp_Placebo: "Event Rate (%)"
ci_low_Placebo: "[95% CI]"
"n_resp_Drug 1": "Event Rate (%)"
"ci_low_Drug 1": "[95% CI]"
"or": "Odds ratio"
or_ci_low: "[95% CI]"
colsWidth rxResponders.columns.mapIt((it, if it == "AAGEGR1": "80px" else: "120px"))
colsAlign alLeft, positions(0)
nbTable rxRespLabelled
| x.x: Efficacy Data | ||||||
|---|---|---|---|---|---|---|
| x.x.x: Occurence of Event per Subgroup - {gt} Analysis Set | ||||||
| Placebo | Drug 1 | Odds ratio | [95% CI] | |||
| Event Rate (%) | [95% CI] | Event Rate (%) | [95% CI] | |||
| Age | ||||||
| <40 | 17/36 (47.2) | [30.4, 64.5] | 33/49 (67.3) | [52.5, 80.1] | 2.31 | [0.95, 5.59] |
| >=40 | 23/54 (42.6) | [29.2, 56.8] | 29/41 (70.7) | [54.5, 83.9] | 3.26 | [1.38, 7.71] |
rx_resp_tbl = (
rx_resp_tbl
.cols_align(align="center", columns=cs.starts_with("n_", "ci", "or"))
.cols_label(
{
"AAGEGR1": "",
"n_resp_Placebo": "Event Rate (%)",
"ci_low_Placebo": "[95% CI]",
"n_resp_Drug 1": "Event Rate (%)",
"ci_low_Drug 1": "[95% CI]",
"or": "Odds ratio",
"or_ci_low": "[95% CI]",
}
)
.cols_width({"AAGEGR1": "80px"} | {c: "120px" for c in rx_responders.columns[1:]})
.cols_align(align="left", columns="AAGEGR1")
)
rx_resp_tbl| x.x: Efficacy Data | ||||||
| x.x.x: Occurence of Event per Subgroup - {gt} Analysis Set | ||||||
| Placebo | Drug 1 | Odds ratio | [95% CI] | |||
|---|---|---|---|---|---|---|
| Event Rate (%) | [95% CI] | Event Rate (%) | [95% CI] | |||
| <40 | 17/36 (47.2) | [30.4, 64.5] | 33/49 (67.3) | [52.5, 80.1] | 2.31 | [0.95, 5.59] |
| >=40 | 23/54 (42.6) | [29.2, 56.8] | 29/41 (70.7) | [54.5, 83.9] | 3.26 | [1.38, 7.71] |
rx_resp_tbl <-
rx_resp_tbl |>
cols_align(
align = "center",
columns = starts_with(c("n_", "ci", "or"))
) |>
cols_label(
.list = c(
"AAGEGR1" = "",
"n_resp_Placebo" = "Event Rate (%)",
"ci_low_Placebo" = "[95% CI]",
"n_resp_Drug 1" = "Event Rate (%)",
"ci_low_Drug 1" = "[95% CI]",
"or" = "Odds ratio",
"or_ci_low" = "[95% CI]"
)
) |>
cols_width(
1 ~ px(80),
everything() ~ px(120)
) |>
cols_align(align = "left", columns = 1)
rx_resp_tbl| x.x: Efficacy Data | ||||||
| x.x.x: Occurence of Event per Subgroup - {gt} Analysis Set | ||||||
|
Placebo
|
Drug 1
|
Odds ratio | [95% CI] | |||
|---|---|---|---|---|---|---|
| Event Rate (%) | [95% CI] | Event Rate (%) | [95% CI] | |||
| Age | ||||||
| <40 | 17/36 (47.2) | [30.4, 64.5] | 33/49 (67.3) | [52.5, 80.1] | 2.31 | [0.95, 5.59] |
| >=40 | 23/54 (42.6) | [29.2, 56.8] | 29/41 (70.7) | [54.5, 83.9] | 3.26 | [1.38, 7.71] |
Finally, we make use of tab_footnote() and can add a footnote to the columns with the 95% CI's around event rates, indicating that these were derived from the Clopper-Pearson method. To change the default symbol choice of tab_footnote() from numbers to letters, we add tab_options(footnote.marks = letters).
let rxRespFinal = tabella(rxRespLabelled):
tabFootnote "Event rate 95% exact confidence interval uses the Clopper−Pearson method.",
loc.columnLabels(cols("ci_low_Placebo", "ci_low_Drug 1"))
tabOptions:
footnotesMarks = fmLetters
nbTable rxRespFinal
| x.x: Efficacy Data | ||||||
|---|---|---|---|---|---|---|
| x.x.x: Occurence of Event per Subgroup - {gt} Analysis Set | ||||||
| Placebo | Drug 1 | Odds ratio | [95% CI] | |||
| Event Rate (%) | [95% CI]a | Event Rate (%) | [95% CI]a | |||
| Age | ||||||
| <40 | 17/36 (47.2) | [30.4, 64.5] | 33/49 (67.3) | [52.5, 80.1] | 2.31 | [0.95, 5.59] |
| >=40 | 23/54 (42.6) | [29.2, 56.8] | 29/41 (70.7) | [54.5, 83.9] | 3.26 | [1.38, 7.71] |
| a Event rate 95% exact confidence interval uses the Clopper−Pearson method. | ||||||
rx_resp_tbl = rx_resp_tbl.tab_footnote(
footnote="Event rate 95% exact confidence interval uses the Clopper−Pearson method.",
locations=loc.column_labels(columns=["ci_low_Placebo", "ci_low_Drug 1"]),
placement="right",
).tab_options(footnotes_marks="letters")
rx_resp_tbl| x.x: Efficacy Data | ||||||
| x.x.x: Occurence of Event per Subgroup - {gt} Analysis Set | ||||||
| Placebo | Drug 1 | Odds ratio | [95% CI] | |||
|---|---|---|---|---|---|---|
| Event Rate (%) | [95% CI]a | Event Rate (%) | [95% CI]a | |||
| <40 | 17/36 (47.2) | [30.4, 64.5] | 33/49 (67.3) | [52.5, 80.1] | 2.31 | [0.95, 5.59] |
| >=40 | 23/54 (42.6) | [29.2, 56.8] | 29/41 (70.7) | [54.5, 83.9] | 3.26 | [1.38, 7.71] |
| a Event rate 95% exact confidence interval uses the Clopper−Pearson method. | ||||||
rx_resp_tbl <-
rx_resp_tbl |>
tab_footnote(
footnote = "Event rate 95% exact confidence interval uses the Clopper−Pearson method.",
locations = cells_column_labels(
columns = c("ci_low_Placebo", "ci_low_Drug 1")
),
placement = "right"
) |>
tab_options(footnotes.marks = letters)
rx_resp_tbl| x.x: Efficacy Data | ||||||
| x.x.x: Occurence of Event per Subgroup - {gt} Analysis Set | ||||||
|
Placebo
|
Drug 1
|
Odds ratio | [95% CI] | |||
|---|---|---|---|---|---|---|
| Event Rate (%) | [95% CI]a | Event Rate (%) | [95% CI]a | |||
| Age | ||||||
| <40 | 17/36 (47.2) | [30.4, 64.5] | 33/49 (67.3) | [52.5, 80.1] | 2.31 | [0.95, 5.59] |
| >=40 | 23/54 (42.6) | [29.2, 56.8] | 29/41 (70.7) | [54.5, 83.9] | 3.26 | [1.38, 7.71] |
| a Event rate 95% exact confidence interval uses the Clopper<e2><88><92>Pearson method. | ||||||
Protocol Deviation Table
For the summary table for protocol deviations (PDs) we will use a second CDISC-flavored dataset, namely rx_addv. This dataset contains a summary row, indicating whether a subject in the ITT population in rx_adsl experienced at least one major PD or not. In addition to the subject level summary, individual PDs are summarized.
echo glimpse(rxAddv())
Rows: 291 Columns: 20 $ STUDYIDGT01, GT01, GT01, GT01, GT01, GT01, GT01, GT01, GT01, … $ STUDYIDN 4001, 4001, 4001, 4001, 4001, 4001, 4001, 4001, 4001, … $ USUBJID GT1001, GT1002, GT1002, GT1003, GT1003, GT1003, … $ TRTA Placebo, Placebo, Placebo, Placebo, Placebo, Placebo, … $ TRTAN 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, … $ ITTFL Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, … $ AGE 41, 39, 39, 38, 38, 38, 45, 45, 35, 35, 35, 35, 35, 42, … $ AAGEGR1 >=40, <40, <40, <40, <40, <40, >=40, >=40, <40, <40, … $ SEX Male, Female, Female, Male, Male, Male, Male, Male, … $ ETHNIC Not Hispanic or Latino, Not Hispanic or Latino, … $ BLBMI 33.3507320063654, 30.4586162811611, 30.4586162811611, … $ DVTERM NA, NA, Lab values not taken at month 3, NA, … $ PARAMCD PDANYM, PDANYM, PDEV02, PDANYM, PDEV01, PDEV02, … $ PARAM At least one major Protocol Deviation, … $ PARCAT1 OVERALL, OVERALL, PROTOCOL DEVIATION, OVERALL, … $ DVCAT NA, NA, Major, NA, Major, Major, NA, Major, NA, NA, … $ ACAT1 NA, NA, Study Procedures Criteria Deviations, NA, … $ AVAL 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, … $ CRIT1 COVID-19 Related, COVID-19 Related, COVID-19 Related, … $ CRIT1FL N, N, N, N, N, N, N, N, N, Y, N, N, Y, N, N, N, N, N, …
rx_addv = pl.read_csv("../data/gt/rx_addv.csv")
rx_addv.glimpse()Rows: 291
Columns: 20
$ STUDYID <str> 'GT01', 'GT01', 'GT01', 'GT01', 'GT01', 'GT01', 'GT01', 'GT01', 'GT01', 'GT01'
$ STUDYIDN <i64> 4001, 4001, 4001, 4001, 4001, 4001, 4001, 4001, 4001, 4001
$ USUBJID <str> 'GT1001', 'GT1002', 'GT1002', 'GT1003', 'GT1003', 'GT1003', 'GT1004', 'GT1004', 'GT1005', 'GT1005'
$ TRTA <str> 'Placebo', 'Placebo', 'Placebo', 'Placebo', 'Placebo', 'Placebo', 'Placebo', 'Placebo', 'Placebo', 'Placebo'
$ TRTAN <i64> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
$ ITTFL <str> 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y'
$ AGE <i64> 41, 39, 39, 38, 38, 38, 45, 45, 35, 35
$ AAGEGR1 <str> '>=40', '<40', '<40', '<40', '<40', '<40', '>=40', '>=40', '<40', '<40'
$ SEX <str> 'Male', 'Female', 'Female', 'Male', 'Male', 'Male', 'Male', 'Male', 'Female', 'Female'
$ ETHNIC <str> 'Not Hispanic or Latino', 'Not Hispanic or Latino', 'Not Hispanic or Latino', 'Not Hispanic or Latino', 'Not Hispanic or Latino', 'Not Hispanic or Latino', 'Not Hispanic or Latino', 'Not Hispanic or Latino', 'Hispanic or Latino', 'Hispanic or Latino'
$ BLBMI <f64> 33.3507320063654, 30.4586162811611, 30.4586162811611, 22.8598554597702, 22.8598554597702, 22.8598554597702, 23.8971279682592, 23.8971279682592, 29.0985568843316, 29.0985568843316
$ DVTERM <str> '', '', 'Lab values not taken at month 3', '', '{gt} Questionnaire (GTQ v1.0) not completed at month 3', 'Lab values not taken at month 3', '', '{gt} Questionnaire (GTQ v1.0) not completed at month 3', '', ''
$ PARAMCD <str> 'PDANYM', 'PDANYM', 'PDEV02', 'PDANYM', 'PDEV01', 'PDEV02', 'PDANYM', 'PDEV01', 'PDANYM', 'PDANYM'
$ PARAM <str> 'At least one major Protocol Deviation', 'At least one major Protocol Deviation', 'Lab values not taken month 3.', 'At least one major Protocol Deviation', 'GTQ not completed month 3.', 'Lab values not taken month 3.', 'At least one major Protocol Deviation', 'GTQ not completed month 3.', 'At least one major Protocol Deviation', 'At least one major Protocol Deviation'
$ PARCAT1 <str> 'OVERALL', 'OVERALL', 'PROTOCOL DEVIATION', 'OVERALL', 'PROTOCOL DEVIATION', 'PROTOCOL DEVIATION', 'OVERALL', 'PROTOCOL DEVIATION', 'OVERALL', 'OVERALL'
$ DVCAT <str> '', '', 'Major', '', 'Major', 'Major', '', 'Major', '', ''
$ ACAT1 <str> '', '', 'Study Procedures Criteria Deviations', '', 'Study Procedures Criteria Deviations', 'Study Procedures Criteria Deviations', '', 'Study Procedures Criteria Deviations', '', ''
$ AVAL <i64> 0, 1, 1, 0, 1, 1, 1, 1, 0, 1
$ CRIT1 <str> 'COVID-19 Related', 'COVID-19 Related', 'COVID-19 Related', 'COVID-19 Related', 'COVID-19 Related', 'COVID-19 Related', 'COVID-19 Related', 'COVID-19 Related', 'COVID-19 Related', 'COVID-19 Related'
$ CRIT1FL <str> 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'Y'rx_addv |> str()tibble [291 x 20] (S3: tbl_df/tbl/data.frame) $ STUDYID : chr [1:291] "GT01" "GT01" "GT01" "GT01" ... ..- attr(*, "label")= chr "Unique Study Identifier" $ STUDYIDN: chr [1:291] "4001" "4001" "4001" "4001" ... ..- attr(*, "label")= chr "Unique Study Identifier (N)" $ USUBJID : chr [1:291] "GT1001" "GT1002" "GT1002" "GT1003" ... ..- attr(*, "label")= chr "Unique Subject Identifier" $ TRTA : Factor w/ 2 levels "Placebo","Drug 1": 1 1 1 1 1 1 1 1 1 1 ... ..- attr(*, "label")= chr "Actual Treatment" $ TRTAN : num [1:291] 1 1 1 1 1 1 1 1 1 1 ... ..- attr(*, "label")= chr "Actual Treatment (N)" $ ITTFL : chr [1:291] "Y" "Y" "Y" "Y" ... ..- attr(*, "label")= chr "ITT Population Flag" $ AGE : int [1:291] 41 39 39 38 38 38 45 45 35 35 ... ..- attr(*, "label")= chr "Age" ..- attr(*, "units")= chr "Years" $ AAGEGR1 : Factor w/ 2 levels "<40",">=40": 2 1 1 1 1 1 2 2 1 1 ... ..- attr(*, "label")= chr "Age Group" $ SEX : Factor w/ 3 levels "Male","Female",..: 1 2 2 1 1 1 1 1 2 2 ... ..- attr(*, "label")= chr "Sex" $ ETHNIC : Factor w/ 3 levels "Hispanic or Latino",..: 2 2 2 2 2 2 2 2 1 1 ... ..- attr(*, "label")= chr "Ethnicity" $ BLBMI : num [1:291] 33.4 30.5 30.5 22.9 22.9 ... ..- attr(*, "label")= chr "Body Mass Index" ..- attr(*, "units")= chr "kg/m2" $ DVTERM : chr [1:291] "" "" "Lab values not taken at month 3" "" ... ..- attr(*, "label")= chr "Protocol Deviation Term" $ PARAMCD : Factor w/ 6 levels "PDANYM","PDEV01",..: 1 1 3 1 2 3 1 2 1 1 ... ..- attr(*, "label")= chr "Parameter Code" $ PARAM : Factor w/ 6 levels "At least one major Protocol Deviation",..: 1 1 3 1 2 3 1 2 1 1 ... ..- attr(*, "label")= chr "Parameter" $ PARCAT1 : chr [1:291] "OVERALL" "OVERALL" "PROTOCOL DEVIATION" "OVERALL" ... ..- attr(*, "label")= chr "Parameter Category 1" $ DVCAT : chr [1:291] "" "" "Major" "" ... ..- attr(*, "label")= chr "Protocol Deviation Category" $ ACAT1 : chr [1:291] "" "" "Study Procedures Criteria Deviations" "" ... ..- attr(*, "label")= chr "Analysis Category 1" $ AVAL : num [1:291] 0 1 1 0 1 1 1 1 0 1 ... ..- attr(*, "label")= chr "Analysis Value" $ CRIT1 : chr [1:291] "COVID-19 Related" "COVID-19 Related" "COVID-19 Related" "COVID-19 Related" ... ..- attr(*, "label")= chr "Analysis Criterion 1" $ CRIT1FL : chr [1:291] "N" "N" "N" "N" ... ..- attr(*, "label")= chr "Criterion 1 Evaluation Flag"
We would now like to build a table to summarize overall and counts of individual PDs by treatment arm and furthermore indicate, whether the PD was related to COVID-19 or not. In order to build this table, we first need to apply some data wrangling with functions from dplyr and tidyr.
let rxAddv = readCsv(newStringStream(rxAddvCsv))
let params = rxMeta()["rx_addv"]["PARAM"]["levels"].to(seq[string])
let firstRow = newDataFrame:
PARAM: ["Subjects with at least:"]
let addvSum = concat([firstRow,
rxAddv
.withColumns(col("USUBJID").nUnique.over("TRTA").alias("NTOT"))
.groupBy("TRTA", "PARCAT1", "PARAM", "CRIT1FL")
.agg(
col("AVAL").sum.alias("n"),
(100 * col("AVAL").sum / col("NTOT").mean).alias("pct"))
.sort([col("TRTA").cast(Enum(arms)), col("PARCAT1"),
col("PARAM").cast(Enum(params)), col("CRIT1FL")])
.withColumns(concatStr([col("TRTA"), col("CRIT1FL")], separator = "_").alias("arm_flag"))
.pivot("arm_flag", index = ["PARCAT1", "PARAM"], values = ["n", "pct"])
.fillNull(0)],
how = ConcatMethod.DiagonalRelaxed)
echo tabella(addvSum)
┌───────────────────────────────────────┬────────────────────┬─────────────┬─────────────┬────────────┬────────────┬────────────────────┬────────────────────┬────────────────────┬────────────────────┐ │ PARAM │ PARCAT1 │ n_Placebo_N │ n_Placebo_Y │ n_Drug 1_N │ n_Drug 1_Y │ pct_Placebo_N │ pct_Placebo_Y │ pct_Drug 1_N │ pct_Drug 1_Y │ ╞═══════════════════════════════════════╪════════════════════╪═════════════╪═════════════╪════════════╪════════════╪════════════════════╪════════════════════╪════════════════════╪════════════════════╡ │ Subjects with at least: │ │ │ │ │ │ │ │ │ │ │ At least one major Protocol Deviation │ OVERALL │ 24 │ 15 │ 23 │ 5 │ 26.666666666666668 │ 16.666666666666668 │ 25.555555555555557 │ 5.555555555555555 │ │ GTQ not completed month 3. │ PROTOCOL DEVIATION │ 7 │ 0 │ 12 │ 0 │ 7.777777777777778 │ 0.0 │ 13.333333333333334 │ 0.0 │ │ Lab values not taken month 3. │ PROTOCOL DEVIATION │ 12 │ 3 │ 7 │ 0 │ 13.333333333333334 │ 3.3333333333333335 │ 7.777777777777778 │ 0.0 │ │ GTQ not completed month 6. │ PROTOCOL DEVIATION │ 7 │ 2 │ 7 │ 1 │ 7.777777777777778 │ 2.2222222222222223 │ 7.777777777777778 │ 1.1111111111111112 │ │ Month 3 Visit not done. │ PROTOCOL DEVIATION │ 9 │ 4 │ 3 │ 1 │ 10.0 │ 4.444444444444445 │ 3.3333333333333335 │ 1.1111111111111112 │ │ Month 6 Visit not done. │ PROTOCOL DEVIATION │ 9 │ 8 │ 9 │ 3 │ 10.0 │ 8.88888888888889 │ 10.0 │ 3.3333333333333335 │ └───────────────────────────────────────┴────────────────────┴─────────────┴─────────────┴────────────┴────────────┴────────────────────┴────────────────────┴────────────────────┴────────────────────┘
params = rx_meta["rx_addv"]["PARAM"]["levels"]
addv_sum = (
rx_addv.with_columns(NTOT=pl.col("USUBJID").n_unique().over("TRTA"))
.group_by("TRTA", "PARCAT1", "PARAM", "CRIT1FL")
.agg(
n=pl.col("AVAL").sum(),
pct=100 * pl.col("AVAL").sum() / pl.col("NTOT").mean(),
)
.sort(
pl.col("TRTA").cast(pl.Enum(arms)), "PARCAT1",
pl.col("PARAM").cast(pl.Enum(params)), "CRIT1FL",
)
.with_columns(arm_flag=pl.concat_str("TRTA", "CRIT1FL", separator="_"))
.pivot(on="arm_flag", index=["PARCAT1", "PARAM"], values=["n", "pct"])
.with_columns(cs.numeric().fill_null(0))
)
addv_sum = pl.concat(
[pl.DataFrame({"PARAM": ["Subjects with at least:"]}), addv_sum],
how="diagonal_relaxed",
)
addv_sum| PARAM | PARCAT1 | n_Placebo_N | n_Placebo_Y | n_Drug 1_N | n_Drug 1_Y | pct_Placebo_N | pct_Placebo_Y | pct_Drug 1_N | pct_Drug 1_Y |
|---|---|---|---|---|---|---|---|---|---|
| str | str | i64 | i64 | i64 | i64 | f64 | f64 | f64 | f64 |
| "Subjects with at least:" | null | null | null | null | null | null | null | null | null |
| "At least one major Protocol De… | "OVERALL" | 24 | 15 | 23 | 5 | 26.666667 | 16.666667 | 25.555556 | 5.555556 |
| "GTQ not completed month 3." | "PROTOCOL DEVIATION" | 7 | 0 | 12 | 0 | 7.777778 | 0.0 | 13.333333 | 0.0 |
| "Lab values not taken month 3." | "PROTOCOL DEVIATION" | 12 | 3 | 7 | 0 | 13.333333 | 3.333333 | 7.777778 | 0.0 |
| "GTQ not completed month 6." | "PROTOCOL DEVIATION" | 7 | 2 | 7 | 1 | 7.777778 | 2.222222 | 7.777778 | 1.111111 |
| "Month 3 Visit not done." | "PROTOCOL DEVIATION" | 9 | 4 | 3 | 1 | 10.0 | 4.444444 | 3.333333 | 1.111111 |
| "Month 6 Visit not done." | "PROTOCOL DEVIATION" | 9 | 8 | 9 | 3 | 10.0 | 8.888889 | 10.0 | 3.333333 |
addv_sum <-
rx_addv |>
dplyr::group_by(TRTA) |>
dplyr::mutate(
NTOT = n_distinct(USUBJID),
.groups = "drop"
) |>
dplyr::group_by(TRTA, PARCAT1, PARAM, CRIT1FL) |>
dplyr::summarize(
n = sum(AVAL, na.rm = TRUE),
pct = 100 * sum(AVAL, na.rm = TRUE) / mean(NTOT),
.groups = "drop"
) |>
tidyr::pivot_wider(
id_cols = c(PARCAT1, PARAM),
names_from = c(TRTA, CRIT1FL),
values_from = c(n, pct)
) |>
dplyr::mutate(across(where(is.numeric), ~ ifelse(is.na(.), 0, .))) |>
dplyr::add_row(PARAM = "Subjects with at least:", .before = 1)
addv_sum# A tibble: 7 x 10 PARCAT1 PARAM n_Placebo_N n_Placebo_Y `n_Drug 1_N` `n_Drug 1_Y` pct_Placebo_N <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> 1 <NA> Subj~ NA NA NA NA NA 2 OVERALL At l~ 24 15 23 5 26.7 3 PROTOCO~ GTQ ~ 7 0 12 0 7.78 4 PROTOCO~ Lab ~ 12 3 7 0 13.3 5 PROTOCO~ GTQ ~ 7 2 7 1 7.78 6 PROTOCO~ Mont~ 9 4 3 1 10 7 PROTOCO~ Mont~ 9 8 9 3 10 # i 3 more variables: pct_Placebo_Y <dbl>, `pct_Drug 1_N` <dbl>, # `pct_Drug 1_Y` <dbl>
This is the dataset that serves as a starting point for gt. We will start by exposing the dataset to gt and add our usual left-aligned headers.
let addvTbl = tabella(addvSum, rownameCol = "PARAM"):
tabHeader "xx.x: Demographic and Baseline Data",
"xx.x.x: Major Protocol Deviations and Relationship to COVID-19 - ITT Set"
optAlignTableHeader "left"
nbTable addvTbl
| xx.x: Demographic and Baseline Data | |||||||||
|---|---|---|---|---|---|---|---|---|---|
| xx.x.x: Major Protocol Deviations and Relationship to COVID-19 - ITT Set | |||||||||
| PARCAT1 | n_Placebo_N | n_Placebo_Y | n_Drug 1_N | n_Drug 1_Y | pct_Placebo_N | pct_Placebo_Y | pct_Drug 1_N | pct_Drug 1_Y | |
| Subjects with at least: | |||||||||
| At least one major Protocol Deviation | OVERALL | 24 | 15 | 23 | 5 | 26.666666666666668 | 16.666666666666668 | 25.555555555555557 | 5.555555555555555 |
| GTQ not completed month 3. | PROTOCOL DEVIATION | 7 | 0 | 12 | 0 | 7.777777777777778 | 0.0 | 13.333333333333334 | 0.0 |
| Lab values not taken month 3. | PROTOCOL DEVIATION | 12 | 3 | 7 | 0 | 13.333333333333334 | 3.3333333333333335 | 7.777777777777778 | 0.0 |
| GTQ not completed month 6. | PROTOCOL DEVIATION | 7 | 2 | 7 | 1 | 7.777777777777778 | 2.2222222222222223 | 7.777777777777778 | 1.1111111111111112 |
| Month 3 Visit not done. | PROTOCOL DEVIATION | 9 | 4 | 3 | 1 | 10.0 | 4.444444444444445 | 3.3333333333333335 | 1.1111111111111112 |
| Month 6 Visit not done. | PROTOCOL DEVIATION | 9 | 8 | 9 | 3 | 10.0 | 8.88888888888889 | 10.0 | 3.3333333333333335 |
addv_tbl = GT(addv_sum, rowname_col="PARAM").tab_header(
title="xx.x: Demographic and Baseline Data",
subtitle="xx.x.x: Major Protocol Deviations and Relationship to COVID-19 - ITT Set",
).opt_align_table_header(align="left")
addv_tbl| xx.x: Demographic and Baseline Data | |||||||||
| xx.x.x: Major Protocol Deviations and Relationship to COVID-19 - ITT Set | |||||||||
| PARCAT1 | n_Placebo_N | n_Placebo_Y | n_Drug 1_N | n_Drug 1_Y | pct_Placebo_N | pct_Placebo_Y | pct_Drug 1_N | pct_Drug 1_Y | |
|---|---|---|---|---|---|---|---|---|---|
| Subjects with at least: | None | None | None | None | None | None | None | None | None |
| At least one major Protocol Deviation | OVERALL | 24 | 15 | 23 | 5 | 26.666666666666668 | 16.666666666666668 | 25.555555555555557 | 5.555555555555555 |
| GTQ not completed month 3. | PROTOCOL DEVIATION | 7 | 0 | 12 | 0 | 7.777777777777778 | 0.0 | 13.333333333333334 | 0.0 |
| Lab values not taken month 3. | PROTOCOL DEVIATION | 12 | 3 | 7 | 0 | 13.333333333333334 | 3.3333333333333335 | 7.777777777777778 | 0.0 |
| GTQ not completed month 6. | PROTOCOL DEVIATION | 7 | 2 | 7 | 1 | 7.777777777777778 | 2.2222222222222223 | 7.777777777777778 | 1.1111111111111112 |
| Month 3 Visit not done. | PROTOCOL DEVIATION | 9 | 4 | 3 | 1 | 10.0 | 4.444444444444445 | 3.3333333333333335 | 1.1111111111111112 |
| Month 6 Visit not done. | PROTOCOL DEVIATION | 9 | 8 | 9 | 3 | 10.0 | 8.88888888888889 | 10.0 | 3.3333333333333335 |
addv_tbl <-
addv_sum |>
gt(rowname_col = "PARAM") |>
tab_header(
title = "xx.x: Demographic and Baseline Data",
subtitle = "xx.x.x: Major Protocol Deviations and Relationship to COVID-19 - ITT Set"
) |>
opt_align_table_header(align = "left")
addv_tbl| xx.x: Demographic and Baseline Data | |||||||||
| xx.x.x: Major Protocol Deviations and Relationship to COVID-19 - ITT Set | |||||||||
| Parameter Category 1 | n_Placebo_N | n_Placebo_Y | n_Drug 1_N | n_Drug 1_Y | pct_Placebo_N | pct_Placebo_Y | pct_Drug 1_N | pct_Drug 1_Y | |
|---|---|---|---|---|---|---|---|---|---|
| Subjects with at least: | NA | NA | NA | NA | NA | NA | NA | NA | NA |
| At least one major Protocol Deviation | OVERALL | 24 | 15 | 23 | 5 | 26.666667 | 16.666667 | 25.555556 | 5.555556 |
| GTQ not completed month 3. | PROTOCOL DEVIATION | 7 | 0 | 12 | 0 | 7.777778 | 0.000000 | 13.333333 | 0.000000 |
| Lab values not taken month 3. | PROTOCOL DEVIATION | 12 | 3 | 7 | 0 | 13.333333 | 3.333333 | 7.777778 | 0.000000 |
| GTQ not completed month 6. | PROTOCOL DEVIATION | 7 | 2 | 7 | 1 | 7.777778 | 2.222222 | 7.777778 | 1.111111 |
| Month 3 Visit not done. | PROTOCOL DEVIATION | 9 | 4 | 3 | 1 | 10.000000 | 4.444444 | 3.333333 | 1.111111 |
| Month 6 Visit not done. | PROTOCOL DEVIATION | 9 | 8 | 9 | 3 | 10.000000 | 8.888889 | 10.000000 | 3.333333 |
In a next step, we would like to create a summary row for all individual PDs to get the overall number of individual PDs, as well as the corresponding percentage. For this, we will first create a row group for individual PDs using tab_row_group() applied to all rows where PARCAT1 is equal to PROTOCOL DEVIATIONS. Then, we'll arrange the order of the row groups to list individual PDs after the overall summary. Finally, we can create a summary row, using summary_rows() and gt sums up all columns with n's and percentages for us (other summary functions are possible, but fn = 'sum' does the job for us).
let addvGrouped = tabella(addvTbl):
tabRowGroup " ", where(it.PARCAT1 == "PROTOCOL DEVIATION")
summaryRows:
"Study Procedure Deviations": agg.sum
groups = [" "]
side = sTop
nbTable addvGrouped
| xx.x: Demographic and Baseline Data | |||||||||
|---|---|---|---|---|---|---|---|---|---|
| xx.x.x: Major Protocol Deviations and Relationship to COVID-19 - ITT Set | |||||||||
| PARCAT1 | n_Placebo_N | n_Placebo_Y | n_Drug 1_N | n_Drug 1_Y | pct_Placebo_N | pct_Placebo_Y | pct_Drug 1_N | pct_Drug 1_Y | |
| Subjects with at least: | |||||||||
| At least one major Protocol Deviation | OVERALL | 24 | 15 | 23 | 5 | 26.666666666666668 | 16.666666666666668 | 25.555555555555557 | 5.555555555555555 |
| Study Procedure Deviations | — | 44 | 17 | 38 | 5 | 48.89 | 18.89 | 42.22 | 5.56 |
| GTQ not completed month 3. | PROTOCOL DEVIATION | 7 | 0 | 12 | 0 | 7.777777777777778 | 0.0 | 13.333333333333334 | 0.0 |
| Lab values not taken month 3. | PROTOCOL DEVIATION | 12 | 3 | 7 | 0 | 13.333333333333334 | 3.3333333333333335 | 7.777777777777778 | 0.0 |
| GTQ not completed month 6. | PROTOCOL DEVIATION | 7 | 2 | 7 | 1 | 7.777777777777778 | 2.2222222222222223 | 7.777777777777778 | 1.1111111111111112 |
| Month 3 Visit not done. | PROTOCOL DEVIATION | 9 | 4 | 3 | 1 | 10.0 | 4.444444444444445 | 3.3333333333333335 | 1.1111111111111112 |
| Month 6 Visit not done. | PROTOCOL DEVIATION | 9 | 8 | 9 | 3 | 10.0 | 8.88888888888889 | 10.0 | 3.3333333333333335 |
# Great Tables has no tab_row_group(): the row groups come from a column of
# the data, so the table is made again with a column `group` (the first rows
# go in a group with an empty label: they cannot be in no group, as gt's NA)
addv_tbl = (
GT(
addv_sum.with_columns(
group=pl.when(pl.col("PARCAT1") == "PROTOCOL DEVIATION")
.then(pl.lit(" "))
.otherwise(pl.lit(""))
),
rowname_col="PARAM",
groupname_col="group",
)
.tab_header(
title="xx.x: Demographic and Baseline Data",
subtitle="xx.x.x: Major Protocol Deviations and Relationship to COVID-19 - ITT Set",
)
.opt_align_table_header(align="left")
.summary_rows(
fns={"Study Procedure Deviations": cs.numeric().sum()},
groups=[" "],
side="top",
)
)
addv_tbl| xx.x: Demographic and Baseline Data | |||||||||
| xx.x.x: Major Protocol Deviations and Relationship to COVID-19 - ITT Set | |||||||||
| PARCAT1 | n_Placebo_N | n_Placebo_Y | n_Drug 1_N | n_Drug 1_Y | pct_Placebo_N | pct_Placebo_Y | pct_Drug 1_N | pct_Drug 1_Y | |
|---|---|---|---|---|---|---|---|---|---|
| Subjects with at least: | None | None | None | None | None | None | None | None | None |
| At least one major Protocol Deviation | OVERALL | 24 | 15 | 23 | 5 | 26.666666666666668 | 16.666666666666668 | 25.555555555555557 | 5.555555555555555 |
| Study Procedure Deviations | --- | 44 | 17 | 38 | 5 | 48.888888888888886 | 18.88888888888889 | 42.22222222222222 | 5.555555555555555 |
| GTQ not completed month 3. | PROTOCOL DEVIATION | 7 | 0 | 12 | 0 | 7.777777777777778 | 0.0 | 13.333333333333334 | 0.0 |
| Lab values not taken month 3. | PROTOCOL DEVIATION | 12 | 3 | 7 | 0 | 13.333333333333334 | 3.3333333333333335 | 7.777777777777778 | 0.0 |
| GTQ not completed month 6. | PROTOCOL DEVIATION | 7 | 2 | 7 | 1 | 7.777777777777778 | 2.2222222222222223 | 7.777777777777778 | 1.1111111111111112 |
| Month 3 Visit not done. | PROTOCOL DEVIATION | 9 | 4 | 3 | 1 | 10.0 | 4.444444444444445 | 3.3333333333333335 | 1.1111111111111112 |
| Month 6 Visit not done. | PROTOCOL DEVIATION | 9 | 8 | 9 | 3 | 10.0 | 8.88888888888889 | 10.0 | 3.3333333333333335 |
addv_tbl <-
addv_tbl |>
tab_row_group(
label = " ",
rows = PARCAT1 == "PROTOCOL DEVIATION"
) |>
row_group_order(groups = c(NA, " ")) |>
summary_rows(
groups = " ",
columns = where(is.numeric),
fns = list(label = "Study Procedure Deviations", fn = "sum"),
side = "top"
)
addv_tbl| xx.x: Demographic and Baseline Data | |||||||||
| xx.x.x: Major Protocol Deviations and Relationship to COVID-19 - ITT Set | |||||||||
| Parameter Category 1 | n_Placebo_N | n_Placebo_Y | n_Drug 1_N | n_Drug 1_Y | pct_Placebo_N | pct_Placebo_Y | pct_Drug 1_N | pct_Drug 1_Y | |
|---|---|---|---|---|---|---|---|---|---|
| Subjects with at least: | NA | NA | NA | NA | NA | NA | NA | NA | NA |
| At least one major Protocol Deviation | OVERALL | 24 | 15 | 23 | 5 | 26.666667 | 16.666667 | 25.555556 | 5.555556 |
| Study Procedure Deviations | 44 | 17 | 38 | 5 | 48.88889 | 18.88889 | 42.22222 | 5.555556 | |
| GTQ not completed month 3. | PROTOCOL DEVIATION | 7 | 0 | 12 | 0 | 7.777778 | 0.000000 | 13.333333 | 0.000000 |
| Lab values not taken month 3. | PROTOCOL DEVIATION | 12 | 3 | 7 | 0 | 13.333333 | 3.333333 | 7.777778 | 0.000000 |
| GTQ not completed month 6. | PROTOCOL DEVIATION | 7 | 2 | 7 | 1 | 7.777778 | 2.222222 | 7.777778 | 1.111111 |
| Month 3 Visit not done. | PROTOCOL DEVIATION | 9 | 4 | 3 | 1 | 10.000000 | 4.444444 | 3.333333 | 1.111111 |
| Month 6 Visit not done. | PROTOCOL DEVIATION | 9 | 8 | 9 | 3 | 10.000000 | 8.888889 | 10.000000 | 3.333333 |
We only kept the column PARCAT1 to facilitate the generation of the row group. We can hide this column now using cols_hide():
let addvHidden = tabella(addvGrouped):
colsHide "PARCAT1"
nbTable addvHidden
| xx.x: Demographic and Baseline Data | ||||||||
|---|---|---|---|---|---|---|---|---|
| xx.x.x: Major Protocol Deviations and Relationship to COVID-19 - ITT Set | ||||||||
| n_Placebo_N | n_Placebo_Y | n_Drug 1_N | n_Drug 1_Y | pct_Placebo_N | pct_Placebo_Y | pct_Drug 1_N | pct_Drug 1_Y | |
| Subjects with at least: | ||||||||
| At least one major Protocol Deviation | 24 | 15 | 23 | 5 | 26.666666666666668 | 16.666666666666668 | 25.555555555555557 | 5.555555555555555 |
| Study Procedure Deviations | 44 | 17 | 38 | 5 | 48.89 | 18.89 | 42.22 | 5.56 |
| GTQ not completed month 3. | 7 | 0 | 12 | 0 | 7.777777777777778 | 0.0 | 13.333333333333334 | 0.0 |
| Lab values not taken month 3. | 12 | 3 | 7 | 0 | 13.333333333333334 | 3.3333333333333335 | 7.777777777777778 | 0.0 |
| GTQ not completed month 6. | 7 | 2 | 7 | 1 | 7.777777777777778 | 2.2222222222222223 | 7.777777777777778 | 1.1111111111111112 |
| Month 3 Visit not done. | 9 | 4 | 3 | 1 | 10.0 | 4.444444444444445 | 3.3333333333333335 | 1.1111111111111112 |
| Month 6 Visit not done. | 9 | 8 | 9 | 3 | 10.0 | 8.88888888888889 | 10.0 | 3.3333333333333335 |
addv_tbl = addv_tbl.cols_hide(columns="PARCAT1")
addv_tbl| xx.x: Demographic and Baseline Data | ||||||||
| xx.x.x: Major Protocol Deviations and Relationship to COVID-19 - ITT Set | ||||||||
| n_Placebo_N | n_Placebo_Y | n_Drug 1_N | n_Drug 1_Y | pct_Placebo_N | pct_Placebo_Y | pct_Drug 1_N | pct_Drug 1_Y | |
|---|---|---|---|---|---|---|---|---|
| Subjects with at least: | None | None | None | None | None | None | None | None |
| At least one major Protocol Deviation | 24 | 15 | 23 | 5 | 26.666666666666668 | 16.666666666666668 | 25.555555555555557 | 5.555555555555555 |
| Study Procedure Deviations | 44 | 17 | 38 | 5 | 48.888888888888886 | 18.88888888888889 | 42.22222222222222 | 5.555555555555555 |
| GTQ not completed month 3. | 7 | 0 | 12 | 0 | 7.777777777777778 | 0.0 | 13.333333333333334 | 0.0 |
| Lab values not taken month 3. | 12 | 3 | 7 | 0 | 13.333333333333334 | 3.3333333333333335 | 7.777777777777778 | 0.0 |
| GTQ not completed month 6. | 7 | 2 | 7 | 1 | 7.777777777777778 | 2.2222222222222223 | 7.777777777777778 | 1.1111111111111112 |
| Month 3 Visit not done. | 9 | 4 | 3 | 1 | 10.0 | 4.444444444444445 | 3.3333333333333335 | 1.1111111111111112 |
| Month 6 Visit not done. | 9 | 8 | 9 | 3 | 10.0 | 8.88888888888889 | 10.0 | 3.3333333333333335 |
addv_tbl <-
addv_tbl |>
cols_hide(columns = "PARCAT1")
addv_tbl| xx.x: Demographic and Baseline Data | ||||||||
| xx.x.x: Major Protocol Deviations and Relationship to COVID-19 - ITT Set | ||||||||
| n_Placebo_N | n_Placebo_Y | n_Drug 1_N | n_Drug 1_Y | pct_Placebo_N | pct_Placebo_Y | pct_Drug 1_N | pct_Drug 1_Y | |
|---|---|---|---|---|---|---|---|---|
| Subjects with at least: | NA | NA | NA | NA | NA | NA | NA | NA |
| At least one major Protocol Deviation | 24 | 15 | 23 | 5 | 26.666667 | 16.666667 | 25.555556 | 5.555556 |
| Study Procedure Deviations | 44 | 17 | 38 | 5 | 48.88889 | 18.88889 | 42.22222 | 5.555556 |
| GTQ not completed month 3. | 7 | 0 | 12 | 0 | 7.777778 | 0.000000 | 13.333333 | 0.000000 |
| Lab values not taken month 3. | 12 | 3 | 7 | 0 | 13.333333 | 3.333333 | 7.777778 | 0.000000 |
| GTQ not completed month 6. | 7 | 2 | 7 | 1 | 7.777778 | 2.222222 | 7.777778 | 1.111111 |
| Month 3 Visit not done. | 9 | 4 | 3 | 1 | 10.000000 | 4.444444 | 3.333333 | 1.111111 |
| Month 6 Visit not done. | 9 | 8 | 9 | 3 | 10.000000 | 8.888889 | 10.000000 | 3.333333 |
Now that the table has roughly the right shape, we can start to format all numeric columns and merge columns for n's and percentages by intervention group and COVID-19 relationship flag.
let addvMerged = tabella(addvHidden):
subMissing(rows = 0, missingText = "")
fmtNumber startsWith("pct"), decimals = 1
for arm in arms:
for flag in ["Y", "N"]:
colsMergeNPct "n_" & arm & "_" & flag, "pct_" & arm & "_" & flag
nbTable addvMerged
| xx.x: Demographic and Baseline Data | ||||
|---|---|---|---|---|
| xx.x.x: Major Protocol Deviations and Relationship to COVID-19 - ITT Set | ||||
| n_Placebo_N | n_Placebo_Y | n_Drug 1_N | n_Drug 1_Y | |
| Subjects with at least: | ||||
| At least one major Protocol Deviation | 24 (26.7) | 15 (16.7) | 23 (25.6) | 5 (5.6) |
| Study Procedure Deviations | 44 | 17 | 38 | 5 |
| GTQ not completed month 3. | 7 (7.8) | 0 | 12 (13.3) | 0 |
| Lab values not taken month 3. | 12 (13.3) | 3 (3.3) | 7 (7.8) | 0 |
| GTQ not completed month 6. | 7 (7.8) | 2 (2.2) | 7 (7.8) | 1 (1.1) |
| Month 3 Visit not done. | 9 (10.0) | 4 (4.4) | 3 (3.3) | 1 (1.1) |
| Month 6 Visit not done. | 9 (10.0) | 8 (8.9) | 9 (10.0) | 3 (3.3) |
addv_tbl = (
addv_tbl
.sub_missing(rows=[0], missing_text="")
.fmt_number(columns=cs.starts_with("pct"), decimals=1)
.cols_merge_n_pct(col_n="n_Placebo_Y", col_pct="pct_Placebo_Y")
.cols_merge_n_pct(col_n="n_Placebo_N", col_pct="pct_Placebo_N")
.cols_merge_n_pct(col_n="n_Drug 1_Y", col_pct="pct_Drug 1_Y")
.cols_merge_n_pct(col_n="n_Drug 1_N", col_pct="pct_Drug 1_N")
)
addv_tbl| xx.x: Demographic and Baseline Data | ||||
| xx.x.x: Major Protocol Deviations and Relationship to COVID-19 - ITT Set | ||||
| n_Placebo_N | n_Placebo_Y | n_Drug 1_N | n_Drug 1_Y | |
|---|---|---|---|---|
| Subjects with at least: | () | () | () | () |
| At least one major Protocol Deviation | 24 (26.7) | 15 (16.7) | 23 (25.6) | 5 (5.6) |
| Study Procedure Deviations | 44 | 17 | 38 | 5 |
| GTQ not completed month 3. | 7 (7.8) | 0 | 12 (13.3) | 0 |
| Lab values not taken month 3. | 12 (13.3) | 3 (3.3) | 7 (7.8) | 0 |
| GTQ not completed month 6. | 7 (7.8) | 2 (2.2) | 7 (7.8) | 1 (1.1) |
| Month 3 Visit not done. | 9 (10.0) | 4 (4.4) | 3 (3.3) | 1 (1.1) |
| Month 6 Visit not done. | 9 (10.0) | 8 (8.9) | 9 (10.0) | 3 (3.3) |
addv_tbl <-
addv_tbl |>
sub_missing(
rows = 1,
missing_text = ""
) |>
fmt_number(
columns = starts_with("pct"),
decimals = 1
) |>
cols_merge_n_pct(
col_n = "n_Placebo_Y",
col_pct = "pct_Placebo_Y"
) |>
cols_merge_n_pct(
col_n = "n_Placebo_N",
col_pct = "pct_Placebo_N"
) |>
cols_merge_n_pct(
col_n = "n_Drug 1_Y",
col_pct = "pct_Drug 1_Y"
) |>
cols_merge_n_pct(
col_n = "n_Drug 1_N",
col_pct = "pct_Drug 1_N"
)
addv_tbl| xx.x: Demographic and Baseline Data | ||||
| xx.x.x: Major Protocol Deviations and Relationship to COVID-19 - ITT Set | ||||
| n_Placebo_N | n_Placebo_Y | n_Drug 1_N | n_Drug 1_Y | |
|---|---|---|---|---|
| Subjects with at least: | ||||
| At least one major Protocol Deviation | 24 (26.7) | 15 (16.7) | 23 (25.6) | 5 (5.6) |
| Study Procedure Deviations | 44 | 17 | 38 | 5 |
| GTQ not completed month 3. | 7 (7.8) | 0 | 12 (13.3) | 0 |
| Lab values not taken month 3. | 12 (13.3) | 3 (3.3) | 7 (7.8) | 0 |
| GTQ not completed month 6. | 7 (7.8) | 2 (2.2) | 7 (7.8) | 1 (1.1) |
| Month 3 Visit not done. | 9 (10.0) | 4 (4.4) | 3 (3.3) | 1 (1.1) |
| Month 6 Visit not done. | 9 (10.0) | 8 (8.9) | 9 (10.0) | 3 (3.3) |
This looks more like a PD table! We can now modify the column names and create a cascade of column spanners.
let addvSpanned = tabella(addvMerged):
tabSpanner md"COVID-19 Related", cols("n_Placebo_Y", "n_Placebo_N")
tabSpanner md"COVID-19 Related", cols("n_Drug 1_Y", "n_Drug 1_N")
tabSpanner md("Placebo \n N=90 (100%) \n n (%)"), cols("n_Placebo_Y", "n_Placebo_N"), level = 2
tabSpanner md("Drug 1 \n N=90 (100%) \n n (%)"), cols("n_Drug 1_Y", "n_Drug 1_N"), level = 2
colsLabel:
n_Placebo_Y: "Yes"
n_Placebo_N: "No"
"n_Drug 1_Y": "Yes"
"n_Drug 1_N": "No"
tabStyle style.text(align = "center"), loc.spanners("COVID-19 Related", "Placebo \n N=90 (100%) \n n (%)", "Drug 1 \n N=90 (100%) \n n (%)")
nbTable addvSpanned
| xx.x: Demographic and Baseline Data | ||||
|---|---|---|---|---|
| xx.x.x: Major Protocol Deviations and Relationship to COVID-19 - ITT Set | ||||
| Placebo N=90 (100%) n (%) | Drug 1 N=90 (100%) n (%) | |||
| COVID-19 Related | COVID-19 Related | |||
| Yes | No | Yes | No | |
| Subjects with at least: | ||||
| At least one major Protocol Deviation | 15 (16.7) | 24 (26.7) | 5 (5.6) | 23 (25.6) |
| Study Procedure Deviations | 17 | 44 | 5 | 38 |
| GTQ not completed month 3. | 0 | 7 (7.8) | 0 | 12 (13.3) |
| Lab values not taken month 3. | 3 (3.3) | 12 (13.3) | 0 | 7 (7.8) |
| GTQ not completed month 6. | 2 (2.2) | 7 (7.8) | 1 (1.1) | 7 (7.8) |
| Month 3 Visit not done. | 4 (4.4) | 9 (10.0) | 1 (1.1) | 3 (3.3) |
| Month 6 Visit not done. | 8 (8.9) | 9 (10.0) | 3 (3.3) | 9 (10.0) |
addv_tbl = (
addv_tbl
.tab_spanner(
label=md("COVID-19 Related"), columns=["n_Placebo_Y", "n_Placebo_N"], id="cov_pla"
)
.tab_spanner(
label=md("COVID-19 Related"), columns=["n_Drug 1_Y", "n_Drug 1_N"], id="cov_dru"
)
.tab_spanner(
label=md("Placebo \n N=90 (100%) \n n (%)"),
columns=["n_Placebo_Y", "n_Placebo_N"],
id="pla",
)
.tab_spanner(
label=md("Drug 1 \n N=90 (100%) \n n (%)"),
columns=["n_Drug 1_Y", "n_Drug 1_N"],
id="dru",
)
.cols_label(
{"n_Placebo_Y": "Yes", "n_Placebo_N": "No", "n_Drug 1_Y": "Yes", "n_Drug 1_N": "No"}
)
.tab_style(
style=style.text(align="center"),
locations=loc.spanner_labels(ids=["cov_pla", "cov_dru", "pla", "dru"]),
)
)
addv_tbl| xx.x: Demographic and Baseline Data | ||||
| xx.x.x: Major Protocol Deviations and Relationship to COVID-19 - ITT Set | ||||
|   |
Placebo N=90 (100%) n (%) |
Drug 1 N=90 (100%) n (%) |
||
|---|---|---|---|---|
| COVID-19 Related | ||||
| Yes | No | Yes | No | |
| Subjects with at least: | () | () | () | () |
| At least one major Protocol Deviation | 15 (16.7) | 24 (26.7) | 5 (5.6) | 23 (25.6) |
| Study Procedure Deviations | 17 | 44 | 5 | 38 |
| GTQ not completed month 3. | 0 | 7 (7.8) | 0 | 12 (13.3) |
| Lab values not taken month 3. | 3 (3.3) | 12 (13.3) | 0 | 7 (7.8) |
| GTQ not completed month 6. | 2 (2.2) | 7 (7.8) | 1 (1.1) | 7 (7.8) |
| Month 3 Visit not done. | 4 (4.4) | 9 (10.0) | 1 (1.1) | 3 (3.3) |
| Month 6 Visit not done. | 8 (8.9) | 9 (10.0) | 3 (3.3) | 9 (10.0) |
addv_tbl <-
addv_tbl |>
tab_spanner(
label = md("COVID-19 Related"),
columns = c("n_Placebo_Y", "n_Placebo_N"),
id = "cov_pla"
) |>
tab_spanner(
label = md("COVID-19 Related"),
columns = c("n_Drug 1_Y", "n_Drug 1_N"),
id = "cov_dru"
) |>
tab_spanner(
label = md("Placebo \n N=90 (100%) \n n (%)"),
columns = c("n_Placebo_Y", "n_Placebo_N")
) |>
tab_spanner(
label = md("Drug 1 \n N=90 (100%) \n n (%)"),
columns = c("n_Drug 1_Y", "n_Drug 1_N")
) |>
cols_label(
.list = list(
"n_Placebo_Y" = "Yes",
"n_Placebo_N" = "No",
"n_Drug 1_Y" = "Yes",
"n_Drug 1_N" = "No"
)
) |>
tab_style(
style = cell_text(align = "center"),
locations = cells_column_spanners(spanners = everything())
)
addv_tbl| xx.x: Demographic and Baseline Data | ||||
| xx.x.x: Major Protocol Deviations and Relationship to COVID-19 - ITT Set | ||||
|
Placebo
N=90 (100%) n (%) |
Drug 1
N=90 (100%) n (%) |
|||
|---|---|---|---|---|
|
COVID-19 Related
|
COVID-19 Related
|
|||
| Yes | No | Yes | No | |
| Subjects with at least: | ||||
| At least one major Protocol Deviation | 15 (16.7) | 24 (26.7) | 5 (5.6) | 23 (25.6) |
| Study Procedure Deviations | 17 | 44 | 5 | 38 |
| GTQ not completed month 3. | 0 | 7 (7.8) | 0 | 12 (13.3) |
| Lab values not taken month 3. | 3 (3.3) | 12 (13.3) | 0 | 7 (7.8) |
| GTQ not completed month 6. | 2 (2.2) | 7 (7.8) | 1 (1.1) | 7 (7.8) |
| Month 3 Visit not done. | 4 (4.4) | 9 (10.0) | 1 (1.1) | 3 (3.3) |
| Month 6 Visit not done. | 8 (8.9) | 9 (10.0) | 3 (3.3) | 9 (10.0) |
We can now add a footnote, indicating that subjects can have more than one PD during the course of the study. The footnote is added with tab_footnote() to the row At least one major Protocol Deviation.
let addvFootnoted = tabella(addvSpanned):
tabFootnote "Subjects can have more than one Protocol Deviation throughout the study.",
loc.stub(rows = where(it.PARAM == "At least one major Protocol Deviation"))
nbTable addvFootnoted
| xx.x: Demographic and Baseline Data | ||||
|---|---|---|---|---|
| xx.x.x: Major Protocol Deviations and Relationship to COVID-19 - ITT Set | ||||
| Placebo N=90 (100%) n (%) | Drug 1 N=90 (100%) n (%) | |||
| COVID-19 Related | COVID-19 Related | |||
| Yes | No | Yes | No | |
| Subjects with at least: | ||||
| At least one major Protocol Deviation1 | 15 (16.7) | 24 (26.7) | 5 (5.6) | 23 (25.6) |
| Study Procedure Deviations | 17 | 44 | 5 | 38 |
| GTQ not completed month 3. | 0 | 7 (7.8) | 0 | 12 (13.3) |
| Lab values not taken month 3. | 3 (3.3) | 12 (13.3) | 0 | 7 (7.8) |
| GTQ not completed month 6. | 2 (2.2) | 7 (7.8) | 1 (1.1) | 7 (7.8) |
| Month 3 Visit not done. | 4 (4.4) | 9 (10.0) | 1 (1.1) | 3 (3.3) |
| Month 6 Visit not done. | 8 (8.9) | 9 (10.0) | 3 (3.3) | 9 (10.0) |
| 1 Subjects can have more than one Protocol Deviation throughout the study. | ||||
addv_tbl = addv_tbl.tab_footnote(
footnote="Subjects can have more than one Protocol Deviation throughout the study.",
locations=loc.stub(rows=["At least one major Protocol Deviation"]),
placement="right",
)
addv_tbl| xx.x: Demographic and Baseline Data | ||||
| xx.x.x: Major Protocol Deviations and Relationship to COVID-19 - ITT Set | ||||
|   |
Placebo N=90 (100%) n (%) |
Drug 1 N=90 (100%) n (%) |
||
|---|---|---|---|---|
| COVID-19 Related | ||||
| Yes | No | Yes | No | |
| Subjects with at least: | () | () | () | () |
| At least one major Protocol Deviation1 | 15 (16.7) | 24 (26.7) | 5 (5.6) | 23 (25.6) |
| Study Procedure Deviations | 17 | 44 | 5 | 38 |
| GTQ not completed month 3. | 0 | 7 (7.8) | 0 | 12 (13.3) |
| Lab values not taken month 3. | 3 (3.3) | 12 (13.3) | 0 | 7 (7.8) |
| GTQ not completed month 6. | 2 (2.2) | 7 (7.8) | 1 (1.1) | 7 (7.8) |
| Month 3 Visit not done. | 4 (4.4) | 9 (10.0) | 1 (1.1) | 3 (3.3) |
| Month 6 Visit not done. | 8 (8.9) | 9 (10.0) | 3 (3.3) | 9 (10.0) |
| 1 Subjects can have more than one Protocol Deviation throughout the study. | ||||
addv_tbl <-
addv_tbl |>
tab_footnote(
footnote = "Subjects can have more than one Protocol Deviation throughout the study.",
locations = cells_stub(rows = c("At least one major Protocol Deviation")),
placement = "right"
)
addv_tbl| xx.x: Demographic and Baseline Data | ||||
| xx.x.x: Major Protocol Deviations and Relationship to COVID-19 - ITT Set | ||||
|
Placebo
N=90 (100%) n (%) |
Drug 1
N=90 (100%) n (%) |
|||
|---|---|---|---|---|
|
COVID-19 Related
|
COVID-19 Related
|
|||
| Yes | No | Yes | No | |
| Subjects with at least: | ||||
| At least one major Protocol Deviation1 | 15 (16.7) | 24 (26.7) | 5 (5.6) | 23 (25.6) |
| Study Procedure Deviations | 17 | 44 | 5 | 38 |
| GTQ not completed month 3. | 0 | 7 (7.8) | 0 | 12 (13.3) |
| Lab values not taken month 3. | 3 (3.3) | 12 (13.3) | 0 | 7 (7.8) |
| GTQ not completed month 6. | 2 (2.2) | 7 (7.8) | 1 (1.1) | 7 (7.8) |
| Month 3 Visit not done. | 4 (4.4) | 9 (10.0) | 1 (1.1) | 3 (3.3) |
| Month 6 Visit not done. | 8 (8.9) | 9 (10.0) | 3 (3.3) | 9 (10.0) |
| 1 Subjects can have more than one Protocol Deviation throughout the study. | ||||
Finally, we can style the table, indenting the individual PDs under Study Procedure Deviations, left-aligning the first column and centering all other columns. Note that for the indentation, we can still use the hidden column PARCAT1 to identify individual PDs.
let addvFinal = tabella(addvFootnoted):
colsAlign alCenter, startsWith("n_")
colsAlign alLeft, cols("PARCAT1", "PARAM")
tabStubIndent where(it.PARCAT1 == "PROTOCOL DEVIATION"), 5
nbTable addvFinal
| xx.x: Demographic and Baseline Data | ||||
|---|---|---|---|---|
| xx.x.x: Major Protocol Deviations and Relationship to COVID-19 - ITT Set | ||||
| Placebo N=90 (100%) n (%) | Drug 1 N=90 (100%) n (%) | |||
| COVID-19 Related | COVID-19 Related | |||
| Yes | No | Yes | No | |
| Subjects with at least: | ||||
| At least one major Protocol Deviation1 | 15 (16.7) | 24 (26.7) | 5 (5.6) | 23 (25.6) |
| Study Procedure Deviations | 17 | 44 | 5 | 38 |
| GTQ not completed month 3. | 0 | 7 (7.8) | 0 | 12 (13.3) |
| Lab values not taken month 3. | 3 (3.3) | 12 (13.3) | 0 | 7 (7.8) |
| GTQ not completed month 6. | 2 (2.2) | 7 (7.8) | 1 (1.1) | 7 (7.8) |
| Month 3 Visit not done. | 4 (4.4) | 9 (10.0) | 1 (1.1) | 3 (3.3) |
| Month 6 Visit not done. | 8 (8.9) | 9 (10.0) | 3 (3.3) | 9 (10.0) |
| 1 Subjects can have more than one Protocol Deviation throughout the study. | ||||
addv_tbl.cols_align(align="center", columns=cs.starts_with("n_")).cols_align(
align="left", columns=["PARCAT1", "PARAM"]
).tab_stub_indent(rows=pl.col("PARCAT1") == "PROTOCOL DEVIATION", indent=5)| xx.x: Demographic and Baseline Data | ||||
| xx.x.x: Major Protocol Deviations and Relationship to COVID-19 - ITT Set | ||||
|   |
Placebo N=90 (100%) n (%) |
Drug 1 N=90 (100%) n (%) |
||
|---|---|---|---|---|
| COVID-19 Related | ||||
| Yes | No | Yes | No | |
| Subjects with at least: | () | () | () | () |
| At least one major Protocol Deviation1 | 15 (16.7) | 24 (26.7) | 5 (5.6) | 23 (25.6) |
| Study Procedure Deviations | 17 | 44 | 5 | 38 |
| GTQ not completed month 3. | 0 | 7 (7.8) | 0 | 12 (13.3) |
| Lab values not taken month 3. | 3 (3.3) | 12 (13.3) | 0 | 7 (7.8) |
| GTQ not completed month 6. | 2 (2.2) | 7 (7.8) | 1 (1.1) | 7 (7.8) |
| Month 3 Visit not done. | 4 (4.4) | 9 (10.0) | 1 (1.1) | 3 (3.3) |
| Month 6 Visit not done. | 8 (8.9) | 9 (10.0) | 3 (3.3) | 9 (10.0) |
| 1 Subjects can have more than one Protocol Deviation throughout the study. | ||||
addv_tbl |>
cols_align(
align = "center",
columns = 3:6
) |>
cols_align(
align = "left",
columns = 1:2
) |>
tab_stub_indent(
rows = PARCAT1 == "PROTOCOL DEVIATION",
indent = 5
)| xx.x: Demographic and Baseline Data | ||||
| xx.x.x: Major Protocol Deviations and Relationship to COVID-19 - ITT Set | ||||
|
Placebo
N=90 (100%) n (%) |
Drug 1
N=90 (100%) n (%) |
|||
|---|---|---|---|---|
|
COVID-19 Related
|
COVID-19 Related
|
|||
| Yes | No | Yes | No | |
| Subjects with at least: | ||||
| At least one major Protocol Deviation1 | 15 (16.7) | 24 (26.7) | 5 (5.6) | 23 (25.6) |
| Study Procedure Deviations | 17 | 44 | 5 | 38 |
| GTQ not completed month 3. | 0 | 7 (7.8) | 0 | 12 (13.3) |
| Lab values not taken month 3. | 3 (3.3) | 12 (13.3) | 0 | 7 (7.8) |
| GTQ not completed month 6. | 2 (2.2) | 7 (7.8) | 1 (1.1) | 7 (7.8) |
| Month 3 Visit not done. | 4 (4.4) | 9 (10.0) | 1 (1.1) | 3 (3.3) |
| Month 6 Visit not done. | 8 (8.9) | 9 (10.0) | 3 (3.3) | 9 (10.0) |
| 1 Subjects can have more than one Protocol Deviation throughout the study. | ||||
import tabbook
bookInit()
nbText: "# Case Study: Clinical Tables"
attribution gtVignette("Case Study: Clinical Tables", "case-study-clinical-tables",
"The R chunks are the article's own; the Python tab writes them with Polars. The datasets " &
"rx_adsl and rx_addv are written from gt as CSV files, with their labels in a JSON file.")
nbText: """
The **gt** package contains the `rx_adsl` dataset, which resembles the structure of a common ADSL ADaM dataset for clinical trial data. Each record refers to demographic information for a single subject in the fictional trial. Every column is equipped with a label attribute allowing the users to get familiar with the data.
"""
tabs """
import json
from math import exp, lgamma, log, sqrt
import polars as pl
import polars.selectors as cs
from great_tables import GT, loc, md, style
# gt's `rx_adsl`, and the labels, units and levels R keeps with its columns
rx_meta = json.load(open("../data/gt/rx_meta.json"))
rx_adsl = pl.read_csv("../data/gt/rx_adsl.csv")
rx_adsl.glimpse()
""", """
library(gt)
library(dplyr)
library(tidyr)
library(rlang)
library(purrr)
rx_adsl |> str()
""":
nbCode:
import std/[json, math, sequtils, streams, strutils]
import nanuq
import tabelline, tabelline/nanuq, gtdata
# gt's `rx_adsl`; `rxMeta()` has the labels, units and levels R keeps with its columns
let rxAdsl = readCsv(newStringStream(rxAdslCsv))
echo rxAdsl.glimpse
tabelline """
gt's datasets `rx_adsl` and `rx_addv` come with the book as CSV files, with what R keeps with
their columns (labels, units, the order of the levels) in a JSON file: Great Tables does not have
them, and a CSV file has no labels.
"""
nbText: """
### Demographic Summary Tables
Let's start with an example of a basic demographic summary table. In a first step, we use **dplyr** and **tidyr** to create a tibble with the shape of our desired table and then use **gt** functions to create the output table:
"""
tabs """
arms = rx_meta["rx_adsl"]["TRTA"]["levels"]
def custom_summary(df, group_var, sum_var):
info = rx_meta["rx_adsl"][sum_var]
if "levels" in info:
category_lbl = f"{info['label']}, n (%)"
df_out = (
df.group_by(group_var, sum_var)
.agg(val=pl.len())
.with_columns(pct=pl.col("val") / pl.col("val").sum().over(group_var))
.pivot(on=group_var, index=sum_var, values=["val", "pct"])
.fill_null(0)
.sort(pl.col(sum_var).cast(pl.Enum(info["levels"])))
.rename({sum_var: "label"})
.with_columns(category=pl.lit(category_lbl))
)
else:
category_lbl = f"{info['label']} ({info['units']})"
rows = {"n": {}, "Mean (SD)": {}, "Median": {}, "Min - Max": {}}
for arm in arms:
x = df.filter(pl.col(group_var) == arm)[sum_var].drop_nulls().cast(pl.Float64)
rows["n"][f"val_{arm}"] = float(x.len())
rows["Mean (SD)"] |= {f"val_{arm}": x.mean(), f"sd_{arm}": x.std()}
rows["Median"][f"val_{arm}"] = x.median()
rows["Min - Max"] |= {f"min_{arm}": x.min(), f"max_{arm}": x.max()}
df_out = pl.DataFrame(
[{"label": label, **values} for label, values in rows.items()],
infer_schema_length=None,
).with_columns(category=pl.lit(category_lbl))
return df_out
adsl_summary = pl.concat(
[
custom_summary(rx_adsl.filter(pl.col("ITTFL") == "Y"), "TRTA", sum_var)
for sum_var in ["AGE", "AAGEGR1", "SEX", "ETHNIC", "BLBMI"]
],
how="diagonal_relaxed",
)
""", """
custom_summary <- function(df, group_var, sum_var) {
group_var <- rlang::ensym(group_var)
sum_var <- rlang::ensym(sum_var)
is_categorical <-
is.character(eval(expr(`$`(df, !!sum_var)))) |
is.factor(eval(expr(`$`(df, !!sum_var))))
if (is_categorical) {
category_lbl <-
sprintf("%s, n (%%)", attr(eval(expr(`$`(df, !!sum_var))), "label"))
df_out <-
df |>
dplyr::group_by(!!group_var) |>
dplyr::mutate(N = dplyr::n()) |>
dplyr::ungroup() |>
dplyr::group_by(!!group_var, !!sum_var) |>
dplyr::summarize(
val = dplyr::n(),
pct = dplyr::n() / mean(N),
.groups = "drop"
) |>
tidyr::pivot_wider(
id_cols = !!sum_var, names_from = !!group_var,
values_from = c(val, pct)
) |>
dplyr::rename(label = !!sum_var) |>
dplyr::mutate(
across(where(is.numeric), ~ ifelse(is.na(.), 0, .)),
category = category_lbl
)
} else {
category_lbl <-
sprintf(
"%s (%s)",
attr(eval(expr(`$`(df, !!sum_var))), "label"),
attr(eval(expr(`$`(df, !!sum_var))), "units")
)
df_out <-
df |>
dplyr::group_by(!!group_var) |>
dplyr::summarize(
n = sum(!is.na(!!sum_var)),
mean = mean(!!sum_var, na.rm = TRUE),
sd = sd(!!sum_var, na.rm = TRUE),
median = median(!!sum_var, na.rm = TRUE),
min = min(!!sum_var, na.rm = TRUE),
max = max(!!sum_var, na.rm = TRUE),
min_max = NA,
.groups = "drop"
) |>
tidyr::pivot_longer(
cols = c(n, mean, median, min_max),
names_to = "label",
values_to = "val"
) |>
dplyr::mutate(
sd = ifelse(label == "mean", sd, NA),
max = ifelse(label == "min_max", max, NA),
min = ifelse(label == "min_max", min, NA),
label = dplyr::recode(
label,
"mean" = "Mean (SD)",
"min_max" = "Min - Max",
"median" = "Median"
)
) |>
tidyr::pivot_wider(
id_cols = label,
names_from = !!group_var,
values_from = c(val, sd, min, max)
) |>
dplyr::mutate(category = category_lbl)
}
return(df_out)
}
adsl_summary <-
dplyr::filter(rx_adsl, ITTFL == "Y") |>
(\(data) purrr::map_df(
.x = dplyr::vars(AGE, AAGEGR1, SEX, ETHNIC, BLBMI),
.f = \(x) custom_summary(df = data, group_var = TRTA, sum_var = !!x)
))()
""":
nbCode:
let meta = rxMeta()["rx_adsl"]
let arms = meta["TRTA"]["levels"].to(seq[string])
proc customSummary(df: DataFrame, groupVar, sumVar: string): DataFrame =
let info = meta[sumVar]
if info.hasKey("levels"):
let categoryLbl = info["label"].getStr & ", n (%)"
df.groupBy(groupVar, sumVar)
.agg(len().alias("val"))
.withColumns((col("val") / col("val").sum.over(groupVar)).alias("pct"))
.pivot(groupVar, index = sumVar, values = ["val", "pct"])
.fillNull(0)
.sort([col(sumVar).cast(Enum(info["levels"].to(seq[string])))])
.rename({sumVar: "label"})
.withColumns(lit(categoryLbl).alias("category"))
else:
let categoryLbl = info["label"].getStr & " (" & info["units"].getStr & ")"
let x = col(sumVar).cast(float)
df.groupBy(groupVar)
.agg(
x.isNotNull.sum.cast(float).alias("n"),
x.mean.alias("mean"),
x.std.alias("sd"),
x.median.alias("median"),
x.min.alias("min"),
x.max.alias("max"),
lit(nil).cast(float).alias("min_max"))
.unpivot(["n", "mean", "median", "min_max"], index = [groupVar, "sd", "min", "max"],
variableName = "label", valueName = "val")
.withColumns(
`when`(col("label") == "mean").then(col("sd")).otherwise(nil).alias("sd"),
`when`(col("label") == "min_max").then(col("max")).otherwise(nil).alias("max"),
`when`(col("label") == "min_max").then(col("min")).otherwise(nil).alias("min"),
col("label").replace({"mean": "Mean (SD)", "min_max": "Min - Max", "median": "Median"}))
.pivot(groupVar, index = "label", values = ["val", "sd", "min", "max"])
.withColumns(lit(categoryLbl).alias("category"))
let adslItt = rxAdsl.filter(col("ITTFL") == "Y")
let adslSummary = concat(
["AGE", "AAGEGR1", "SEX", "ETHNIC", "BLBMI"].mapIt(customSummary(adslItt, "TRTA", it)),
how = ConcatMethod.DiagonalRelaxed)
tabelline """
tabelline makes tables, not data: the data steps are written with
[nanuq](https://github.com/pietroppeter/nanuq), Polars for Nim, as R writes them with dplyr and
tidyr (`unpivot` is `pivot_longer`, `pivot` is `pivot_wider`), and `import tabelline/nanuq`
makes `tabella(df)` work for its data frames. `` `when` `` takes backticks: `when` is a word of
Nim. `rxMeta()` gives the labels and the order of the levels, as R's attributes do.
"""
nbText: """
We can now start to expose our tibble with the summary of adsl variables to **gt** using `gt()`. Values should be grouped by category, with labels as rownames. In addition, we can give our table a nice title and subtitle.
"""
tabs """
rx_adsl_tbl = GT(
adsl_summary, rowname_col="label", groupname_col="category"
).tab_header(
title="x.x: Demographic Characteristics",
subtitle="x.x.x: Demographic Characteristics - ITT Analysis Set",
)
rx_adsl_tbl
""", """
rx_adsl_tbl <-
adsl_summary |>
gt(
rowname_col = "label",
groupname_col = "category"
) |>
tab_header(
title = "x.x: Demographic Characteristics",
subtitle = "x.x.x: Demographic Characteristics - ITT Analysis Set"
)
rx_adsl_tbl
""":
nbCode:
let rxAdslTbl = tabella(adslSummary, rownameCol = "label", groupnameCol = "category"):
tabHeader "x.x: Demographic Characteristics",
"x.x.x: Demographic Characteristics - ITT Analysis Set"
nbTable rxAdslTbl
nbText: """
As a first step, let's try to format the columns, formatting counts, min, max and medians with `fmt_integer()`, percentages with `fmt_percent()`, and mean and sd values with `fmt_number()` using `1` and `2` decimals, respectively. We are intentionally keeping the `NA` values for now, as these will be needed in the `cols_merge()` pattern in the next step.
"""
tabs """
rx_adsl_tbl = (
rx_adsl_tbl
.fmt_integer(
columns=cs.starts_with("val_", "min_", "max_"),
# (and the counts, which R shows as 36 unformatted, Polars as 36.0)
rows=pl.col("label").is_in(["n", "Median", "Min - Max"])
| pl.col("category").str.ends_with(", n (%)"),
)
.fmt_percent(columns=cs.starts_with("pct_"), decimals=1)
.fmt_number(
columns=cs.starts_with("val_"), rows=pl.col("label") == "Mean (SD)", decimals=1
)
.fmt_number(
columns=cs.starts_with("sd_"), rows=pl.col("label") == "Mean (SD)", decimals=2
)
)
rx_adsl_tbl
""", """
rx_adsl_tbl <-
rx_adsl_tbl |>
fmt_integer(
columns = starts_with(c("val_", "min_", "max_")),
rows = label %in% c("n", "Median", "Min - Max")
) |>
fmt_percent(
columns = starts_with("pct_"),
decimals = 1
) |>
fmt_number(
columns = starts_with("val_"),
rows = label == "Mean (SD)",
decimals = 1
) |>
fmt_number(
columns = starts_with("sd_"),
rows = label == "Mean (SD)",
decimals = 2
)
rx_adsl_tbl
""":
nbCode:
let rxAdslFmt = tabella(rxAdslTbl):
fmtInteger startsWith(["val_", "min_", "max_"]),
rows = where($it.label in ["n", "Median", "Min - Max"] or ($it.category).endsWith(", n (%)"))
fmtPercent startsWith("pct_"), decimals = 1
fmtNumber startsWith("val_"), rows = where(it.label == "Mean (SD)"), decimals = 1
fmtNumber startsWith("sd_"), rows = where(it.label == "Mean (SD)"), decimals = 2
nbTable rxAdslFmt
tabelline """
`startsWith` takes several prefixes, as gt's `starts_with()` does, in brackets:
`startsWith("val_", "min_")` with two texts would be Nim's own `startsWith` of strutils.
"""
nbText: """
This looks way better but our table still has a rather wide style. To collapse the columns appropriately, we will use `cols_merge()`, combining mean and SD, min and max, as well as n and percentages, respectively. We will use the `pattern` argument to specify our custom merging pattern.
"""
tabs """
rx_adsl_tbl = rx_adsl_tbl.cols_merge(
columns=["val_Placebo", "pct_Placebo", "sd_Placebo", "min_Placebo", "max_Placebo"],
pattern="<<{0}>><< ({1})>><< ({2})>><<{3} - {4}>>",
).cols_merge(
columns=["val_Drug 1", "pct_Drug 1", "sd_Drug 1", "min_Drug 1", "max_Drug 1"],
pattern="<<{0}>><< ({1})>><< ({2})>><<{3} - {4}>>",
)
rx_adsl_tbl
""", """
rx_adsl_tbl <-
rx_adsl_tbl |>
cols_merge(
columns = c("val_Placebo", "pct_Placebo", "sd_Placebo", "min_Placebo", "max_Placebo"),
pattern = "<<{1}>><< ({2})>><< ({3})>><<{4} - {5}>>"
) |>
cols_merge(
columns = c("val_Drug 1", "pct_Drug 1", "sd_Drug 1", "min_Drug 1", "max_Drug 1"),
pattern = "<<{1}>><< ({2})>><< ({3})>><<{4} - {5}>>"
)
rx_adsl_tbl
""":
nbCode:
let rxAdslMerged = tabella(rxAdslFmt):
for arm in arms:
colsMerge ["val_", "pct_", "sd_", "min_", "max_"].mapIt(it & arm),
pattern = "<<{1}>><< ({2})>><< ({3})>><<{4} - {5}>>"
nbTable rxAdslMerged
tabelline """
A `for` in a `tabella` body repeats its verbs: one merge for each arm. Great Tables numbers the
columns of a pattern from `{0}`, gt and tabelline from `{1}`.
"""
nbText: """
Now that looks more like a demographic table. But let's take a step back and understand the merging pattern. `{ }` are used to arrange the single column values in a row-wise fashion. The number in curly braces corresponds to the order specified in the `columns = ` argument. We use `<< >>` to surround spans of text that will be omitted if any of the values within contain missing values. Our first column in the call to `cols_merge()` contains values for n's, means and medians and is printed if the values are not missing (meaning for the cells for numeric and categorical n's, means and medians but not for min and max). The SD and percentages for categorical grouping variables are then appended to the cells for mean and categorical n's, because these are the only rows that contain non-missing values. All of the previous aspects are ignored for the min and max row, as n's, percentages, SD's and medians are missing. Here, only min and max are arranged.
We can now start to look in to style features. Let us indent the values in the stub using `tab_stub_indent()` and left-align the title with `opt_align_table_header()`.
"""
tabs """
rx_adsl_tbl = rx_adsl_tbl.tab_stub_indent(
rows=list(range(len(adsl_summary))), indent=5
).opt_align_table_header(align="left")
rx_adsl_tbl
""", """
rx_adsl_tbl <-
rx_adsl_tbl |>
tab_stub_indent(
rows = everything(),
indent = 5
) |>
opt_align_table_header(align = "left")
rx_adsl_tbl
""":
nbCode:
let rxAdslIndented = tabella(rxAdslMerged):
tabStubIndent allRows(), 5
optAlignTableHeader "left"
nbTable rxAdslIndented
nbText: """
Let's now change the column width of our Placebo and Drug 1 columns and align all values to the center, making use of `cols_width()` and `cols_align()`.
"""
tabs """
rx_adsl_tbl = rx_adsl_tbl.cols_width(
{"label": "250px"} | {f"val_{arm}": "200px" for arm in arms}
).cols_align(align="center", columns=cs.starts_with("val_"))
rx_adsl_tbl
""", """
rx_adsl_tbl <-
rx_adsl_tbl |>
cols_width(
starts_with("val_") ~ px(200),
1 ~ px(250)
) |>
cols_align(
align = "center",
columns = starts_with("val_")
)
rx_adsl_tbl
""":
nbCode:
let rxAdslAligned = tabella(rxAdslIndented):
colsWidth @[("label", "250px")] & arms.mapIt(("val_" & it, "200px"))
colsAlign alCenter, startsWith("val_")
nbTable rxAdslAligned
nbText: """
In a final step we can now take care of the column names and assign something more meaningful. Out column header should be the name of the study intervention together with the respective subject count. To make use of `cols_label()`'s ability to handle lists, we summarize our new column labels in a named list.
"""
tabs """
# Count subjects per arm and make the labels of the columns
arm_n = (
rx_adsl.filter(pl.col("ITTFL") == "Y")
.group_by("TRTA")
.len()
.sort(pl.col("TRTA").cast(pl.Enum(arms)))
)
collbl_list = {
f"val_{arm}": f"{arm} N={n} (100%)" for arm, n in arm_n.iter_rows()
}
rx_adsl_tbl = rx_adsl_tbl.cols_label(collbl_list)
rx_adsl_tbl
""", """
### Count subjects per arm and summarize values in a list
arm_n <-
rx_adsl |>
dplyr::filter(ITTFL == "Y") |>
dplyr::group_by(TRTA) |>
dplyr::summarize(
lbl = sprintf("%s N=%i (100%%)", unique(TRTA), dplyr::n()),
.groups = "drop"
) |>
dplyr::arrange(TRTA)
collbl_list <- as.list(arm_n$lbl)
names(collbl_list) <- paste0("val_", arm_n$TRTA)
rx_adsl_tbl <-
rx_adsl_tbl |>
cols_label(.list = collbl_list)
rx_adsl_tbl
""":
nbCode:
# Count subjects per arm and make the labels of the columns
var collblList: seq[(string, string)]
for arm in arms:
let n = adslItt.filter(col("TRTA") == arm).height
collblList.add ("val_" & arm, arm & " N=" & $n & " (100%)")
let rxAdslLabelled = tabella(rxAdslAligned):
colsLabel collblList
nbTable rxAdslLabelled
nbText: """
### Response / Event Rate Analysis Tables
In another table, we can summarize the number of subjects with an event per intervention in the subgroup defined by the age groups. Within each intervention group we are counting the number and percentage of participants with an event (`EVNTFL == "Y"`) as well as the total number of participants. The number of participants with an event divided by the number without an event are the odds of experiencing the event per study intervention. The odds ratio is then computed as the odds under Drug 1 divided by the odds under Placebo.
The below code performs the calculation outlined above within the subgroup defined by `AAGEGR1`, where confidence intervals around the event rates are computed using the Clopper Pearson method.
"""
tabs """
# The probability of at most `k` events in `n`, each with probability `p`
def binom_cdf(k, n, p):
return sum(
exp(lgamma(n + 1) - lgamma(i + 1) - lgamma(n - i + 1) + i * log(p) + (n - i) * log(1 - p))
for i in range(k + 1)
)
# The `p` in (0, 1) where `f`, increasing, is zero
def bisect(f):
lo, hi = 0.0, 1.0
for _ in range(60):
mid = (lo + hi) / 2
lo, hi = (mid, hi) if f(mid) < 0 else (lo, mid)
return (lo + hi) / 2
# The Clopper-Pearson interval of `x` events in `n` (R's `qf()` gives the same)
def clopper_pearson(x, n, level=0.95):
a = (1 - level) / 2
low = 0.0 if x == 0 else bisect(lambda p: 1 - binom_cdf(x - 1, n, p) - a)
up = 1.0 if x == n else bisect(lambda p: a - binom_cdf(x, n, p))
return low, up
z = 1.959963984540054 # R's qnorm(0.975)
rows = []
for age in rx_meta["rx_adsl"]["AAGEGR1"]["levels"]:
row = {"AAGEGR1": age}
for arm in arms:
subjects = rx_adsl.filter(
(pl.col("ITTFL") == "Y") & (pl.col("TRTA") == arm) & (pl.col("AAGEGR1") == age)
)
x = (subjects["EVNTFL"] == "Y").sum()
n = len(subjects)
low, up = clopper_pearson(x, n)
row |= {
f"n_resp_{arm}": x, f"n_total_{arm}": n, f"pct_{arm}": 100 * x / n,
f"ci_up_{arm}": 100 * up, f"ci_low_{arm}": 100 * low, f"odds_{arm}": x / (n - x),
}
rows.append(row)
rx_responders = pl.DataFrame(rows)
rx_responders = (
rx_responders.with_columns(
**{
"or": pl.when(pl.col("odds_Placebo") == 0)
.then(None)
.otherwise(pl.col("odds_Drug 1") / pl.col("odds_Placebo"))
}
)
.with_columns(
se=(
1 / pl.col("n_resp_Placebo")
+ 1 / pl.col("n_resp_Drug 1")
+ 1 / (pl.col("n_total_Placebo") - pl.col("n_resp_Placebo"))
+ 1 / (pl.col("n_total_Drug 1") - pl.col("n_resp_Drug 1"))
).sqrt()
)
.with_columns(
or_ci_low=(pl.col("or").log() - z * pl.col("se")).exp(),
or_ci_up=(pl.col("or").log() + z * pl.col("se")).exp(),
)
.select(
"AAGEGR1",
*[f"{v}_{arm}" for v in ["n_resp", "n_total", "pct", "ci_up", "ci_low"] for arm in arms],
"or", "or_ci_low", "or_ci_up",
)
)
""", """
rx_responders <-
rx_adsl |>
dplyr::filter(ITTFL == "Y") |>
dplyr::group_by(TRTA, AAGEGR1) |>
dplyr::summarize(
n_resp = sum(EVNTFL == "Y"),
n_total = dplyr::n(),
pct = 100 * sum(EVNTFL == "Y") / dplyr::n(),
ci_up = 100 * (
1 + (dplyr::n() - sum(EVNTFL == "Y")) / (
(sum(EVNTFL == "Y") + 1) * qf(
0.975,
2 * (sum(EVNTFL == "Y") + 1),
2 * (dplyr::n() - sum(EVNTFL == "Y"))
)
)
)^(-1),
ci_low = ifelse(
sum(EVNTFL == "Y") == 0,
0,
100 * (
1 + (dplyr::n() - sum(EVNTFL == "Y") + 1) /
(sum(EVNTFL == "Y") * qf(
0.025,
2 * sum(EVNTFL == "Y"),
2 * (dplyr::n() - sum(EVNTFL == "Y") + 1)
)
)
)^(-1)
),
odds = sum(EVNTFL == "Y") / (dplyr::n() - sum(EVNTFL == "Y")),
.groups = "drop"
) |>
tidyr::pivot_wider(
id_cols = AAGEGR1,
names_from = TRTA,
values_from = c(n_resp, n_total, pct, ci_up, ci_low, odds)
) |>
dplyr::mutate(
or = ifelse(
odds_Placebo == 0,
NA_real_,
!!sym("odds_Drug 1") / odds_Placebo
),
or_ci_low = exp(
log(or) - qnorm(0.975) * sqrt(
1 / n_resp_Placebo +
1 / !!sym("n_resp_Drug 1") +
1 / (n_total_Placebo - n_resp_Placebo) +
1 / (!!sym("n_total_Drug 1") - !!sym("n_resp_Drug 1"))
)
),
or_ci_up = exp(
log(or) + qnorm(0.975) * sqrt(
1 / n_resp_Placebo +
1 / !!sym("n_resp_Drug 1") +
1 / (n_total_Placebo - n_resp_Placebo) +
1 / (!!sym("n_total_Drug 1") - !!sym("n_resp_Drug 1"))
)
)
) |>
dplyr::select(-tidyselect::starts_with("odds_"))
""":
nbCode:
# The probability of at most `k` events in `n`, each with probability `p`
proc binomCdf(k, n: int, p: float): float =
for i in 0 .. k:
result += exp(lgamma(float(n + 1)) - lgamma(float(i + 1)) - lgamma(float(n - i + 1)) +
float(i) * ln(p) + float(n - i) * ln(1 - p))
# The `p` in (0, 1) where `f`, increasing, is zero
proc bisect(f: proc (p: float): float): float =
var (lo, hi) = (0.0, 1.0)
for _ in 1 .. 60:
let mid = (lo + hi) / 2
if f(mid) < 0: lo = mid else: hi = mid
(lo + hi) / 2
# The Clopper-Pearson interval of `x` events in `n` (R's `qf()` gives the same)
proc clopperPearson(x, n: int, level = 0.95): (float, float) =
let a = (1 - level) / 2
let low = if x == 0: 0.0 else: bisect(proc (p: float): float = 1 - binomCdf(x - 1, n, p) - a)
let up = if x == n: 1.0 else: bisect(proc (p: float): float = a - binomCdf(x, n, p))
(low, up)
const z = 1.959963984540054 # R's qnorm(0.975)
let counts = adslItt
.groupBy("TRTA", "AAGEGR1")
.agg((col("EVNTFL") == "Y").sum.alias("n_resp"), len().alias("n_total"))
.sort([col("TRTA").cast(Enum(arms)),
col("AAGEGR1").cast(Enum(meta["AAGEGR1"]["levels"].to(seq[string])))])
# the intervals in Nim, one for each row: nanuq has no quantiles of the F distribution
let intervals = zip(counts.toList("n_resp", int), counts.toList("n_total", int))
.mapIt(clopperPearson(it[0], it[1]))
let (resp, total) = (col("n_resp"), col("n_total"))
proc byArm(name, arm: string): Expr = col(name & "_" & arm)
let rxResponders = counts
.withColumns(
series("ci_up", intervals.mapIt(100 * it[1])),
series("ci_low", intervals.mapIt(100 * it[0])))
.withColumns((100 * resp / total).alias("pct"), (resp / (total - resp)).alias("odds"))
.pivot("TRTA", index = "AAGEGR1",
values = ["n_resp", "n_total", "pct", "ci_up", "ci_low", "odds"])
.withColumns(
`when`(byArm("odds", "Placebo") == 0).then(nil)
.otherwise(byArm("odds", "Drug 1") / byArm("odds", "Placebo")).alias("or"),
sqrt(1 / byArm("n_resp", "Placebo") + 1 / byArm("n_resp", "Drug 1") +
1 / (byArm("n_total", "Placebo") - byArm("n_resp", "Placebo")) +
1 / (byArm("n_total", "Drug 1") - byArm("n_resp", "Drug 1"))).alias("se"))
.withColumns(
exp(log(col("or")) - z * col("se")).alias("or_ci_low"),
exp(log(col("or")) + z * col("se")).alias("or_ci_up"))
.drop(["odds_Placebo", "odds_Drug 1", "se"])
tabelline """
Nim's standard library has no quantiles of the F distribution (R's `qf()`): the book finds the
Clopper-Pearson interval from the binomial probabilities, by bisection, which gives the same
numbers. Neither has `qnorm()`, so its value at 0.975 is written out.
"""
nbText: """
Let's first create a basic **gt** table with a left-aligned table title and subtitle. Here we are using `tab_header()` and `opt_align_table_header()` again.
"""
tabs """
rx_resp_tbl = GT(rx_responders).tab_header(
title="x.x: Efficacy Data",
subtitle="x.x.x: Occurence of Event per Subgroup - {gt} Analysis Set",
).opt_align_table_header(align="left")
rx_resp_tbl
""", """
rx_resp_tbl <- rx_responders |>
gt() |>
tab_header(
title = "x.x: Efficacy Data",
subtitle = "x.x.x: Occurence of Event per Subgroup - {gt} Analysis Set"
) |>
opt_align_table_header(align = "left")
rx_resp_tbl
""":
nbCode:
let rxRespTbl = tabella(rxResponders):
tabHeader "x.x: Efficacy Data",
"x.x.x: Occurence of Event per Subgroup - {gt} Analysis Set"
optAlignTableHeader "left"
nbTable rxRespTbl
nbText: """
Next, we are formatting the columns for counts to integers with `fmt_integer()`, percentages and CI's around percentages as numbers with one decimal and odds ratio and the CI around the odds ratio as numbers with two decimals, in both cases using `fmt_number()`.
"""
tabs """
rx_resp_tbl = (
rx_resp_tbl
.fmt_integer(columns=cs.starts_with("n_"))
.fmt_number(columns=cs.starts_with("pct_", "ci_"), decimals=1)
.fmt_number(columns=cs.starts_with("or"), decimals=2)
)
rx_resp_tbl
""", """
rx_resp_tbl <-
rx_resp_tbl |>
fmt_integer(columns = starts_with("n_")) |>
fmt_number(columns = starts_with(c("pct_", "ci_")), decimals = 1) |>
fmt_number(columns = starts_with("or"), decimals = 2)
rx_resp_tbl
""":
nbCode:
let rxRespFmt = tabella(rxRespTbl):
fmtInteger startsWith("n_")
fmtNumber startsWith(["pct_", "ci_"]), decimals = 1
fmtNumber startsWith("or"), decimals = 2
nbTable rxRespFmt
nbText: """
We can now merge the columns for participants with events, total number of participants and percentage of participants with events, as well as the 95% CI's around the event rate using `cols_merge()`. To indicate the intervention group we are adding tab spanners with `tab_spanner()`.
"""
tabs """
rx_resp_tbl = (
rx_resp_tbl
.cols_merge(
columns=["n_resp_Placebo", "n_total_Placebo", "pct_Placebo"], pattern="{0}/{1} ({2})"
)
.cols_merge(
columns=["n_resp_Drug 1", "n_total_Drug 1", "pct_Drug 1"], pattern="{0}/{1} ({2})"
)
.cols_merge(columns=["ci_low_Placebo", "ci_up_Placebo"], pattern="[{0}, {1}]")
.cols_merge(columns=["ci_low_Drug 1", "ci_up_Drug 1"], pattern="[{0}, {1}]")
.cols_merge(columns=["or_ci_low", "or_ci_up"], pattern="[{0}, {1}]")
.tab_spanner(label="Drug 1", columns=["n_resp_Drug 1", "ci_low_Drug 1"])
.tab_spanner(label="Placebo", columns=["n_resp_Placebo", "ci_low_Placebo"])
)
rx_resp_tbl
""", """
rx_resp_tbl <-
rx_resp_tbl |>
cols_merge(
columns = c("n_resp_Placebo", "n_total_Placebo", "pct_Placebo"),
pattern = "{1}/{2} ({3})"
) |>
cols_merge(
columns = c("n_resp_Drug 1", "n_total_Drug 1", "pct_Drug 1"),
pattern = "{1}/{2} ({3})"
) |>
cols_merge(
columns = c("ci_low_Placebo", "ci_up_Placebo"),
pattern = "[{1}, {2}]"
) |>
cols_merge(
columns = c("ci_low_Drug 1", "ci_up_Drug 1"),
pattern = "[{1}, {2}]"
) |>
cols_merge(
columns = c("or_ci_low", "or_ci_up"),
pattern = "[{1}, {2}]"
) |>
tab_spanner(
label = "Drug 1",
columns = c("n_resp_Drug 1", "ci_low_Drug 1")
) |>
tab_spanner(
label = "Placebo",
columns = c("n_resp_Placebo", "ci_low_Placebo")
)
rx_resp_tbl
""":
nbCode:
let rxRespMerged = tabella(rxRespFmt):
for arm in arms:
colsMerge ["n_resp_" & arm, "n_total_" & arm, "pct_" & arm], pattern = "{1}/{2} ({3})"
colsMerge ["ci_low_" & arm, "ci_up_" & arm], pattern = "[{1}, {2}]"
colsMerge ["or_ci_low", "or_ci_up"], pattern = "[{1}, {2}]"
tabSpanner "Drug 1", cols("n_resp_Drug 1", "ci_low_Drug 1")
tabSpanner "Placebo", cols("n_resp_Placebo", "ci_low_Placebo")
nbTable rxRespMerged
tabelline """
A spanner gathers its columns next to the first one, as gt's `tab_spanner()` does
(`gather = false` leaves them where they are).
"""
nbText: """
The table is looking way better now. Let's now group the two categories and highlight the fact that these are actually age subgroups. We are using `tab_row_group()` to manually add a row group label *Age*.
"""
tabs """
""", """
rx_resp_tbl <-
rx_resp_tbl |>
tab_row_group(
label = "Age",
rows = everything()
)
rx_resp_tbl
""":
nbCode:
let rxRespGrouped = tabella(rxRespMerged):
tabRowGroup "Age", allRows()
nbTable rxRespGrouped
tabelline """
Great Tables has no `tab_row_group()`: its groups come from a column of the data. Its tab here
goes on without the group *Age*.
"""
nbText: """
Next, we'll take care of the column labels. As we now have the `tab_row_group()` label in place, we no longer need the label for the first column and can assign an empty string. Also, because of the two tab spanners, we can assign equal column labels for event rates and 95% CI's in both intervention groups.
Using `cols_width()` and `cols_align()` we can apply a more convenient column width and left-align the first column.
"""
tabs """
rx_resp_tbl = (
rx_resp_tbl
.cols_align(align="center", columns=cs.starts_with("n_", "ci", "or"))
.cols_label(
{
"AAGEGR1": "",
"n_resp_Placebo": "Event Rate (%)",
"ci_low_Placebo": "[95% CI]",
"n_resp_Drug 1": "Event Rate (%)",
"ci_low_Drug 1": "[95% CI]",
"or": "Odds ratio",
"or_ci_low": "[95% CI]",
}
)
.cols_width({"AAGEGR1": "80px"} | {c: "120px" for c in rx_responders.columns[1:]})
.cols_align(align="left", columns="AAGEGR1")
)
rx_resp_tbl
""", """
rx_resp_tbl <-
rx_resp_tbl |>
cols_align(
align = "center",
columns = starts_with(c("n_", "ci", "or"))
) |>
cols_label(
.list = c(
"AAGEGR1" = "",
"n_resp_Placebo" = "Event Rate (%)",
"ci_low_Placebo" = "[95% CI]",
"n_resp_Drug 1" = "Event Rate (%)",
"ci_low_Drug 1" = "[95% CI]",
"or" = "Odds ratio",
"or_ci_low" = "[95% CI]"
)
) |>
cols_width(
1 ~ px(80),
everything() ~ px(120)
) |>
cols_align(align = "left", columns = 1)
rx_resp_tbl
""":
nbCode:
let rxRespLabelled = tabella(rxRespGrouped):
colsAlign alCenter, startsWith(["n_", "ci", "or"])
colsLabel:
AAGEGR1: ""
n_resp_Placebo: "Event Rate (%)"
ci_low_Placebo: "[95% CI]"
"n_resp_Drug 1": "Event Rate (%)"
"ci_low_Drug 1": "[95% CI]"
"or": "Odds ratio"
or_ci_low: "[95% CI]"
colsWidth rxResponders.columns.mapIt((it, if it == "AAGEGR1": "80px" else: "120px"))
colsAlign alLeft, positions(0)
nbTable rxRespLabelled
tabelline """
In a block of pairs a name that is not a Nim name is written as a string: `"n_resp_Drug 1"`,
and `"or"`, a keyword of Nim. Columns by position count from 0 in tabelline, as in Python:
gt's `columns = 1` is `positions(0)`.
"""
nbText: """
Finally, we make use of `tab_footnote()` and can add a footnote to the columns with the 95% CI's around event rates, indicating that these were derived from the Clopper-Pearson method. To change the default symbol choice of `tab_footnote()` from numbers to letters, we add `tab_options(footnote.marks = letters)`.
"""
tabs """
rx_resp_tbl = rx_resp_tbl.tab_footnote(
footnote="Event rate 95% exact confidence interval uses the Clopper−Pearson method.",
locations=loc.column_labels(columns=["ci_low_Placebo", "ci_low_Drug 1"]),
placement="right",
).tab_options(footnotes_marks="letters")
rx_resp_tbl
""", """
rx_resp_tbl <-
rx_resp_tbl |>
tab_footnote(
footnote = "Event rate 95% exact confidence interval uses the Clopper−Pearson method.",
locations = cells_column_labels(
columns = c("ci_low_Placebo", "ci_low_Drug 1")
),
placement = "right"
) |>
tab_options(footnotes.marks = letters)
rx_resp_tbl
""":
nbCode:
let rxRespFinal = tabella(rxRespLabelled):
tabFootnote "Event rate 95% exact confidence interval uses the Clopper−Pearson method.",
loc.columnLabels(cols("ci_low_Placebo", "ci_low_Drug 1"))
tabOptions:
footnotesMarks = fmLetters
nbTable rxRespFinal
tabelline """
tabelline puts a mark after the label always, so it needs no `placement`.
"""
nbText: """
### Protocol Deviation Table
For the summary table for protocol deviations (PDs) we will use a second CDISC-flavored dataset, namely `rx_addv`. This dataset contains a summary row, indicating whether a subject in the ITT population in `rx_adsl` experienced at least one major PD or not. In addition to the subject level summary, individual PDs are summarized.
"""
tabs """
rx_addv = pl.read_csv("../data/gt/rx_addv.csv")
rx_addv.glimpse()
""", """
rx_addv |> str()
""":
nbCode:
echo glimpse(rxAddv())
nbText: """
We would now like to build a table to summarize overall and counts of individual PDs by treatment arm and furthermore indicate, whether the PD was related to COVID-19 or not. In order to build this table, we first need to apply some data wrangling with functions from **dplyr** and **tidyr**.
"""
tabs """
params = rx_meta["rx_addv"]["PARAM"]["levels"]
addv_sum = (
rx_addv.with_columns(NTOT=pl.col("USUBJID").n_unique().over("TRTA"))
.group_by("TRTA", "PARCAT1", "PARAM", "CRIT1FL")
.agg(
n=pl.col("AVAL").sum(),
pct=100 * pl.col("AVAL").sum() / pl.col("NTOT").mean(),
)
.sort(
pl.col("TRTA").cast(pl.Enum(arms)), "PARCAT1",
pl.col("PARAM").cast(pl.Enum(params)), "CRIT1FL",
)
.with_columns(arm_flag=pl.concat_str("TRTA", "CRIT1FL", separator="_"))
.pivot(on="arm_flag", index=["PARCAT1", "PARAM"], values=["n", "pct"])
.with_columns(cs.numeric().fill_null(0))
)
addv_sum = pl.concat(
[pl.DataFrame({"PARAM": ["Subjects with at least:"]}), addv_sum],
how="diagonal_relaxed",
)
addv_sum
""", """
addv_sum <-
rx_addv |>
dplyr::group_by(TRTA) |>
dplyr::mutate(
NTOT = n_distinct(USUBJID),
.groups = "drop"
) |>
dplyr::group_by(TRTA, PARCAT1, PARAM, CRIT1FL) |>
dplyr::summarize(
n = sum(AVAL, na.rm = TRUE),
pct = 100 * sum(AVAL, na.rm = TRUE) / mean(NTOT),
.groups = "drop"
) |>
tidyr::pivot_wider(
id_cols = c(PARCAT1, PARAM),
names_from = c(TRTA, CRIT1FL),
values_from = c(n, pct)
) |>
dplyr::mutate(across(where(is.numeric), ~ ifelse(is.na(.), 0, .))) |>
dplyr::add_row(PARAM = "Subjects with at least:", .before = 1)
addv_sum
""":
nbCode:
let rxAddv = readCsv(newStringStream(rxAddvCsv))
let params = rxMeta()["rx_addv"]["PARAM"]["levels"].to(seq[string])
let firstRow = newDataFrame:
PARAM: ["Subjects with at least:"]
let addvSum = concat([firstRow,
rxAddv
.withColumns(col("USUBJID").nUnique.over("TRTA").alias("NTOT"))
.groupBy("TRTA", "PARCAT1", "PARAM", "CRIT1FL")
.agg(
col("AVAL").sum.alias("n"),
(100 * col("AVAL").sum / col("NTOT").mean).alias("pct"))
.sort([col("TRTA").cast(Enum(arms)), col("PARCAT1"),
col("PARAM").cast(Enum(params)), col("CRIT1FL")])
.withColumns(concatStr([col("TRTA"), col("CRIT1FL")], separator = "_").alias("arm_flag"))
.pivot("arm_flag", index = ["PARCAT1", "PARAM"], values = ["n", "pct"])
.fillNull(0)],
how = ConcatMethod.DiagonalRelaxed)
echo tabella(addvSum)
nbText: """
This is the dataset that serves as a starting point for `gt`. We will start by exposing the dataset to `gt` and add our usual left-aligned headers.
"""
tabs """
addv_tbl = GT(addv_sum, rowname_col="PARAM").tab_header(
title="xx.x: Demographic and Baseline Data",
subtitle="xx.x.x: Major Protocol Deviations and Relationship to COVID-19 - ITT Set",
).opt_align_table_header(align="left")
addv_tbl
""", """
addv_tbl <-
addv_sum |>
gt(rowname_col = "PARAM") |>
tab_header(
title = "xx.x: Demographic and Baseline Data",
subtitle = "xx.x.x: Major Protocol Deviations and Relationship to COVID-19 - ITT Set"
) |>
opt_align_table_header(align = "left")
addv_tbl
""":
nbCode:
let addvTbl = tabella(addvSum, rownameCol = "PARAM"):
tabHeader "xx.x: Demographic and Baseline Data",
"xx.x.x: Major Protocol Deviations and Relationship to COVID-19 - ITT Set"
optAlignTableHeader "left"
nbTable addvTbl
nbText: """
In a next step, we would like to create a summary row for all individual PDs to get the overall number of individual PDs, as well as the corresponding percentage. For this, we will first create a row group for individual PDs using `tab_row_group()` applied to all rows where `PARCAT1` is equal to `PROTOCOL DEVIATIONS`. Then, we'll arrange the order of the row groups to list individual PDs after the overall summary. Finally, we can create a summary row, using `summary_rows()` and `gt` sums up all columns with n's and percentages for us (other summary functions are possible, but `fn = 'sum'` does the job for us).
"""
tabs """
# Great Tables has no tab_row_group(): the row groups come from a column of
# the data, so the table is made again with a column `group` (the first rows
# go in a group with an empty label: they cannot be in no group, as gt's NA)
addv_tbl = (
GT(
addv_sum.with_columns(
group=pl.when(pl.col("PARCAT1") == "PROTOCOL DEVIATION")
.then(pl.lit(" "))
.otherwise(pl.lit(""))
),
rowname_col="PARAM",
groupname_col="group",
)
.tab_header(
title="xx.x: Demographic and Baseline Data",
subtitle="xx.x.x: Major Protocol Deviations and Relationship to COVID-19 - ITT Set",
)
.opt_align_table_header(align="left")
.summary_rows(
fns={"Study Procedure Deviations": cs.numeric().sum()},
groups=[" "],
side="top",
)
)
addv_tbl
""", """
addv_tbl <-
addv_tbl |>
tab_row_group(
label = " ",
rows = PARCAT1 == "PROTOCOL DEVIATION"
) |>
row_group_order(groups = c(NA, " ")) |>
summary_rows(
groups = " ",
columns = where(is.numeric),
fns = list(label = "Study Procedure Deviations", fn = "sum"),
side = "top"
)
addv_tbl
""":
nbCode:
let addvGrouped = tabella(addvTbl):
tabRowGroup " ", where(it.PARCAT1 == "PROTOCOL DEVIATION")
summaryRows:
"Study Procedure Deviations": agg.sum
groups = [" "]
side = sTop
nbTable addvGrouped
tabelline """
Rows in no group come first in tabelline, so it needs no `rowGroupOrder` here (gt's
`row_group_order(groups = c(NA, " "))`).
"""
nbText: """
We only kept the column `PARCAT1` to facilitate the generation of the row group. We can hide this column now using `cols_hide()`:
"""
tabs """
addv_tbl = addv_tbl.cols_hide(columns="PARCAT1")
addv_tbl
""", """
addv_tbl <-
addv_tbl |>
cols_hide(columns = "PARCAT1")
addv_tbl
""":
nbCode:
let addvHidden = tabella(addvGrouped):
colsHide "PARCAT1"
nbTable addvHidden
nbText: """
Now that the table has roughly the right shape, we can start to format all numeric columns and merge columns for n's and percentages by intervention group and COVID-19 relationship flag.
"""
tabs """
addv_tbl = (
addv_tbl
.sub_missing(rows=[0], missing_text="")
.fmt_number(columns=cs.starts_with("pct"), decimals=1)
.cols_merge_n_pct(col_n="n_Placebo_Y", col_pct="pct_Placebo_Y")
.cols_merge_n_pct(col_n="n_Placebo_N", col_pct="pct_Placebo_N")
.cols_merge_n_pct(col_n="n_Drug 1_Y", col_pct="pct_Drug 1_Y")
.cols_merge_n_pct(col_n="n_Drug 1_N", col_pct="pct_Drug 1_N")
)
addv_tbl
""", """
addv_tbl <-
addv_tbl |>
sub_missing(
rows = 1,
missing_text = ""
) |>
fmt_number(
columns = starts_with("pct"),
decimals = 1
) |>
cols_merge_n_pct(
col_n = "n_Placebo_Y",
col_pct = "pct_Placebo_Y"
) |>
cols_merge_n_pct(
col_n = "n_Placebo_N",
col_pct = "pct_Placebo_N"
) |>
cols_merge_n_pct(
col_n = "n_Drug 1_Y",
col_pct = "pct_Drug 1_Y"
) |>
cols_merge_n_pct(
col_n = "n_Drug 1_N",
col_pct = "pct_Drug 1_N"
)
addv_tbl
""":
nbCode:
let addvMerged = tabella(addvHidden):
subMissing(rows = 0, missingText = "")
fmtNumber startsWith("pct"), decimals = 1
for arm in arms:
for flag in ["Y", "N"]:
colsMergeNPct "n_" & arm & "_" & flag, "pct_" & arm & "_" & flag
nbTable addvMerged
nbText: """
This looks more like a PD table! We can now modify the column names and create a cascade of column spanners.
"""
tabs """
addv_tbl = (
addv_tbl
.tab_spanner(
label=md("COVID-19 Related"), columns=["n_Placebo_Y", "n_Placebo_N"], id="cov_pla"
)
.tab_spanner(
label=md("COVID-19 Related"), columns=["n_Drug 1_Y", "n_Drug 1_N"], id="cov_dru"
)
.tab_spanner(
label=md("Placebo \n N=90 (100%) \n n (%)"),
columns=["n_Placebo_Y", "n_Placebo_N"],
id="pla",
)
.tab_spanner(
label=md("Drug 1 \n N=90 (100%) \n n (%)"),
columns=["n_Drug 1_Y", "n_Drug 1_N"],
id="dru",
)
.cols_label(
{"n_Placebo_Y": "Yes", "n_Placebo_N": "No", "n_Drug 1_Y": "Yes", "n_Drug 1_N": "No"}
)
.tab_style(
style=style.text(align="center"),
locations=loc.spanner_labels(ids=["cov_pla", "cov_dru", "pla", "dru"]),
)
)
addv_tbl
""", """
addv_tbl <-
addv_tbl |>
tab_spanner(
label = md("COVID-19 Related"),
columns = c("n_Placebo_Y", "n_Placebo_N"),
id = "cov_pla"
) |>
tab_spanner(
label = md("COVID-19 Related"),
columns = c("n_Drug 1_Y", "n_Drug 1_N"),
id = "cov_dru"
) |>
tab_spanner(
label = md("Placebo \n N=90 (100%) \n n (%)"),
columns = c("n_Placebo_Y", "n_Placebo_N")
) |>
tab_spanner(
label = md("Drug 1 \n N=90 (100%) \n n (%)"),
columns = c("n_Drug 1_Y", "n_Drug 1_N")
) |>
cols_label(
.list = list(
"n_Placebo_Y" = "Yes",
"n_Placebo_N" = "No",
"n_Drug 1_Y" = "Yes",
"n_Drug 1_N" = "No"
)
) |>
tab_style(
style = cell_text(align = "center"),
locations = cells_column_spanners(spanners = everything())
)
addv_tbl
""":
nbCode:
let addvSpanned = tabella(addvMerged):
tabSpanner md"COVID-19 Related", cols("n_Placebo_Y", "n_Placebo_N")
tabSpanner md"COVID-19 Related", cols("n_Drug 1_Y", "n_Drug 1_N")
tabSpanner md("Placebo \n N=90 (100%) \n n (%)"), cols("n_Placebo_Y", "n_Placebo_N"), level = 2
tabSpanner md("Drug 1 \n N=90 (100%) \n n (%)"), cols("n_Drug 1_Y", "n_Drug 1_N"), level = 2
colsLabel:
n_Placebo_Y: "Yes"
n_Placebo_N: "No"
"n_Drug 1_Y": "Yes"
"n_Drug 1_N": "No"
tabStyle style.text(align = "center"), loc.spanners("COVID-19 Related", "Placebo \n N=90 (100%) \n n (%)", "Drug 1 \n N=90 (100%) \n n (%)")
nbTable addvSpanned
tabelline """
Two spanners with the same label are told apart by their columns, so tabelline needs no `id`;
a spanner over another one takes `level = 2`, where gt finds the level itself. A spanner gathers
its columns in the order given, Yes before No, as gt's does, and `md` breaks a line at two spaces
and a newline, as Markdown does.
"""
nbText: """
We can now add a footnote, indicating that subjects can have more than one PD during the course of the study. The footnote is added with `tab_footnote()` to the row `At least one major Protocol Deviation`.
"""
tabs """
addv_tbl = addv_tbl.tab_footnote(
footnote="Subjects can have more than one Protocol Deviation throughout the study.",
locations=loc.stub(rows=["At least one major Protocol Deviation"]),
placement="right",
)
addv_tbl
""", """
addv_tbl <-
addv_tbl |>
tab_footnote(
footnote = "Subjects can have more than one Protocol Deviation throughout the study.",
locations = cells_stub(rows = c("At least one major Protocol Deviation")),
placement = "right"
)
addv_tbl
""":
nbCode:
let addvFootnoted = tabella(addvSpanned):
tabFootnote "Subjects can have more than one Protocol Deviation throughout the study.",
loc.stub(rows = where(it.PARAM == "At least one major Protocol Deviation"))
nbTable addvFootnoted
nbText: """
Finally, we can style the table, indenting the individual PDs under `Study Procedure Deviations`, left-aligning the first column and centering all other columns. Note that for the indentation, we can still use the hidden column `PARCAT1` to identify individual PDs.
"""
tabs """
addv_tbl.cols_align(align="center", columns=cs.starts_with("n_")).cols_align(
align="left", columns=["PARCAT1", "PARAM"]
).tab_stub_indent(rows=pl.col("PARCAT1") == "PROTOCOL DEVIATION", indent=5)
""", """
addv_tbl |>
cols_align(
align = "center",
columns = 3:6
) |>
cols_align(
align = "left",
columns = 1:2
) |>
tab_stub_indent(
rows = PARCAT1 == "PROTOCOL DEVIATION",
indent = 5
)
""":
nbCode:
let addvFinal = tabella(addvFootnoted):
colsAlign alCenter, startsWith("n_")
colsAlign alLeft, cols("PARCAT1", "PARAM")
tabStubIndent where(it.PARCAT1 == "PROTOCOL DEVIATION"), 5
nbTable addvFinal
bookSave()
On GitHub: book/vignettes/case_study_clinical.nim