1
$\begingroup$

I'm working through the example code given by Matlab, but I can't seem to exactly reproduce the ROC curve that is plotted. I want to make sure I am understanding the thresholding concept properly. Could anyone help me to understand why the two figures plotted below are different?

clear; clc; load fisheriris;
pred = meas(51:end,1:2);
resp = (1:100)'>50;  % Versicolor = 0, virginica = 1
mdl = fitglm(pred,resp,'Distribution','binomial','Link','logit');
scores = mdl.Fitted.Probability;
[X,Y,T,AUC] = perfcurve(species(51:end,:),scores,'virginica');

figure; plot(X,Y);
xlabel('False positive rate'); ylabel('True positive rate');
title('ROC , built-in');

tpr = nan(length(T),1); fpr = nan(length(T),1);
for ind_F = 1:1:length(T)
  t_true = scores >= T(ind_F);
  group = resp; grouphat = t_true;
  t_cm = confusionmat(group,grouphat);
  % ROC : TPR / FPR
  tpr(ind_F) = t_cm(1,1)/sum(t_cm(1,:));
  fpr(ind_F) = t_cm(2,1)/sum(t_cm(2,:));
end

figure; plot(fpr,tpr); xlabel('fpr'); ylabel('tpr');
title('ROC , derived');

Thanks for the help.

$\endgroup$

closed as off-topic by mdewey, kjetil b halvorsen, Carl, Peter Flom Dec 21 '18 at 12:42

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – mdewey, kjetil b halvorsen, Carl, Peter Flom
If this question can be reworded to fit the rules in the help center, please edit the question.

0
$\begingroup$

After looking into the confusionmat code, I realized the problem. Matlab orders the "positive" variable as the 2nd dimension, i.e. second row and second column. In most specificity / sensitivity docs I had read the positive variable is ordered as the first dimension. As a result, the following plot matches:

tpr = nan(length(T),1); fpr = nan(length(T),1);
for ind_F = 1:1:length(T)
  t_true = scores >= T(ind_F);
  group = resp; grouphat = t_true;
  [t_cm,t_c] = confusionmat(group,grouphat);
  % ROC : TPR / FPR
  tpr(ind_F) = t_cm(2,2)/sum(t_cm(2,:));
  fpr(ind_F) = t_cm(1,2)/sum(t_cm(1,:));
end

figure; plot(fpr,tpr); xlabel('fpr'); ylabel('tpr');

Hopefully this might be helpful for someone else.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.