The following program calculates the statistical coefficients for the following polynomial model:
G(Y) = C0 + C1 F(X) + C2 F(X)^2 + ... + Cn F(X)^n
Where X is the independent variable and Y is the dependent variable. In addition, G() and F() are optional transformation functions for the regression variables. The program also calculates the coefficient of determination R-Square.
The program performs the following tasks:
1. Prompts you for the polynomial order.
2. Prompts you for the number of observations.
3. Prompts you for the values of independent variable.
4. Prompts you for the values of the dependent variable Y.
5. Calculates and displays the regression coefficients C(0), C(1), and so on..
6. Calculates and displays the coefficient of determination R-Square.
Here is a sample session that fits the data in the following table:
X | Y |
0.8 | 24 |
1.0 | 20 |
1.2 | 10 |
1.4 | 13 |
1.6 | 12 |
DISPLAY |
ENTER/PRESS |
> | [RUN] |
POLYNOM ORDER? | 3[END LINE] |
NUMBER OF POINTS? | 5[END LINE] |
X(1)? | 0.8[END LINE] |
X(2)? | 1[END LINE] |
X(3)? | 1.2[END LINE] |
X(4)? | 1.4[END LINE] |
X(5)? | 1.6[END LINE] |
Y(1)? | 24[END LINE] |
Y(2)? | 20[END LINE] |
Y(3)? | 10[END LINE] |
Y(4)? | 13[END LINE] |
Y(5)? | 12[END LINE] |
C( 0 ) =47.942858998 | [CONT] |
C( 1 ) =-9.761906484 | [CONT] |
C( 2 ) =-41.071429946 | [CONT] |
C( 3 ) =20.833331924 | [CONT] |
R2 = 0.868504071094 |
Here is the BASIC listing:
10 ! POLYNOMIAL REGRESSION
15 DESTROY ALL @ STD
20 INPUT "POLYNOM ORDER? ";N
30 INPUT "NUMBER OF POINTS? ";M
40 N1=N+1
50 DIM X(M),X9(M,N1),Y(M),X0(N1,M),X1(N1,N1),Y1(N1),C(N1)
60 MAT INPUT X
70 MAT INPUT Y
80 CALL TRSNF(X(),Y(),M,N1)
90 FOR I=1 TO M
100 FOR J=1 TO N1
110 X9(I,J)=X(I)^(J-1)
120 NEXT J
130 NEXT I
140 MAT X0=TRN(X9)
150 MAT X1=X0*X9
160 MAT Y1=X0*Y
170 MAT X1=INV(X1)
180 MAT C=X1*Y1
190 FOR I=1 TO N1
200 DISP "C(";I-1;")=";C(I) @ PAUSE
210 NEXT I
220 S=0 @ S1=0 @ S2=0
230 FOR I=1 TO M
240 S=S+Y(I) @ S2=S2+Y(I)^2
250 NEXT I
260 FOR I=1 TO N1
270 S1=S1+C(I)*Y1(I)
280 NEXT I
290 R2=(S1-S^2/M)/(S2-S^2/M)
300 DISP "R^2=";R2
310 END
320 SUB TRSNF(X(),Y(),M,N1)
330 ! DATA TRANSFORMATION
340 END SUB
The subroutine TRNSF allows you to place any required data transformation statements. The current version of the code has that subroutine void of any executable statements. This means that the current multiple regression in strictly linear.
To perform a logarithmic transformation on Y values , for example, the subroutine TRNSF would look like:
320 SUB TRSNF(X(),Y(),M,N1)
330 ! DATA TRANSFORMATION
331 FOR I =1 TO M
330 Y(I)=LOG(Y(I))
330 NEXT I
340 END SUB
Copyright (c) Namir Shammas. All rights reserved.