---
title: LBT09
subtitle: Liver Laboratory Tests -- Patients with Elevated Post-Baseline AST or ALT Levels
---
------------------------------------------------------------------------
{{< include ../../_utils/envir_hook.qmd >}}
```{r setup, echo = FALSE, warning = FALSE, message = FALSE}
library(dplyr)
library(tern)
adhy <- random.cdisc.data::cadhy
# Ensure character variables are converted to factors and empty strings and NAs are explicit missing levels.
adhy_liver <- df_explicit_na(adhy)
# Define values of interest in PARAMCD variable.
paramcd_tbili_alt <- c("BLAL", "BGAL", "BA2AL", "BA5AL")
paramcd_tbili_ast <- c("BLAS", "BGAS", "BA2AS", "BA5AS")
# Select LBT09 parameters.
adhy_liver <- adhy_liver %>%
filter(
SAFFL == "Y",
AVISIT %in% c("BASELINE", "POST-BASELINE"),
PARAMCD %in% c(paramcd_tbili_alt, paramcd_tbili_ast)
)
# Let's be explicit about factor levels for AVISIT and PARAMCD.
adhy_liver <- adhy_liver %>%
mutate(
AVISIT = factor(AVISIT, levels = c("BASELINE", "POST-BASELINE")),
PARAMCD = factor(PARAMCD, levels = c(paramcd_tbili_alt, paramcd_tbili_ast))
)
# Create indicator and category variables.
adhy_liver <- adhy_liver %>%
mutate(
# Create TBILI_CAT categories variable - this is needed so we get the right nesting in the table.
TBILI_CAT = factor(
case_when(
PARAMCD %in% c(paramcd_tbili_alt[1], paramcd_tbili_ast[1]) ~ "Total Bilirubin <= 2xULN",
PARAMCD %in% c(paramcd_tbili_alt[2], paramcd_tbili_ast[2]) ~ "Total Bilirubin > 2xULN",
PARAMCD %in% c(paramcd_tbili_alt[3], paramcd_tbili_ast[3]) ~
"Total Bilirubin > 2xULN and Alkaline Phosphatase <= 2xULN",
PARAMCD %in% c(paramcd_tbili_alt[4], paramcd_tbili_ast[4]) ~
"Total Bilirubin > 2xULN and Alkaline Phosphatase <= 5xULN"
),
levels = c(
"Total Bilirubin <= 2xULN",
"Total Bilirubin > 2xULN",
"Total Bilirubin > 2xULN and Alkaline Phosphatase <= 2xULN",
"Total Bilirubin > 2xULN and Alkaline Phosphatase <= 5xULN"
)
),
# Create ALTAST_CAT categories variable
# this will be the labels for different ALT/AST categories displayed in the table.
ALTAST_CAT = factor(
case_when(
PARAMCD %in% paramcd_tbili_alt & AVALC == ">3-5ULN" ~ "ALT >3 - <= 5xULN",
PARAMCD %in% paramcd_tbili_alt & AVALC == ">5-10ULN" ~ "ALT >5 - <= 10xULN",
PARAMCD %in% paramcd_tbili_alt & AVALC == ">10-20ULN" ~ "ALT >10 - <= 20xULN",
PARAMCD %in% paramcd_tbili_alt & AVALC == ">20ULN" ~ "ALT > 20xULN",
PARAMCD %in% paramcd_tbili_ast & AVALC == ">3-5ULN" ~ "AST >3 - <= 5xULN",
PARAMCD %in% paramcd_tbili_ast & AVALC == ">5-10ULN" ~ "AST >5 - <= 10xULN",
PARAMCD %in% paramcd_tbili_ast & AVALC == ">10-20ULN" ~ "AST >10 - <= 20xULN",
PARAMCD %in% paramcd_tbili_ast & AVALC == ">20ULN" ~ "AST > 20xULN",
TRUE ~ "Criteria not met"
),
levels = c(
"ALT >3 - <= 5xULN", "ALT >5 - <= 10xULN", "ALT >10 - <= 20xULN",
"20" = "ALT > 20xULN",
"AST >3 - <= 5xULN", "AST >5 - <= 10xULN", "AST >10 - <= 20xULN", "AST > 20xULN",
"Criteria not met"
)
),
ALTAST_ind = factor(
case_when(
PARAMCD %in% paramcd_tbili_alt ~ "ALT",
PARAMCD %in% paramcd_tbili_ast ~ "AST"
),
levels = c("ALT", "AST")
)
)
map <- data.frame(
ALTAST_ind = c(rep("ALT", 5), rep("AST", 5)),
ALTAST_CAT = c(
"ALT >3 - <= 5xULN", "ALT >5 - <= 10xULN", "ALT >10 - <= 20xULN",
"20" = "ALT > 20xULN", "Criteria not met",
"AST >3 - <= 5xULN", "AST >5 - <= 10xULN", "AST >10 - <= 20xULN", "AST > 20xULN", "Criteria not met"
),
stringsAsFactors = FALSE
)
```
```{r include = FALSE}
webr_code_labels <- c("setup")
```
{{< include ../../_utils/webr_no_include.qmd >}}
## Output
::::: panel-tabset
## Standard Table
::: {.panel-tabset .nav-justified group="webr"}
## {{< fa regular file-lines sm fw >}} Preview
```{r variant1, test = list(result_v1 = "result")}
result <- basic_table() %>%
split_cols_by("ARM") %>%
split_cols_by("AVISIT") %>%
split_rows_by("TBILI_CAT") %>%
# below split helps us get the right denominator between ALT/AST but it can be hidden
split_rows_by("ALTAST_ind", split_fun = trim_levels_to_map(map), child_labels = "hidden") %>%
count_occurrences(
vars = "ALTAST_CAT",
.stats = "fraction",
denom = "n",
drop = FALSE
) %>%
append_topleft("Liver Laboratory Test Criterion") %>%
build_table(df = adhy_liver)
# trim away rows with criteria not met
criteria_fun <- function(tr) {
row_label <- obj_label(tr)
ifelse(row_label == "Criteria not met", TRUE, FALSE)
}
result <- result %>% trim_rows(criteria = criteria_fun)
result
```
```{r include = FALSE}
webr_code_labels <- c("variant1")
```
{{< include ../../_utils/webr.qmd >}}
:::
## Table Selecting Sections
::: {.panel-tabset .nav-justified group="webr"}
## {{< fa regular file-lines sm fw >}} Preview
```{r variant2, test = list(result_v2 = "result")}
result <- basic_table() %>%
split_cols_by("ARM") %>%
split_cols_by("AVISIT") %>%
split_rows_by(
"TBILI_CAT",
split_fun = remove_split_levels("Total Bilirubin > 2xULN and Alkaline Phosphatase <= 2xULN")
) %>%
# below split helps us get the right denominator between ALT/AST but it can be hidden
split_rows_by(
"ALTAST_ind",
split_fun = trim_levels_to_map(map), child_labels = "hidden"
) %>%
count_occurrences(
vars = "ALTAST_CAT",
.stats = "fraction",
denom = "n",
drop = FALSE
) %>%
append_topleft("Liver Laboratory Test Criterion") %>%
build_table(df = adhy_liver)
# trim away rows with criteria not met
criteria_fun <- function(tr) {
row_label <- obj_label(tr)
ifelse(row_label == "Criteria not met", TRUE, FALSE)
}
result <- result %>% trim_rows(criteria = criteria_fun)
result
```
```{r include = FALSE}
webr_code_labels <- c("variant2")
```
{{< include ../../_utils/webr.qmd >}}
:::
## Data Setup
```{r setup}
#| code-fold: show
```
:::::
{{< include ../../_utils/save_results.qmd >}}
{{< include ../../repro.qmd >}}