HP Forums
(15C) Multiple Linear Regression for HP-15C - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: General Software Library (/forum-13.html)
+--- Thread: (15C) Multiple Linear Regression for HP-15C (/thread-525.html)



(15C) Multiple Linear Regression for HP-15C - Namir - 01-26-2014 02:16 PM

To perform the regression of the following linear model:

Y = a + bX + cZ

We transform it into the following equations:

(Y-Ymean)/(X-Xmean) = b + c*(Z-Zmean)/(X-Xmean)

Then Calculate a using:

a = Ymean - b*Xmean - c*Zmean

I have added a ZIP file containing the source code for the HP-15C that Torsten recently introduced to this web site (click here).

The drawback for this method is having to enter the data twice. The advantage for this method (over using matrices in the HP-15C to perform multiple regression) is that there is no limit for the number of data points entered.

Memory Map


RCL 0 = Ymean
RCL 1 = Xmean
RCL 8 = Zmean
RCL 9 = X - Xmean

Flags

F0 = When internally set, the program goes into data deletion mode.

Listing

Code:
1 LBL B        # Enter Y before pressing B. Use this label to delete data
2 SF 0        # set flag 0 to delete data
3 LBL A        # Enter Y before pressing A
4 RCL 0
5 -        # Calculate Y - Ymean
6 R/S        # Enter X
7 RCL 1
8 -        # Calculate X - Xmean
9 STO 9
10 /        # Calculate (Y-Ymean)/(X-Xmean)
11 R/S        # Enter Z
12 RCL 8
13 -        # Calculate Z - Zmean
14 RCL 9        
15 /        # Calculate (Z-Zmean)/(X-Xmean)
16 FS? 0        
17 GTO 0
18 Sigma+
19 RTN
20 LBL 0
21 CF 0
22 Sigma-
23 RTN
24 LBL C        # Calculate slope and intercept
25 L.R.
26 R/S
27 RCL 1
28 *        # calculate b*Xmean
29 X<>Y
30 RCL 8
31 *        # calculate c*zmean
32 +        # calculate b*Xmean + c*Zmean
33 CHS
34 RCL 0        
35 +        # calculate a = Ymean - b*Xmean - c*Zmean
36 RTN

Usage

Phase 1

In this phase perform the following:
1. Clear teh statistical registers
2. Enter the values of Y.
3. Calculate the mean value of Y and record it.
4. Clear teh statistical registers
5. Enter the values of X.
6. Calculate the mean value of X and record it.
7. Clear teh statistical registers
8. Enter the values of Z.
9. Calculate the mean value of Z and record it.
10. Clear the statistical registers.
11. Enter teh means for Y, X, and Z in registers 0, 1, and 8 respectively.

Phase 2

1. Enter the value of Y and press the keys f and A.
2. Enter the value of X and press R/S.
3. Enter the value of Z and press R/S.
4. Repeat steps 1 through 3 for all the observations.
5. To calculate the values of coefficients b and c, press the keys f and C. The program, displays the value of coefficient b in the X register.
6. Press the key X<>Y to view and record the value of coefficient c.
7. Press the key X<>Y.
8. Press the key R/S to display the coefficient a.

To delete a data point:
1. Enter the value of Y and press the keys f and B.
2. Enter the value of X and press R/S.
3. Enter the value of Z and press R/S.

Example

Give the following data:

Code:
Y    X    Z
-4.5    1    55
-6.1    2    67
7.1    3.5    33
7.8    4    34
7.7    5    41

In phase 1 calculate the mean for Y as 2.4, the mean for X as 3.1, and the mean for Z as 46. Store these means in registers 0, 1, and 8, respectively.

1. Enter the value of -4.5 and press the keys f and A.
2. Enter the value of 1 and press R/S.
3. Enter the value of 33 and press R/S.
4. Repeat steps 1 through 3 for all the above observations.
5. To calculate the values of coefficients b and c, press the keys f and C. The program, displays the 2.000 as the value of coefficient b in the X register.
6. Press the key X<>Y to view and record -0.30000 as the value of coefficient c.
7. Press the key X<>Y.
8. Press the key R/S to display 10.000 as the coefficient a.

