1
{ ******************************************************************
2
Multiple linear regression (Gauss-Jordan method)
3
****************************************************************** }
13
procedure MulFit(X : PMatrix;
15
Lb, Ub, Nvar : Integer;
19
{ ------------------------------------------------------------------
20
Multiple linear regression: Y = B(0) + B(1) * X + B(2) * X2 + ...
21
------------------------------------------------------------------
22
Input parameters: X = matrix of independent variables
23
Y = vector of dependent variable
25
Nvar = number of independent variables
26
ConsTerm = presence of constant term B(0)
27
Output parameters: B = regression parameters
29
------------------------------------------------------------------ }
31
procedure WMulFit(X : PMatrix;
33
Lb, Ub, Nvar : Integer;
37
{ ----------------------------------------------------------------------
38
Weighted multiple linear regression
39
----------------------------------------------------------------------
40
S = standard deviations of observations
41
Other parameters as in MulFit
42
---------------------------------------------------------------------- }
46
procedure MulFit(X : PMatrix;
48
Lb, Ub, Nvar : Integer;
54
Lb1 : Integer; { Index of first param. (0 if cst term, 1 otherwise) }
55
I, J, K : Integer; { Loop variables }
56
Det : Float; { Determinant }
59
if Ub - Lb < Nvar then
61
SetErrCode(MatErrDim);
73
{ If constant term, set line 0 and column 0 of matrix V }
76
V^[0]^[0] := Int(Ub - Lb + 1);
80
V^[0]^[J] := V^[0]^[J] + X^[K]^[J];
81
B^[0] := B^[0] + Y^[K];
84
V^[J]^[0] := V^[0]^[J];
87
{ Set other elements of V }
92
V^[I]^[J] := V^[I]^[J] + X^[K]^[I] * X^[K]^[J];
93
B^[I] := B^[I] + X^[K]^[I] * Y^[K];
96
{ Fill in symmetric matrix }
98
for J := 1 to Pred(I) do
99
V^[I]^[J] := V^[J]^[I];
101
{ Solve normal equations }
102
if ConsTerm then Lb1 := 0 else Lb1 := 1;
103
LinEq(V, B, Lb1, Nvar, Det);
106
procedure WMulFit(X : PMatrix;
108
Lb, Ub, Nvar : Integer;
114
Lb1 : Integer; { Index of first param. (0 if cst term, 1 otherwise) }
115
I, J, K : Integer; { Loop variables }
116
W : PVector; { Vector of weights }
117
WX : Float; { W * X }
118
Det : Float; { Determinant }
121
if Ub - Lb < Nvar then
123
SetErrCode(MatErrDim);
137
W^[K] := 1.0 / Sqr(S^[K]);
140
for I := 0 to Nvar do
142
for J := 0 to Nvar do
147
{ If constant term, set line 0 and column 0 of matrix V }
152
V^[0]^[0] := V^[0]^[0] + W^[K];
153
for J := 1 to Nvar do
154
V^[0]^[J] := V^[0]^[J] + W^[K] * X^[K]^[J];
155
B^[0] := B^[0] + W^[K] * Y^[K];
157
for J := 1 to Nvar do
158
V^[J]^[0] := V^[0]^[J];
161
{ Set other elements of V }
163
for I := 1 to Nvar do
165
WX := W^[K] * X^[K]^[I];
166
for J := I to Nvar do
167
V^[I]^[J] := V^[I]^[J] + WX * X^[K]^[J];
168
B^[I] := B^[I] + WX * Y^[K];
171
{ Fill in symmetric matrix }
172
for I := 2 to Nvar do
173
for J := 1 to Pred(I) do
174
V^[I]^[J] := V^[J]^[I];
176
{ Solve normal equations }
177
if ConsTerm then Lb1 := 0 else Lb1 := 1;
178
LinEq(V, B, Lb1, Nvar, Det);
b'\\ No newline at end of file'