; ******************************************************************************************************************************* ; ; L I N R E G ; ; Program: LINREG ; ; Programmer: Dr. David G. Simpson ; Department of Physical Science ; Prince George's Community College ; Largo, Maryland 20774 ; ; Date: January 22, 2002 ; ; Language: IDL ; ; Description: This program performs a linear regression analysis for a set of data given as (X,Y) pairs. The output from ; the progrma is the slope and y-intercept of the least-squares best fit straight line through the data points. ; ; ******************************************************************************************************************************* PRINT, 'LINREG - Perform linear regression' ; print introductory message PRINT, ' (Enter END to stop data entry and compute linear regression.)' ; ; Initialize sums and "declare" variables ; B = 0.0 ; y-intercpt of least-squares best fit line M = 0.0 ; slope of least-squares best fit line N = 0.0 ; number of data points R = 0.0 ; squared correlation coefficient STR = '' ; input string SUMX = 0.0 ; sum of X SUMX2 = 0.0 ; sum of X**2 SUMXY = 0.0 ; sum of X * Y SUMY = 0.0 ; sum of Y SUMY2 = 0.0 ; sum of Y**2 X = 0.0 ; input X data Y = 0.0 ; input Y data ; ; Accumulate sums ; WHILE (1) DO BEGIN ; loop for all data points READ, PROMPT='Enter X Y: ', FORMAT='(A)', STR ; read input data IF ((STR EQ 'END') OR (STR EQ 'end')) THEN GOTO, DONE ; check if no more data READS, STR, X, Y ; read data from string N = N + 1.0 ; increment number of data points by 1 SUMX = SUMX + X ; compute sum of X SUMX2 = SUMX2 + X * X ; compute sum of X**2 SUMXY = SUMXY + X * Y ; compute sum of X * Y SUMY = SUMY + Y ; compute sum of Y SUMY2 = SUMY2 + Y * Y ; compute sum of Y**2 ENDWHILE DONE: M = (N * SUMXY - SUMX * SUMY) / (N * SUMX2 - SUMX^2) ; compute slope B = (SUMY * SUMX2 - SUMX * SUMXY) / (N * SUMX2 - SUMX^2) ; compute y-intercept R = (SUMXY - SUMX * SUMY / N) / $ ; compute correlation coefficient SQRT((SUMX2 - SUMX^2/N) * (SUMY2 - SUMY^2/N)) PRINT, FORMAT='("Slope m = ", E15.6)', M ; print results PRINT, FORMAT='("y-intercept b = ", E15.6)', B PRINT, FORMAT='("Correlation r = ", E15.6)', R END