-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindPropertyCorrelations.m
716 lines (601 loc) · 24.5 KB
/
FindPropertyCorrelations.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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
function varargout = FindPropertyCorrelations(xData, yData, varargin)
% resultStruct = FindPropertyCorrelations(xData, yData, [options])
% Find correlations in data, and evaluate their significance
% INPUTS
% xData numData x numXProperties matrix of floating point values
% each row corresponds to a unique data point
% each column corresponds to a measured property
% if a property isn't measured for one data point, that entry
% should be set to NaN
% (OPTIONAL)
% yData numData x numYProperties matrix. numData should be the same as
% for xData, but numYPeroperties can differ from numXProperties
% if yData is specified:
% correlations are sought by finding linear combinations of
% xData that reconstruct yData and linear combinations of
% yData that reconstruct xData
% if yData is empty (pass yData = [])
% correlations are sought by finding linear combinations of x
% properties that reconstruct each individual x property
% options default options can be overloaded by passing ordered key/value
% pairs or a single structure with key/value elements.
% numShuffledTrials 10000
% if non-zero, compute adjusted p-values by finding correlations in
% shuffled data. If zero, compute adjusted p-values by Pearson test
% and correcting with Holm-Bonferroni.
% significanceLevel 0.05
% The threshold for a p-value to be considered significant
% typeLabels {}
% Either empty or a numPoints x 1 cell array of strings specifying
% the "type" of each data point. Correlations can be pooled across
% type, or broken down by type independently
% fitPooled true
% Find correlations by pooling across type
% fitSubTypes false
% Find correlations by restricting to each sub-type in turn
% xLabel 'x'
% The label that collectively applies to xData
% yLabel 'y'
% The label that collectively applies to yData
% xPropLabels {}
% List of labels that apply to each column of xData
% yPropLabels {}
% List of labels that apply to each column of yData
% colormap []
% numTypes x 3 matrix of colors for individual plots. If colormap is
% empty and numTypes > 1, a colormap will be created automatically using
% DistinctColormap.m
% OUTPUTS
% resultStruct A structure that contains information about the
% correlations and their significance. It contains the
% following subfields
% numFits The number of distinct correlations that will be
% tested.
% shuffledFitError Present if numShuffledTrials == true
% numFits x numShuffledTrials array of fit error
% for each fit in each shuffled trial
% adjustedPValues numFits x 1 array of adjusted p-values
% isSignificant numFits x 1 array of boolean listing the
% significance of each found correlation
% correlations Structure with the bulk of the fit information
% label collective description of the correlations
% fitErr numFits x 1 array of fit error
% rSquared numFits x 1 array of rSquared for best
% correlations
% descriptions numFits x 1 cell array of correlation
% descriptions
% indices numFits x 1 cell array with list of columns
% used in each fit
% propVals numFits x 1 cell array with the data used for
% each fit
% predictedPropVals numFits x 1 cell array with the data that would
% be predicted by the results of the fit
% coefs numFits x 1 cell array of fit coefficients
% pValues numFits x 1 array of raw p-values
% sections list of which fits were grouped into which
% computational batches
if nargin < 2
help FindPropertyCorrelations
error('Incorrect number of input arguments.')
end
if nargout > 1
help FindPropertyCorrelations
error('Incorrect number of return values.')
end
% get options that control how FindPropertyCorrelations will fit data
% -first set some default options
defaultOptions = { ...
'numShuffledTrials', 10000, ...
'xLabel', 'x', 'yLabel', 'y', ...
'xPropLabels', {}, 'yPropLabels', {}, ...
'typeLabels', {}, ...
'significanceLevel', 0.05 ...
'fitPooled', true, ...
'fitSubTypes', false, ...
'colormap', [] ...
};
options = getCorrelationOptions(defaultOptions, xData, yData, varargin{:});
% Minor preliminary processing
% -Remove all the rows where XData is all NaN or yData is all NaN
% -ZScore the data
[xData, yData, options] = preprocessData(xData, yData, options);
% -Make two structs, one to hold results, and one to hold data
% necessary for fits
[resultStruct, dataStruct] = organizeDataStructs(xData, yData, options);
% Do the analysis with the selected map properties
isShuffledTrial = false;
resultStruct.correlations = ...
correlateProperties(xData, yData, dataStruct, options, isShuffledTrial);
% adjust p-values for multiple correlations
if options.numShuffledTrials > 0
% do all the shuffled trials in this block
isShuffledTrial = true;
% open matlab pool if it isn't already open
parBlock = ParallelBlock();
%Make a popup to display progress
if isempty(yData)
popupLabel = ...
sprintf('%s vs %s shuffled trials', options.xLabel, options.xLabel);
else
popupLabel = ...
sprintf('%s vs %s shuffled trials', options.xLabel, options.yLabel);
end
PopupProgress(popupLabel, options.numShuffledTrials)
shuffledFitErr = resultStruct.shuffledFitErr;
parfor n = 1:options.numShuffledTrials
% get the fit error for each shuffled trial, distributing them in
% parallel
shuffledFitErr(:,n) = ...
correlateProperties(xData, yData, dataStruct, options, ...
isShuffledTrial);
PopupProgress(popupLabel)
end
resultStruct.shuffledFitErr = shuffledFitErr;
% close the matlab pool if it is no longer required
parBlock.endBlock();
% adjust p-values for multiple correlations, using shuffled trials
resultStruct = shuffleCorrect(resultStruct, options);
else
% adjust p-values for multiple correlations using Holm-Bonferroni
resultStruct = holmBonferroniCorrect(resultStruct, options);
end
% display the results
DisplayCorrelationResults(resultStruct, options);
if nargout == 1
varargout = {resultStruct};
else
varargout = {};
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function options = getCorrelationOptions(defaultOptions, xData, yData, ...
varargin)
% return an options structure based on defaultOptions, but with user
% overrides
% check for user overrides
options = GetOptions(defaultOptions, varargin, true);
% ensure that 'xPropLabels' and 'yPropLabels' are appropriately defined
if isempty(options.xPropLabels)
options.xPropLabels = makePropertyLabels(options.xLabel, ...
size(xData, 2));
end
if isempty(options.yPropLabels)
options.yPropLabels = makePropertyLabels(options.yLabel, ...
size(yData, 2));
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function labels = makePropertyLabels(baseLabel, numProperties)
% Make some default property labels so that findPropertyCorrelations can
% report back which correlations were significant, etc
%
% Labels are of the form baseLabel_#
% e.g. if baseLabels = 'x' and numProperties = 3, then
% labels = {'x_1', 'x_2', 'x_3'}
if numProperties == 0
labels = {};
else
labels = cell(1, numProperties);
for n = 1:numProperties
labels{n} = sprintf('%s_%d', baseLabel, n);
end
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [xData, yData, options] = preprocessData(xData, yData, options)
% Remove all the rows where XData is all NaN or yData is all NaN
% ZScore the data
% Keep rows were some xData is finite and some yData is finite
if size(yData, 2) > 0
% yData is used:
% keep rows were some xData is finite and some yData is finite
keepRows = find( sum(isfinite(xData), 2) > 0 & ...
sum(isfinite(yData), 2) > 0 );
yData = ZScore(yData(keepRows,:), 1, 1);
else
% yData isn't used
% keep rows were some xData is finite
keepRows = find( sum(isfinite(xData), 2) > 0 );
% make yData a L x 0 matrix (avoids some annoying if-statements later)
yData = zeros(length(keepRows), 0);
end
xData = ZScore(xData(keepRows,:), 1, 1);
if isfield(options, 'typeLabels') && ~isempty(options.typeLabels)
options.typeLabels = options.typeLabels(keepRows);
end
if isfield(options, 'IDs') && ~isempty(options.IDs)
options.IDs = options.IDs(keepRows);
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [resultS, dataS] = organizeDataStructs(xData, yData, options)
% construct two structs, one to hold results, and one to hold data
% necessary for fits
% resultS: a structure to hold results of the correlation algorithm
% dataS: a structrure to hold data used by the correlation algorithm
% check for consistency in property data set sizes, and describe the
% overall fit
numDataPoints = size(xData, 1);
if size(yData, 1) ~= numDataPoints
error('xData and yData have different numbers of data points')
end
if size(yData, 2) > 0
% finding correlations between two distinct sets of properties
label = sprintf('Correlations between %s and %s', ...
options.xLabel, options.yLabel);
else
% finding correlations among one set of properties
label = sprintf('Correlations among %s', options.xLabel);
end
% determine if we're breaking down
types = unique(options.typeLabels);
numTypes = length(types);
numTypeFits = options.fitPooled + numTypes * options.fitSubTypes;
% Make lists of which rows and columns to use for each fit, and keep track
% of the total number of fits
useRows = cell(numTypeFits, 1);
useXCols = cell(numTypeFits, 1);
useYCols = cell(numTypeFits, 1);
numFits = 0;
fitNum = 1;
if options.fitPooled
% only do this if we're fitting all the data, pooled across cell type
useRows{fitNum} = 1:numDataPoints;
useXCols{fitNum} = find(sum(isfinite(xData), 1) > 0);
useYCols{fitNum} = find(sum(isfinite(yData), 1) > 0);
numFits = numFits + length(useXCols{fitNum}) + length(useYCols{fitNum});
fitNum = fitNum + 1;
end
if options.fitSubTypes
% only do this if we're breaking down fits by cell type
for n = 1:numTypes
subType = types{n};
useRows{fitNum} = find(strcmp(options.typeLabels, subType));
useXCols{fitNum} = ...
find(sum(isfinite(xData(useRows{fitNum},:)), 1) > 0);
useYCols{fitNum} = ...
find(sum(isfinite(yData(useRows{fitNum},:)), 1) > 0);
numFits = ...
numFits + length(useXCols{fitNum}) + length(useYCols{fitNum});
fitNum = fitNum + 1;
end
end
resultS = struct( ...
'numFits', numFits ...
);
if options.numShuffledTrials > 0
resultS.shuffledFitErr = zeros(numFits, options.numShuffledTrials);
end
% Create a struct holding information necesary for computing correlations
% (due to the madness of the MATLAB language, add cell array fields later)
dataS = struct( ...
'corrLabel', label, ...
'numFits', numFits, ...
'numTypes', numTypes ...
);
dataS.types = types;
dataS.useRows = useRows;
dataS.useXCols = useXCols;
dataS.useYCols = useYCols;
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function correlations = correlateProperties(xData, yData, dataStruct, ...
options, isShuffledTrial)
% Do the correlations, section by section.
% This routine calls different correlation functions based upon whether:
% yData is empty
% isShuffledTrial is true/false
% if true, correlations is just a numFits x 1 array
% if false, correlations is a structure containing a lot of
% supplemental information about the results of the correlation
numFits = dataStruct.numFits;
if isShuffledTrial
correlations = zeros(numFits, 1);
else
correlations = struct ( ...
'label', dataStruct.corrLabel, ...
'fitErr', zeros(numFits, 1), ...
'rSquared', zeros(numFits, 1), ...
'pValues', zeros(numFits, 1), ...
'sections', [] ...
);
correlations.descriptions = cell(numFits, 1);
correlations.indices = cell(numFits, 1);
correlations.propVals = cell(numFits, 1);
correlations.predictedPropVals = cell(numFits, 1);
correlations.coefs = cell(numFits, 1);
end
if isempty(yData)
if isShuffledTrial
% find correlations between each column of xData and (shuffled rows of)
% the remaining columns of xData
mainCorrRoutine = @(label, correlations, fitNum) ...
corrShuffledX(xData, dataStruct, correlations, fitNum);
else
% find correlations between each element of xData and the rest of xData
mainCorrRoutine = @(label, correlations, fitNum) ...
corrX(xData, options, dataStruct, ...
label, correlations, fitNum);
end
else
if isShuffledTrial
% find correlations between indpendently shuffled xData and yData
mainCorrRoutine = @(label, correlations, fitNum) ...
corrShuffledXY(xData, yData, dataStruct, correlations, fitNum);
else
% find correlations between xData and yData
mainCorrRoutine = @(label, correlations, fitNum) ...
corrXY(xData, yData, options, dataStruct, ...
label, correlations, fitNum);
end
end
%First do the correlation with all cells grouped together
fitNum = 1;
if options.fitPooled
label = 'Pooled';
correlations = mainCorrRoutine(label, correlations, fitNum);
fitNum = fitNum + 1;
end
%Now separate the cells out by type:
if options.fitSubTypes
for n = 1:dataStruct.numSubTypes
label = dataStruct.types{n};
correlations = mainCorrRoutine(label, correlations, fitNum);
fitNum = fitNum + 1;
end
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function correlations = corrShuffledX(xData, dataStruct, ...
correlations, fitNum)
% find correlations between each column of xData and (shuffled rows of)
% the remaining columns of xData
useRows = dataStruct.useRows{fitNum};
xCols = dataStruct.useXCols{fitNum};
numDataPoints = length(useRows);
numXProps = length(xCols);
% shuffle the order of the rows (the data point labels)
xRows = useRows(randperm(numDataPoints));
yRows = useRows(randperm(numDataPoints));
% use the valid data points and properties
x = xData(xRows, xCols);
y = xData(yRows, xCols);
%Fit each (shuffled) column with linear combinations of the other columns
% for shuffled trials, just get the fit error
n1 = 1 + (fitNum - 1) * numXProps;
for propNum = 1:numXProps
propDiff = setdiff(1:numXProps, propNum);
n = n1 + propNum - 1;
correlations(n) = FindCorrelations(x(:,propDiff), y(:,propNum));
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function correlations = corrX(xData, options, dataStruct, ...
label, correlations, fitNum)
useRows = dataStruct.useRows{fitNum};
xCols = dataStruct.useXCols{fitNum};
numDataPoints = length(useRows);
numXProps = length(xCols);
% use the valid data points and properties
x = xData(useRows, xCols);
xDataLabel = options.xLabel;
xFitLabels = cell(numXProps, 1);
for n = 1:numXProps
xFitLabels{n} = [label, ': ', options.xPropLabels{xCols(n)}];
end
%Fit yData with linear combinations of xData
n1 = 1 + (fitNum - 1) * numXProps;
n2 = n1 + (numXProps - 1);
for propNum = 1:numXProps
% get some additional information, because we care about e.g., which
% properties are fit
propDiff = setdiff(1:numXProps, propNum);
n = n1 + propNum - 1;
[correlations.fitErr(n), correlations.rSquared(n), ...
correlations.indices(n), correlations.propVals(n), ...
correlations.predictedPropVals(n), correlations.coefs(n), ...
correlations.pValues(n)] = ...
FindCorrelations(x(:,propDiff), x(:,propNum));
correlations.descriptions(n) = ...
getDescriptions(xFitLabels(propDiff), xFitLabels(propNum), ...
correlations.indices(n));
end
section.label = ...
sprintf('%s: Finding correlations among %s', label, xDataLabel);
section.inds = n1:n2;
section.fitLabels = xFitLabels;
section.paramLabels = xFitLabels;
correlations.sections = [correlations.sections, section];
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function correlations = corrShuffledXY(xData, yData, dataStruct, ...
correlations, fitNum)
useRows = dataStruct.useRows{fitNum};
xCols = dataStruct.useXCols{fitNum};
yCols = dataStruct.useYCols{fitNum};
numDataPoints = length(useRows);
numXProps = length(xCols);
numYProps = length(yCols);
% shuffle the order of the rows (the data point labels)
xRows = useRows(randperm(numDataPoints));
yRows = useRows(randperm(numDataPoints));
% use the valid data points and properties
x = xData(xRows, xCols);
y = yData(yRows, yCols);
%Fit yData with linear combinations of xData
n1 = 1 + (fitNum - 1) * (numXProps + numYProps);
n2 = n1 + numYProps - 1;
% for shuffled trials, just get the fit error
correlations(n1:n2) = FindCorrelations(x, y);
%Fit xData with linear combinations of yData
n1 = n2 + 1;
n2 = n2 + numXProps;
correlations(n1:n2) = FindCorrelations(y, x);
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function correlations = corrXY(xData, yData, options, dataStruct, ...
label, correlations, fitNum)
useRows = dataStruct.useRows{fitNum};
xCols = dataStruct.useXCols{fitNum};
yCols = dataStruct.useYCols{fitNum};
numXProps = length(xCols);
numYProps = length(yCols);
% use the valid data points and properties
x = xData(useRows, xCols);
y = yData(useRows, yCols);
xDataLabel = options.xLabel;
yDataLabel = options.yLabel;
xFitLabels = cell(numXProps, 1);
yFitLabels = cell(numYProps, 1);
for n = 1:numXProps
xFitLabels{n} = [label, ': ', options.xPropLabels{xCols(n)}];
end
for n = 1:numYProps
yFitLabels{n} = [label, ': ', options.yPropLabels{yCols(n)}];
end
%Fit yData with linear combinations of xData
n1 = 1 + (fitNum - 1) * (numXProps + numYProps);
n2 = n1 + numYProps - 1;
% get some additional information, because we care about e.g., which
% properties are fit
[correlations.fitErr(n1:n2), correlations.rSquared(n1:n2), ...
correlations.indices(n1:n2), correlations.propVals(n1:n2), ...
correlations.predictedPropVals(n1:n2), correlations.coefs(n1:n2), ...
correlations.pValues(n1:n2)] = FindCorrelations(x, y);
correlations.descriptions(n1:n2) = ...
getDescriptions(xFitLabels, yFitLabels, correlations.indices(n1:n2));
section.label = ...
sprintf('%s: Fitting %s with %s', label, yDataLabel, xDataLabel);
section.inds = n1:n2;
section.fitLabels = yFitLabels;
section.paramLabels = xFitLabels;
correlations.sections = [correlations.sections, section];
%Fit xData with linear combinations of yData
n1 = n2 + 1;
n2 = n2 + numXProps;
[correlations.fitErr(n1:n2), correlations.rSquared(n1:n2), ...
correlations.indices(n1:n2), correlations.propVals(n1:n2), ...
correlations.predictedPropVals(n1:n2), correlations.coefs(n1:n2), ...
correlations.pVals(n1:n2)] = FindCorrelations(y, x);
correlations.descriptions(n1:n2) = ...
getDescriptions(yFitLabels, xFitLabels, correlations.indices(n1:n2));
section.label = ...
sprintf('%s: fitting %s with %s', label, xDataLabel, yDataLabel);
section.inds = n1:n2;
section.fitLabels = xFitLabels;
section.paramLabels = yFitLabels;
correlations.sections = [correlations.sections, section];
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function descriptions = getDescriptions(paramLabels, fitLabels, indices)
% create a text description of the results of the fit
numFit = length(fitLabels);
descriptions = cell(numFit, 1);
for n = 1:numFit
% loop over each property that was fit and make a description for it
descriptions{n} = sprintf('%s fit with', fitLabels{n});
for ind = indices{n}
descriptions{n} = sprintf('%s %s', descriptions{n}, ...
stripSectionLabel(paramLabels{ind}));
end
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function stripped = stripSectionLabel(label)
n = strfind(label, ': ');
if isempty(n)
stripped = label;
else
n = n(1) + 2;
stripped = label(n:end);
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function resultS = shuffleCorrect(resultS, options)
% Compute the adjusted p-values and significance of a set of correlations
% by comparing them to the results of correlations of shuffled data.
% -Start by considering every possible fit in the scrambled data.
% -Loop through the fits being considered, starting with the best (lowest
% fitErr)
% -The adjusted p-value is the fraction of correlations in scrambled
% data that are better than the correlations in the best fit.
% -If the adjusted p-value is better than the significance level:
% -The fit is significant
% -That fit is removed from consideration
% -Looping continues with the next best fit
% -Otherwise, that fit and all remaining fits are not significant
sigLevel = options.significanceLevel;
numShuffle = size(resultS.shuffledFitErr, 2);
numFits = resultS.numFits;
% allocate some arrays
isSignificant = false(numFits, 1); % true/false for each fit
pAdjusted = ones(numFits, 1); % p-values adjusted for multiple correlations
% Sort the fit error from smallest to largest
[sortFitErr, sortInds] = sort(resultS.correlations.fitErr);
% Start by considering every possible fit in the scrambled data.
considerInds = 1:numFits;
for n = 1:numFits
%Loop through data, starting with the best fit (lowest fitError).
% Get the best fits to the considered shuffled data, and see how many are
% better
bestFits = min(resultS.shuffledFitErr(considerInds,:), [], 1);
numBetter = sum(bestFits < sortFitErr(n));
% Get a p-value on this fit being better than fits to shuffled data
propNum = sortInds(n);
pAdjusted(propNum) = (numBetter + 1) / (numShuffle + 1);
if pAdjusted(propNum) < sigLevel
% The adjusted p-value is small enough, so the fit is % significant.
isSignificant(propNum) = true;
%Remove the shuffled trials of *this* fit from consideration,
% and go on to the next-best fit.
considerInds = setdiff(considerInds, propNum);
else
% The adjusted p-value is NOT small enough so this fit and all
% subsequent fits are NOT significant.
for k = (n+1):numFits
numBetter = sum(bestFits < sortFitErr(k));
propNum = sortInds(k);
pAdjusted(propNum) = (numBetter + 1) / (numShuffle + 1);
end
break
end
end
% copy p-values into resultS structure
resultS.pAdjusted = pAdjusted;
resultS.isSignificant = isSignificant;
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function resultS = holmBonferroniCorrect(resultS, options)
% Compute the adjusted p-values and significance of a set of correlations
% by by applying the Holm-Bonferroni correction
% -Start by considering every possible fit in the scrambled data.
% -Loop through the fits being considered, starting with the best (lowest
% pValues)
% -n = the number of fits being considered
% -The adjusted p-value = 1 - (1 - p)^n
% -If the adjusted p-value is better than the significance level:
% -The fit is significant
% -That fit is removed from consideration (i.e. n is decreased)
% -Looping continues with the next best fit
% -Otherwise, that fit and all remaining fits are not significant
sigLevel = options.significanceLevel;
[sortP, sortInd] = sort(resultS.correlations.pValues, 'descend');
numFits = resultS.numFits;
isSignificant = false(numFits, 1);
pAdjusted = ones(numFits, 1);
for n = numFits:-1:1
ind_n = sortInd(n);
pAdjusted(ind_n) = 1.0 - ( 1.0 - sortP(n) )^n;
if pAdjusted(ind_n) < sigLevel
isSignificant(ind_n) = true;
else
remaining = sortInd((n-1):-1:1);
pAdjusted(remaining) = ...
1.0 - ( 1.0 - resultS.correlations.pValues(remaining) ).^n;
break
end
end
% copy p-values into resultS structure
resultS.pAdjusted = pAdjusted;
resultS.isSignificant = isSignificant;
return