---
title: "Step 2. Obtain the sequence ratios"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Step 2. Obtain the sequence ratios}
  %\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 `summariseSequenceRatios()` function, which is used to generate the sequence ratios of the SSA. As this function uses the output of `sequenceCohort()` function (explained in detail in the vignette: **Step 1. Generate a sequence cohort**), we will pick up the explanation from where we left off in the previous vignette. However, in this case we are now using the default settings of the package.

```{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")

```

Recall that in the previous vignette: Step 1. Generate a sequence cohort, we've generated `cdm$aspirin` and `cdm$amoxicillin` before and using them we could generate `cdm$intersect` like so:

```{r message= FALSE, warning=FALSE}
# Generate a sequence cohort
cdm$intersect <- sequenceCohort(
  cdm = cdm,
  indexCohortTable = "aspirin",
  markerCohortTable = "amoxicillin",
  name = "intersect")
```


# Obtain sequence ratios
One can obtain the crude and adjusted sequence ratios (with its corresponding confidence intervals) using `summariseSequenceRatios()` function:
```{r message = FALSE, warning = FALSE}
summariseSequenceRatios(
  cohort = cdm$intersect
) |> 
  dplyr::glimpse()
```

The obtained output has a summarised result format. In the later vignette (**Step 3. Visualise results**) we will explore how to visualise the results in a more intuitive way. 
