Is it possible to use polyfit in a way that it takes into account the errorbars?
40 views (last 30 days)
Show older comments
Dear all,
I have a two sets of data that I want to compare. I do so by making a scatterplot of the two and afterwards fit the data with a simple linear regression model. However, for some of the datapoints I assume a large uncertainty that I have already estimated. As far as I understand the polyfit command, it finds a polynomial function that tries to "collect" all the datapoints in the best possible way (in the sense of the smallest squares). I would be interested though in a fit function, that fits best with all the ranges of values I have estimated and not the actual given datapoints. I could not find an answer to that in the documentation and hope someone here can give me a hint?
Thank you very much.
2 Comments
dpb
on 17 Jul 2018
Are these estimated errors symmetric around the measured points? If so, the OLS and other estimate will still be around the mean.
Typically, one uses a weighting function to give points with hither error a lower weight in the fit; what's your objective here--almost sounds like you're trying to bias towards an error rather than fit...
As always, showing the data for context would be helpful.
Answers (2)
John D'Errico
on 16 Dec 2023
Different people mean different things when they say error bars. But you also use the word weights, which is a bit more standard. So I'll assume that you have weights for each point.
However polyfit does not allow weights. Other tools, such as fit from the curve fitting toolbox do allow weights, but you need that toolbox. Or if you have the stats toolbox, you could use fitlm. With slightly more effort, you could also use tools like lsqlin (optimization toolbox), or lscov, but they will require you to create appropriate matrices. If you know enough to create the necessary matrices to use those tools, then even backslash will solve the problem.
0 Comments
Douglas Novaes
on 16 Dec 2023
MATLAB's fit function provides the capability to account for error bar weights in curve fitting. The fit function, combined with the appropriate fitting type ('poly5' for a 5th-degree polynomial, for instance) and the error model ('Weights', specifying the weights), can be used to fit curves while considering the error bar weights.
0 Comments
See Also
Categories
Find more on Analysis of Variance and Covariance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!