Multiple Linear Regression Program for the HP-42S


The program MLRAN is a program that performs multiple linear regression and provides the following results:

1. Coefficients of multiple linear regressions
2. Coefficient of determination and its adjusted value
3. The regression ANOVA table

The program uses matrix operations to calculate the regression coefficients column matrix:

C = inv(X' X) * (X' Y)

Where X' is the transpose of matrix X. The program calculates the ANOVA table entries:

SSR = C' X' Y - sum(Y)^2 / N
SSE = Y' Y - C'X'Y
SST = Y' Y - sum(Y)^2 / N
DF Residuals = K
DF Error = N - K - 1
DF Total = N - 1
MSR = SSR / K
MSE = SSE / (N - K - 1)
Fstat = MSR / MSE
R2 = SSR / SST
R2adj = 1 - (1 - R2) (N - 1) / (N - K - 1)

Where N is the number of observations and K is the number of independent variables.

USAGE:

1. Before you start the program you must enter the dependent variable's observations is a 
   column matrix (call it MY) and the independent variables' observations in a matrix (call it MX).
   The first column of this matrix must be full of ones. The second column in the matrix contains
   the data for the first independent variable. The third column in the matrix contains the data
   for the second independent variable, and so on. You can create and edit these matrices in 
   the stack with or without storing them. I recommend storing them to be on the safe side.
2. Push matrix MY into the stack (if no in the stack already).
3. Push matrix MX into the stack (if no in the stack already) 
4. Start the program by executing the command XEQ "MLRAN"
5. The program prints the regression results.
6. To view or review the output on the display execute XEQ "VWRS". The program places the matrix
   MC on the stack and executes an EDIT command. The program halts while you are inspecting
   the matrix elements. Once you are done, press R/S to view the other results. Press R/S
   to view the next results. The program beeps when you there are no more results to display.
7. To clear the program's variables except variables MX, MY, and MC, execute the command XEQ 
   "CLRANV". Please remember that once you execute this step you cannot execute step 6 since
   the variables for the ANOVA table are deleted.
   
Note: The program stores various data in the following variables:

MY	Dependent variable's observations.
MX	Independent variable's observations.
MC	Regression coefficients.
R2	Coefficient of determination.
R2adj	Adjusted value of R2
SSR	Sum of squares to due regression
SSE	Sum of squares due to error
SST	Total sum of squares of dependent variable.
DFR	Degrees of freedom for regression
DFE	Degrees of freedom of error
DFT	Total degrees of freedom
MSR	Mean sum of residuals
MSE	Mean sum of errors
FSTAT	F statistics

EXAMPLE:

Given the following matrix of observations:

X 	Y 	Z 	T
2	2	34	101.010
3	7	26	81.191
4	3	76	14.439
5	5	43	61.667
6	9	23	108.915
7	2	15	113.257


Fit the model:

T = c0 + c1 X + c2 Y + c3 Z


The column matrix MY is:


101.010
81.191
14.439
61.667
108.915
113.257


The matrix MX is:

1	2	2	34
1	3	7	26
1	4	3	76
1	5	5	43
1	6	9	23
1	7	2	15


Enter these matrices either in the stack (MY in register y and MX in register X) and 
then execute the program "MLR".

               XEQ "MLRAN"
MC=       [ 4x1 Matrix ]
1:1=            152.0517
2:1=             -0.9443
3:1=             -1.2403
4:1=             -1.7125
R2=               0.9182
R2ADJ=            0.7955
ANOVA TABLE
SSR=          6,445.0129
SSE=            574.2363
SST=          7,019.2492
DFR=              3.0000
DFE=              2.0000
DFT=              5.0000
MSR=          2,148.3376
MSE=            287.1182
FSTAT=            7.4824


The fitted model is:

T = 152.0517 -0.9443 X -1.2403 Y -1.7125 Z

The coefficient of determination for the above model and data is 0.9182.

To review the results on the LCD, execute command XEQ "VWRS". The output starts with an
EDIT command that allows you to inspect the column matrix for the regression coefficients.
Once you are done with the inspection, press R/S to view the other results as shown below:

              XEQ "VWRS"
1:1=152.0517
		       ->	(view next element)
2:1=-0.9443
                       ->	(view next element)\
3:1=-1.2403                       
                       ->	(view next element)
4:1=-1.7125

		     EXIT
		     
                     RUN
R2=0.9182
                     RUN
R2ADJ=0.7955
                     RUN
SSR=6,445.0129
                     RUN
SSE=574.2363
                     RUN
SST=7,019.2492
                     RUN
DFR=3.0000
                     RUN
DFE=2.0000
                     RUN
DFT=5.0000
                     RUN
MSR=2,148.3376
                     RUN
MSE=287.1182
                     RUN
FSTAT=7.4824
                     RUN
(audible beep)

MEMORY USAGE:

