1
{ ******************************************************************
2
This program performs a least squares fit of a straight line:
4
****************************************************************** }
12
N = 5; { Number of points }
13
Alpha = 0.05; { Significance level }
17
X : array[1..N] of Float = (10, 20, 30, 40, 50);
18
Y : array[1..N] of Float = ( 0.1865, 0.3616, 0.537, 0.7359, 0.9238);
21
B : PVector; { Regression parameters }
23
function PltFunc(X : Float) : Float;
24
{ ------------------------------------------------------------------
25
Function to be plotted
26
------------------------------------------------------------------ }
29
PltFunc := B^[0] + B^[1] * X
32
procedure WriteResults(X, Y, Ycalc, B : PVector;
36
{ ------------------------------------------------------------------
37
Writes results to screen
38
------------------------------------------------------------------ }
42
Line2 : String; { Separating lines }
43
Delta : Float; { Residual }
44
Sr : Float; { Residual standard deviation }
45
SB : Float; { Standard deviations of parameters }
46
I : Integer; { Loop variable }
49
Line1 := StrChar(73, '-');
50
Line2 := StrChar(73, '=');
53
WriteLn('Linear regression: Y = B(0) + B(1) * X');
56
WriteLn('Parameter Est.value Std.dev. ',
57
(100 * (1 - Alpha)):2:0, '% Confidence Interval');
63
SB := Sqrt(V^[I]^[I]);
64
WriteLn('B(', I:1, ')', B^[I]:17:8, SB:17:8,
65
(B^[I] - Tc * SB):17:8, ';', (B^[I] + Tc * SB):17:8);
70
WriteLn('Number of observations : n = ', N:5);
75
WriteLn('Residual error : s = ', Sr:10:4);
76
WriteLn('Coefficient of correlation : r = ', (Sgn(B^[1]) * Sqrt(R2)):10:4);
77
WriteLn('Coefficient of determination : r2 = ', R2:10:4);
78
WriteLn('Adjusted coeff. of determination : r2a = ', R2a:10:4);
79
WriteLn('Variance ratio (explained/resid.) : F(', Nu1:3, ', ', Nu2:3, ') = ', F:10:4);
80
WriteLn('Critical variance ratio : F(p = ', (1 - Alpha):4:2, ') = ', Fc:10:4);
84
WriteLn(' i Y obs. Y calc. Residual Std.dev. Std.res.');
89
Delta := Y^[I] - Ycalc^[I];
90
WriteLn(I:3, Y^[I]:14:4, Ycalc^[I]:14:4, Delta:14:4, Sr:14:4, (Delta / Sr):14:4);
96
procedure PlotGraph(X, Y, B : PVector);
97
{ ------------------------------------------------------------------
98
Plots histogram and normal curve
99
------------------------------------------------------------------ }
102
Xmin, Xmax, Xstep : Float; { Ox scale }
103
Ymin, Ymax, Ystep : Float; { Oy scale }
106
if not InitGraphics(9, 2, 'c:\tp\bgi') then { 640x480 16 color }
108
Writeln('Unable to set graphic mode');
112
SetWindow(15, 85, 15, 85, True);
114
AutoScale(X, 1, N, LinScale, Xmin, Xmax, Xstep);
115
AutoScale(Y, 1, N, LinScale, Ymin, Ymax, Ystep);
117
SetOxScale(LinScale, Xmin, Xmax, Xstep);
118
SetOyScale(LinScale, Ymin, Ymax, Ystep);
120
SetGraphTitle('Linear Regression');
131
SetLineParam(1, 0, 0, 0); { Don't connect points }
132
PlotCurve(X, Y, 1, N, 1);
134
PlotFunc({$IFDEF FPC}@{$ENDIF}PltFunc, Xmin, Xmax, 2);
141
{ ******************************************************************
143
****************************************************************** }
146
XX, YY : PVector; { Data }
147
Ycalc : PVector; { Computed Y values }
148
V : PMatrix; { Variance-covariance matrix }
149
Test : TRegTest; { Statistical tests }
150
Tc : Float; { Critical t value }
151
Fc : Float; { Critical F value }
152
I : Integer; { Loop variable }
169
{ Perform regression }
170
LinFit(XX, YY, 1, N, B, V);
172
{ Compute predicted Y values }
174
Ycalc^[I] := B^[0] + B^[1] * XX^[I];
176
{ Update variance-covariance matrix and compute statistical tests }
177
RegTest(YY, Ycalc, 1, N, V, 0, 1, Test);
179
{ Compute Student's t and Snedecor's F }
180
Tc := InvStudent(N - 2, 1 - 0.5 * Alpha);
181
Fc := InvSnedecor(1, N - 2, 1 - Alpha);
184
WriteResults(XX, YY, Ycalc, B, V, Test, Tc, Fc);
187
PlotGraph(XX, YY, B);