How do I determine which model fits my data the best?

27 views (last 30 days)
I am fitting two different curves to a set of data to determine which of them is the better fit. I'm using
myfit1 = fittype('-2^(a.*x+b)+c','coefficients',{'a','b','c'});
fit1 = fit(scantimes,data,myfit1,'StartPoint',[0.005,-2,0.4])
myfit2 = fittype('exp(-d.*x+f)+g','coefficients',{'d','f','g'});
fit2 = fit(scantimes,data,myfit2,'StartPoint',[0.04,1,0])
in a try and catch sequence such that the code is able to compute a fit for each equation. The data will follow one or the other of these patterns, however, I need a way to analyse which is the closer fit. scantimes and data are both just a list of points and I have guessed the start point from observation of data. How would I go about making a comparison between the two fits? Should this use chi squared or is there an easier/better way?

Accepted Answer

dpb
dpb on 16 Sep 2019
I'd start by returning the second, optional gof (goodness of fit) output variable that contains
Field Value
sse Sum of squares due to error
rsquare R-squared (coefficient of determination)
dfe Degrees of freedom in the error
adjrsquare Degree-of-freedom adjusted coefficient of determination
rmse Root mean squared error (standard error)
See the doc for fit for details...of course, I'd also want to plot the data and fitted results to ensure they make sense, not just rely on one or two statistics.
myfit1 = fittype('-2^(a.*x+b)+c','coefficients',{'a','b','c'});
[fit1,gof1] = fit(scantimes,data,myfit1,'StartPoint',[0.005,-2,0.4]);
myfit2 = fittype('exp(-d.*x+f)+g','coefficients',{'d','f','g'});
[fit2,gof2] = fit(scantimes,data,myfit2,'StartPoint',[0.04,1,0]);
Also, as a general coding paradigm I'd strongly recommend to consider recasting the variable naming scheme to use cell arrays or similar rather than using similar names with hardcoded numeric suffixes. Much simpler to generalize the code that way.

More Answers (1)

the cyclist
the cyclist on 16 Sep 2019
The second output from the fit command gives goodness-of-fit statistics.
You might also take a look at "Determing the Best Fit" in the documentation here.

Community Treasure Hunt

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

Start Hunting!