-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexp004_LearningCurvesSim.m
517 lines (434 loc) · 22.2 KB
/
exp004_LearningCurvesSim.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
function exp004_LearningCurvesSim(dataTable, allCurves, numRepCurves, ...
numPermsLC, Seeds, outDir)
% Function to draw learning curves for a linear SVM classification given a
% set of parameters
%% Inputs:
% dataTable: table type variable; should have the following variables
% (in this order):
% * names: subject name
% * Age: age of the subject
% * Sex: character type M/F
% * Female: 0/1 coded for female = yes
% * TIV: TIV of the subject
% * Site: cell type having site which will be used
% for classification
% * Stratify: cell type having a variable to be used for
% stratification during cross-validation
% * Actual features start from column 8 onwards
% allCurves: vector of sample sizes per site to be used for drawing
% learning curves
% numPerms: number of times permutation testing should be done
% numRepCurves: number of times the learning curves should be generated
% numPermsLC: number of times permutation testing of learning curves
% should be done
% Seeds: [numPermsLC x 1] vector of seeds to be used during
% cross-validation and permutation testing
% outDir: full path to where results should be saved
%
%% Output:
% A 'Results.mat' file is saved in the outDir containing the results
%
%% Notes:
% The goal of this experiment is to draw a series of learning curves using
% simulated data; for a given pool of data, we split the data into 20 folds
% - SVMTest set and the remaining data; the remaining data is then split
% into SVMTrain set (70 samples per site) and the NHLearn set. Within the
% NHLearn set, we sub-select different sample sizes (as specified in
% allCurves) and use them to learn harmonization parameters. These
% harmonization parameters are then applied to SVMTrain and SVMTest sets.
% After this a linear SVM classifier is trained using the features in
% SVMTrain and then predictions are made on SVMTest. Permutation testing
% is performed to test if the classifier performance is above chance level
%
%% Defaults:
% allCurves: 10:10:500
% numRepCurves: 50
% numPermsLC: 100
% Seeds: rng(0, 'twister'); randi(9999999, numPermsLC, 1)
% outDir: pwd/exp004
%
%% Authors:
% Bhalerao, Gaurav
% Parekh, Pravesh
% October 30, 2021
% ADBS
%% Check inputs and assign defaults
% Check dataTable
if ~exist('dataTable', 'var') || isempty(dataTable)
error('Please provide dataTable to work with');
else
if ~istable(dataTable)
error('Expect dataTable to be of table type');
end
end
% Check allCurves
if ~exist('allCurves', 'var') || isempty(allCurves)
allCurves = 10:10:500;
end
% Check numRepCurves
if ~exist('numRepCurves', 'var') || isempty(numRepCurves)
numRepCurves = 50;
end
% Check numPermsLC
if ~exist('numPermsLC', 'var') || isempty(numPermsLC)
numPermsLC = 100;
end
% Check Seeds
if ~exist('Seeds', 'var') || isempty(Seeds)
rng(0, 'twister');
Seeds = randi(9999999, numPermsLC, 1);
else
if length(Seeds) ~= numPermsLC
error(['Expected ', num2str(numPermsLC), ' many seeds']);
end
end
% Check outDir
if ~exist('outDir', 'var') || isempty(outDir)
outDir = fullfile(pwd, 'exp004');
end
% Make output directory, if it does not exist
if ~exist(outDir, 'dir')
mkdir(outDir);
end
% Check if it is a multi class problem
if length(unique(dataTable.Site)) > 2
multiClass = true;
else
multiClass = false;
end
%% Perform unsupervised feature elimination
% a) remove constant features
% b) remove features with NaN
% c) remove features with less than 10% unique values
toWork = dataTable{:, 8:end};
% Find constant locations
locs_constant = find(var(toWork) == 0);
% Find locations which have NaN values
locs_NaN = find(sum(isnan(toWork)) ~= 0);
% Find locations which have less than 10% unique values in data
tmp = round(toWork, 4);
cutoff = round(10*size(toWork,1)/100, 0);
count = 1;
locs_novariance = [];
for feat = 1:size(toWork,2)
tmp2 = unique(tmp(:,feat));
if length(tmp2) < cutoff
locs_novariance(count) = feat; %#ok<AGROW>
count = count + 1;
end
end
% All locations to delete: toWork is a matrix
locDelete = unique([locs_constant'; locs_NaN'; locs_novariance']);
% Actual locations to delete in the original table
locDelete = locDelete + 7;
% Record variable names that are being removed
deletedVariables = dataTable.Properties.VariableNames(locDelete); %#ok<NASGU>
% Delete these variables
dataTable(:, locDelete) = [];
%% Initialize
numSites = length(unique(dataTable.Site));
numCurves = length(allCurves);
[groundTruth_SVM_Train, groundTruth_SVM_Test, ...
predictions_SVM_Train, predictions_SVM_Test, ...
groundTruth_SVM_Perm_Train, groundTruth_SVM_Perm_Test, ...
predictions_SVM_Perm_Train, predictions_SVM_Perm_Test, ...
accuracy_SVM_Train, accuracy_SVM_Test, ...
accuracy_SVM_Perm_Train, accuracy_SVM_Perm_Test, ...
pValues_test, overall_pValue, ...
MD_noHarm_SVMTrain, MD_noHarm_SVMTest, ...
MD_noHarm_NHLearn, MD_Harm_SVMTrain, ...
MD_Harm_SVMTest, MD_Harm_NHLearn] = deal(cell(numCurves, 1));
%% Run experiments
for curves = 1:numCurves
% Start timer
t_init = tic;
% Run numRepCurves and numPermsLC
[groundTruth_SVM_Train{curves,1}, groundTruth_SVM_Test{curves,1}, ...
predictions_SVM_Train{curves,1}, predictions_SVM_Test{curves,1}, ...
groundTruth_SVM_Perm_Train{curves,1}, groundTruth_SVM_Perm_Test{curves,1}, ...
predictions_SVM_Perm_Train{curves,1}, predictions_SVM_Perm_Test{curves,1}, ...
accuracy_SVM_Train{curves,1}, accuracy_SVM_Test{curves,1}, ...
accuracy_SVM_Perm_Train{curves,1}, accuracy_SVM_Perm_Test{curves,1}, ...
pValues_test{curves,1}, overall_pValue{curves,1}, ...
MD_noHarm_SVMTrain{curves,1}, MD_noHarm_SVMTest{curves,1}, ...
MD_noHarm_NHLearn{curves,1}, MD_Harm_SVMTrain{curves,1}, ...
MD_Harm_SVMTest{curves,1}, MD_Harm_NHLearn{curves,1}] = ...
doLC(dataTable, numSites, numRepCurves, numPermsLC, allCurves(curves), Seeds, multiClass, outDir);
% Stop timer
t_end = toc(t_init);
% Evaluate p value
if overall_pValue{curves,1} < 0.05
disp(['Finished sample size : ', num2str(allCurves(curves)), ' [', num2str(t_end, '%.2f'), ' seconds]']);
else
% Compile results table
results = cell2table([num2cell(allCurves'), overall_pValue], 'VariableNames', {'SampleSizePerSite'; 'Overall_pValue'}); %#ok<NASGU>
disp(['Completed sample size : ', num2str(allCurves(curves)), ' [', num2str(t_end, '%.2f'), ' seconds]']);
break
end
end
% Save everything
save(fullfile(outDir, 'Results.mat'), 'dataTable', 'curves', 'allCurves', 'pValues*', 'overall*');
try
save(fullfile(outDir, 'AdditionalResults.mat'), '-v7.3');
catch
end
function [hTrainData, hremTrainData, hTestData] = doHarmonization_LC(trainData, remTrainData, testData, ...
trainSite, remTrainSite, testSite, tmpDir)
% Function to call neuroHarmonize and return harmonized data
% Write out csv files
dlmwrite(fullfile(tmpDir, 'temp_trainData.csv'), trainData);
dlmwrite(fullfile(tmpDir, 'temp_remTrainData.csv'), remTrainData);
dlmwrite(fullfile(tmpDir, 'temp_testData.csv'), testData);
% Create covariates files for writing
fid_train = fopen(fullfile(tmpDir, 'temp_trainSite.csv'), 'w');
fid_remTrain = fopen(fullfile(tmpDir, 'temp_remTrainSite.csv'), 'w');
fid_test = fopen(fullfile(tmpDir, 'temp_testSite.csv'), 'w');
% Print header
% Walter's example: https://www.mathworks.com/matlabcentral/answers/364295
tmpHeader = 'SITE';
fprintf(fid_train, '%s\n', tmpHeader);
fprintf(fid_remTrain, '%s\n', tmpHeader);
fprintf(fid_test, '%s\n', tmpHeader);
% Write out data: integer or float doesn't seem to matter
for lines = 1:size(trainSite,1)
fprintf(fid_train, '%s\n', trainSite{lines,:});
end
for lines = 1:size(remTrainSite,1)
fprintf(fid_remTrain, '%s\n', remTrainSite{lines,:});
end
for lines = 1:size(testSite,1)
fprintf(fid_test, '%s\n', testSite{lines,:});
end
% Close files
fclose(fid_train);
fclose(fid_remTrain);
fclose(fid_test);
% Might need to add full path to doHarmonization_LCSim.py script
command = ['python doHarmonization_LCSim.py ', tmpDir];
system(command);
% Read adjusted data back in
hTrainData = dlmread(fullfile(tmpDir, 'adjustedTrainData.csv'));
hremTrainData = dlmread(fullfile(tmpDir, 'adjustedremTrainData.csv'));
hTestData = dlmread(fullfile(tmpDir, 'adjustedTestData.csv'));
% Delete files
delete(fullfile(tmpDir, 'temp_trainData.csv'));
delete(fullfile(tmpDir, 'temp_remTrainData.csv'));
delete(fullfile(tmpDir, 'temp_testData.csv'));
delete(fullfile(tmpDir, 'temp_trainSite.csv'));
delete(fullfile(tmpDir, 'temp_remTrainSite.csv'));
delete(fullfile(tmpDir, 'temp_testSite.csv'));
delete(fullfile(tmpDir, 'adjustedTrainData.csv'));
delete(fullfile(tmpDir, 'adjustedremTrainData.csv'));
delete(fullfile(tmpDir, 'adjustedTestData.csv'));
% -------------------------------------------------------------------------
function [stdData, stdCoeff] = standardizeData(data, stdCoeff)
% Function to standardize a given feature matrix or apply already learned
% standardization coefficients to a feature matrix
% stdData = (data - mean(data))/std(data)
% stdCoeff = [mean(data), std(data)]
% Determine if standardization coefficients are o be learnt or not
if ~exist('stdCoeff', 'var') || isempty(stdCoeff)
toEstimate = true;
else
toEstimate = false;
end
% Learn coefficients
if toEstimate
stdCoeff(1,:) = mean(data);
stdCoeff(2,:) = std(data);
end
% Apply scaling
% stdData = (data - stdCoeff(1))./ stdCoeff(2);
stdData = bsxfun(@rdivide, bsxfun(@minus, data, stdCoeff(1,:)), stdCoeff(2,:));
% -------------------------------------------------------------------------
function [groundTruth_SVM_Train, groundTruth_SVM_Test, ...
predictions_SVM_Train, predictions_SVM_Test, ...
groundTruth_SVM_Perm_Train, groundTruth_SVM_Perm_Test, ...
predictions_SVM_Perm_Train, predictions_SVM_Perm_Test, ...
accuracy_SVM_Train, accuracy_SVM_Test, ...
accuracy_SVM_Perm_Train, accuracy_SVM_Perm_Test, ...
pValues_test, overall_pValue, ...
MD_noHarm_SVMTrain, MD_noHarm_SVMTest, ...
MD_noHarm_NHLearn, MD_Harm_SVMTrain, ...
MD_Harm_SVMTest, MD_Harm_NHLearn] = ...
doLC(dataTable, numSites, numRepeats, numPermsLC, currSampleSize, seeds, multiClass, outDir)
% Initialize
[groundTruth_SVM_Train, groundTruth_SVM_Test, ...
predictions_SVM_Train, predictions_SVM_Test, ...
groundTruth_SVM_Perm_Train, groundTruth_SVM_Perm_Test, ...
predictions_SVM_Perm_Train, predictions_SVM_Perm_Test] = deal(cell(numPermsLC, 1));
[accuracy_SVM_Train, accuracy_SVM_Test, ...
accuracy_SVM_Perm_Train, accuracy_SVM_Perm_Test, ...
MD_noHarm_SVMTrain, MD_noHarm_SVMTest, ...
MD_noHarm_NHLearn, MD_Harm_SVMTrain, ...
MD_Harm_SVMTest, MD_Harm_NHLearn] = deal(zeros(numPermsLC, 20));
[accuracy_SVM_Train_repeats, accuracy_SVM_Test_repeats, ...
accuracy_SVM_Perm_Train_repeats, accuracy_SVM_Perm_Test_repeats] = deal(zeros(numPermsLC, 1));
allSites = unique(dataTable.Site);
for repeats = 1:numRepeats
% Set seed
rng(seeds(repeats), 'twister');
% Separate SVM Test samples
cv = cvpartition(dataTable.Site, 'KFold', 20, 'stratify', true);
for fold = 1:20
% Extract SVM test and remaining data
data_SVM_Test = dataTable(cv.test(fold),:);
data_remaining = dataTable(cv.training(fold),:);
% Split the remaining data into SVM train and NH learn
cv2 = cvpartition(data_remaining.Site, 'Holdout', 70*numSites, 'stratify', true);
% Extract SVM train and NH learn
data_SVM_Train = data_remaining(cv2.test,:);
data_rem_Learn = data_remaining(cv2.training,:);
if currSampleSize*numSites == height(data_rem_Learn)
data_NH_Learn = data_rem_Learn;
else
% Take currSampleSize from NH Learn
cv3 = cvpartition(data_rem_Learn.Site, 'HoldOut', currSampleSize*numSites, 'stratify', true);
data_NH_Learn = data_rem_Learn(cv3.test,:);
end
% Extract features
features_NH_Learn = data_NH_Learn{:, 8:end};
features_SVM_Train = data_SVM_Train{:, 8:end};
features_SVM_Test = data_SVM_Test{:, 8:end};
% Extract site information
site_NH_Learn = data_NH_Learn.Site;
site_SVM_Train = data_SVM_Train.Site;
site_SVM_Test = data_SVM_Test.Site;
% Find sites
for sites = 1:length(allSites)
loc_SVMTrain(:,sites) = ismember(data_SVM_Train.Site, allSites(sites)); %#ok<AGROW>
loc_SVMTest(:, sites) = ismember(data_SVM_Test.Site, allSites(sites)); %#ok<AGROW>
loc_NHlearn(:, sites) = ismember(data_NH_Learn.Site, allSites(sites)); %#ok<AGROW>
end
% Send to neuroHarmonize
[features_NH_Learn, features_SVM_Train, features_SVM_Test] = doHarmonization_LC(features_NH_Learn, features_SVM_Train, features_SVM_Test, ...
site_NH_Learn, site_SVM_Train, site_SVM_Test, outDir); %#ok<ASGLU>
% Standardize features
[features_SVM_Train, coeff_std] = standardizeData(features_SVM_Train);
features_SVM_Test = standardizeData(features_SVM_Test, coeff_std);
% Get labels
labels_SVM_Train = data_SVM_Train.Site;
labels_SVM_Test = data_SVM_Test.Site;
% Prepare permutation labels
tmp_labelsAll = [labels_SVM_Train; labels_SVM_Test];
numSamples = length(tmp_labelsAll);
randOrder = randperm(numSamples, numSamples);
tmp_labelsAll = tmp_labelsAll(randOrder);
labels_SVM_Perm_Train = tmp_labelsAll(1:length(labels_SVM_Train));
labels_SVM_Perm_Test = tmp_labelsAll(length(labels_SVM_Train)+1:end);
% Train classifier
if multiClass
mdl_SVM = fitcecoc(features_SVM_Train, labels_SVM_Train, 'Coding', 'onevsone', 'Learners', 'svm');
mdl_SVM_Perm = fitcecoc(features_SVM_Train, labels_SVM_Perm_Train, 'Coding', 'onevsone', 'Learners', 'svm');
else
mdl_SVM = fitcsvm(features_SVM_Train, labels_SVM_Train, 'KernelFunction', 'linear', 'Standardize', false, 'BoxConstraint', 1);
mdl_SVM_Perm = fitcsvm(features_SVM_Train, labels_SVM_Perm_Train, 'KernelFunction', 'linear', 'Standardize', false, 'BoxConstraint', 1);
end
% Record ground truth for posterity
groundTruth_SVM_Train{repeats, fold} = labels_SVM_Train;
groundTruth_SVM_Test{repeats, fold} = labels_SVM_Test;
groundTruth_SVM_Perm_Train{repeats, fold} = labels_SVM_Perm_Train;
groundTruth_SVM_Perm_Test{repeats, fold} = labels_SVM_Perm_Test;
% Make predictions
predictions_SVM_Train{repeats, fold} = predict(mdl_SVM, features_SVM_Train);
predictions_SVM_Test{repeats, fold} = predict(mdl_SVM, features_SVM_Test);
predictions_SVM_Perm_Train{repeats, fold} = predict(mdl_SVM_Perm, features_SVM_Train);
predictions_SVM_Perm_Test{repeats, fold} = predict(mdl_SVM_Perm, features_SVM_Test);
% Evaluate classifier performances
accuracy_SVM_Train(repeats, fold) = sum(strcmpi(predictions_SVM_Train{repeats, fold}, labels_SVM_Train))/length(labels_SVM_Train) * 100;
accuracy_SVM_Test(repeats, fold) = sum(strcmpi(predictions_SVM_Test{repeats, fold}, labels_SVM_Test))/length(labels_SVM_Test) * 100;
accuracy_SVM_Perm_Train(repeats, fold) = sum(strcmpi(predictions_SVM_Perm_Train{repeats, fold}, labels_SVM_Perm_Train))/length(labels_SVM_Perm_Train) * 100;
accuracy_SVM_Perm_Test(repeats, fold) = sum(strcmpi(predictions_SVM_Perm_Test{repeats, fold}, labels_SVM_Perm_Test))/length(labels_SVM_Perm_Test) * 100;
end
% Average over folds
accuracy_SVM_Train_repeats(repeats,1) = mean(accuracy_SVM_Train(repeats,:));
accuracy_SVM_Test_repeats(repeats,1) = mean(accuracy_SVM_Test(repeats,:));
accuracy_SVM_Perm_Train_repeats(repeats,1) = mean(accuracy_SVM_Perm_Train(repeats,:));
accuracy_SVM_Perm_Test_repeats(repeats,1) = mean(accuracy_SVM_Perm_Test(repeats,:));
end
for repeats = numRepeats+1:numPermsLC
% Set seed
rng(seeds(repeats), 'twister');
% Separate SVM Test samples
cv = cvpartition(dataTable.Site, 'KFold', 20, 'stratify', true);
for fold = 1:20
% Extract SVM test and remaining data
data_SVM_Test = dataTable(cv.test(fold),:);
data_remaining = dataTable(cv.training(fold),:);
% Split the remaining data into SVM train and NH learn
cv2 = cvpartition(data_remaining.Site, 'Holdout', 70*numSites, 'stratify', true);
% Extract SVM train and NH learn
data_SVM_Train = data_remaining(cv2.test,:);
data_rem_Learn = data_remaining(cv2.training,:);
% Take currSampleSize from NH Learn
if currSampleSize*numSites == height(data_rem_Learn)
data_NH_Learn = data_rem_Learn;
else
% Take currSampleSize from NH Learn
cv3 = cvpartition(data_rem_Learn.Site, 'HoldOut', currSampleSize*numSites, 'stratify', true);
data_NH_Learn = data_rem_Learn(cv3.test,:);
end
% Extract features
features_NH_Learn = data_NH_Learn{:, 8:end};
features_SVM_Train = data_SVM_Train{:, 8:end};
features_SVM_Test = data_SVM_Test{:, 8:end};
% Extract site information
site_NH_Learn = data_NH_Learn.Site;
site_SVM_Train = data_SVM_Train.Site;
site_SVM_Test = data_SVM_Test.Site;
% Find sites
for sites = 1:length(allSites)
loc_SVMTrain(:,sites) = ismember(data_SVM_Train.Site, allSites(sites));
loc_SVMTest(:, sites) = ismember(data_SVM_Test.Site, allSites(sites));
loc_NHlearn(:, sites) = ismember(data_NH_Learn.Site, allSites(sites));
end
% Send to neuroHarmonize
[features_NH_Learn, features_SVM_Train, features_SVM_Test] = doHarmonization_LC(features_NH_Learn, features_SVM_Train, features_SVM_Test, ...
site_NH_Learn, site_SVM_Train, site_SVM_Test, outDir); %#ok<ASGLU>
% Standardize features
[features_SVM_Train, coeff_std] = standardizeData(features_SVM_Train);
features_SVM_Test = standardizeData(features_SVM_Test, coeff_std);
% Get labels
labels_SVM_Train = data_SVM_Train.Site;
labels_SVM_Test = data_SVM_Test.Site;
% Prepare permutation labels
tmp_labelsAll = [labels_SVM_Train; labels_SVM_Test];
numSamples = length(tmp_labelsAll);
randOrder = randperm(numSamples, numSamples);
tmp_labelsAll = tmp_labelsAll(randOrder);
labels_SVM_Perm_Train = tmp_labelsAll(1:length(labels_SVM_Train));
labels_SVM_Perm_Test = tmp_labelsAll(length(labels_SVM_Train)+1:end);
% Train classifier
if multiClass
mdl_SVM_Perm = fitcecoc(features_SVM_Train, labels_SVM_Perm_Train, 'Coding', 'onevsone', 'Learners', 'svm');
else
mdl_SVM_Perm = fitcsvm(features_SVM_Train, labels_SVM_Perm_Train, 'KernelFunction', 'linear', 'Standardize', false, 'BoxConstraint', 1);
end
% Record ground truth for posterity
groundTruth_SVM_Train{repeats, fold} = labels_SVM_Train;
groundTruth_SVM_Test{repeats, fold} = labels_SVM_Test;
groundTruth_SVM_Perm_Train{repeats, fold} = labels_SVM_Perm_Train;
groundTruth_SVM_Perm_Test{repeats, fold} = labels_SVM_Perm_Test;
% Make predictions
predictions_SVM_Train{repeats, fold} = cell(length(labels_SVM_Train), 1);
predictions_SVM_Test{repeats, fold} = cell(length(labels_SVM_Test), 1);
predictions_SVM_Perm_Train{repeats, fold} = predict(mdl_SVM_Perm, features_SVM_Train);
predictions_SVM_Perm_Test{repeats, fold} = predict(mdl_SVM_Perm, features_SVM_Test);
% Evaluate classifier performances
accuracy_SVM_Train(repeats, fold) = NaN;
accuracy_SVM_Test(repeats, fold) = NaN;
accuracy_SVM_Perm_Train(repeats, fold) = sum(strcmpi(predictions_SVM_Perm_Train{repeats, fold}, labels_SVM_Perm_Train))/length(labels_SVM_Perm_Train) * 100;
accuracy_SVM_Perm_Test(repeats, fold) = sum(strcmpi(predictions_SVM_Perm_Test{repeats, fold}, labels_SVM_Perm_Test))/length(labels_SVM_Perm_Test) * 100;
end
% Average over folds
accuracy_SVM_Train_repeats(repeats,1) = mean(accuracy_SVM_Train(repeats,:));
accuracy_SVM_Test_repeats(repeats,1) = mean(accuracy_SVM_Test(repeats,:));
accuracy_SVM_Perm_Train_repeats(repeats,1) = mean(accuracy_SVM_Perm_Train(repeats,:));
accuracy_SVM_Perm_Test_repeats(repeats,1) = mean(accuracy_SVM_Perm_Test(repeats,:));
end
% Calculate p value
pValues_test = zeros(numRepeats,1);
for repeats = 1:numRepeats
pValues_test(repeats,1) = (sum(accuracy_SVM_Perm_Test_repeats >= accuracy_SVM_Test_repeats(repeats,1)) + 1)/(numPermsLC + 1);
end
overall_pValue = mean(pValues_test);