---
title: "Quick custom comorbidity maps are helpful"
author: "Jack O. Wasey"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Quick custom comorbidity maps are helpful}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
  %\VignetteKeyword{comorbidity, comorbidities, biomedical informatics, administrative data, ICD-9, ICD-10, ICD-9-CM, ICD-10-CM, mapping, algorithm}
---

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

Many problems with clinical data are best solved using the standard and widely validated comorbidities, mapped to subsets of ICD-9 or ICD-10 codes, as published by authors including Elixhauser, Quan, Deyo and the AHRQ. It is also common to have a pre-defined specific set of ICD codes. Examples of this include:

- Studying sub-types of a common comorbidity, such as obesity: although obesity is in the AHRQ and Elixhauser mappings, there is no granularity.
- Studying a particular disease: most diseases are not in the standard comorbidity maps. To distinguish patients having or not having a particular set of diseases is usually not possible using the standard comorbidity maps. E.g. Which patients have eczema? Which emergency room patients presented with a sexual health problem?

`icd` has a simple mechanism to use custom category-ICD maps. A comorbidity map is a list of character vectors. Each list must be named to reflect the ICD codes it contains; the character vector contains the ICD codes themselves.

Let's take a look at the first few items in the Charlson map from Quan and Deyo:

```{r charlsonexample}
print(icd::icd10_map_quan_deyo, n_comorbidities = 3, n_codes = 8)
```

# Obesity example

Now we can make our own comorbidity map, and apply it to [a question on 'Stackoverflow'](https://stackoverflow.com/questions/53954537/creating-categorical-variable-in-r/53954950#53954950).

```{r obesity}
library(icd)
obesity_map <- list(
  "1" = "E663",
  "2" = c("E669", "E668", "E6609"),
  "3" = "E661",
  "4" = "E662")

obesity <- data.frame(
  ICD.10.Code = c("E6601", "E663", "E663", "E6609"),
  Encounter.ID = c("408773", "542207", "358741", "342534")
)

custom_map_result <- icd::icd10_comorbid(
  obesity,
  map = obesity_map)

custom_map_result

# finally, format as requested by the user
apply(custom_map_result, 1, function(x) {
  if (!any(x)) 4 else which(x)[1]
  })

# see also:
icd::icd10_map_ahrq$Obesity
icd::icd10_map_quan_elix$Obesity
icd::icd10_comorbid_quan_elix(
  obesity,
  return_df = TRUE)["Obesity"]
```

## Maps with ranges of codes

Sometimes there are a large number of ICD codes, and they can be defined more succinctly with ranges, then by specifying every single code. In addition, as new codes are added to ICD-10-CM, (especially ICD-10-CM -- it moves much faster than ICD-10 from the WHO) having specific hard-coded ICD-10 codes will miss closely related codes in the future. Using the ranges functions from `icd` helps with both these problems.

Again, using a [Stackoverflow question](https://stackoverflow.com/questions/54245282/how-to-compare-icd-10-character-string-or-search-whether-a-diagnosis-fall-into-t/54277842#54277842):

```{r sotwo}
library(icd)
diagnoses <- c("C349", "A219", "B003", "C509", "B700", "A090")
one_pt <- data.frame(id = rep("patient1", length(diagnoses)),
                     diagnoses)
dif_pt <- data.frame(id = paste0("patient", seq_along(diagnoses)),
                     diagnoses)
my_map <- list(c01to17 = expand_range("C01", "C17"),
               a74to75 = expand_range("A748", "A759"),
               b00to33 = expand_range("B001", "B331"),
               b69to72 = expand_range("B69", "B72"),
               c00to94 = expand_range("C000", "C942"))
# optionally use as.comorbidity_map which ensures it is valid, and let's it
# print more pleasantly
my_map <- as.comorbidity_map(my_map)
print(my_map)
icd::comorbid(one_pt, map = my_map)
(six_pts_cmb <- icd::comorbid(dif_pt, map = my_map))
```

```{r matriximage, fig.width=6, echo = FALSE}
{
  image(t(six_pts_cmb),
        col = c("light blue", "blue"),
        xlab = "Custom disease ranges",
        axes = FALSE
  )
  axis(1,
       at = seq(0, 1, length.out = length(my_map)),
       labels = names(my_map),
       lwd = 0
  )
  axis(2,
       at = seq(0, 1, length.out = nrow(dif_pt)),
       labels = dif_pt$id,
       lwd = 0,
       las = 2
  )
}
```
