---
title: "Using Hierarchical Condition Codes"
author:
- Andrew Cooper (original)
- Jack Wasey (minor updates)
date: "`r Sys.Date()`"
output:
  html_document:
    toc: yes
vignette: >
  %\VignetteIndexEntry{Using Hierarchical Condition Codes}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteDepends{icd.data}
  %\VignetteEncoding{UTF-8}
---

Hierarchical Condition Categories (HCC) is a method of categorizing ICD codes created by the Centers for Medicare and Medicaid Services (CMS), which is implemented in the [icd package](https://cran.r-project.org/package=icd) for R. The package provides [fast comorbidity calculations](introduction.html) for HCCs and other maps which translate ICD codes to comorbidities.

HCC is designed to categorize the risk level of a patient with chronic or serious illness using diagnoses and demographic information. Healthcare providers implement HCC in payment models for patients, as it can help predict the amount of services a patient will need in the future. HCC can be a useful tool in analysis with ICD codes, and the ICD package provides methods to use them.

HCC first assigns ICD codes to Condition Categories (CC). Condition categories are numeric values ranked based on severity/risk. If an ICD code belongs to more than one CC, the most severe CC is assigned. In other words, HCC classifies patients based on their most severe conditions.

We can see the mapping of ICD-9 and ICD-10 codes to HCC with the `icd9_map_cc` and `icd10_map_cc` data.

```{r head}
library("icd")
head(icd9_map_cc)
```

The method returns a table with each ICD-9 code and its corresponding CC. The third column labeled "year" specifies the year that a ICD-9 code was assigned to a corresponding CC. This is needed because HCC has been changed and updated over the years, so the CC assigned to a code in one year might be different from the CC assigned to that same code in another year.

ICD also provides a method for mapping specific ICD codes to HCCs. The method `comorbid_hcc` takes as input a list of ICD-9 or ICD-10 codes and outputs a mapping of those codes to their corresponding CCs. For this example, we have an arbitrary table of five ICD-9 codes along with their corresponding patient identifiers and dates of visit.

```{r pts}
pts <- data.frame(patient_id = c("1", "2", "3", "4", "4"),
                  icd_code = c("20084", "1742", "30410", "41514", "95893"), 
                  date = as.Date(c("2011-01-01", "2011-01-02", "2011-01-03", "2011-01-04", "2011-01-04")))

pts
```

The function `comorbid_hcc` takes as input a data frame in the "long" format (i.e., multiple rows per patients). If the data has one row per patient, but multiple codes in different columns, this is the "wide" format. `wide_to_long` could be used to convert before inputting into `comorbid_hcc`. Doing it this way, instead of with tools like `dplyr` allows some guessing for which columns are codes, and which are identifiers, and is also written specifically to do this task efficiently, given the usual structure of hospital data. `comorbid_hcc` also requires the ICD codes to be in "short" format. If your codes are in "decimal" format, you can easily convert them with the function `decimal_to_short`.

Now that our data are in the correct form, we run the mapping function `comorbid_hcc` with our input, specifying the name of the column in our dataset that contains patients' visit dates.

```{r hcc}
comorbid_hcc(pts)
```

Each of the four patients is assigned to an appropriate CC based on the risk level of their most severe diagnoses.
