Introduction to CohortSymmetry

CohortSymmetry provides tools to perform Sequence Symmetry Analysis (SSA). Before using the package, it is highly recommended that this method is tested beforehand against well-known positive and negative controls. The details of SSA and the relevant controls could be found using Pratt et al (2015).

The functions you will interact with are:

  1. generateSequenceCohortSet(): this function will create a cohort with individuals present in both (the index and the marker) cohorts.

  2. summariseSequenceRatios(): this function will calculate sequence ratios.

  3. tableSequenceRatios() and plotSequenceRatios(): these functions will help us to visualise the sequence ratio results.

  4. summariseTemporalSymmetry(): this function will produce aggregated results based on the time difference between two cohort start dates.

  5. plotTemporalSymmetry(): this function will help us to visualise the results from summariseTemporalSymmetry().

Below, you will find an example analysis that offers a brief and comprehensive overview of the package’s functionalities. More context and further examples for each of these functions are provided in later vignettes.

First, let’s load the relevant libraries.

library(CDMConnector)
library(dplyr)
library(DBI)
library(omock)
library(CohortSymmetry)
library(duckdb)

The CohortSymmetry package works with data mapped to the OMOP CDM. Hence, the initial step involves connecting to a database. As an example, we will be using Omock package to generate a mock database with two mock cohorts: the index_cohort and the marker_cohort.

cdm <- emptyCdmReference(cdmName = "mock") |>
  mockPerson(nPerson = 100) |>
  mockObservationPeriod() |>
  mockCohort(
    name = "index_cohort",
    numberCohorts = 1,
    cohortName = c("index_cohort"),
    seed = 1,
  ) |>
  mockCohort(
    name = "marker_cohort",
    numberCohorts = 1,
    cohortName = c("marker_cohort"), 
    seed = 2
  )

con <- dbConnect(duckdb::duckdb())
cdm <- copyCdmTo(con = con, cdm = cdm, schema = "main", overwrite = T)

Once we have established a connection to the database, we can use the generateSequenceCohortSet() function to find the intersection of the two cohorts. This function will provide us with the individuals who appear in both cohorts, which will be named intersect - another cohort in the cdm reference.

cdm <- generateSequenceCohortSet(
  cdm = cdm,
  indexTable = "index_cohort",
  markerTable = "marker_cohort",
  name = "intersect",
  combinationWindow = c(0, Inf)
)

See below that the generated cohort follows the format of an OMOP CDM cohort with the addition of two extra columns: index_date and marker_date. These columns correspond to the cohort_start_date in the index_cohort and the marker_cohort, respectively.

cdm$intersect |> 
  dplyr::glimpse()
#> Rows: ??
#> Columns: 6
#> Database: DuckDB v1.1.3-dev165 [unknown@Linux 6.5.0-1025-azure:R 4.4.2/:memory:]
#> $ cohort_definition_id <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
#> $ subject_id           <int> 35, 37, 54, 64, 48, 21, 25, 26, 23, 33, 40, 43, 4…
#> $ cohort_start_date    <date> 1992-03-19, 2011-04-20, 2004-11-07, 1994-06-20, …
#> $ cohort_end_date      <date> 1993-07-10, 2011-06-05, 2005-03-08, 1995-07-20, …
#> $ index_date           <date> 1992-03-19, 2011-06-05, 2005-03-08, 1994-06-20, …
#> $ marker_date          <date> 1993-07-10, 2011-04-20, 2004-11-07, 1995-07-20, …

Once we have the intersect cohort, you are able to explore the temporal symmetry by using summariseTemporalSymmetry, tableTemporalSymmetry, and plotTemporalSymmetry():

temporal_symmetry <- summariseTemporalSymmetry(
  cohort = cdm$intersect, 
  timescale = "year")

The result can be viewed using table and plot functions.

tableTemporalSymmetry(result = temporal_symmetry)
plotTemporalSymmetry(result = temporal_symmetry)

Next, we will use the summariseSequenceRatios() function to get the crude sequence ratios, adjusted sequence ratios, and the corresponding confidence intervals.

sequence_ratio <- summariseSequenceRatios(cohort = cdm$intersect)

Finally, we can visualise the results using tableSequenceRatios():

tableSequenceRatios(result = sequence_ratio)

Or create a plot with the adjusted sequence ratios:

plotSequenceRatios(result = sequence_ratio)

As a diagram

Diagrammatically, the work flow using CohortSymmetry resembles the following flow chat: