-
Notifications
You must be signed in to change notification settings - Fork 3
/
spm_plot_ci.m
269 lines (216 loc) · 8.36 KB
/
spm_plot_ci.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
function spm_plot_ci(E,C,x,j,s)
% Plot mean and conditional confidence intervals
% FORMAT spm_plot_ci(E,C,x,j,s)
% E - expectation (structure or array)
% C - variance or covariance (structure or array)
% x - domain
% j - rows of E to plot
% s - string to specify plot type:e.g. '--r' or 'exp', 'log' etc
%
% If E is a row vector with two elements, confidence regions will be
% plotted; otherwise, bar charts with confidence intervals are provided.
%__________________________________________________________________________
% Copyright (C) 2008-2021 Wellcome Trust Centre for Neuroimaging
% Karl Friston
% $Id: spm_plot_ci.m 8073 2021-03-02 19:55:30Z guillaume $
# SPDX-License-Identifier: GPL-2.0
% get axis
%--------------------------------------------------------------------------
ax = gca;
col = get(ax,'ColorOrder');
coli = get(ax,'ColorOrderIndex');
coli = (mod(coli-1, length(col)) + 1); %wrap around to starting color if we walk off the end
disp(int2str(coli));
coll = col(coli,:);
colf = erf(coll + 1);
% confidence region (CR) plotting
%--------------------------------------------------------------------------
if size(E,1) == 1 && size(E,2) == 2
E = E';
CR = true;
else
CR = false;
end
% unpack expectations into a matrix
%--------------------------------------------------------------------------
if isstruct(E), E = spm_vec(E); end
if iscell(E), E = spm_vec(E); end
if ~exist('x','var'), x = 1:size(E,2); end
if ~exist('j','var'), j = 1:size(E,1); end
if ~exist('s','var'), s = ''; end
if isempty(x), x = 1:size(E,2); end
if isempty(j), j = 1:size(E,1); end
% order and length of sequence
%--------------------------------------------------------------------------
O = E;
E = E(j,:);
[n,N] = size(E);
% unpack conditional covariances
%--------------------------------------------------------------------------
ci = spm_invNcdf(1 - 0.05); % confidence interval
gr = 0.9; % grey level
if iscell(C)
% try cell array of covariances (from spm_DEM amd spm_LAP)
%----------------------------------------------------------------------
try
for i = 1:N
c(:,i) = ci*sqrt(diag(C{i}(j,j)));
end
catch
% try cell array of variances
%------------------------------------------------------------------
c = ci*sqrt(spm_unvec(spm_vec(C),O));
c = c(j,:);
end
elseif isstruct(C)
% try structure of variances
%----------------------------------------------------------------------
c = ci*sqrt(spm_unvec(spm_vec(C),O));
c = c(j,:);
elseif isnumeric(C)
% try matrix of variances
%----------------------------------------------------------------------
if all(size(C) == size(O))
c = ci*sqrt(C(j,:));
elseif all(size(C') == size(O))
c = ci*sqrt(C(:,j));
c = c(:)';
else
% try covariance matrix
%------------------------------------------------------------------
C = diag(C);
c = ci*sqrt(C);
c = c(:)';
end
end
% set plot parameters
%--------------------------------------------------------------------------
switch lower(get(ax,'NextPlot'))
case 'add'
col = [1 1/4 1/4];
width = .9;
otherwise
col = [1 3/4 3/4];
width = .8;
end
% plot elliptical confidence region
%--------------------------------------------------------------------------
if CR
[x,y] = ellipsoid(E(1),E(2),1,c(1),c(2),0,32);
fill(x(16,:)',y(16,:)',[1 1 1]*gr,'EdgeColor',[1 1 1]*.5,'Parent',ax);
hold(ax,'on');
plot(ax,E(1),E(2),'.','MarkerSize',16);
hold(ax,'off'); drawnow
return
end
% plot bar chart
%--------------------------------------------------------------------------
if N >= 8
% time-series plot
%======================================================================
if strcmpi(s,'exp')
fill([x fliplr(x)],exp([full(E + c) fliplr(full(E - c))]),...
colf,'EdgeColor','none','Parent',ax,'FaceAlpha', 0.4);
hold(ax,'on');
plot(x,exp(E),'Color',coll);
set(ax,'ColorOrderIndex',coli + 1);
elseif strcmpi(s,'log')
fill([x fliplr(x)],log(abs([full(E + c) fliplr(full(E - c))])),...
colf,'EdgeColor','none','Parent',ax,'FaceAlpha', 0.4);
hold(ax,'on');
plot(x,log(abs(E)),'Color',coll);
set(ax,'ColorOrderIndex',coli + 1);
else
fill([x fliplr(x)],[full(E + c) fliplr(full(E - c))],...
colf,'EdgeColor','none','Parent',ax,'FaceAlpha', 0.4);
hold(ax,'on');
plot(ax,x,E,s,'Color',coll);
set(ax,'ColorOrderIndex',coli + 1);
end
else
% bar
%======================================================================
if N == 1
if strcmpi(s,'exp')
% conditional means
%--------------------------------------------------------------
bar(ax,exp(E),width,'Edgecolor',colf,'FaceColor',colf);
hold(ax,'on');
% conditional variances
%--------------------------------------------------------------
for k = 1:n
line([k k],exp([-1 1]*c(k) + E(k)),...
'LineWidth',4,'Color',col,'Parent',ax);
end
elseif strcmpi(s,'log')
% conditional means
%--------------------------------------------------------------
bar(ax,log(abs(E)),width,'EdgeColor',colf,'FaceColor',colf);
hold(ax,'on');
% conditional variances
%--------------------------------------------------------------
for k = 1:n
line([k k],log(abs([-1 1]*c(k) + E(k))),...
'LineWidth',4,'Color',col,'Parent',ax);
end
else
if n > 1
% conditional means
%----------------------------------------------------------
bar(ax,E,width,'EdgeColor',colf,'FaceColor',colf);
hold(ax,'on');
else
% conditional means
%----------------------------------------------------------
bar(ax,E,'EdgeColor',colf,'FaceColor',colf);
hold(ax,'on');
end
% conditional variances
%--------------------------------------------------------------
for k = 1:n
line([k k],[-1 1]*c(k) + E(k),...
'LineWidth',4,'Color',col,'Parent',ax);
end
end
box(ax,'off');
set(ax,'XLim',[0 n + 1]);
else
if strcmpi(s,'exp')
% conditional means (exponential)
%--------------------------------------------------------------
h = bar(ax,exp(E)'); hold(ax,'on');
% conditional variances
%--------------------------------------------------------------
for m = 1:n
if ~isempty(get(h(m),'Children'))
x = mean(get(get(h(m),'Children'),'XData'));
else
x = get(h(m),'XEndPoints');
end
for k = 1:N
line([x(k) x(k)],exp([-1 1]*c(m,k) + E(m,k)),...
'LineWidth',1,'Color',col,'Parent',ax);
end
end
else
% conditional means
%--------------------------------------------------------------
h = bar(ax,E); hold(ax,'on');
% conditional variances
%--------------------------------------------------------------
for m = 1:N
if ~isempty(get(h(m),'Children'))
x = mean(get(get(h(m),'Children'),'Xdata'));
else
x = get(h(m),'XEndPoints');
end
for k = 1:n
line([x(k) x(k)],[-1 1]*c(k,m) + E(k,m),...
'LineWidth',4,'Color',col,'Parent',ax);
end
end
end
end
end
hold(ax,'off');
drawnow