# Is it possible to monitor the sum of squared errors in a NonlinearModelFit or FindFit procedure during the process?

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?

• Perhaps the StepMonitor or EvaluationMonitor options, and calculate the error in the function you associate with it, printing/sowing/monitoring as needed.
– ciao
Commented Jan 28, 2014 at 10:29

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]


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])]


• @rasher We were working in parallel :) +1 to you too Commented Jan 28, 2014 at 11:19
• Thanks a lot Rasher and Belisarius! I really appreciate your help. Commented Jan 28, 2014 at 13:01