01-28-2019, 12:32 AM
Linear Regression with a User Keyboard
This program creates a user keyboard with the following keys defined:
[ I ] Clears the statistics array and variables
[ A ] Add a data point X,Y: the sample size is shown
[ D ] Delete a data point X,Y: the sample size is shown
[ M ] Sample Mean for X and Y data. Press CONT for mean of Y values.
[ S ] Sums for X and Y data. Press CONT for sum of Y values.
[ E ] Standard Deviation for X and Y data. Press CONT for standard dev. of Y values.
[ R ] Execute Linear Regression. A = intercept, B = slope, correlation is calculated, fit to the line Y = A + B*X
[ H ] Help: cycles what all the keys do
[ X ] Clears all the user keys, prepares you to go to the next program
PROGRAM STATLIN
689 bytes, 1/26/2019
Example to try:
Data: X,Y
-5.56, 0.79
-4.30, 0.84
1.86, 1.93
2.24, 2.01
3.95, 2.26
To add data: (in USER mode) [ A ] -5.56, 0.79 [END LINE] (repeat for all data points)
Results: n = 5
Mean:
[ M ] "X-BAR=" -0.362 [ f ] [ + ] (CONT) "Y-BAR=" 1.566
Sums:
[ S ] "∑X=" -1.81 [ f ] [ + ] (CONT) "∑Y=" 7.83
Standard Deviations:
[ E ] "sX=" 4.26696847891 [ f ] [ + ] (CONT) "sY=" 0.696512742166
Linear Regression:
[ R ] "INT=" 1.62489805306 [ f ] [ + ] (CONT)
"SLP=" 0.162701804029 [ f ] [ + ] (CONT)
"r=" 0.996741950615 [ f ] [ + ] (CONT)
"Y=1.6248905306 + 0.162701804029 X"
This program creates a user keyboard with the following keys defined:
[ I ] Clears the statistics array and variables
[ A ] Add a data point X,Y: the sample size is shown
[ D ] Delete a data point X,Y: the sample size is shown
[ M ] Sample Mean for X and Y data. Press CONT for mean of Y values.
[ S ] Sums for X and Y data. Press CONT for sum of Y values.
[ E ] Standard Deviation for X and Y data. Press CONT for standard dev. of Y values.
[ R ] Execute Linear Regression. A = intercept, B = slope, correlation is calculated, fit to the line Y = A + B*X
[ H ] Help: cycles what all the keys do
[ X ] Clears all the user keys, prepares you to go to the next program
PROGRAM STATLIN
689 bytes, 1/26/2019
Code:
10 DEF KEY "I", 'RUN 110'
20 DEF KEY "A", 'RUN 200'
30 DEF KEY "D", 'RUN 300'
40 DEF KEY "M", 'RUN 400'
45 DEF KEY "S", 'RUN 440'
50 DEF KEY "E", 'RUN 500'
60 DEF KEY "R", 'RUN 600'
70 DEF KEY "X", 'RUN 700'
75 DEF KEY "H", 'RUN 750'
80 USER ON
90 END
110 DESTROY S,A,B,X,Y,H$,I
120 STAT S(2) @ PRINT "ALL CLEAR"
130 END
200 INPUT "+ X,Y:";X,Y
210 ADD X,Y & DISP "N=";TOTAL(0)
220 END
300 INPUT "- X,Y:";X,Y
310 DROP X,Y & DISP "N=";TOTAL(0)
320 END
400 PRINT "X-BAR=";MEAN(1) @ PAUSE
420 PRINT "Y-BAR=";MEAN(2)
430 END
440 PRINT CHR$(28);"X=";TOTAL(1) @PAUSE
450 PRINT CHR$(28);"Y=";TOTAL(2)
460 END
500 PRINT "sX:";SDEV(1) @ PAUSE
510 PRINT "sY:";SDEV(2)
520 END
600 LR 2,1,A,B
610 PRINT "INT=";A @ PAUSE
620 PRINT "SLP=";B @ PAUSE
630 PRINT "r=";CORR(2,1) @ PAUSE
640 PRINT "Y=";A;"+";B;"X"
650 END
700 PURGE KEYS @ USER OFF
710 PRINT "EXIT COMPLETE"
750 FOR I=1 TO 8
760 READ H$ @ PRINT H$ @ WAIT 1
770 DATA "I: CLEAR","A: ADD","D: DEL","M: MEAN","S: SUMS","E: SDEV","R: A+BX","X: EXIT"
780 NEXT I
790 PRINT "READY." @ END
Example to try:
Data: X,Y
-5.56, 0.79
-4.30, 0.84
1.86, 1.93
2.24, 2.01
3.95, 2.26
To add data: (in USER mode) [ A ] -5.56, 0.79 [END LINE] (repeat for all data points)
Results: n = 5
Mean:
[ M ] "X-BAR=" -0.362 [ f ] [ + ] (CONT) "Y-BAR=" 1.566
Sums:
[ S ] "∑X=" -1.81 [ f ] [ + ] (CONT) "∑Y=" 7.83
Standard Deviations:
[ E ] "sX=" 4.26696847891 [ f ] [ + ] (CONT) "sY=" 0.696512742166
Linear Regression:
[ R ] "INT=" 1.62489805306 [ f ] [ + ] (CONT)
"SLP=" 0.162701804029 [ f ] [ + ] (CONT)
"r=" 0.996741950615 [ f ] [ + ] (CONT)
"Y=1.6248905306 + 0.162701804029 X"