---
title: "Competing risk survival"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Competing risk survival}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE, warning = FALSE, message = FALSE,
  out.width = "100%",
  comment = "#>"
)
```


## Set up

Let's first load the packages required.

```{r}
library(CDMConnector)
library(CohortSurvival)
library(dplyr)
library(ggplot2)
```

We'll create a cdm reference to use our example MGUS2 survival dataset. In practice you would use the CDMConnector package to connect to your data mapped to the OMOP CDM.

```{r}
cdm <- CohortSurvival::mockMGUS2cdm()
```

We will proceed as we did with the single event survival, but this time we are considering an event of interest (progression of disease) with a competing risk (mortality).

We would typically need to define study cohorts ourselves, but in the case of our example data we already have these cohorts available. You can see for our diagnosis cohort we also have a number of additional features recorded for individuals which we'll use for stratification.

```{r}
cdm$mgus_diagnosis %>% 
  glimpse()

cdm$death_cohort %>% 
  glimpse()

cdm$progression %>%
  glimpse()
```

## Estimating survival with competing risk

The package allows to estimate survival of both an outcome and competing risk outcome. We can then stratify, see information on events, summarise the estimates and check the contributing participants in the same way we did for the single event survival analysis.

```{r, fig.width=5}
MGUS_death_prog <- estimateCompetingRiskSurvival(cdm,
  targetCohortTable = "mgus_diagnosis",
  outcomeCohortTable = "progression",
  competingOutcomeCohortTable = "death_cohort"
) 

MGUS_death_prog %>% 
  asSurvivalResult() %>% 
  glimpse()
```
 
As we can see above our results have been outputted in long format. We can plot these results like so.

```{r, out.width = "75%"}
plotSurvival(MGUS_death_prog, cumulativeFailure = TRUE,
             colour = "variable_level") + 
  theme(legend.position = "top")
```

Our returned results also have attributes containing information that summarises survival. 

```{r}
tableSurvival(MGUS_death_prog) 
```


## With stratification

To estimate survival for particular strata of interest we need these features to have been added to the target cohort table. Once we have them defined, and as seen above we already have a number of example characteristics added to our diagnosis cohort, we can add stratifications like so.

```{r}
MGUS_death_prog <-  estimateCompetingRiskSurvival(cdm,
  targetCohortTable = "mgus_diagnosis",
  outcomeCohortTable = "progression",
  competingOutcomeCohortTable = "death_cohort",
  strata = list(c("sex"))
)

```

As we can see as well as results for each strata, we'll always also have overall results returned. We can filter the output table to plot only the results for the different strata levels. We can also ask for the cumulative failure probability to be plotted instead of the survival probability.

```{r, fig.height=6, fig.width=8}
plotSurvival(MGUS_death_prog %>% 
               dplyr::filter(strata_name != "Overall"), 
             facet = "strata_level",
             colour = "variable_level",
             cumulativeFailure = TRUE)
```

And we also now have summary statistics for each of the strata as well as overall.

```{r}
tableSurvival(MGUS_death_prog)
```

## Disconnect from the cdm database connection

```{r}
cdm_disconnect(cdm)
```
