3
$\begingroup$

I want to fit my data from a txt file to a curve in log-log scale. What I tried to do was this:

data = Import["Path", "Table"][[All, {2, 1}]];
FindFit[Log[data], Log[1 - Gamma[A, B/x]/Gamma[A]], {A, B}, x]

When I plot this, this is not the scale I get from ListLogLogPlot. How do I fit data on log-log scale to a curve in log-log scale? The curve I want to fit is $f(x)=1-\Gamma[a,b/x]/\Gamma[a]$ (Data and everything is imported correctly - I just want to change the scale on which I'm fitting)

$\endgroup$
2
  • $\begingroup$ Are you aware that fitting the curve in log-space will generally yield a different fit? Also, assumptions about distribution of errors (e.g, that they are normally distributed) will often be violated. $\endgroup$ Jan 13, 2016 at 23:01
  • $\begingroup$ Yes, I'm aware of that. That's why I want to do it. (My data has a lot of points near the origin of axis and I'm mostly interested in the points far away). $\endgroup$
    – Caims
    Jan 13, 2016 at 23:03

1 Answer 1

7
$\begingroup$
data = Table[{x, 1 - Gamma[1, 2/x]/Gamma[1] + Random[]/10}, {x, 1, 10, .1}];
res = FindFit[data, 1 - Gamma[A, B/x]/Gamma[A], {A, B}, x];
(* {A -> 0.913848, B -> 2.06033} *)

Show[
  ListLogLogPlot[data],
  LogLogPlot[1 - Gamma[A, B/x]/Gamma[A] /. res // Evaluate, {x, 1, 10}],
  Frame -> True
]

Mathematica graphics

You can fit in log-log space, but then don't forget to scale x in your gamma expression as Exp[x], because you scaled it as Log[x] in the input:

res2 = FindFit[Log[data], Log[1 - Gamma[A, B/Exp[x]]/Gamma[A]], {A, B}, x]
(* {A -> 0.923818, B -> 2.09093} *)

Show[
 ListLogLogPlot[data],
 LogLogPlot[1 - Gamma[A, B/x]/Gamma[A] /. res // Evaluate, {x, 1, 10}],
 LogLogPlot[1 - Gamma[A, B/x]/Gamma[A] /. res2 // Evaluate, {x, 1, 10}, 
  PlotStyle -> {Red, Dashed}],
 Frame -> True
]

Mathematica graphics

The two fits are very similar, but if you inspect the fitted parameters you'll see they differ slightly. As I said in the comments above, fitting in the log domain is not the same as in the linear domain.

$\endgroup$
3
  • $\begingroup$ I don't think that's what I want to do. Doesn't your code fit my curve to a "linear" data and just shows log-log plot of it? (Meant the first one) $\endgroup$
    – Caims
    Jan 13, 2016 at 23:20
  • $\begingroup$ @Caims What about the second one? I wasn't too sure what you actually wanted. $\endgroup$ Jan 13, 2016 at 23:30
  • $\begingroup$ It is perfect, Thank you so much! That's what I wanted. $\endgroup$
    – Caims
    Jan 13, 2016 at 23:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.