-
Notifications
You must be signed in to change notification settings - Fork 0
/
Script08_Hypothesis4a.m
110 lines (103 loc) · 4.62 KB
/
Script08_Hypothesis4a.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
clc;
clear all;
close all;
%% Hypothesis 4a: subsequent memory effect in voltage amplitudes of all channels
for Subject=[1:33]
All_channels=[1:65];
file_name=['Z:\projects\Hamid\Projects\EEGManyPipelines\Analyses\Subject',num2str(Subject,'%02.f'),...
'\','Subject',num2str(Subject,'%02.f'),'_preprocessed_data.mat'];
load(file_name,'Data_matrix','events','channels')
% Decoding
tr1=0;
tr2=0;
for trial=1:length(events)
if strcmp(events(trial).subsequent_memory,'subsequent_remembered')
tr1=tr1+1;
ClassA(:,:,tr1)=Data_matrix(All_channels,:,trial);
elseif strcmp(events(trial).subsequent_memory,'subsequent_forgotten')
tr2=tr2+1;
ClassB(:,:,tr2)=Data_matrix(All_channels,:,trial);
end
end
% Equalizing number of trials between classes by undersampling the
% dominant class and repeating the procedure untill all data is used:
% this is necessary to avoid bias in decoding
for time=1:size(Data_matrix,2)
ClassA_time=squeeze(ClassA(:,time,:));
ClassB_time=squeeze(ClassB(:,time,:));
folds=floor(size(ClassA_time,2)./size(ClassB_time,2));
if size(ClassA_time,2)>size(ClassB_time,2)
inds_tmp=1:size(ClassA_time,2);
inds_second=size(ClassB_time,2);
else
inds_tmp=1:length(ClassB_time,2);
inds_second=size(ClassA_time,2);
end
for fld=1:folds
inds=randsample(inds_tmp,inds_second);
for j=1:length(inds)
inds_tmp((inds_tmp==inds(j)))=[];
end
if size(ClassA_time,2)>size(ClassB_time,2)
Xready=[ClassA_time(:,inds)';ClassB_time'];
Yready=[ones(size(ClassA_time(:,inds),2),1);zeros(size(ClassB_time,2),1)]';
else
Xready=[ClassB_time(:,inds)';ClassA_time'];
Yready=[ones(size(ClassB_time(:,inds),2),1);zeros(size(ClassA_time,2),1)]';
end
Classifier_Model = fitcdiscr(Xready,Yready);
decoding(time,fld)=1-kfoldLoss(crossval(Classifier_Model));
end
[Subject time]
end
decoding_accuracy=nanmean(decoding,2);
file_name=['Z:\projects\Hamid\Projects\EEGManyPipelines\Analyses\Subject',num2str(Subject,'%02.f'),...
'\','Subject',num2str(Subject,'%02.f'),'_Results_Hypothesis4a.mat'];
save(file_name,'decoding_accuracy');
clearvars -except Subject All_channels
end
%% Plotting and statistical testing
clc;
clear all;
close all;
% loading decoding results from all participants
Accuracies=nan(33,769);
for Subject=1:33
file_name=['Z:\projects\Hamid\Projects\EEGManyPipelines\Analyses\Subject',num2str(Subject,'%02.f'),...
'\','Subject',num2str(Subject,'%02.f'),'_Results_Hypothesis4a.mat'];
load(file_name,'decoding_accuracy');
Accuracies(Subject,:)=decoding_accuracy;
end
% Evaluation of significance of effect against decoding in the baseline
% period
for time=1:size(Accuracies,2)
[p(time),h(time)]=signrank(Accuracies(:,time),nanmean(Accuracies(:,1:256),2),'tail','right');
end
% Corrrecting p values for multiple comparisons using Combined Probability of Fisher
threshold=0.05;
[pCorrected,~, ~, ~] = mt_fisher(reshape(p,1,[]), threshold);
pnew=zeros(size(pCorrected));
pnew(pCorrected<threshold)=1;
time_span_of_clustering= 5; % 5 = 10 ms: only the time and frequency spans
% in which all points are sginificant will be marked as significant
pCorrected=bwareaopen(pnew,time_span_of_clustering);
% plotting decoding curves and indicating time points of significant effect
xticks=[-256:512]*1000./512;
yticks=[45:5:60];
smoothing=5; % smoothing the decoding curve of clearer visualtion
plott=shadedErrorBar(xticks,nanmean(Accuracies*100),nanstd(Accuracies*100)*1.96./sqrt(33),{'color','b','LineWidth',2},1);
hold on;
plot([xticks(1) xticks(end)],[50 50],'k');
plot([0 0],[yticks(1) yticks(end)],'k');
Sig_plot=plot(xticks,pCorrected*47.5,'*');
set(gca,'FontSize',10,'LineWidth',1,'XTick',...
[xticks(1):250:xticks(end)],'XTickLabel',...
[xticks(1):250:xticks(end)],'YTick',...
[yticks],'YTickLabel',{[yticks]},...
'XMinorTick','on','YMinorTick','off','ycolor','k','tickdir','out','xcolor','k','box','off');
xlim([xticks(1) xticks(end)]);
ylim([yticks(1) yticks(end)]);
legend([plott.mainLine,plott.patch Sig_plot],{'Mean: remembered vs. forgotten';'95% CI';'Significant'},'location','northwest')
xlabel('Time relative to stimulus onset (ms)');
ylabel('Decoding accuracy (%)');
title ('H4a: subsequent memory effect in all channels');