CALCLR.OPL Linear regression functions coded in OPL for use from Psion Series3 calculator application. I am making these routines freely available for non-commercial use in the hope that they will be useful. I retain the copyright. Daniel James. March 1998. Calculator memories M2..M9 are used. M9 holds sigma(x*y) M8 holds sigma(y^2) M7 holds sigma(x^2) M6 holds sigma(y) M5 holds sigma(x) M4 holds N M3 holds the slope of the line M2 holds the intercept of the line (the y coordinate of the point where the line crosses the x axis). PROC calclr: This is the top-level function. This should not be called from the calculator but if CALCLR.OPO is accidentally run as an application this proc will display a message. PROC pt:(x,y) This is called from the calculator to add a new data point(x,y). The proc updates the running totals in memories M4..M9 PROC delpt:(x,y) This is called from the calculator to remove an incorrectly entered data point (x,y). PROC mx: Returns the mean of the x coordinate values. PROC my: Returns the mean of the y coordinate values. PROC Clear: Clears the calculator memories M2..M9, deleting all data points and calculaton results. PROC LineFit: Computes the slope and intercept of the straight line representing the best fit to the coordinate set represented by the values in M4..M9. The return value is the correlation coefficient R. PROC FitX:(y) Returns the x coordinate of the point on the line at the specified y coordinate. You must call LineFit: Before FitX: or you will get a zero-divide error. PROC FitY:(x) Returns the y coordinate of the point on the line at the specified x coordinate. You must call LineFit: Before FitY: or you will get a result of zero. PROC sdevx: Returns the standard deviation of the x coordinate values The 'sample' standard deviation calculation (N-1 weight) is used. This proc is used by LineFit: to compute the correlation coefficient. PROC sdevy: returns the standard deviation of the y coordinate values. This proc is used by LineFit: to compute the correlation coefficient. Example of use -------------- To fit a line through 4 points: (1,4.9) (2,7.1) (3,10.0) (4,12.95) 1. Start the calculator application (use the diamond key to switch to the advanced calculator if necessary on the 3c) and use Psion+O to load the calclr.opo module. 2. Type "Clear:" at the calc prompt, this will clear memories 2..9. The calculator will display "=0", incicating that there are no points defined. NOTE that you have to type the colon after "Clear" - all OPL function names end with a colon. 3. Enter "pt:(1,4.9)" at the calc prompt. The calculator will display "=1", indicating that a point has been defined. Enter the remaining points in the same way. Again, note the colon. 4. Enter "LineFit:" at the calc: prompt to compute the slope and intercept of the best straight line to fit these points. The calculator will display "=0.997796705326", which is the correlation coefficient computed for the fit (the more closely the line fits the given points the nearer this value is to 1). The slope of the line will be stored in M3 and the intercept (the y coordinate of the point where the line crosses the x axis) in M2. To obtain the slope of the line enter "M3" at the calc prompt and the calculator will displat "=2.705", to obtain the intervept enter "M2" and the calculator will display "=1.975". Note that in this case you are recalling the value from a calculator memory not calling an OPL function so there is no colon to type. 5. To find the y value corresponding to x=10 enter "FitY:(10)" and the calculator will display "=29.025"; to find the x value for which y is 10 enter "fitx:(10)" and the calculator will display "=2.96672828096".