MY	Dependent variable's observations.
MX	Independent variable's observations.
MC	Regression coefficients.
R2	Coefficient of determination.
R2adj	Adjusted value of R2
SSR	Sum of squares to due regression
SSE	Sum of squares due to error
SST	Total sum of squares of dependent variable.
DFR	Degrees of freedom for regression
DFE	Degrees of freedom of error
DFT	Total degrees of freedom
MSR	Mean sum of residuals
MSE	Mean sum of errors
FSTAT	F statistics
N	Number of observations
K	Number of independent variables
MM	used
SY	used


LISTING:

 01>LBL "MLRAN"
 02 STO "MX"
 03 TRANS   
 04 X<>Y    
 05 STO "MY"
 06 ×       
 07 RCL "MX"
 08 TRANS   
 09 LASTX   
 10 ×       
 11 ÷       
 12 STO "MC"	# Calculate coefficients of regression
 13 RCL "MX"
 14 DIM?    
 15 1       
 16 -       
 17 STO "K" 	# Store number of independent variables
 18 X<>Y    
 19 STO "N" 	# Store number of observations
 20 RCL "MY"
 21 TRANS   
 22 RSUM    
 23 XEQ 00  
 24 X^2     
 25 RCL "N" 
 26 ÷       
 27 STO "SY"	# Store Sum(y)^2 / N
 28 RCL "MC"
 29 TRANS   
 30 RCL "MX"
 31 TRANS   
 32 ×       
 33 RCL "MY"
 34 ×       
 35 XEQ 00  
 36 RCL "SY"
 37 -       
 38 STO "SSR"	# Calculate SSR
 39 RCL "MY"
 40 TRANS   
 41 RCL "MY"
 42 ×       
 43 XEQ 00  
 44 RCL "SY"
 45 -       
 46 STO "SST"	# Calculate SST
 47 RCL "SSR"
 48 -       
 49 STO "SSE"	# Calculate SSE
 50 RCL "SSR"
 51 RCL "SST"
 52 ÷       
 53 STO "R2"	# Calculate R2
 54 +/-     
 55 1       
 56 +       
 57 RCL "N" 
 58 1       
 59 -       
 60 ×       
 61 RCL "N" 
 62 1       
 63 -       
 64 RCL "K" 
 65 -       
 66 ÷       
 67 +/-     
 68 1       
 69 +       
 70 STO "R2ADJ"	# Calculate R2adj
 71 RCL "K" 
 72 STO "DFR"	# Calculate DF residuals 
 73 +/-     
 74 1       
 75 -       
 76 RCL "N" 
 77 +       
 78 STO "DFE"	# Calculate DF error
 79 RCL "K" 
 80 +       
 81 STO "DFT"	# Calculate total DF 
 82 RCL "SSR"
 83 RCL "DFR"
 84 ÷       
 85 STO "MSR"	# Calculate MSR
 86 RCL "SSE"
 87 RCL "DFE"
 88 ÷       
 89 STO "MSE"	# Calculate MSE
 90 ÷       
 91 STO "FSTAT"	# Calculate F stat
 92 PRV "MC"	# Print results
 93 PRV "R2"
 94 PRV "R2ADJ"
 95 "ANOVA TABLE"
 96 PRA     
 97 PRV "SSR"
 98 PRV "SSE"
 99 PRV "SST"
100 PRV "DFR"
101 PRV "DFE"
102 PRV "DFT"
103 PRV "MSR"
104 PRV "MSE"
105 PRV "FSTAT"
106 RCL "R2"
107 RTN     

108>LBL 00  
109 STO "MM"
110 INDEX "MM"
111 RCLEL   
112 RTN     

113>LBL "CLRANV"
114 CLV "SSR"
115 CLV "MM"
116 CLV "K" 
117 CLV "SY"
118 CLV "N" 
119 CLV "SSE"
120 CLV "SST"
121 CLV "DFR"
122 CLV "DFE"
123 CLV "DFT"
124 CLV "MSR"
125 CLV "MSE"
126 CLV "FSTAT"
127 CLV "R2"
128 CLV "R2ADJ"
129 .END.   

130>LBL "VWRS"
131 RCL "MC"
132 EDIT    
133 STOP    
134 "R2="   
135 ARCL "R2"
136 PROMPT  
137 "R2ADJ="
138 ARCL "R2ADJ"
139 PROMPT  
140 "SSR="  
141 ARCL "SSR"
142 PROMPT  
143 "SSE="  
144 ARCL "SSE"
145 PROMPT  
146 "SST="  
147 ARCL "SST"
148 PROMPT  
149 "DFR="  
150 ARCL "DFR"
151 PROMPT  
152 "DFE="  
153 ARCL "DFE"
154 PROMPT  
155 "DFT="  
156 ARCL "DFT"
157 PROMPT  
158 "MSR="  
159 ARCL "MSR"
160 PROMPT  
161 "MSE="  
162 ARCL "MSE"
163 PROMPT  
164 "FSTAT="
165 ARCL "FSTAT"
166 PROMPT  
167 BEEP    
168 RTN     
169 .END.   

LABEL USAGE:

MLRAN	Main program
CLRANV	Clear ANOVA and miscellaneous variables
VWRS	View results
00	Used

BACK

Copyright (c) Namir Shammas. All rights reserved.