-
Notifications
You must be signed in to change notification settings - Fork 0
/
CEDAR_DQI_example.RMD
233 lines (190 loc) · 7.78 KB
/
CEDAR_DQI_example.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
Demo Script for GMDS2023 Workshop
```{r, echo=FALSE,results=FALSE, warning=FALSE}
# libraries DQ
library(dataquieR)
library(tibble)
library(tidyverse)
library(dplyr)
library(data.table)
library(lubridate)
library(dataquieR)
library(markdown)
# libraries CEDAR
library(httr2)
library(httpuv)
library(yaml)
library(jsonlite)
# dashboard
library(flexsiteboard)
library(flexdashboard)
library(DT)
# report
library(rmarkdown)
```
generate test data
according to github csv schema for VHF use case
see https://github.com/medizininformatik-initiative/Projectathon6-smith2 readme for description
```{r}
### build test data functions ###
# function for random dates
# source https://stackoverflow.com/questions/21502332/generating-random-dates
rdate <- function(x,
min = paste0(format(Sys.Date(), '%Y'), '-01-01'),
max = paste0(format(Sys.Date(), '%Y'), '-12-31'),
sort = TRUE) {
dates <- sample(seq(as.Date(min), as.Date(max), by = "day"), x, replace = TRUE)
if (sort == TRUE) {
sort(dates)
} else {
dates
}
}
### cohort ###
# generate empty df
cohort_synt <- data.frame(subject=character(),
NTproBNP.date=as.Date(character()),
NTproBNP.value=double(),
NTproBNP.unit=character(),
NTproBNP.unitSystem=character(),
gender=character(),
birthdate=as.Date(character()),
encounter.id=character(),
encounter.start=as.Date(character()),
encounter.end=as.Date(character()),
serviceType=character(),
stringsAsFactors=FALSE)
# generate synthetic data, assuming ntproBNP normally distributed
cohort_synt <- data.frame(subject = as.character(seq(1000,5999)),
NTproBNP.date = rdate(5000, min = "2019-01-01", max = "2021-12-31",sort = FALSE),
NTproBNP.value = rnorm(5000, mean = 5000, sd = 1000),
NTproBNP.unit = rep("pg/mL",5000),
NTproBNP.unitSystem = rep("http://unitsofmeasure.org",5000),
gender = sample(c("male","female","other"), prob = c(0.475,0.475,0.05), size = 5000, replace = TRUE),
birthdate = rdate(5000, min = "1960-01-01", max = "1990-12-31", sort = FALSE),
encounter.id = as.character(seq(91000,95999)),
encounter.start = rdate(5000, min = "2019-01-01", max = "2020-12-31",sort = FALSE),
encounter.end = rdate(5000, min = "2020-01-01", max = "2021-12-31",sort = FALSE),
serviceType = rep("unknown",5000))
# #check dist, limits 1.5k 8.5k for 99th quantile
# quantile(cohort_synt_DQ$NTproBNP.value, probs = c(0.01,0.99))
# hist(cohort_synt$NTproBNP.value)
# add outliers
outliers_ID <- sample(seq(0,4999), size = 50, replace = TRUE)
cohort_synt[outliers_ID,]$NTproBNP.value <- runif(1,10000,12000)
#check dist, limits 30 10k
#hist(cohort_synt$NTproBNP.value)
#cohort_synt
```
transform cohort to dataquieR format
```{r}
cohort_synt_DQ <- cohort_synt
# calculate age
cohort_synt_DQ$birthdate <- trunc((cohort_synt_DQ$birthdate %--% Sys.Date()) / years(1))
# rename cols and fix type
cohort_synt_DQ <- rename(cohort_synt_DQ, age = birthdate)
cohort_synt_DQ$age <- as.numeric(cohort_synt_DQ$age)
# apply levels
cohort_synt_DQ$NTproBNP.unit <- gsub("pg/mL","1",cohort_synt_DQ$NTproBNP.unit)
cohort_synt_DQ$NTproBNP.unit <- as.numeric(cohort_synt_DQ$NTproBNP.unit)
cohort_synt_DQ$NTproBNP.unitSystem <- gsub("http://unitsofmeasure.org","1",cohort_synt_DQ$NTproBNP.unitSystem)
cohort_synt_DQ$NTproBNP.unitSystem <- as.numeric(cohort_synt_DQ$NTproBNP.unitSystem)
cohort_synt_DQ$gender <- gsub("female","2",cohort_synt_DQ$gender)
cohort_synt_DQ$gender <- gsub("male","1",cohort_synt_DQ$gender)
cohort_synt_DQ$gender <- gsub("other","3",cohort_synt_DQ$gender)
cohort_synt_DQ$gender <- as.numeric(cohort_synt_DQ$gender)
cohort_synt_DQ$serviceType <- gsub("unknown","1",cohort_synt_DQ$serviceType)
cohort_synt_DQ$serviceType <- as.numeric(cohort_synt_DQ$serviceType)
#fix typing
cohort_synt_DQ$subject <- as.numeric(cohort_synt_DQ$subject)
cohort_synt_DQ$encounter.id <- as.numeric(cohort_synt_DQ$encounter.id)
#cohort_synt_DQ
```
read dataquieR conform metadata, from .csv
```{r}
metadata_report <- read.csv("input\\metadata.csv")
checks_report <- read.csv("input\\contradictions.csv")
```
analysis with dataquieR
contradiction checks
https://dataquality.qihs.uni-greifswald.de/VIN_con_impl_contradictions.html
```{r,warning=FALSE}
# full dq report as html
# dq_report is deprecated, use dq_report2
# my_dq_report <- dq_report(study_data = cohort_synt_DQ, #sd1
# meta_data = metadata_report,#md1
# check_table = checks_report,
# label_col = LABEL)
#
# # show results
# my_dq_report
# contradiction checks
AnyContradictions <- con_contradictions(study_data = cohort_synt_DQ,
meta_data = metadata_report,
label_col = "LABEL",
check_table = checks_report,
threshold_value = 1)
AnyContradictions$SummaryPlot
```
outlier detection
https://dataquality.qihs.uni-greifswald.de/VIN_acc_impl_robust_univariate_outlier.html
```{r,warning=FALSE}
outliers <- acc_univariate_outlier(study_data = cohort_synt_DQ,
meta_data = metadata_report,
label_col = "LABEL")
outliers$SummaryPlotList$ntprobnp
```
run limit checks
https://dataquality.qihs.uni-greifswald.de/VIN_DQ-report-SHIP-example.html
```{r,warning=FALSE}
limit_checks <- con_limit_deviations(study_data = cohort_synt_DQ,
meta_data = metadata_report,
label_col = "LABEL",
limits = "SOFT_LIMITS")
limit_checks$SummaryPlotList$ntprobnp
```
get new metadata from CEDAR
https://more.metadatacenter.org/tools-training/cedar-api#userfacingapi
```{r}
# read config with API KEY
# see readme.MD on how to find your API KEY
config <- read_yaml("input/config.yml")
# select metadata instance by ID
# pjt6 template
#template_instance_id <- "4e297825-9c12-41b8-8d97-d783bfb3686e"
# workshop template
template_instance_id <- "853bc707-c67d-49fe-998a-d373fb2b9287"
tmp <- tempfile()
if(!dir.exists("tmp")){dir.create("tmp")}
# GET request to CEDAR with httr2
cedar_req <- request("https://resource.metadatacenter.org/")
cedar_req <- cedar_req %>%
req_headers("Accept" = "application/json") %>%
req_headers("Authorization" = paste0("apiKey ",config$apiKey)) %>%
req_retry(max_tries = 5) %>%
req_url_path_append(paste0("template-instances/https%3A%2F%2Frepo.metadatacenter.org%2Ftemplate-instances%2F",template_instance_id))
# dry run
#cedar_req %>% req_dry_run()
# send get request
cedar_resp <- req_perform(cedar_req)
tmp <- cedar_resp %>% resp_body_json()
# save
write_json(tmp,"tmp/get.json")
# #get limits from tmp
tmp$NTproBNP$Range$`Lower Limit`
tmp$NTproBNP$Range$`Upper Limit`
# update dataquieR metadata
metadata_report$SOFT_LIMITS[3]<- paste0("[",tmp$NTproBNP$Range$`Lower Limit`,";",tmp$NTproBNP$Range$`Upper Limit`,"]")
```
run limit checks again
```{r,warning=FALSE}
limit_checks <- con_limit_deviations(study_data = cohort_synt_DQ,
meta_data = metadata_report,
label_col = "LABEL",
limits = "SOFT_LIMITS")
limit_checks$SummaryPlotList$ntprobnp
```
export generated data
```{r}
if(!dir.exists("output")){dir.create("output")}
write.csv(cohort_synt,"output\\cohort_synt.csv", row.names = FALSE)
```