How to retrieve the fit and confidence bounds values, plotted in the graph upon using fitlm function?

40 views (last 30 days)
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)

Answers (1)

Star Strider
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')
mdl =
Linear regression model: y ~ 1 + x1 Estimated Coefficients: Estimate SE tStat pValue ________ ________ _______ __________ (Intercept) 4.822 0.57225 8.4264 2.9992e-05 x1 -2.0132 0.092227 -21.829 2.0461e-08 Number of observations: 10, Error degrees of freedom: 8 Root Mean Squared Error: 0.838 R-squared: 0.983, Adjusted R-Squared: 0.981 F-statistic vs. constant model: 476, p-value = 2.05e-08
figure
% plot (x,y,'o');
hold on
plot(mdl)
hold off
[ypre,yci] = predict(mdl, x) % Easy Way
ypre = 10×1
2.8088 0.7956 -1.2176 -3.2308 -5.2440 -7.2572 -9.2704 -11.2836 -13.2968 -15.3100
yci = 10×2
1.6734 3.9442 -0.1673 1.7585 -2.0274 -0.4077 -3.9199 -2.5416 -5.8640 -4.6239 -7.8772 -6.6371 -9.9595 -8.5812 -12.0934 -10.4737 -14.2597 -12.3339 -16.4454 -14.1746
Lines = findobj(gca, 'Type','Line') % More Challenging Way
Lines =
4×1 Line array: Line Line (Confidence bounds) Line (Fit) Line (Data)
CI = findobj('Type','line','DisplayName','Confidence bounds')
CI =
Line (Confidence bounds) with properties: Color: [1 0 0] LineStyle: ':' LineWidth: 0.5000 Marker: 'none' MarkerSize: 6 MarkerFaceColor: 'none' XData: [1 1.0909 1.1818 1.2727 1.3636 1.4545 1.5455 1.6364 1.7273 1.8182 1.9091 2 2.0909 2.1818 2.2727 2.3636 2.4545 2.5455 2.6364 2.7273 2.8182 2.9091 3 3.0909 3.1818 3.2727 3.3636 3.4545 3.5455 3.6364 3.7273 3.8182 3.9091 4 4.0909 … ] YData: [1.6734 1.5067 1.3398 1.1728 1.0058 0.8386 0.6712 0.5038 0.3362 0.1685 6.8919e-04 -0.1673 -0.3355 -0.5038 -0.6723 -0.8409 -1.0098 -1.1788 -1.3481 -1.5176 -1.6873 -1.8572 -2.0274 -2.1979 -2.3686 -2.5396 -2.7110 -2.8826 -3.0546 … ] Show all properties
CIX = CI.XData
CIX = 1×100
1.0000 1.0909 1.1818 1.2727 1.3636 1.4545 1.5455 1.6364 1.7273 1.8182 1.9091 2.0000 2.0909 2.1818 2.2727 2.3636 2.4545 2.5455 2.6364 2.7273 2.8182 2.9091 3.0000 3.0909 3.1818 3.2727 3.3636 3.4545 3.5455 3.6364
CIY = CI.YData
CIY = 1×100
1.6734 1.5067 1.3398 1.1728 1.0058 0.8386 0.6712 0.5038 0.3362 0.1685 0.0007 -0.1673 -0.3355 -0.5038 -0.6723 -0.8409 -1.0098 -1.1788 -1.3481 -1.5176 -1.6873 -1.8572 -2.0274 -2.1979 -2.3686 -2.5396 -2.7110 -2.8826 -3.0546 -3.2269
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
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')
mdl =
Linear regression model: y ~ 1 + x1 Estimated Coefficients: Estimate SE tStat pValue ________ _______ ______ __________ (Intercept) 5.596 0.54448 10.278 6.9152e-06 x1 -1.9902 0.08775 -22.68 1.5131e-08 Number of observations: 10, Error degrees of freedom: 8 Root Mean Squared Error: 0.797 R-squared: 0.985, Adjusted R-Squared: 0.983 F-statistic vs. constant model: 514, p-value = 1.51e-08
[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.
.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!