1 Read the source functions

Instead of using the package overdoser or injuryepi you can run the code below to make the functions used in this analysis available during the R Session.

source("https://raw.githubusercontent.com/injuryepi/Utils/master/utils_funs_icd9.R")

Otherwise download the file utils_funs_icd9.R and run it with source("utils_funs_icd9.R"), assuming that the file was saved in the working directory, or indicate the path of its location.

2 Reading the Dummy Hospital Data

  1. Assuming the dummy file ICD9CM dummy dataset.xlsx is in the working directory:
cste_dummy9 <- readxl::read_excel("ICD9CM dummy dataset.xlsx", sheet = "data")

cste_dummy9 <- cste_dummy9 %>% 
  set_names(tolower)
  1. Otherwise read a compressed copy of the dummy dataset from GitHub with this code:
cste_dummy9 <- readRDS(gzcon(url("https://github.com/injuryepi/datasets/raw/master/ICD9CM_dummy_dataset.rds")))

2.1 Filter the Data to Criteria

Filter to 2014 Q2 - 2015 Q3, residents, non fatal discharges and acute care hospitals as indicated in the code below:

cste_dummy9_res <- cste_dummy9 %>%
  filter(discharge_date > as.Date("2014-03-31"), 
         discharge_date < as.Date("2015-10-01"),
         state_res == 50,
         disp != 20,
         str_sub(hosp_id, 1, 1) %in% c("A", "B", "D", "X"))

2.2 Add The Quarters

cste_dummy9_res <- cste_dummy9_res %>% 
  mutate(dis_year = lubridate::year(discharge_date),
         quarters = quarters(discharge_date),
         year_quarters = paste(dis_year, quarters, sep = "_"))

2.3 Add the four age groups

cste_dummy9_res <- cste_dummy9_res %>%
  add_age4(age = age)

3 Finding the First valid E-code

3.1 Defining first valid E-code

Since there are no dedicated e-code field, look for e-codes or no code (^E|$) in all the diagnosis fields with the function valid_e1() defined below. Note the difference in an earlier iteration where real data were used and there were dedicated columns for E-codes:

valid_e1 <- function(x){
  grep("^(?!(?:E0[0-2]|E030|E849|E967|E8694|E87|E9[34]))(?:^E|$)", 
       x , perl = T, value = T, ignore.case = T)[1]
}

3.2 Creating first valid E-code variable

Applying the function valid_e1 to all the Dx fields for each observation to capture the first valid e-code.
The result is the data frame cste_dummy_valid1 with the variable valid_ecode1 having the first valid e-code only or missing.

cste_dummy_valid1 <- cste_dummy9_res %>% 
  select(id, dx1:dx10) %>% 
  group_by(id) %>% 
  nest(.key = first_valid) %>% 
  mutate(valid_ecode1 = map(first_valid, valid_e1)) %>% 
  unnest()

3.3 Join first valid ecodes to the whole hospital data

Remove dx2 to dx10 that are no longer needed

cste_dummy9_res <- cste_dummy9_res %>% 
  select(-dx2:-dx10) %>% 
  left_join(select(cste_dummy_valid1, id, valid_ecode1))  

4 Adding drug variables

Using the function od_drug_types_icd9cm(). The argument diag_ecode_col expects the indices of the variables that contain the icd-9cm/ecodes of interest. The figures 12 and 18 respectively are the column indices for dx1 and the first valid ecode (valid_ecode1)

cste_dummy9_res <- cste_dummy9_res %>% 
  od_drug_types_icd9cm(diag_ecode_col = c(12,18)) 

5 Drug Overdose Count Tables

The final results are in Table 5.1 for any drug, Table 5.2 for non-heroin opioid and Table 5.3 for heroin.

5.1 Steps for complete tables

To keep all strata even in absence of cases.

age_4 <- cste_dummy9_res %>% 
  arrange(agegrp4) %>% 
  distinct(age4) %>% pull(age4)

yrs_qtrs <- sort(unique(cste_dummy9_res$year_quarters))

yr_qtr_age4 <- tibble(year_quarters = rep(yrs_qtrs, each = 4),
                      age4 = rep(age_4, times = 6))

5.2 All drug count by age and quarters

dummy9_anyd <- cste_dummy9_res %>% 
  filter(any_drug_icd9cm == 1) %>% 
  count(year_quarters, age4) %>% 
  right_join(yr_qtr_age4) %>% 
  replace_na(list(n = 0))

# transpose

dummy9_anyd_w <- dummy9_anyd %>% 
  select(year_quarters, age4, n) %>% 
  spread(key = year_quarters, value = n, fill = "")

# paste onto the template
onclip(dummy9_anyd_w[,-1], col.names = F)

# total
dummy9_anyd_t <- cste_dummy9_res %>% 
  filter(any_drug_icd9cm == 1) %>% 
  count(year_quarters) 

# paste onto the template
onclip(t(dummy9_anyd_t[, 2]), col.names = F)
Table 5.1: Discharge Diagnosis With Any Drug
age4 2014_Q2 2014_Q3 2014_Q4 2015_Q1 2015_Q2 2015_Q3
<25 21 30 39 31 20 17
25-44 48 54 107 45 27 9
45-64 40 47 58 33 26 11
65+ 8 13 20 15 6 7
total 117 144 224 124 79 44

5.3 Non Heroin opioid count by age and quarters

dummy9_nhopi <- cste_dummy9_res %>% 
  filter(non_heroin_icd9cm == 1) %>% 
  count(year_quarters, age4) %>% 
  right_join(yr_qtr_age4) %>% 
  replace_na(list(n = 0))

# transpose

dummy9_nhopi_w <- dummy9_nhopi %>% 
  select(year_quarters, age4, n) %>% 
  spread(key = year_quarters, value = n, fill = "")

# paste onto the template
onclip(dummy9_nhopi_w[,-1], col.names = F)

# total
dummy9_nhopi_t <- cste_dummy9_res %>% 
  filter(non_heroin_icd9cm == 1) %>% 
  count(year_quarters) 

# paste onto the template
onclip(t(dummy9_nhopi_t[, 2]), col.names = F)
Table 5.2: Discharge Diagnosis With Non-Heroin Opioid
age4 2014_Q2 2014_Q3 2014_Q4 2015_Q1 2015_Q2 2015_Q3
<25 9 13 17 11 8 5
25-44 22 25 61 17 12 4
45-64 19 23 29 14 10 3
65+ 4 7 11 7 2 2
total 54 68 118 49 32 14

5.4 Heroin count by age and quarters

dummy9_hero <- cste_dummy9_res %>% 
  filter(heroin_icd9cm == 1) %>% 
  count(year_quarters, age4) %>% 
  right_join(yr_qtr_age4) %>% 
  replace_na(list(n = 0))

# transpose

dummy9_hero_w <- dummy9_hero %>% 
  select(year_quarters, age4, n) %>% 
  spread(key = year_quarters, value = n, fill = "")

# paste onto the template
onclip(dummy9_hero_w[,-1], col.names = F)

# total
dummy9_hero_t <- cste_dummy9_res %>% 
  filter(heroin_icd9cm == 1) %>% 
  count(year_quarters) 

# paste onto the template
onclip(t(dummy9_hero_t[, 2]), col.names = F)
Table 5.3: Discharge Diagnosis With Heroin
age4 2014_Q2 2014_Q3 2014_Q4 2015_Q1 2015_Q2 2015_Q3
<25 7 10 13 9 7 5
25-44 20 22 30 15 11 4
45-64 17 20 25 12 9 3
65+ 2 4 7 5 1 2
total 46 56 75 41 28 14