---
title: "Baseline Characteristics Table"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Baseline Characteristics Table}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r}
library(metalite)
library(metalite.sl)
```

# Create Baseline Characteristic Table

The baseline characteristic analysis aims to provide tables to summarize details of participants.
The development of baseline characteristic analysis involves functions:

- `prepare_base_char`: prepare analysis raw datasets.
- `format_base_char`: prepare analysis outdata with proper format.
- `rtf_base_char`: transfer output dataset to RTF table.

## Build a metadata

```{r}
adsl <- r2rtf::r2rtf_adsl
adsl$TRTA <- adsl$TRT01A
adsl$TRTA <- factor(
  adsl$TRTA,
  levels = c("Placebo", "Xanomeline Low Dose", "Xanomeline High Dose"),
  labels = c("Placebo", "Low Dose", "High Dose")
)
```

```{r}
meta <- meta_sl(
  dataset_population = adsl,
  population_term = "apat",
  parameter_term = "age;gender;race",
  parameter_var = "AGE^AGEGR1;SEX;RACE",
  treatment_group = "TRTA"
)
```

<details>
<summary>Click to show the output</summary>
```{r}
meta
```
</details>

## Analysis preparation

The input of the function `prepare_base_char()` is a `meta` object created by the metalite package.

```{r}
outdata <- meta |>
  prepare_base_char(parameter = "age;gender;race")

outdata
```


<details>
<summary>Click to show the output</summary>
```{r}
outdata$n
outdata$char_n
outdata$char_var
outdata$char_prop
```
</details>


## Format the numbers

`format_base_char` to prepare analysis dataset before generate RTF output

```{r}
outdata <-
  outdata |> format_base_char(
    display_col = c("n", "prop", "total"),
    digits_prop = 2
  )
```

<details>
<summary>Click to show the output</summary>
```{r}
outdata$tbl
```
</details>

## Output as RTF

`rtf_base_char` to generate RTF output

```{r}
outdata |> rtf_base_char(
  source = "Source: [CDISCpilot: adam-adsl]",
  path_outdata = tempfile(fileext = ".Rdata"),
  path_outtable = "outtable/base0char.rtf"
)
```

```{r, out.width = "100%", out.height = "400px", echo = FALSE, fig.align = "center"}
knitr::include_graphics("pdf/base0char.pdf")
```

# Create Baseline Characteristic Table for Subgroup Analysis 

The baseline characteristic subgroup analysis aims to provide tables to summarize details of participants by subgroup.
The development of baseline characteristic subgroup analysis involves functions:

- `prepare_base_char_subgroup()`: prepare analysis raw datasets.
- `format_base_char_subgroup()`: prepare analysis outdata with proper format.
- `rtf_base_char_subgroup()`: transfer output dataset to RTF table.

## Build a metadata

```{r}
adsl <- r2rtf::r2rtf_adsl
adsl$TRTA <- adsl$TRT01A
adsl$TRTA <- factor(
  adsl$TRTA,
  levels = c("Placebo", "Xanomeline Low Dose", "Xanomeline High Dose"),
  labels = c("Placebo", "Low Dose", "High Dose")
)
```

```{r}
plan <- plan(
  analysis = "base_char_subgroup",
  population = "apat",
  observation = "apat",
  parameter = "age;gender;race"
)
```

```{r}
meta <- meta_adam(
  population = adsl,
  observation = adsl
) |>
  define_plan(plan = plan) |>
  define_population(
    name = "apat",
    group = "TRTA",
    subset = quote(SAFFL == "Y"),
    var = c("USUBJID", "TRTA", "SAFFL", "AGEGR1", "SEX", "RACE")
  ) |>
  define_parameter(
    name = "age",
    var = "AGE",
    label = "Age (years)",
    vargroup = "AGEGR1"
  ) |>
  define_parameter(
    name = "race",
    var = "RACE",
    label = "Race"
  ) |>
  define_analysis(
    name = "base_char_subgroup",
    title = "Participant by Age Category and Sex",
    label = "baseline characteristic sub group table"
  ) |>
  meta_build()
```

<details>
<summary>Click to show the output</summary>
```{r}
meta
```
</details>

## Analysis preparation

The input of the function `prepare_base_char_subgroup()` is a `meta` object created by the metalite package.

The output of the function is an `outdata` object containing a list of analysis
raw datasets. Key arguments are:

- `subgroup_var`: a character value of subgroup variable name in observation data saved in meta$data_observation.
- `subgroup_header`: a character vector for column header hierarchy. The first element will be the first level header and the second element will be second level header.

```{r}
outdata <- prepare_base_char_subgroup(
  meta,
  population = "apat",
  parameter = "age;race",
  subgroup_var = "TRTA",
  subgroup_header = c("SEX", "TRTA"),
  display_subgroup_total = TRUE
)
```

<details>
<summary>Click to show the output</summary>
The output dataset contains commonly used statistics within each `subgroup_var`.

```{r}
outdata$out_all$`Placebo`
outdata$out_all$`High Dose`
outdata$out_all$`Low Dose`
```

The information about subgroup saved with `outdata$group` and `outdata$subgroup`.

```{r}
outdata$group
outdata$subgroup
```

`n_pop`: participants in population within each `subgroup_var`.

```{r}
outdata$out_all$`Placebo`$n
outdata$out_all$`High Dose`$n
outdata$out_all$`Low Dose`$n
```
</details>

## Format output

`format_base_char_subgroup` to prepare analysis dataset before generate RTF output

```{r}
outdata <- format_base_char_subgroup(outdata)
```

<details>
<summary>Click to show the output</summary>
```{r}
outdata$tbl
```
</details>

## Output as RTF

`rtf_base_char_subgroup` to generate RTF output

```{r, warning=FALSE}
outdata |>
  rtf_base_char_subgroup(
    source = "Source:  [CDISCpilot: adam-adsl]",
    path_outdata = tempfile(fileext = ".Rdata"),
    path_outtable = "outtable/base0charsubgroup.rtf"
  )
```


```{r, out.width = "100%", out.height = "400px", echo = FALSE, fig.align = "center"}
knitr::include_graphics("pdf/base0charsubgroup.pdf")
```
