How to retrieve the fit and confidence bounds values, plotted in the graph upon using fitlm function?
40 views (last 30 days)
Show older comments
Dear All,
I have used fitlm function to fit my data. When we plot the data automatically, confidence bounds and the fits are plotted. I want the values for fits and confidence bounds as i want to plot it graph plotting software-sigmaplot. I have attached the code and the graph for reference. x and y have a linear relationship.
Please help!
mdl = fitlm(x,y,'poly1')
plot (x,y,'o');
hold on
plot(mdl)
0 Comments
Answers (1)
Star Strider
on 10 Aug 2022
That is one of the 'line' objects, however getting the information from it is not straightforward. Itis easier to just use the predict function (links follow) —
x = (1:10).';
y = 5-2*x + randn(size(x));
mdl = fitlm(x,y,'poly1')
figure
% plot (x,y,'o');
hold on
plot(mdl)
hold off
[ypre,yci] = predict(mdl, x) % Easy Way
Lines = findobj(gca, 'Type','Line') % More Challenging Way
CI = findobj('Type','line','DisplayName','Confidence bounds')
CIX = CI.XData
CIY = CI.YData
It is possible to get the information from the figure, however that requires also getting the 'Fit' data as well as the 'Confidence Bounds' data and then calculating the upper confidence bound from it. It is easiest to just use the predict function and be done with it. (Getting the information depends on whether the name in the 'lines' object is a 'Tag' or 'DisplayName' property. The findobj call requres that information to work correctly.)
.
3 Comments
Star Strider
on 11 Aug 2022
NOTE — To use predict most effectively, use that output to draw the plot —
x = (1:10).';
y = 5-2*x + randn(size(x));
mdl = fitlm(x,y,'poly1')
[ypre,yci] = predict(mdl, x); % Easy Way
figure
hd = plot (x,y,'o', 'DisplayName','Data');
hold on
hf = plot(x, ypre, '-r', 'DisplayName','Fit');
hci = plot(x, yci, ':r', 'DisplayName','Confidence bounds');
hold off
xlabel('X')
ylabel('Y')
legend([hd;hf;hci(1)], 'Location','best')
The data are random, so not the same as the previous plot.
.
Star Strider
on 11 Aug 2022
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!