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

조회 수: 20 (최근 30일)
JJH
JJH 2019년 9월 16일
답변: dpb 2019년 9월 16일
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