DD_Stats.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. %% define some variables
  2. clear
  3. % select dataset
  4. daSeI = 1; % 1: only Low/High; 2: 3/8 and Low/High
  5. anaType = 1; % 1: window-based analysis; 2: point-based analysis
  6. % define time windows to be analysed (in s)
  7. % low
  8. win1L = [0.00,0.01]; % ABR (whole)
  9. win2L = [0.0055,0.009]; % ABR (peak 5 only)
  10. win3L = [0.0045,0.0075]; % ABR (late)
  11. win4L = [0.006,0.010]; % ABR (omitted); original window ([0.007,0.017]) is too long for 25 ms dataset
  12. % high
  13. win1H = [0.00,0.008]; % ABR (whole)
  14. win2H = [0.0045,0.008]; % ABR (peak 5 only)
  15. win3H = [0.0045,0.007]; % ABR (late)
  16. win4H = [0.0065,0.01]; % ABR (omitted)
  17. % win1L = [0.000,0.0075]; % ABR (whole)
  18. % win2L = [0.000,0.0045]; % ABR (early)
  19. % win3L = [0.0045,0.0075]; % ABR (late)
  20. % win4L = [0.000,0.007]; % ABR (omitted); original window ([0.007,0.017]) is too long for 25 ms dataset
  21. % % high
  22. % win1H = [0,0.007]; % ABR (whole)
  23. % win2H = [0,0.0045]; % ABR (early)
  24. % win3H = [0.0045,0.007]; % ABR (late)
  25. % win4H = [0,0.007]; % ABR (omitted)
  26. winTxt = ["ABR","ABR early","ABR late","Omission Response"];
  27. inclCtrl = 0; % include control data? (0: no, 1: 50Per, 2: MR, 3: DevOnly)
  28. omRespI = 0; % run stats for omission responses? (0: no, 1: yes)
  29. % define number of std-position datasets
  30. switch omRespI
  31. case 0
  32. nSubStd = 2; % 2 for all datasets
  33. case 1
  34. nSubStd = 1; % except Om dataset (which has only std after dev)
  35. end
  36. %% load datasets
  37. % load control data and rename important variables
  38. for repRateI = 1:3 % once for each rep rate dataset
  39. for subStdI = 1:nSubStd % once for each std position dataset
  40. switch inclCtrl
  41. case {0,3} % use different rep rate datasets only if no ctrl or dev only control is included (because there are no control data for 25 and 30 ms)
  42. switch repRateI
  43. case 1
  44. repRate = '25ms';
  45. case 2
  46. repRate = '30ms';
  47. case 3
  48. repRate = '50ms';
  49. end
  50. case {1,2} % if 50Per or MR control is included, only use 50 ms dataset
  51. repRate = '50ms';
  52. end
  53. switch subStdI
  54. case 1
  55. subStd = 'SaD';
  56. case 2
  57. subStd = 'SbD';
  58. end
  59. switch omRespI
  60. case 0
  61. switch inclCtrl
  62. case 1
  63. load(['DD_avData_50Per_',repRate,'_',subStd,'.mat'])
  64. dataAv_cell_Ctrl = dataAv_cell; % rename
  65. fileI_Oddb_cell_Ctrl = fileI_Oddb_cell; % rename
  66. fileI_Ctrl_cell_Ctrl = fileI_Ctrl_cell; % rename
  67. case 2
  68. load(['DD_avData_MR_',repRate,'_',subStd,'.mat'])
  69. dataAv_cell_Ctrl = dataAv_cell; % rename
  70. fileI_Oddb_cell_Ctrl = fileI_Oddb_cell; % rename
  71. fileI_Ctrl_cell_Ctrl = fileI_Ctrl_cell; % rename
  72. case 3
  73. load(['DD_avData_LowOnly_',repRate,'_',subStd,'.mat'])
  74. dataAv_cell_LowOnly = dataAv_cell; % rename
  75. fileI_Oddb_cell_Ctrl = fileI_Oddb_cell; % rename - not necessary to do the same with corresponding "high" file indices, since they should be the same between high and low
  76. fileI_Ctrl_cell_Ctrl = fileI_Ctrl_cell; % rename - not necessary to do the same with corresponding "high" file indices, since they should be the same between high and low
  77. load(['DD_avData_HighOnly_',repRate,'_',subStd,'.mat'])
  78. dataAv_cell_HighOnly = dataAv_cell; % rename
  79. end
  80. % load oddball data
  81. load(['DD_avData_Oddball_',repRate,'_',subStd,'.mat'])
  82. case 1
  83. inclCtrl = 0; % no control for OM data
  84. % load OM data
  85. load(['DD_avData_HighOm_',repRate,'.mat'])
  86. dataAv_cell_HighOm = dataAv_cell; % rename
  87. load(['DD_avData_LowOm_',repRate,'.mat'])
  88. dataAv_cell_LowOm = dataAv_cell; % rename
  89. end
  90. %% do some additional calulations that are required
  91. fs = fs_cell{1}(1);
  92. pts2begin = pts2begin_cell{1}(1);
  93. winAll{1} = round([win1L;win2L;win3L;win4L]*fs)+pts2begin+round(blcEnd(1)*fs+1); % combine time windows, convert them into points and correct for stim delay (pts2begin) and baseline correction window (blcEnd = stimulus peak)
  94. winAll{2} = round([win1H;win2H;win3H;win4H]*fs)+pts2begin+round(blcEnd(2)*fs+1);
  95. switch anaType
  96. case 1 % window-based analysis
  97. nSmpl = size(winAll{1},1); % number of windows to be analysed
  98. case 2 % point-based analysis
  99. nSmpl = size(dataAv_cell{1}{1},1); % number of points of each recording
  100. end
  101. nStims = 2; % number of different stimuli per combination
  102. nFilt = nFilt_cell{1};
  103. switch inclCtrl
  104. case 0
  105. switch omRespI
  106. case 0
  107. fileName = 'DD_statsData_noCtrl';
  108. case 1
  109. fileName = 'DD_statsData_Om';
  110. % replace data in dataAv_cell with omission response data
  111. for c = 1:nComb
  112. dataAv_cell{c}{1} = dataAv_cell_HighOm{c}{3};
  113. dataAv_cell{c}{2} = dataAv_cell_HighOm{c}{4};
  114. dataAv_cell{c}{3} = dataAv_cell_LowOm{c}{1};
  115. dataAv_cell{c}{4} = dataAv_cell_LowOm{c}{2};
  116. end
  117. end
  118. nCond = 2; % 2 conditions: dev and std
  119. data = dataAv_cell; % only oddball data
  120. case {1,2,3}
  121. switch inclCtrl
  122. case 1
  123. fileName = 'DD_statsData_50Per';
  124. case 2
  125. fileName = 'DD_statsData_MR';
  126. case 3
  127. fileName = 'DD_statsData_DevO';
  128. end
  129. nCond = 3; % 3 conditions: dev, std and control
  130. data = cell(size(dataAv_cell));
  131. for c = 1:nComb % once for each stimulus combination
  132. if inclCtrl==3 % for DevOnly control
  133. dataAv_cell_Ctrl{c}{1} = dataAv_cell_LowOnly{c}{1}; % assign DevOnly Low data as control of stimulus 1
  134. dataAv_cell_Ctrl{c}{2} = dataAv_cell_HighOnly{c}{3}; % assign DevOnly High data as control of stimulus 2
  135. end
  136. for s = 1:nStims % once for each stimulus frequency
  137. for cn = 1:nCond % once for each stimulus condition (dev, std, ctrl)
  138. switch cn % change dataset depending on condition (Oddball vs. control)
  139. case {1,2} % if oddball
  140. data{c}{(s-1)*nCond+cn} = dataAv_cell{c}{(s-1)*2+cn}(:,fileI_Oddb_cell_Ctrl{c},:); % concatenate oddball data of respective stimulus & condition (dev/std)
  141. case 3 % if control
  142. data{c}{(s-1)*nCond+cn} = dataAv_cell_Ctrl{c}{s}(:,fileI_Ctrl_cell_Ctrl{c},:); % concatenate control data of respective stimulus
  143. end
  144. end
  145. end
  146. end
  147. end
  148. %% calulate statistical variable (RMS, mean V, etc.) for each sample (points or windows)
  149. varS = cell(1,nComb); % preallocate
  150. maxV = cell(1,nComb); % preallocate
  151. maxI = cell(1,nComb); % preallocate
  152. timS = cell(1,nComb); % preallocate
  153. varCsS = cell(1,nComb); % preallocate
  154. maxCsV = cell(1,nComb); % preallocate
  155. maxCsI = cell(1,nComb); % preallocate
  156. timCsS = cell(1,nComb); % preallocate
  157. varAvCsS = cell(1,nComb); % preallocate
  158. varSeCsS = cell(1,nComb); % preallocate
  159. timAvCsS = cell(1,nComb); % preallocate
  160. timSeCsS = cell(1,nComb); % preallocate
  161. varGrAvCsS = cell(1,nComb); % preallocate
  162. maxGrAvCsV = cell(1,nComb); % preallocate
  163. maxGrAvCsI = cell(1,nComb); % preallocate
  164. timGrAvCsS = cell(1,nComb); % preallocate
  165. for c = 1:nComb % once for each stimulus combination
  166. nFiles = nFiles_cell{c};
  167. varS{c} = zeros(nFiles,nStims,nCond,nFilt,nSmpl); % preallocate
  168. maxV{c} = zeros(nFiles,nStims,nCond,nFilt,nSmpl); % preallocate
  169. maxI{c} = zeros(nFiles,nStims,nCond,nFilt,nSmpl); % preallocate
  170. timS{c} = zeros(nFiles,nStims,nCond,nFilt,nSmpl); % preallocate
  171. varCsS{c} = zeros(nFiles,nStims,nFilt,nSmpl,nConS); % preallocate
  172. maxCsV{c} = zeros(nFiles,nStims,nFilt,nSmpl,nConS); % preallocate
  173. maxCsI{c} = zeros(nFiles,nStims,nFilt,nSmpl,nConS); % preallocate
  174. timCsS{c} = zeros(nFiles,nStims,nFilt,nSmpl,nConS); % preallocate
  175. varAvCsS{c} = zeros(nStims,nFilt,nSmpl,nConS); % preallocate
  176. varSeCsS{c} = zeros(nStims,nFilt,nSmpl,nConS); % preallocate
  177. timAvCsS{c} = zeros(nStims,nFilt,nSmpl,nConS); % preallocate
  178. timSeCsS{c} = zeros(nStims,nFilt,nSmpl,nConS); % preallocate
  179. varGrAvCsS{c} = zeros(nStims,nFilt,nSmpl,nConS); % preallocate
  180. maxGrAvCsV{c} = zeros(nStims,nFilt,nSmpl,nConS); % preallocate
  181. maxGrAvCsI{c} = zeros(nStims,nFilt,nSmpl,nConS); % preallocate
  182. timGrAvCsS{c} = zeros(nStims,nFilt,nSmpl,nConS); % preallocate
  183. for s = 1:nStims % once for each stimulus frequency
  184. for cn = 1:nCond % once for each stimulus condition (dev, std, (ctrl))
  185. for ft = 1:nFilt % once for each filter
  186. for sp = 1:nSmpl % once for each sample (windows or points)
  187. switch anaType
  188. case 1
  189. varS{c}(:,s,cn,ft,sp) = peak2peak(data{c}{(s-1)*nCond+cn}((winAll{s}(sp,1):winAll{s}(sp,2)),:,ft)); % calculate variable per window
  190. [maxV{c}(:,s,cn,ft,sp),maxI{c}(:,s,cn,ft,sp)] = max(data{c}{(s-1)*nCond+cn}((winAll{s}(sp,1):winAll{s}(sp,2)),:,ft)); % identify peak and peak index (=timing) per window
  191. timS{c}(:,s,cn,ft,sp) = (maxI{c}(:,s,cn,ft,sp)+(winAll{s}(sp,1)-pts2begin-round(blcEnd(s)*fs+1)))/fs*1000; % convert points to timing
  192. % standard cluster analysis
  193. if cn==1 % only 1 condition exists
  194. for cs = 1:nConS % once for each consecutive std
  195. varCsS{c}(:,s,ft,sp,cs) = peak2peak(conSAv{c}((winAll{s}(sp,1):winAll{s}(sp,2)),s,:,cs,ft)); % calculate variable per window
  196. [maxCsV{c}(:,s,ft,sp,cs),maxCsI{c}(:,s,ft,sp,cs)] = max(conSAv{c}((winAll{s}(sp,1):winAll{s}(sp,2)),s,:,cs,ft)); % identify peak and peak index (=timing) per window
  197. timCsS{c}(:,s,ft,sp,cs) = maxCsI{c}(:,s,ft,sp,cs)/fs*1000; % convert points to timing
  198. varAvCsS{c}(s,ft,sp,cs) = mean(varCsS{c}(:,s,ft,sp,cs),1); % calculate average from values of individual responses
  199. varStdDCsS = std(varCsS{c}(:,s,ft,sp,cs),0,1);
  200. varSeCsS{c}(s,ft,sp,cs) = varStdDCsS/sqrt(nFiles);
  201. timAvCsS{c}(s,ft,sp,cs) = mean(timCsS{c}(:,s,ft,sp,cs),1);
  202. timStdDCsS = std(timCsS{c}(:,s,ft,sp,cs),0,1);
  203. timSeCsS{c}(s,ft,sp,cs) = timStdDCsS/sqrt(nFiles);
  204. % additionally
  205. varGrAvCsS{c}(s,ft,sp,cs) = peak2peak(conSGrAv{c}((winAll{s}(sp,1):winAll{s}(sp,2)),s,cs,ft)); % calculate value from grand averaged response
  206. [maxGrAvCsV{c}(s,ft,sp,cs),maxGrAvCsI{c}(s,ft,sp,cs)] = max(conSGrAv{c}((winAll{s}(sp,1):winAll{s}(sp,2)),s,cs,ft)); % identify peak and peak index (=timing) per window
  207. timGrAvCsS{c}(s,ft,sp,cs) = maxGrAvCsI{c}(s,ft,sp,cs)/fs*1000; % convert points to timing
  208. end
  209. end
  210. case 2
  211. varS{c}(:,s,cn,ft,sp) = (data{c}{(s-1)*nCond+cn}(sp,:,ft)); % calculate variable per point
  212. end
  213. end
  214. end
  215. end
  216. end
  217. end
  218. %% run t-test or ANOVA on each time window
  219. cohensD_V = zeros(nComb,nStims,nFilt,nSmpl,3); % preallocate
  220. cohensDT_V = zeros(nComb,nStims,nFilt,nSmpl,3); % preallocate
  221. switch inclCtrl
  222. case 0
  223. hV = zeros(nComb,nStims,nFilt,nSmpl); % preallocate
  224. pV = zeros(nComb,nStims,nFilt,nSmpl); % preallocate
  225. ciV = zeros(nComb,nStims,nFilt,nSmpl,2); % preallocate
  226. statsV = cell(nComb,nStims,nFilt,nSmpl); % preallocate
  227. hTV = zeros(nComb,nStims,nFilt,nSmpl); % preallocate
  228. pTV = zeros(nComb,nStims,nFilt,nSmpl); % preallocate
  229. ciTV = zeros(nComb,nStims,nFilt,nSmpl,2); % preallocate
  230. statsTV = cell(nComb,nStims,nFilt,nSmpl); % preallocate
  231. case {1,2,3}
  232. anovaV = cell(nComb,nStims,nFilt,nSmpl); % preallocate
  233. multcompV = cell(nComb,nStims,nFilt,nSmpl); % preallocate
  234. within = table([1,2,3]','VariableNames',{'stim_prob'}); % define within model
  235. end
  236. for c = 1:nComb % once for each stimulus combination
  237. for s = 1:nStims % once for each stimulus frequency
  238. for ft = 1:nFilt % once for each filter
  239. for sp = 1:nSmpl % once for each time window
  240. % amplitude
  241. cohensD_V(c,s,ft,sp,1) = CohensD(varS{c}(:,s,1,ft,sp),varS{c}(:,s,2,ft,sp)); % calculate Cohens D; dev vs. std
  242. [hV(c,s,ft,sp),pV(c,s,ft,sp),ciV(c,s,ft,sp,:),statsV{c,s,ft,sp}] = ttest(varS{c}(:,s,1,ft,sp),varS{c}(:,s,2,ft,sp)); % run t-test
  243. % timing
  244. cohensDT_V(c,s,ft,sp,1) = CohensD(timS{c}(:,s,2,ft,sp),timS{c}(:,s,1,ft,sp)); % calculate Cohens D; dev vs. std
  245. [hTV(c,s,ft,sp),pTV(c,s,ft,sp),ciTV(c,s,ft,sp,:),statsTV{c,s,ft,sp}] = ttest(timS{c}(:,s,2,ft,sp),timS{c}(:,s,1,ft,sp)); % run t-test
  246. switch inclCtrl
  247. case {1,2,3} % if control is included
  248. t = table(varS{c}(:,s,1,ft,sp),varS{c}(:,s,2,ft,sp),varS{c}(:,s,3,ft,sp),'VariableNames',{'dev','std','ctrl'}); % required for ANOVA
  249. rm = fitrm(t,'dev-ctrl~1','WithinDesign',within); % required for ANOVA
  250. anovaV{c,s,ft,sp} = ranova(rm); % run ANOVA
  251. multcompV{c,s,ft,sp} = multcompare(rm,'stim_prob','ComparisonType','bonferroni'); % run multiple comparison to find differences between responses
  252. cohensD_V(c,s,ft,sp,2) = CohensD(varS{c}(:,s,1,ft,sp),varS{c}(:,s,3,ft,sp)); % calculate Cohens D; dev vs. ctrl
  253. cohensD_V(c,s,ft,sp,3) = CohensD(varS{c}(:,s,3,ft,sp),varS{c}(:,s,2,ft,sp)); % ctrl vs. std
  254. end
  255. end
  256. end
  257. end
  258. end
  259. %% save data
  260. fileName = append(fileName,['_',repRate]); % append rep Rate to file name
  261. if omRespI==0
  262. fileName = append(fileName,['_',subStd]); % append Std position to file name
  263. end
  264. switch inclCtrl
  265. case 0
  266. save(fileName,'winAll','winTxt','varS','hV','pV','ciV','statsV','cohensD_V','timS','hTV','pTV','ciTV','statsTV','cohensDT_V',...
  267. 'varCsS','timCsS','varAvCsS','varSeCsS','timAvCsS','timSeCsS','varGrAvCsS','timGrAvCsS')
  268. case {1,2,3}
  269. save(fileName,'winAll','winTxt','varS','hV','pV','ciV','statsV','anovaV','multcompV','cohensD_V')
  270. end
  271. end
  272. end