HP-71B Program to Calculate

General Multiple Linear Regression With the Math Module

(DATA Statement Version)

by Namir Shammas

The following program calculates the statistical coefficients for the following model:

G(Y) = C0 + C1 F1(X1)  + C2 F2(X2) + ...  + Cn Fn(Xn)

Where X1,  X2, ..., and Xn are the independent variables and Y is the dependent variable. In addition, G(), F1(),  F2() and so on are optional transformation functions for the regression variables. The program also calculates the coefficient of determination R-Square.

This program obtains its input from DATA statements. This approach allows you to comfortably re-run the programs multiple times and examine the effects of changing some of the data and/or the data transformation. Since the DATA statements provide the input, your save on keystrokes.

The program performs the following tasks:

1. Reads the number of independent variables from a DATA statement.

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

3. Reads the values in the matrix of  independent variables from DATA statements. REMEMBER THAT THE FIRST COLUMN OF THIS MATRIX IS ONES.

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 (note that X0 is a dummy variable that represents the column of 1's):

X0 X1 X2 X3 Y
1 7 25 6 60
1 1 29 15 52
1 11 56 8 20
1 11 31 8 47
1 7 52 6 33

 

DISPLAY

 ENTER/PRESS

> [RUN]
PROCESSING DATA  
C( 0 ) =103.447316589 [CONT]
C( 1 ) =-1.28409650404 [CONT]
C( 2 ) =-1.03692762188 [CONT]
C( 3 ) =-1.33948793673 [CONT]
R2 = 0.998937219108  

Here is the BASIC listing:

10 ! MULTIPLE LINEAR REGRESSION
20 DESTROY ALL @ STD
30 DISP "PROCESSING DATA"
40 READ N ! NUMBER OF X VARS
50 READ M ! NUMBER OF POINTS
60 N1=N+1
70 DIM X(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 MAT X0=TRN(X)
110 MAT X1=X0*X
120 MAT Y1=X0*Y
130 MAT X1=INV(X1)
140 MAT C=X1*Y1
150 FOR I=1 TO N1
160 DISP "C(";I-1;")=";C(I) @ PAUSE
170 NEXT I
180 S=0 @ S1=0 @ S2=0
190 FOR I=1 TO M
200 S=S+Y(I) @ S2=S2+Y(I)^2
210 NEXT I
220 FOR I=1 TO N1
230 S1=S1+C(I)*Y1(I)
240 NEXT I
250 R2=(S1-S^2/M)/(S2-S^2/M)
260 DISP "R^2=";R2
270 ! DATA FOR N AND M
280 DATA 3, 5
290 ! DATA FOR X. FIRST VALUE MUST BE 1
300 DATA 1,7,25,6
310 DATA 1,1,29,15
320 DATA 1,11,56,8
330 DATA 1,11,31,8
340 DATA 1,7,52,6
350 ! DATA FOR Y
360 DATA 60,52,20,47,33
370 END
380 SUB TRSNF(X(,),Y(),M,N1)
390 ! DATA TRANSFORMATION
400 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 power regression for all the variables, for example, the subroutine TRNSF would look like:

380 SUB TRSNF(X(,),Y(),M,N1)
390 ! DATA TRANSFORMATION
391 FOR I = 1 TO M @ Y(I) = LOG(Y(I)) @ NEXT I
394 FOR I = 1 TO M
395 FOR J = 2 TO N1
396 X(I,J) = LOG(X(I,J))
397 NEXT J
398 NEXT I
400 END SUB
 

BACK

Copyright (c) Namir Shammas. All rights reserved.