Methods

Installation

library(devtools)
load_all()
library(icdb)
icdb::use_cache(TRUE)
library(kableExtra)

Cohort definitions

All patients in the BNSSG area with a hospital admission for an acute coronary syndrome event, between July 2018 and December 2021, were included in the main cohort. This date window was chosen as prescription data in the SWD only exists from July 2018 onward. GP attribute data only exists from October 2019 onward.

Date window

acs_date_min <- as.POSIXct("2018-07-01", tz="GMT")
acs_date_max <- as.POSIXct("2022-12-31", tz="GMT")
kbl(data.frame("Variable" = c("Start date", "End date"), 
               "Value"    = c(acs_date_min, acs_date_max)), 
    longtable=T, booktabs=T, caption="Date window")

Hospital admission method

Acute admissions were identified by filtering on the appropriate admission method codes. Here we accept admission methods that contain the word "emergency".

adm_method_codes <- system.file("admission_method_codes/admission_method_codes.csv", package="acsprojectns")
adm_method <- acsprojectns::get_codes(adm_method_codes) |>
  dplyr::filter(grepl("emergency", .data$admission_method_description))
kbl(adm_method, longtable = T, booktabs = T, caption = "Admission methods")

Acute coronary syndrome

Acute coronary syndrome codes were defined as the following.

acs_icd_codes <- yaml::read_yaml(system.file("icd10_codes", "acs.yaml", package="acsprojectns")) |>
  icdb::parse_codes()
unlist(unname(acs_icd_codes))

Connect to the server

If working outside of the ICB we need to use the synthetic testing data, to create run the script located in scripts/create_dummy_database_scripts/write_testing_sqlite_database.R

if(.Platform$OS.type == "unix") {
  msrv <- icdb::mapped_server(config  = system.file("database_connections", "home", "mysql.yaml", package="acsprojectns"), 
                              mapping = system.file("database_connections", "home", "mapping.yaml", package="acsprojectns"))
}else if (.Platform$OS.type == "windows") {
  msrv <- icdb::mapped_server("XSW", 
                              mapping = system.file("database_connections", "work", "mapping.yaml", package="acsprojectns"))
}

Generate the cohort

# Define the unique cohort ids
cohort_ids <- msrv$sus$apc_spells_primary_diagnosis |>
  # Time window
  dplyr::filter(spell_start >= acs_date_min & spell_end <= acs_date_max) |>
  # Emergency admission
  dplyr::filter(as.character(method_of_admission_hospital_provider_spell) %in% !!adm_method$admission_method_code) |>
  # Acute coronary syndrome primary diagnosis
  #icdb::codes_from(system.file("icd10_codes", "acs.yaml", package="acsprojectns"), primary_diagnosis_icd) |>
  # Only bnssg patients
  dplyr::inner_join(msrv$swd$attr_h |> dplyr::distinct(nhs_number), by="nhs_number") |>
  # Collect to local
  icdb::run() |>
  # Recode the ICD10 codes
  dplyr::mutate(nhs_number = as.character(nhs_number), 
                primary_diagnosis = icdb::icd10(primary_diagnosis_icd, 
                                                system.file("icd10_codes", "icd10_ns.yaml", package="acsprojectns"))) %>%
  # Make sure ICD10 codes are all valid
  # {if(all(icdb::is_valid(.$primary_diagnosis))) . else stop("error1 - couldn't parse all the ICD codes")} |>
  # Only ACS events
  dplyr::filter(primary_diagnosis %in_group% c("acs_stemi", "acs_nstemi", "acs_unstable_angina")) |>
  # Distinct NHS numbers
  dplyr::distinct(nhs_number) |>
  # As a plain character vector
  dplyr::pull() |> as.character()

# View
head(cohort_ids, n=10L)


nicksunderland/acsprojectns documentation built on July 20, 2023, 7:23 p.m.