# How do I use LsqFit to fit points with an associated error?

**URL:** <https://discourse.julialang.org/t/how-do-i-use-lsqfit-to-fit-points-with-an-associated-error/88291>\
**Category:** General Usage\
**Tags:** package, curve-fitting, lsqfit\
**Created:** [October 5, 2022, 1:24pm UTC](https://discourse.julialang.org/t/how-do-i-use-lsqfit-to-fit-points-with-an-associated-error/88291 "2022-10-05T13:24:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ultrapoci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ultrapoci/32/25356_2.png) [@ultrapoci](https://discourse.julialang.org/u/ultrapoci)\
**Post date:** [October 5, 2022, 1:24pm UTC](https://discourse.julialang.org/t/how-do-i-use-lsqfit-to-fit-points-with-an-associated-error/88291/1 "2022-10-05T13:24:22Z")

</div>

I have these points I want to fit:

```julia
x y err
24.0 0.0386341 4.29118e-5
24.33 0.03952 2.10541e-5
24.66 0.0398329 1.76137e-5
25.0 0.0399503 1.75643e-5
25.33 0.0399654 1.63191e-5
25.66 0.0398724 1.72015e-5
26.0 0.0397342 2.06237e-5

```

I’m using LsqFit.jl to do this. The problem is that I’m not sure how to use the errors. I think I have to give a weight vector to the `curve_fit` function, but I get crazy results when I try this. For example, fitting this data _without weights_, only using the y column, with the model

```julia
(x, p) -> p[2] .+ p[3] .* (x .- p[1]) .^ 2 + p[4] .* (x .- p[1]) .^ 3 + p[5] .* (x .- p[1]) .^ 4

```

yields these parameters:

```julia
25.111393704165895
 0.039954617396037284
-0.00027948006042561755
 0.0003111490269699087
-0.00035643137150145855

```

with these errors (which are provided by LsqFit’s `stderror` function)

```julia
0.0869412489407782
2.055454237390897e-5
0.00016140094273954191
0.00019062903506274383
0.00010671690424624444

```

I then tried to build a weight vector. My idea was to give the maximum weight to the point with the least error, and viceversa. So, I took the inverse squared of the `err` column above, and I normalized the result by dividing each number by the maximum of the vector. I got:

```julia
0.14462239237947602
0.6007810802559573
0.8584025972308643
0.8632298289063326
1.0
0.9000334378603128
0.6261211360104819

```

Using this vector as weights in `curve_fit`, yielded these model parameters:

```julia
25.120191638543336
 0.03995798049346908
-0.00030331529662780605
 0.0002925450095476317
-0.0003231972927598868

```

which are close enough, but the associated errors for these parameters are nonsensical:

```julia
 3146.8126470564166
    0.7860552955285658
    6.117611017553491
    6.862314788647446
    4.823917655489848

```

Clearly, I’m using weights in the wrong way. What should I do?

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [October 5, 2022, 8:00pm UTC](https://discourse.julialang.org/t/how-do-i-use-lsqfit-to-fit-points-with-an-associated-error/88291/2 "2022-10-05T20:00:25Z")

</div>

what is `err` in your first table?

---

<div class="post-metadata">

**Author:** ![ultrapoci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ultrapoci/32/25356_2.png) [@ultrapoci](https://discourse.julialang.org/u/ultrapoci)\
**Post date:** [October 5, 2022, 8:32pm UTC](https://discourse.julialang.org/t/how-do-i-use-lsqfit-to-fit-points-with-an-associated-error/88291/3 "2022-10-05T20:32:07Z")

</div>

Nevermind, I found [this post](https://discourse.julialang.org/t/weighted-linear-regression-with-confidence-interval-fitted-to-error-bars/60743/6) that explains how to do it. Basically, I don’t have to normalize the vector of weights. I simply take the inverse squared of each measurement error.
