# Find best fit function for different data set with errors

I have the following 3 data sets for the same quantity with errors as shown in the figure in different colors. How to find the best fit function of 1) each data set separately 2) combined of the 3 data set. Suppose I find the best fit function of each data set separately; then how to combine the three best-fit functions correctly so that I can have a single fit function. Which method will give the more accurate results; finding one single fit function for all the data sets or finding separately each and then adding them up? How I would check the accuracy.

ListPlot[{{Around[0.2, 0.1], Around[1, 0.1], Around[2, 0.2],
Around[3, 0.3]}, {Around[0.5, 0.1], Around[2.5, 0.1],
Around[3.5, 0.2], Around[3.5, 0.3]}, {Around[0.8, 0.1],
Around[1.5, 0.1], Around[2.8, 0.2], Around[1.8, 0.3],
Around[5, 0.5]}}, Frame -> True]


• There is no general "best fit function". You have to specify a family of valid functions . E.g. you could ask for the best fit linear function in the least square sense. Mar 25, 2022 at 9:41
• @DanielHuber, Can you try this here. Do you mean for each individual case? Can you explain why a general function is not possible? Mar 25, 2022 at 9:51
• To find a fit, either for each dataset, or for all of them together, you would use either LinearModelFit or NonlinearModelFit. However, your questions touch upon basic fitting concepts, rather than their implementation in Mathematica. I recommend that you ask in the statistics forum first. Mar 25, 2022 at 13:50
• If these data are real measurements, you have vital background information. Use this. From the data as shown, my conclusion is: combining them is a waste of time. Mar 25, 2022 at 18:09
• @Romke Bontekoe, The only background information is that it's measurements of the same physical quantity by different experiments. Mar 25, 2022 at 19:55

You could try something like this:

data = {
{Around[0.2, 0.1], Around[1, 0.1],   Around[2, 0.2],   Around[3, 0.3]},
{Around[0.5, 0.1], Around[2.5, 0.1], Around[3.5, 0.2], Around[3.5, 0.3]},
{Around[0.8, 0.1], Around[1.5, 0.1], Around[2.8, 0.2], Around[1.8, 0.3], Around[5, 0.5]}
};

models =
LinearModelFit[
(* extract the central values to fit *)
# /. a_Around :> a["Value"],
(* use a quadratic model for the fits *)
{1, x, x^2}, x,
(* use the inverse of the uncertainties as weights in the fit *)
Weights -> (# /. a_Around :> 1/a["Uncertainty"])
]& /@ data


Show[
ListPlot[data, Frame -> True, Axes -> False],
Plot[Evaluate@Through[models[x]], {x, 0, 5}],
PlotRange -> All
]


• Interesting! The blue and orange look perfect; however, the green is not so well. Anyway, I will further improve this. Further, I want to know is that how to find that these fit functions can also rightly predict the 1 sigma error of each data point. 2nd, how to find a single function out of the three? Mar 25, 2022 at 20:03
• I am trying your suggested method on the following data, but it doesn't work: data1 ={{1.916, 0.352, 0.04}, {2.076, 0.432, 0.019}, {2.25, 0.494, 0.018}, {2.44, 0.448, 0.016}, {2.73, 0.52, 0.015}, {2.976, 0.456, 0.012}, {3.26, 0.538, 0.012}, {3.532, 0.374, 0.011}} . Can you please look at this? Mar 28, 2022 at 17:40
• @SciJewel What do the three numbers mean in each point? Mar 29, 2022 at 16:02
• The first one is the x-axis, 2nd is the data point (Y-axis), and the 3rd one is the error on the data point. Mar 29, 2022 at 19:41
• @SciJewel Try LinearModelFit[data1[[All, ;;2]], {1, x, x^2}, x, Weights -> 1/data1[[All, 3]]] Mar 30, 2022 at 2:01