---
title: "Charlson and Van Walraven scores"
author: "Jack O. Wasey"
date: "`r Sys.Date()`"
output:
  html_document:
    toc: yes
vignette: >
  %\VignetteIndexEntry{Charlson and Van Walraven scores}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteDepends{magrittr, icd.data}
  %\VignetteEncoding{UTF-8}
---
<!--
Copyright (C) 2014 - 2018  Jack O. Wasey

This file is part of icd.

icd is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

icd is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with icd. If not, see <http:#www.gnu.org/licenses/>.
-->
```{r setup, include=FALSE}
suppressWarnings({
  suppressPackageStartupMessages({
    requireNamespace("knitr")
    library("icd")
    library("magrittr")
    })
  })
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```
## ICD code scoring systems
A common use of ICD codes is calculation as a Charlson score, which gives a measure of how well a patient is, albeit based on the limited available in admission and discharge diagnoses. The Charlson scoring system attributes scores based on presence of diseases falling into any of the Charlson comorbidities. Quan updated the scores given to each comorbidity to better reflect morbidity and mortality in a more recent population. Van Walraven provides a similar scoring methodology for the Elixhauser comorbidities (as used by the US AHRQ).

More complicated scoring systems may use lab values, patient demographic information, and so on. Any contributions to this package for calculations of scoring systems based on comorbidities and other data would be welcome.

## Vermont example data, Charlson scores

The Vermont data are actually discharge, not admission diagnoses, but can be used to demonstrate generating Charlson scores.
```{r vermont-charlson}
head(icd.data::vermont_dx[1:10])
v <- wide_to_long(icd.data::vermont_dx)
head(v)
charlson(v) %>% summary
head(charlson(v))
head(names(charlson(v)))
```
This default result is a numeric vector with the names (top numbers) as the patient identifiers. Those who like working with 'tidy' data frames can use:
```{r charlsondf}
head(charlson(v, return_df = TRUE))
```

Behind the scenes, _icd_ calculates the Charlson comorbidities for those ICD codes, applies the Charlson scoring system, and returns the Charlson score for each patient.

## Vermont example data, Van Walraven scores

The same principle can be used to calculate the Van Walraven score, which is the Charlson score counterpart for Elixhauser comorbidities.

```{r vermontvanwalraven}
`Vermont Van Walraven Scores` <- van_walraven(v)
hist(`Vermont Van Walraven Scores`)
```

## Working with mixed ICD-9 and ICD-10 codes

All the functions in `icd` work with one code type. They are tolerant of having different sub-types of ICD-9 or ICD-10 codes together for comorbidity calculations, but patients with mixed data can be combined. E.g. patient A has two ICD-9 and two ICD-10 codes:
```{r icd9and10}
icd9 <- data.frame(pts = c("A", "A"), c("041.04", "244.9"))
icd10 <- data.frame(pts = c("A", "A"), c("C82.28", "M08.979"))
both <- comorbid_elix(icd9) | comorbid_elix(icd10)
van_walraven_from_comorbid(both)
```
More commonly, some patients before a certain date will have ICD-9 codes, and others will have ICD-10 codes:
```{r icd9then10}
icd9 <- data.frame(pts = c("A", "A"), c("041.04", "244.9"))
icd10 <- data.frame(pts = c("B", "B"), c("C82.28", "M08.979"))
both <- rbind(comorbid_elix(icd9), comorbid_elix(icd10))
van_walraven_from_comorbid(both)
```
