# Increasing the accuracy of the fit when using LinearModelFit

Question: is it possible to increase the accuracy of the BestFit property for LinearModelFit?

I have code that uses LinearModelFit and the BestFit property to return the best fitting slope of a list of data. But, a peculiar result I've ran into is very slightly negative slope as a result for data where the slope is 0.

Example:

{{Log, Log}, {Log, Log}, {Log, Log},{Log, Log}, {Log, Log}}


produces

7.411988753*10^-21


for the slope, even though we can see that the exact slope is 0.

Here is the context:

For[j = 1, j <= Length[pointsList], j++,
measureData = Map[{Log[1/#], Log[BoxCount[pointsList[[j]], #]]} &, scaleList];
line = LinearModelFit[measureData, x, x];
AppendTo[dimensionList, Coefficient[line["BestFit"], x, 1]]];


pointsList is just a list of lists of points; scaleList = {1/10, 1/20, 1/30, 1/40, 1/50}. The example I shared was one list from pointsList and its corresponding element in dimensionList.

So, is there a way to increase the accuracy of linear model fit? Alternatively, I could emplace an If expression to solve the best fitting slopes I can solve algebraicly, but I was hoping this wasn't necessary.

• add the option ...,WorkingPrecision->100] to LinearModelFit[...] to have a 100 digits working precision. – andre314 Jul 8 '17 at 16:37
• try also ...WorkingPrecision->Infinity] to have exact arithmetic – andre314 Jul 8 '17 at 16:38
• This didn't seem to make the guess for the slope more correct, just closer to the intended guess. Using infinity left me with a large expression saturated with logs instead of a slope of 0. – Anthony Monterrosa Jul 8 '17 at 19:43
• What is BoxCount? – m_goldberg Jul 8 '17 at 20:48
• Try Coefficient[line["BestFit"] // Chop – m_goldberg Jul 8 '17 at 20:51

The concern raised in the OP makes no effective difference. The range of the independent variable is Log - Log, so the difference in the predicted value of the dependent variable is at most

(7.411988753*10^-21 (Log - Log)


When added to Log it makes no difference (literally zero) at machine precision, due to the way floating-point numbers work:

(7.411988753*10^-21 (Log - Log) + Log) - Log
(*  0.  *)


The nonzero value of the slope is most likely due to rounding or truncation error in the numerical procedure used to estimate the "best" fit. But this error is negligible in this case.

Note: This is merely an explanation of the numerics of the problem, which seemed to be the basis for the issue raised. More important issues related to how to model -- some raised in the comments -- are nearly impossible to address, since the data provided seemed manufactured simply to illustrate the error in the slope.