Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code status proposal #46

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
9 changes: 9 additions & 0 deletions concepts/code_status/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The files here create a patient table that tracks the changes to the patient code status. The careplangeneral table was used instead of careplaneol because careplaneol had much less recorded patients, and the type of code status change was recorded in careplangeneral. There are two versions of the files: .Rmd and .sql. The Rmd version is the supported version, while the SQL version, having been mostly autogenerated using show_query(), is not supported and was added just as a template.

Rmd:
- careplan_getitemvalues creates a csv that contains all the cplitemvalues for inspection, in order to choose the values that correspond to patient code status.
- careplan_getpatientcode creates a table that lists the recorded patient code status at a certain time (offset).

SQL:
- careplan_getitemvalues creates a materialized view that lists all the cplitemvalues (possible statuses) in descending order of the counts
 - careplan_getpatientcode creates a materialized view that lists the recorded patient code status at a certain time (offset).
48 changes: 48 additions & 0 deletions concepts/code_status/careplan_getitemvalues.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
title: "carePlan_getItemValues"
output: html_document
author: Joseph Park
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

Load configuration settings. Password requested by rstudioapi.

```{r}
dbdriver <- 'PostgreSQL'
host <- 'localhost'
port <- '5432'
user <- 'josephpark'
dbname <- 'eicu'
schema <- 'eicu'
options <- paste("--search_path=", schema, sep="")
```

Connect to database.

```{r}
require("RPostgreSQL")
drv <- dbDriver(dbdriver)
con <- dbConnect(drv, dbname = dbname,
host = host, port = port,
user = user, password = rstudioapi::askForPassword("Database password"),
options = options)
dbExistsTable(con, "patient")
```

Write cplitemvalues to file "possible_status" for inspection on which items indicate patient code status.
All cplitemvalues that were chosen were in cplgroup = 'Care Limitation' (all cplitemvalues in this cplgroup was used),
except cplitemvalue = 'End of life', which was in cplgroup = 'Ordered Protocols'.
SQL command was generated using show_query() for possible_status

```{r, connection = con}
library(dplyr)
carePlanGeneral_tbl <- tbl(con, "careplangeneral")
possible_status <- carePlanGeneral_tbl %>%
group_by(cplitemvalue) %>%
summarize(n=n()) %>%
arrange(desc(n))
write.csv(possible_status, "possible_status.csv")
```
10 changes: 10 additions & 0 deletions concepts/code_status/careplan_getitemvalues.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- author: Joseph Park
-- NOT SUPPORTED: autogenerated from careplan_getitemvalues.Rmd by using show_query()

DROP MATERIALIZED VIEW IF EXISTS possibleCodeStatus;
CREATE MATERIALIZED VIEW possibleCodeStatus AS

SELECT "cplitemvalue", count(*) AS "n"
FROM "careplangeneral"
GROUP BY "cplitemvalue"
ORDER BY "n" DESC;
78 changes: 78 additions & 0 deletions concepts/code_status/careplan_getpatientcode.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
title: "carePlan_getPatientCode"
output: html_document
author: Joseph Park
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

Load configuration settings. Password requested by rstudioapi.

```{r}
dbdriver <- 'PostgreSQL'
host <- 'localhost'
port <- '5432'
user <- 'josephpark'
dbname <- 'eicu'
schema <- 'eicu'
options <- paste("--search_path=", schema, sep="")
```

Connect to database.

```{r}
require("RPostgreSQL")
drv <- dbDriver(dbdriver)
con <- dbConnect(drv, dbname = dbname,
host = host, port = port,
user = user, password = rstudioapi::askForPassword("Database password"),
options = options)
dbExistsTable(con, "patient")
```

After inspecting table generated from careplan_getitemvalues.Rmd, the values in cpitemvalue_code were selected to relate to patient code status.

```{r, connection = con}
library(dplyr)
carePlanGeneral_tbl <- tbl(con, "careplangeneral")
cplitemvalue_code = c('Full therapy', 'Do not resuscitate', 'No CPR', 'No intubation', 'Comfort measures only',
'No cardioversion', 'No vasopressors/inotropes', 'No augmentation of care',
'End of life', 'No blood products', 'No blood draws', 'Advance directives')
```

Generate table that lists the patient along with code status.
SQL command was generated using show_query() for patientCodeStatus

```{r, connection = con}
patientCodeStatus <- carePlanGeneral_tbl %>%
filter(cplitemvalue %in% cplitemvalue_code) %>%
select(patientunitstayid, cplitemoffset, cplgroup, cplitemvalue) %>%
arrange(patientunitstayid, cplitemoffset, cplitemvalue) %>%
group_by(patientunitstayid, cplitemoffset) %>%
summarize(cplitemvalue = paste(cplitemvalue, collapse=", "))
patientCodeStatus
```

Output the file as a csv.

```{r, connection = con}
write.csv(patientCodeStatus, "patient_status_table.csv", row.names=FALSE)
```

Optional: sorts patients by which patient had the most number of status updates.

```{r, connection = con,eval=FALSE}
patientCodeStatus %>%
group_by(patientunitstayid) %>%
summarize(n = n()) %>%
arrange(desc(n))
```

Example of a patient. This patient was strange with both full therapy and no CPR at the same time.

```{r, connection = con,eval=FALSE}
patientCodeStatus %>%
filter(patientunitstayid == 266999)
```
14 changes: 14 additions & 0 deletions concepts/code_status/careplan_getpatientcode.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- author: Joseph Park
-- NOT SUPPORTED: autogenerated from careplan_getpatientcode.Rmd by using show_query()

DROP MATERIALIZED VIEW IF EXISTS patientCodeStatus;
CREATE MATERIALIZED VIEW patientCodeStatus AS

SELECT "patientunitstayid", "cplitemoffset", string_agg("cplitemvalue", ', ') AS "cplitemvalue"
FROM (SELECT *
FROM (SELECT "patientunitstayid" AS "patientunitstayid", "cplitemoffset" AS "cplitemoffset", "cplgroup" AS "cplgroup", "cplitemvalue" AS "cplitemvalue"
FROM (SELECT *
FROM "careplangeneral"
WHERE ("cplitemvalue" IN ('Full therapy', 'Do not resuscitate', 'No CPR', 'No intubation', 'Comfort measures only', 'No cardioversion', 'No vasopressors/inotropes', 'No augmentation of care', 'End of life', 'No blood products', 'No blood draws', 'Advance directives'))) "ivtmaaxwzk") "oytldaespk"
ORDER BY "patientunitstayid", "cplitemoffset", "cplitemvalue") "vrxthajevr"
GROUP BY "patientunitstayid", "cplitemoffset";