3
$\begingroup$

Since fitting procedures using NonlinearModelFit (or FindFit) sometimes take very long, I would like to monitor the sum of squared errors during the process (seeing it afterwards is well documented). Does someone know how to do this?

$\endgroup$
  • $\begingroup$ Perhaps the StepMonitor or EvaluationMonitor options, and calculate the error in the function you associate with it, printing/sowing/monitoring as needed. $\endgroup$ – ciao Jan 28 '14 at 10:29
8
$\begingroup$

Here's a trivial example of the method in my comment. I've used total absolute difference for error (you can use whatever you please), and I put in a Pause so you can observe the effect for this trivial problem that would be blink-of-an-eye fast. In reality, you'd want to use UpdateInterval or equivalent, or Sow if you want the "history" post-run. Doing anything like this will of course increase run-times I'm sure I need not say.

data = {{0, 1}, {1, 0}, {3, 2}, {5, 4}, {6, 4}, {7, 5}};

Monitor[nlm = NonlinearModelFit[data, Log[a + b x^2], {a, b}, x,
   StepMonitor :> (mon = 
      Abs[Log[a + b #^2] & /@ data[[All, 1]] - data[[All, 2]] // 
        Total]; Pause[.5])], mon]
$\endgroup$
8
$\begingroup$

Expanding a little over rasher's answer:

data = Table[{x, Log[3.5 + 2.5 x^2] + RandomReal[{-1, 1}]}, {x, 0, 10}];
r = {}; s = {}; u = {};
Dynamic[
 GraphicsGrid[{{
    Plot[Log[r[[-1]] + s[[-1]] x^2], {x, Min@data[[All, 1]], Max@data[[All, 1]]}, 
         PlotLabel -> "Fitting", Frame -> True, 
         Epilog :> {Red, PointSize[Medium], Point[data]}, PlotRangePadding -> 2],
    ListLinePlot[{r, s}, PlotLegends -> Placed[{"a", "b"}, Above], 
                 Frame -> True, PlotRangePadding -> 1, AxesOrigin -> {0, 0}], 
    ListLogPlot[u, Joined -> True, PlotLabel -> "Absolute error", 
                Frame -> True]}}, Spacings -> 100]]
nlm = NonlinearModelFit[data, Log[a + b x^2], {a, b}, x, 
      StepMonitor :> (AppendTo[r, a]; AppendTo[s, b];
                     AppendTo[u, Abs[Log[a + b #^2] & /@ data[[All, 1]] - data[[All, 2]] // 
                     Total]]; Pause[1])]

Mathematica graphics

$\endgroup$
  • $\begingroup$ @rasher We were working in parallel :) +1 to you too $\endgroup$ – Dr. belisarius Jan 28 '14 at 11:19
  • $\begingroup$ Thanks a lot Rasher and Belisarius! I really appreciate your help. $\endgroup$ – Frits Jan 28 '14 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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