5

In functions like NonlinearModelFit I need to specify parameters with a symbol name. If I localize these parameters they get cluttered in the output (e.g."ParameterTable"):

{a, b, c, d} = {2, 30, 4, 0};
data = a PDF[NormalDistribution[b, c], Range@100] + 
   RandomReal[{-.01, .01}, 100];

fitFunc[a_, b_, c_, d_, x_] := a PDF[NormalDistribution[b, c], x] + d

fit[data_] := Module[{a, b, c, d, aStart, nlmf},
  aStart = Max@data;
  nlmf = NonlinearModelFit[data, 
    fitFunc[a, b, c, d, x], {{a, aStart}, {b, 30}, {c, 4}, {d, 0}}, x];
  nlmf["ParameterTable"]
  ]

fit@data

How can I avoid outputs like a$18554 in the the ParameterTable without using global symbol names for the parameters a, b, c, d inside NonlinearModelFit?

grbl
  • 1,042
  • 5
  • 14
  • Do you care about the actual symbols used or just their appearance in the table? If the latter, you could use the package I posted here along with Block in place of Module. The parameters will all be renamed to things like TransformedParameter$4 but will still display as the original symbols. – Oleksandr R. Feb 05 '13 at 15:53
  • 1
    Could put the variable names in the input, as fit[data_,vars_] := Module[...] where you then use vars explicitly in the Module. – Daniel Lichtblau Feb 05 '13 at 15:54
  • Have you considered using Formal Symbols? – Mr.Wizard Feb 05 '13 at 16:42
  • 2
    Some reasonable starting values to use (instead of the hard-coded ones) are dStart = Min@data; bStart = Position[data, Max@data] // Flatten // Mean; cStart = Max[#] - Min[#] & @ Flatten[Position[data, Alternatives @@ Select[data, 2 (# - dStart ) >= (Max@data - dStart) &]]] /2; aStart = (Max@data - dStart) cStart; – whuber Feb 05 '13 at 17:03

1 Answers1

1

One brute force way is to convert parameters to strings:

fit[data_] := 
  Module[{a, b, c, d, aStart, nlmf}, aStart = Max@data;
      nlmf = NonlinearModelFit[data, fitFunc[a, b, c, d, x], 
            {{a, aStart}, {b, 30}, {c, 4}, {d, 0}}, x];
       nlmf["ParameterTable"] /. {a -> "a", b -> "b", c -> "c", d -> "d"}
  ]
halmir
  • 15,082
  • 37
  • 53
  • This is not as elegant as I had hoped. But it is a working solution! Maybe I will ask for a more general solution in another question. Thx! – grbl Feb 06 '13 at 15:26