The model is Y = 10 + 2*X - 0.3*Z


RE: Multiple Linear Regression for HP-15C - Thomas Klemm - 01-27-2014 07:35 AM

This example could be calculated without entering data twice or using any program.
Make sure to set the calculator to USER mode.

Enter matrix \(A\):
\(A = \begin{bmatrix}
1 & 1 & 55 \\
1 & 2 & 67 \\
1 & 3.5 & 33 \\
1 & 4 & 34 \\
1 & 5 & 41 \\
\end{bmatrix}
\)

Code:
5 ENTER 3
DIM A
MATRIX 1
1   STO A
    STO A
55  STO A
1   STO A
2   STO A
(…)
5   STO A
41  STO A

Enter matrix \(B\):
\(
B = \begin{bmatrix}
-4.5 \\
-6.1 \\
7.1 \\
7.8 \\
7.7 \\
\end{bmatrix}
\)

Code:
5 ENTER 1
DIM B
-4.5   STO B
-6.1   STO B
(…)
 7.7   STO B

Calculate \(C=A^{\top}A\):
Code:
RESULT C
RCL MATRIX A
ENTER
MATRIX 5

Calculate \(D=A^{\top}B\):
Code:
RESULT D
RCL MATRIX A
RCL MATRIX B
MATRIX 5

Calculate \(E=C^{-1}D\):
Code:
RESULT E
RCL MATRIX D
RCL MATRIX C
/

Get result:
Code:
RCL E   -> 10
RCL E   -> 2
RCL E   -> -0.3

Cheers
Thomas


RE: Multiple Linear Regression for HP-15C - Namir - 01-27-2014 11:57 AM

What is the size limit on using matrices in the 15C do to multiple regression?


RE: Multiple Linear Regression for HP-15C - Paul Dale - 01-27-2014 12:04 PM

Registers will be the limit. You've got to store all the data points, the results and the intermediates inside of 64 registers.


- Pauli


RE: Multiple Linear Regression for HP-15C - Thomas Klemm - 01-27-2014 07:23 PM

(01-27-2014 11:57 AM)Namir Wrote:  What is the size limit on using matrices in the 15C do to multiple regression?
15. But you have to calculate D first, then delete B (DIM 0,0) and only then calculate C and E. My emulator on the iPhone sucks: it will just crash when I try to create a matrix that uses all 64 registers. So I can't tell you what happens when you try with 16 on the real thing.

If you want to avoid entering the data twice you could use \(\sum{+}\) to calculate \(n\), \(\sum{X}\), \(\sum{X^2}\), \(\sum{Z}\), \(\sum{Z^2}\) and \(\sum{XZ}\). In addition to that you have to calculate \(\sum{Y}\), \(\sum{XY}\) and \(\sum{ZY}\) separately.

Cheers
Thomas


RE: Multiple Linear Regression for HP-15C - Namir - 01-28-2014 11:16 PM

(01-27-2014 07:23 PM)Thomas Klemm Wrote:  
(01-27-2014 11:57 AM)Namir Wrote:  What is the size limit on using matrices in the 15C do to multiple regression?
15. But you have to calculate D first, then delete B (DIM 0,0) and only then calculate C and E. My emulator on the iPhone sucks: it will just crash when I try to create a matrix that uses all 64 registers. So I can't tell you what happens when you try with 16 on the real thing.

If you want to avoid entering the data twice you could use \(\sum{+}\) to calculate \(n\), \(\sum{X}\), \(\sum{X^2}\), \(\sum{Z}\), \(\sum{Z^2}\) and \(\sum{XZ}\). In addition to that you have to calculate \(\sum{Y}\), \(\sum{XY}\) and \(\sum{ZY}\) separately.

Cheers
Thomas

Thomas,

My main point is to perform multiple linear regression using the L.R. command. One can always use the equations from the HP-67 Stat Pac I to do a straight forward calculations. Such calculations will require a longer program BUT has the advantage of entering the data once.