-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvlsm_logistic.m
196 lines (150 loc) · 4.41 KB
/
vlsm_logistic.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
function vlsm_logistic(handles)
global VLSM
% Check SPM version
try
spmver = spm('ver');
catch
spmver = '';
end
if ~strcmpi(spmver,'SPM12'),
text_status = sprintf('Error: Setpath SPM12 directory1');
set(handles.text_status,'String',text_status);
disp(text_status);
return
end
spm('Defaults', 'fMRI');
% Get Parameters from VLSM
DATApath = VLSM.DATApath;
subjlist = VLSM.subjname;
nsubj = length(subjlist);
ROIfolder = VLSM.ROIfolder;
ROIprefix = VLSM.ROIprefix;
groupVar = VLSM.groupVar;
ngrp = VLSM.ngrp;
% Get Covariates from VLSM input
nCovariates = VLSM.nCovariates;
if nCovariates>0,
covariateVars = VLSM.covariate.values;
covariateNames = VLSM.covariate.names;
else
covariateVars = [];
covariateNames = [];
end
if ngrp~=2,
% Print Status on chi2test window
pause(0.5);
text_status = sprintf('Error: No. of groups should be 2');
set(handles.text_status,'String',text_status);
disp(text_status);
return
end
% Change 'Run' button color
set(handles.pushbutton_runGroup,'ForegroundColor',[1 1 1]);
set(handles.pushbutton_runGroup,'BackgroundColor',[11 132 199]./256);
pause(0.2);
% Output Path Setup
if nCovariates>0,
OUTpath = fullfile(DATApath,['logistic_' covariateNames],groupVar);
else
OUTpath = fullfile(DATApath,'logistic',groupVar);
end
mkdir(OUTpath);
% Get Image information
fn_tmp = sprintf('w%s%s.nii',ROIprefix, subjlist{1});
fn_roi = fullfile(DATApath, ROIfolder, fn_tmp);
if ~spm_existfile(fn_roi)
pause(0.5);
[p,f,e] = fileparts(fn_roi);
text_status = sprintf('File "%s" does not exist.', [f e]);
set(handles.text_status,'String',text_status);
disp(text_status);
return;
end
vref = spm_vol(fn_roi);
idbrainmask = fmri_load_maskindex(vref);
% Find Valid Voxels
IMG = zeros(vref.dim);
for c=1:nsubj,
fn_tmp = sprintf('w%s%s.nii',ROIprefix, subjlist{c});
fn_roi = fullfile(DATApath, ROIfolder, fn_tmp);
vo = spm_vol(fn_roi);
I = spm_read_vols(vo);
idroi = find(I>0);
idx = intersect(idroi,idbrainmask);
IMG(idx) = IMG(idx) + 1;
end
% Collect Data from All Subjects
idvox = find(IMG>0); clear IMG;
nvox = length(idvox);
data = zeros(nsubj,nvox);
for c=1:nsubj,
fn_tmp = sprintf('w%s%s.nii',ROIprefix, subjlist{c});
fn_roi = fullfile(DATApath, ROIfolder, fn_tmp);
vo = spm_vol(fn_roi);
I = spm_read_vols(vo);
data(c,:) = I(idvox);
end
%----------------------------------------------------
% Group Variable
%----------------------------------------------------
group = VLSM.group;
Pval = zeros(nvox,1);
LL = zeros(nvox,1);
UL = zeros(nvox,1);
Beta = zeros(nvox,1);
parfor i=1:nvox,
dat = data(:,i);
[B,dev,stats] = mnrfit([dat, covariateVars],group); % mnrfit(X,Y)
Pval(i) = stats.p(2); % effects of lesion(yes/no) in predicting disease
LL(i) = stats.beta(2) - 1.96.*stats.se(2);
UL(i) = stats.beta(2) + 1.96.*stats.se(2);
Beta(i) = stats.beta(2);
end
%--------------------------------------------------------------------------
% Write the resulting group comparison
%--------------------------------------------------------------------------
% Get significant voxels
ids = find(Pval<1);
% Write Upper-Limit image
IMG = zeros(vref.dim);
IMG(idvox(ids)) = UL(ids);
v = vref;
v.dt = [16 0];
v.fname = fullfile(OUTpath, 'logistic_UL.nii');
spm_write_vol(v, IMG);
% Write Lower-Limit image
IMG = zeros(vref.dim);
IMG(idvox(ids)) = LL(ids);
v = vref;
v.dt = [16 0];
v.fname = fullfile(OUTpath, 'logistic_LL.nii');
spm_write_vol(v, IMG);
% Write Beta image
IMG = zeros(vref.dim);
IMG(idvox(ids)) = Beta(ids);
v = vref;
v.dt = [16 0];
v.fname = fullfile(OUTpath, 'logistic_beta.nii');
spm_write_vol(v, IMG);
% Write -log10(Puncorr) image
IMG = zeros(vref.dim);
IMG(idvox(ids)) = -log10(Pval(ids));
v = vref;
v.dt = [16 0];
v.fname = fullfile(OUTpath, 'logistic_log10_P_uncorr.nii');
spm_write_vol(v, IMG);
% Write -log10(Pfdr) image
[h, crit_p, adj_ci_cvrg, adj_p] = fdr_bh(Pval);
IMG = zeros(vref.dim);
IMG(idvox(ids)) = -log10(adj_p(ids));
v = vref;
v.dt = [16 0];
v.fname = fullfile(OUTpath, 'logistic_log10_P_fdr.nii');
spm_write_vol(v, IMG);
% Change 'Run' button color to the original
set(handles.pushbutton_runGroup,'ForegroundColor',[0 0 0]);
set(handles.pushbutton_runGroup,'BackgroundColor',[248 248 248]./256);
% Print Status on chi2test window
pause(0.5);
text_status = sprintf('images of logistic regressions were created.');
set(handles.text_status,'String',text_status);