# How can I do a “linear” fit on a log log plot

I have plotted some data with a log scale on both axes as shown in the image below. I want to be able to fit this data such that the fit appears as linear on this log-log plot but I haven't been able to get the fit to work.

I have tried both fitting the original data without the log scaling and then converting the fit into a log scale but this generated an incorrect fit. What is the best way of doing this?

• Make a linear fit on Log[data]? – Henrik Schumacher Aug 31 '18 at 16:59
• Would you be so kind as to share the actual Mathematica code you are having trouble with? Have you tried regressing Log[Intensity] on Log[Laser Power] and a constant? – yosimitsu kodanuri Aug 31 '18 at 17:05

Contrary to my former comment, if one has a reason to expect a power law directly between the observed quantities, fitting a nonlinear model is a better idea since otherwise the random distribution of the error gets distorted.

The following produces x-y pairs with a power law with a normally distributed error on the y-coordinates.

n = 1000;
x = Subdivide[2., 4., n - 1];
b = RandomReal[{1, 4}];
a = RandomReal[{1, 20}];
c = RandomReal[{0, 20}];
y = a x^b + c + RandomVariate[NormalDistribution[0, 15], n]


Desing a model and with the parameters to the data with FindFit.

model = α t^β + γ;
fit = t \[Function] Evaluate[ model /. FindFit[Transpose[{x, y}], model, {α, β, γ}, t]];


Plotting in log-log scales:

Show[
ListLinePlot[Transpose[{x, y}], ScalingFunctions -> {"Log", "Log"}],
Plot[fit[t], {t, Min[x], Max[x]}, PlotStyle -> Red,
ScalingFunctions -> {"Log", "Log"}]
]


• +1 Thanks for pointing out that the error structure is an essential part of the model, too. – JimB Aug 31 '18 at 17:43
• @JimB Always at you service. =) – Henrik Schumacher Aug 31 '18 at 18:00

If Log10[10^x] gives a line in a LogLog plot, then using it as Linear fit should work. Example:

datay = {500, 1000, 3000, 5000, 10000};
datax = {50, 100, 300, 500, 1000};
ListLogLogPlot[Transpose@{datax, datay}, Joined -> True]
lm = LinearModelFit[Log10[datay], Log10[10^x], x]
ListLogLogPlot[Transpose@{Table[lm[x], {x, datax}], datax},
Joined -> True]