How to retrieve the fit and confidence bounds values, plotted in the graph upon using fitlm function?
41 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (1 件)
Star Strider
2022 年 8 月 10 日
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 件のコメント
Star Strider
2022 年 8 月 11 日
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
2022 年 8 月 11 日
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!