9.1 Phenotype outcome events
Load packages and reference.R
which will be used in phenotyping diabetes (details in DM section below).
library(tidyverse)
library(data.table)
source("reference.R")
Import primary care data.
<- fread("generated_data/entire_gp_clinical_30March2021_formatted.txt") pc
Import outcome-specific dictionaries.
<- readRDS("generated_data/dm_dict.RDS")
dm_dict <- readRDS("generated_data/dm_eye_disease_dict.RDS")
dr_dict <- readRDS("generated_data/kidney_disease_case_dict.RDS") kidney_disease_case_dict
9.1.1 DM
Define T1D and T2D keywords.
<- "type 2|type II|adult onset|non[ -]insulin[ -]dep|NIDD|maturity onset diabetes mellitus$"
type2_kw <- "type 1|type I[ $-]|juvenile|insulin-dependant|insulin dependent" type1_kw
Add diabetes types to DM dictionary.
<- dm_dict %>%
dm_dict mutate(dm_type = ifelse(grepl(type2_kw,term_description, ignore.case=T), "Type2", ifelse(grepl(type1_kw, term_description, ignore.case=T), "Type1", "Uncertain")))
Subset the primary care data to subjects with diabetes and identify which type of diabetes each event is associated with
<- pc %>%
diabetics_any select(f.eid, code, event_dt) %>%
filter(code %in% unique(dm_dict$code)) %>% distinct() %>%
left_join(dm_dict %>% select(code,dm_type), by = "code")
Next, we select the terms in the DM-specific dictionary whose descriptions reflect more clear indications of DM. Defined as an object dm_descriptions
in reference.R
, the descriptions were selected with the help of an expert.
<- dm_dict %>% filter(term_description %in% dm_descriptions) %>% .$code %>% unique dm_codes_confident
We define this list of terms with clear DM indications to be able to more accurately identify the first diagnosis date diabetes. Specifically, if a subject had terms predating the certain diagnosis which are less certain, we assume that the earlier diagnosis was the better start date. For example, consider a hypothetical subject with following diabetes events:
- 2012: “Diabetic dietary review”
- 2013: “Type II diabetes”
This individual will be considered type 2 diabetic patient with 2012 as the first date of DM diagnosis. Although the predating description “Diabetic dietary review” is a less certain of diagnosis, a subsequent diagnosis of “Type II diabetes” tells us this subject clearly has Type II diabetes. In contrast, the following subject would not be included as diabetes patient because the codes are not strong enough to indicate diabetes:
- 2012: “Diabetic dietary review”
- 2013: “Diabetic dietary review”
Based on this procedure, we will generate first occurrence event table for diabetes. First, subset the event table with any indications of diabetes to events with clear indications of diabetes.
<-
diabetics_simple %>%
diabetics_any group_by(f.eid) %>%
filter(any(code %in% dm_codes_confident)) %>%
mutate(event_dt = as.Date(event_dt)) %>%
arrange(f.eid,event_dt) %>%
distinct()
Generate first occurrence diabetes events.
<- diabetics_simple %>%
dm_firstoccur_pc group_by(f.eid) %>%
arrange(event_dt) %>%
slice(1) %>%
select(f.eid, event_dt)
saveRDS(diabetics_simple,"generated_data/dm_pc.RDS")
saveRDS(dm_firstoccur_pc,"generated_data/dm_firstoccur_pc.RDS")
9.1.2 DR
Phenotype diabetic eye disease event table.
<- pc %>% filter(code %in% dr_dict$code) %>%
dr_pc select(f.eid,event_dt) %>%
mutate(event_dt=as.Date(event_dt)) %>%
distinct() %>% arrange(f.eid,event_dt)
Generate first occurrence diabetic eye disease event table.
<- dr_pc %>% group_by(f.eid) %>% arrange(event_dt) %>% slice(1) dr_firstoccur_pc
saveRDS(dr_pc,"generated_data/dr_pc.RDS")
saveRDS(dr_firstoccur_pc,"generated_data/dr_firstoccur_pc.RDS")
9.1.3 DKD
Phenotype diabetic kidney disease event table.
<-
kidney_disease_case_pc %>% filter(code %in% kidney_disease_case_dict$code) %>%
pc select(f.eid,event_dt) %>%
mutate(event_dt = as.Date(event_dt)) %>%
distinct() %>% arrange(f.eid,event_dt)
Generate first occurrence diabetic kidney disease event table.
<- kidney_disease_case_pc %>% group_by(f.eid) %>% arrange(event_dt) %>% slice(1) kidney_disease_case_firstoccur_pc
saveRDS(kidney_disease_case_pc,"generated_data/kidney_disease_case_pc.RDS")
saveRDS(kidney_disease_case_firstoccur_pc,"generated_data/kidney_disease_case_firstoccur_pc.RDS")