-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathlongCombat_example.R
392 lines (360 loc) · 13.6 KB
/
longCombat_example.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
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
###########################################################
# longCombat package examples
# JCBeer [email protected]
# 13 Sept 2021
###########################################################
#################################
# install longCombat package
#################################
# install.packages('devtools')
# devtools::install_github("jcbeer/longCombat")
#################################
# load longCombat package
#################################
library(longCombat)
# check documentation
?longCombat
#################################
# install and load invgamma & lmer package
#################################
# install.packages('invgamma')
library(invgamma)
library(lme4)
#################################
# simulate data to run the functions
#################################
# 100 subjects with 5 time points each
# 8 scanners / batches
# 20 features
#################################
# set random seed
set.seed(1)
# simulate the covariates
simdata <- data.frame(
subid=rep(1:100, each=5),
age=rep(sample(c(20:60), 100, replace=TRUE), each=5),
diagnosis=rep(c(0,1), each=250),
time=rep(0:4, times=100)
)
# define 4 batch patterns
# each column of this matrix represents a batch pattern over time
batch.patterns <- matrix(c(1,1,1,2,2,
3,3,4,4,5,
6,6,6,6,6,
7,7,8,8,8),
ncol=4)
# randomly sample 100 batch patterns
batch.pattern.sample <- sample(1:4, 100, replace=TRUE)
simdata$batch <- as.vector(batch.patterns[,batch.pattern.sample])
# simulate the brain features
features <- matrix(rnorm(100*5*20), nrow=500)
# simulate additive batch effects (normally distributed)
gamma <- runif(n=8, min=-5, max=5)
tau <- runif(n=8, min=0.1, max=0.3)
batch.add <- matrix(c(
rnorm(mean=gamma[1], sd=tau[1], n=20),
rnorm(mean=gamma[2], sd=tau[2], n=20),
rnorm(mean=gamma[3], sd=tau[3], n=20),
rnorm(mean=gamma[4], sd=tau[4], n=20),
rnorm(mean=gamma[5], sd=tau[5], n=20),
rnorm(mean=gamma[6], sd=tau[6], n=20),
rnorm(mean=gamma[7], sd=tau[7], n=20),
rnorm(mean=gamma[8], sd=tau[8], n=20)),
ncol=8)
# simulate multiplicative batch effects (inverse gamma distributed)
lambda <- sample(c(2, 3), 8, replace=TRUE)
theta <- sample(c(0.5, 1), 8, replace=TRUE)
batch.mult <- matrix(c(
rinvgamma(n=20, shape=lambda[1], scale=theta[1]),
rinvgamma(n=20, shape=lambda[2], scale=theta[2]),
rinvgamma(n=20, shape=lambda[3], scale=theta[3]),
rinvgamma(n=20, shape=lambda[4], scale=theta[4]),
rinvgamma(n=20, shape=lambda[5], scale=theta[5]),
rinvgamma(n=20, shape=lambda[6], scale=theta[6]),
rinvgamma(n=20, shape=lambda[7], scale=theta[7]),
rinvgamma(n=20, shape=lambda[8], scale=theta[8])),
ncol=8)
# add / multiply batch effects to the features
for(i in 1:500){
features[i,] <- features[i,]*batch.mult[,simdata$batch[i]] + batch.add[,simdata$batch[i]]
}
# add covariate effects to the features
features <- features - 0.1*simdata$age + simdata$diagnosis - 0.5*simdata$time - 2*simdata$diagnosis*simdata$time
# add subject random effect to the features (will be the same across features in this case)
features <- features + rep(rnorm(n=100), each=5)
# save feature names
featurenames <- paste0('feature', 1:20)
colnames(features) <- featurenames
# combine into one data frame
simdata <- data.frame(simdata, features)
# remove some stuff no longer needed
rm('features', 'batch.patterns', 'batch.pattern.sample', 'i')
#################################
# longCombat functions:
#################################
# batchTimeViz() -- visualize change in batch over time
# batchBoxplot() -- to visualize residuals across batches
# trajPlot() -- visualize trajectories
# addTest() -- test for additive scanner effects
# multTest() -- test for multiplicative scanner effects
# longCombat() -- apply longitudinal ComBat
#################################
# type e.g. ?longCombat to get further documentation
#################################
# these examples (ranef='(1|subid)') are for random subject intercept
# use ranef='(1 + time|subid)' to add random slope
#################################
# batchTimeViz() -- visualize change in batch over time
#################################
batchTimeViz(batchvar='batch',
timevar='time',
data=simdata)
#################################
# batchBoxplot() -- to visualize residuals across batches
# can do for each feature you are interested in
#################################
# make batch boxplot for feature1, do not adjust for batch
batchBoxplot(idvar='subid',
batchvar='batch',
feature='feature1',
formula='age + diagnosis*time',
ranef='(1|subid)',
data=simdata,
colors=1:8)
# make batch boxplot for feature2, do not adjust for batch
batchBoxplot(idvar='subid',
batchvar='batch',
feature='feature2',
formula='age + diagnosis*time',
ranef='(1|subid)',
data=simdata,
colors=1:8)
# make batch boxplot for feature2, DO adjust for batch
# order by increasing batch variance
# (centers boxplot means on the zero line)
batchBoxplot(idvar='subid',
batchvar='batch',
feature='feature2',
formula='age + diagnosis*time',
ranef='(1|subid)',
data=simdata,
adjustBatch=TRUE,
orderby='var',
colors=1:8)
#################################
# trajPlot() -- visualize trajectories
#################################
# for everyone
trajPlot(idvar='subid',
timevar='time',
feature='feature2',
batchvar='batch',
data=simdata,
point.col=simdata$batch,
line.col=simdata$diagnosis[!duplicated(simdata$subid)]+1)
# for only diagnosis=0
trajPlot(idvar='subid',
timevar='time',
feature='feature2',
batchvar='batch',
data=simdata[simdata$diagnosis==0,],
point.col=simdata$batch[simdata$diagnosis==0])
# for only diagnosis=1
trajPlot(idvar='subid',
timevar='time',
feature='feature2',
batchvar='batch',
data=simdata[simdata$diagnosis==1,],
point.col=simdata$batch[simdata$diagnosis==1],
line.col=rep(2,250))
#################################
# addTest() -- test for additive scanner effects
#################################
addTestTable <- addTest(idvar='subid',
batchvar='batch',
features=featurenames,
formula='age + diagnosis*time',
ranef='(1|subid)',
data=simdata)
# when we generate data with set.seed(1)
# feature5 has largest additive scanner effects
# (since it is the first row in the addTestTable)
# check boxplot to see this
batchBoxplot(idvar='subid',
batchvar='batch',
feature='feature5',
formula='age + diagnosis*time',
ranef='(1|subid)',
data=simdata,
colors=1:8,
title='Feature 5')
# compare with feature 2
batchBoxplot(idvar='subid',
batchvar='batch',
feature='feature2',
formula='age + diagnosis*time',
ranef='(1|subid)',
data=simdata,
colors=1:8,
title='Feature 2')
#################################
# multTest() -- test for multiplicative scanner effects
#################################
multTestTable <- multTest(idvar='subid',
batchvar='batch',
features=featurenames,
formula='age + diagnosis*time',
ranef='(1|subid)',
data=simdata)
# when we generate data with set.seed(1)
# feature3 has largest multiplicative scanner effects
# (since it is the first row in the multTestTable)
# check boxplot to see this
# (we will adjust for batch and order by variance
# to best see the multiplicative batch effects)
batchBoxplot(idvar='subid',
batchvar='batch',
feature='feature3',
formula='age + diagnosis*time',
ranef='(1|subid)',
data=simdata,
colors=1:8,
adjustBatch=TRUE,
orderby='var',
title='Feature 3')
# compare with feature1
batchBoxplot(idvar='subid',
batchvar='batch',
feature='feature1',
formula='age + diagnosis*time',
ranef='(1|subid)',
data=simdata,
colors=1:8,
adjustBatch=TRUE,
orderby='var',
title='Feature 1')
#################################
# longCombat() -- apply longitudinal ComBat
#################################
simdata_combat <- longCombat(idvar='subid',
timevar='time',
batchvar='batch',
features=featurenames,
formula='age + diagnosis*time',
ranef='(1|subid)',
data=simdata)
#################################
# get the harmonized data
simdata_harmonized <- simdata_combat$data_combat
# save combat feature names
featurenames.combat <- names(simdata_harmonized)[4:23]
# merge with original dataframe
simdata <- merge(simdata, simdata_harmonized[,c(1,2,4:23)], by=c('subid', 'time'))
#################################
# test for additive scanner effects in combatted data
#################################
addTestTableCombat <- addTest(idvar='subid',
batchvar='batch',
features=featurenames.combat,
formula='age + diagnosis*time',
ranef='(1|subid)',
data=simdata)
# there are still some significant additive batch effects (p<0.05)
# but p-values tend to be larger (-log10(p-values are smaller)) in overall distribution
boxplot(-log(as.numeric(addTestTable$`KR p-value`), base=10),
-log(as.numeric(addTestTableCombat$`KR p-value`), base=10),
ylim=c(0, 8),
las=1,
ylab='additive batch effect -log10(p-value)',
names=c('before ComBat', 'after ComBat'))
# check feature 5 boxplot before combat
batchBoxplot(idvar='subid',
batchvar='batch',
feature='feature5',
formula='age + diagnosis*time',
ranef='(1|subid)',
data=simdata,
colors=1:8,
title='feature 5 before combat')
# check feature 5 boxplot after combat
batchBoxplot(idvar='subid',
batchvar='batch',
feature='feature5.combat',
formula='age + diagnosis*time',
ranef='(1|subid)',
data=simdata,
colors=1:8,
title='feature 5 after combat')
#################################
# test for multiplicative scanner effects in combatted data
#################################
multTestTableCombat <- multTest(idvar='subid',
batchvar='batch',
features=featurenames.combat,
formula='age + diagnosis*time',
ranef='(1|subid)',
data=simdata)
# there are still some significant multiplicative batch effects (p<0.05)
# but p-values tend to be larger (-log10(p-values are smaller)) in overall distribution
boxplot(-log(as.numeric(multTestTable$`p-value`), base=10),
-log(as.numeric(multTestTableCombat$`p-value`), base=10),
las=1,
ylab='multiplicative batch effect -log10(p-value)',
names=c('before ComBat', 'after ComBat'))
# check feature3 boxplot before combat
batchBoxplot(idvar='subid',
batchvar='batch',
feature='feature3',
formula='age + diagnosis*time',
ranef='(1|subid)',
data=simdata,
colors=1:8,
adjustBatch=TRUE,
orderby='var',
title='Feature 3 before ComBat')
# check feature3 boxplot after combat
batchBoxplot(idvar='subid',
batchvar='batch',
feature='feature3.combat',
formula='age + diagnosis*time',
ranef='(1|subid)',
data=simdata,
colors=1:8,
adjustBatch=TRUE,
orderby='var',
title='Feature 3 after ComBat')
#################################
# plot trajectories before and after combat
#################################
par(mfrow=c(1,2))
trajPlot(idvar='subid',
timevar='time',
feature='feature2',
batchvar='batch',
data=simdata,
ylimits=c(-30, 12),
title='feature 2 before combat',
point.col=simdata$batch,
line.col=simdata$diagnosis[!duplicated(simdata$subid)]+1)
trajPlot(idvar='subid',
timevar='time',
feature='feature2.combat',
batchvar='batch',
data=simdata,
ylimits=c(-30,12),
title='feature 2 after combat',
point.col=simdata$batch,
line.col=simdata$diagnosis[!duplicated(simdata$subid)]+1)
#################################
# fit LME models before / after ComBat
#################################
# before ComBat
feature5.fit <- lmer(feature5 ~ age + diagnosis*time + (1|subid), data=simdata)
feature5.batch.fit <- lmer(feature5 ~ age + diagnosis*time + as.factor(batch) + (1|subid), data=simdata)
# after ComBat
feature5combat.fit <- lmer(feature5.combat ~ age + diagnosis*time + (1|subid), data=simdata)
feature5combat.batch.fit <- lmer(feature5.combat ~ age + diagnosis*time + as.factor(batch) + (1|subid), data=simdata)
summary(feature5.fit)
summary(feature5.batch.fit)
summary(feature5combat.fit)
summary(feature5combat.batch.fit)