-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart_corr.m
executable file
·282 lines (212 loc) · 10.7 KB
/
part_corr.m
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
clc;
clear all;
close all;
% This script can do two things, depending on the selection ("choice", see below):
% Option 1
% This file stores the desired lags as columns to the psychometric
% observations in psychometr_msrs
% Option 2
% this file takes the selected file with psychometric msrs and performs
% partial correlation and multiple regression with the chosen regressors.
choice=questdlg('Choose option','What do you want to do?','Add lag to psychometrics file','Perform partial correlation','partcorr');
switch choice
case 'lag'
% sourcefile is the file from which to take all psychometric measures (stored as a cell)
[sourceFILENAME, sourcePATHNAME] = uigetfile(['' pwd filesep '*.mat'],'Choose sourcefile containing cell table "psychometr_msrs"');
sourcefile=strcat(sourcePATHNAME,sourceFILENAME)
m=load(sourcefile);
% newfile is wehere the new table will be stored. The new data corresponds
% to the table in sourcefile plus some appended columns
newfile=(strcat(sourcePATHNAME,sourceFILENAME(1:end-4),'_plus'));
% Now this is important: Choose a nifti-file, or a mat-file that stands represantative of
% all the nifti-files from which you want to extract a specific lag.
[lagFILENAME, lagPATHNAME] = uigetfile(['' pwd filesep '*.mat;*.nii'],'Choose lagfile (nii or mat) single-subject file, represantative of all included files. (Can be from either grp1 or grp2)');
niifile=strcat(lagPATHNAME,lagFILENAME)
% In its name, niifile should have the subject's ID as it is stored exactly
% in the first column of the psychometrics file (the "name" column).
% Possibly, niifile has a longer name than that. Determine suffix & prefix:
for ii=1:size(m.psychometr_msrs,1);
k=strfind(lagFILENAME,m.psychometr_msrs{ii,1});
if ~isempty(k)
prefix=lagFILENAME(1:k-1);
suffix=lagFILENAME(k+length(m.psychometr_msrs{ii,1}):end);
end
end
if strcmp(lagFILENAME(end-3:end),'.nii')
prompt={ 'i (MATLAB)', 'j (MATLAB)', 'k (MATLAB)' };
dlg_title='Enter voxel coordinates in MATLAB space';
num_lines=1;
answer=inputdlg(prompt,dlg_title,num_lines);
i=str2double(answer{1});
j=str2double(answer{2});
k=str2double(answer{3});
elseif strcmp(lagFILENAME(end-3:end),'.mat')
prompt='Enter ROIs (intensity values as given in column list) over which to average lag value. Sperate by comma.';
dlg_title='Input';
num_lines=1;
rois_temp=inputdlg(prompt,dlg_title,num_lines);
rois_temp=strsplit(rois_temp{1},',');
rois=[];
for entry=1:length(rois_temp)
if ~isempty(rois_temp(entry))
rois=[rois, str2double(rois_temp(entry))];
end
end
end
prompt={ 'Enter name of new Column of the psychometr_msrs table' };
dlg_title='Enter name of new Column:';
num_lines=1;
column_name=inputdlg(prompt,dlg_title,num_lines);
if strcmp(lagFILENAME(end-3:end),'.nii')
psychometr_msrs=m.psychometr_msrs;
cluster_lags=cell(length(psychometr_msrs),1);
cluster_lags{1,1}=column_name{1};
psychometr_msrs=[psychometr_msrs, cluster_lags];
for line=2:size(psychometr_msrs,1)
line
lagPATHNAMEparent=strsplit(lagPATHNAME,'grp');
if psychometr_msrs{line,2}==1
file=strcat(lagPATHNAMEparent{1},'grp1/',prefix,psychometr_msrs{line,1},suffix);
elseif psychometr_msrs{line,2}==2
temp=[];
file=strcat(lagPATHNAMEparent{1},'grp2/',prefix,psychometr_msrs{line,1},suffix);
end
if exist(['' file ''])
[data,~,~,~,~]=readnifti(['' file '']);
psychometr_msrs{line,end}=data(i,j,k);
end
end
elseif strcmp(lagFILENAME(end-3:end),'.mat')
psychometr_msrs=m.psychometr_msrs;
cluster_lags=cell(length(psychometr_msrs),1);
cluster_lags{1,1}=column_name{1};
psychometr_msrs=[psychometr_msrs, cluster_lags];
for line=2:length(psychometr_msrs)
line=line
file=strcat(lagPATHNAME,psychometr_msrs{line,1},'.mat');
rois_idx=[];
if exist(['' file ''])
subj=load(['' file '']);
for jj=rois
rois_idx=[rois_idx find(jj==subj.TDrois_cols)];
end
avrg_lag=mean(subj.ColumnMeans(rois_idx));
psychometr_msrs{line,end}=avrg_lag;
end
end
end
save(newfile,'psychometr_msrs');
disp('Done.');
case 'Perform partial correlation'
% sourcefile is the file from which to take all psychometric measures (stored as a cell)
[sourceFILENAME, sourcePATHNAME] = uigetfile(['' pwd filesep '*.mat'],'Choose sourcefile containing cell table "psychometr_msrs"');
sourcefile=strcat(sourcePATHNAME,sourceFILENAME)
m=load(sourcefile);
% The following vector stores the columns of the psychometrics
% file that serve as partial regressors.
regressors= [4 10 11]; % GA, BW, Opti
% The following vector contains the columns which serve as
% dependent variables in the partial correlation
DepVars=[14 15]; %Left Frontop, Right Frontop
for group=1:3 %1=pre, 2=term, 3=both
if group~=3
if group==1
disp('Preterm');
else
disp('Term');
end
I=find([m.psychometr_msrs{2:end,2}]'==group);
Z=[];
for ii=regressors
Z=[Z, [m.psychometr_msrs{min(I)+1:max(I)+1,ii}]'];
end
for jj=DepVars
if jj==14
disp('Left Frontop');
else
disp('Right Frontop');
end
X=[];
X=[[m.psychometr_msrs{min(I)+1:max(I)+1,jj}]',Z];
% RHO = partialcorr(X) returns the sample linear partial correlation
% coefficients between pairs of variables in X,
% controlling for the remaining variables in X.
disp('Matrix: jj,GA,BW,Opti')
disp('Partial Correlation');
[RHO,PVAL]=partialcorr(X)
% regstats(RESPONSES,DATA,MODEL) performs multiple
% regression
disp('Multiple regression');
RESPONSES=[m.psychometr_msrs{min(I)+1:max(I)+1,jj}]';
DATA=Z;
stats=regstats(RESPONSES,DATA);
pval=stats.tstat.pval
beta=stats.tstat.beta
model_p=stats.fstat.pval
% Perform outlier diagnostics with regress
disp('Outlier Diagnostics');
% Returns n-by-2 matrix rint of intervals that can be
% used to diagnose outliers. Id the interval rint(i,:)
% for observation i does not contain zero, the
% corresponding residual is larger than epected 95% of
% new observations, suggesting an outlier.
% regress(y,X) on responses in y on predictions in X. y
% is an n-by-1 vector of observed responses
y=[m.psychometr_msrs{min(I)+1:max(I)+1,jj}]';
X=[ones(size(Z,1),1) Z];
[~,~,~,rint,~]=regress(y,X);
outliers=all(rint'<0) + all(rint'>0);
RESPONSES(find(outliers==1),:)=[];
DATA(find(outliers==1),:)=[];
disp('After removal of outliers:')
disp('Multiple regression');
stats=regstats(RESPONSES,DATA);
pval=stats.tstat.pval
beta=stats.tstat.beta
model_p=stats.fstat.pval
end
else
disp('Large group');
Z=[];
for ii=regressors
Z=[Z, [m.psychometr_msrs{2:end,ii}]'];
end
for jj=DepVars
if jj==14
disp('Left Frontop');
else
disp('Right Frontop');
end
X=[];
X=[[m.psychometr_msrs{2:end,jj}]',Z];
% RHO = partialcorr(X) returns the sample linear partial correlation
% coefficients between pairs of variables in X,
% controlling for the remaining variables in X.
disp('Matrix: jj,GA,BW,Opti')
disp('Partial Correlation');
[RHO,PVAL]=partialcorr(X)
% regstats(RESPONSES,DATA,MODEL) performs multiple
% regression
disp('Multiple regression');
RESPONSES=[m.psychometr_msrs{2:end,jj}]';
DATA=Z;
stats=regstats(RESPONSES,DATA);
pval=stats.tstat.pval
beta=stats.tstat.beta
model_p=stats.fstat.pval
y=RESPONSES;
X=[ones(size(DATA,1),1) DATA];
[~,~,~,rint,~]=regress(y,X);
outliers=all(rint'<0) + all(rint'>0);
RESPONSES(find(outliers==1),:)=[];
DATA(find(outliers==1),:)=[];
disp('After removal of outliers:')
disp('Multiple regression');
stats=regstats(RESPONSES,DATA);
pval=stats.tstat.pval
beta=stats.tstat.beta
model_p=stats.fstat.pval
end
end
end
end