-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIDA_adnimerge.Rmd
540 lines (435 loc) · 14.5 KB
/
IDA_adnimerge.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
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
---
title: "IDA_adnimerge"
author: "Yibin Feng"
date: "1/31/2022"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Install ADNIMERGE
```{r}
#install.packages("Hmisc")
#install.packages("restricted/ADNIMERGE_0.0.1.tar.gz", repos=NULL, type="source")
```
# Load package
```{r}
rm(list = ls())
library(tidyverse)
library(ggpubr)
library(ggfortify)
library(Hmisc)
library(ADNIMERGE)
```
# Instruction
- ADNIMERGE package contains all ADNI data files
- One of the dataframes is adnimerge which contains commonly used variables.
```{r, eval=FALSE}
help(package = "ADNIMERGE")
```
# Utility function
Define some utility functions for future use here.
```{r}
get_datadic <- function(variables, table = 'ADNIMERGE', all = F){
# Retrieve all info from datadict, or most frequently used info
if (all) {
subset(datadic, TBLNAME == table & FLDNAME %in% variables)
} else {
subset(datadic, TBLNAME == table & FLDNAME %in% variables,
c('FLDNAME', 'TBLNAME', 'TEXT', 'NOTES'))
}
}
```
# IDA of dataframe `adnimerge`
This section does not involve any data preparation.
## Variable names
List all
```{r, eval=FALSE}
names(adnimerge)
```
variable names containing ".bl", note `Years.bl` and `Month.bl` are not baseline values but time-varying
```{r}
names(adnimerge)[grepl(".bl", names(adnimerge))]
```
variable names not containing ".bl"
```{r}
names(adnimerge)[!grepl(".bl", names(adnimerge))]
```
Baseline covariates
```{r}
vars_baseline <- c("AGE", "PTGENDER", "PTEDUCAT", "PTETHCAT", "PTRACCAT", "PTMARRY", "APOE4")
get_datadic(vars_baseline)
```
## Identifier for subject
Look up the datadic.
```{r}
get_datadic(variables = "RID")
```
Count unique RIDs.
```{r}
print(paste("Number of subjects in adnimerge =", length(unique(adnimerge$RID))))
```
## Multiple identifier for time
There are multiple variables related to time i.e. "VISCODE", "EXAMDATE", "Years.bl", "Month.bl", "Month", "M".
What are the differences?
Look up datadic
```{r}
get_datadic(variables = c("VISCODE", "EXAMDATE", "Years.bl", "Month.bl", "Month", "M"))
```
We summarize the findings here and elaborate them further below:
- `Month`, `M` are not based on exact date.
- `Month` is factor, but `M` is numeric (integer).
- `Month` may differs from `M`, despite both are said to be months since baseline.
- `M` is mostly consistent with `VISCODE` e.g. M==6 and VISCODE==m06.
- `Years.bl`, `Month.bl` description are missing in datadic.
- `Years.bl`, `Month.bl` do not represent a baseline value and are time-varying, contrary to other variables with `.bl` suffix e.g. `EXAMDATE.bl`.
- `Years.bl`, `Month.bl` measures the actual follow-up time (in years or months) after baseline `EXAMDATE`.
```{r}
adnimerge %>%
select("VISCODE", "Month", "M", "EXAMDATE", "EXAMDATE.bl", "Years.bl", "Month.bl") %>%
str()
```
"M" is consistent with the "VISCODE"
```{r}
tmp_viscode <- adnimerge$VISCODE
tmp_viscode[tmp_viscode == "bl"] <- "m00" # Set bl to m00, so format is consistent with m06, m12 etc.
tmp_viscode <- sapply(tmp_viscode, function(x) {
as.numeric(strsplit(x, split = "m")[[1]][2])
})
print(paste("[Check] all VISCODE identical to M:", all(tmp_viscode == adnimerge$M)))
```
Construct the Years.bl and Month.bl using EXAMDATE:
```{r}
adnimerge %>%
filter(RID == 2) %>%
select("VISCODE", "Month", "M", "EXAMDATE", "EXAMDATE.bl", "Years.bl", "Month.bl") %>%
mutate(verify_Days.bl = as.numeric(EXAMDATE - EXAMDATE.bl),
verify_Years.bl = verify_Days.bl / 365.25,
verify_Months.bl = verify_Years.bl * 12)
```
Now we compute the differences using our deduced conversion formula:
Years.bl = (EXAMDATE - EXAMDATE.bl) / 365.25?
```{r}
# Deduced that two should be convertible from Month.bl = (EXAMDATE - EXAMDATE.bl) / 366 * 12
adnimerge %>%
select(RID, EXAMDATE.bl, EXAMDATE, Years.bl) %>%
mutate(time_followup = as.numeric((EXAMDATE - EXAMDATE.bl) / 365.25)) %>%
mutate(abs_diff = abs(time_followup - Years.bl)) %>%
select(abs_diff) %>% colSums()
```
Month.bl = (EXAMDATE - EXAMDATE.bl) / 365.25 * 12?
```{r}
# Deduced that two should be convertible from Month.bl = (EXAMDATE - EXAMDATE.bl) / 366 * 12
adnimerge %>%
select(RID, EXAMDATE.bl, EXAMDATE, Month.bl) %>%
mutate(time_followup = as.numeric((EXAMDATE - EXAMDATE.bl) / 365.25 * 12)) %>%
mutate(abs_diff = abs(time_followup - Month.bl)) %>%
select(abs_diff) %>% colSums()
```
One more thing, are `M` and `Month` identical?
Not always!
```{r}
(adnimerge$Month == adnimerge$M) %>%
table()
```
Contrast the differences between `M` and `Month`:
levels in `Month`:
```{r}
as.numeric(levels(adnimerge$Month))
```
unique values of `M`:
```{r}
sort(unique(adnimerge$M))
```
When does these two disagree?
For example, we observe that for subject RID 31, `Month` == 60 and 72 appear twice.
We consider the `Month.bl` equivalent to `EXAMDATE` as the ground truth.
The first `Month` == 60 matches with the `Month.bl` exactly, but there is mismatch with `VISCODE` == 54.
The second `Month` == 60 differs from `Month.bl` by 1 month, and matches the `VISCODE`.
A guess is that `Month` is factorize on a predefined, regular grid, and the actual visit time is approximated to the nearest grid.
There is a risk double entry at one time may cause data error in some modeling using regular time grid, e.g. MFPCCox
```{r}
adnimerge %>%
select(RID, VISCODE, EXAMDATE, Month.bl, Month, M) %>%
mutate(is_difference = !(Month == M)) %>%
filter(RID == 31, M > 42, M < 84) # Pick subject RID 31 as example
```
Look at data gathered at two `Month` == 60 took place on 2010-10-28 and 2010-12-01:
- From two different COLPROT (collection protocol) ADNI1 and ADNIGO
- No measurement values for first visit.
```{r}
adnimerge %>%
filter(RID == 31, Month == 60) %>%
select(names(adnimerge)[!grepl(".bl", names(adnimerge))]) # Exclude .bl
```
Similar at two `Month` == 72:
- From two different COLPROT (collection protocol) ADNIGO and ADNI2
- No measurement values for first visit.
```{r}
adnimerge %>%
filter(RID == 31, Month == 72) %>%
select(names(adnimerge)[!grepl(".bl", names(adnimerge))]) # Exclude .bl
```
List all subjects with this `Month` != `M` issue to have a broader look:
```{r}
adnimerge %>%
select(RID, VISCODE, EXAMDATE, Month.bl, Month, M) %>%
filter(!(Month == M))
```
Subset potentially problematic observations with this problem to probe into observations:
- Not all cases related to changing COLPROT, see RID 2
- Not all case involve double entry, see RID 29
### Considerations for data cleaning
Two problems we found when selecting the time label, first is double entry, second is mismatch between M, Month, Month.bl
1) whether double entry will happen on `Month` or `M`?'
Both.
In all cases except one, the `Month` count is less than number of visits, implying double entry.
In one case, RID 6014, the `M` count is less than number of visits, n_visit = 4, n_M = 3
2) which one is closer to the ground truth `EXAMDATE` when used as time label?
`Month` by eye test
3) at times of double entry, whether these entries contain different values or single value?
RID 98, `Month` == 12 has two entries, contain different values.
RID 6014, `M` == 0 has two entries, only one entry contains values, other one is NAs.
3) if we have to use a regular time grid for some methods, whether to use `Month` or `M`?
`M` (numeric, stick to viscode, more different from EXAMDATE) or `Month` (closer to truth, need to handle different values at same time label, factor)
```{r}
tmp <- adnimerge %>%
select(RID, VISCODE, EXAMDATE, Month.bl, Month, M) %>%
filter(!(Month == M)) %>%
select(RID, Month)
tmp2 <- lapply(1:nrow(tmp), function(i) {
adnimerge %>%
filter(RID == tmp[i, "RID"], Month == tmp[i, "Month"]) %>%
select(Month.bl, names(adnimerge)[!grepl(".bl", names(adnimerge))]) # Exclude .bl
})
tmp3 <- do.call(rbind, tmp2) %>%
select(RID, COLPROT, ORIGPROT, VISCODE, M, Month, Month.bl, EXAMDATE, everything())
tmp3
```
descriptive statistics of this issue:
```{r}
adnimerge %>%
#filter(RID == 31) %>%
group_by(RID) %>%
dplyr::summarize(
n_visit = n(),
n_Month = length(unique(Month)),
n_M = length(unique(M)),
visit_Month = n_visit == n_Month,
visit_M = n_visit == n_M,
all_n_equal = visit_Month & visit_M
) %>%
filter(!all_n_equal)
```
Probe RID 6014, we find that:
- `M` == 0 appears twice and `EXAMDATE` are close
- `Month` == 3 is way off and incorrect to describe EXAMDATE
- good news is that we can drop the one filled with NAs `Month` == 3
- but no idea why it exists at first place?
```{r}
adnimerge %>%
filter(RID == 6014) %>%
select(names(adnimerge)[!grepl(".bl", names(adnimerge))]) %>% # Exclude .bl
select(Month, M, VISCODE, EXAMDATE, everything())
```
## Ethnicity and race
Underrepresented. Make sure to mention this in thesis.
Why white is dominant in this population?
What is the difference between ethnicity Hisp/Latino and other?
```{r}
adnimerge %>%
select(PTETHCAT, PTRACCAT) %>%
summary()
```
## Age
- There are NAs in baseline age, need to be cleaned
```{r}
adnimerge %>%
select(AGE) %>%
summary()
```
## Follow up period
```{r}
adnimerge %>%
group_by(RID) %>%
dplyr::summarize(last.visit = max(Years.bl),
ORIGPROT = unique(ORIGPROT)) %>%
ggplot() +
geom_histogram(aes(last.visit),
binwidth = 1) +
facet_wrap(vars(ORIGPROT)) +
labs(title = "Observation period in each ADNI phase",
x = "Years since baseline")
```
```{r}
adnimerge %>%
group_by(RID) %>%
dplyr::summarize(last.visit = max(Years.bl),
ORIGPROT = unique(ORIGPROT)) %>%
ggplot() +
geom_histogram(aes(last.visit),
binwidth = 1) +
facet_wrap(vars(ORIGPROT)) +
labs(title = "Observation period by each ADNI phase",
x = "Years since baseline")
```
```{r}
adnimerge %>%
group_by(RID) %>%
dplyr::summarize(last.visit = max(Years.bl),
ORIGPROT = unique(ORIGPROT),
DX.bl = unique(DX.bl)) %>%
ggplot() +
geom_jitter(aes(last.visit, DX.bl,
color = DX.bl)) +
facet_wrap(vars(ORIGPROT)) +
labs(title = "Observation period by baseline diagnosis DX.bl in different ADNI phase",
x = "Year since baseline")
```
### Consideration for data cleaning
Exclude subjects DX == NA or Dementia at baseline
```{r}
adnimerge %>%
group_by(RID) %>%
dplyr::summarize(last.visit = max(Years.bl),
ORIGPROT = unique(ORIGPROT)) %>%
left_join(adnimerge %>% filter(VISCODE == "bl") %>% select(RID, DX), by = "RID") %>%
ggplot() +
geom_jitter(aes(last.visit, DX,
color = DX),
alpha = 0.5) +
facet_wrap(vars(ORIGPROT)) +
labs(title = "Observation period by diagnosis DX at baseline in different ADNI phase",
subtitle = "baseline filter by VISCODE = bl",
x = "Years since baseline")
```
```{r}
# distribution of follow-up period
adnimerge %>%
group_by(RID) %>%
dplyr::summarize(fup.period = max(Years.bl)) %>%
select(fup.period) %>%
summary()
```
## Average distinct values per variables
```{r, eval=FALSE}
# Compute average distinct values per variables
adnimerge %>%
group_by(RID) %>%
select(names(adnimerge)[!grepl(".bl", names(adnimerge))]) %>% # Exclude .bl
dplyr::summarise(across(everything(), n_distinct)) %>%
dplyr::select(-RID) %>%
dplyr::summarise(across(everything(), mean)) %>%
pivot_longer(cols = everything(), names_to = "var_name", values_to = "avg_distinct_values") %>%
arrange(desc(avg_distinct_values)) %>%
View()
```
```{r, eval=FALSE}
# Compute max distinct values per variables
adnimerge %>%
group_by(RID) %>%
select(names(adnimerge)[!grepl(".bl", names(adnimerge))]) %>% # Exclude .bl
dplyr::summarise(across(everything(), n_distinct)) %>%
dplyr::select(-RID) %>%
dplyr::summarise(across(everything(), max)) %>%
pivot_longer(cols = everything(), names_to = "var_name", values_to = "max_distinct_values") %>%
arrange(desc(max_distinct_values)) %>%
View()
```
## Number of visits
### Consideration for data cleaning
Exclude subjects with only single visit i.e. at baseline only, no repeated measurement
```{r}
ids_one_visit <- adnimerge %>%
group_by(RID) %>%
dplyr::summarize(last.visit = max(M)) %>%
filter(last.visit == 0) %>%
select(RID) %>%
unlist()
adnimerge %>%
filter(RID %in% ids_one_visit) %>%
select(RID, DX.bl, DX)
```
```{r}
adnimerge %>%
group_by(RID) %>%
dplyr::summarize(n.visits = n()) %>%
ggplot() +
geom_bar(aes(n.visits)) +
labs(
title = "Number of visits",
subtitle = "But not every visit has measurements, observation is irregular!"
)
```
## NAs in baseline covariates
```{r}
vars_baseline <- c("AGE", "PTGENDER", "PTEDUCAT", "PTETHCAT", "PTRACCAT", "PTMARRY", "APOE4")
adnimerge %>%
select(vars_baseline) %>%
summary()
```
## Event of interest i.e. diagnosis
```{r, eval=FALSE}
# Check DX at baseline
adnimerge %>% filter(VISCODE == "bl") %>% select(DX) %>% summarytools::freq()
```
### Who are the DX.bl == "AD"?
```{r}
adnimerge %>%
filter(DX.bl == "AD") %>%
select(RID, DX.bl, DX, EXAMDATE, M) %>%
tail()
```
```{r}
# At baseline row
with(adnimerge %>%
filter(VISCODE=="bl"), summarytools::ctable(DX.bl, DX))
```
```{r}
# DX.bl == AD will sometimes lead to DX == MCI rather then Dementia
with(adnimerge, summarytools::ctable(DX.bl, DX))
```
```{r}
# Further investigate
# Participant 162, 167, 739, 4641, 4845
adnimerge %>%
filter(DX.bl == "AD", DX == "MCI") %>%
select(RID, EXAMDATE, M, DX.bl, DX)
```
```{r}
# Reversion seems to be rare but possible according to ADNI slide?
adnimerge %>%
filter(RID %in% c(162, 167, 739, 4641, 4845)) %>%
select(RID, EXAMDATE, M, DX.bl, DX) %>%
ggplot() +
geom_point(aes(M, DX, color = DX)) +
labs(title = "PTs with AD at baseline but with MCI instead of Dementia at follow up",
caption = "Baseline refers to DX.bl, follow up refers to DX",
x = "Month since baseline") +
facet_wrap(vars(RID)) +
theme(legend.position = "top")
```
# Check dropout
```{r}
# Early Discontinuation and Withdrawal
#ADNIMERGE::treatdis %>% head() %>% View()
ADNIMERGE::treatdis %>%
# filter(RID %in% df.surv_preds$RID) %>%
select(WDREASON) %>%
table()
```
```{r}
# Not all subjects in cleaned data found in Early Discontinuation and Withdrawal
# Likely reason: not drop out yet? new RIDs not included => up to ADNI2
ADNIMERGE::treatdis %>%
# filter(RID %in% df.surv_preds$RID) %>%
nrow() # Check n size
# Count death frequency
sapply(
ADNIMERGE::treatdis %>%
# filter(RID %in% df.surv_preds$RID) %>%
select(WDREASON),
function(reason){
grepl("Death", reason)
}) %>% sum()
```