HP-71B Program to Calculate

General Polynomial Regression With the Math Module

(DATA Statement Version)

by Namir Shammas

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. Reads the polynomial order from a DATA statement.

2. Reads the number of observations from a DATA statement.

3. Reads for the values of  independent variable from DATA statements.

4. Reads the values of the dependent variable Y from DATA statements.

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]
PROCESSING DATA  
NUMBER OF POINTS? 5[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
20 DESTROY ALL @ STD
30 DISP "PROCESSING DATA"
40 READ M ! POLYNOM ORDER
50 READ N ! NUMBER OF POINTS
60 N1=N+1
70 DIM X(M),X9(M,N1),Y(M),X0(N1,M),X1(N1,N1),Y1(N1),C(N1)
80 READ X,Y
90 CALL TRSNF(X(),Y(),M,N1)
100 FOR I=1 TO M
110 FOR J=1 TO N1
120 X9(I,J)=X(I)^(J-1)
130 NEXT J
140 NEXT I
150 MAT X0=TRN(X9)
160 MAT X1=X0*X9
170 MAT Y1=X0*Y
180 MAT X1=INV(X1)
190 MAT C=X1*Y1
200 FOR I=1 TO N1
210 DISP "C(";I-1;")=";C(I) @ PAUSE
220 NEXT I
230 S=0 @ S1=0 @ S2=0
240 FOR I=1 TO M
250 S=S+Y(I) @ S2=S2+Y(I)^2
260 NEXT I
270 FOR I=1 TO N1
280 S1=S1+C(I)*Y1(I)
290 NEXT I
300 R2=(S1-S^2/M)/(S2-S^2/M)
310 DISP "R^2=";R2
320 ! DATA FOR N AND M
330 DATA 3,4
340 ! DATA FOR X
350 DATA .8,1,1.2,1.4,1.6,1.8
360 ! DATA FOR Y
370 DATA 24,20,10,13,12
380 END
390 SUB TRSNF(X(),Y(),M,N1)
400 ! DATA TRANSFORMATION
410 END SUB
 

The DATA statements in the above listing are specific to the problem solved earlier. The must appear before the END statement. You must edit the DATA statement every time you deal with a different set of observations. The advantage of using the DATA statements is that you can easily re-run the program for slightly different data without typing in the observations over and over again. You can change, add, or remove observations and then re-run the program. You can also keep the observations intact, but try different data transformations, using subroutine TRNSF.

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:

390 SUB TRSNF(X(),Y(),M,N1)
400 ! DATA TRANSFORMATION
402 FOR I =1 TO M
404 Y(I)=LOG(Y(I))
406 NEXT I
410 END SUB
 

BACK

Copyright (c) Namir Shammas. All rights reserved.