Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

i_FigS7CuePERewHist.m 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. %look at impact of previous trials on Cue and PE
  2. %Looking at the average firing rate for a given window in each of 4
  3. %current/previous reward conditions
  4. clear all;
  5. load ('R_2R.mat');
  6. load ('RAW.mat');
  7. %run linear model and stats? 1 is yes, 0 is no
  8. runanalysis=1;
  9. %divide neurons up by region
  10. NAneurons=strcmp(R_2R.Ninfo(:,3),'NA');
  11. VPneurons=strcmp(R_2R.Ninfo(:,3),'VP');
  12. %get parameters
  13. trialsback=6; %how many trials back to look
  14. PEBaseline=R_2R.Param.BinBase+0.5; %For normalizing activity
  15. CueBaseline=[-11 -1];
  16. CueWindow=[0 0.5]; %what window of activity is analyzed
  17. PEWindow=[-0.6 0.7];
  18. BinDura=R_2R.Param.BinDura;
  19. bins=R_2R.Param.bins;
  20. binint=R_2R.Param.binint;
  21. binstart=R_2R.Param.binstart;
  22. %sorting bin -- which bin the neurons' activity is sorted on for heatmap(in seconds)
  23. SortBinTime=1; %seconds
  24. SortBin=(((SortBinTime-BinDura(2)/2)-binstart)/binint); %convert to bin name
  25. %reset
  26. NN=0;
  27. PEEvMeanz=0;
  28. if runanalysis==1
  29. for i=1:length(RAW) %loops through sessions
  30. if strcmp('NA',RAW(i).Type(1:2)) | strcmp('VP',RAW(i).Type(1:2)) %only look at suc v mal sessions
  31. %events
  32. EV3=strmatch('CueP1',RAW(i).Einfo(:,2),'exact');
  33. EV4=strmatch('CueP2',RAW(i).Einfo(:,2),'exact');
  34. EV1=strmatch('PEP1',RAW(i).Einfo(:,2),'exact');
  35. EV2=strmatch('PEP2',RAW(i).Einfo(:,2),'exact');
  36. Cue=strmatch('Cue',RAW(i).Einfo(:,2),'exact');
  37. PE=strmatch('PECue',RAW(i).Einfo(:,2),'exact');
  38. R1=strmatch('Reward1Deliv',RAW(i).Einfo(:,2),'exact');
  39. R2=strmatch('Reward2Deliv',RAW(i).Einfo(:,2),'exact');
  40. %% linear model for impact of previous rewards
  41. %reset
  42. Xcue=[];
  43. Xpe=[];
  44. Y=[];
  45. %set up the matrix with outcome identity for each session
  46. rewards1=cat(2,RAW(i).Erast{R1,1}(:,1),ones(length(RAW(i).Erast{R1,1}(:,1)),1));
  47. rewards2=cat(2,RAW(i).Erast{R2,1}(:,1),zeros(length(RAW(i).Erast{R2,1}(:,1)),1));
  48. rewards=cat(1,rewards1,rewards2);
  49. [rewards(:,1),ind]=sort(rewards(:,1));
  50. rewards(:,2)=rewards(ind,2);
  51. %cue
  52. firstcue=find(RAW(i).Erast{Cue,1}==min(RAW(i).Erast{Cue,1}(RAW(i).Erast{Cue,1}(:,1)>rewards(trialsback),1))); %find first cue with at least 5 rewards prior
  53. for k=firstcue+1:length(RAW(i).Erast{Cue,1}(:,1))
  54. time=RAW(i).Erast{Cue,1}(k,1);
  55. entry=find(rewards==max(rewards(rewards(:,1)<time)));
  56. for m=1:trialsback
  57. Xcue(k-trialsback,m)=rewards(entry+1-m,2);
  58. end
  59. end
  60. %PE
  61. for k=trialsback+1:length(RAW(i).Erast{PE,1}(:,1))
  62. time=RAW(i).Erast{PE,1}(k,1);
  63. entry=find(rewards==max(rewards(rewards(:,1)<time)));
  64. for m=1:trialsback
  65. Xpe(k-trialsback,m)=rewards(entry+1-m,2);
  66. end
  67. end
  68. for j= 1:length(RAW(i).Nrast) %Number of neurons within sessions
  69. NN=NN+1;
  70. %cue
  71. rewspk=0;
  72. basespk=0;
  73. %get mean baseline firing for all cues
  74. [Bcell1,B1n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{Cue},CueBaseline,{2});% makes trial by trial rasters for baseline
  75. for y= 1:B1n
  76. basespk(1,y)=sum(Bcell1{1,y}>CueBaseline(1));
  77. end
  78. Bhz=basespk/(CueBaseline(1,2)-CueBaseline(1,1));
  79. Bmean=nanmean(Bhz);
  80. Bstd=nanstd(Bhz);
  81. %get trial by trial firing rate for all cue trials
  82. [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{Cue},CueWindow,{2});% makes trial by trial rasters for event
  83. for y= 1:EVn
  84. rewspk(y,1)=sum(EVcell{1,y}>CueWindow(1));
  85. end
  86. Y=((rewspk(trialsback+1:end,1)/(CueWindow(1,2)-CueWindow(1,1)))-Bmean)/Bstd; %normalize the activity to baseline
  87. %true data
  88. linmdl{NN,1}=fitlm(Xcue,Y);
  89. R_2R.RewHist.LinMdlCueWeights(NN,1:trialsback)=linmdl{NN,1}.Coefficients.Estimate(2:trialsback+1)';
  90. R_2R.RewHist.LinMdlCuePVal(NN,1:trialsback)=linmdl{NN,1}.Coefficients.pValue(2:trialsback+1)';
  91. %shuffled
  92. YSh=Y(randperm(length(Y)));
  93. linmdlSh{NN,1}=fitlm(Xcue,YSh);
  94. R_2R.RewHist.LinMdlCueWeightsSh(NN,1:trialsback)=linmdlSh{NN,1}.Coefficients.Estimate(2:trialsback+1)';
  95. R_2R.RewHist.LinMdlCuePValSh(NN,1:trialsback)=linmdlSh{NN,1}.Coefficients.pValue(2:trialsback+1)';
  96. %PE
  97. rewspk=0;
  98. basespk=0;
  99. %get mean baseline firing for all PEs
  100. [Bcell1,B1n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{PE},PEBaseline,{2});% makes trial by trial rasters for baseline
  101. for y= 1:B1n
  102. basespk(1,y)=sum(Bcell1{1,y}>PEBaseline(1));
  103. end
  104. Bhz=basespk/(PEBaseline(1,2)-PEBaseline(1,1));
  105. Bmean=nanmean(Bhz);
  106. Bstd=nanstd(Bhz);
  107. %get trial by trial firing rate for all PE trials
  108. [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{PE},PEWindow,{2});% makes trial by trial rasters for event
  109. for y= 1:EVn
  110. rewspk(y,1)=sum(EVcell{1,y}>PEWindow(1));
  111. end
  112. Y=((rewspk(trialsback+1:end,1)/(PEWindow(1,2)-PEWindow(1,1)))-Bmean)/Bstd; %normalize the activity to baseline
  113. %true data
  114. linmdl{NN,1}=fitlm(Xpe,Y);
  115. R_2R.RewHist.LinMdlPEWeights(NN,1:trialsback)=linmdl{NN,1}.Coefficients.Estimate(2:trialsback+1)';
  116. R_2R.RewHist.LinMdlPEPVal(NN,1:trialsback)=linmdl{NN,1}.Coefficients.pValue(2:trialsback+1)';
  117. %shuffled
  118. YSh=Y(randperm(length(Y)));
  119. linmdlSh{NN,1}=fitlm(Xpe,YSh);
  120. R_2R.RewHist.LinMdlPEWeightsSh(NN,1:trialsback)=linmdlSh{NN,1}.Coefficients.Estimate(2:trialsback+1)';
  121. R_2R.RewHist.LinMdlPEPValSh(NN,1:trialsback)=linmdlSh{NN,1}.Coefficients.pValue(2:trialsback+1)';
  122. %% stats comparing effect of current and previous reward on PE
  123. %resetting
  124. Bcell=0;
  125. EV1spk=0;
  126. EV2spk=0;
  127. EV1norm=0;
  128. EV2norm=0;
  129. %get mean baseline firing for all PE trials
  130. [Bcell1,B1n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EV1},PEBaseline,{2});% makes trial by trial rasters for baseline
  131. for y= 1:B1n
  132. Bcell(1,y)=sum(Bcell1{1,y}>PEBaseline(1));
  133. end
  134. [Bcell2,B2n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EV2},PEBaseline,{2});% makes trial by trial rasters for baseline
  135. for z= 1:B2n
  136. Bcell(1,z+B1n)=sum(Bcell2{1,z}>PEBaseline(1));
  137. end
  138. Bhz=Bcell/(PEBaseline(1,2)-PEBaseline(1,1));
  139. Bmean=nanmean(Bhz);
  140. Bstd=nanstd(Bhz);
  141. %get trial by trial firing rate for post suc trials
  142. [EV1cell,EV1n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EV1},CueWindow,{2});% makes trial by trial rasters for event
  143. for y= 1:EV1n
  144. EV1spk(1,y)=sum(EV1cell{1,y}>CueWindow(1));
  145. end
  146. EV1hz=EV1spk/(CueWindow(1,2)-CueWindow(1,1));
  147. for x= 1:EV1n
  148. EV1norm(1,x)=((EV1hz(1,x)-Bmean)/Bstd);
  149. end
  150. %get trial by trial firing rate for post mal trials
  151. [EV2cell,EV2n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EV2},CueWindow,{2});% makes trial by trial rasters for event
  152. for y= 1:EV2n
  153. EV2spk(1,y)=sum(EV2cell{1,y}>CueWindow(1));
  154. end
  155. EV2hz=EV2spk/(CueWindow(1,2)-CueWindow(1,1));
  156. for x= 1:EV2n
  157. EV2norm(1,x)=((EV2hz(1,x)-Bmean)/Bstd);
  158. end
  159. PEEvMeanz(NN,1)=nanmean(EV1norm);
  160. PEEvMeanz(NN,2)=nanmean(EV2norm);
  161. %% stats comparing effect of current and previous reward on cue
  162. %resetting
  163. Bcell=0;
  164. EV1spk=0;
  165. EV2spk=0;
  166. EV1norm=0;
  167. EV2norm=0;
  168. %get mean baseline firing for all cue trials
  169. [Bcell1,B1n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EV3},CueBaseline,{2});% makes trial by trial rasters for baseline
  170. for y= 1:B1n
  171. Bcell(1,y)=sum(Bcell1{1,y}>CueBaseline(1));
  172. end
  173. [Bcell2,B2n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EV4},CueBaseline,{2});% makes trial by trial rasters for baseline
  174. for z= 1:B2n
  175. Bcell(1,z+B1n)=sum(Bcell2{1,z}>CueBaseline(1));
  176. end
  177. Bhz=Bcell/(CueBaseline(1,2)-CueBaseline(1,1));
  178. Bmean=nanmean(Bhz);
  179. Bstd=nanstd(Bhz);
  180. %get trial by trial firing rate for post suc trials
  181. [EV1cell,EV1n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EV3},CueWindow,{2});% makes trial by trial rasters for event
  182. for y= 1:EV1n
  183. EV1spk(1,y)=sum(EV1cell{1,y}>CueWindow(1));
  184. end
  185. EV1hz=EV1spk/(CueWindow(1,2)-CueWindow(1,1));
  186. for x= 1:EV1n
  187. EV1norm(1,x)=((EV1hz(1,x)-Bmean)/Bstd);
  188. end
  189. %get trial by trial firing rate for post mal trials
  190. [EV2cell,EV2n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EV4},CueWindow,{2});% makes trial by trial rasters for event
  191. for y= 1:EV2n
  192. EV2spk(1,y)=sum(EV2cell{1,y}>CueWindow(1));
  193. end
  194. EV2hz=EV2spk/(CueWindow(1,2)-CueWindow(1,1));
  195. for x= 1:EV2n
  196. EV2norm(1,x)=((EV2hz(1,x)-Bmean)/Bstd);
  197. end
  198. CueEvMeanz(NN,1)=nanmean(EV1norm);
  199. CueEvMeanz(NN,2)=nanmean(EV2norm);
  200. fprintf('Neuron # %d\n',NN);
  201. end
  202. end
  203. R_2R.RewHist.PrevRewPEMeanz=PEEvMeanz;
  204. R_2R.RewHist.PrevRewCueMeanz=CueEvMeanz;
  205. end
  206. end
  207. %% which neurons to look at for stats and plotting?
  208. % Sel=R_2R.SucN | R_2R.MalN; %only look at reward-selective neurons
  209. Sel=NAneurons | VPneurons; %look at all neurons
  210. %Sel=R_2R.RewHist.LinMdlPVal(:,2)<0.1; %only neurons with significant impact of previous trial
  211. %Sel=R_2R.RewHist.LinMdlPEWeights(:,2)<-1; %only neurons with strong negative impact of previous trial
  212. %% ANOVAs
  213. %setup and run ANOVA for effects of current reward, previous reward, and region on reward firing
  214. PrevRew=cat(2,zeros(length(NAneurons),1),ones(length(NAneurons),1));
  215. Region=cat(2,NAneurons,NAneurons);
  216. %to look at a specific selection of cells
  217. PEEvMeanz=R_2R.RewHist.PrevRewPEMeanz(Sel,:);
  218. CueEvMeanz=R_2R.RewHist.PrevRewCueMeanz(Sel,:);
  219. PrevRew=PrevRew(Sel,:);
  220. Region=Region(Sel,:);
  221. [~,R_2R.RewHist.PrevRewPEStats{1,1},R_2R.RewHist.PrevRewPEStats{1,2}]=anovan(PEEvMeanz(:),{PrevRew(:),Region(:)},'varnames',{'Previous Reward','Region'},'display','off','model','full');
  222. [~,R_2R.RewHist.PrevRewCueStats{1,1},R_2R.RewHist.PrevRewCueStats{1,2}]=anovan(CueEvMeanz(:),{PrevRew(:),Region(:)},'varnames',{'Previous Reward','Region'},'display','off','model','full');
  223. %setup and run ANOVA for effects of shuffle, trial, and region on coefficient
  224. Trial=[];
  225. Region=[];
  226. for i=1:trialsback
  227. Trial=cat(2,Trial,i*ones(length(NAneurons),1));
  228. Region=cat(2,Region,NAneurons);
  229. end
  230. Trial=cat(2,Trial,Trial);
  231. Region=cat(2,Region,Region);
  232. Shuffd=cat(2,zeros(length(NAneurons),trialsback),ones(length(NAneurons),trialsback));
  233. %Cue
  234. Coeffs=cat(2,R_2R.RewHist.LinMdlCueWeights(:,1:trialsback),R_2R.RewHist.LinMdlCueWeightsSh(:,1:trialsback));
  235. [~,R_2R.RewHist.LinCoeffCueStats{1,1},R_2R.RewHist.LinCoeffCueStats{1,2}]=anovan(Coeffs(:),{Shuffd(:),Region(:),Trial(:)},'varnames',{'Shuffd','Region','Trial'},'display','off','model','full');
  236. %PE
  237. Coeffs=cat(2,R_2R.RewHist.LinMdlPEWeights(:,1:trialsback),R_2R.RewHist.LinMdlPEWeightsSh(:,1:trialsback));
  238. [~,R_2R.RewHist.LinCoeffPEStats{1,1},R_2R.RewHist.LinCoeffPEStats{1,2}]=anovan(Coeffs(:),{Shuffd(:),Region(:),Trial(:)},'varnames',{'Shuffd','Region','Trial'},'display','off','model','full');
  239. %% plotting
  240. XaxisCue=[-0.5 1];
  241. XaxisPE=[-1 2];
  242. Ishow=find(R_2R.Param.Tm>=XaxisCue(1) & R_2R.Param.Tm<=XaxisCue(2));
  243. Ushow=find(R_2R.Param.Tm>=XaxisPE(1) & R_2R.Param.Tm<=XaxisPE(2));
  244. time1=R_2R.Param.Tm(Ishow);
  245. time2=R_2R.Param.Tm(Ushow);
  246. %color map
  247. [magma,inferno,plasma,viridis]=colormaps;
  248. colormap(plasma);
  249. c=[-100 2000];ClimE=sign(c).*abs(c).^(1/4);%colormap
  250. %colors
  251. sucrose=[.95 0.55 0.15];
  252. maltodextrin=[.9 0.3 .9];
  253. water=[0.00 0.75 0.75];
  254. total=[0.3 0.1 0.8];
  255. inh=[0.1 0.021154 0.6];
  256. exc=[0.9 0.75 0.205816];
  257. NAc=[0.5 0.1 0.8];
  258. VP=[0.3 0.7 0.1];
  259. %extra colors to make a gradient
  260. sucrosem=[.98 0.8 0.35];
  261. sucrosel=[1 1 0.4];
  262. maltodextrinm=[1 0.75 1];
  263. maltodextrinl=[1 0.8 1];
  264. RD1P1=strcmp('CueP1', R_2R.Erefnames);
  265. RD1P2=strcmp('CueP2', R_2R.Erefnames);
  266. RD2P1=strcmp('PEP1', R_2R.Erefnames);
  267. RD2P2=strcmp('PEP2', R_2R.Erefnames);
  268. %% Get mean firing according to previous trial and then plot
  269. %NAc
  270. %plot suc after suc
  271. psth1=nanmean(R_2R.Ev(RD1P1).PSTHz(Sel&NAneurons,Ishow),1);
  272. sem1=nanste(R_2R.Ev(RD1P1).PSTHz(Sel&NAneurons,Ishow),1); %calculate standard error of the mean
  273. up1=psth1+sem1;
  274. down1=psth1-sem1;
  275. %plot suc after malt
  276. psth2=nanmean(R_2R.Ev(RD1P2).PSTHz(Sel&NAneurons,Ishow),1);
  277. sem2=nanste(R_2R.Ev(RD1P2).PSTHz(Sel&NAneurons,Ishow),1); %calculate standard error of the mean
  278. up2=psth2+sem2;
  279. down2=psth2-sem2;
  280. %make the plot
  281. subplot(2,4,1);
  282. hold on;
  283. title(['Cue response, NAc'])
  284. plot(time1,psth1,'Color',sucrose,'linewidth',1);
  285. plot(time1,psth2,'Color',maltodextrin,'linewidth',1);
  286. patch([time1,time1(end:-1:1)],[up2,down2(end:-1:1)],maltodextrin,'EdgeColor','none');alpha(0.5);
  287. patch([time1,time1(end:-1:1)],[up1,down1(end:-1:1)],sucrose,'EdgeColor','none');alpha(0.5);
  288. plot([-2 5],[0 0],':','color','k','linewidth',0.75);
  289. plot([0 0],[-2 8],':','color','k','linewidth',0.75);
  290. plot([CueWindow(1) CueWindow(1)],[-2 8],'color','b','linewidth',0.85);
  291. plot([CueWindow(2) CueWindow(2)],[-2 8],'color','b','linewidth',0.85);
  292. axis([XaxisCue(1) XaxisCue(2) min(down2)-0.15 max(up1)+0.2]);
  293. ylabel('Mean firing (z-score)');
  294. xlabel('Seconds from cue');
  295. legend('Post-suc','Post-mal','location','northeast');
  296. subplot(2,4,5);
  297. hold on;
  298. title('PE response, NAc');
  299. %plot malt after suc
  300. psth3=nanmean(R_2R.Ev(RD2P1).PSTHz(Sel&NAneurons,Ushow),1);
  301. sem3=nanste(R_2R.Ev(RD2P1).PSTHz(Sel&NAneurons,Ushow),1); %calculate standard error of the mean
  302. up3=psth3+sem3;
  303. down3=psth3-sem3;
  304. %plot malt after malt
  305. psth4=nanmean(R_2R.Ev(RD2P2).PSTHz(Sel&NAneurons,Ushow),1);
  306. sem4=nanste(R_2R.Ev(RD2P2).PSTHz(Sel&NAneurons,Ushow),1); %calculate standard error of the mean
  307. up4=psth4+sem4;
  308. down4=psth4-sem4;
  309. plot(time2,psth3,'Color',sucrose,'linewidth',1);
  310. plot(time2,psth4,'Color',maltodextrin,'linewidth',1);
  311. patch([time2,time2(end:-1:1)],[up3,down3(end:-1:1)],sucrose,'EdgeColor','none');alpha(0.5);
  312. patch([time2,time2(end:-1:1)],[up4,down4(end:-1:1)],maltodextrin,'EdgeColor','none');alpha(0.5);
  313. plot([-2 5],[0 0],':','color','k','linewidth',0.75);
  314. plot([0 0],[-2 8],':','color','k','linewidth',0.75);
  315. plot([PEWindow(1) PEWindow(1)],[-2 8],'color','b','linewidth',0.85);
  316. plot([PEWindow(2) PEWindow(2)],[-2 8],'color','b','linewidth',0.85);
  317. axis([XaxisPE(1) XaxisPE(2) min(down3)-0.2 max(up4)+0.2]);
  318. ylabel('Mean firing (z-score)');
  319. xlabel('Seconds from cue');
  320. legend('Post-suc','Post-mal','location','northeast');
  321. %VP
  322. %plot suc after suc
  323. psth1=nanmean(R_2R.Ev(RD1P1).PSTHz(Sel&VPneurons,Ishow),1);
  324. sem1=nanste(R_2R.Ev(RD1P1).PSTHz(Sel&VPneurons,Ishow),1); %calculate standard error of the mean
  325. up1=psth1+sem1;
  326. down1=psth1-sem1;
  327. %plot suc after malt
  328. psth2=nanmean(R_2R.Ev(RD1P2).PSTHz(Sel&VPneurons,Ishow),1);
  329. sem2=nanste(R_2R.Ev(RD1P2).PSTHz(Sel&VPneurons,Ishow),1); %calculate standard error of the mean
  330. up2=psth2+sem2;
  331. down2=psth2-sem2;
  332. %make the plot
  333. subplot(2,4,2);
  334. title(['Cue response, VP'])
  335. hold on;
  336. plot(time1,psth1,'Color',sucrose,'linewidth',1);
  337. plot(time1,psth2,'Color',maltodextrin,'linewidth',1);
  338. patch([time1,time1(end:-1:1)],[up2,down2(end:-1:1)],maltodextrin,'EdgeColor','none');alpha(0.5);
  339. patch([time1,time1(end:-1:1)],[up1,down1(end:-1:1)],sucrose,'EdgeColor','none');alpha(0.5);
  340. plot([-2 5],[0 0],':','color','k','linewidth',0.75);
  341. plot([0 0],[-2 8],':','color','k','linewidth',0.75);
  342. plot([CueWindow(1) CueWindow(1)],[-2 8],'color','b','linewidth',0.85);
  343. plot([CueWindow(2) CueWindow(2)],[-2 8],'color','b','linewidth',0.85);
  344. axis([XaxisCue(1) XaxisCue(2) min(down2)-0.3 max(up1)+0.3]);
  345. ylabel('Mean firing (z-score)');
  346. xlabel('Seconds from PE');
  347. legend('Post-suc','Post-mal','location','northeast');
  348. subplot(2,4,6);
  349. title('PE response, VP');
  350. hold on;
  351. %plot malt after suc
  352. psth3=nanmean(R_2R.Ev(RD2P1).PSTHz(Sel&VPneurons,Ushow),1);
  353. sem3=nanste(R_2R.Ev(RD2P1).PSTHz(Sel&VPneurons,Ushow),1); %calculate standard error of the mean
  354. up3=psth3+sem3;
  355. down3=psth3-sem3;
  356. %plot malt after malt
  357. psth4=nanmean(R_2R.Ev(RD2P2).PSTHz(Sel&VPneurons,Ushow),1);
  358. sem4=nanste(R_2R.Ev(RD2P2).PSTHz(Sel&VPneurons,Ushow),1); %calculate standard error of the mean
  359. up4=psth4+sem4;
  360. down4=psth4-sem4;
  361. plot(time2,psth3,'Color',sucrose,'linewidth',1);
  362. plot(time2,psth4,'Color',maltodextrin,'linewidth',1);
  363. patch([time2,time2(end:-1:1)],[up3,down3(end:-1:1)],sucrose,'EdgeColor','none');alpha(0.5);
  364. patch([time2,time2(end:-1:1)],[up4,down4(end:-1:1)],maltodextrin,'EdgeColor','none');alpha(0.5);
  365. plot([-2 5],[0 0],':','color','k','linewidth',0.75);
  366. plot([0 0],[-2 8],':','color','k','linewidth',0.75);
  367. plot([PEWindow(1) PEWindow(1)],[-2 8],'color','b','linewidth',0.85);
  368. plot([PEWindow(2) PEWindow(2)],[-2 8],'color','b','linewidth',0.85);
  369. axis([XaxisPE(1) XaxisPE(2) min(down4)-0.3 max(up3)+0.3]);
  370. ylabel('Mean firing (z-score)');
  371. xlabel('Seconds from PE');
  372. legend('Post-suc','Post-mal','location','northeast');
  373. %% plot linear model coefficients
  374. %Plot all neurons
  375. Sel=NAneurons<2;
  376. %coefficients for each trial
  377. subplot(2,4,3);
  378. hold on;
  379. errorbar(1:trialsback,nanmean(R_2R.RewHist.LinMdlCueWeights(Sel&NAneurons,1:trialsback),1),nanste(R_2R.RewHist.LinMdlCueWeights(Sel&NAneurons,1:trialsback),1),'color',NAc);
  380. errorbar(1:trialsback,nanmean(R_2R.RewHist.LinMdlCueWeights(Sel&VPneurons,1:trialsback),1),nanste(R_2R.RewHist.LinMdlCueWeights(Sel&VPneurons,1:trialsback),1),'color',VP);
  381. errorbar(1:trialsback,nanmean(R_2R.RewHist.LinMdlCueWeightsSh(Sel&NAneurons,1:trialsback),1),nanste(R_2R.RewHist.LinMdlCueWeights(Sel&NAneurons,1:trialsback),1),'color','k');
  382. errorbar(1:trialsback,nanmean(R_2R.RewHist.LinMdlCueWeightsSh(Sel&VPneurons,1:trialsback),1),nanste(R_2R.RewHist.LinMdlCueWeights(Sel&VPneurons,1:trialsback),1),'color','k');
  383. xlabel('Trials back');
  384. ylabel('Mean coefficient weight');
  385. title('Linear model coefficients');
  386. axis([0 trialsback+1 -0.5 1]);
  387. plot([-1 trialsback+1],[0 0],':','color','k','linewidth',0.75);
  388. legend('NAc','VP','Shuff');
  389. %stats to check if VP is greater than NAc
  390. [c,~,~,~]=multcompare(R_2R.RewHist.LinCoeffStats{1,2},'dimension',[1,2,3],'display','off');
  391. for i=1:trialsback
  392. %NAc vs VP
  393. Cel=c(:,1)==4*(i-1)+1 & c(:,2)==4*(i-1)+3;
  394. if c(Cel,6)<0.05 R_2R.RewHist.LinCoeffCueMultComp(i,1)=1; else R_2R.RewHist.LinCoeffCueMultComp(i,1)=0; end
  395. R_2R.RewHist.LinCoeffCueMultComp(i,2)=c(Cel,6);
  396. %VP vs shuff
  397. Cel=c(:,1)==4*(i-1)+1 & c(:,2)==4*(i-1)+2;
  398. if c(Cel,6)<0.05 R_2R.RewHist.LinCoeffCueMultComp(i,3)=1; else R_2R.RewHist.LinCoeffCueMultComp(i,3)=0; end
  399. R_2R.RewHist.LinCoeffCueMultComp(i,4)=c(Cel,6);
  400. %NAc vs shuff
  401. Cel=c(:,1)==4*(i-1)+3 & c(:,2)==4*(i-1)+4;
  402. if c(Cel,6)<0.05 R_2R.RewHist.LinCoeffCueMultComp(i,5)=1; else R_2R.RewHist.LinCoeffCueMultComp(i,5)=0; end
  403. R_2R.RewHist.LinCoeffCueMultComp(i,6)=c(Cel,6);
  404. end
  405. plot([1:trialsback]-0.15,(R_2R.RewHist.LinCoeffCueMultComp(:,3)-0.5)*4.85,'*','color',VP); %VP vs shuff
  406. plot([1:trialsback],(R_2R.RewHist.LinCoeffCueMultComp(:,5)-0.5)*4.85,'*','color',NAc); %NAc vs shuff
  407. plot([1:trialsback]+0.15,(R_2R.RewHist.LinCoeffCueMultComp(:,1)-0.5)*4.85,'*','color','k'); %VP vs NAc
  408. %number of neurons with significant weights
  409. subplot(2,4,4);
  410. hold on;
  411. plot(1:trialsback,sum(R_2R.RewHist.LinMdlCuePVal(Sel&NAneurons,1:trialsback)<0.05,1)/sum(Sel&NAneurons),'color',NAc);
  412. plot(1:trialsback,sum(R_2R.RewHist.LinMdlCuePVal(Sel&VPneurons,1:trialsback)<0.05,1)/sum(Sel&VPneurons),'color',VP);
  413. plot(1:trialsback,sum(R_2R.RewHist.LinMdlCuePValSh(Sel&NAneurons,1:trialsback)<0.05,1)/sum(Sel&NAneurons),'color',NAc/3);
  414. plot(1:trialsback,sum(R_2R.RewHist.LinMdlCuePValSh(Sel&VPneurons,1:trialsback)<0.05,1)/sum(Sel&VPneurons),'color',VP/3);
  415. axis([0 trialsback+1 0 0.5]);
  416. xlabel('Trials back');
  417. ylabel('Fraction of the population');
  418. title('Outcome-modulated cue resp');
  419. legend('NAc','VP','Shuff');
  420. %Chi squared stat for each trial
  421. for i=1:trialsback
  422. [~,R_2R.RewHist.LinMdlCue2all(i,1),R_2R.RewHist.LinMdlCue2all(i,2)]=crosstab(cat(1,R_2R.RewHist.LinMdlCuePVal(Sel,i)<0.05,R_2R.RewHist.LinMdlCuePValSh(Sel,i)<0.05),cat(1,VPneurons,VPneurons+2));
  423. [~,R_2R.RewHist.LinMdlCue2region(i,1),R_2R.RewHist.LinMdlCue2region(i,2)]=crosstab(R_2R.RewHist.LinMdlCuePVal(Sel,i)<0.05,VPneurons);
  424. end
  425. %plot([1:trialsback]-0.1,(R_2R.RewHist.LinMdlCue2all(:,2)<0.05)-0.52,'*','color',NAc);
  426. plot([1:trialsback],(R_2R.RewHist.LinMdlCue2region(:,2)<0.05&R_2R.RewHist.LinMdlCue2all(:,2)<0.05)-0.52,'*','color','k');
  427. %% PE
  428. %coefficients for each trial
  429. subplot(2,4,7);
  430. hold on;
  431. errorbar(1:trialsback,nanmean(R_2R.RewHist.LinMdlPEWeights(Sel&NAneurons,1:trialsback),1),nanste(R_2R.RewHist.LinMdlPEWeights(Sel&NAneurons,1:trialsback),1),'color',NAc);
  432. errorbar(1:trialsback,nanmean(R_2R.RewHist.LinMdlPEWeights(Sel&VPneurons,1:trialsback),1),nanste(R_2R.RewHist.LinMdlPEWeights(Sel&VPneurons,1:trialsback),1),'color',VP);
  433. errorbar(1:trialsback,nanmean(R_2R.RewHist.LinMdlPEWeightsSh(Sel&NAneurons,1:trialsback),1),nanste(R_2R.RewHist.LinMdlPEWeights(Sel&NAneurons,1:trialsback),1),'color','k');
  434. errorbar(1:trialsback,nanmean(R_2R.RewHist.LinMdlPEWeightsSh(Sel&VPneurons,1:trialsback),1),nanste(R_2R.RewHist.LinMdlPEWeights(Sel&VPneurons,1:trialsback),1),'color','k');
  435. xlabel('Trials back');
  436. ylabel('Mean coefficient weight');
  437. title('Linear model coefficients');
  438. axis([0 trialsback+1 -0.5 1]);
  439. plot([-1 trialsback+1],[0 0],':','color','k','linewidth',0.75);
  440. legend('NAc','VP','Shuff');
  441. %stats to check if VP is greater than NAc
  442. [c,~,~,~]=multcompare(R_2R.RewHist.LinCoeffPEStats{1,2},'dimension',[1,2,3],'display','off');
  443. for i=1:trialsback
  444. %NAc vs VP
  445. Cel=c(:,1)==4*(i-1)+1 & c(:,2)==4*(i-1)+3;
  446. if c(Cel,6)<0.05 R_2R.RewHist.LinCoeffPEMultComp(i,1)=1; else R_2R.RewHist.LinCoeffPEMultComp(i,1)=0; end
  447. R_2R.RewHist.LinCoeffPEMultComp(i,2)=c(Cel,6);
  448. %VP vs shuff
  449. Cel=c(:,1)==4*(i-1)+1 & c(:,2)==4*(i-1)+2;
  450. if c(Cel,6)<0.05 R_2R.RewHist.LinCoeffPEMultComp(i,3)=1; else R_2R.RewHist.LinCoeffPEMultComp(i,3)=0; end
  451. R_2R.RewHist.LinCoeffPEMultComp(i,4)=c(Cel,6);
  452. %NAc vs shuff
  453. Cel=c(:,1)==4*(i-1)+3 & c(:,2)==4*(i-1)+4;
  454. if c(Cel,6)<0.05 R_2R.RewHist.LinCoeffPEMultComp(i,5)=1; else R_2R.RewHist.LinCoeffPEMultComp(i,5)=0; end
  455. R_2R.RewHist.LinCoeffPEMultComp(i,6)=c(Cel,6);
  456. end
  457. plot([1:trialsback]-0.15,(R_2R.RewHist.LinCoeffPEMultComp(:,3)-0.5)*1.9,'*','color',VP); %VP vs shuff
  458. plot([1:trialsback],(R_2R.RewHist.LinCoeffPEMultComp(:,5)-0.5)*1.8,'*','color',NAc); %NAc vs shuff
  459. plot([1:trialsback]+0.15,(R_2R.RewHist.LinCoeffPEMultComp(:,1)-0.5)*1.9,'*','color','k'); %VP vs NAc
  460. %number of neurons with significant weights
  461. subplot(2,4,8);
  462. hold on;
  463. plot(1:trialsback,sum(R_2R.RewHist.LinMdlPEPVal(Sel&NAneurons,1:trialsback)<0.05,1)/sum(Sel&NAneurons),'color',NAc);
  464. plot(1:trialsback,sum(R_2R.RewHist.LinMdlPEPVal(Sel&VPneurons,1:trialsback)<0.05,1)/sum(Sel&VPneurons),'color',VP);
  465. plot(1:trialsback,sum(R_2R.RewHist.LinMdlPEPValSh(Sel&NAneurons,1:trialsback)<0.05,1)/sum(Sel&NAneurons),'color',NAc/3);
  466. plot(1:trialsback,sum(R_2R.RewHist.LinMdlPEPValSh(Sel&VPneurons,1:trialsback)<0.05,1)/sum(Sel&VPneurons),'color',VP/3);
  467. axis([0 trialsback+1 0 0.5]);
  468. xlabel('Trials back');
  469. ylabel('Fraction of the population');
  470. title('Outcome-modulated PE resp');
  471. legend('NAc','VP','Shuff');
  472. %Chi squared stat for each trial
  473. for i=1:trialsback
  474. [~,R_2R.RewHist.LinMdlPEX2all(i,1),R_2R.RewHist.LinMdlPEX2all(i,2)]=crosstab(cat(1,R_2R.RewHist.LinMdlPEPVal(Sel,i)<0.05,R_2R.RewHist.LinMdlPEPValSh(Sel,i)<0.05),cat(1,VPneurons,VPneurons+2));
  475. [~,R_2R.RewHist.LinMdlPEX2region(i,1),R_2R.RewHist.LinMdlPEX2region(i,2)]=crosstab(R_2R.RewHist.LinMdlPEPVal(Sel,i)<0.05,VPneurons);
  476. end
  477. %plot([0:trialsback]-0.1,(R_2R.RewHist.LinMdlPE2all(:,2)<0.05)-0.52,'*','color',NAc);
  478. plot([1:trialsback],(R_2R.RewHist.LinMdlPEX2region(:,2)<0.05&R_2R.RewHist.LinMdlPEX2all(:,2)<0.05)-0.52,'*','color','k');
  479. save('R_2R.mat','R_2R');