-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprt_plot_ROC.m
More file actions
147 lines (133 loc) · 4.88 KB
/
Copy pathprt_plot_ROC.m
File metadata and controls
147 lines (133 loc) · 4.88 KB
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
function prt_plot_ROC(PRT, model, fold, axes_handle)
% Function that plots the ROC plot that appears on prt_ui_results
%
% FORMAT prt_plot_ROC(PRT, model, fold, axes_handle)
% Inputs:
% PRT - data/design/model structure (it needs to contain
% at least one estimated model).
% model - the number of the model that will be ploted
% fold - the number of the fold
% axes_handle - (Optional) axes where the plot will be displayed
%
% Output:
% None
%__________________________________________________________________________
% Copyright (C) 2011 Machine Learning & Neuroimaging Laboratory
% Written by M. J. Rosa and J. Schrouff
% $Id: prt_plot_ROC.m 706 2013-06-07 14:33:34Z cphillip $
nfold = length(PRT.model(model).output.fold);
stats_tool = which('perfcurve');
tpr_up = [];
if fold == 1
fpr_mean = linspace(0,1,100);
tpr = cell(nfold,1);
fpr = cell(nfold,1);
tpr_mean = zeros(nfold,length(fpr_mean));
% Compute average ROC with std if possible
for f = 1:nfold
targets = PRT.model(model).output.fold(f).targets;
if isfield(PRT.model(model).output.fold(f),'func_val')
fVals = PRT.model(model).output.fold(f).func_val;
else
fVals = PRT.model(model).output.fold(f).predictions;
end
% [UPDATE v3.1] Fixed pre-existing bug: if/else was inverted.
% stats_tool = which('perfcurve') returns non-empty string if the
% Statistics and Machine Learning Toolbox is installed.
% perfcurve() should be called when stats_tool is non-empty (toolbox
% available), and prt_tpr_fpr() used as fallback when it is empty.
if stats_tool
[fpr{f},tpr{f}] = perfcurve(targets,fVals,1);
else
% Fallback: use PRoNTo's own TPR/FPR function when
% Statistics Toolbox is not installed
[tpr{f},fpr{f}] = prt_tpr_fpr(targets,fVals);
end
if all(isnan(tpr{f})) || all(isnan(fpr{f}))
tpr_mean(f,:) = NaN * ones(1,length(fpr_mean));
end
if which('interp1q')
tpr_mean(f,:) = (interp1q(fpr{f},tpr{f},fpr_mean'))';
else
legend_labs{f} = ['ROC fold ',num2str(f)];
end
end
if which('interp1q') % was able to compute interpolation for average
clear tpr fpr
tpr{1} = mean(tpr_mean,1)';
tpr_std = std(tpr_mean,[],1)';
fpr{1} = fpr_mean';
if tpr{1}(1)~= 0 % Add (0,0) point if needed
tpr{1} =[0;tpr{1}];
fpr{1} =[0;fpr{1}];
tpr_std = [0;tpr_std];
elseif tpr{1}(end) ~= 1 % Add (1,1) point if needed
tpr{1} =[tpr{1};1];
fpr{1} =[fpr{1};1];
tpr_std = [tpr_std;1];
end
if ~any(isnan(tpr{1}))
tpr_low = max(tpr{1}-tpr_std,0);
tpr_up = min(tpr{1}+tpr_std,1);
legend_labs = {'ROC curve','+/- 1*std'};
else
legend_labs = {'No ROC to display'};
end
end
else
% if folds wise
targets = PRT.model(model).output.fold(fold-1).targets;
if isfield(PRT.model(model).output.fold(fold-1),'func_val')
fVals = PRT.model(model).output.fold(fold-1).func_val;
else
fVals = PRT.model(model).output.fold(fold-1).predictions;
end
if stats_tool
[fpr{1},tpr{1}] = perfcurve(targets,fVals,1);
else
[tpr{1},fpr{1}] = prt_tpr_fpr(targets,fVals);
end
legend_labs = {'ROC curve'};
end
%If no axes_handle is given, create a new window
if ~exist('axes_handle', 'var')
figure;
axes_handle = axes;
else
set(axes_handle, 'XScale','linear');
end
% Prepare axis
rotate3d off
cla(axes_handle, 'reset');
% Plot curve
cc = cbrewer('qual','Set3',max(length(tpr),3));
cc = brighten(cc,-0.5);
hold on
% Plot ROC curves, one per fold if no stats toolbox
for i = 1:length(tpr)
if numel(tpr{i})<40 % display markers at each point
plot(axes_handle,fpr{i},tpr{i},'-s','Color',cc(i,:), ...
'LineWidth',2, 'MarkerEdgeColor',cc(i,:),...
'MarkerFaceColor',cc(i,:),...
'MarkerSize',4);
else % Do not display markers if a lot are present
plot(axes_handle,fpr{i},tpr{i},'-','Color',cc(i,:), ...
'LineWidth',2);
end
end
% Plot std of curve if across folds and not NaN
if ~isempty(tpr_up)
std_x = [fpr{1}; flipud(fpr{1})];
inBetween = [tpr_low;flipud(tpr_up)];
fill(axes_handle,std_x,inBetween,[0.8 0.8 0.8],'FaceAlpha',0.4,'EdgeColor',[0.5 0.5 0.5]);
end
%Plot 'luck'
plot([0 1],[0,1],'--r')
legend_labs = [legend_labs,{'Chance'}];
title(axes_handle,sprintf('Receiver Operator Curve'));
xlabel(axes_handle,'False positive rate','FontWeight','bold')
ylabel(axes_handle,'True positive rate','FontWeight','bold')
set(axes_handle,'Color',[1,1,1])
xlim([-0.05 1.05])
ylim([-0.05 1.05])
legend(legend_labs,'Location','SouthEast')