I have over 1000 spectra I would like to batch fit and then use the results for further study.
I'm not fitting anything especially complicated just a Lorentzian of the form, $$y(x) = A \frac{\gamma^{2} /2}{(x - x_{0})^{2} + \gamma^{2}/4} + y_{0}.$$
The problem I have is that some of my spectra fit quite happily and with good/reasonable results. However some spectra despite looking very similar and the respective fit being initialised in a reasonable way produce very odd fit results. Here is the "Good" data:
GoodFitData = {{29.642582062500000000000,
4.3190002032711183*10^-7}, {29.6425820937500000000000,
5.3406344114652258*10^-7}, {29.642582125000000000000,
4.3050696173846918*10^-7}, {29.642582156250000000000,
3.8476329392273719*10^-7}, {29.642582187500000000000,
3.0946371958194098*10^-7}, {29.642582218750000000000,
3.3232729264197710*10^-7}, {29.642582250000000000000,
6.3905435090395855*10^-7}, {29.6425822812500000000000,
8.753370135668026*10^-7}, {29.642582312500000000000,
8.3216510981331772*10^-7}, {29.642582343750000000000,
5.6798059206608427*10^-7}, {29.642582375000000000000,
3.2057831588605133*10^-7}, {29.642582406250000000000,
6.0006018141506731*10^-7}, {29.642582437500000000000,
5.9220334443374970*10^-7}, {29.642582468750000000000,
5.9043428760173520*10^-7}, {29.6425825000000000000000,
5.3142937509568503*10^-7}, {29.6425825312500000000000,
1.1512362985808565*10^-6}, {29.642582562500000000000,
2.2005730090913985*10^-6}, {29.642582593750000000000,
1.3061501910622896*10^-6}, {29.642582625000000000000,
7.0306201913894104*10^-7}, {29.642582656250000000000,
7.5940811728371987*10^-7}, {29.642582687500000000000,
8.896327027735458*10^-7}, {29.6425827187500000000000,
5.1906915196157101*10^-7}, {29.642582750000000000000,
5.0793870783619110*10^-7}, {29.642582781250000000000,
3.9490581635532259*10^-7}, {29.642582812500000000000,
3.2285197574563654*10^-7}, {29.642582843750000000000,
5.0979050094687087*10^-7}, {29.642582875000000000000,
5.7917798046612257*10^-7}, {29.642582906250000000000,
2.9746088835309100*10^-7}, {29.642582937500000000000,
2.8446466006038211*10^-7}, {29.642582968750000000000,
5.1303286544108108*10^-7}, {29.642583000000000000000,
4.4818646841234343*10^-7}, {29.6425830312500000000000,
3.3482541855830208*10^-7}, {29.6425830625000000000000,
2.2472479916643503*10^-7}}
And the "Bad" data:
BadFitData = {{29.642582375000000000000,
1.4966773070074960*10^-7}, {29.6425823906250000000000,
3.2796187771352530*10^-7}, {29.642582406250000000000,
3.3407923869075059*10^-7}, {29.6425824218750000000000,
1.4881722372170584*10^-7}, {29.642582437500000000000,
1.5636813428753172*10^-7}, {29.642582453125000000000,
4.1457809704998334*10^-7}, {29.642582468750000000000,
5.1785323732788303*10^-7}, {29.642582484375000000000,
4.0880319580912151*10^-7}, {29.6425825000000000000000,
2.4201807866572215*10^-7}, {29.6425825156250000000000,
2.8251493555106467*10^-7}, {29.6425825312500000000000,
4.8387544256366870*10^-7}, {29.642582546875000000000,
5.5329474183042744*10^-7}, {29.642582562500000000000,
6.5744418237327978*10^-7}, {29.642582578125000000000,
7.9552641010675053*10^-7}, {29.642582593750000000000,
4.4648275313948193*10^-7}, {29.642582609375000000000,
5.9516896108447851*10^-7}, {29.642582625000000000000,
2.4332278859121748*10^-6}, {29.642582640625000000000,
1.9634656151700146*10^-6}, {29.642582656250000000000,
1.7604070459744035*10^-7}, {29.642582671875000000000,
2.2592814520073882*10^-7}, {29.642582687500000000000,
2.6774361092830216*10^-7}, {29.6425827031250000000000,
3.3119779204170161*10^-7}, {29.6425827187500000000000,
5.1003549704521799*10^-7}, {29.6425827343750000000000,
2.7701066539193851*10^-7}, {29.642582750000000000000,
2.5924933518664618*10^-7}, {29.642582765625000000000,
4.6290702798493546*10^-7}, {29.642582781250000000000,
4.5979200634303145*10^-7}, {29.642582796875000000000,
6.4157516355120082*10^-7}, {29.642582812500000000000,
4.2154540563855831*10^-7}, {29.642582828125000000000,
2.3994878809517319*10^-7}, {29.642582843750000000000,
4.8672163908576681*10^-7}, {29.642582859375000000000,
5.0605896869103541*10^-7}, {29.642582875000000000000,
4.4161357956856321*10^-7}}
My fit code is:
LorentzFunction[A_,\[Gamma]_,y0_,\[Nu]0_,\[Nu]_] = A (\[Gamma]^2/2)/((\[Nu] - \[Nu]0)^2 + \[Gamma]^2/4) + y0;
NonlinearModelFit[
CutPeakData,
LorentzFunction[A,\[Gamma],y0,\[Nu]0,\[Nu]],
{{A,PeakSignal},{\[Gamma],2*10^-8},{\[Nu]0,PeakFrequency},{y0,5*10^-7}},\[Nu],
MaxIterations->1000
];
I initialise the fit parameters by extracting the peak $y$ and $x$ values for A
and \[Nu]0
respectively. For \[Gamma]
I set 2*10^-8
and y0
I take the mean of the signal floor both sides of the peak.
I've tried various approaches such as the ones described here -- which is a really excellent guide and has worked for me with other data sets (exponential fits) -- and playing with the number of MaxIterations
. None seem to help the BadFitData
despite the data itself not looking that bad, at least by eye -- thoughts?
NonlinearModelFit
or Mathematica issue. SAS, R, SPSS, etc. all have similar issues. It's just the nature of iterative fitting AND users not following guidelines as suggested in @Feyre's answer. $\endgroup$