1
{ **********************************************************************
4
* (c) J. Debord, October 1998 *
5
**********************************************************************
6
This unit fits the multiple linear equation:
8
y = b0 + b1.x1 + b2.x2 + ...
10
********************************************************************** }
19
FMath, Matrices, Regress;
21
function FuncName : String;
23
function FirstParam : Integer;
25
function LastParam : Integer;
27
function ParamName(I : Integer) : String;
29
function RegFunc(X, B : PVector) : Float;
31
function FitModel(Method : Integer; X : PMatrix; Y, W : PVector;
32
N : Integer; B : PVector; V : PMatrix) : Integer;
34
procedure InitModel(CstPar : PVector);
40
Nvar : Integer = 2; { Number of independent variables }
41
ConsTerm : Boolean = True; { Flags the presence of a constant term b0 }
43
function FuncName : String;
44
{ --------------------------------------------------------------------
45
Returns the name of the regression function
46
-------------------------------------------------------------------- }
53
Name := Name + 'b0 + ';
54
Name := Name + 'b1.x1';
58
Name := Name + ' + b' + S + '.x' + S;
63
function FirstParam : Integer;
64
{ --------------------------------------------------------------------
65
Returns the index of the first parameter to be fitted
66
-------------------------------------------------------------------- }
74
function LastParam : Integer;
75
{ --------------------------------------------------------------------
76
Returns the index of the last parameter to be fitted
77
-------------------------------------------------------------------- }
82
function ParamName(I : Integer) : String;
83
{ --------------------------------------------------------------------
84
Returns the name of the I-th parameter
85
-------------------------------------------------------------------- }
93
function RegFunc(X, B : PVector) : Float;
94
{ --------------------------------------------------------------------
95
Computes the regression function at observation X
96
B is the vector of parameters.
97
-------------------------------------------------------------------- }
102
if ConsTerm then Y := B^[0] else Y := 0.0;
103
for I := 1 to Nvar do
104
Y := Y + B^[I] * X^[I];
108
function FitModel(Method : Integer; X : PMatrix; Y, W : PVector;
109
N : Integer; B : PVector; V : PMatrix) : Integer;
110
{ --------------------------------------------------------------------
111
Multiple linear regression
112
--------------------------------------------------------------------
113
Input : Method = 0 for unweighted regression, 1 for weighted
114
X = matrix of independent variables
115
Y = vector of dependent variable
116
W = vector of weights
117
N = number of observations
118
Output : B = estimated regression parameters
119
V = variance-covariance matrix of parameters
120
-------------------------------------------------------------------- }
123
0 : FitModel := MulFit(X, Y, N, Nvar, ConsTerm, B, V);
124
1 : FitModel := WMulFit(X, Y, W, N, Nvar, ConsTerm, B, V);
128
procedure InitModel(CstPar : PVector);
129
{ --------------------------------------------------------------------
130
Initializes the global variables of the unit
131
--------------------------------------------------------------------
132
CstPar^[0] = number of independent variables
133
CstPar^[1] = 1 to include a constant term (b0)
134
-------------------------------------------------------------------- }
136
Nvar := Round(CstPar^[0]);
137
ConsTerm := (CstPar^[1] = 1);