Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
Perhaps the StepMonitor or EvaluationMonitor options, and calculate the error in the function you associate with it, printing/sowing/monitoring as needed. –  rasher Jan 28 at 10:29

2 Answers 2

up vote 7 down vote accepted

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]
share|improve this answer

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

share|improve this answer
    
+1 for the neat idea expansion! –  rasher Jan 28 at 11:18
    
@rasher We were working in parallel :) +1 to you too –  belisarius Jan 28 at 11:19
    
Thanks a lot Rasher and Belisarius! I really appreciate your help. –  Frits Jan 28 at 13:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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