11
$\begingroup$

Since ErrorListPlot has been superseded by new functionality in ListPlot for Mathematica 12.0, what is the best way to handle errors efficiently? Previously I would simply format my data in a table as

    Data = 
    Table[
            Stuf to calculate values and errors
            {{xValue, yValue, yError}},
            {i, 1, Stop}
         ]

Which made it easy to stick into ErrorListPlot or use in a fit as

NonlinearModel[Data[[1;;,{1, 2}]], Function, Weights->1/Data[[1;;,3]]^2 ]

Now it seems you have to use Around[yValue, yError]

        Data = 
        Table[
                Stuf to calculate values and errors
                {{xValue, Around[yValue, yError]}},
                {i, 1, Stop}
             ]

Which works fine in ListPlot and I can fit data with it, as in it produces a fit result, but I can't figure out how to use Weights with Around and I can't plot the result of the fit in the usual way. Can anyone recommend an efficient way to format data in line with the new updates?

$\endgroup$
  • 1
    $\begingroup$ maybe NonlinearModelFit[data /. a_Around:>a["Value"], model, parameters, vars, Weights->(1/(data[[All,2]] /. a_Around:>a["Uncertainty"])^2)]? $\endgroup$ – kglr Aug 17 at 18:44
  • $\begingroup$ That'll do it! Thanks! $\endgroup$ – Q.P. Aug 17 at 19:26
  • $\begingroup$ QuantumPenguin, posted the comment as an answer. $\endgroup$ – kglr Aug 17 at 19:42
  • 1
    $\begingroup$ Don't forget to use "MeanPredictionBands" after fitting which gives you the result of the fit with the measurement errors. That is the "error" display that would be of most interest to the user of your analysis. $\endgroup$ – JimB Aug 17 at 22:04
  • $\begingroup$ Thanks for the advice JimB. $\endgroup$ – Q.P. Aug 17 at 22:15
10
$\begingroup$

You can extract the properties "Value" and "Uncertainty" from Around objects:

NonlinearModelFit[data /. a_Around :> a["Value"], 
 model, parameters, vars, 
 Weights -> (1/(data[[All,2]] /. a_Around :> a["Uncertainty"])^2)]
$\endgroup$
  • $\begingroup$ Thanks for this nice answer, it's poorly documented on how to use errors in version 12 so this is a great help. $\endgroup$ – Q.P. Aug 17 at 19:44
  • 2
    $\begingroup$ Don't forget to set the VarianceEstimatorFunction appropriately. My favorite is VarianceEstimatorFunction->(1&) but opinions differ. $\endgroup$ – Roman Aug 18 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.