-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09-phenotype_outcomes_pcp.Rmd
193 lines (136 loc) · 6.05 KB
/
09-phenotype_outcomes_pcp.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# Phenotype outcomes using primary care data{#phenotype-outcomes-pcp}
In this chapter, we use outcome-specific dictionaries generated in chapter \@ref(make-outcome-specific-code-dictionary) and the primary care data prepared in chapter \@ref(prep-pcp-data) to generate the following outcomes and control exclusion events:
- diabetes
- diabetic kidney disease
- diabetic retinopathy
- kidney disease control exclusion events
- non-diabetic eye disease events
The event tables are created by subsetting the primary care data with a corresponding outcome-specific dictionary.
## Phenotype outcome events
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, eval=F)
```
Load packages and `reference.R` which will be used in phenotyping diabetes (details in DM section below).
```{r, message=F}
library(tidyverse)
library(data.table)
source("reference.R")
```
Import primary care data.
```{r}
pc <- fread("generated_data/entire_gp_clinical_30March2021_formatted.txt")
```
Import outcome-specific dictionaries.
```{r}
dm_dict <- readRDS("generated_data/dm_dict.RDS")
dr_dict <- readRDS("generated_data/dm_eye_disease_dict.RDS")
kidney_disease_case_dict <- readRDS("generated_data/kidney_disease_case_dict.RDS")
```
### DM
Define T1D and T2D keywords.
```{r}
type2_kw <- "type 2|type II|adult onset|non[ -]insulin[ -]dep|NIDD|maturity onset diabetes mellitus$"
type1_kw <- "type 1|type I[ $-]|juvenile|insulin-dependant|insulin dependent"
```
Add diabetes types to DM dictionary.
```{r}
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
```{r}
diabetics_any <- pc %>%
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.
```{r}
dm_codes_confident <- dm_dict %>% filter(term_description %in% dm_descriptions) %>% .$code %>% unique
```
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.
```{r}
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.
```{r}
dm_firstoccur_pc <- diabetics_simple %>%
group_by(f.eid) %>%
arrange(event_dt) %>%
slice(1) %>%
select(f.eid, event_dt)
```
```{r}
saveRDS(diabetics_simple,"generated_data/dm_pc.RDS")
saveRDS(dm_firstoccur_pc,"generated_data/dm_firstoccur_pc.RDS")
```
### DR
Phenotype diabetic eye disease event table.
```{r}
dr_pc <- pc %>% filter(code %in% dr_dict$code) %>%
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.
```{r}
dr_firstoccur_pc <- dr_pc %>% group_by(f.eid) %>% arrange(event_dt) %>% slice(1)
```
```{r}
saveRDS(dr_pc,"generated_data/dr_pc.RDS")
saveRDS(dr_firstoccur_pc,"generated_data/dr_firstoccur_pc.RDS")
```
### DKD
Phenotype diabetic kidney disease event table.
```{r}
kidney_disease_case_pc <-
pc %>% filter(code %in% kidney_disease_case_dict$code) %>%
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.
```{r}
kidney_disease_case_firstoccur_pc <- kidney_disease_case_pc %>% group_by(f.eid) %>% arrange(event_dt) %>% slice(1)
```
```{r}
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")
```
## Phenotype control exclusion events
Import the outcome-specific dictionaries for control exclusion events.
```{r}
nondm_eye_disease_dict <- readRDS("generated_data/nondm_eye_disease_dict.RDS")
kidney_disease_control_exclusion_dict <- readRDS("generated_data/kidney_disease_control_exclusion_dict.RDS")
```
### Non-diabetic eye disease events
Phenotype non-diabetic eye disease event table.
```{r}
nondm_eye_disease_pc <-
pc %>% filter(code %in% nondm_eye_disease_dict$code) %>% select(f.eid,event_dt) %>% mutate(event_dt=as.Date(event_dt))
```
```{r}
saveRDS(nondm_eye_disease_pc,"generated_data/nondm_eye_disease_pc.RDS")
```
### Kidney disease control exclusion events
Phenotype kidney disease control exclusion event table.
```{r}
kidney_disease_control_exclusion_pc <-
pc %>% filter(code %in% kidney_disease_control_exclusion_dict$code) %>%
mutate(event_dt = as.Date(event_dt))
```
```{r}
saveRDS(kidney_disease_control_exclusion_pc,"generated_data/kidney_disease_control_exclusion_pc.RDS")
```