e1_taskPLS_memory_erp_nooutlier_LV1.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. % Create an overview plot featuring the results of the multivariate PLS
  2. % comparing spectral changes during the stimulus period under load
  3. clear all; cla; clc;
  4. currentFile = mfilename('fullpath');
  5. [pathstr,~,~] = fileparts(currentFile);
  6. cd(fullfile(pathstr,'..'))
  7. rootpath = pwd;
  8. pn.data = fullfile(rootpath, 'data', 'stats');
  9. pn.figures = fullfile(rootpath, 'figures');
  10. pn.tools = fullfile(rootpath, 'tools');
  11. addpath(genpath(fullfile(pn.tools, '[MEG]PLS', 'MEGPLS_PIPELINE_v2.02b')))
  12. addpath(fullfile(pn.tools, 'fieldtrip')); ft_defaults;
  13. addpath(genpath(fullfile(pn.tools, 'RainCloudPlots')));
  14. addpath(fullfile(pn.tools, 'BrewerMap'));
  15. addpath(fullfile(pn.tools, 'winsor'));
  16. % set custom colormap
  17. cBrew = brewermap(500,'RdBu');
  18. cBrew = flipud(cBrew);
  19. colormap(cBrew)
  20. load(fullfile(pn.data, 'e1_taskpls_erp_nooutlier.mat'),...
  21. 'stat', 'result', 'lvdat', 'lv_evt_list', 'num_chans', 'num_freqs', 'num_time')
  22. load(fullfile(rootpath, 'data','erp', ['sub-001_erp.mat']));
  23. elec = erp.scene_category{1}.elec;
  24. result.perm_result.sprob
  25. indLV = 1;
  26. lvdat = reshape(result.boot_result.compare_u(:,indLV), num_chans, num_freqs, num_time);
  27. stat.prob = lvdat;
  28. stat.mask = lvdat > 3 | lvdat < -3;
  29. % maskNaN = double(stat.mask);
  30. % maskNaN(maskNaN==0) = NaN;
  31. %% plot BSR loadings
  32. h = figure('units','centimeter','position',[0 0 15 10]);
  33. set(gcf,'renderer','Painters')
  34. hold on;
  35. statsPlot = [];
  36. statsPlot = cat(1, statsPlot,squeeze(nanmean(stat.mask(1:64,:,:).*stat.prob(1:64,:,:),1)));
  37. plot(stat.time,statsPlot, 'k')
  38. xlabel('Time [s from stim onset]'); ylabel('max abs BSR');
  39. title({'ERP changes'; ['p = ', num2str(round(result.perm_result.sprob(indLV),4))]})
  40. set(findall(gcf,'-property','FontSize'),'FontSize',18)
  41. %% plot multivariate topographies
  42. h = figure('units','centimeters','position',[0 0 10 10]);
  43. set(gcf,'renderer','Painters')
  44. cfg = [];
  45. cfg.layout = 'biosemi64.lay';
  46. cfg.parameter = 'powspctrm';
  47. cfg.comment = 'no';
  48. cfg.colormap = cBrew;
  49. cfg.colorbar = 'EastOutside';
  50. plotData = [];
  51. plotData.label = elec.label; % {1 x N}
  52. plotData.dimord = 'chan';
  53. cfg.zlim = [-2 2];
  54. cfg.figure = h;
  55. plotData.powspctrm = squeeze(nanmean(stat.mask(:,:,stat.time>.3 & stat.time<.8).*...
  56. stat.prob(:,:,stat.time>.3 & stat.time<.8),3));
  57. ft_topoplotER(cfg,plotData);
  58. cb = colorbar('location', 'EastOutside'); set(get(cb,'ylabel'),'string','Mean BSR');
  59. %% plot using raincloud plot
  60. groupsizes=result.num_subj_lst;
  61. conditions=lv_evt_list;
  62. conds = {'forgotten'; 'remembered'};
  63. condData = []; uData = [];
  64. for indGroup = 1
  65. if indGroup == 1
  66. relevantEntries = 1:groupsizes(1)*numel(conds);
  67. elseif indGroup == 2
  68. relevantEntries = groupsizes(1)*numel(conds)+1:...
  69. groupsizes(1)*numel(conds)+groupsizes(2)*numel(conds);
  70. end
  71. for indCond = 1:numel(conds)
  72. targetEntries = relevantEntries(conditions(relevantEntries)==indCond);
  73. condData{indGroup}(indCond,:) = result.vsc(targetEntries,indLV);
  74. uData{indGroup}(indCond,:) = result.usc(targetEntries,indLV);
  75. end
  76. end
  77. cBrew(1,:) = 2.*[.3 .1 .1];
  78. cBrew(2,:) = [.6 .6 .6];
  79. idx_outlier = cell(1); idx_standard = cell(1);
  80. for indGroup = 1
  81. dataToPlot = uData{indGroup}';
  82. % define outlier as lin. modulation that is more than three scaled median absolute deviations (MAD) away from the median
  83. X = [1 1; 1 2]; b=X\dataToPlot'; IndividualSlopes = b(2,:);
  84. outliers = isoutlier(IndividualSlopes, 'median');
  85. idx_outlier{indGroup} = find(outliers);
  86. idx_standard{indGroup} = find(outliers==0);
  87. end
  88. h = figure('units','centimeter','position',[0 0 10 8]);
  89. for indGroup = 1
  90. dataToPlot = uData{indGroup}';
  91. % read into cell array of the appropriate dimensions
  92. data = []; data_ws = [];
  93. for i = 1:2
  94. for j = 1:1
  95. data{i, j} = dataToPlot(:,i);
  96. % individually demean for within-subject visualization
  97. data_ws{i, j} = dataToPlot(:,i)-...
  98. nanmean(dataToPlot(:,:),2)+...
  99. repmat(nanmean(nanmean(dataToPlot(:,:),2),1),size(dataToPlot(:,:),1),1);
  100. data_nooutlier{i, j} = data{i, j};
  101. data_nooutlier{i, j}(idx_outlier{indGroup}) = [];
  102. data_ws_nooutlier{i, j} = data_ws{i, j};
  103. data_ws_nooutlier{i, j}(idx_outlier{indGroup}) = [];
  104. % sort outliers to back in original data for improved plot overlap
  105. data_ws{i, j} = [data_ws{i, j}(idx_standard{indGroup}); data_ws{i, j}(idx_outlier{indGroup})];
  106. end
  107. end
  108. % IMPORTANT: plot individually centered estimates, stats on uncentered estimates!
  109. set(gcf,'renderer','Painters')
  110. cla;
  111. cl = cBrew(indGroup,:);
  112. [~, dist] = rm_raincloud_fixedSpacing(data_ws, [.8 .8 .8],1);
  113. h_rc = rm_raincloud_fixedSpacing(data_ws_nooutlier, cl,1,[],[],[],dist);
  114. view([90 -90]);
  115. axis ij
  116. box(gca,'off')
  117. set(gca, 'YTickLabels', {conds{2}; conds{1}});
  118. yticks = get(gca, 'ytick'); ylim([yticks(1)-(yticks(2)-yticks(1))./2, yticks(2)+(yticks(2)-yticks(1))./1.5]);
  119. minmax = [min(min(cat(2,data_ws{:}))), max(max(cat(2,data_ws{:})))];
  120. xlim(minmax+[-0.2*diff(minmax), 0.2*diff(minmax)])
  121. ylabel('scene category'); xlabel({'Brainscore'; '[Individually centered]'})
  122. % test linear effect
  123. curData = [data_nooutlier{1, 1}, data_nooutlier{2, 1}];
  124. X = [1 1; 1 2]; b=X\curData'; IndividualSlopes = b(2,:);
  125. [~, p, ci, stats] = ttest(IndividualSlopes);
  126. %title(['M:', num2str(round(mean(IndividualSlopes),3)), '; p=', num2str(round(p,3))])
  127. title(['M:', num2str(round(mean(IndividualSlopes),3)), '; p=', num2str(round(result.perm_result.sprob(indLV),3))])
  128. end
  129. set(findall(gcf,'-property','FontSize'),'FontSize',14)