Function Repository Resource:

FitWithErrors

Source Notebook

Perform linear and nonlinear fits on data with error bars

Contributed by: Sjoerd Smit

ResourceFunction["FitWithErrors"][LinearModelFit[data,basis,vars]]

performs a LinearModelFit on data with Around values by finding the appropriate Weights option value based on the uncertainties in data.

ResourceFunction["FitWithErrors"][NonlinearModelFit[data,form,params,vars]]

performs a NonlinearModelFit on data with Around values.

ResourceFunction["FitWithErrors"][expr,n]

limits the algorithm to n iterations to find the weights.

Details

ResourceFunction["FitWithErrors"] is HoldFirst and inspects the given fit expression to find the correct weights.
The data is expected to be a matrix or vector input.
Only symmetric Around values are supported.
Uncertainties in the response variable of the fit are assumed to be Gaussian. The weights are computed as the inverse of the variance. Because of this, all rows of the data need to have non-zero variance.
Uncertainties in the predictor variables are accounted for using the method of Fasano & Vio (see references). This method propagates the uncertainties through the model to obtain the corresponding uncertainties in the responses, which are then converted to weights. The procedure is iterated several times. By default, the number of iterations is limited to 20.

Examples

Basic Examples (2) 

Fit a linear model to some data with errors in the response variable:

In[1]:=
data = {{1, Around[1, 1]}, {2, Around[0, 2]}, {3, Around[2, 0.5]}, {5,
     Around[4, 3]}};
lm = ResourceFunction["FitWithErrors"][
  LinearModelFit[data, x, x]
  ]
Out[2]=
In[3]:=
Show[
 Plot[lm[x], {x, 0, 6}],
 ListPlot[data]
 ]
Out[3]=

Fit data with uncertainties in both x and y:

In[4]:=
data = {{Around[1, 0.1], Around[1, 1]}, {Around[2, 1], Around[0, 2]}, {Around[3, 1], Around[2, 0.5]}, {Around[5, 1.5], Around[4, 3]}};
lm = ResourceFunction["FitWithErrors"][
  LinearModelFit[data, x, x]
  ]
Out[5]=
In[6]:=
Show[
 Plot[lm[x], {x, 0, 6}],
 ListPlot[data]
 ]
Out[6]=

Scope (1) 

Fit a NonlinearModelFit with error bars:

In[7]:=
data = {{Around[0., 0.8], Around[1., 0.8]}, {Around[1., 0.70], Around[0., 0.7]}, {Around[3., 0.4], Around[2., 0.4]}, {Around[5., 0.7], Around[4., 0.7]}, {Around[6., 0.5], Around[4., 0.5]}, {Around[7., 0.5], Around[5., 0.5]}};
nlm = ResourceFunction["FitWithErrors"] @ NonlinearModelFit[data, Log[a + b x^2], {a, b}, x]
Out[8]=
In[9]:=
Show[ListPlot[data], Plot[nlm[x], {x, 0, 7}]]
Out[9]=

Possible Issues (3) 

If one of the rows of the input data does not have any uncertainty, it's not possible to calculate the weights for that row and the fit will fail:

In[10]:=
ResourceFunction["FitWithErrors"][
 LinearModelFit[{{1, Around[1, 1]}, {2, 0}, {3, Around[2, 0.5]}, {5, Around[4, 3]}}, x, x]
 ]
Out[10]=

Mixing x errors with y errors does work because it's still possible to compute a weight for each row:

In[11]:=
ResourceFunction["FitWithErrors"][
 LinearModelFit[{{1, Around[1, 1]}, {Around[2, 1], 0}, {3, Around[2, 0.5]}, {5, Around[4, 3]}}, x, x]
 ]
Out[11]=

Only vector and matrix inputs are currently supported:

In[12]:=
ResourceFunction["FitWithErrors"][
 LinearModelFit[{1 -> Around[1, 1], 2 -> Around[0, 2], 3 -> Around[2, 0.5], 5 -> Around[4, 3]}, {1, x}, x]
 ]
In[13]:=
Failure["ConfirmationFailed", <|"MessageTemplate" -> "Assertion `` failed.", "MessageParameters" -> {
HoldForm[
MatrixQ[data$17200]]}, "ConfirmationType" -> "ConfirmAssert", "HeldTest" -> Hold[
MatrixQ[data$17200]], "Test" :> False, "Information" :> "Only vector and matrix inputs are supported"|>]

Non-symmetric Around values are not supported:

In[14]:=
ResourceFunction["FitWithErrors"][
 LinearModelFit[{{1, Around[1, {1, 2}]}, {2, Around[0, 2]}, {3, Around[2, 0.5]}, {5, Around[4, 3]}}, x, x]
 ]
Out[14]=

Publisher

Sjoerd Smit

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.0.2 – 21 February 2024
  • 1.0.1 – 04 October 2023
  • 1.0.0 – 02 October 2023

Source Metadata

Related Resources

License Information