---
title: "Survival Analysis with visR using CDISC ADaM Time-To-Event Analysis Dataset (ADTTE)"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Survival Analysis with visR using CDISC ADaM Time-To-Event Analysis Dataset (ADTTE)}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

## Introduction

This tutorial illustrates how a standard time-to-event analysis can be done very efficiently when the data set adheres to the [CDISC ADaM standard](https://www.cdisc.org/standards/foundational/adam/adam-basic-data-structure-bds-time-event-tte-analyses-v1-0). A more detailed time-to-event analysis with a more broad overview of visR's functionality is presented in another vignette.


```{r imports, echo=TRUE, warning=FALSE, message=FALSE}
library(ggplot2)
library(visR)
```

## Global Document Setup
```{r globalSetup}
# Metadata Title
DATASET <- paste0("Analyis Data Time-To-Event (ADTTE)")

# Save original options()
old <- options()  

# Global formatting options
options(digits = 3)

# Global ggplot settings
theme_set(theme_bw())

# Global table settings 
options(DT.options = list(pageLength = 10, 
                          language = list(search = 'Filter:'), 
                          scrollX = TRUE))

# load ADTTE from CDISC pilot 
data(adtte)

# Restore original options()
options(old)

```


## Time-to-event analysis

visR includes a wrapper function to easily display summary tables (e.g. `tableone`)

```{r table1_get_default}
# Display a summary table (e.g. tableone)
visR::tableone(adtte[,c("TRTP", "AGE")],
         title = "Demographic summary" , datasource = DATASET)
```

The wrapper function to estimate a Kaplan-Meier curve is compatible with `%>%` and purrr::map functions without losing traceability of the dataset name inside the call of the object. If a data set adheres to the CDISC ADaM standards, only a stratifier needs to be specified.

```{r km_est}
# Estimate a survival object
survfit_object <-  adtte %>%
  visR::estimate_KM(data = ., strata = "TRTP")
survfit_object
```

Given a survival object, visR includes several functions to quickly extract additional information from the survival object (e.g. test statistics and p-values) and a general function to display a table (`render`).

```{r km_tab_options_1}
# Display test statistics associated with the survival estimate

visR::render(survfit_object %>% get_pvalue(), title = "P-values", datasource = DATASET)

```

A survival object can be plotted using the visR function `visr`. Additional information like confidence intervals and a risktable can be added to the plot.

```{r km_plot_1, fig.align='center', fig.width= 6, fig.height=4}
# Create and display a Kaplan-Meier from the survival object and add a risktable
visr(survfit_object) %>% 
  visR::add_CI() %>%
  visR::add_risktable()
```
