How to get a good fit : exp, power

Dear,
I am new in Igor Pro. It look very nice and powerful!
I make some tests:
Comparison with KaleidaGraph (My current software).:
Fit (x-y) data with power law. The results are very differents. For me KaleidaGraph gives the correct results.
Please find in the attached file my data (x=col 1, y= col2). Any suggestions are welcome.
Thank in advance
LdFlux.txt (1.53 KB)
darg wrote:
...
Comparison with KaleidaGraph (My current software).:
Fit (x-y) data with power law. The results are very differents.


My first response would be to ask ...

* Are you using exactly the same fitting equations?
* Are you using exactly the same methods of fitting (data range, coefficient limits, non-linear regression method)?

darg wrote:
For me KaleidaGraph gives the correct results.


I would wonder how you know or believe this?

What might help further is when you could post an example graphical output showing the fitted curve in each case.

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville
Igor's Power Law fit function fits

f(x) = y0 + A*x^pow

If the value of y0 is held at zero (which is the usual way a power law is used) then this function yields a straight line on a log-log plot. I have attached a file containing a graph of your data, column1 vs column0 (I have changed the column numbers to zero-based to match what Igor does). You can see that the data do not fall on a straight line on this plot.

Also on that graph I have shown a fit to the part of the data that are at least mostly straight. I held the y0 coefficient at zero. The fit report from the history:

•CurveFit/H="100"/NTHR=0/TBOX=0 Power ydata[pcsr(A),pcsr(B)] /X=xdata /D
Fit converged properly
Curve fit with data subrange:
ydata[8,24]
fit_ydata= W_coef[0]+W_coef[1]*fitX_ydata[p]^W_coef[2]
W_coef={0,1389.6,-0.24321}
V_chisq= 1.261e-05;V_npnts= 17;V_numNaNs= 0;V_numINFs= 0;
V_startRow= 8;V_endRow= 24;
W_sigma={0,140,0.00248}
Coefficient values ± one standard deviation
y0 =0 ± 0
A =1389.6 ± 140
pow =-0.24321 ± 0.00248

This appears to me to be reasonable. Can you give some more details about the problem you have? What equation did you fit in Kaleidagraph? How did you do the fit in Igor? What was the result? What, in detail, do you think is wrong with the result?

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
Graph0_5.png (12.14 KB) Graph0_5.png (12.14 KB) Graph0_5.png (12.14 KB)
Dear jjweimer and johnweeks,
First all, thank you very much for your quick responses. It is very appreciable!
Before posting my questions and read your answers, I have used Igor pro as KGraph without checking everything! Thank you!
jjweimer, the equations is not the same: Igor pro use y0+A*y^x, Kgraph use A*y^x.
KGraph is correct because I used it many time and check it many times. For basic functions like power law there is no problem with KGraph. For fitting methods I don't know much ! In KGraph the user manual is marked : "Least Squares Curve Fits" and "Levenberg-Marquardt algorithm".
johnweeks, Your results are closed to those givent by KGraph. In my first test I don't fixed y0. By fixing y0 results seem to be good.

Attached files contain the results.
For instant, It seem that using Igor pro in not very simple compared to KGraph. But Igor pro is very interesting for scripting and automation!
Thank you
Best regards

darg wrote:
jjweimer, the equations is not the same: Igor pro use y0+A*y^x, Kgraph use A*y^x.


Well, then that is one reason why the results are different.

darg wrote:
KGraph is correct because I used it many time and check it many times.


Check it many times against what? Unfortunately, as far as this statement is concerned, you have told me little to nothing that confirms the correctness (accuracy) of your answers using KG.

darg wrote:
For basic functions like power law there is no problem with KGraph. For fitting methods I don't know much ! In KGraph the user manual is marked : "Least Squares Curve Fits" and "Levenberg-Marquardt algorithm".
johnweeks, Your results are closed to those givent by KGraph. In my first test I don't fixed y0. By fixing y0 results seem to be good.


Ack, pooh! The graphs show, your results in KG are different from those you got in IP. I understand the methods in IP and completely trust their robustness, so I would say your results in KG are wrong. Prove me otherwise. Here are some questions in that direction ...

* Why are the fitting coefficients different when KG and IP should be using the same methods? What else is different between these two fitting tests?
* Using IP, the uncertainties in the fitting coefficients are directly quoted after the coefficients? What are the uncertainties on the fitting coefficients in KG? How confident are you in your coefficient -0.25064 using KG, and why did you fix the coefficient in IP to be exactly -0.24535 (as near as I can tell)?

Here are a few comments on a more tutorial approach ...

* In Igor Pro, you can constrain a fitting coefficient or allow it to vary. When you do the fit to the equation y0 + A*y^x, you should constrain y0 to 0 and allow both A and x to vary. The report that x = -0.24535 ± 0 suggests that you also constrained x to have no variation. Was this the case?

* Did you do a non-linear regression fit to z = A*y^x in Kalidegraph, or did you do a linear-regression fit to the linearized equation log(z) = x*log(y) + log(A)? The report with R^2 in your plot with Kalidegraph suggests the latter (you fit to a linearized equation), not the former (you fit using non-linear regression). The two methods are NOT the same!

Sorry perhaps to sound a bit harsh otherwise. The reports you are making are however indicating to me a basic misunderstanding of some of the key concepts about doing proper non-linear-regression curve fitting. This appears to have caused you to think that Igor Pro is "wrong", when in fact it could be that your fitting method (in Kalidegraph) has been wrong from the outset.

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville
johnweeks fit 17 points (points 8 through 24 - zero-based) but the Kaleidagraph fit is 18 points (points 7 through 24).

