www.gusucode.com > stats 源码程序 matlab案例代码 > stats/ComputePointwiseConfidenceIntervalsForROCCurveExample.m
%% Compute Pointwise Confidence Intervals for ROC Curve % Load the sample data. % Copyright 2015 The MathWorks, Inc. load fisheriris %% % The column vector |species| consists of iris flowers of three different % species: setosa, versicolor, virginica. The double matrix |meas| consists % of four types of measurements on the flowers: sepal length, sepal width, % petal length, and petal width. All measures are in centimeters. %% % Use only the first two features as predictor variables. Define a binary % problem by using only the measurements that correspond to the % versicolor and virginica species. pred = meas(51:end,1:2); %% % Define the binary response variable. resp = (1:100)'>50; % Versicolor = 0, virginica = 1 %% % Fit a logistic regression model. mdl = fitglm(pred,resp,'Distribution','binomial','Link','logit'); %% % Compute the pointwise confidence intervals on the true positive rate (TPR) by % vertical averaging (VA) and sampling using bootstrap. [X,Y,T] = perfcurve(species(51:end,:),mdl.Fitted.Probability,... 'virginica','NBoot',1000,'XVals',[0:0.05:1]); %% % |'NBoot',1000| sets the number of bootstrap replicas to 1000. % |'XVals','All'| prompts |perfcurve| to return |X|, |Y|, and |T| values % for all scores, and average the |Y| values (true positive rate) at % all |X| values (false positive rate) using vertical % averaging. If you do not specify |XVals|, then |perfcurve| computes % the confidence bounds using threshold averaging by default. %% % Plot the pointwise confidence intervals. errorbar(X,Y(:,1),Y(:,1)-Y(:,2),Y(:,3)-Y(:,1)); xlim([-0.02,1.02]); ylim([-0.02,1.02]); xlabel('False positive rate') ylabel('True positive rate') title('ROC Curve with Pointwise Confidence Bounds') legend('PCBwVA','Location','Best') %% % It might not always be possible to control the false positive rate (FPR, % the |X| value in this example). So you might want to compute the pointwise % confidence intervals on true positive rates (TPR) by threshold averaging. [X1,Y1,T1] = perfcurve(species(51:end,:),mdl.Fitted.Probability,... 'virginica','NBoot',1000); %% % If you set |'TVals'| to |'All'|, or if you do not specify |'TVals'| or |'Xvals'|, then % |perfcurve| returns |X|, |Y|, and |T| values for all scores and computes % pointwise confidence bounds for |X| and |Y| using threshold averaging. %% % Plot the confidence bounds. figure() errorbar(X1(:,1),Y1(:,1),Y1(:,1)-Y1(:,2),Y1(:,3)-Y1(:,1)); xlim([-0.02,1.02]); ylim([-0.02,1.02]); xlabel('False positive rate') ylabel('True positive rate') title('ROC Curve with Pointwise Confidence Bounds') legend('PCBwTA','Location','Best') %% % Specify the threshold values to fix and compute the ROC curve. Then plot % the curve. [X1,Y1,T1] = perfcurve(species(51:end,:),mdl.Fitted.Probability,... 'virginica','NBoot',1000,'TVals',0:0.05:1); figure() errorbar(X1(:,1),Y1(:,1),Y1(:,1)-Y1(:,2),Y1(:,3)-Y1(:,1)); xlim([-0.02,1.02]); ylim([-0.02,1.02]); xlabel('False positive rate') ylabel('True positive rate') title('ROC Curve with Pointwise Confidence Bounds') legend('PCBwTA','Location','Best')