%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Bland-Altman plot
%
% Bland Altam is a method of data plotting used in analysing the agreement
% between two different assays.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Written by:
% Andrea Padoan
% University Hospital of Padua, Italy
% 12-13-2008
% -------------------------------------------------------------------
% Please respect GNU General Public License agreement:
% Someone can distribute this program plus their own modifications.
% Thus, all enhancements and additions to this software must also be
% distributed as free software
% -------------------------------------------------------------------
% You must define data array before use the code. It must contain 3 columns
% array:
% data (:, 1) = first column = sample number (or patient number)
% data (:, 1) can id number or what you want or zero (it isnt' used by the
% program)
% data (:, 2) = second column = instrument 1 output = S1
% data (:, 3) = third column = instrument 2 output = S2
% Array funxy contains the Cartesian coordinates of Bland Altman function,
% given of sample S with value data (:, 1)=S1 and data (:, 2)=S2 determined by
% the two assays. The Bland Altman function is
% S(x,y)= ( (S1+S2)/2, (S1-S2) )
funxy(:,1) = (data(:,2) + data(:,3))/2; %poin mean between two methods
funxy(:,2) = data (:,2) - data (:,3); %point difference between two methods
disp ('Bland Altman Plot. Written by Andrea Padoan, 2008');
%Delete NaN if present. Bland Altman can't use missing values
funxy(any(isnan(funxy),2),:) = [ ];
%Calculate mean of differences
diffmean = mean (funxy (:, 2))
%Calculate standard deviation of difference
sdev = std(funxy(:,2), 1)
%You want to calculate 95 % confidence interval for the mean = +-2sd
twosdev = sdev * 1.96;
%Calculate max value of the mean
maxx = max (funxy (:, 1));
%Calculate max value of the difference
maxy = max (funxy (:, 2));
if (maxy < twosdev)
maxy = twosdev;
end
%Add 1 sd to optimize graph
maxy = maxy + sdev;
%Min value of difference
miny = min (funxy (:, 2));
if (miny > -twosdev)
miny = -twosdev;
end
%Subtract 1 sd to optimize graph
miny = miny - sdev;
% scatterplot of funxy
figure (1);
scatter (funxy(:,1), funxy(:,2));
%xlim ([0 maxx]);
%ylim ([miny maxy]);
axis ([0 maxx miny maxy]);
axis on;
hold on;
%Title and axis titles
title ('Bland-Altman plot');
xlabel ('Average by two assays');
ylabel ('Difference (Assay A - Assay B)');
%Draw 2*sdev line
line ([0 maxx], [(twosdev+diffmean) (twosdev+diffmean)],'LineStyle','--');
text (maxx, twosdev, ' 1.96 sd ', 'Color', 'b' , 'FontSize', 12, 'FontWeight', 'bold');
line ([0 maxx], [(diffmean-twosdev) (diffmean-twosdev)], 'LineStyle','--');
text (maxx, -twosdev, ' -1.96 sd', 'Color', 'b' , 'FontSize', 12, 'FontWeight', 'bold');
%Draw line as mean of differences
line ([0 maxx], [diffmean diffmean],'LineStyle','-', 'LineWidth', 2);
%Add text 'mean'
text (maxx, diffmean, ' mean', 'Color', 'b' , 'FontSize', 14, 'FontWeight', 'bold');
clear funxy miny maxy maxx twosdev;