I fit 18 points and I get the same results as darg but I also get "40 iterations with no convergence". I suspect that the data are not a good fit to the function or the fit needs to be tweaked to converge with 18 points.

I have attached the experiment.

Note that I loaded the data as double-precision.
dargfit.pxp (54.21 KB)
Dear all,
Thank you for your comments and suggestions.
jjweimer, your comments are very useful and constructive. Thank you.
You are right: It seems that KGraph use linear-regression fit to the linearized equation log(z) = x*log(y) + log(A).
I calculate ln(Xdata) and ln(Ydata) and use Igor Pro to fit with "Line" function. I got the same results (see attached figure).
So I have a general question : What methods did you recomand (if it is possible):
1) fit with linearized equation
2) fit with equation

hrodstein, thank you!

Thank you again
IgorPro_Fit_lnYdataVslnXdata.png (43.33 KB)
darg wrote:
...
So I have a general question : What methods did you recomand (if it is possible):
1) fit with linearized equation
2) fit with equation


Fitting an equation via robust, non-linear methods is always preferred over linearizing the equation for fitting.

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville
Thank you for this very usefull discussion! I learn a lot!
My problem is solved.
However, as a new user of Igor Pro, any suggestions, examples of data fitting... to help me, are welcome!
Best regards.
darg wrote:
Thank you for this very usefull discussion! I learn a lot!


Glad to hear the discussions were enlightening.

darg wrote:
However, as a new user of Igor Pro, any suggestions, examples of data fitting... to help me, are welcome!


Doing the relevant tutorials in Igor Pro is usually highly recommended for new users. Even we "pros" often find useful tips in them that we did not otherwise know. Look under the File menu for Example Experiments.

This Web site and the email listserver are also top notch for getting answers to further questions.

Looking forward to hearing your successful progress!

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville
darg wrote:
You are right: It seems that KGraph use linear-regression fit to the linearized equation log(z) = x*log(y) + log(A).
I calculate ln(Xdata) and ln(Ydata) and use Igor Pro to fit with "Line" function. I got the same results (see attached figure).
So I have a general question : What methods did you recomand (if it is possible):
1) fit with linearized equation
2) fit with equation

The decision between using the nonlinear fit or the linearized fit to the log-log data depends on the structure of your measurement errors. If the measurement errors are proportional to the X values, it would be appropriate to use the linearized fit. If the measurement errors are independent of the X values, or vary in a way that's different from being proportional to the X values, then you should use the nonlinear fit. In the case of measurement errors that vary, but in a way that's not proportional to the X values, then you should be using a weighting wave giving the estimated errors for each data point.

In your case, in addition to whatever measurement errors your data include, you have a systematic misfit. That is, the power law model doesn't really describe your data perfectly. That is why it is necessary to fit a subset of your data. When I did the fit I saw that even with the subset of the data, the residuals were systematic- the data fall closer to a power law as the X values get larger, but they never really fall exactly on a power law model. Because this misfit decreases as you move to larger X values, it may not be a serious issue as long as you keep it in mind.

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
johnweeks wrote:

The decision between using the nonlinear fit or the linearized fit to the log-log data depends on the structure of your measurement errors. ...


Thanks for the better clarification.

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville

Sorry perhaps to sound a bit harsh otherwise. The reports you are making are however indicating to me a basic misunderstanding of some of the key concepts about doing proper non-linear-regression curve fitting. This appears to have caused you to think that Igor Pro is "wrong", when in fact it could be that your fitting method (in Kalidegraph) has been wrong from the outset.


Ahh...what a pet peeve. In theory I may be as guilty of a default bias in Igor's favor when somebody else's favorite program disagrees, but in practice I don't think I've ever been wrong to trust Igor's results. In fairness that has more to do with Igor's thorough documentation than an ability to always default to the best of multiple algorithms for all possible user data.

Good catch on the linearization of the power law. It almost certainly means Kalidegraph linearizes exponential fits as well, which I've run into before. It will suffer from the same problem...data close to the baseline will dominate the results. With the benefit of hindsight, the other hint besides reporting R^2 was the lack of an option to float the baseline. Log(Ay^x + y0) doesn't linearize.

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville[/quote]
ikonen wrote:

Ahh...what a pet peeve. In theory I may be as guilty of a default bias in Igor's favor when somebody else's favorite program disagrees, but in practice I don't think I've ever been wrong to trust Igor's results. In fairness that has more to do with Igor's thorough documentation than an ability to always default to the best of multiple algorithms for all possible user data.


Yes. Even I have to step back sometimes to recognize that my enthusiasm for Igor Pro might overstep my recommendation on the issue at hand. John's correction to my statements on when it is best to fit via linearized vs non-linearized methods are a case in point.

ikonen wrote:
Good catch on the linearization of the power law. It almost certainly means Kalidegraph linearizes exponential fits as well, which I've run into before. It will suffer from the same problem...data close to the baseline will dominate the results. With the benefit of hindsight, the other hint besides reporting R^2 was the lack of an option to float the baseline. Log(Ay^x + y0) doesn't linearize.


IOW, as far as regression analysis is concerned, fitting log(z) = log(ay + yo) is equivalent to fitting z = ay + yo but not equivalent to fitting log(z) = a*log(y) + log(yo).

Interesting insight there. Thanks!

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville