2
$\begingroup$

I need to fit data to a line. This data also includes tolerances.

Now, I was wondering, if there exists a method, to account for those tolerances?

For instance, a data point with a lot of tolerances should not be given the same weight as a data point with small tolerances.

Here is a sample of my data:

x| Data-point (y) | +/- tolerance

1| 120.984 | 0.426248

2| 132.194 | 0.418291

3| 130.413 | 1.260100

4| 127.687 | 0.414457

5| 125.710 | 0.410716

6| 128.188 | 1.431200

I was thinking of applying weighted least squares, but I don't know how to implement it.

data = {{1, 120.984}, {2, 132.194}, {3, 130.413}, {4, 127.687}, {5, 
    125.710}, {6, 128.188}};
tol = {0.426248, 0.418291, 1.260100, 0.414457, 0.410716, 1.431200};
LinearModelFit[data, x, x, Weights -> 1/tol]

I don't know... what do you think of the method ? Is the weight correctly chosen ?

$\endgroup$
9
  • $\begingroup$ NonlinearModelFit has a Weights option. Perhaps you could start there. $\endgroup$
    – MarcoB
    Mar 17, 2017 at 19:06
  • $\begingroup$ Is this a question about the Mathematica computer language? If not, it belongs on Cross Validated stack exchange (statistics). $\endgroup$ Mar 17, 2017 at 19:08
  • $\begingroup$ @DavidG.Stork thanks for the advice. $\endgroup$
    – henry
    Mar 17, 2017 at 19:13
  • $\begingroup$ @MarcoB Thanks for the hint! You can see my first try in the question. $\endgroup$
    – henry
    Mar 17, 2017 at 19:13
  • $\begingroup$ "what is the appropriate weight" really is a statistics question. My guess is since your tolerances are fairly similar what you have done is pretty good, but no doubt there is a formally correct approach. $\endgroup$
    – george2079
    Mar 17, 2017 at 19:18

1 Answer 1

3
$\begingroup$

a non-statistician approach:

Example data:

data={{0, 9.90427}, {1, 8.63647}, {2, 8.38828}, {3, 12.1254}, {4,7.83307}, {5, 9.91714}, {6, 16.2134}, {7, 22.6182}, {8,26.1951}, {9, 28.5855}, {10, 30.2941}}
tol={3.04485, 6.88185, 8.32968, 6.69215, 8.60092, 9.38202, 2.39801,1.0914, 2.9695, 1.14305, 0.634177}
Needs["ErrorBarPlots`"]
ErrorListPlot[MapThread[{#1, ErrorBar[#2]} &, {data, tol}]]

enter image description here

simulate additional data based on the variance at each point, and fit to that:

sim = Flatten[
   MapThread[ 
    Function[{a}, {#1[[1]], a}] /@ 
      RandomVariate[NormalDistribution[#1[[2]], #2 ], 200] & , {data, 
     tol}], 1];
fit = LinearModelFit[sim, x, x]

4.41422 +2.42061 x

the OP's approach for comparison:

fit2 = LinearModelFit[data, x, x, Weights -> 1/tol]

5.32159 + 2.46208 x

Show[{ListPlot[sim, PlotStyle -> {Opacity[.25], Gray}], 
  ListPlot[data, PlotStyle -> Red], Plot[{fit[x],fit2[x]},{x, 0, data[[-1,1]]}]}]

enter image description here

$\endgroup$
1
  • $\begingroup$ @corey979 sorry, xd=data[[All,1]] (no need for that though, fixed) $\endgroup$
    – george2079
    May 2, 2018 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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