How to find the Regression line and the Bland-Altman plot
30 views (last 30 days)
Show older comments
Hi,
I have a medical data with 1*124 matrix in each data, I have plotted the Regression line with the y=mx+c formula and also ploted the Bland-Altman plot, but for the Bland-Altman plot I am not getting the correct output, I dont know why can any one help me out.
I used the code below to plot the
- Regression line
- Residuals
- Linear Fit of Data with 95% Prediction Interval
- Bland-Altman plot (I need to find the mean value with upper and lower boundaries)
I also attache the data which I used to extract the Bland-Altman, Can anyone help me is there any mistake and how to rectify it or is my method correct I cannot understand and cannot plot the mean value.
I have also attche the pictures of Bland-Altman plot and Regression model
Please can any one help me.
% This is a demo for lineare Regression modell
% Plot of the fitting model, the coefficient for the regression line, evaluation of the godness of the model
clc;
close all;
xdata = load('C:\Users\lokes\OneDrive\Desktop\Thesis\Thesis Data\Merged files\New files after all editing\New SP\Aorta');
ydata = load('C:\Users\lokes\OneDrive\Desktop\Thesis\Thesis Data\Merged files\New files after all editing\New SP\Femoral');
% Data
x = (xdata.x)';
y = (ydata.y)';
% Lineare regression line
[xyfit,GodnessFit] = fit(x,y,'poly1'); % for quadratic regression Poly2,Cubic Poly 3,
% This will be the output of the regression analysis
% xyfit =
%
% Linear model Poly1:
% xyfit(x) = p1*x + p2
% Coefficients (with 95% confidence bounds):
% p1 = 2.011 (1.711, 2.311)
% p2 = 140.9 (134.4, 147.4)
% Coefficient values of the regression model; y=p1x+p2
fprintf('The coefficient of the regression Model:')
coeff=coeffvalues(xyfit)
% Evaluation of the fitting model
fprintf(' The Godness of the regression Modell:')
R2=GodnessFit.rsquare
R=sqrt(R2)
% Plot of Regression model
figure;
plot(xyfit,x,y,'o')
title('Regression model')
% Plot of the Residuals: the data outside the 95% range are as oultiers
figure;
plot(xyfit,x,y,'o','residuals')
title('Residuals')
%%Polyfit function
[p,S] = polyfit(x,y,1);
[y_fit,delta] = polyval(p,x,S);
figure;
plot(x,y,'bo')
hold on
plot(x,y_fit,'r-')
plot(x,y_fit+2*delta,'m--',x,y_fit-2*delta,'m--')
title('Linear Fit of Data with 95% Prediction Interval')
legend('Data','Linear Fit','95% Prediction Interval')
% Bland Altmann plot
var1=y';
var2=x';
means = mean([var1;var2]);
diffs = var1-var2;
meanDiff = mean(diffs);
sdDiff = std(diffs);
CR = [meanDiff + 1.96 * sdDiff, meanDiff - 1.96 * sdDiff]; %%95% confidence range
linFit = polyfit(means,diffs,1); %%%work out the linear fit coefficients
% Bland altman plots
figure;
plot(means,diffs,'o')
hold on
plot(means, ones(1,length(means)).*CR(1),'k-'); %%%plot the upper CR
plot(means, ones(1,length(means)).*CR(2),'k-'); %%%plot the lower CR
plot(means,zeros(1,length(means)),'k'); %%%plot zero
plot(means, means.*linFit(1)+linFit(2),'k--'); %%%plot the linear fit
%function [means,diffs,meanDiff,CR,linFit] = BlandAltman(var1, var2, flag)
%%%Plots a Bland-Altman Plot
%%%INPUTS:
%%% var1 and var2 - vectors of the measurements
%%%flag - how much you want to plot
%%% 0 = no plot
%%% 1 = just the data
%%% 2 = data and the difference and CR lines
%%% 3 = above and a linear fit
%%%
%%%OUTPUTS:
%%% means = the means of the data
%%% diffs = the raw differences
%%% meanDiff = the mean difference
%%% CR = the 2SD confidence limits
%%% linfit = the paramters for the linear fit
%
% if (nargin<1)
% %%%Use test data
% var1=[512,430,520,428,500,600,364,380,658,445,432,626,260,477,259,350,451];%,...
% var2=[525,415,508,444,500,625,460,390,642,432,420,605,227,467,268,370,443];
% flag = 3;
% end
%
% if nargin==2
% flag = 0;
% end
%
% means = mean([var1;var2]);
% diffs = var1-var2;
%
% meanDiff = mean(diffs);
% sdDiff = std(diffs);
% CR = [meanDiff + 1.96 * sdDiff, meanDiff - 1.96 * sdDiff]; %%95% confidence range
%
% linFit = polyfit(means,diffs,1); %%%work out the linear fit coefficients
% plot(means, ones(1,length(means)).*CR(1),'k-'); %%%plot the upper CR
% plot(means, ones(1,length(means)).*CR(2),'k-'); %%%plot the lower CR
% plot(means,zeros(1,length(means)),'k'); %%%plot zero
%
% %%%plot results unless flag is 0
% if flag ~= 0
% plot(means,diffs,'o')
% hold on
% if flag > 1
% plot(means, ones(1,length(means)).*CR(1),'k-'); %%%plot the upper CR
% plot(means, ones(1,length(means)).*CR(2),'k-'); %%%plot the lower CR
% plot(means,zeros(1,length(means)),'k'); %%%plot zero
% end
% if flag > 2
% plot(means, means.*linFit(1)+linFit(2),'k--'); %%%plot the linear fit
% end
% title('Bland-Altman Plot')
% end
%
% end
% % save the regression modell data
2 Comments
Image Analyst
on 30 Jul 2022
What should the correct output look like?
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
Rik
on 1 Aug 2022
Also, why did you include code from someone else without attribution? Or did you just happen to land on the same syntax as one of the most popular Bland-Altman plot functions on the file exchange?
Answers (0)
See Also
Categories
Find more on Polynomials in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!