Can one force a linear regression fit to go through the origin?

data = Table[{i, 3 i + RandomReal[2]}, {i, 10}]
line = Fit[data, {x}, x]
Show[ListPlot[data, PlotStyle -> Red], Plot[line, {x, 0, 10}], PlotLabel -> line]

Mathematica graphics


Another possibility, a bit more general :

data = Table[{i, 3 i + RandomReal[2]}, {i, 10}];

model[x_] = a Cos[x] + b x^2 + c;

sol = FindFit[data, {model[x]}, {a, b, c}, x];

One can add constraints on the parameters to the model :

sol2 = FindFit[data, {model[x], model[0] == 0}, {a, b, c}, x];

Show[ListPlot[data],  Plot[{model[x] /. sol, model[x] /. sol2}, {x, 0, 10}]]

plot


I have given a derivation of the needed formulae in this math.SE answer. As already mentioned by belisarius, the canonical method for finding the equation of the least-squares line constrained to pass through the origin in Mathematica would be either of

Fit[data, {x}, x]

which produces the explicit linear function, or

FindFit[data, m x, m, x]

which produces just the slope of the best-fit line as a replacement rule.

To handle the case of a least-squares line constrained to pass through an arbitrary point $(h,k)$, you can again use either of Fit[] or FindFit[], but things are slightly trickier. (I'll leave the task of encapsulating that method in a Mathematica routine as an exercise.) For this answer, I'll present the explicit formula, as implemented in Mathematica:

fixedLine[data_?MatrixQ, pt : (_?VectorQ) : {0, 0}] := Module[{a, b, dat, sx, sy},
  dat = If[Precision[data] === Infinity, N[data], data];
  {sx, sy} = Transpose[dat] - pt;
  b = sx.sy/sx.sx;
  a = #2 - b #1 & @@ pt;
  Function[\[FormalX], Evaluate[Chop[a + b \[FormalX]]]]]

This returns the best-fit line as a pure function. Here are a few examples, taken from Kolb's Curve Fitting for Programmable Calculators:

fixedLine[{{100, 140}, {200, 230}, {300, 310}, {400, 400}, {500, 480}}, {300, 310}]
   Function[\[FormalX]$, 55. + 0.85 \[FormalX]$]

fixedLine[{{11, 15}, {17, 23}, {23, 31}, {29, 39}}]
   Function[\[FormalX]$, 1.34831 \[FormalX]$]

Tags:

Fitting