# How to do weighted least squares fit with LsqFit.jl

**URL:** <https://discourse.julialang.org/t/how-to-do-weighted-least-squares-fit-with-lsqfit-jl/61136>\
**Category:** General Usage\
**Tags:** curve-fitting\
**Created:** [May 14, 2021, 9:57am UTC](https://discourse.julialang.org/t/how-to-do-weighted-least-squares-fit-with-lsqfit-jl/61136 "2021-05-14T09:57:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [May 14, 2021, 9:57am UTC](https://discourse.julialang.org/t/how-to-do-weighted-least-squares-fit-with-lsqfit-jl/61136/1 "2021-05-14T09:57:43Z")

</div>

I want to preform a weigted least squares fit with LsqFit.jl

The documentation ( [Tutorial · LsqFit.jl (julianlsolvers.github.io)](https://julianlsolvers.github.io/LsqFit.jl/latest/tutorial/#Weighted-Least-Squares-1)) does have a section on it, but no example of `curve_fit()` actually being called with a weight-vector. I have tried the following, with one of the last two lines commented out at a time:

```julia
using LsqFit

model(ys, p) = @. p[1]*ys+p[2]
xdata = 1:100
ydata = xdata + randn(length(xdata))
wt = xdata

# This line throws a method error
curve_fit(R_model, xdata, ydata, [1.0, 1.0], wt=wt)

# This line throws a dimension error, but seems to misunderstand what I want
curve_fit(R_model, xdata, ydata, [1.0, 1.0], wt)

```

I don’t know if I am passing the arguments in the wrong way, but I can not find an example for how to do it. Anyone know how?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [May 14, 2021, 10:15am UTC](https://discourse.julialang.org/t/how-to-do-weighted-least-squares-fit-with-lsqfit-jl/61136/2 "2021-05-14T10:15:42Z")

</div>

Please check one possible solution [in this post](https://discourse.julialang.org/t/weighted-linear-regression-with-confidence-interval-fitted-to-error-bars/60743/6).

---

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [May 14, 2021, 10:54am UTC](https://discourse.julialang.org/t/how-to-do-weighted-least-squares-fit-with-lsqfit-jl/61136/3 "2021-05-14T10:54:15Z")

</div>

And that fixes it! The solution was calling `curve_fit(model, xdata, ydata, wt, p0)`, where I hope the names of the variables indicate what they are interpreted as.
