---
title: "Adding concept intersections"
output: 
  html_document:
    pandoc_args: [
      "--number-offset=1,0"
      ]
    number_sections: yes
    toc: yes
vignette: >
  %\VignetteIndexEntry{concept-intersect}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
editor_options: 
  chunk_output_type: console
---

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

## Introduction

Concept sets play an important role when working with data in the format of the OMOP CDM. They can be used to create cohorts after which, as we've seen in the previous vignette, we can identify intersections between the cohorts. PatientProfiles adds another option for working with concept sets which is use them for adding associated variables directly without first having to create a cohort.

It is important to note, and is explained more below, that results may differ when generating a cohort and then identifying intersections between two cohorts compared to working directly with concept sets. The creation of cohorts will involve the collapsing of overlapping records as well as imposing certain requirements such as only including records that were observed during an individuals observation period. When adding variables based on concept sets we will be working directly with record-level data in the OMOP CDM clinical tables.

## Adding variables from concept sets

For this vignette we'll use the Eunomia synthetic dataset. First lets create our cohort of interest, individuals with an ankle sprain.

```{r, eval = TRUE, echo = FALSE}
library(CDMConnector)
if (Sys.getenv("EUNOMIA_DATA_FOLDER") == "") Sys.setenv("EUNOMIA_DATA_FOLDER" = tempdir())
if (!dir.exists(Sys.getenv("EUNOMIA_DATA_FOLDER"))) dir.create(Sys.getenv("EUNOMIA_DATA_FOLDER"))
if (!eunomia_is_available()) downloadEunomiaData()
```

```{r}
library(CDMConnector)
library(CodelistGenerator)
library(PatientProfiles)
library(dplyr)
library(ggplot2)

con <- DBI::dbConnect(duckdb::duckdb(),
                       dbdir = CDMConnector::eunomia_dir())
cdm <- CDMConnector::cdm_from_con(con,
                                   cdm_schem = "main",
                                   write_schema = "main")

cdm <- generateConceptCohortSet(
  cdm = cdm,
  name = "ankle_sprain",
  conceptSet = list("ankle_sprain" = 81151),
  end = "event_end_date",
  limit = "all",
  overwrite = TRUE
)

cdm$ankle_sprain
```

Now let's say we're interested in summarising use of acetaminophen among our ankle sprain cohort. We can start by identifying the relevant concepts.

```{r}
acetaminophen_cs <- getDrugIngredientCodes(cdm = cdm, 
                        name = c("acetaminophen"))

acetaminophen_cs
```

Once we have our codes for acetaminophen we can create variables based on these. As with cohort intersections, PatientProfiles provides four types of functions for concept intersections.

First, we can add a binary flag variable indicating whether an individual had a record of acetaminophen on the day of their ankle sprain or up to 30 days afterwards.

```{r}
cdm$ankle_sprain %>%
  addConceptIntersectFlag(conceptSet = acetaminophen_cs, 
                          indexDate = "cohort_start_date", 
                          window = c(0, 30)) %>% 
  dplyr::glimpse()
```

Second, we can count the number of records of acetaminophen in this same window for each individual.

```{r}
cdm$ankle_sprain %>%
  addConceptIntersectCount(conceptSet = acetaminophen_cs, 
                          indexDate = "cohort_start_date", 
                          window = c(0, 30)) %>% 
  dplyr::glimpse()
```

Third, we could identify the first start date of acetaminophen in this window.

```{r}
cdm$ankle_sprain %>%
  addConceptIntersectDate(conceptSet = acetaminophen_cs, 
                          indexDate = "cohort_start_date", 
                          window = c(0, 30), 
                          order = "first") %>% 
  dplyr::glimpse()
```

Or fourth, we can get the number of days to the start date of acetaminophen in the window.

```{r}
cdm$ankle_sprain %>%
  addConceptIntersectDays(conceptSet = acetaminophen_cs, 
                          indexDate = "cohort_start_date", 
                          window = c(0, 30), 
                          order = "first") %>% 
  dplyr::glimpse()
```

## Adding multiple concept based variables

We can add more than one variable at a time when using these functions. For example, we might want to add variables for multiple time windows.

```{r}
cdm$ankle_sprain %>%
  addConceptIntersectFlag(conceptSet = acetaminophen_cs, 
                          indexDate = "cohort_start_date", 
                          window = list(c(-Inf, -1),
                                        c(0, 0),
                                        c(1, Inf))) %>% 
  dplyr::glimpse()
```

Or we might want to get variables for multiple drug ingredients of interest.

```{r}
meds_cs <- getDrugIngredientCodes(cdm = cdm, 
                                  name = c("acetaminophen",
                                           "amoxicillin",
                                           "aspirin",
                                           "heparin",
                                           "morphine",
                                           "oxycodone",
                                           "warfarin"))

cdm$ankle_sprain %>%
  addConceptIntersectFlag(conceptSet = meds_cs, 
                          indexDate = "cohort_start_date", 
                          window = list(c(-Inf, -1),
                                        c(0, 0))) %>% 
  dplyr::glimpse()
```

## Cohort-based versus concept-based intersections

In the previous vignette we saw that we can add an intersection variable using a cohort we have created. Meanwhile in this vignette we see that we can instead create an intersection variable using a concept set directly. It is important to note that under some circumstances these two approaches can lead to different results.

When creating a cohort we combine overlapping records, as cohort entries cannot overlap. Thus when adding an intersection count, `addCohortIntersectCount()` will return a count of cohort entries in the window of interest while `addConceptIntersectCount()` will return a count of records withing the window. We can see the impact for acetaminophen for our example data below, where we have slightly more records than cohort entries.

```{r, fig.width=7}
acetaminophen_cs <- getDrugIngredientCodes(cdm = cdm, 
                                  name = c("acetaminophen"))

cdm <- generateConceptCohortSet(
  cdm = cdm,
  name = "acetaminophen",
  conceptSet = acetaminophen_cs,
  end = "event_end_date",
  limit = "all"
)

dplyr::bind_rows(
cdm$ankle_sprain |> 
  addCohortIntersectCount(targetCohortTable = "acetaminophen",
                         window = c(-Inf, Inf)) |> 
  dplyr::group_by(acetaminophen_minf_to_inf)  |> 
  dplyr::tally() |> 
  dplyr::collect() |> 
  dplyr::arrange(desc(acetaminophen_minf_to_inf)) |> 
  dplyr::mutate(type = "cohort"),
cdm$ankle_sprain |> 
  addConceptIntersectCount(conceptSet = acetaminophen_cs, 
                         window = c(-Inf, Inf)) |> 
  dplyr::group_by(acetaminophen_minf_to_inf) |> 
  dplyr::tally() |> 
  dplyr::collect() |> 
  dplyr::arrange(desc(acetaminophen_minf_to_inf)) |> 
  dplyr::mutate(type = "concept_set")) |> 
  ggplot() +
  geom_col(aes(acetaminophen_minf_to_inf, n, fill = type), 
           position = "dodge") +
  theme_bw()+
  theme(legend.title = element_blank(), 
        legend.position = "top")
```

Additional differences between cohort and concept set intersections may also result from cohort table rules. For example, cohort tables will typically omit any records that occur outside an individual´s observation time (as defined in the observation period window). Such records, however, would not be excluded when adding a concept based intersection.
