How can add confidence intervals in the plot generated by the Curve Fitting Toolbox?
Mostra commenti meno recenti
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)
John D'Errico
il 8 Dic 2020
Modificato: John D'Errico
il 8 Dic 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])
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
If you don't know what methods apply there, then use methods!!!!!!!
methods(fittedmdl)
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
Dipon Sarkar
il 10 Dic 2020
Salman Salamn
il 18 Nov 2021
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.
Editor
il 7 Ott 2022
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
Editor
il 10 Ott 2022
@John D'Errico Thank you very much
Dani A
il 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.
Categorie
Scopri di più su Linear Predictive Coding in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

