# How to find and show a fitting line for ListLogLogPlot?

I got a set of data, and the ListLogLogPlot output appears to have a slope of $-1$. Thus, I try to fit a line to show the slope according to this answer.

Note: the IntervalSlider function included in this code is a new thing of V10.0, so you should run this on V10.0 and later.

I try to get a straight line by working out log coordinates for this data:

nn = Length[data]

lgData = Table[{N[Log[n]], Log[data[[n]]]}, {n, nn}];


Using DynamicModule to find the straight line:

DynamicModule[{n1 = 1, n2 = nn, line, x},
Column[{Row[{IntervalSlider[Dynamic[{n1, n2}], {1, nn, 1},
Method -> "Push", ImageSize -> 6 72,
Appearance -> {"Markers", "Labeled"}]}],
Dynamic[line = Fit[lgData[[n1 ;; n2]], {1, x}, x]],
Dynamic[Show[ListLogLogPlot[data, ImageSize -> 6 72],
Plot[line, {x, Log[1], Log[nn]}, PlotStyle -> Red, PlotRange -> All]
]]}]]


Unfortunately, it gives me the following error of a string numbers like this:

Fit[{{0., {-3.3123, 3.40072}},...]

Except for the above main problem, I have three minor issues:

1. I am curious to know that what is the base in ListLogLogPlot? I actually want a base-10 log-log plot, which is given by ListLogLogPlot so I guess the base built-in it is 10 instead of $e$. But in @Hugh's solution, it just includes Log rather than Log10, but appears to give the correct figure, see this question.

2. Is it possible to find the intercepts of the resulting "trendline" accurately?

3. I found that in V9 ListLogLogPlot with GridLines -> Automatic yields a plot with grid line corresponding each scale, however, in v10 it yields a plot only with gird line corresponding main scale, as show in the above figure.

• A single line is clearly not a good description of the data. You should consider two lines using piecewise regression. Can up upload a representative subset of the data?
– JimB
Jan 19 '16 at 3:30
• Hi @ Jim Baldwin, thanks for your comment. Actually, I just want to fit a straight line for the range from near $7\times 10^{-6}$ to near $4\times 10^{-4}$. Do you have any trouble on download the data? Please find here. Jan 19 '16 at 4:14
• I actually had trouble even noticing that the data was available and the link at the beginning of your question took me to a site that looks like a great way to get software installed that I don't want. The link in your comment works fine, however.
– JimB
Jan 19 '16 at 4:15
• Sorry @ Jim Baldwin, kindly check my updated dropbox link. Many thanks! Jan 19 '16 at 4:17

I used just the subset of data "for the range from near $7 \times 10^{-6}$ to $4 \times 10^{-4}$". Then the data was transformed by taking the logs followed by a call to LinearModelFit:

(* Take logs (base 10) *)
data2 = Log10[data];

lm = LinearModelFit[data2, x, x];
estimates = lm["BestFitParameters"]
(* {-2.4226244334755775,-0.9507209972732249} *)
lm["ParameterConfidenceIntervals"]
(* {{-2.4708760402072065,-2.3743728267439486},{-0.9552945803176384, -0.9461474142288114}} *)

(* Show results *)
ListLogLogPlot[{data, Table[{data[[i, 1]], 10^(estimates[[1]] + estimates[[2]] Log10[data[[i, 1]]])},
{i, Length[data[[All, 1]]]}]}, Joined -> {False, True}, Frame -> True]


While the intercept will change depending on whether Log or Log10 is used, the slope will not change. The 95% confidence interval for the slope is (-0.9552945803176384, -0.9461474142288114). So the slope is near -1.

Maybe I missed something in your question but I don't see why you'd want to use Dynamic to essentially fit the curve "by eye" when a linear regression fitting procedure is available.

Also, there still appears to be a considerable lack of fit for the larger values of the independent variable as the line under-predicts for all points greater than $10^{-4}$.

• @ Jim Baldwin, Thanks, may I know what is the meaning of the quantities yielded by estimates = lm["BestFitParameters"]? I guess the first one, i.e. estimates[[1]], is the intercept, and the second one, i.e. estimates[[2]], is the slope. Am I right? Jan 19 '16 at 8:31
• You are correct.
– JimB
Jan 19 '16 at 13:39
• Hi @ Jim Baldwin, just check with you the last issue: is the intercept estimates[[1]] the intersection of the fitting straight line on the vertical line $x=10^0=1$, if Log10 is used. Thanks! Jan 19 '16 at 14:50
• Yes. You are fitting $\log_{10} y= a + b\log_{10} x + \epsilon$ where $\epsilon \sim N(0,\sigma^2)$. So the intercept $a$ is where $\log_{10} x = 0$ or equivalently when $x=1$.
– JimB
Jan 20 '16 at 1:05
• @ Jim Baldwin, thanks for your explanation! Jan 20 '16 at 1:17