HP-71B Program to Calculate

General Multiple Linear Regression With the Math Module

(Keyboard Input 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.

The program performs the following tasks:

1. Prompts you for the number of independent variables.

2. Prompts you for the number of observations.

3. Prompts you for the values in the matrix of  independent variables. REMEMBER THAT THE FIRST COLUMN OF THIS MATRIX IS ONES.

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 (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]
NUMBER OF X VARS? 3[END LINE]
NUMBER OF POINTS? 5[END LINE]
X(1,1)? 1[END LINE]
X(1,2)? 7[END LINE]
X(1,3)? 25[END LINE]
X(1,4)? 6[END LINE]
X(2,1)? 1[END LINE]
X(2,2)? 1[END LINE]
X(2,3)? 29[END LINE]
X(2,4)? 15[END LINE]
X(3,1)? 1[END LINE]
X(3,2)? 11[END LINE]
X(3,3)? 56[END LINE]
X(3,4)? 8[END LINE]
X(4,1)? 1[END LINE]
X(4,2)? 11[END LINE]
X(4,3)? 31[END LINE]
X(4,4)? 8[END LINE]
X(5,1)? 1[END LINE]
X(5,2)? 7[END LINE]
X(5,3)? 52[END LINE]
X(5,4)? 6[END LINE]
Y(1)? 60[END LINE]
Y(2)? 52[END LINE]
Y(3)? 20[END LINE]
Y(4)? 47[END LINE]
Y(5)? 33[END LINE]
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
15 DESTROY ALL @ STD
20 INPUT "NUMBER OF X VARS? ";N
30 INPUT "NUMBER OF POINTS? ";M
40 N1=N+1
50 DIM X(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 MAT X0=TRN(X)
100 MAT X1=X0*X
110 MAT Y1=X0*Y
120 MAT X1=INV(X1)
130 MAT C=X1*Y1
140 FOR I=1 TO N1
150 DISP "C(";I-1;")=";C(I) @ PAUSE
160 NEXT I
170 S=0 @ S1=0 @ S2=0
180 FOR I=1 TO M
190 S=S+Y(I) @ S2=S2+Y(I)^2
200 NEXT I
210 FOR I=1 TO N1
220 S1=S1+C(I)*Y1(I)
230 NEXT I
240 R2=(S1-S^2/M)/(S2-S^2/M)
250 DISP "R^2=";R2
260 END
270 SUB TRSNF(X(,),Y(),M,N1)
280 ! DATA TRANSFORMATION
290 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 power regression for all the variables, for example, the subroutine TRNSF would look like:

270 SUB TRSNF(X(,),Y(),M,N1)
280 ! DATA TRANSFORMATION
281 FOR I = 1 TO M @ Y(I) = LOG(Y(I)) @ NEXT I
282 FOR I = 1 TO M
283 FOR J = 2 TO N1
284 X(I,J) = LOG(X(I,J))
283 NEXT J
283 NEXT I
290 END SUB
 

BACK

Copyright (c) Namir Shammas. All rights reserved.