procedure LineFit(const B: TVec; const X: TVec; const Y: TVec; const Weights: TVec);
Fits linear equation to data.
| # | Name | Description |
|---|---|---|
| 1 | X | Vector of independent variable. |
| 2 | Y | Vector of dependent variable. |
| 3 | Weights | Weights (optional). Weights are used only if they are set. |
| 4 | B | Returns regression coefficients for linear function. |
Result: stored in self (calling object)
Remarks:
The routine fits equations to data by minimizing the sum of squared residuals. The observed values obey the following equation:
Examples
Uses MtxExpr, MtxVecTee, Series, RegModels;
procedure Example(Series1, Series2: TLineSeries);
var Y,YHat,B,X: Vector;
begin
X.Size(100);
Y.Size(X);
X.Ramp(-5,0.05); // X = (-5, -4.95, ...-0.05)
Y.RandGauss(3.5,0.12); // populate sample data
LineFit(B,X,Y,NULL); // calculate coefficients
LineEval(B.Values,X,YHat); // evaluate y by using calculated coefficients
DrawValues(X,Y,Series1); // draw original data
DrawValues(X,YHat,Series2); // draw fitted data
end;
See Also: RegModels.LineDeriv, RegModels.LineEval