# Algorithm to switch from one to two gaussian fit

I have a big dataset that I have to fit with one or two gaussians. This dataset represents the evolution of a population in two different population as a function of time. That means that at same point I need to see the presence of a second gaussian.

Here example of data (for simplicity only two):

data={{{0.520168, 1.35919, 0.*10^-18}, {0.520168, 1.36219,
0.*10^-18}, {0.520168, 1.3652, 0.0384193}, {0.520168, 1.36823,
0.00788955}, {0.520168, 1.37126, 0.00237192}, {0.520168,
1.37432, -0.00804829}, {0.520168, 1.37738, 0.02853}, {0.520168,
1.38046, 0.0449871}, {0.520168, 1.38356, -0.0140081}, {0.520168,
1.38666, -0.0483559}, {0.520168, 1.38979, 0.0144628}, {0.520168,
1.39292, 0.00641026}, {0.520168, 1.39607, 0.00736648}, {0.520168,
1.39923, 0.*10^-18}, {0.520168, 1.40241, 0.00745921}, {0.520168,
1.40561, -0.0597015}, {0.520168, 1.40881, 0.00305011}, {0.520168,
1.41203, 0.0361809}, {0.520168, 1.41527, -0.0175281}, {0.520168,
1.41852, -0.0381526}, {0.520168, 1.42179, 0.0152878}, {0.520168,
1.42507, 0.0422195}, {0.520168, 1.42837, -0.00356506}, {0.520168,
1.43168, 0.0701058}, {0.520168, 1.43501, 0.024228}, {0.520168,
1.43835, -0.0280899}, {0.520168, 1.44171, -0.0004329}, {0.520168,
1.44508, 0.0346964}, {0.520168, 1.44847, -0.00677831}, {0.520168,
1.45188, 0.0013587}, {0.520168, 1.4553, -0.00375799}, {0.520168,
1.45874, -0.0107411}, {0.520168, 1.4622, -0.00323232}, {0.520168,
1.46567, 0.0167939}, {0.520168, 1.46915, -0.00421617}, {0.520168,
1.47266, -0.0179886}, {0.520168, 1.47618, 0.*10^-18}, {0.520168,
1.47972, -0.0228945}, {0.520168, 1.48327, -0.00458866}, {0.520168,
1.48684, 0.0207697}, {0.520168, 1.49043, 0.00723545}, {0.520168,
1.49404, -0.00948407}, {0.520168, 1.49766, 0.0148949}, {0.520168,
1.50131, -0.00513071}, {0.520168, 1.50497, -0.00526466}, {0.520168,
1.50864, -0.000457387}, {0.520168, 1.51234,
0.00574873}, {0.520168, 1.51605, 0.000335711}, {0.520168,
1.51978, -0.000603732}, {0.520168, 1.52353,
0.000369805}, {0.520168, 1.5273, -0.000565078}, {0.520168, 1.53109,
0.0000234659}, {0.520168, 1.5349, -0.00196331}, {0.520168,
1.53872, -0.00143444}, {0.520168, 1.54257, -0.00132528}, {0.520168,
1.54643, -0.00202046}, {0.520168, 1.55031, -0.0018166}, {0.520168,
1.55422, -0.0017395}, {0.520168, 1.55814, -0.00201671}, {0.520168,
1.56208, -0.00192227}, {0.520168,
1.56604, -0.00211416}, {0.520168, 1.57002, -0.00177544}, {0.520168,
1.57403, -0.00215494}, {0.520168,
1.57805, -0.00263811}, {0.520168, 1.58209, -0.00326802}, {0.520168,
1.58616, -0.00501298}, {0.520168,
1.59024, -0.000711156}, {0.520168, 1.59435, 0.00603807}, {0.520168,
1.59848, 0.000240348}, {0.520168, 1.60263,
0.000116092}, {0.520168, 1.6068, 0.000180624}, {0.520168,
1.61099, -0.000102818}, {0.520168, 1.61521,
0.0000918084}, {0.520168, 1.61944, -0.0000660886}, {0.520168,
1.6237, -2.27973*10^-6}, {0.520168, 1.62798,
0.00644817}, {0.520168, 1.63229, -0.000865175}, {0.520168,
1.63661, -0.000535858}, {0.520168, 1.64096,
0.000183894}, {0.520168, 1.64534, 0.000376466}, {0.520168, 1.64973,
0.000748602}, {0.520168, 1.65415, 0.000479742}, {0.520168, 1.6586,
0.0000810521}, {0.520168, 1.66306, 0.0000575546}, {0.520168,
1.66755, -0.000819007}, {0.520168,
1.67207, -0.00147817}, {0.520168, 1.67661, -0.00111427}, {0.520168,
1.68118, -0.000540573}, {0.520168,
1.68577, -0.000269274}, {0.520168,
1.69038, -0.0000462053}, {0.520168,
1.69502, -2.29579*10^-6}, {0.520168,
1.69969, -0.0000265303}, {0.520168,
1.70438, -0.000512523}, {0.520168, 1.7091, -0.00128458}, {0.520168,
1.71384, -0.00132003}, {0.520168,
1.71861, -0.000880777}, {0.520168,
1.72341, -0.0000480267}, {0.520168,
1.72824, -0.0000589702}, {0.520168,
1.73309, -4.57609*10^-6}, {0.520168, 1.73797,
0.000209495}, {0.520168, 1.74287, 0.0000433013}, {0.520168,
1.74781, -0.000250679}, {0.520168,
1.75277, -0.000431359}, {0.520168,
1.75776, -0.00052058}, {0.520168,
1.76278, -0.000377031}, {0.520168, 1.76782, 0.00111511}, {0.520168,
1.7729, -0.000330877}, {0.520168,
1.778, -0.0000600787}, {0.520168,
1.78314, -0.0000428803}, {0.520168, 1.7883,
0.000981561}, {0.520168, 1.7935, -0.000432282}, {0.520168,
1.79872, -0.0000251174}, {0.520168, 1.80398,
0.000335717}, {0.520168, 1.80927, 0.00128558}, {0.520168, 1.81458,
0.00149209}, {0.520168, 1.81993, 0.00193628}, {0.520168, 1.82531,
0.00258719}, {0.520168, 1.83073, 0.00359621}, {0.520168, 1.83617,
0.00380417}, {0.520168, 1.84165, 0.00449736}, {0.520168, 1.84716,
0.00816104}, {0.520168, 1.8527, 0.00983078}, {0.520168, 1.85828,
0.0135281}, {0.520168, 1.86389, 0.0147486}, {0.520168, 1.86953,
0.0170608}, {0.520168, 1.87521, 0.0272148}, {0.520168, 1.88092,
0.0280809}, {0.520168, 1.88667, 0.0423847}, {0.520168, 1.89245,
0.0489285}, {0.520168, 1.89827, 0.0634671}, {0.520168, 1.90412,
0.0723151}, {0.520168, 1.91001, 0.0899392}, {0.520168, 1.91594,
0.0964298}, {0.520168, 1.92191, 0.11216911064559132}, {0.520168,
1.92791, 0.11673583082258436}, {0.520168, 1.93395,
0.129026859677013883}, {0.520168, 1.94002,
0.124465934965584890}, {0.520168, 1.94614,
0.128520008469193314}, {0.520168, 1.95229,
0.11688072912562658}, {0.520168, 1.95848,
0.11348708432740769}, {0.520168, 1.96472, 0.0970145}, {0.520168,
1.97099, 0.0852619}, {0.520168, 1.9773, 0.0669193}, {0.520168,
1.98365, 0.0589419}, {0.520168, 1.99005, 0.0414015}, {0.520168,
1.99648, 0.0324957}, {0.520168, 2.00296, 0.0240941}, {0.520168,
2.00948, 0.0142356}, {0.520168, 2.01604, 0.0106347}, {0.520168,
2.02264, 0.00576612}, {0.520168, 2.02929, 0.000709421}, {0.520168,
2.03598, -0.00472318}, {0.520168, 2.04272, -0.00676576}, {0.520168,
2.0495, -0.00953474}, {0.520168, 2.05633, -0.00973084}, {0.520168,
2.0632, -0.0116964}, {0.520168, 2.07012, -0.0120155}, {0.520168,
2.07708, -0.016141}, {0.520168, 2.08409, -0.0138745}, {0.520168,
2.09115, -0.0171615}, {0.520168, 2.09826, -0.0144657}, {0.520168,
2.10541, -0.0172262}, {0.520168, 2.11262, -0.0146465}, {0.520168,
2.11987, -0.0178844}, {0.520168, 2.12717, -0.0167251}, {0.520168,
2.13453, -0.0158942}, {0.520168, 2.14193, -0.015394}, {0.520168,
2.14939, -0.0170091}, {0.520168, 2.1569, -0.01566}, {0.520168,
2.16446, -0.0140476}, {0.520168, 2.17208, -0.0143257}, {0.520168,
2.17974, -0.0157233}, {0.520168, 2.18747, -0.0143234}, {0.520168,
2.19524, -0.0116745}, {0.520168, 2.20308, -0.0128713}, {0.520168,
2.21097, -0.01387}, {0.520168, 2.21891, -0.0133061}, {0.520168,
2.22692, -0.012921}, {0.520168, 2.23498, -0.0116915}, {0.520168,
2.2431, -0.0114789}, {0.520168, 2.25128, -0.0106662}, {0.520168,
2.25952, -0.0126415}, {0.520168, 2.26781, -0.0113232}, {0.520168,
2.27618, -0.0108931}, {0.520168, 2.2846, -0.00702988}, {0.520168,
2.29308, -0.0107143}, {0.520168, 2.30163, -0.00839169}, {0.520168,
2.31024, -0.00752048}, {0.520168, 2.31892, -0.00700669}, {0.520168,
2.32766, -0.0102079}, {0.520168, 2.33647, -0.00684065}, {0.520168,
2.34535, -0.00655061}, {0.520168,
2.35429, -0.00973321}, {0.520168, 2.3633, -0.00821091}, {0.520168,
2.37238, -0.00585439}, {0.520168, 2.38153, -0.00684852}, {0.520168,
2.39076, -0.00633605}, {0.520168,
2.40005, -0.00831252}, {0.520168, 2.40942, -0.00906764}, {0.520168,
2.41885, -0.00747464}, {0.520168, 2.42837, -0.0062804}, {0.520168,
2.43796, -0.00750697}, {0.520168,
2.44762, -0.00858099}, {0.520168, 2.45736, -0.013027}, {0.520168,
2.46718, -0.00808093}, {0.520168, 2.47708, -0.00713428}, {0.520168,
2.48706, -0.00497512}, {0.520168,
2.49712, -0.00570709}, {0.520168, 2.50726, -0.00794331}, {0.520168,
2.51748, -0.00971922}, {0.520168,
2.52779, -0.00635745}, {0.520168, 2.53818, -0.00142266}, {0.520168,
2.54866, -0.00899665}, {0.520168, 2.55922, -0.0104959}, {0.520168,
2.56988, 0.00522193}, {0.520168, 2.58062, 0.00953246}, {0.520168,
2.59145, 0.00313808}, {0.520168, 2.60237, -0.0321447}, {0.520168,
2.61339, -0.000701016}, {0.520168, 2.6245, 0.0296378}, {0.520168,
2.6357, -0.00376766}, {0.520168, 2.647, 0.00306346}, {0.520168,
2.6584, 0.00354715}, {0.520168, 2.66989, -0.00317797}, {0.520168,
2.68149, -0.00739958}, {0.520168, 2.69319, 0.00991609}, {0.520168,
2.70498, 0.00408789}, {0.520168, 2.71689, -0.0235405}, {0.520168,
2.7289, -0.0115502}, {0.520168, 2.74101, 0.00392157}, {0.520168,
2.75323, 0.00517539}, {0.520168, 2.76557, -0.00880088}, {0.520168,
2.77801, 0.0246609}, {0.520168, 2.79057, 0.0706941}, {0.520168,
2.80324, -0.00863132}, {0.520168, 2.81602, 0.0448878}, {0.520168,
2.82892, 0.0303218}, {0.520168, 2.84195, 0.0110701}, {0.520168,
2.85509, -0.00824873}, {0.520168, 2.86835, 0.0423841}, {0.520168,
2.88174, 0.00428659}, {0.520168, 2.89525, -0.033033}, {0.520168,
2.90889, -0.0134788}, {0.520168, 2.92266, 0.000888889}, {0.520168,
2.93657, -0.0194384}, {0.520168, 2.9506, -0.0108696}, {0.520168,
2.96477, 0.00242424}, {0.520168, 2.97907, -0.0184237}, {0.520168,
2.99352, -0.0231806}, {0.520168, 3.0081, 0.0263158}, {0.520168,
3.02283, -0.0184438}, {0.520168, 3.0377, -0.00772201}, {0.520168,
3.05272, -0.013834}, {0.520168, 3.06789, -0.0318966}, {0.520168,
3.08321, 0.*10^-18}, {0.520168, 3.09869, 0.*10^-18}}, {{1286.61,
1.35919, 0.*10^-18}, {1286.61, 1.36219, 0.*10^-18}, {1286.61,
1.3652, -0.016538}, {1286.61, 1.36823, -0.0794473}, {1286.61,
1.37126, 0.00555813}, {1286.61, 1.37432, 0.0692464}, {1286.61,
1.37738, 0.010968}, {1286.61, 1.38046, 0.0640704}, {1286.61,
1.38356, -0.014153}, {1286.61, 1.38666, -0.0882917}, {1286.61,
1.38979, -0.047047}, {1286.61, 1.39292, -0.0065189}, {1286.61,
1.39607, 0.00360036}, {1286.61, 1.39923, -0.0324786}, {1286.61,
1.40241, 0.000454133}, {1286.61, 1.40561, -0.0305677}, {1286.61,
1.40881, 0.0224041}, {1286.61, 1.41203, -0.00196078}, {1286.61,
1.41527, 0.0164911}, {1286.61, 1.41852, -0.012}, {1286.61, 1.42179,
0.00224719}, {1286.61, 1.42507, -0.0483351}, {1286.61,
1.42837, -0.0158103}, {1286.61, 1.43168,
0.12385919165580182}, {1286.61, 1.43501, -0.0230456}, {1286.61,
1.43835, 0.0331754}, {1286.61, 1.44171, -0.00851064}, {1286.61,
1.44508, -0.00235849}, {1286.61, 1.44847, 0.0116326}, {1286.61,
1.45188, 0.00528402}, {1286.61, 1.4553, 0.00565611}, {1286.61,
1.45874, 0.0679916}, {1286.61, 1.4622, -0.0173982}, {1286.61,
1.46567, -0.0181406}, {1286.61, 1.46915, 0.000758438}, {1286.61,
1.47266, -0.0616706}, {1286.61, 1.47618, 0.00405306}, {1286.61,
1.47972, 0.0162206}, {1286.61, 1.48327, 0.0144832}, {1286.61,
1.48684, 0.025}, {1286.61, 1.49043, 0.0315056}, {1286.61,
1.49404, -0.000377929}, {1286.61, 1.49766, -0.00463149}, {1286.61,
1.50131, -0.00120366}, {1286.61, 1.50497, -0.00311791}, {1286.61,
1.50864, -0.000756315}, {1286.61, 1.51234, 0.00477064}, {1286.61,
1.51605, 0.00591716}, {1286.61, 1.51978, 0.0040393}, {1286.61,
1.52353, -0.00147235}, {1286.61, 1.5273, -0.000629822}, {1286.61,
1.53109, 0.000946992}, {1286.61, 1.5349, -0.000127516}, {1286.61,
1.53872, 0.000138794}, {1286.61, 1.54257, -0.000312179}, {1286.61,
1.54643, 0.000135325}, {1286.61, 1.55031, -0.000159561}, {1286.61,
1.55422, -0.0000478994}, {1286.61,
1.55814, -0.000250611}, {1286.61, 1.56208, -0.000265145}, {1286.61,
1.56604, -0.000121025}, {1286.61, 1.57002, 0.000210043}, {1286.61,
1.57403, 0.000445888}, {1286.61, 1.57805, 0.000555992}, {1286.61,
1.58209, 0.000985255}, {1286.61, 1.58616, 0.00192411}, {1286.61,
1.59024, 0.000405682}, {1286.61, 1.59435, -0.00120466}, {1286.61,
1.59848, -0.000977694}, {1286.61, 1.60263, 0.000181822}, {1286.61,
1.6068, 0.000431955}, {1286.61, 1.61099, -0.0000601533}, {1286.61,
1.61521, 0.000107657}, {1286.61, 1.61944, 0.000245601}, {1286.61,
1.6237, -0.000505463}, {1286.61, 1.62798, 0.0218386}, {1286.61,
1.63229, 0.000889975}, {1286.61, 1.63661, 0.000785872}, {1286.61,
1.64096, 0.000547845}, {1286.61, 1.64534, 0.00018167}, {1286.61,
1.64973, 0.000350225}, {1286.61, 1.65415, 0.000631509}, {1286.61,
1.6586, 0.000900663}, {1286.61, 1.66306, 0.00104316}, {1286.61,
1.66755, 0.0004201}, {1286.61, 1.67207, -0.0000564004}, {1286.61,
1.67661, 0.0000210289}, {1286.61, 1.68118, -0.000296299}, {1286.61,
1.68577, 0.000127354}, {1286.61, 1.69038, 0.000848129}, {1286.61,
1.69502, 0.00165248}, {1286.61, 1.69969, 0.00267759}, {1286.61,
1.70438, 0.00291195}, {1286.61, 1.7091, 0.00303109}, {1286.61,
1.71384, 0.0031616}, {1286.61, 1.71861, 0.00317959}, {1286.61,
1.72341, 0.00352698}, {1286.61, 1.72824, 0.00437499}, {1286.61,
1.73309, 0.00552558}, {1286.61, 1.73797, 0.00677134}, {1286.61,
1.74287, 0.00784083}, {1286.61, 1.74781, 0.00784449}, {1286.61,
1.75277, 0.00821921}, {1286.61, 1.75776, 0.00855089}, {1286.61,
1.76278, 0.00895674}, {1286.61, 1.76782, 0.00898223}, {1286.61,
1.7729, 0.0100537}, {1286.61, 1.778, 0.0107426}, {1286.61, 1.78314,
0.0123753}, {1286.61, 1.7883, 0.0122144}, {1286.61, 1.7935,
0.01428}, {1286.61, 1.79872, 0.0118078}, {1286.61, 1.80398,
0.0141342}, {1286.61, 1.80927, 0.0156003}, {1286.61, 1.81458,
0.0150735}, {1286.61, 1.81993, 0.0158265}, {1286.61, 1.82531,
0.0166757}, {1286.61, 1.83073, 0.0175448}, {1286.61, 1.83617,
0.0160824}, {1286.61, 1.84165, 0.0183359}, {1286.61, 1.84716,
0.0160626}, {1286.61, 1.8527, 0.0180719}, {1286.61, 1.85828,
0.0164347}, {1286.61, 1.86389, 0.0185447}, {1286.61, 1.86953,
0.0178544}, {1286.61, 1.87521, 0.0206399}, {1286.61, 1.88092,
0.0201119}, {1286.61, 1.88667, 0.0186228}, {1286.61, 1.89245,
0.0185113}, {1286.61, 1.89827, 0.0228163}, {1286.61, 1.90412,
0.0205396}, {1286.61, 1.91001, 0.0255566}, {1286.61, 1.91594,
0.0273663}, {1286.61, 1.92191, 0.0275007}, {1286.61, 1.92791,
0.0248642}, {1286.61, 1.93395, 0.0279457}, {1286.61, 1.94002,
0.0269589}, {1286.61, 1.94614, 0.0315162}, {1286.61, 1.95229,
0.0323472}, {1286.61, 1.95848, 0.0344668}, {1286.61, 1.96472,
0.0299849}, {1286.61, 1.97099, 0.031354}, {1286.61, 1.9773,
0.0263369}, {1286.61, 1.98365, 0.0274796}, {1286.61, 1.99005,
0.0204096}, {1286.61, 1.99648, 0.0180417}, {1286.61, 2.00296,
0.0143834}, {1286.61, 2.00948, 0.0143258}, {1286.61, 2.01604,
0.0101685}, {1286.61, 2.02264, 0.00809399}, {1286.61, 2.02929,
0.00804533}, {1286.61, 2.03598, 0.00166004}, {1286.61, 2.04272,
0.00255096}, {1286.61, 2.0495, -0.00218029}, {1286.61,
2.05633, -0.00266964}, {1286.61, 2.0632, -0.00670397}, {1286.61,
2.07012, -0.00486135}, {1286.61, 2.07708, -0.00400138}, {1286.61,
2.08409, -0.00485953}, {1286.61, 2.09115, -0.00658365}, {1286.61,
2.09826, -0.00872127}, {1286.61, 2.10541, -0.00736512}, {1286.61,
2.11262, -0.0101703}, {1286.61, 2.11987, -0.0116224}, {1286.61,
2.12717, -0.00961741}, {1286.61, 2.13453, -0.0102698}, {1286.61,
2.14193, -0.0115959}, {1286.61, 2.14939, -0.00968409}, {1286.61,
2.1569, -0.00886572}, {1286.61, 2.16446, -0.0113369}, {1286.61,
2.17208, -0.00939074}, {1286.61, 2.17974, -0.00726735}, {1286.61,
2.18747, -0.00840575}, {1286.61, 2.19524, -0.00948909}, {1286.61,
2.20308, -0.0102624}, {1286.61, 2.21097, -0.00840522}, {1286.61,
2.21891, -0.00673587}, {1286.61, 2.22692, -0.00792175}, {1286.61,
2.23498, -0.00996085}, {1286.61, 2.2431, -0.00669331}, {1286.61,
2.25128, -0.00872889}, {1286.61, 2.25952, -0.0104449}, {1286.61,
2.26781, -0.00575828}, {1286.61, 2.27618, -0.00511206}, {1286.61,
2.2846, -0.00599705}, {1286.61, 2.29308, -0.00710218}, {1286.61,
2.30163, -0.00596121}, {1286.61, 2.31024, -0.00673509}, {1286.61,
2.31892, -0.00799166}, {1286.61, 2.32766, -0.00770474}, {1286.61,
2.33647, -0.00394187}, {1286.61, 2.34535, -0.00768032}, {1286.61,
2.35429, -0.00608255}, {1286.61, 2.3633, -0.00756352}, {1286.61,
2.37238, -0.00999647}, {1286.61, 2.38153, -0.00764796}, {1286.61,
2.39076, -0.00950526}, {1286.61, 2.40005, -0.0058142}, {1286.61,
2.40942, -0.00601601}, {1286.61, 2.41885, -0.00426625}, {1286.61,
2.42837, -0.00212525}, {1286.61, 2.43796, -0.00283186}, {1286.61,
2.44762, -0.0046397}, {1286.61, 2.45736, -0.00160268}, {1286.61,
2.46718, -0.00480463}, {1286.61, 2.47708, -0.00217239}, {1286.61,
2.48706, -0.00310756}, {1286.61, 2.49712, 0.000249356}, {1286.61,
2.50726, -0.00486186}, {1286.61, 2.51748, 0.000357494}, {1286.61,
2.52779, 0.00284369}, {1286.61, 2.53818, 0.00357188}, {1286.61,
2.54866, -0.00120048}, {1286.61, 2.55922, -0.00697314}, {1286.61,
2.56988, 0.*10^-18}, {1286.61, 2.58062, 0.00615655}, {1286.61,
2.59145, -0.000690608}, {1286.61, 2.60237, -0.00888889}, {1286.61,
2.61339, -0.00135547}, {1286.61, 2.6245, 0.0041841}, {1286.61,
2.6357, 0.00918555}, {1286.61, 2.647, -0.00041511}, {1286.61,
2.6584, -0.00253879}, {1286.61, 2.66989, 0.0252851}, {1286.61,
2.68149, -0.00204918}, {1286.61, 2.69319, -0.0049505}, {1286.61,
2.70498, 0.0206897}, {1286.61, 2.71689, -0.0369369}, {1286.61,
2.7289, -0.0261324}, {1286.61, 2.74101, -0.021519}, {1286.61,
2.75323, 0.0215297}, {1286.61, 2.76557, -0.0154799}, {1286.61,
2.77801, -0.00702165}, {1286.61, 2.79057, -0.00921659}, {1286.61,
2.80324, -0.0123023}, {1286.61, 2.81602, 0.0263473}, {1286.61,
2.82892, -0.00179426}, {1286.61, 2.84195, 0.0562212}, {1286.61,
2.85509, -0.0521092}, {1286.61, 2.86835, -0.0326223}, {1286.61,
2.88174, 0.*10^-18}, {1286.61, 2.89525, 0.00490196}, {1286.61,
2.90889, 0.0215054}, {1286.61, 2.92266, 0.022807}, {1286.61,
2.93657, 0.0049505}, {1286.61, 2.9506, -0.0148402}, {1286.61,
2.96477, 0.00893921}, {1286.61, 2.97907, -0.00506586}, {1286.61,
2.99352, 0.0411568}, {1286.61, 3.0081, -0.023015}, {1286.61,
3.02283, -0.00285225}, {1286.61, 3.0377, -0.00855513}, {1286.61,
3.05272, 0.0398034}, {1286.61, 3.06789, 0.0913082}, {1286.61,
3.08321, 0.*10^-18}, {1286.61, 3.09869, 0.*10^-18}}}


