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

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')

Risposte (1)

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 Commenti

Dear John,
Thanks for the very detailed answer. I took the example that you have shown and used in my data and generated both the confidence intervals.
Im again sorry for my ignorance, but my supervisors asked me 'to provide confidence interval around the fitted model'. In this case should I be using the narrow bands for the function estimate or the observational bands?
Just to provide some background: these are growth rate data of a bacteria generated at different temperatures and the model is being fitted to create a predictive model that can be used to predict the growth rate in this temperature range.
x= 4,4,10,10,15,15,20,20,25,25,30,30,35,35,40,40
y=0.1157, 0.1233, 0.1865, 0.2520, 0.2519, 0.2814, 0.3680, 0.3186, 0.4549, 0.4559, 0.4241, 0.4697, 0.4953, 0.5767, 0.4163, 0.4279
Also here is my code and results
mdl= 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];
[fitresult, gof] = fit( xData, yData, ft, opts )
xint = linspace(min(x),max(x),100);
CIO = predint(fitresult,xint,0.95,'obs');
CIF = predint(fittedmdl,xint,0.95,'Functional');
plot(fitresult)
hold on
plot(x,y,'o')
plot(xint,CIF,':b')
plot(xint,CIO,':g')
Thanks,
Dipon
You can do that by adding 'predobs' to the line where you the plot function:
h = plot( fitresult, xData, yData, 'predobs' );
This works with 2018a version. I am not sure if it is still valid for other versions.
Then you can find the lower/upper limits by confint(Fitresult, 0.95), where Fitresult is the output from the curve fitting tool, and the second argument specifies the confidence interval.
Hope this helps.
Hi @John D'Errico. Thank you for your practical example. It has really helped me a lot. However, I was wondering whether you could provide a link to a documentation so that I learn more about it?
Thank you in anticipation.
I have no idea what links you are looking for. You can find the help for all of it in MATLAB. Just use the help. Or use doc.
help cfit/predint
PREDINT Prediction intervals for a fit result object or new observations. CI = PREDINT(FITRESULT,X,LEVEL) returns prediction intervals for a new Y value at the specified X value. LEVEL is the confidence level and has a default value of 0.95. CI = PREDINT(FITRESULT,X,LEVEL,'INTOPT','SIMOPT') specifies the type of interval to compute. 'INTOPT' can be either 'observation' (the default) to compute bounds for a new observation, or 'functional' to compute bounds for the curve evaluated at X. 'SIMOPT' can be 'on' to compute simultaneous confidence bounds or 'off' to compute non-simultaneous bounds. If 'INTOPT' is 'functional', the bounds measure the uncertainty in estimating the curve. If 'INTOPT' is 'observation', the bounds are wider to represent the addition uncertainty in predicting a new Y value (the curve plus random noise). Suppose the confidence level is 95% and 'INTOPT' is 'functional'. If 'SIMOPT' is 'off' (the default), then given a single pre-determined X value you have 95% confidence that the true curve lies between the confidence bounds. If 'SIMOPT' is 'on', then you have 95% confidence that the entire curve (at all X values) lies between the bounds. Documentation for cfit/predint doc cfit/predint
@Dipon Sarkar Please mark this answer as correct, especially given the time that John put in to writing this extremely detailed answer.

Accedi per commentare.

Richiesto:

il 8 Dic 2020

Commentato:

il 14 Nov 2022

Community Treasure Hunt

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

Start Hunting!

Translated by