How to do weighted least squares fit with LsqFit.jl

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

The documentation ( Tutorial · LsqFit.jl (julianlsolvers.github.io)) 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:

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?

Please check one possible solution in this post.

1 Like

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.

1 Like