What I want to do (and I am struggling with it) is to write a fitting function such that Mathematica recognises when it is time of fitting with one gaussian and when with two gaussian. I cannot do it manually as the data are collected for 40 minutes, and I have a spectrum every second.

This is what I have at the moment

    gauss[data_] := Table[Module[{model},
model = NonlinearModelFit[data[[1]][[j]][[90 ;; 155]][[All, {2, 3}]], {amp1*Exp[-(x - m1)^2/2 s1^2] + amp2*Exp[-(x - m2)^2/2 s2^2], And @@ Thread[{m2, m1, amp1, amp2, s1, s2} > 0], amp1 <= amp2, m1 <= m2, 1.83 <= m1 <= 1.91, 1.90 <= m2 <= 1.97, s2 <= 30,s1 <= 30}, {{amp1, 0.02}, {amp2, 0.15}, {m1, 1.86}, {s1,20}, {s2, 30}, {m2,data[[1]][[j]][[80 ;; 155]][[Position[data[[1]][[j]][[80 ;; 155]],
Max[data[[1]][[j]][[80 ;; 155]][[All, 3]]]][[1, 1]]]][[2]]}}, x, MaxIterations ->15000];

gaussplot = {Show[ListPlot[data[[1]][[j]][[90 ;; 155]][[All, {2, 3}]], Plot[model[x], {x, 1.65, 2.3}]], ListLinePlot[model["FitResiduals"],model["ParameterTable"],
model["EstimatedVariance"]}],{j, 1, 2}]

result= gauss[data];


Thanks in advance for the help.

• Your code has syntax errors when pasted into Mathematica and an explanation of the structure of the data would be helpful (actually essential). Please revise your question.
– JimB
Jun 18, 2021 at 17:37
• This post is relevant. Jun 18, 2021 at 19:16
• I am imagining the applications to camel recognition software. Jul 14, 2022 at 14:04

This is just an extended comment to pinpoint where I think there needs to be more clarity in the question.

I'm not understanding the structure of the data but a guess from your code is that you have two different time periods and for some reason "know" that you only want elements 90 through 155 to be used. A plot of those two parts of the data results in the following:

ListPlot[data[[1, 90 ;; 155, {2, 3}]], PlotRange -> All]


ListPlot[data[[2, 90 ;; 155, {2, 3}]], PlotRange -> All]


Neither are really of a Gaussian form (either a single Gaussian shape or a sum of two Gaussians shape). I use the term "shape" because you don't have Gaussian probability distributions.

You might want to consider describing what differences the "emerging" population is expected to have: different peaks? different spreads? Some combination of the two? I ask because you need to select a function of the parameters that characterizes the difference. But assuming a Gaussian shape for each (when that doesn't look plausible) can make that function of the parameters meaningless.

I assume the first dataset is an example of what you think of as a "single Gaussian curve" (with the second dataset an example of a "two-Gaussian curve"). Fitting the two models (one-Gaussian and two-Gaussian) to the first dataset results in a better fit for the two-Gaussian model using both the root mean square error and $$AIC_c$$ as criteria.

nlm1 = NonlinearModelFit[data[[1]][[90 ;; 155]][[All, {2, 3}]],
{a + b Exp[-(x - c)^2/d], d > 0 && b > 0 && c > 0},
{{a, 0}, {b, 0.13}, {c, 1.94}, {d, 0.05}}, x];
nlm2 = NonlinearModelFit[data[[1]][[90 ;; 155]][[All, {2, 3}]],
{a + b1 Exp[-(x - c1)^2/d1] + b2  Exp[-(x - c2)^2/d2],
d1 > 0 && b1 > 0 && c1 > 0 && d2 > 0 && b2 > 0 && c2 > 0},
{{a, 0}, {b1, 0.13}, {c1, 1.94}, {d1, 0.05}, {b2, 0.13}, {c2,
1.94}, {d2, 0.05}}, x];
Show[ListPlot[data[[1]][[90 ;; 155]][[All, {2, 3}]]],
Plot[{nlm1[x], nlm2[x]}, {x, 1.69, 2.06},
PlotLegends -> Placed[{"Single Gaussian", "Two Gaussians"}, {Right, Center}]]]


nlm1["AICc"]
(* -566.06 *)
nlm2["AICc"]
(* -618.464 *)
nlm1["EstimatedVariance"]^0.5
(* 0.00315015 *)
nlm2["EstimatedVariance"]^0.5
(* 0.00204657 *)


This is not a criticism of what you want to do. It is just that unaccounted deviations from the proposed models in the data generation process can give results far from reality. Maybe better approach would be to define how the data generation process changes over time.

• Hi Jim, the information i want to get is the peak position as a function of time. I understand your point regarding the gaussian but at this stage i do not really care whether is gaussian or another function but i would like to learn how to instruct mathematica in switching from one fit to another. Thanks for the feedback, it is always good to be critical about data. What function do you think can work better? Jun 19, 2021 at 18:05
• So it would seem that your "data" is really either the height or time of the peak as opposed to all of the data used to construct the peak. As a statistician I do focus on how the data is collected and what you want to do with it (as opposed to just writing a recipe for an analysis). For example, does your decision on when or if there's a change need to happen in real time? Or is the decision make long after the data is collected?
– JimB
Jun 19, 2021 at 21:12
• To make it maybe more clear. I have a system with a certain energy (represented by the peak position of the first peak). In Time, this system evolves in two sub-systems represented by two energies (the peak energy of the two peaks). I want to have a plot of how this energy evolves over time Jun 20, 2021 at 22:19
• Sorry, some one else will need to help you. I think I would need to know the subject matter to fill in the blanks and I don't know the subject matter.
– JimB
Jun 20, 2021 at 22:21