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

38 次查看(过去 30 天)
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?

采纳的回答

dpb
dpb 2019-9-16
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.

更多回答(1 个)

the cyclist
the cyclist 2019-9-16
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.

类别

Help CenterFile Exchange 中查找有关 Linear and Nonlinear Regression 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by