g_Fig3FigS6Decoding.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. %analyze and plot the decoding data
  2. %NOTE: this program takes a really long time because of large ANOVAs
  3. clear all;
  4. load ('R_2R.mat');
  5. load ('RAW.mat');
  6. load ('PoolDec_2R.mat');
  7. load ('UnitDec_2R.mat');
  8. sucrose=[1 0.6 0.1];
  9. maltodextrin=[.9 0.3 .9];
  10. water=[0.00 0.75 0.75];
  11. total=[0.3 0.1 0.8];
  12. NAc=[0.5 0.1 0.8];
  13. VP=[0.3 0.7 0.1];
  14. NAcP{5,1}=[1 0.7 1];
  15. NAcP{4,1}=[0.85 0.3 0.9];
  16. NAcP{3,1}=[0.6 0.1 0.8];
  17. NAcP{2,1}=[0.3 0.05 0.5];
  18. NAcP{1,1}=[0.2 0 0.3];
  19. VPP{5,1}=[0.6 1 0.25];
  20. VPP{4,1}=[0.4 0.9 0.15];
  21. VPP{3,1}=[0.3 0.7 0.1];
  22. VPP{2,1}=[0.1 0.4 0.05];
  23. VPP{1,1}=[0.03 0.2 0];
  24. NAcShuff=[0.3 0.05 0.5];
  25. VPShuff=[0.05 0.4 0];
  26. %load parameters
  27. BinDura=R_2R.Param.BinDura;
  28. bins=R_2R.Param.bins;
  29. binint=R_2R.Param.binint;
  30. binstart=R_2R.Param.binstart;
  31. NumNeurons=R_2R.Param.NumNeurons;
  32. repetitions=length(PoolDec{1,1}.True{1,1}(:,1));
  33. testbins=R_2R.Param.SelectiveBins;
  34. xaxis=linspace(binstart+BinDura(2)/2,binstart+(bins-1)*binint+BinDura(2)/2,bins); %include all bins
  35. %% single unit decoding
  36. %divide neurons up by region
  37. NAneurons=strcmp(R_2R.Ninfo(:,3),'NA');
  38. VPneurons=strcmp(R_2R.Ninfo(:,3),'VP');
  39. %if you change this to e=1:3, you can also look at nonselective neurons
  40. for e=1:2 %different selections of neurons
  41. figure;
  42. %pick which set of neurons: all, reward-specific, or non-reward specific
  43. if e==1 selection=R_2R.SucN<2; end %all neurons
  44. if e==2 selection=R_2R.SucN | R_2R.MalN; end %reward-selective neurons
  45. if e==3 selection=(R_2R.SucN | R_2R.MalN) == 0; end %reward-nonselective neurons
  46. %get average accuracy for each bin and run stats comparing region
  47. for i = 1:bins
  48. AvgAccNAc(1,i)=nanmean(UnitDec.True(NAneurons&selection,i)); %average accuracy NAc
  49. SEMAccNAc(1,i)=nanste(UnitDec.True(NAneurons&selection,i),1); %SEM
  50. AvgAccNAcShuff(1,i)=nanmean(UnitDec.Shuff(NAneurons&selection,i)); %average accuracy NAcShuff
  51. SEMAccNAcShuff(1,i)=nanste(UnitDec.Shuff(NAneurons&selection,i),1); %SEM
  52. AvgAccVP(1,i)=nanmean(UnitDec.True(VPneurons&selection,i)); %average accuracy NAcShuff
  53. SEMAccVP(1,i)=nanste(UnitDec.True(VPneurons&selection,i),1); %SEM
  54. AvgAccVPShuff(1,i)=nanmean(UnitDec.Shuff(VPneurons&selection,i)); %average accuracy NAcShuff
  55. SEMAccVPShuff(1,i)=nanste(UnitDec.Shuff(VPneurons&selection,i),1); %SEM
  56. end
  57. %prepare shading
  58. upSEMNAc=AvgAccNAc+SEMAccNAc;
  59. downSEMNAc=AvgAccNAc-SEMAccNAc;
  60. upSEMVP=AvgAccVP+SEMAccVP;
  61. downSEMVP=AvgAccVP-SEMAccVP;
  62. upSEMNAcShuff=AvgAccNAcShuff+SEMAccNAcShuff;
  63. downSEMNAcShuff=AvgAccNAcShuff-SEMAccNAcShuff;
  64. upSEMVPShuff=AvgAccVPShuff+SEMAccVPShuff;
  65. downSEMVPShuff=AvgAccVPShuff-SEMAccVPShuff;
  66. %plotting decoder accuracies over time
  67. subplot(2,3,1);
  68. hold on;
  69. plot(xaxis,AvgAccNAc(1:bins),'Color', NAc,'linewidth',1.5); %accumbens
  70. plot(xaxis,AvgAccVP(1:bins),'Color', VP,'linewidth',1.5); %vp
  71. %shuffled
  72. plot(xaxis,AvgAccNAcShuff(1:bins),'Color', 'k','linewidth',1.5);
  73. plot(xaxis,AvgAccVPShuff(1:bins),'Color', 'k','linewidth',1.5);
  74. %error
  75. patch([xaxis,xaxis(end:-1:1)],[upSEMNAc,downSEMNAc(end:-1:1)],NAc,'EdgeColor','none');alpha(0.5);
  76. patch([xaxis,xaxis(end:-1:1)],[upSEMNAcShuff,downSEMNAcShuff(end:-1:1)],'k','EdgeColor','none');alpha(0.5);
  77. patch([xaxis,xaxis(end:-1:1)],[upSEMVP,downSEMVP(end:-1:1)],VP,'EdgeColor','none');alpha(0.5);
  78. patch([xaxis,xaxis(end:-1:1)],[upSEMVPShuff,downSEMVPShuff(end:-1:1)],'k','EdgeColor','none');alpha(0.5);
  79. xlabel('Seconds from reward delivery');
  80. ylabel('Accuracy');
  81. title('Unit decoding accuracy');
  82. legend('NAc units','VP units','Shuffled','Location','northwest');
  83. axis([xaxis(1) xaxis(end) 0.47 max(AvgAccVP)+0.04]);
  84. %find and plot bins exceeding confidence interval
  85. for i=1:bins
  86. %confidence interval for NAc
  87. x = UnitDec.Shuff(NAneurons&selection,i); % Create Data
  88. SEM = nanstd(x)/sqrt(length(x)); % Standard Error
  89. ts = tinv([0.005 0.995],length(x)-1); % T-Score
  90. CI = nanmean(x) + ts*SEM; % Confidence Intervals
  91. if nanmean(UnitDec.True(NAneurons&selection,i))>CI(2) NAcConf(1,i)=1; else NAcConf(1,i)=0; end
  92. %confidence interval for VP
  93. x = UnitDec.Shuff(VPneurons&selection,i); % Create Data
  94. SEM = nanstd(x)/sqrt(length(x)); % Standard Error
  95. ts = tinv([0.005 0.995],length(x)-1); % T-Score
  96. CI = nanmean(x) + ts*SEM; % Confidence Intervals
  97. if nanmean(UnitDec.True(VPneurons&selection,i))>CI(2) VPConf(1,i)=1; else VPConf(1,i)=0; end
  98. end
  99. %find consecutive bins
  100. R_2R.UnitNAcComp{e,1}=zeros(1,bins);
  101. R_2R.UnitVPComp{e,1}=zeros(1,bins);
  102. for i=2:bins-1
  103. if NAcConf(1,i)==1
  104. if NAcConf(1,i-1)==1 | NAcConf(1,i+1)==1
  105. R_2R.UnitNAcComp{e,1}(1,i)=1;
  106. end
  107. end
  108. if VPConf(1,i)==1
  109. if VPConf(1,i-1)==1 | VPConf(1,i+1)==1
  110. R_2R.UnitVPComp{e,1}(1,i)=1;
  111. end
  112. end
  113. end
  114. %check to see if the first bin above shuffled data would be different
  115. %if using fewer VP neurons to match NAc
  116. for j=1:20
  117. for i=1:bins
  118. matchedneurons=cat(1,ones(sum(NAneurons&selection),1),zeros(sum(VPneurons&selection)-sum(NAneurons&selection),1));
  119. matchedneurons=(matchedneurons(randperm(length(matchedneurons)))==1);
  120. %confidence interval for VP
  121. VPselectionSh=UnitDec.Shuff(VPneurons&selection,i);
  122. VPselection=UnitDec.True(VPneurons&selection,i);
  123. x = VPselectionSh(matchedneurons,1); % Create Data
  124. SEM = nanstd(x)/sqrt(length(x)); % Standard Error
  125. ts = tinv([0.005 0.995],length(x)-1); % T-Score
  126. CI = nanmean(x) + ts*SEM; % Confidence Intervals
  127. if nanmean(VPselection(matchedneurons,1))>CI(2) R_2R.VPaboveshuff{e,1}(j,i)=1; else R_2R.VPaboveshuff{e,1}(j,i)=0; end
  128. end
  129. end
  130. %plot
  131. plot(xaxis,R_2R.UnitVPComp{e,1}*0.48,'*','Color',VP);
  132. plot(xaxis,R_2R.UnitNAcComp{e,1}*0.485,'*','Color',NAc);
  133. %multiple comparisons for NAc vs VP
  134. %make a matrix indicating which region each neuron-bin comes from
  135. nbinregion=[];
  136. binname=[];
  137. rat=[];
  138. for i=1:bins %total number of bins being tested
  139. nbinregion=cat(2,nbinregion,NAneurons);
  140. binname=cat(2,binname,i*ones(length(NAneurons),1));
  141. rat=cat(2,rat,R_2R.Ninfo(:,4));
  142. end
  143. testtrue=UnitDec.True(selection,testbins(1)+1:testbins(2)+1); %becaue the bin 3 is actually the 4th entry, etc
  144. testshuff=UnitDec.Shuff(selection,testbins(1)+1:testbins(2)+1);
  145. testregion=nbinregion(selection,testbins(1)+1:testbins(2)+1);
  146. testbinname=binname(selection,testbins(1)+1:testbins(2)+1);
  147. testrat=rat(selection,testbins(1)+1:testbins(2)+1);
  148. %find effects of real vs shuffled, region, and bins on accuracy
  149. [~,R_2R.UnitDecStatsSubj{e,1},R_2R.UnitDecStatsSubj{e,2}]=anovan(cat(1,testtrue(:),testshuff(:)),{cat(1,zeros(length(testtrue(:)),1),ones(length(testshuff(:)),1)),cat(1,testregion(:),testregion(:)),cat(1,testbinname(:),testbinname(:)),cat(1,testrat(:),testrat(:))},'varnames',{'real vs shuffled','region','bins','subject'},'random',[4],'nested',[0 0 0 0;0 0 0 0;0 0 0 0;0 1 0 0],'display','off','model','full');
  150. % %do post-hoc comparisons
  151. % %this is commented out because can't do post-hoc comparison with nested anova (which we're using for including random effect of subject)
  152. % %if you do want to look at this in matlab, need to do the anova without subject (as written out here)
  153. %
  154. % [~,R_2R.UnitDecStats{e,1},R_2R.UnitDecStats{e,2}]=anovan(cat(1,testtrue(:),testshuff(:)),{cat(1,zeros(length(testtrue(:)),1),ones(length(testshuff(:)),1)),cat(1,testregion(:),testregion(:)),cat(1,testbinname(:),testbinname(:))},'varnames',{'real vs shuffled','region','bins'},'display','off','model','full');
  155. % [c,~,~,names]=multcompare(R_2R.UnitDecStats{e,2},'Dimension',[1 2 3],'CType','tukey-kramer','display','off');
  156. %
  157. % %find post-hoc differences
  158. % R_2R.UnitNAcVPComp{e,1}=NaN(2,bins);
  159. % for i=1:1+testbins(2)-testbins(1)
  160. % %NAc vs VP
  161. % Sel=c(:,1)==4*(i-1)+1 & c(:,2)==4*(i-1)+3;
  162. % if c(Sel,6)<0.05 R_2R.UnitNAcVPComp{e,1}(1,i+testbins(1))=1; else R_2R.UnitNAcVPComp{e,1}(1,i+testbins(1))=0; end
  163. % R_2R.UnitNAcVPComp{e,1}(2,i+testbins(1))=c(Sel,6);
  164. % end
  165. %
  166. % %add it to plot
  167. % plot(xaxis,R_2R.UnitNAcVPComp{e,1}(1,:)*(max(AvgAccVP)+0.015),'*','Color','k');
  168. %plotting CDF at peak bin
  169. subplot(2,3,4)
  170. hold on;
  171. %NAc
  172. [~,NAcbin]=max(AvgAccNAc);
  173. [cdfNAc,xNAc] = ecdf(UnitDec.True(NAneurons&selection,NAcbin));
  174. [cdfNAcSh,xNAcSh] = ecdf(UnitDec.Shuff(NAneurons&selection,NAcbin));
  175. plot(xNAc,cdfNAc,'Color',NAc,'linewidth',1.5);
  176. %VP
  177. [~,VPbin]=max(AvgAccVP);
  178. [cdfVP,xVP] = ecdf(UnitDec.True(VPneurons&selection,VPbin));
  179. [cdfVPSh,xVPSh] = ecdf(UnitDec.Shuff(VPneurons&selection,VPbin));
  180. plot(xVP,cdfVP,'Color',VP,'linewidth',1.5);
  181. %shuffled
  182. plot(xNAcSh,cdfNAcSh,'Color','k','linewidth',1.5);
  183. xlabel('Decoding accuracy')
  184. plot(xVPSh,cdfVPSh,'Color','k','linewidth',1.5);
  185. axis([0 1 0 1]);
  186. plot([0.5 0.5],[0 1],':','color','k','linewidth',0.75);
  187. title(['Accuracy at peak bin: ' num2str(((NAcbin-1)*binint)+(binstart+BinDura(2)/2)) 's NAc, ' num2str(((VPbin-1)*binint)+(binstart+BinDura(2)/2)) 's VP']);
  188. xlabel('Decoding accuracy');
  189. ylabel('Cumulative fraction of population');
  190. legend('NAc units','VP units','Shuffled','Location','northwest');
  191. %stats comparing peak bins
  192. [~,R_2R.UnitDecPeakBinSubj{e,1},~]=anovan(cat(1,UnitDec.True(NAneurons&selection,NAcbin),UnitDec.Shuff(NAneurons&selection,NAcbin),UnitDec.True(VPneurons&selection,VPbin),UnitDec.Shuff(VPneurons&selection,VPbin)),...
  193. {cat(1,zeros(sum(NAneurons&selection),1),ones(sum(NAneurons&selection),1),zeros(sum(VPneurons&selection),1),ones(sum(VPneurons&selection),1)),...
  194. cat(1,zeros(sum(NAneurons&selection),1),zeros(sum(NAneurons&selection),1),ones(sum(VPneurons&selection),1),ones(sum(VPneurons&selection),1)),...
  195. cat(1,R_2R.Ninfo(NAneurons&selection,4),R_2R.Ninfo(NAneurons&selection,4),R_2R.Ninfo(VPneurons&selection,4),R_2R.Ninfo(VPneurons&selection,4))},'varnames',{'real vs shuffled','region','subject'},'random',[3],'nested',[0 0 0;0 0 0;0 1 0],'display','off','model','full');
  196. %% pooled decoding
  197. %reset matrices for stats analysis
  198. A=[];
  199. B=[];
  200. C=[];
  201. D=[];
  202. %get average accuracy for each bin
  203. for v=1:length(PoolDec{e,1}.True) %each condition (number of neurons used)
  204. for i = 1:bins
  205. %NAc
  206. if length(PoolDec{e,1}.True{v,1})>1 %in case analysis wasn't run because not enough neurons
  207. PoolAccNAc{v,1}(1,i)=nanmean(PoolDec{e,1}.True{v,1}(:,i)); %average accuracy NAc
  208. PoolSEMNAc{v,1}(1,i)=nanste(PoolDec{e,1}.True{v,1}(:,i),1); %SEM
  209. PoolAccNAcShuff{v,1}(1,i)=nanmean(PoolDec{e,1}.Shuff{v,1}(:,i)); %average accuracy NAcShuff
  210. PoolSEMNAcShuff{v,1}(1,i)=nanste(PoolDec{e,1}.Shuff{v,1}(:,i),1); %SEM
  211. else
  212. PoolAccNAc{v,1}(1,i)=NaN;
  213. PoolSEMNAc{v,1}(1,i)=NaN;
  214. PoolAccNAcShuff{v,1}(1,i)=NaN;
  215. PoolSEMNAcShuff{v,1}(1,i)=NaN;
  216. end
  217. %VP
  218. if length(PoolDec{e,2}.True{v,1})>1 %in case analysis wasn't run because not enough neurons
  219. PoolAccVP{v,1}(1,i)=nanmean(PoolDec{e,2}.True{v,1}(:,i)); %average accuracy VP
  220. PoolSEMVP{v,1}(1,i)=nanste(PoolDec{e,2}.True{v,1}(:,i),1); %SEM
  221. PoolAccVPShuff{v,1}(1,i)=nanmean(PoolDec{e,2}.Shuff{v,1}(:,i)); %average accuracy VPshuff
  222. PoolSEMVPShuff{v,1}(1,i)=nanste(PoolDec{e,2}.Shuff{v,1}(:,i),1); %SEM
  223. else
  224. PoolAccVP{v,1}(1,i)=NaN;
  225. PoolSEMVP{v,1}(1,i)=NaN;
  226. PoolAccVPShuff{v,1}(1,i)=NaN;
  227. PoolSEMVPShuff{v,1}(1,i)=NaN;
  228. end
  229. end
  230. %find the time of the most accurate bin for each of the repetitions
  231. %(Following reward delivery)
  232. time0bin=round((((0-BinDura(2)/2)-binstart)/binint));
  233. for j = 1:repetitions
  234. %NAc
  235. if length(PoolDec{e,1}.True{v,1})>1 %in case analysis wasn't run because not enough neurons
  236. [~,PeakBinsNAc{v,1}(j,1)]=max(PoolDec{e,1}.True{v,1}(j,time0bin+1:end));
  237. PeakBinsNAc{v,1}(j,2)=(PeakBinsNAc{v,1}(j,1)-1)*binint;
  238. else
  239. PeakBinsNAc{v,1}(j,1)=NaN;
  240. PeakBinsNAc{v,1}(j,2)=NaN;
  241. end
  242. %VP
  243. if length(PoolDec{e,2}.True{v,1})>1 %in case analysis wasn't run because not enough neurons
  244. [~,PeakBinsVP{v,1}(j,1)]=max(PoolDec{e,2}.True{v,1}(j,time0bin+1:end));
  245. PeakBinsVP{v,1}(j,2)=(PeakBinsVP{v,1}(j,1)-1)*binint;
  246. else
  247. PeakBinsVP{v,1}(j,1)=NaN;
  248. PeakBinsVP{v,1}(j,2)=NaN;
  249. end
  250. end
  251. %prepare shading
  252. PupSEMNAc{v,1}=PoolAccNAc{v,1}+PoolSEMNAc{v,1};
  253. PdownSEMNAc{v,1}=PoolAccNAc{v,1}-PoolSEMNAc{v,1};
  254. PupSEMVP{v,1}=PoolAccVP{v,1}+PoolSEMVP{v,1};
  255. PdownSEMVP{v,1}=PoolAccVP{v,1}-PoolSEMVP{v,1};
  256. PupSEMNAcShuff{v,1}=PoolAccNAcShuff{v,1}+PoolSEMNAcShuff{v,1};
  257. PdownSEMNAcShuff{v,1}=PoolAccNAcShuff{v,1}-PoolSEMNAcShuff{v,1};
  258. PupSEMVPShuff{v,1}=PoolAccVPShuff{v,1}+PoolSEMVPShuff{v,1};
  259. PdownSEMVPShuff{v,1}=PoolAccVPShuff{v,1}-PoolSEMVPShuff{v,1};
  260. %here I plot the main lines for each condition that will have an entry
  261. %in the legend (legend goes by order plotted)
  262. %plotting decoder accuracy over time
  263. subplot(2,3,2); %accumbens
  264. hold on;
  265. plot(xaxis,PoolAccNAc{v,1}(1:bins),'Color', NAcP{v,1},'linewidth',1);
  266. %plot(xaxis,PoolAccNAcShuff{v,1}(1:66),'Color', NAcShuff,'linewidth',3);
  267. %plot(xaxis,NAcSig{v,1}-0.53,'*','Color', 'k');
  268. %patch([xaxis,xaxis(end:-1:1)],[PupSEMNAc{v,1},PdownSEMNAc{v,1}(end:-1:1)],NAc{v,1},'EdgeColor','none');alpha(0.5);
  269. %patch([xaxis,xaxis(end:-1:1)],[PupSEMNAcShuff{v,1},PdownSEMNAcShuff{v,1}(end:-1:1)],NAcShuff,'EdgeColor','none');alpha(0.5);
  270. xlabel('Seconds post reward delivery');
  271. ylabel('Accuracy');
  272. title('NAc decoding accuracy');
  273. axis([xaxis(1) xaxis(end) 0.4 1]);
  274. subplot(2,3,3); %VP
  275. hold on;
  276. plot(xaxis,PoolAccVP{v,1}(1:bins),'Color', VPP{v,1},'linewidth',1);
  277. %plot(xaxis,PoolAccVPShuff{v,1}(1:66),'Color', VPShuff,'linewidth',3);
  278. %plot(xaxis,VPSig{v,1}-0.53,'*','Color','k');
  279. %patch([xaxis,xaxis(end:-1:1)],[PupSEMVP{v,1},PdownSEMVP{v,1}(end:-1:1)],VP{v,1},'EdgeColor','none');alpha(0.5);
  280. %patch([xaxis,xaxis(end:-1:1)],[PupSEMVPShuff{v,1},PdownSEMVPShuff{v,1}(end:-1:1)],VPShuff,'EdgeColor','none');alpha(0.5);
  281. xlabel('Seconds post reward delivery');
  282. ylabel('Accuracy');
  283. title('VP decoding accuracy');
  284. axis([xaxis(1) xaxis(end) 0.4 1]);
  285. %comparison at best bin
  286. subplot(2,3,5);
  287. hold on;
  288. [~,NAcbin]=max(PoolAccNAc{v,1});
  289. [~,VPbin]=max(PoolAccVP{v,1});
  290. if length(PoolDec{e,1}.True{v,1})>1 && length(PoolDec{e,2}.True{v,1})>1
  291. %make matrices that will include best bin at all levels
  292. A=cat(1,A,PoolDec{e,1}.True{v,1}(:,NAcbin));
  293. B=cat(1,B,PoolDec{e,1}.Shuff{v,1}(:,NAcbin));
  294. C=cat(1,C,PoolDec{e,2}.True{v,1}(:,VPbin));
  295. D=cat(1,D,PoolDec{e,2}.Shuff{v,1}(:,VPbin));
  296. end
  297. errorbar(NumNeurons(v),PoolAccNAc{v,1}(NAcbin),PoolSEMNAc{v,1}(NAcbin),'*','Color', NAcP{v,1},'linewidth',1.5);
  298. errorbar(NumNeurons(v),PoolAccVP{v,1}(VPbin),PoolSEMVP{v,1}(VPbin),'*','Color', VPP{v,1},'linewidth',1.5);
  299. xlabel('Neurons used');
  300. ylabel('Accuracy');
  301. title('Accuracy for most predictive bin');
  302. legend('NAc','VP','Location','southeast');
  303. axis([0 155 0.5 1]);
  304. %plotting time of most accurate bin
  305. subplot(2,3,6);
  306. hold on;
  307. errorbar(NumNeurons(v),nanmean(PeakBinsNAc{v,1}(:,2)),nanste(PeakBinsNAc{v,1}(:,2),1),'*','Color', NAcP{v,1},'linewidth',1.5);
  308. errorbar(NumNeurons(v),nanmean(PeakBinsVP{v,1}(:,2)),nanste(PeakBinsVP{v,1}(:,2),1),'*','Color', VPP{v,1},'linewidth',1.5);
  309. xlabel('Neurons used');
  310. ylabel('Seconds post reward delivery')
  311. title('Time of most accurate bin');
  312. %legend('NAc','VP','Location','southeast');
  313. axis([0 155 0 2.5]);
  314. legend('NAc','VP','location','southeast')
  315. end %conditions
  316. %now plot the shading, significance, and the shuffled separately
  317. for v=1:length(PoolDec{e,1}.True) %each condition (number of neurons used)
  318. %plotting decoder accuracy over time
  319. subplot(2,3,2); %accumbens
  320. hold on;
  321. plot(xaxis,PoolAccNAcShuff{v,1}(1:66),'Color', 'k','linewidth',1);
  322. patch([xaxis,xaxis(end:-1:1)],[PupSEMNAc{v,1},PdownSEMNAc{v,1}(end:-1:1)],NAcP{v,1},'EdgeColor','none');alpha(0.5);
  323. patch([xaxis,xaxis(end:-1:1)],[PupSEMNAcShuff{v,1},PdownSEMNAcShuff{v,1}(end:-1:1)],'k','EdgeColor','none');alpha(0.5);
  324. xlabel('Seconds post reward delivery');
  325. title('NAc decoding accuracy');
  326. axis([xaxis(1) xaxis(end) 0.4 1]);
  327. legend('10 units','25 units','50 units','100 units','150 units','Shuffled','location','northwest')
  328. subplot(2,3,3); %VP
  329. hold on;
  330. plot(xaxis,PoolAccVPShuff{v,1}(1:66),'Color', 'k','linewidth',1);
  331. patch([xaxis,xaxis(end:-1:1)],[PupSEMVP{v,1},PdownSEMVP{v,1}(end:-1:1)],VPP{v,1},'EdgeColor','none');alpha(0.5);
  332. patch([xaxis,xaxis(end:-1:1)],[PupSEMVPShuff{v,1},PdownSEMVPShuff{v,1}(end:-1:1)],'k','EdgeColor','none');alpha(0.5);
  333. xlabel('Seconds post reward delivery');
  334. title('VP decoding accuracy');
  335. axis([xaxis(1) xaxis(end) 0.4 1]);
  336. legend('10 units','25 units','50 units','100 units','150 units','Shuffled','location','northwest')
  337. end
  338. %stats!
  339. %ANOVA for effects of number of neurons and region on time of peak accuracy
  340. %time
  341. PeakTimes=[];
  342. for i=1:length(NumNeurons)
  343. if length(PoolDec{e,1}.True{i,1})>1 && length(PoolDec{e,2}.True{i,1})>1
  344. PeakTimes(1:repetitions,i)=PeakBinsNAc{i,1}(:,2);
  345. PeakTimes(1+repetitions:repetitions+repetitions,i)=PeakBinsVP{i,1}(:,2);
  346. end
  347. end
  348. [~,R_2R.PoolDecTimeStats{e,1},R_2R.PoolDecTimeStats{e,2}]=anova2(PeakTimes,repetitions,'off'); %columns is ensemble size, rows is region
  349. %ANOVA for effect of ensemble size, region, and shuff vs true on accuracy
  350. %of peak bin
  351. size=[];
  352. for i=1:length(NumNeurons)
  353. if length(PoolDec{e,1}.True{i,1})>1 && length(PoolDec{e,2}.True{i,1})>1
  354. size=cat(1,size,i*ones(repetitions,1));
  355. end
  356. end
  357. %with shuff vs true
  358. % size2=cat(1,size,size,size,size);
  359. % shuffd=cat(1,ones(length(A),1),zeros(length(B),1),ones(length(C),1),zeros(length(D),1));
  360. % region=cat(1,ones(length(A)+length(B),1),zeros(length(C)+length(D),1));
  361. % [~,R_2R.AccAnova,~]=anovan(cat(1,A,B,C,D),{shuffd,region,size2},'varnames',{'real vs shuffled','region','ens size'},'display','off','model','full');
  362. %without shuff vs true
  363. size3=cat(1,size,size);
  364. region3=cat(1,ones(length(A),1),zeros(length(C),1));
  365. [~,R_2R.PoolDecAccStats{e,1},R_2R.PoolDecAccStats{e,2}]=anovan(cat(1,A,C),{region3,size3},'varnames',{'region','ens size'},'display','off','model','full');
  366. end %selections
  367. save('R_2R.mat','R_2R');