Saturday, August 13, 2016

sensitivity and specificity test using matlab

Matlab code example to generate some data a hypothetical scenario

Applications: sensitivity (true positive rate) and specificity (true negative rate) analysis In medical diagnosis and equipment test, sensitivity is a test of correctly identify those with the disease. specificity is a test of correctly identify those without the disease.

Contents

close

close all; % Clear all windows
clear; % Clear all variables
clc; % Clear console
clf;

subplot(211);
% this is the training data set
% group 1 (centered at 0.2, 0.2)
M1 = 20;
c1 = [0.2,0.2];
x1 = randn(M1,1)+c1(1);
y1 = randn(M1,1)+c1(2);

h1 = line(x1,y1,'linestyle','none','marker','o','DisplayName','normal');
line(c1(1),c1(2),'marker','x','color','b','markersize',12);


% group 2 (centered at 0.4,0.3)
M2 = 20;
c2 = [2, 3];
x2 = randn(M2,1)+c2(1);
y2 = randn(M2,1)+c2(2);

h2 = line(x2,y2,'linestyle','none','marker','^','color','r','DisplayName','atrial tach');
line(c2(1),c2(2),'marker','x','color','r','markersize',12);

xlim([-3,5]);
ylim([-3,7]);

legend([h1, h2]);
xlabel('\propto Heart rate');
ylabel('\propto P-R interval');
title('Hypothetical classification data');

perform linear classification

% concatenate the data
X = [x1, y1, ones(M1,1); x2, y2, ones(M2,1)];

% ground truth (desired classifier output)
d = [repmat(0,[M1,1]); repmat(1,[M2,1])];

% scatter plot
subplot(212);
plot(X(:,1),X(:,2),'o');
xlim([-3,5]);
ylim([-3,7]);

% solve for classifier weights
w = inv(X.' * X) * X.' * d;

% plot decision line
xx = linspace(min(xlim()), max(xlim()), 64);
yy = -w(1)/w(2)*xx - (w(3)-0.5)/w(2);
line(xx,yy,'color','k');

% note: decision boundary depends on the realization of the training data!

Apply classifier to the data

d_out = X*w>0.5;

figure;
cla
line(X((d_out==0),1), X((d_out==0),2),'marker','o','color','b','linestyle','none');
line(X((d_out==1),1), X((d_out==1),2),'marker','^','color','r','linestyle','none');

line(xx,yy,'color','k');

xlim([-3,5]);
ylim([-3,7]);
title('classifier output');





 

No comments:

Post a Comment