This program is by Namir Shammas and is used here by permission.
This program is supplied without representation or warranty of any kind. Namir Shammas and The Museum of HP Calculators therefore assume no responsibility and shall have no liability, consequential or otherwise, of any kind arising from the use of this program material or any part thereof.
The following program calculates the statistical coefficients for the following model:
H(Z) = A + B F(X) + C G(Y)
Where X, and Y are interdependent variables and Z is the dependent variable. In addition, H(), F(), and G() are transformation functions for the regression variables. The program also calculates the coefficient of determination R-Square.
The program has a simple menu that allows you to:
1. Add observations to the regression's statistical summations.
2. Delete an observation.
3. Reinitialize the regression's statistical summations.
4. Calculate the regression coefficients.
5. Quit the program.
Here is a sample session that fits the data in the following table:
X | Y | Z |
1.5 | 0.7 | 2.1 |
0.45 | 2.3 | 4.0 |
1.8 | 1.6 | 4.1 |
2.8 | 4.5 | 9.4 |
DISPLAY |
ENTER/PRESS |
> | [RUN] |
STAT SUMS RESET | |
1)ADD 2)DEL 3)CALC | |
4)NEW 5)QUIT CHOICE=> | 1[END LINE] |
# OF POINTS TO ADD? | 4[END LINE] |
X? | 1.5[END LINE] |
Y? | 0.7[END LINE] |
Z? | 2.1[END LINE] |
X? | 0.45[END LINE] |
Y? | 2.3[END LINE] |
Z? | 4.0[END LINE] |
X? | 1.8[END LINE] |
Y? | 1.6[END LINE] |
Z? | 4.1[END LINE] |
X? | 2.8[END LINE] |
Y? | 4.5[END LINE] |
Z? | 9.4[END LINE] |
1)ADD 2)DEL 3)CALC | |
4)NEW 5)QUIT CHOICE=> | 3[END LINE] |
A = -0.10 | [CONT] |
B = 0.70 | [CONT] |
C = 1.63 | [CONT] |
R2 = 1.00 | [CONT] |
1)ADD 2)DEL 3)CALC | |
4)NEW 5)QUIT CHOICE=> | 5[END LINE] |
BYE! |
Here is the BASIC listing:
10 REM MULTIPLE LINEAR REGRESSION WITH A
MENU
20 REM INIT STAT SUMS
30 GOSUB 780
40 REM MENU
50 DISP "1)ADD 2)DEL 3)CALC" @ WAIT 3
60 INPUT "4)NEW 5)QUIT CHOICE=>";C
70 IF C = 1 THEN GOSUB 160
80 IF C = 2 THEN GOSUB 400
90 IF C = 3 THEN GOSUB 610
100 IF C = 4 THEN GOSUB 780
110 IF C = 5 THEN GOTO 140
120 GOTO 40
130 DISP "BYE!"
140 REM QUIT
150 END
160 REM ADD
170 INPUT "# OF POINTS TO ADD? ";M
180 IF M < 1 THEN 380
190 FOR I = 1 TO M
200 INPUT "X? ";X
210 INPUT "Y? ";Y
220 INPUT "Z? ";Z
230 REM OPTIONAL TRANSFORMATIONS
240 GOSUB <<TX>
250 GOSUB 880
260 GOSUB <<TZ>
270 N = N + 1
280 S1 = S1 + X
290 S4 = S4 + X * X
300 S2 = S2 + Y
310 S5 = S5 + Y * Y
320 S7 = S7 + X * Y
330 S3 = S3 + Z
340 S6 = S6 + Z * Z
350 S8 = S8 + X * Z
360 S9 = S9 + Y * Z
370 NEXT I
380 REM ADD_END
390 RETURN
400 REM DEL
410 IF N < 1 THEN DISP "NO DATA TO DELETE" @ WAIT 3 @ GOTO 590
420 INPUT "X? ";X
430 INPUT "Y? ";Y
440 INPUT "Z? ";Z
450 REM TRANSFORMATION
460 GOSUB 840
470 GOSUB 880
480 GOSUB 920
490 N = N - 1
500 S1 = S1 - X
510 S4 = S4 - X * X
520 S2 = S2 - Y
530 S5 = S5 - Y * Y
540 S7 = S7 - X * Y
550 S3 = S3 - Z
560 S6 = S6 - Z * Z
570 S8 = S8 - X * Z
580 S9 = S9 - Y * Z
590 REM DEL_END
600 RETURN
610 REM CALC
620 IF N < 2 THEN DISP "NOT ENOUGH DATA" @ PAUSE @ GOTO 760
630 C1 = N * S4 - S1^2
640 C2 = N * S5 - S2^2
650 C3 = N * S7 - S1 * S2
660 C4 = N * S8 - S3 * S1
670 C5 = N * S9 - S3 * S2
680 C = (C1 * C5 - C3 * C4) / (C1 * C2 - C3^2)
690 B = (C4 - C * C3) / C1
700 A = (S3 - C * S2 - B * S1) / N
710 R2 = (A * S3 + B * S8 + C * S9 - S3^2 / N) / (S6 - S3^2 / N)
720 DISP "A = "; A @ PAUSE
730 DISP "B = "; B @ PAUSE
740 DISP "C = "; C @ PAUSE
750 DISP "R^2 = "; R2 @ PAUSE
760 REM CALC_END
770 RETURN
780 REM NEW
790 S1 = 0 @ S4 = 0 @ N = 0
800 S2 = 0 @ S5 = 0 @ S7 = 0
810 S3 = 0 @ S6 = 0 @ S8 = 0 @ S9 = 0
820 DISP "STAT SUMS RESET" @ WAIT 3
830 RETURN
840 REM TX
850 REM X=F(X)
860 X = X
870 RETURN
880 REM TY
890 REM Y=F(Y)
900 Y = Y
910 RETURN
920 REM TZ
930 REM Z=F(Z)
940 Z = Z
950 RETURN
Subroutines that start at lines 840, 880, and 930 contains the BASIC statements that transform the variables X, Y, and Z. The current code performs no effective transformation (simply assigns each variable to itself). As such, the program performs a multiple linear regression:
Z = A + B X + C Y
You can alter the statements in these subroutines to linearize the data if need be. For example, to perform a power fit you write the subroutine lines as:
840 REM TX
850 REM X=F(X)
860 X = LOG(X)
870 RETURN
880 REM TY
890 REM Y=F(Y)
900 Y = LOG(Y)
910 RETURN
920 REM TZ
930 REM Z=F(Z)
940 Z = LOG(Z)
950 RETURN
The above code will fit the following model:
ln(Z) = A + B ln(X) + C ln(Y)
Go back to the software library
Go
back to the main exhibit hall