how to make a roc curve on matlab

7 views (last 30 days)
I want to generate a ROC curve for the data in the attached excel sheet. I am using the following code:
meanthresh = 0.8:0.1:2.5; % This alters the mean threshold between 0.8 and 2.5 by 0.1
%% Beginning the Mean ROC
for a = 1:length(meanthresh) %% the for loop makes it to where the threshold is automatically altered and you do not have to do it manually
positive_a = meanthresh(a) < (prelimdataforROCs(:, 3)); % declaring an algorithm to determine if the value is greater than the threshold
true = prelimdataforROCs(:, 1) == 1; %true = if the data in the column is equal to (==) 1
false = prelimdataforROCs(:, 1) == 0; %false = if the data in the column is equal to (==) 0
TP = sum(positive_a(true)); %true positive = if both the true statement (true) and the positive statement (positive_a) are met, then sum all the true values
FP = sum(positive_a(false)); %false positive = if both the false statement (false) and the positive statement (positive_a) are met, then sum all the false values
FN = sum(true) - TP; %false negative = sum of all true values minus the true positives
TN = sum(false) - FP; %true negative = sum of all false values minus the false positives
sensitivity_a(a) = (TP/(TP+FN)); %%sensitivity equation
specificity_a(a) = (TN/(TN+FP)); %%specificity equation
end %% end of for loop
stdevthresh = 0:0.1:1.7; %This alters the st dev threshold from 0 to 1.7 by 0.1
% Beginning of St Dev ROC
for b = 1:length(stdevthresh)
positive_b = stdevthresh(b) < (prelimdataforROCs(:, 4)); % declaring an algorithm to determine if the value is greater than the threshold
true = prelimdataforROCs(:, 1) == 1; %true = if the data in the column is equal to (==) 1
false = prelimdataforROCs(:, 1) == 0; %false = if the data in the column is equal to (==) 0
TPsd = sum(positive_b(true)); %true positive = if both the true statement (true) and the positive statement (positive_b) are met, then sum all the true values
FPsd = sum(positive_b(false)); %false positive = if both the false statement (false) and the positive statement (positive_b) are met, then sum all the false values
FNsd = sum(true) - TPsd; %false negative = sum of all true values minus the true positives
TNsd = sum(false) - FPsd; %true negative = sum of all false values minus the false positives
sensitivity_b(b) = (TPsd/(TPsd+FNsd)); %%equations for sensitivity and specificity
specificity_b(b) = (TNsd/(TNsd+FPsd));
end
plot(1-specificity_b, sensitivity_b, 'Marker', '.', 'MarkerSize', 16, 'LineWidth', 1); %%this is the standard deviation plot
hold on %%allows more than one graph to be on the same plot--must be followed by "hold off" when done
plot(1-specificity_a, sensitivity_a, 'Marker', '.', 'MarkerSize', 24, 'LineWidth', 1); %%plotting command for the mean ROC
hold off
legend('St. Devs', 'Averages'); %%this puts a legend on the graph--legend('first name', 'second name')
xlabel('False Positive Rate'); %%label on x axis
ylabel('True Positive Rate'); %%label on y axistitle('ROC for Mean Path Length'); %%title of graph
But I am getting the following error: "Undefined operator '<' for input arguments of type 'table'."
Can you please tell me where I am going wrong?

Accepted Answer

Meg Noah
Meg Noah on 6 Jan 2020
I was able to run a slightly modified version with no errors. I do not know if it is doing what you want it to do.
% I want to generate a ROC curve for the data in the attached excel sheet. I am using the following code:
meanthresh = 0.8:0.1:2.5; % This alters the mean threshold between 0.8 and 2.5 by 0.1
rocTable = readtable('prelim data for ROCs.xlsx','range','$A3:$D18');
%% Beginning the Mean ROC
for a = 1:length(meanthresh) %% the for loop makes it to where the threshold is automatically altered and you do not have to do it manually
positive_a = meanthresh(a) < (rocTable.Average); % declaring an algorithm to determine if the value is greater than the threshold
true = rocTable.FID == 1; %true = if the data in the column is equal to (==) 1
false = rocTable.FID == 0; %false = if the data in the column is equal to (==) 0
TP = sum(positive_a(true)); %true positive = if both the true statement (true) and the positive statement (positive_a) are met, then sum all the true values
FP = sum(positive_a(false)); %false positive = if both the false statement (false) and the positive statement (positive_a) are met, then sum all the false values
FN = sum(true) - TP; %false negative = sum of all true values minus the true positives
TN = sum(false) - FP; %true negative = sum of all false values minus the false positives
sensitivity_a(a) = (TP/(TP+FN)); %%sensitivity equation
specificity_a(a) = (TN/(TN+FP)); %%specificity equation
end %% end of for loop
stdevthresh = 0:0.1:1.7; %This alters the st dev threshold from 0 to 1.7 by 0.1
% Beginning of St Dev ROC
for b = 1:length(stdevthresh)
positive_b = stdevthresh(b) < (rocTable.SD); % declaring an algorithm to determine if the value is greater than the threshold
true = rocTable.FID == 1; %true = if the data in the column is equal to (==) 1
false = rocTable.FID == 0; %false = if the data in the column is equal to (==) 0
TPsd = sum(positive_b(true)); %true positive = if both the true statement (true) and the positive statement (positive_b) are met, then sum all the true values
FPsd = sum(positive_b(false)); %false positive = if both the false statement (false) and the positive statement (positive_b) are met, then sum all the false values
FNsd = sum(true) - TPsd; %false negative = sum of all true values minus the true positives
TNsd = sum(false) - FPsd; %true negative = sum of all false values minus the false positives
sensitivity_b(b) = (TPsd/(TPsd+FNsd)); %%equations for sensitivity and specificity
specificity_b(b) = (TNsd/(TNsd+FPsd));
end
plot(1-specificity_b, sensitivity_b, 'Marker', '.', 'MarkerSize', 16, 'LineWidth', 1); %%this is the standard deviation plot
hold on %%allows more than one graph to be on the same plot--must be followed by "hold off" when done
plot(1-specificity_a, sensitivity_a, 'Marker', '.', 'MarkerSize', 24, 'LineWidth', 1); %%plotting command for the mean ROC
hold off
legend('St. Devs', 'Averages'); %%this puts a legend on the graph--legend('first name', 'second name')
xlabel('False Positive Rate'); %%label on x axis
ylabel('True Positive Rate'); %%label on y axistitle('ROC for Mean Path Length'); %%title of graph
% But I am getting the following error: "Undefined operator '<' for input arguments of type 'table'."
% Can you please tell me where I am going wrong?
The resulting image is:
roc.png
  5 Comments
Meg Noah
Meg Noah on 7 Jan 2020
Great! If you're satisfied, do you mind accepting the answer?
Namarta Kapil
Namarta Kapil on 12 Jan 2020
Hi,
I have been using this code in the computer in my lab and it works fine but for some reason on my personal computer its giving the following error:
"Error using readtable (line 223)
Unable to determine range. Range must be of the form 'A1' (cell), 'A:B' (column-select), '1:5' (row-select), 'A1:B5' (rectangle-select), or a valid named range in the sheet."
Would you be able to tell me what this means and how to remedy it?

Sign in to comment.

More Answers (0)

Categories

Find more on ROC - AUC in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!