---
title: "Introduction to IncidencePrevalence"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{a01_Introduction_to_IncidencePrevalence}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

To do a study of incidence and prevalence, the analytics functions from this package that you would interact with are

1) `generateDenominatorCohortSet()` and `generateTargetDenominatorCohortSet()` - these function will identify a set of denominator populations from the database population as a whole, the former, or based on individuals in a target cohort, the latter

2) `estimatePointPrevalence()` and `estimatePeriodPrevalence()` - these function will estimate point and period prevalence for outcomes among denominator populations

3) `estimateIncidence()` - this function will estimate incidence rates for outcomes among denominator populations

Below, we show an example analysis to provide an broad overview of how this functionality provided by the IncidencePrevalence package can be used. More context and further examples for each of these functions are provided in later vignettes.

First, let's load relevant libraries.

```{r, message= FALSE, warning=FALSE}
library(CDMConnector)
library(IncidencePrevalence)
library(dplyr)
library(tidyr)
library(ggplot2)
```

The IncidencePrevalence package works with data mapped to the OMOP CDM and we will first need to connect to a database, after which we can use the CDMConnector package to represent our mapped data as a cdm reference. For this example though we´ll use a synthetic cdm reference containing 50,000 hypothetical patients which we create using the `mockIncidencePrevalence()` function.

```{r, message= FALSE, warning=FALSE}
cdm <- mockIncidencePrevalence(
  sampleSize = 50000,
  outPre = 0.2
)
```

This example data already includes an outcome cohort. 

```{r eval=FALSE}
cdm$outcome %>%
  glimpse()
```

Once we have a cdm reference, we can use the `generateDenominatorCohortSet()` to identify a denominator cohort to use later when calculating incidence and prevalence. In this case we identify three denominator cohorts one with males, one with females, and one with both males and females included. For each of these cohorts only those aged between 18 and 65 from 2008 to 2012, and who had 365 days of prior history available are included.

```{r, message= FALSE, warning=FALSE}
cdm <- generateDenominatorCohortSet(
  cdm = cdm,
  name = "denominator",
  cohortDateRange = c(as.Date("2008-01-01"), as.Date("2012-01-01")),
  ageGroup = list(c(18, 65)),
  sex = c("Male", "Female", "Both"),
  daysPriorObservation = 365
)
```

We can see that each of our denominator cohorts is in the format of an OMOP CDM cohort:

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

We can also see the settings associated with each cohort:

```{r}
settings(cdm$denominator)
```

And we can also see the count for each cohort

```{r}
cohortCount(cdm$denominator)
```

Now that we have our denominator cohorts, and using the outcome cohort that was also generated by the `mockIncidencePrevalence()` function, we can estimate prevalence for each using the `estimatePointPrevalence()` function. Here we calculate point prevalence on a yearly basis.

```{r, message= FALSE, warning=FALSE}
prev <- estimatePeriodPrevalence(
  cdm = cdm,
  denominatorTable = "denominator",
  outcomeTable = "outcome",
  interval = "quarters"
)

prev %>%
  glimpse()
```

```{r, message= FALSE, warning=FALSE, echo=FALSE}
plotPrevalence(prev,
  facet = "denominator_sex",
  colour = "denominator_sex"
)
```

Similarly we can use the `estimateIncidence()` function to estimate incidence rates. Here we annual incidence rates, with 180 days used for outcome washout windows.

```{r, message= FALSE, warning=FALSE}
inc <- estimateIncidence(
  cdm = cdm,
  denominatorTable = "denominator",
  outcomeTable = "outcome",
  interval = c("Years"),
  outcomeWashout = 180
)

inc %>%
  glimpse()
```

```{r, message= FALSE, warning=FALSE, echo=FALSE}
plotIncidence(inc, facet = "denominator_sex", colour = "denominator_sex")
```
