-
Notifications
You must be signed in to change notification settings - Fork 2
/
alff_age_and_sex_fx_T.R
364 lines (271 loc) · 13.9 KB
/
alff_age_and_sex_fx_T.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
######################################
### Initial construction 1/14/2021 ###
######################################
####################
##### Summary ######
####################
#input: asc files, and pnc demographics, cnb, clinical files
#output: 10242 length vector csvs with T and/or p values for vertex-wide regression
#uses: goes vertex by vertex and does regression (coupling by age, sex, cognition, etc), pulls out T and p from these values and sticks it in a vector. The vector can then be used for visualization in matlab
#dependencies: R (3.6.3 is my current default in pmacs)
####################
### Libraries ###
####################
library(mgcv)
library(dplyr)
library(ggplot2)
#####################################################################################
#### Makes the 831x10242 matrices, both left and right ####
#####################################################################################
#set home directory, switch this depending on whether running from PMACS or from home directory
#homedir <- "/Users/eballer/BBL/imco/pmacs/PMACS_remote/"
homedir <- "/project/imco/"
# read in demos
alff_subjDemos <- read.csv(paste0(homedir, "/baller/subjectLists/n831_alff_cbf_finalSample_imageOrder.csv"))
#some verification preprocessing
alff_subjDemos$sex <- as.factor(alff_subjDemos$sex)
alff_subjDemos$race <- as.factor(alff_subjDemos$race)
alff_subjDemos$race2 <- as.factor(alff_subjDemos$race2)
#add osex category for use in gam later
alff_subjDemos$osex <- ordered(alff_subjDemos$sex)
#add psych bifactor scores
psych <- read.csv(paste0(homedir, "/pnc/clinical/n1601_goassess_itemwise_bifactor_scores_20161219.csv"), header = TRUE)
#remove 4factorv2 from title
names(psych) <-gsub("_4factorv2", "", names(psych))
#merge
alff_subjDemos <- merge(alff_subjDemos, psych, by = "bblid")
#drop the scanid.y
alff_subjDemos <- subset(alff_subjDemos, select = -scanid.y)
#rename scanid.x to scanid
names(alff_subjDemos) <- gsub("scanid.x", "scanid", names(alff_subjDemos))
#make list of bblid/scanid
bblid_scanid <- paste0(alff_subjDemos$bblid, "_", alff_subjDemos$datexscanid)
#####################
##### Left Side #####
#####################
#initiate matrix for storage
numrows <- dim(alff_subjDemos)[1]
lh_matrix <- matrix(nrow = numrows, ncol = 10242)
#go through each subject, grab 5th column in asc, transpose and stick in matrix
# output is 831 x 10242 matrix
for (subj in 1:numrows) {
bblid <- alff_subjDemos$bblid[subj]
datexscanid <- alff_subjDemos$datexscanid[subj]
file_path <- paste0(homedir, "/surfaceMaps/alff_from_chead/", bblid, "_", datexscanid, "_lh_fs5_surf.asc")
alff_data <- read.table(file_path, stringsAsFactors = FALSE)
lh_matrix[subj,] <- t(alff_data$V5)
}
#append with demographics
alff_subjDemos_with_lh <- cbind(alff_subjDemos, lh_matrix)
#write output
write.table(alff_subjDemos_with_lh, file = paste0(homedir, "/baller/processed_data/alff_matrices/alff_subjDemos_with_lh_", numrows, "x10242.csv"), sep = ",")
#####################
#### Right Side #####
#####################
#initiate matrix for storage
numrows <- dim(alff_subjDemos)[1]
rh_matrix <- matrix(nrow = numrows, ncol = 10242)
#go through each subject, grab 5th column in asc, transpose and stick in matrix
# output is 831 x 10242 matrix
for (subj in 1:numrows) {
bblid <- alff_subjDemos$bblid[subj]
datexscanid <- alff_subjDemos$datexscanid[subj]
file_path <- paste0(homedir, "/surfaceMaps/alff_from_chead/", bblid, "_", datexscanid, "_rh_fs5_surf.asc")
alff_data <- read.table(file_path, stringsAsFactors = FALSE)
rh_matrix[subj,] <- t(alff_data$V5)
}
#append with demographics
alff_subjDemos_with_rh <- cbind(alff_subjDemos, rh_matrix)
#write output
write.table(alff_subjDemos_with_rh, file = paste0(homedir, "/baller/processed_data/alff_matrices/alff_subjDemos_with_rh_", numrows, "x10242.csv"), sep = ",")
####-----------------------------------------------------------------------------####
####---------------------------End of Part 1- Making matrices---_----------------####
####-----------------------------------------------------------------------------####
#####################################################################################
#### Run regression, both left and right ####
#####################################################################################
#make easier to reference names
lh_cbf_asl <- alff_subjDemos_with_lh #can also read directly from files if you'd like
rh_cbf_asl <- alff_subjDemos_with_rh
#####################################################
# lm/gams #
#####################################################
#initialize vectors for models
hemis <- c("lh", "rh") #hemispheres
models <- c("age", "sex", "age_sex") #models of interest
coeffs <- c("p", "t") #p or t value
corrs <- c("uncor", "fdr") #correction
for (hemi in hemis){
for (model in models) {
for (coeff in coeffs) {
for (corr in corrs) {
vector_init_cmd <- paste0(hemi, "_gam_", model, "_", coeff, "_", corrs, " <- vector(length = 10242)")
print(vector_init_cmd)
eval(parse(text=as.name(vector_init_cmd)))
}
}
}
}
#make linear models as well
lm_models <- c("age")
for (hemi in hemis) {
for (model in lm_models) {
for (coeff in coeffs) {
for (corr in corrs) {
vector_init_cmd <- paste0(hemi, "_lm_", model, "_", coeff, "_", corrs, "<- vector(length= 10242)")
eval(parse(text=as.name(vector_init_cmd)))
}
}
}
}
#######################
######## Left #########
#######################
#get # of items in df for calculation of column)
numcolumns <- dim(lh_cbf_asl)[2]
#run gams models and store info in respective vectors
for (i in 1:10242) {
curcol = (numcolumns - 10242 + i) # will start you counting at the right part of the df
#gams
age_sex_model <- gam(lh_cbf_asl[,curcol] ~ pcaslRelMeanRMSMotion + restRelMeanRMSMotion +
osex + s(ageAtScan1, k = 4, fx = T), data=lh_cbf_asl)
age_sex_intx_model <- gam(lh_cbf_asl[,curcol] ~ pcaslRelMeanRMSMotion + restRelMeanRMSMotion +
osex + s(ageAtScan1, k = 4, fx = T) + s(ageAtScan1, by = osex, k = 4, fx = T), data=lh_cbf_asl)
#lm_for_directionality
age_lm_model <- lm(lh_cbf_asl[,curcol] ~ pcaslRelMeanRMSMotion + restRelMeanRMSMotion + ageAtScan1, data=lh_cbf_asl)
#put pvalue in it's appropriate lm
lh_gam_age_p_uncor[i] <- summary(age_sex_model)$s.table[1,4] #smooth term for ageAtScan1
lh_gam_sex_p_uncor[i] <- summary(age_sex_model)$p.table[4,4] #linear term
lh_gam_age_sex_p_uncor[i] <- summary(age_sex_intx_model)$s.table[2,4] #smooth term for interaction, this was changed
#lm to assess directionality
lh_lm_age_p_uncor[i] <- summary(age_lm_model)$coeff[4,4]
#pull tvalue into its appropriate lm
lh_gam_age_t_uncor[i] <- summary(age_sex_model)$s.table[1,3] #smooth term for ageAtScan1
lh_gam_sex_t_uncor[i] <- summary(age_sex_model)$p.table[4,3] #linear term
lh_gam_age_sex_t_uncor[i] <- summary(age_sex_intx_model)$s.table[2,3] #smooth term for interaction
#lm to assess directionality
lh_lm_age_t_uncor[i] <- summary(age_lm_model)$coeff[4,3]
}
#####################
###### RIGHT ########
#####################
#get # of items in df for calculation of column)
numcolumns <- dim(rh_cbf_asl)[2]
#run gams models and store info in respective vectors
for (i in 1:10242) {
curcol = (numcolumns - 10242 + i) # will start you counting at the right part of the df
#gams
age_sex_model <- gam(rh_cbf_asl[,curcol] ~ pcaslRelMeanRMSMotion + restRelMeanRMSMotion +
osex + s(ageAtScan1, k = 4, fx = T), data=rh_cbf_asl)
age_sex_intx_model <- gam(rh_cbf_asl[,curcol] ~ pcaslRelMeanRMSMotion + restRelMeanRMSMotion +
osex + s(ageAtScan1, k = 4, fx = T) + s(ageAtScan1, by = osex, k = 4, fx = T), data=rh_cbf_asl)
#lm_for_directionality
age_lm_model <- lm(rh_cbf_asl[,curcol] ~ pcaslRelMeanRMSMotion + restRelMeanRMSMotion + ageAtScan1, data=rh_cbf_asl)
#put pvalue in it's appropriate lm
rh_gam_age_p_uncor[i] <- summary(age_sex_model)$s.table[1,4] #smooth term for ageAtScan1
rh_gam_sex_p_uncor[i] <- summary(age_sex_model)$p.table[4,4] #linear term
rh_gam_age_sex_p_uncor[i] <- summary(age_sex_intx_model)$s.table[2,4] #smooth term for interaction, this was changed
#lm to assess directionality
rh_lm_age_p_uncor[i] <- summary(age_lm_model)$coeff[4,4]
#pull tvalue into its appropriate lm
rh_gam_age_t_uncor[i] <- summary(age_sex_model)$s.table[1,3] #smooth term for ageAtScan1
rh_gam_sex_t_uncor[i] <- summary(age_sex_model)$p.table[4,3] #linear term
rh_gam_age_sex_t_uncor[i] <- summary(age_sex_intx_model)$s.table[2,3] #smooth term for interaction
#lm to assess directionality
rh_lm_age_t_uncor[i] <- summary(age_lm_model)$coeff[4,3]
}
#################################################################################
#################################################################################
#####################################################
# results #
#####################################################
#### FDR correction ####
for (hemi in hemis) {
for (model in models) {
hemi_model_p_unc <- paste0(hemi, "_gam_", model, "_p_uncor")
hemi_model_p_fdr <- paste0(hemi, "_gam_", model, "_p_fdr")
print(hemi_model_p_unc)
#correct p values
pfdr <- eval(substitute(p.adjust(i, method="fdr"), list(i = as.name(hemi_model_p_unc))))
#figure out which values are < 0.05 and add to pfdr matrix
pfdr <- as.data.frame(pfdr)
pfdr$sig <- ifelse(pfdr<0.05, 1, 0)
pfdr$sig_noNA <- ifelse(is.na(pfdr$sig), 0, pfdr$sig)
names(pfdr) <- c("pfdr", "sig05", "sig05_noNA")
hemi_model_p_fdr <- as.data.frame(pfdr[,1]) #sig05
#multiply T values by fdr vector to get the list of Ts that are fdr corrected
hemi_model_t_unc <- paste0(hemi, "_gam_", model, "_t_uncor")
hemi_model_t_fdr <- paste0(hemi, "_gam_", model, "_t_fdr"
)
t_df <- eval(substitute(as.data.frame(i), list(i = as.name(hemi_model_t_unc))))
names(t_df) <- c("tval")
t_df$tfdr <- pfdr[,3] * t_df$tval
hemi_model_t_fdr <- as.data.frame(t_df[,2])
#######################
#### write tables #####
#######################
## uncorrected ##
### p
filename <- paste0(homedir, "/baller/results/alff_age_and_sex/", hemi_model_p_unc, ".csv")
write_table_command <- paste0("write.table(x = ", hemi_model_p_unc, ", file = \"", filename,"\", row.names = FALSE, col.names = FALSE)")
eval(parse(text=write_table_command))
### t
filename <- paste0(homedir, "/baller/results/alff_age_and_sex/", hemi_model_t_unc, ".csv")
write_table_command <- paste0("write.table(x = ", hemi_model_t_unc, ", file = \"", filename,"\", row.names = FALSE, col.names = FALSE)")
eval(parse(text=write_table_command))
## corrected ##
### p
filename <- paste0(homedir, "/baller/results/alff_age_and_sex/", hemi, "_gam_", model, "_p_fdr05.csv")
write_table_command <- paste0("write.table(x = ", hemi_model_p_fdr, ", file = \"", filename,"\", row.names = FALSE, col.names = FALSE)")
eval(parse(text=write_table_command))
### t
filename <- paste0(homedir, "/baller/results/alff_age_and_sex/", hemi, "_gam_", model, "_t_fdr05.csv")
write_table_command <- paste0("write.table(x = ", hemi_model_t_fdr, ", file = \"", filename,"\", row.names = FALSE, col.names = FALSE)")
eval(parse(text=write_table_command))
}
}
###### linear model alone #####
#### FDR correction ####
for (hemi in hemis) {
for (model in lm_models) {
hemi_model_p_unc <- paste0(hemi, "_lm_", model, "_p_uncor")
hemi_model_p_fdr <- paste0(hemi, "_lm_", model, "_p_fdr")
print(hemi_model_p_unc)
#correct p values
pfdr <- eval(substitute(p.adjust(i, method="fdr"), list(i = as.name(hemi_model_p_unc))))
#figure out which values are < 0.05 and add to pfdr matrix
pfdr <- as.data.frame(pfdr)
pfdr$sig <- ifelse(pfdr<0.05, 1, 0)
pfdr$sig_noNA <- ifelse(is.na(pfdr$sig), 0, pfdr$sig)
names(pfdr) <- c("pfdr", "sig05", "sig05_noNA")
hemi_model_p_fdr <- as.data.frame(pfdr[,1]) #sig05
#multiply T values by fdr vector to get the list of Ts that are fdr corrected
hemi_model_t_unc <- paste0(hemi, "_lm_", model, "_t_uncor")
hemi_model_t_fdr <- paste0(hemi, "_lm_", model, "_t_fdr")
t_df <- eval(substitute(as.data.frame(i), list(i = as.name(hemi_model_t_unc))))
names(t_df) <- c("tval")
t_df$tfdr <- pfdr[,3] * t_df$tval
hemi_model_t_fdr <- as.data.frame(t_df[,2])
#######################
#### write tables #####
#######################
## uncorrected ##
### p
filename <- paste0(homedir, "/baller/results/alff_age_and_sex/", hemi_model_p_unc, ".csv")
write_table_command <- paste0("write.table(x = ", hemi_model_p_unc, ", file = \"", filename,"\", row.names = FALSE, col.names = FALSE)")
eval(parse(text=write_table_command))
### t
filename <- paste0(homedir, "/baller/results/alff_age_and_sex/", hemi_model_t_unc, ".csv")
write_table_command <- paste0("write.table(x = ", hemi_model_t_unc, ", file = \"", filename,"\", row.names = FALSE, col.names = FALSE)")
eval(parse(text=write_table_command))
## corrected ##
### p
filename <- paste0(homedir, "/baller/results/alff_age_and_sex/", hemi, "_lm_", model, "_p_fdr05.csv")
write_table_command <- paste0("write.table(x = ", hemi_model_p_fdr, ", file = \"", filename,"\", row.names = FALSE, col.names = FALSE)")
eval(parse(text=write_table_command))
### t
filename <- paste0(homedir, "/baller/results/alff_age_and_sex/", hemi, "_lm_", model, "_t_fdr05.csv")
write_table_command <- paste0("write.table(x = ", hemi_model_t_fdr, ", file = \"", filename,"\", row.names = FALSE, col.names = FALSE)")
eval(parse(text=write_table_command))
}
}