HP Forums

Full Version: (71B) Statistics: Linear, Exp., Log, Power, Inverse Regression
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Statistics: Regression

The program CURVEFIT fits data to one of five regression models:

1. Linear Regression: y = a + bx
2. Exponential Regression: y = a * e^(b*x)
3. Logarithm Regression: y = a + b * ln x
4. Power Regression: y = a * x^b
5. Inverse Regression: y = a + b/x

On the HP 71, the ln function is represented by LOG.

The program will allow different calculations with the same data set.

Code:
Program CURVEFIT (622 bytes)
10 DESTROY H,S,D,X,Y,A,B,E
12 STAT S(2) @ CLSTAT
20 REM CHOOSE REG
22 DISP “1. LIN 2. EXP 3. LOG” @ WAIT 1.5
24 DISP “4. POW 5. INV” @ WAIT 1.5
28 INPUT “CHOICE #:”; H
29 IF E=1 THEN 60
30 REM INPUT PROCESS
32 INPUT “X,Y:”; X,Y
34 ON H GOTO 36,38,40,42,44
36 ADD X,Y @ GOTO 46
38 ADD X,LOG(Y) @ GOTO 46
40 ADD LOG(X),Y @ GOTO 46
42 ADD LOG(X),LOG(Y) @ GOTO 46
44 ADD 1/X,Y @ GOTO 46
46 INPUT “DONE? (Y=1,N=0):”; D
48 IF D=0 THEN 32
60 LR 2,1,A,B
62 IF H=2 OR H=4 THEN A=EXP(A)
64 ON H GOTO 70,72,74,76,78
70 DISP A; “+”; B; “x” @ GOTO 80
72 DISP A; “*EXP(“; B; “x)” @ GOTO 80
74 DISP A; “+”; B; “*LOG(x)” @ GOTO 80
76 DISP A; “x^”; B @ GOTO 80
78 DISP A; “+”; B; “/x” @ GOTO 80
80 PAUSE
84 DISP “ANOTHER ANALYSIS?” @ WAIT 1.5
86 INPUT “Y=1,N=0:”; E
88 IF E=1 THEN 20
90 DISP “DONE”
HI,

Cool program, but missing the ability to compute predicted y^ from the regressions.
Also, for relatively long statistic series of data, answering DONE ? 0 [ENTER] after each input may be a bit boring.

So, since I fill free to modify Eddie's program adding in the way a predicting section, I now have to paid for it and share my version. Still expecting that this will inspire and ease future users to developed an even better or powerfuller version.


10 DESTROY A,B,I,N,R,X,Y @ DIM E$[43],X$[6],Y$[6],C$[5],D$[7] @ STAT S(2) @ CLSTAT
12 INPUT "1lin 2ex 3lg 4pw 5/x ";R @ ON R RESTORE 31,32,33,34,35 @ READ X$,Y$,C$,D$
14 INPUT "ADD#';N @ FOR I=1 TO N @ DISP I; @ INPUT "X,Y ";X,Y @ ADD VAL(X$),VAL(Y$) @ NEXT I
16 LR 2,1,A,B @ IF R=2 OR R=4 THEN A=EXP(A) ELSE IF B<0 THEN C$=""
18 E$=STR$(A)&C$&STR$(B)&D$ @ DISP "R2";CORR(2,1)^2 @ PAUSE @ DISP "Y=";E$ @ PAUSE
20 INPUT "y^ : (x)=";X @ DISP X;VAL(E$) @ PAUSE @ GOTO 20
31 DATA X,Y,+,*X
32 DATA X,LOG(Y),*EXP(,*X)
33 DATA LOG(X),Y,+,*LOG(X)
34 DATA LOG(X),LOG(Y),*X^,
35 DATA 1/X,Y,+,/X


Usage:
It the same usage as original version.
[RUN] to start the program and select which regression by entering 1 to 5 in the menu to select incrementally:
  1. Linear regression: data points lined up on normal scaled paper
    Equation: Y=a+b·X
  2. Exponential regression: data points lined up on lin-log scaled graph.
    Equation: Y=a·EXP(b·X)
  3. Logarithm Regression: data lined up on log-lin scaled graph.
    Equation: Y=a+b·LN(X)
  4. Power regression: data points only lined up on log-log scaled paper graph.
    Equation: Y=a·X^b
  5. Hyperbolic Regression: data points lined up when inverted 1/x abscise are use (never see a millimeter hyperbolic paper sheet)
    Equation: Y=a+b/X

Wen prompt, ADD# enter the number of data to add in the set.
When all data are entered, the R² coefficient of fit goodness is displayed . The more R² approaches one, the better your data points lined up on your paper with lin/log scales.
Press [f]+[CONT] to see the equation of regression Y= f(a,b,X).
Press [f]+[CONT] again to be prompt for Y^ prediction. Enter abscise (x), next display show (x) and predicted Y. Press [f]+[CONT] to compute further prediction or press [RUN] for a new regression.

P.S.: I am not sure that different calculations are allowed with the same data set.
One may add more data by typing RUN 14 [END LINE]. But changing regression type without restarting all the data entries may lead to erroneous results.
Currently developing a version with may carry regression in parallel so the user may select the better one depending of fitness R².
Reference URL's