-
Notifications
You must be signed in to change notification settings - Fork 0
/
FSpwr.m
400 lines (338 loc) · 14.2 KB
/
FSpwr.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
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
function FSpwr(cfg)
load('FSpwr.surfData.mat');
%% tests
% create outPath
oldOutPath= cfg.outputPath;
outPathExists = 1;
useNewOutPath = 0;
while outPathExists
if exist(cfg.outputPath, 'dir') %outpath already exists try outPath+
cfg.outputPath = [cfg.outputPath, '+'];
useNewOutPath = 1;
else
outPathExists = 0;
end
end
mkdir(cfg.outputPath);
if useNewOutPath
disp(['ATTENTION: ', oldOutPath, ' exists.']);
end
disp(['Data will be written to ', cfg.outputPath]);
%create vars to be calculated
switch cfg.analysisType
case 1
cfg.N = 0;
case 2
cfg.power = 0;
end
if ~isfield(cfg,'diffInPercent')
cfg.diffInPercent = 0;
end
cfg.diffAsNum = cfg.difference;
%check for forbidden values
if (cfg.measure <= 0 || cfg.measure >= 5)
error('cfg.measure must be 1, 2, 3 or 4');
end
if ~(strcmp(cfg.test,'paired') || strcmp(cfg.test,'two-sample') )
error('cfg.test must be paired or two-sample');
end
if ~(cfg.analysisType == 1 || cfg.analysisType == 2 || cfg.analysisType == 3)
error('cfg.analysisType must be 1, 2 or 3');
end
if (cfg.alpha <= 0 || cfg.alpha >= 1)
error('Alpha must be 0 < alpha < 1');
end
if ~(cfg.tail == 1 || cfg.tail == 2)
error('tail must be 1 or 2');
end
if (cfg.power < 0 || cfg.power > 1)
error('Power must be 0 < power < 1');
end
if (cfg.N < 0)
error('N cannot be negative');
end
if (rem(cfg.N,1))
error('N must be whole number');
end
if ~(cfg.diffInPercent == 0 || cfg.diffInPercent == 1)
error('cfg.diffInPercent must either be 0 or 1');
end
%%
disp('************************');
disp('Starting Power Analysis');
disp('************************');
save(fullfile(cfg.outputPath,[brain.meas{cfg.measure},'.', cfg.test, '.configFile.mat']), 'cfg');
nHemis = 2;
nSmoothing = size(brain.smoothing,2);
if (cfg.measure == 4) %subcortical
nHemis = 1; %set hemi to 1, because lh & rh are concatenated
nSmoothing = 1; %set smoothing to 1, because no smoothing is applied
end
for h = 1:nHemis
disp(brain.hemi{h});
for s = 1:nSmoothing
disp(brain.smoothing{s});
%if cfg.diffInPercent==1 a vertex-wise percental difference is entered into
%power analysis
if cfg.diffInPercent == 1
cfg.differenceAbs = brain.M{cfg.measure,h,s} .* cfg.difference .* .01;
else
cfg.differenceAbs = cfg.difference;
end
switch cfg.analysisType
case 1 % A-priori -> calc. N
out{h,s}.reqN = aPriori(cfg.test,cfg.alpha, cfg.power, cfg.tail, cfg.differenceAbs, brain.sd{cfg.measure,h,s}, brain.rho{cfg.measure,h,s});
out{h,s}.actualPower = postHoc(cfg.test, out{h,s}.reqN, cfg.alpha, cfg.tail, cfg.differenceAbs, brain.sd{cfg.measure,h,s}, brain.rho{cfg.measure,h,s});
if (cfg.measure == 4) % subcortical
write_subcort_outputFile(cfg,brain, out);
else
save_mgh(out{h,s}.reqN,fullfile(cfg.outputPath,[brain.hemi{h},'.', brain.meas{cfg.measure},'.', cfg.test, '.reqN.smoothing',brain.smoothing{s},'.mgh']),eye(4));
save_mgh(out{h,s}.actualPower,fullfile(cfg.outputPath,[brain.hemi{h},'.', brain.meas{cfg.measure},'.', cfg.test, '.actualPower.smoothing',brain.smoothing{s},'.mgh']),eye(4));
end
case 2 % Post-hoc
out{h,s}.actualPower = postHoc(cfg.test, cfg.N, cfg.alpha, cfg.tail, cfg.differenceAbs, brain.sd{cfg.measure,h,s}, brain.rho{cfg.measure,h,s});
if (cfg.measure == 4) % subcortical
write_subcort_outputFile(cfg,brain, out);
else
save_mgh(out{h,s}.actualPower,fullfile(cfg.outputPath,[brain.hemi{h},'.', brain.meas{cfg.measure},'.', cfg.test, '.actualPower.smoothing',brain.smoothing{s},'.mgh']),eye(4));
end
case 3 % Sensitivity
[out{h,s}.smallestDifferece, out{h,s}.smallestCohensD] = sensitivity(cfg.test,cfg.N, cfg.alpha, cfg.power, cfg.tail, brain.sd{cfg.measure,h,s}, brain.rho{cfg.measure,h,s});
if (cfg.measure == 4) % subcortical
write_subcort_outputFile(cfg,brain, out);
else
save_mgh(out{h,s}.smallestDifferece,fullfile(cfg.outputPath,[brain.hemi{h},'.', brain.meas{cfg.measure},'.', cfg.test, '.smallestDifference.smoothing',brain.smoothing{s},'.mgh']),eye(4));
save_mgh(out{h,s}.smallestCohensD,fullfile(cfg.outputPath,[brain.hemi{h},'.', brain.meas{cfg.measure},'.', cfg.test, '.smallestCohensD.smoothing',brain.smoothing{s},'.mgh']),eye(4));
end
end
end
end
%write output text file
if (cfg.measure ~= 4)
write_cortical_summary_outputFile(cfg, brain, out);
end
disp('Calculations finished');
%% functions
function reqN = aPriori(theTest,alpha, targetPower, tail, delta, sd, rho)
incN = [100,50,25,10,1];
newStartN = ones(size(sd));
disp('calculating required N');
for i=1:size(incN,2)
powerReached = 0;
pow = [];
N = newStartN;
while powerReached == 0
switch theTest
case 'two-sample'
pow(end+1,:) = calcPower_2sample(N(end,:),alpha,tail,delta,sd);
case 'paired'
pow(end+1,:) = calcPower_paired(N(end,:),alpha,tail,delta,sd,rho);
end
lastPowCalc = pow(end,:);
N(end+1,:) = N(end,:) + incN(i);
% disp(N(end,1));
if ~(all(isnan(lastPowCalc)))
if ( all(lastPowCalc(~isnan(lastPowCalc)) > targetPower) ) %target Power reached
powerReached = 1;
N(end,:) = [];
end
end
end
newStartN = calcReqN(pow, targetPower, N);
if (incN(i)>1)
newStartN = newStartN-1*incN(i);
else
reqN = newStartN;
end
end
function pow = postHoc(theTest, N, alpha, tail, delta, sd, rho)
disp('calculating actual Power');
switch theTest
case 'two-sample'
pow = calcPower_2sample(N,alpha,tail,delta,sd);
case 'paired'
pow = calcPower_paired(N,alpha,tail,delta,sd,rho);
end
function [smallestDifferece, smallestCohensD] = sensitivity(theTest,N, alpha, targetPower, tail, sd, rho)
disp('calculating smallest detectable difference');
incD = [1,.1,.01,.001, .0001];
newStartD = zeros(size(sd));
for i=1:size(incD,2)
powerReached = 0;
pow = [];
D = newStartD;
while powerReached == 0
switch theTest
case 'two-sample'
pow(end+1,:) = calcPower_2sample(N,alpha,tail,D(end,:),sd);
case 'paired'
pow(end+1,:) = calcPower_paired(N,alpha,tail,D(end,:),sd,rho);
end
lastPowCalc = pow(end,:);
D(end+1,:) = D(end,:) + incD(i);
if ~(all(isnan(lastPowCalc)))
if ( all(lastPowCalc(~isnan(lastPowCalc)) > targetPower) ) %target Power reached
powerReached = 1;
D(end,:) = [];
end
end
end
newStartD = calcReqN(pow, targetPower, D);
if (i < length(incD) )
newStartD = newStartD-1*incD(i);
else
reqD = newStartD;
end
end
switch theTest
case 'two-sample'
smallestCohensD = reqD ./ sd;
case 'paired'
smallestCohensD = reqD ./ (sqrt(2 * sd.^2 .* (1 - rho)));
end
smallestDifferece = round(reqD .*1000) ./ 1000;
smallestCohensD = round(smallestCohensD .*1000) ./ 1000;
function reqN = calcReqN(pow, targetPower, N)
powerReached = pow > targetPower;
temp = (powerReached .* N);
temp(temp == 0) = NaN;
reqN = min(temp);
function pow = calcPower_2sample(N,alpha,tail,delta,sd)
%N per group
if (size(N,2) == 1) %if N == int make vector
N = N * ones(size(sd));
end
dz = delta ./ sd;
d = dz .* sqrt((N.^2) ./ (2*N));
isNaN = find(isnan(d));
d(isNaN) = 0;
df = 2*N - 2;
tc=tinv(1-alpha/tail, df);
pow = 1 - nctcdf(tc, df, d );
pow(isNaN) = NaN;
function pow = calcPower_paired(N,alpha,tail,delta,sd,rho)
if (size(N,2) == 1) %if N == int make vector
N = N * ones(size(sd));
end
dz = delta ./ sqrt(2*sd.^2 - 2*rho.*sd.^2);
d = dz .* sqrt(N);
isNaN = find(isnan(d));
d(isNaN) = 0;
df = N - 1;
tc=tinv(1-alpha/tail, df);
pow = 1 - nctcdf(tc, df, d );
pow(isNaN) = NaN;
function write_cortical_summary_outputFile(cfg, brain, out)
nSmoothing = size(brain.smoothing,2);
ck = clock;
fid = fopen(fullfile(cfg.outputPath,[ brain.meas{cfg.measure},'.',cfg.test,'.results.txt']), 'w');
fprintf(fid,'%s %u:%u\n',date,ck(4),ck(5));
fprintf(fid, 'FSpwr.m * Freesurfer power analysis * F. Liem\n');
fprintf(fid,'See %s for surface overlays in fsaverage space\n', cfg.outputPath);
fprintf(fid,'View, e.g., with "tksurfer fsaverage lh inflated -overlay lh.[...].smoothing0.mgh"\n\n');
if cfg.diffInPercent == 1
measStr = 'Percent';
else
measStr = sprintf('mm%u',cfg.measure);
end
switch cfg.analysisType
case 1 %aPriori
fprintf(fid,'\n**********************\n');
fprintf(fid, 'A Priori Analysis\n');
fprintf(fid,'**********************\n');
fprintf(fid, '%s: %s: target Power = %f; alpha = %f; difference (in %s) = %f; %d-tailed\n\n', brain.meas{cfg.measure},cfg.test, cfg.power, cfg.alpha, measStr, cfg.diffAsNum, cfg.tail);
if strcmp(cfg.test,'two-sample')
fprintf(fid,'N is N per group!\n\n');
end
for s = 1:nSmoothing
Ndata = [out{1,s}.reqN,out{2,s}.reqN];
fprintf(fid, 'smoothing (mm): %s\n', brain.smoothing{s});
fprintf(fid, 'Mean required N = %f\n', nanmean(Ndata));
fprintf(fid, 'SD required N = %f\n', nanstd(Ndata));
fprintf(fid, '95th percentile: required N = %f\n', prctile(Ndata,95));
actPow = [out{1,s}.actualPower,out{2,s}.actualPower];
fprintf(fid, 'Mean actual Power = %f\n', nanmean(actPow));
fprintf(fid, '95th percentile: actual Power = %f\n\n', prctile(actPow,95));
end
case 2 %post hoc
fprintf(fid,'\n**********************\n');
fprintf(fid, 'Post-hoc Analysis\n');
fprintf(fid,'**********************\n');
fprintf(fid, '%s: %s: N = %d; alpha = %f; difference (in %s) = %f; %d-tailed\n\n', brain.meas{cfg.measure},cfg.test, cfg.N, cfg.alpha, measStr, cfg.diffAsNum, cfg.tail);
if strcmp(cfg.test,'two-sample')
fprintf(fid,'N is N per group!\n\n');
end
for s = 1:nSmoothing
fprintf(fid, 'smoothing (mm): %s\n', brain.smoothing{s});
actPow = [out{1,s}.actualPower,out{2,s}.actualPower];
fprintf(fid, 'Mean actual Power = %f\n', nanmean(actPow));
fprintf(fid, '95th percentile: actual Power = %f\n\n', prctile(actPow,95));
end
case 3 %sensitivity
fprintf(fid,'\n**********************\n');
fprintf(fid, 'Sensitivity Analysis\n');
fprintf(fid,'**********************\n');
fprintf(fid, '%s: %s: N = %d; alpha = %f; power = %f; %d-tailed\n\n', brain.meas{cfg.measure},cfg.test, cfg.N, cfg.alpha, cfg.power, cfg.tail);
if strcmp(cfg.test,'two-sample')
fprintf(fid,'N is N per group!\n\n');
end
for s = 1:nSmoothing
fprintf(fid, 'smoothing (mm): %s\n', brain.smoothing{s});
sallestDiff = [out{1,s}.smallestDifferece,out{2,s}.smallestDifferece];
sallestCohensD = [out{1,s}.smallestCohensD,out{2,s}.smallestCohensD];
fprintf(fid, 'Mean smallest difference (in mm%u) = %.3f\n', cfg.measure, nanmean(sallestDiff));
fprintf(fid, 'SD smallest difference (in mm%u) = %.3f\n', cfg.measure, nanstd(sallestDiff));
fprintf(fid, '95th percentile: smallest difference (in mm%u) = %.3f\n\n',cfg.measure, prctile(sallestDiff,95));
fprintf(fid, 'Mean smallest Cohen''s D = %.3f\n', nanmean(sallestCohensD));
fprintf(fid, 'SD smallest Cohen''s D = %.3f\n', nanstd(sallestCohensD));
fprintf(fid, '95th percentile: smallest Cohen''s D = %.3f\n\n', prctile(sallestCohensD,95));
end
end
function write_subcort_outputFile(cfg, brain, out)
ck = clock;
fid = fopen(fullfile(cfg.outputPath,[ brain.meas{cfg.measure},'.',cfg.test,'ROIs.results.txt']), 'w');
fprintf(fid,'%s %u:%u\n',date,ck(4),ck(5));
fprintf(fid, 'FSpwr.m * Freesurfer power analysis * F. Liem\n');
if cfg.diffInPercent == 1
measStr = 'Percent';
else
measStr = sprintf('mm3');
end
switch cfg.analysisType
case 1 %aPriori
fprintf(fid,'\n**********************\n');
fprintf(fid, 'A Priori Analysis\n');
fprintf(fid,'**********************\n');
fprintf(fid, '%s: %s: target Power = %f; alpha = %f; difference (in %s) = %f; %d-tailed\n\n', brain.meas{cfg.measure},cfg.test, cfg.power, cfg.alpha, measStr, cfg.diffAsNum, cfg.tail);
if strcmp(cfg.test,'two-sample')
fprintf(fid,'N is N per group!\n\n');
end
fprintf(fid, 'Region\trequired N\n');
for r=1:size(out{1,1}.reqN,2)
fprintf(fid, '%s\t%d\n', brain.asegNames{r}, out{1,1}.reqN(r));
end
case 2 %post hoc
fprintf(fid,'\n**********************\n');
fprintf(fid, 'Post-hoc Analysis\n');
fprintf(fid,'**********************\n');
fprintf(fid, '%s: %s: N = %d; alpha = %f; difference (in %s) = %f; %d-tailed\n\n', brain.meas{cfg.measure},cfg.test, cfg.N, cfg.alpha, measStr, cfg.diffAsNum, cfg.tail);
if strcmp(cfg.test,'two-sample')
fprintf(fid,'N is N per group!\n\n');
end
fprintf(fid, 'Region\tactual Power\n');
for r=1:size(out{1,1}.actualPower,2)
fprintf(fid, '%s\t%f\n', brain.asegNames{r}, out{1,1}.actualPower(r));
end
case 3 %sensitivity
fprintf(fid,'\n**********************\n');
fprintf(fid, 'Sensitivity Analysis\n');
fprintf(fid,'**********************\n');
fprintf(fid, '%s: %s: N = %d; alpha = %f; power = %f; %u-tailed\n\n', brain.meas{cfg.measure},cfg.test, cfg.N, cfg.alpha, cfg.power, cfg.tail);
if strcmp(cfg.test,'two-sample')
fprintf(fid,'N is N per group!\n\n');
end
fprintf(fid, 'Region\tsmallesd difference (mm3)\tsmallest Cohen''s D\n');
for r=1:size(out{1,1}.smallestDifferece,2)
fprintf(fid, '%s\t%.3f\t%.3f\n', brain.asegNames{r}, out{1,1}.smallestDifferece(r),out{1,1}.smallestCohensD(r));
end
end