# How to fit a function to measurements with error?

**URL:** <https://discourse.julialang.org/t/how-to-fit-a-function-to-measurements-with-error/65369>\
**Category:** Data\
**Tags:** question, curve-fitting, measurements\
**Created:** [July 27, 2021, 1:23pm UTC](https://discourse.julialang.org/t/how-to-fit-a-function-to-measurements-with-error/65369 "2021-07-27T13:23:29Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [July 27, 2021, 2:44pm UTC](https://discourse.julialang.org/t/how-to-fit-a-function-to-measurements-with-error/65369/4 "2021-07-27T14:44:53Z")

</div>

Ah I see - it looks like LsqFit is doing an explicit promotion to float somewhere. Not sure whether one should expect LsqFit to work with Measurements - maybe @pkofod can comment.

What do you expect to happen with the uncertainty in estimation? It looks like measurement error in the covariates (right hand side) of your equation to me, which traditionally in econometrics would just lead to attenuation bias. Or do you have different (known) degrees of uncertainties in different parts of your data set? In that case this discussion here might be of relevance, which shows the use of inverse variance weights in LsqFit:

> [@Weighted linear regression with confidence interval fitted to error bars](https://discourse.julialang.org/t/weighted-linear-regression-with-confidence-interval-fitted-to-error-bars/60743/6):
>
> As indicated in GLM.jl’s doc above, glm does not handle inverse-variance weighting. For this purpose, you may use LsqFit.jl. using DataFrames, LsqFit, Printf, Plots; gr() df = DataFrame(x = [0.0, 0.0669873, 0.25, 0.5, 0.75, 0.933013, 1.0], y = [0.223, 0.291, 0.393, 0.549, 0.73, 0.85, 0.896], u\_y = [0.023, 0.024, 0.027, 0.031, 0.037, 0.041, 0.043]) x, y = df.x, df.y wt = 1 ./ df.u\_y .^2 p0 = [0.5, 0.5] m(x, p) = p[1] .+ p[2] \* x # p: model parameters fit = curve\_fit(m…

---

_[View the full topic](https://discourse.julialang.org/t/how-to-fit-a-function-to-measurements-with-error/65369)._
