Tuesday, April 24, 2012

matlab ROC curve


function makeROC()

[X,Y,T,AUC] = perfcurve(  trueTestLabel ,  predictedScore , positiveClassLabel );

AUC
plot(X,Y)
xlabel('1-specificity'); ylabel('sensitivity');
figure
plot(T, [Y  1-X   ] );
legend('sensitivity','specificity');
xlabel('Threshold'); ylabel('change in sensitivity and specificity')

end


function perfBasedOnROCthreshold

thr = .41
rocInfo = load('roc20.info');
perfOrigLabel = rocInfo(: , 1);
perfPredLabel = rocInfo(: , 2);
perfPredScore = rocInfo(: , 3);
noTeseCase = size(perfPredScore ,1);
newPredictedLabel = zeros( noTeseCase,1);

[X,Y,T,AUC] = perfcurve(perfOrigLabel, perfPredScore,1);
plot(X,Y)
xlabel('1-specificity'); ylabel('sensitivity');

figure
plot(T,Y)
xlabel('threshold'); ylabel('sensitivity');

figure
plot(T,1-X)
xlabel('threshold'); ylabel('specificity');

newPos = find(perfPredScore >= thr);
newNeg = find(perfPredScore < thr);
newPredictedLabel(newPos) = 1;
newPredictedLabel(newNeg) = MINUSLABEL;


[C,order] = confusionmat(perfOrigLabel,newPredictedLabel,'order', [1 MINUSLABEL]);
tp = C(1,1);
fn = C(1,2);
fp = C(2,1);
tn = C(2,2);
       
finalSen = (tp)/(tp+fn);
finalSpe = (tn)/(fp+tn);
finalAcc = (tp+tn)/(tp+fn + fp+tn);

end