---
title: "Introduction"
author: "Alessandro Gasparini"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Introduction}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
editor_options: 
  chunk_output_type: console
---

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

This vignette illustrates how to use the {comorbidity} package to identify comorbid conditions and to compute weighted (or unweighted) comorbidity scores.

For this, we will simulate a dataset with 100 patients and 10000 ICD-10 codes using the `sample_diag()` function:

```{r setup}
library(comorbidity)

set.seed(1)
df <- data.frame(
  id = sample(seq(100), size = 10000, replace = TRUE),
  code = sample_diag(n = 100)
)
# Sort
df <- df[order(df$id, df$code), ]
str(df)
```

By default, the `sample_diag()` function simulates ICD-10 data; it is however possible to simulate ICD-9 codes too, as we will see later on.

# Mapping Comorbidities

The `comorbidity()` function can be used to apply mapping algorithms to a dataset.
Here, for instance, we use the Quan et al. (2005) version of the Charlson Comorbidity Index:

```{r}
charlson_df <- comorbidity(
  x = df,
  id = "id",
  code = "code",
  map = "charlson_icd10_quan",
  assign0 = FALSE
)
str(charlson_df)
```

The resulting data frame has a row per subject, a column for IDs, and a column for each condition included in a given score (e.g. 17 conditions for the Charlson score).

```{r}
length(unique(df$id)) == nrow(charlson_df)
```

The different columns are also labelled for compatibility with the RStudio viewer, see e.g. `View(charlson_df)` after running the code above on your computer.

To see all supported mapping algorithms, please see the vignette:

```r
vignette("B-comorbidity-scores", package = "comorbidity")
```

# Comorbidity Scores

After calculating a data frame of comorbid conditions, that can be used to calculate comorbidity scores using the `score()` function.
Say we want to calculate the Charlson comorbidity score, weighted using the Quan et al. (2011) weights:

```{r}
quan_cci <- score(x = charlson_df, weights = "quan", assign0 = FALSE)
table(quan_cci)
```
 
This returns a single value per subject:

```{r}
length(quan_cci) == nrow(charlson_df)
```

If a pure combination of conditions is required (e.g. an unweighted score), pass the `NULL` value to the `weights` argument of the `score()` function:

```{r}
unw_cci <- score(x = charlson_df, weights = NULL, assign0 = FALSE)
table(unw_cci)
```

Once again, available weighting systems/algorithms are described in the same vignette that was mentioned above.

# References

* Charlson ME et al. _A new method of classifying prognostic comorbidity in longitudinal studies: development and validation_. Journal of Chronic Diseases 1987; 40:373-383. DOI: [10.1016/0021-9681(87)90171-8](https://doi.org/10.1016/0021-9681(87)90171-8)

* Quan H et al. _Coding algorithms for defining comorbidities in ICD-9-CM and ICD-10 administrative data_. Medical Care 2005; 43(11):1130-1139. DOI: [10.1097/01.mlr.0000182534.19832.83](https://doi.org/10.1097/01.mlr.0000182534.19832.83)

* Quan H et al. _Updating and validating the Charlson Comorbidity Index and score for risk adjustment in hospital discharge abstracts using data from 6 countries_. American Journal of Epidemiology 2011;173(6):676-82. DOI: [10.1093/aje/kwq433](http://dx.doi.org/10.1093/aje/kwq433)
