This post Estimate error on slope of linear regression given data with associated uncertainty gives the function for the linear regression in the case where both x- and y- data are affected by errors.

I wonder if someone known/has a function that does the same but for the case of no-constant term (similar to the option IncludeConstantBasis -> False in LinearModelFit).


Edit: Random Data Sample

n = 10;
x = Range[n];
y = x + 0.1 RandomVariate[NormalDistribution[], n];
data = {x, y}\[Transpose]
ex = Abs[0.03*RandomVariate[NormalDistribution[], n]];
ey = Abs[0.1 RandomVariate[NormalDistribution[], n]];
err = {ex, ey}\[Transpose]
  • $\begingroup$ It could be modified a bit, yes. Do you have data we can use? Is the data weighted or unweighted (the latter case is easily done in a few lines, while the former will take some work). $\endgroup$ Feb 3 '17 at 7:58
  • $\begingroup$ J.M. I hope you were listening! I added a random dataset. The regression should go through (0,0). I have also a question: Why Mathematica does not have a built-in function for this? $\endgroup$
    – Fabio
    Feb 3 '17 at 8:15
  • $\begingroup$ General orthogonal fitting is highly nontrivial to properly and stably implement, and apparently not enough people are demanding it. $\endgroup$ Feb 3 '17 at 9:09
  • $\begingroup$ I fully agree with you. For instance in 11 years I've never used it, but as a researcher working in a group you have to agree with people that do not understand math in a good way (not saying at all)... so the trouble is here. $\endgroup$
    – Fabio
    Feb 3 '17 at 9:31
  • $\begingroup$ J.M. I was trying your original script on this data = {{0.02475, 1513.58}, {0.02927, 1514.14}, {0.03509, 1514.83}, {0.04235, 1515.82}, {0.05397, 1517.12}, {0.08012, 1519.94}} and err = {{0.000710526, 0.0208167}, {0.000840287, 0.0173205}, {0.00100737, 0.0251661}, {0.00121579, 0.0152753}, {0.00154938, 0.01}, {0.0023001, 0.0152753}} and the results are a bit strange. Could you please check? The fitting is way off the data $\endgroup$
    – Fabio
    Feb 3 '17 at 10:23

Well, I've used a trick by performing the fit over the original data extended to included the reflected data about the constrained point. This is the code using the original solution posted by JM, with the trick for constraining the fit passing through (0,0).

n = 20;
\[Alpha] = 2;
x = N[Range[n]];
y = \[Alpha]*x + 5 RandomVariate[NormalDistribution[], n];
ex = Abs[RandomVariate[NormalDistribution[], n]];
ey = Abs[RandomVariate[NormalDistribution[], n]];
data = {Flatten@{x, -x}, Flatten@{y, -y}}\[Transpose];
err = {Flatten@{ex, ex}, Flatten@{ey, ey}}\[Transpose];
{lin, {sm, sk}} = ortlinfit[data, err]
 Plot[lin[x], {x, 0, n}, PlotStyle -> Black],     
 Graphics[MapThread[Circle, {data, err}]],
 PlotRange -> {{0, n}, {0, \[Alpha] n}}, Frame -> True, 
 PlotRangePadding -> 0, Axes -> None

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.