# Weighted Least Square

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 ?

• NonlinearModelFit has a Weights option. Perhaps you could start there. – MarcoB Mar 17 '17 at 19:06
• Is this a question about the Mathematica computer language? If not, it belongs on Cross Validated stack exchange (statistics). – David G. Stork Mar 17 '17 at 19:08
• @DavidG.Stork thanks for the advice. – henry Mar 17 '17 at 19:13
• @MarcoB Thanks for the hint! You can see my first try in the question. – henry Mar 17 '17 at 19:13
• "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. – george2079 Mar 17 '17 at 19:18

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"]


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

sim = Flatten[
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]]}]}]


• @corey979 sorry, xd=data[[All,1]]` (no need for that though, fixed) – george2079 May 2 '18 at 11:58