---
title: "Step 5: Visualise temporal symmetry"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Step 5: Visualise temporal symmetry}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  warning = FALSE,
  message = FALSE,
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 5,
  eval = Sys.getenv("$RUNNER_OS") != "macOS"
)
```

# Introduction
In this vignette we will explore the functionality and arguments of a set of functions that will help us to understand and visualise the temporal symmetry results (produced **Step 4: Obtain aggregated data on temporal symmetry**). In particular, we will delve into the following function:

- `plotTemporalSymmetry()`: to plot the temporal symmetry.

This function builds-up on previous functions, such as `sequenceCohort()` and `summariseTemporalSymmetry()` function.

```{r message= FALSE, warning=FALSE, include=FALSE}
# Load libraries
library(dplyr)
library(CohortSymmetry)
library(CohortConstructor)
library(omock)

# Get mock data from omock
cdm <- mockCdmFromDataset(datasetName = "GiBleed")

# Generate index-marker cohorts
aspirin_codelist <- CodelistGenerator::getDrugIngredientCodes(
  cdm = cdm,
  name = "aspirin",
  nameStyle = "{concept_name}"
)

cdm[["aspirin"]] <- CohortConstructor::conceptCohort(
  cdm = cdm,
  conceptSet = aspirin_codelist,
  exit = "event_end_date",
  name = "aspirin")


amoxicillin_codelist <- CodelistGenerator::getDrugIngredientCodes(
  cdm = cdm,
  name = "amoxicillin",
  nameStyle = "{concept_name}"
)

cdm[["amoxicillin"]] <- CohortConstructor::conceptCohort(
  cdm = cdm,
  conceptSet = amoxicillin_codelist,
  exit = "event_end_date",
  name = "amoxicillin")

# Generate a sequence cohort
cdm$intersect <- sequenceCohort(
  cdm = cdm,
  indexCohortTable = "aspirin",
  markerCohortTable = "amoxicillin",
  name = "intersect")

```

Let's regather the output from `summariseTemporalSymmetry()`

```{r message= FALSE, warning=FALSE}
temporal_symmetry <- summariseTemporalSymmetry(cohort = cdm$intersect)
```

With this established, much like `summariseSequenceRatios()`, the object `temporal_symmetry` could then be fed into `tableTemporalSymmetry()` or `plotTemporalSymmetry()` to visualise the results:

```{r message= FALSE, warning=FALSE}
tableTemporalSymmetry(result = temporal_symmetry)
```

<center>

```{r message= FALSE, warning=FALSE}
plotTemporalSymmetry(result = temporal_symmetry)
```
</center>

Note that the $x$ axis is the time, which we recall to be the initiation of the marker minus the initiation of the index. The unit of the time difference here is 30 days as this is the default from `summarisTemporalSymmetry()`. 


**This is the end of the vignette, please cite our paper and let us know of any bugs or improvements we can make - have fun with the package!**
