-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplate_final_merge.R
62 lines (42 loc) · 2.17 KB
/
template_final_merge.R
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
# Pull together static patient data from all_diabetes_cohort table with biomarker, comorbidity, and sociodemographic (smoking/alcohol) data at the index dates
## Add in age and duration of diabetes at index date
############################################################################################
# Setup
library(tidyverse)
library(aurum)
library(EHRBiomarkr)
rm(list=ls())
cprd = CPRDData$new(cprdEnv = "test-remote",cprdConf = "~/.aurum.yaml")
cohort_prefix <- ""
# e.g. "mm" for treatment response (MASTERMIND) cohort
analysis = cprd$analysis(cohort_prefix)
############################################################################################
# Get handles to pre-existing data tables
## Cohort-specific IDs and index dates
index_dates <- index_dates %>% analysis$cached("index_dates")
## Cohort and patient characteristics
analysis = cprd$analysis("all")
diabetes_cohort <- diabetes_cohort %>% analysis$cached("diabetes_cohort")
## Baseline biomarkers plus CKD stage
analysis = cprd$analysis(cohort_prefix)
baseline_biomarkers <- baseline_biomarkers %>% analysis$cached("baseline_biomarkers")
ckd_stages <- ckd_stages %>% analysis$cached("ckd_stages")
## Comorbidities
comorbidities <- comorbidities %>% analysis$cached("comorbidities")
## Smoking status
smoking <- smoking %>% analysis$cached("smoking")
## Alcohol status
alcohol <- alcohol %>% analysis$cached("alcohol")
############################################################################################
# Make final merge table and add age and diabetes duration at index date
final_merge <- index_dates %>%
left_join(diabetes_cohort, by="patid") %>%
left_join(baseline_biomarkers, by=c("patid", "index_date")) %>%
left_join(ckd_stages, by=c("patid", "index_date")) %>%
left_join(comorbidities, by=c("patid", "index_date")) %>%
left_join(smoking, by=c("patid", "index_date")) %>%
left_join(alcohol, by=c("patid", "index_date")) %>%
mutate(index_date_age=datediff(index_date, dob)/365.25,
index_date_dm_dur_all=datediff(index_date, dm_diag_date_all)/365.25) %>%
relocate(c(index_date_age, index_date_dm_dur_all), .before=gender) %>%
analysis$cached("final_merge", indexes=c("patid", "index_date"))