2
$\begingroup$

I use the following code to fit some data given in a previous question of mine:

data = Import["https://pastebin.com/raw/gHdYLCbA", "Table"]

fit = Piecewise[{{a1 Sqrt[x], x < 10^4}, {(
     a3/x Log[1 + a4/x + a5 x]*a2 x^0.45)/(
     a3/x Log[1 + a4/x + a5 x] + a2 x^0.45), 10^4 < x < 2*10^6}}];
model = NonlinearModelFit[
  Transpose[{data[[All, 1]]*10^6, data[[All, 2]]*19.32}], {fit, 
   a4 > 0 && a5 > 0}, {a1, a2, a3, a4, a5}, x, Method -> "NMinimize", 
  MaxIterations -> 1000]

Show[ListLogLogPlot[
  Transpose[{data[[All, 1]]*10^6, data[[All, 2]]*19.32}], 
  PlotRange -> All], 
 LogLogPlot[model[x], {x, 100, 2000000}, PlotRange -> All, 
  PlotStyle -> {Red, Thick}]]

The result is

enter image description here

How can I improve the fit?

$\endgroup$
1
  • 3
    $\begingroup$ You probably want to rescale your data back to values on the order of 1 before you fit. Nonlinear models are difficult to fit when the values are all over the place. Furthermore, the fact that you're plotting the data in log-log space indicates that you should probably not fit y as a function of x, but Log[y] as a function of Log[x]. If you fit y[x], then the fit will only be sensitive to the large values of y, which is precisely what you're seeing here. $\endgroup$ Commented Aug 3, 2018 at 12:52

1 Answer 1

5
$\begingroup$

You can use Quantile Regression. Below is code that uses the (recently proclaimed) package MonadicQuantileRegression.m.

I specified a special list of B-spline basis knots in order to avoid aliasing effects. The relative errors plot shows a good fit. Then regression function is then taken out of the monad and its piecewise form is simplified.

Import["https://raw.githubusercontent.com/antononcube/\
MathematicaForPrediction/master/MonadicProgramming/\
MonadicQuantileRegression.m"]

enter image description here

data = Import["https://pastebin.com/raw/gHdYLCbA", "Table"];

p =
  QRMonUnit[data]⟹
   QRMonEchoDataSummary⟹
   QRMonQuantileRegression[Join[Range[0, 1, 0.1], Range[1.1, 1.1 Max[data[[All, 1]]], 0.3]],0.5]⟹
   QRMonPlot⟹
   QRMonErrorPlots;

enter image description here

fitFunc = First[p⟹QRMonTakeRegressionFunctions][x];
PiecewiseExpand[fitFunc]

enter image description here

$\endgroup$

Your Answer

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

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