-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
512 lines (512 loc) · 20.7 KB
/
.Rhistory
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
MAX(start_ordinal) OVER (PARTITION BY person_id, ingredient_concept_id
ORDER BY event_date, event_type ROWS unbounded preceding) AS start_ordinal,
-- this pulls the current START down from the prior rows so that the NULLs
-- from the END DATES will contain a value we can compare with
ROW_NUMBER() OVER (PARTITION BY person_id, ingredient_concept_id
ORDER BY event_date, event_type) AS overall_ord
-- this re-numbers the inner UNION so all rows are numbered ordered by the event date
FROM (
-- select the start dates, assigning a row number to each
SELECT person_id, ingredient_concept_id, drug_exposure_start_date AS event_date,
-1 AS event_type,
ROW_NUMBER() OVER (PARTITION BY person_id, ingredient_concept_id
ORDER BY drug_exposure_start_date) AS start_ordinal
FROM ctePreDrugTarget
UNION ALL
SELECT person_id, ingredient_concept_id, drug_exposure_end_date, 1 AS event_type, NULL
FROM ctePreDrugTarget
)")
cteSubExposureEndDates %>%
mutate(is_valid_end_date = (2 * start_ordinal) - overall_ord == 0)
cteSubExposureEndDates <- cteSubExposureEndDates %>%
filter(2*start_ordinal == overall_ord) %>%
rename(end_date = event_date)
cteDrugExposureEnds <- sqldf::sqldf("
SELECT
dt.person_id
, dt.ingredient_concept_id as drug_concept_id
, dt.drug_exposure_start_date
, MIN(e.end_date) AS drug_sub_exposure_end_date
FROM ctePreDrugTarget dt
JOIN cteSubExposureEndDates e
ON dt.person_id = e.person_id
AND dt.ingredient_concept_id = e.ingredient_concept_id
AND e.end_date >= dt.drug_exposure_start_date
GROUP BY
dt.drug_exposure_id
, dt.person_id
, dt.ingredient_concept_id
, dt.drug_exposure_start_date
")
cteDrugExposureEnds
cteSubExposures <- sqldf::sqldf("
SELECT
ROW_NUMBER() OVER (PARTITION BY person_id, drug_concept_id, drug_sub_exposure_end_date ORDER BY person_id) as row_number,
person_id,
drug_concept_id,
MIN(drug_exposure_start_date) AS drug_sub_exposure_start_date,
drug_sub_exposure_end_date,
COUNT(*) AS drug_exposure_count
FROM cteDrugExposureEnds
GROUP BY person_id, drug_concept_id, drug_sub_exposure_end_date")
cteSubExposures
cteFinalTarget <- sqldf::sqldf("
SELECT
row_number,
person_id,
drug_concept_id as ingredient_concept_id,
drug_sub_exposure_start_date,
drug_sub_exposure_end_date, drug_exposure_count,
drug_sub_exposure_end_date - drug_sub_exposure_start_date AS days_exposed
FROM cteSubExposures
")
cteFinalTarget
cteEndDates <- sqldf::sqldf("
SELECT person_id, ingredient_concept_id, event_date, event_type,
MAX(start_ordinal) OVER (PARTITION BY person_id, ingredient_concept_id
ORDER BY event_date, event_type ROWS UNBOUNDED PRECEDING) AS start_ordinal,
ROW_NUMBER() OVER (PARTITION BY person_id, ingredient_concept_id ORDER BY event_date, event_type) AS overall_ord
FROM (
-- select the start dates, assigning a row number to each
SELECT person_id, ingredient_concept_id, drug_sub_exposure_start_date AS event_date,
-1 AS event_type,
ROW_NUMBER() OVER (PARTITION BY person_id, ingredient_concept_id
ORDER BY drug_sub_exposure_start_date) AS start_ordinal
FROM cteFinalTarget
UNION ALL
-- pad the end dates by 30 to allow a grace period for overlapping ranges.
SELECT person_id, ingredient_concept_id, drug_sub_exposure_end_date + 30, 1 AS event_type, NULL
FROM cteFinalTarget
)")
cteEndDates
cteEndDates <- cteEndDates %>%
filter(2*start_ordinal == overall_ord) %>%
# unpad the end dates
mutate(event_date = event_date - 30) %>%
select(person_id, ingredient_concept_id, end_date = event_date)
cteDrugEraEnds <- sqldf::sqldf("
SELECT
ft.person_id
, ft.ingredient_concept_id as drug_concept_id
, ft.drug_sub_exposure_start_date
, MIN(e.end_date) AS drug_era_end_date
, drug_exposure_count
, days_exposed
FROM cteFinalTarget ft
JOIN cteEndDates e
ON ft.person_id = e.person_id
AND ft.ingredient_concept_id = e.ingredient_concept_id
AND e.end_date >= ft.drug_sub_exposure_start_date
GROUP BY
ft.person_id
, ft.ingredient_concept_id
, ft.drug_sub_exposure_start_date
, drug_exposure_count
, days_exposed")
cteDrugEraEnds
sqldf::sqldf(
"SELECT
row_number()over(order by person_id) drug_era_id
, person_id
, drug_concept_id
, MIN(drug_sub_exposure_start_date) AS drug_era_start_date
, drug_era_end_date
, SUM(drug_exposure_count) AS drug_exposure_count
, (drug_era_end_date - MIN(drug_sub_exposure_start_date)) - SUM(days_exposed) as gap_days
FROM cteDrugEraEnds dee
GROUP BY person_id, drug_concept_id, drug_era_end_date;")
concept %>% filter(CONCEPT_ID %in% c(1127433, 1127078))
concept %>% filter(CONCEPT_ID == 1125315)
knitr::opts_chunk$set(echo = TRUE, message = FALSE)
library(dplyr)
library(Eunomia)
cd <- getEunomiaConnectionDetails()
con <- DatabaseConnector::connect(cd)
concept <- dbGetQuery(con, "select * from concept") %>% tibble()
concept_ancestor <- dbGetQuery(con, "select * from concept_ancestor") %>% tibble()
DatabaseConnector::disconnect(con)
# concept_ancestor %>% filter(ANCESTOR_CONCEPT_ID == 1125315)
drug_exposure <- tribble(
~drug_exposure_id, ~person_id, ~drug_concept_id, ~drug_exposure_start_date, ~drug_exposure_end_date, ~days_supply,
1, 1, 1127433, 1, 50, NA_real_,
2, 1, 1127078, 10, 60, NA_real_,
3, 1, 1127078, 80, 120, NA_real_
)
drug_exposure
concept %>% filter(CONCEPT_ID %in% c(1127433, 1127078))
ctePreDrugTarget <- sqldf::sqldf("
SELECT
d.drug_exposure_id
, d.person_id
, c.concept_id AS ingredient_concept_id
, d.drug_exposure_start_date AS drug_exposure_start_date
, d.days_supply AS days_supply
, COALESCE(
---NULLIF returns NULL if both values are the same, otherwise it returns the first parameter
NULLIF(drug_exposure_end_date, NULL),
---If drug_exposure_end_date != NULL, return drug_exposure_end_date, otherwise go to next case
NULLIF(drug_exposure_start_date + days_supply, drug_exposure_start_date),
---If days_supply != NULL or 0, return drug_exposure_start_date + days_supply, otherwise go to next case
drug_exposure_start_date + 1
---Add 1 day to the drug_exposure_start_date since there is no end_date or INTERVAL for the days_supply
) AS drug_exposure_end_date
FROM drug_exposure d
JOIN concept_ancestor ca ON ca.descendant_concept_id = d.drug_concept_id
JOIN concept c ON ca.ancestor_concept_id = c.concept_id
WHERE c.vocabulary_id = 'RxNorm' ---8 selects RxNorm from the vocabulary_id
AND c.concept_class_id = 'Ingredient'
AND d.drug_concept_id != 0 ---Our unmapped drug_concept_id's are set to 0, so we don't want different drugs wrapped up in the same era
AND coalesce(d.days_supply,0) >= 0
")
ctePreDrugTarget %>% arrange(drug_exposure_id)
concept %>% filter(CONCEPT_ID == 1125315)
cteSubExposureEndDates <- sqldf::sqldf("
SELECT person_id, ingredient_concept_id, event_date, event_type,
MAX(start_ordinal) OVER (PARTITION BY person_id, ingredient_concept_id
ORDER BY event_date, event_type ROWS unbounded preceding) AS start_ordinal,
-- this pulls the current START down from the prior rows so that the NULLs
-- from the END DATES will contain a value we can compare with
ROW_NUMBER() OVER (PARTITION BY person_id, ingredient_concept_id
ORDER BY event_date, event_type) AS overall_ord
-- this re-numbers the inner UNION so all rows are numbered ordered by the event date
FROM (
-- select the start dates, assigning a row number to each
SELECT person_id, ingredient_concept_id, drug_exposure_start_date AS event_date,
-1 AS event_type,
ROW_NUMBER() OVER (PARTITION BY person_id, ingredient_concept_id
ORDER BY drug_exposure_start_date) AS start_ordinal
FROM ctePreDrugTarget
UNION ALL
SELECT person_id, ingredient_concept_id, drug_exposure_end_date, 1 AS event_type, NULL
FROM ctePreDrugTarget
)")
cteSubExposureEndDates %>%
mutate(is_valid_end_date = (2 * start_ordinal) - overall_ord == 0)
cteSubExposureEndDates <- cteSubExposureEndDates %>%
filter(2*start_ordinal == overall_ord) %>%
rename(end_date = event_date)
cteDrugExposureEnds <- sqldf::sqldf("
SELECT
dt.person_id
, dt.ingredient_concept_id as drug_concept_id
, dt.drug_exposure_start_date
, MIN(e.end_date) AS drug_sub_exposure_end_date
FROM ctePreDrugTarget dt
JOIN cteSubExposureEndDates e
ON dt.person_id = e.person_id
AND dt.ingredient_concept_id = e.ingredient_concept_id
AND e.end_date >= dt.drug_exposure_start_date
GROUP BY
dt.drug_exposure_id
, dt.person_id
, dt.ingredient_concept_id
, dt.drug_exposure_start_date
")
cteDrugExposureEnds
cteSubExposures <- sqldf::sqldf("
SELECT
ROW_NUMBER() OVER (PARTITION BY person_id, drug_concept_id, drug_sub_exposure_end_date ORDER BY person_id) as row_number,
person_id,
drug_concept_id,
MIN(drug_exposure_start_date) AS drug_sub_exposure_start_date,
drug_sub_exposure_end_date,
COUNT(*) AS drug_exposure_count
FROM cteDrugExposureEnds
GROUP BY person_id, drug_concept_id, drug_sub_exposure_end_date")
cteSubExposures
cteFinalTarget <- sqldf::sqldf("
SELECT
row_number,
person_id,
drug_concept_id as ingredient_concept_id,
drug_sub_exposure_start_date,
drug_sub_exposure_end_date, drug_exposure_count,
drug_sub_exposure_end_date - drug_sub_exposure_start_date AS days_exposed
FROM cteSubExposures
")
cteFinalTarget
cteEndDates <- sqldf::sqldf("
SELECT person_id, ingredient_concept_id, event_date, event_type,
MAX(start_ordinal) OVER (PARTITION BY person_id, ingredient_concept_id
ORDER BY event_date, event_type ROWS UNBOUNDED PRECEDING) AS start_ordinal,
ROW_NUMBER() OVER (PARTITION BY person_id, ingredient_concept_id ORDER BY event_date, event_type) AS overall_ord
FROM (
-- select the start dates, assigning a row number to each
SELECT person_id, ingredient_concept_id, drug_sub_exposure_start_date AS event_date,
-1 AS event_type,
ROW_NUMBER() OVER (PARTITION BY person_id, ingredient_concept_id
ORDER BY drug_sub_exposure_start_date) AS start_ordinal
FROM cteFinalTarget
UNION ALL
-- pad the end dates by 30 to allow a grace period for overlapping ranges.
SELECT person_id, ingredient_concept_id, drug_sub_exposure_end_date + 30, 1 AS event_type, NULL
FROM cteFinalTarget
)")
cteEndDates
cteEndDates <- cteEndDates %>%
filter(2*start_ordinal == overall_ord) %>%
# unpad the end dates
mutate(event_date = event_date - 30) %>%
select(person_id, ingredient_concept_id, end_date = event_date)
cteDrugEraEnds <- sqldf::sqldf("
SELECT
ft.person_id
, ft.ingredient_concept_id as drug_concept_id
, ft.drug_sub_exposure_start_date
, MIN(e.end_date) AS drug_era_end_date
, drug_exposure_count
, days_exposed
FROM cteFinalTarget ft
JOIN cteEndDates e
ON ft.person_id = e.person_id
AND ft.ingredient_concept_id = e.ingredient_concept_id
AND e.end_date >= ft.drug_sub_exposure_start_date
GROUP BY
ft.person_id
, ft.ingredient_concept_id
, ft.drug_sub_exposure_start_date
, drug_exposure_count
, days_exposed")
cteDrugEraEnds
sqldf::sqldf(
"SELECT
row_number()over(order by person_id) drug_era_id
, person_id
, drug_concept_id
, MIN(drug_sub_exposure_start_date) AS drug_era_start_date
, drug_era_end_date
, SUM(drug_exposure_count) AS drug_exposure_count
, (drug_era_end_date - MIN(drug_sub_exposure_start_date)) - SUM(days_exposed) as gap_days
FROM cteDrugEraEnds dee
GROUP BY person_id, drug_concept_id, drug_era_end_date;")
library(dplyr)
library(Eunomia)
cd <- getEunomiaConnectionDetails()
con <- DatabaseConnector::connect(cd)
concept <- dbGetQuery(con, "select * from concept") %>% tibble()
concept_ancestor <- dbGetQuery(con, "select * from concept_ancestor") %>% tibble()
DatabaseConnector::disconnect(con)
concept_ancestor %>% filter(ANCESTOR_CONCEPT_ID == 1125315)
con <- DatabaseConnector::connect(cd)
drug_exposure <- tribble(
~drug_exposure_id, ~person_id, ~drug_concept_id, ~drug_exposure_start_date, ~drug_exposure_end_date, ~days_supply,
1, 1, 1127433, 1, 50, NA_real_,
2, 1, 1127078, 10, 60, NA_real_,
3, 1, 1127078, 80, 120, NA_real_
)
drug_exposure
ctePreDrugTarget <- sqldf::sqldf("
SELECT
d.drug_exposure_id
, d.person_id
, c.concept_id AS ingredient_concept_id
, d.drug_exposure_start_date AS drug_exposure_start_date
, d.days_supply AS days_supply
, COALESCE(
---NULLIF returns NULL if both values are the same, otherwise it returns the first parameter
NULLIF(drug_exposure_end_date, NULL),
---If drug_exposure_end_date != NULL, return drug_exposure_end_date, otherwise go to next case
NULLIF(drug_exposure_start_date + days_supply, drug_exposure_start_date),
---If days_supply != NULL or 0, return drug_exposure_start_date + days_supply, otherwise go to next case
drug_exposure_start_date + 1
---Add 1 day to the drug_exposure_start_date since there is no end_date or INTERVAL for the days_supply
) AS drug_exposure_end_date
FROM drug_exposure d
JOIN concept_ancestor ca ON ca.descendant_concept_id = d.drug_concept_id
JOIN concept c ON ca.ancestor_concept_id = c.concept_id
WHERE c.vocabulary_id = 'RxNorm' ---8 selects RxNorm from the vocabulary_id
AND c.concept_class_id = 'Ingredient'
AND d.drug_concept_id != 0 ---Our unmapped drug_concept_id's are set to 0, so we don't want different drugs wrapped up in the same era
AND coalesce(d.days_supply,0) >= 0
")
ctePreDrugTarget %>% arrange(drug_exposure_id)
cteSubExposureEndDates <- cteSubExposureEndDates %>%
filter(2*start_ordinal == overall_ord) %>%
rename(end_date = event_date)
cteSubExposureEndDates <- sqldf::sqldf("
SELECT person_id, ingredient_concept_id, event_date, event_type,
MAX(start_ordinal) OVER (PARTITION BY person_id, ingredient_concept_id
ORDER BY event_date, event_type ROWS unbounded preceding) AS start_ordinal,
-- this pulls the current START down from the prior rows so that the NULLs
-- from the END DATES will contain a value we can compare with
ROW_NUMBER() OVER (PARTITION BY person_id, ingredient_concept_id
ORDER BY event_date, event_type) AS overall_ord
-- this re-numbers the inner UNION so all rows are numbered ordered by the event date
FROM (
-- select the start dates, assigning a row number to each
SELECT person_id, ingredient_concept_id, drug_exposure_start_date AS event_date,
-1 AS event_type,
ROW_NUMBER() OVER (PARTITION BY person_id, ingredient_concept_id
ORDER BY drug_exposure_start_date) AS start_ordinal
FROM ctePreDrugTarget
UNION ALL
SELECT person_id, ingredient_concept_id, drug_exposure_end_date, 1 AS event_type, NULL
FROM ctePreDrugTarget
)")
cteSubExposureEndDates %>%
mutate(is_valid_end_date = (2 * start_ordinal) - overall_ord == 0)
cteSubExposureEndDates <- cteSubExposureEndDates %>%
filter(2*start_ordinal == overall_ord) %>%
rename(end_date = event_date)
cteSubExposureEndDates
cteDrugExposureEnds <- sqldf::sqldf("
SELECT
dt.person_id
, dt.ingredient_concept_id as drug_concept_id
, dt.drug_exposure_start_date
, MIN(e.end_date) AS drug_sub_exposure_end_date
FROM ctePreDrugTarget dt
JOIN cteSubExposureEndDates e
ON dt.person_id = e.person_id
AND dt.ingredient_concept_id = e.ingredient_concept_id
AND e.end_date >= dt.drug_exposure_start_date
GROUP BY
dt.drug_exposure_id
, dt.person_id
, dt.ingredient_concept_id
, dt.drug_exposure_start_date
")
cteDrugExposureEnds
cteSubExposures <- sqldf::sqldf("
SELECT
ROW_NUMBER() OVER (PARTITION BY person_id, drug_concept_id, drug_sub_exposure_end_date ORDER BY person_id) as row_number,
person_id,
drug_concept_id,
MIN(drug_exposure_start_date) AS drug_sub_exposure_start_date,
drug_sub_exposure_end_date,
COUNT(*) AS drug_exposure_count
FROM cteDrugExposureEnds
GROUP BY person_id, drug_concept_id, drug_sub_exposure_end_date")
cteSubExposures
cteFinalTarget <- sqldf::sqldf("
SELECT
row_number,
person_id,
drug_concept_id as ingredient_concept_id,
drug_sub_exposure_start_date,
drug_sub_exposure_end_date, drug_exposure_count,
drug_sub_exposure_end_date - drug_sub_exposure_start_date AS days_exposed
FROM cteSubExposures
")
cteFinalTarget
cteEndDates <- sqldf::sqldf("
SELECT person_id, ingredient_concept_id, event_date, event_type,
MAX(start_ordinal) OVER (PARTITION BY person_id, ingredient_concept_id
ORDER BY event_date, event_type ROWS UNBOUNDED PRECEDING) AS start_ordinal,
ROW_NUMBER() OVER (PARTITION BY person_id, ingredient_concept_id ORDER BY event_date, event_type) AS overall_ord
FROM (
-- select the start dates, assigning a row number to each
SELECT person_id, ingredient_concept_id, drug_sub_exposure_start_date AS event_date,
-1 AS event_type,
ROW_NUMBER() OVER (PARTITION BY person_id, ingredient_concept_id
ORDER BY drug_sub_exposure_start_date) AS start_ordinal
FROM cteFinalTarget
UNION ALL
-- pad the end dates by 30 to allow a grace period for overlapping ranges.
SELECT person_id, ingredient_concept_id, drug_sub_exposure_end_date + 30, 1 AS event_type, NULL
FROM cteFinalTarget
)")
cteEndDates
cteEndDates <- cteEndDates %>%
filter(2*start_ordinal == overall_ord) %>%
# unpad the end dates
mutate(event_date = event_date - 30) %>%
select(person_id, ingredient_concept_id, end_date = event_date)
cteDrugEraEnds <- sqldf::sqldf("
SELECT
ft.person_id
, ft.ingredient_concept_id as drug_concept_id
, ft.drug_sub_exposure_start_date
, MIN(e.end_date) AS drug_era_end_date
, drug_exposure_count
, days_exposed
FROM cteFinalTarget ft
JOIN cteEndDates e
ON ft.person_id = e.person_id
AND ft.ingredient_concept_id = e.ingredient_concept_id
AND e.end_date >= ft.drug_sub_exposure_start_date
GROUP BY
ft.person_id
, ft.ingredient_concept_id
, ft.drug_sub_exposure_start_date
, drug_exposure_count
, days_exposed")
cteDrugEraEnds
sqldf::sqldf(
"SELECT
row_number()over(order by person_id) drug_era_id
, person_id
, drug_concept_id
, MIN(drug_sub_exposure_start_date) AS drug_era_start_date
, drug_era_end_date
, SUM(drug_exposure_count) AS drug_exposure_count
, (drug_era_end_date - MIN(drug_sub_exposure_start_date)) - SUM(days_exposed) as gap_days
FROM cteDrugEraEnds dee
GROUP BY person_id, drug_concept_id, drug_era_end_date;")
con <- DatabaseConnector::connect(cd)
measurement <- dbGetQuery(con, "select * from measurement") %>% tibble()
View(measurement)
concept %>% filter(CONCEPT_ID %in% ctePreDrugTarget$ingredient_concept_id)
cteSubExposureEndDates <- sqldf::sqldf("
SELECT person_id, ingredient_concept_id, event_date AS end_date
FROM cteSubExposureEndDates
WHERE (2 * e.start_ordinal) - e.overall_ord = 0")
cteSubExposureEndDates <- sqldf::sqldf("
SELECT person_id, ingredient_concept_id, event_date, event_type,
MAX(start_ordinal) OVER (PARTITION BY person_id, ingredient_concept_id
ORDER BY event_date, event_type ROWS unbounded preceding) AS start_ordinal,
-- this pulls the current START down from the prior rows so that the NULLs
-- from the END DATES will contain a value we can compare with
ROW_NUMBER() OVER (PARTITION BY person_id, ingredient_concept_id
ORDER BY event_date, event_type) AS overall_ord
-- this re-numbers the inner UNION so all rows are numbered ordered by the event date
FROM (
-- select the start dates, assigning a row number to each
SELECT person_id, ingredient_concept_id, drug_exposure_start_date AS event_date,
-1 AS event_type,
ROW_NUMBER() OVER (PARTITION BY person_id, ingredient_concept_id
ORDER BY drug_exposure_start_date) AS start_ordinal
FROM ctePreDrugTarget
UNION ALL
SELECT person_id, ingredient_concept_id, drug_exposure_end_date, 1 AS event_type, NULL
FROM ctePreDrugTarget
)")
cteSubExposureEndDates %>%
mutate(is_valid_end_date = (2 * start_ordinal) - overall_ord == 0)
cteSubExposureEndDates <- sqldf::sqldf("
SELECT person_id, ingredient_concept_id, event_date AS end_date
FROM cteSubExposureEndDates
WHERE (2 * e.start_ordinal) - e.overall_ord = 0")
cteSubExposureEndDates <- sqldf::sqldf("
SELECT person_id, ingredient_concept_id, event_date AS end_date
FROM cteSubExposureEndDates
WHERE (2 * start_ordinal) - overall_ord = 0")
cteEndDates %>%
mutate(is_valid_end_date = (2 * start_ordinal) - overall_ord == 0)
cteEndDates <- sqldf::sqldf("
SELECT person_id, ingredient_concept_id, event_date, event_type,
MAX(start_ordinal) OVER (PARTITION BY person_id, ingredient_concept_id
ORDER BY event_date, event_type ROWS UNBOUNDED PRECEDING) AS start_ordinal,
ROW_NUMBER() OVER (PARTITION BY person_id, ingredient_concept_id ORDER BY event_date, event_type) AS overall_ord
FROM (
-- select the start dates, assigning a row number to each
SELECT person_id, ingredient_concept_id, drug_sub_exposure_start_date AS event_date,
-1 AS event_type,
ROW_NUMBER() OVER (PARTITION BY person_id, ingredient_concept_id
ORDER BY drug_sub_exposure_start_date) AS start_ordinal
FROM cteFinalTarget
UNION ALL
-- pad the end dates by 30 to allow a grace period for overlapping ranges.
SELECT person_id, ingredient_concept_id, drug_sub_exposure_end_date + 30, 1 AS event_type, NULL
FROM cteFinalTarget
)")
cteEndDates %>%
mutate(is_valid_end_date = (2 * start_ordinal) - overall_ord == 0)
cteEndDates <- sqldf::sqldf("
SELECT person_id, ingredient_concept_id, event_date - 30 AS end_date -- unpad the end date
FROM cteEndDates
WHERE (2 * e.start_ordinal) - e.overall_ord = 0")
cteEndDates <- sqldf::sqldf("
SELECT person_id, ingredient_concept_id, event_date - 30 AS end_date -- unpad the end date
FROM cteEndDates
WHERE (2 * start_ordinal) - overall_ord = 0")