RS_Stats.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. %% define some variables
  2. clear
  3. % define time windows to be analysed (in s)
  4. % low
  5. win1L = [0.000,0.01]; % ABR (whole)
  6. win2L = [0.0055,0.009]; % ABR (peak 5 only)
  7. win3L = [0,0.023]; % ABR (late)
  8. win4L = [0.025,0.035]; % late component (overlaid with ABR in fast data set)
  9. % high
  10. win1H = [0,0.008]; % ABR (whole)
  11. win2H = [0.0045,0.008]; % ABR (peak 5 only)
  12. win3H = [0,0.023]; % ABR (late)
  13. win4H = [0.025,0.035]; % late component (overlaid with ABR in fast data set)
  14. winTxt = ["ABR","ABR early","ABR late","Omission Response"];
  15. %% run stats on suppression / entrainment effects within blocks and correlation between ABRs and slow waves
  16. for daSe = 1:2 % run stats for each SOA dataset
  17. % load data and define filename
  18. switch daSe
  19. case 1
  20. load('RS_avData_25ms.mat')
  21. filename = 'RS_statsData_25ms';
  22. int = [1,25]; % intervals for curve fitting (Rep# and time)
  23. case 2
  24. load('RS_avData_50ms.mat')
  25. filename = 'RS_statsData_50ms';
  26. int = [1,50]; % intervals for curve fitting (Rep# and time)
  27. end
  28. data = dataAv_cell; % rename
  29. winAllSti = cell(2,2);
  30. varS = cell(2,2); % preallocate
  31. varAvS = cell(2,2); % preallocate
  32. varStdDS = cell(2,2); % preallocate
  33. varSeS = cell(2,2); % preallocate
  34. meanV = cell(2,2); % preallocate
  35. meanGrAv = cell(2,2); % preallocate
  36. maxV = cell(2,2); % preallocate
  37. maxI = cell(2,2); % preallocate
  38. timS = cell(2,2); % preallocate
  39. timAvS = cell(2,2); % preallocate
  40. timStdDS = cell(2,2); % preallocate
  41. timSeS = cell(2,2); % preallocate
  42. for s = 1:2 % run once for each stimulus (low/high)
  43. for f = 1:2 % once for each stimulation type (block-stim and o1st-stim)
  44. % do some additional calulations that are required
  45. c = (f-1)*2+s; % index of dataset to be used
  46. fs = fs_cell{c};
  47. nResp = nResp_cell{c};
  48. pSiResp = pSiResp_cell{c};
  49. pts2begin = pts2begin_cell{c};
  50. winAll{1} = round([win1L;win2L;win3L;win4L]*fs)+pts2begin+round(blcEnd(s)*fs+1); % combine time windows, convert them into points and correct for stim delay (pts2begin)
  51. winAll{2} = round([win1H;win2H;win3H;win4H]*fs)+pts2begin+round(blcEnd(s)*fs+1);
  52. nWin = size(winAll{1},1);
  53. winAllSti{c} = zeros(nWin,nResp,2); % preallocate
  54. for r = 1:nResp
  55. winAllSti{c}(:,r,:) = [(r-1)*pSiResp+winAll{s}(:,1),(r-1)*pSiResp+winAll{s}(:,2)]; % concatenate windows for each stimulus of the block
  56. end
  57. nFilt = nFilt_cell{1};
  58. % calulate RMS and mean values within defined time windows
  59. nFiles = nFiles_cell{c};
  60. varS{s,f} = zeros(nFiles,nFilt,nResp,nWin); % preallocate
  61. varAvS{s,f} = zeros(nFilt,nResp,nWin); % preallocate
  62. varStdDS{s,f} = zeros(nFilt,nResp,nWin); % preallocate
  63. varSeS{s,f} = zeros(nFilt,nResp,nWin); % preallocate
  64. maxV{s,f} = zeros(nFiles,nFilt,nResp,nWin); % preallocate
  65. maxI{s,f} = zeros(nFiles,nFilt,nResp,nWin); % preallocate
  66. timS{s,f} = zeros(nFiles,nFilt,nResp,nWin); % preallocate
  67. timAvS{s,f} = zeros(nFilt,nResp,nWin); % preallocate
  68. timStdDS{s,f} = zeros(nFilt,nResp,nWin); % preallocate
  69. timSeS{s,f} = zeros(nFilt,nResp,nWin); % preallocate
  70. for ft = 1:nFilt % once for each filter
  71. for r = 1:nResp
  72. for w = 1:nWin % once for each time window
  73. % amplitude
  74. varS{s,f}(:,ft,r,w) = peak2peak(data{c}(winAllSti{c}(w,r,1):winAllSti{c}(w,r,2),:,ft)); % calculate stats value
  75. varAvS{s,f}(ft,r,w) = mean(varS{c}(:,ft,r,w),1); % calculate grand average of the stats value
  76. varStdDS{s,f}(ft,r,w) = std(varS{c}(:,ft,r,w),0,1); % calculate standard deviation
  77. varSeS{s,f}(ft,r,w) = varStdDS{s,f}(ft,r,w)/sqrt(nFiles); % calculate std error
  78. % timing
  79. [maxV{s,f}(:,ft,r,w),maxI{s,f}(:,ft,r,w)] = max(data{c}(winAllSti{c}(w,r,1):winAllSti{c}(w,r,2),:,ft)); % identify peak and peak index (=timing) per window
  80. timS{s,f}(:,ft,r,w) = (maxI{s,f}(:,ft,r,w)+(winAllSti{c}(w,1,1)-pts2begin-round(blcEnd(s)*fs+1)))/fs*1000; % convert points to timing
  81. % timS{s,f}(:,ft,r,w) = (maxI{s,f}(:,ft,r,w))/fs*1000; % convert points to timing
  82. timAvS{s,f}(ft,r,w) = mean(timS{c}(:,ft,r,w),1); % calculate grand average of the stats value
  83. timStdDS{s,f}(ft,r,w) = std(timS{c}(:,ft,r,w),0,1); % calculate standard deviation
  84. timSeS{s,f}(ft,r,w) = timStdDS{s,f}(ft,r,w)/sqrt(nFiles); % calculate std error
  85. % other stuff
  86. meanV{s,f}(:,ft,r,w) = mean(data{c}(winAllSti{c}(w,r,1):winAllSti{c}(w,r,2),:,ft)); % calculate mean
  87. meanGrAv{s,f}(ft,r,w) = mean(meanV{c}(:,ft,r,w),1); % calculate grand average of the mean
  88. end
  89. end
  90. end
  91. end
  92. end
  93. % run t-test on each time window
  94. cohensD_V = zeros(nComb,nFilt,nResp,nWin); % preallocate
  95. hV = zeros(nComb,nFilt,nResp,nWin); % preallocate
  96. pV = zeros(nComb,nFilt,nResp,nWin); % preallocate
  97. ciV = zeros(nComb,nFilt,nResp,nWin,2); % preallocate
  98. statsV = cell(nComb,nFilt,nResp,nWin); % preallocate
  99. stdD_rms = zeros(nComb,nFilt,nResp,nWin); % preallocate
  100. cohensD_V_Bvs1 = zeros(nComb,nFilt,2,nWin); % preallocate
  101. hV_Bvs1 = zeros(nComb,nFilt,2,nWin); % preallocate
  102. pV_Bvs1 = zeros(nComb,nFilt,2,nWin); % preallocate
  103. ciV_Bvs1 = zeros(nComb,nFilt,2,nWin,2); % preallocate
  104. statsV_Bvs1 = cell(nComb,nFilt,2,nWin); % preallocate
  105. stdD_rms_Bvs1 = zeros(nComb,nFilt,2,nWin); % preallocate
  106. ciV_1st = zeros(2,nFilt,nWin,2,2); % preallocate
  107. fV = cell(2,2,nFilt,nWin); % preallocate
  108. gofV = cell(2,2,nFilt,nWin); % preallocate
  109. mdl = cell(2,2,nFilt,nWin); % preallocate
  110. for s = 1:2 % run once for each stimulus (low/high)
  111. for f = 1:2 % once for each stimulation type (block-stim and o1st-stim)
  112. c = (f-1)*2+s; % index of dataset to be used
  113. nResp = nResp_cell{c};
  114. for ft = 1:nFilt % once for each filter
  115. for r = 1:nResp % once for each stimulus
  116. for w = 1:nWin % once for each time window
  117. [hV(c,ft,r,w),pV(c,ft,r,w),ciV(c,ft,r,w,:),statsV{c,ft,r,w}] = ttest(varS{c}(:,ft,1,w),varS{c}(:,ft,r,w)); % run t-test; the first one vs. every window
  118. cohensD_V(c,ft,r,w) = CohensD(varS{c}(:,ft,1,w),varS{c}(:,ft,r,w)); % calculate Cohens D; the first one vs. every window
  119. stdD_rms(c,ft,r,w) = std(varS{c}(:,ft,r,w));
  120. end
  121. end
  122. % fit curve to RMS values of block-responses
  123. switch f
  124. case 1
  125. for w = 1:nWin
  126. % fitTy = fittype('V*exp(t/tau)+b','independent','t'); % model for exponential growth (different to bat data)
  127. % fitTy = fittype('L/(1+exp(-k*(t-x)))','independent','t'); % model for exponential growth (different to bat data)
  128. fitTy = fittype('poly1'); % model
  129. x = (1:nResp-1)';
  130. for v = 1:2 % once for amplitude, once for timing
  131. switch v
  132. case 1
  133. varTemp = varS;
  134. case 2
  135. varTemp = timS;
  136. end
  137. pd = fitdist(varTemp{s,f}(:,ft,1,w),'Normal'); % Fit a normal distribution object to the RMS data of the first response
  138. ciV_1st(s,ft,w,:,:) = paramci(pd); % calculate confidence intervals for the RMS data of the first response
  139. yMax = max(reshape(mean(varTemp{s,f}(:,ft,1:end-1,w),1),nResp-1,1)); % caluclate min of y (for normalisation)
  140. % y = reshape(mean(varTemp{s,f}(:,ft,1:end-1,w),1),nResp-1,1)/yMax; % normalise y to max of y
  141. y = reshape(mean(varTemp{s,f}(:,ft,1:end-1,w),1),nResp-1,1); % y without normalisation
  142. [fV{v,s,ft,w},gofV{v,s,ft,w}] = fit(x,y,fitTy); % fit model to data (fexible model)
  143. mdl{v,s,ft,w} = fitlm(x,y); % fit model to data (linear regression model)
  144. end
  145. end
  146. case 2
  147. % compare o1st response vs. first and last response of block stim
  148. for r = 1:2 % once for first and once for last response of block stim
  149. for w = 1:nWin % once for each time window
  150. switch r
  151. case 1 % the first block response vs. the o1st response
  152. [hV_Bvs1(s,ft,r,w),pV_Bvs1(s,ft,r,w),ciV_Bvs1(s,ft,r,w,:),statsV_Bvs1{s,ft,r,w}] = ttest(varS{s,1}(:,ft,1,w),varS{s,2}(:,ft,1,w)); % run t-test
  153. cohensD_V_Bvs1(s,ft,r,w) = CohensD(varS{s,1}(:,ft,1,w),varS{s,2}(:,ft,1,w)); % calculate Cohens D
  154. case 2 % the last block response vs. the o1st response
  155. [hV_Bvs1(s,ft,r,w),pV_Bvs1(s,ft,r,w),ciV_Bvs1(s,ft,r,w,:),statsV_Bvs1{s,ft,r,w}] = ttest(varS{s,1}(:,ft,end,w),varS{s,2}(:,ft,1,w)); % run t-test
  156. cohensD_V_Bvs1(s,ft,r,w) = CohensD(varS{s,1}(:,ft,end,w),varS{s,2}(:,ft,1,w)); % calculate Cohens D
  157. end
  158. end
  159. end
  160. end
  161. end
  162. end
  163. end
  164. for c = 1:nComb % once for each stimulus combination
  165. nResp = nResp_cell{c};
  166. % determine correlation between ABR-size and average value of the slow wave
  167. waveAbrSize = zeros(2,nResp,nComb,nWin);
  168. waveAbrNorm = zeros(2,nResp,nComb,nWin);
  169. RPea = zeros(nComb,nWin); % preallocate
  170. pRPea = zeros(nComb,nWin); % preallocate
  171. RSpea = zeros(nComb,nWin); % preallocate
  172. pRSpea = zeros(nComb,nWin); % preallocate
  173. for w = 1:nWin
  174. % individual averages
  175. abrSize = zeros(1,size(varS{c},1)*nResp); % preallocate
  176. waveSize = zeros(1,size(meanV{c},1)*nResp); % preallocate
  177. abrSize(:) = varS{c}(:,3,:,w); % RMS of the ABR response for the respective stimulus and window
  178. waveSize(:) = meanV{c}(:,5,:,w); % RMS of the ABR response for the respective stimulus and window
  179. abrNorm = (abrSize-min(abrSize))/(max(abrSize)-min(abrSize)); % normalise data between 0 and 1
  180. waveNorm = (waveSize-min(waveSize))/(max(waveSize)-min(waveSize)); % normalise data between 0 and 1
  181. % grand averages (work much better)
  182. abrSizeGrAv = varAvS{c}(3,:,w); % RMS of the ABR response for the respective stimulus and window
  183. abrNormGrAv = (abrSizeGrAv-min(abrSizeGrAv))/(max(abrSizeGrAv)-min(abrSizeGrAv)); % normalise data between 0 and 1
  184. waveSizeGrAv = meanGrAv{c}(5,:,w); % mean value of the slow wave for the respective stimulus and window
  185. waveNormGrAv = (waveSizeGrAv-min(waveSizeGrAv))/(max(waveSizeGrAv)-min(waveSizeGrAv)); % normalise data between 0 and 1
  186. waveAbrSize(:,:,c,w) = [abrSizeGrAv;waveSizeGrAv];
  187. waveAbrNorm(:,:,c,w) = [abrNormGrAv;waveNormGrAv];
  188. [RPea(c,w),pRPea(c,w)] = corr(waveAbrNorm(1,:,c,w)',waveAbrNorm(2,:,c,w)','Type','Pearson');
  189. [RSpea(c,w),pRSpea(c,w)] = corr(waveAbrNorm(1,:,c,w)',waveAbrNorm(2,:,c,w)','Type','Spearman');
  190. end
  191. end
  192. % save data
  193. save(filename,'int','winAllSti','winTxt','varS','varAvS','varSeS','meanV',...
  194. 'timS','timAvS','timSeS','meanGrAv','hV','pV','ciV','statsV',...
  195. 'stdD_rms','cohensD_V','ciV_1st','fV','gofV','hV_Bvs1',...
  196. 'pV_Bvs1','ciV_Bvs1','statsV_Bvs1','stdD_rms_Bvs1','cohensD_V_Bvs1',...
  197. 'waveAbrSize','waveAbrNorm','RPea','pRPea','RSpea','pRSpea','mdl')
  198. end