How can add confidence intervals in the plot generated by the Curve Fitting Toolbox?

84 views (last 30 days)
I have used the Curve Fitting Toolbox to fit a custom equation (modified Ratkowsky sqaure root model) to my data and generated using the Nonlinear least square method with the trust region algorithm. The app generates the 95%confidence limit of each of the parameters, but how can i use this to generate th confidence limits of the fit in the plot?
I have generated the code of the fitting from the app to edit the plot and the code is given below:
(aplogies if this is not enough informatio to asnwer the question. I am relatively new to coding, so if anyone needs more info, I can provide it).
Thanks,
Dipon
%% Fit: 'untitled fit 1'.
[xData, yData] = prepareCurveData( mid35x, mid35y );
% Set up fittype and options.
ft = fittype( 'a*(x-c)*(1-exp(b*(x-d)))', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [0.260107478852342 0.290941052369803 0.995531610700984 0.800330575352401];
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult, xData, yData );
xlim([0,45]);
legend('observed growth rate', 'fitted model', 'Location', 'NorthWest' )
% Label axes
xlabel 'Temperature (in °C)'
ylabel 'Sq GR'
grid on
print(gcf,'foo.png','-dpng','-r300')

Answers (1)

John D'Errico
John D'Errico on 8 Dec 2020
Edited: John D'Errico on 8 Dec 2020
I lack your data. But it is simple enough to make some up.
x = rand(50,1);
y = 1 + 2*exp(0.75*x) + randn(size(x))/10;
plot(x,y,'o')
So some arbitrary crap data. The model is exponential.
mdl = fittype('a + b*exp(c*x)','indep','x');
fittedmdl = fit(x,y,mdl,'start',[1 1 1])
fittedmdl =
General model: fittedmdl(x) = a + b*exp(c*x) Coefficients (with 95% confidence bounds): a = 1.166 (-0.2608, 2.593) b = 1.844 (0.4799, 3.208) c = 0.783 (0.3754, 1.191)
Pretty noisy data, so the parameters are not that close to the underlying model. fittedmdl is an object from the curve fitting TB.
whos fittedmdl
Name Size Bytes Class Attributes fittedmdl 1x1 837 cfit
If you don't know what methods apply there, then use methods!!!!!!!
methods(fittedmdl)
Methods for class cfit: argnames coeffnames dependnames fitoptions integrate numcoeffs probnames type category coeffvalues differentiate formula islinear plot probvalues cfit confint feval indepnames numargs predint setoptions
Scan through the list. There, we see confint is one of the methods. Any bets what confint does? How about predint? Be careful, because you have both confint and predint available. You NEED to know the difference. confint just gives you intervals on the parameters.
xint = linspace(min(x),max(x),100);
CIF = predint(fittedmdl,xint,0.95,'Functional');
CIO = predint(fittedmdl,xint,0.95,'obs');
plot(fittedmdl)
hold on
plot(x,y,'o')
plot(xint,CIF,':b')
plot(xint,CIO,':g')
The wider set of bands allow you to predict where a new observation would fall. The narrow bands are around the estimated function itself. So the observational bands essentially have the estimated process noise added back in.
  6 Comments
Dani Agramonte
Dani Agramonte on 14 Nov 2022
@Dipon Sarkar Please mark this answer as correct, especially given the time that John put in to writing this extremely detailed answer.

Sign in to comment.

Categories

Find more on Linear and Nonlinear Regression 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!