program linreg (input, output); {*****************************************************************************} { } { 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 21, 2002 } { } { Language: Pascal } { } { Description: This program performs a linear regression analysis for a set } { of data given as (x,y) pairs. The output from the program } { is the slope and y-intercept of the least-squares best fit } { straight line through the data points. } { } {*****************************************************************************} {*****************************************************************************} { Global variables } {*****************************************************************************} label { label definition } 1; var b : real; { y-intercrpt of best-fit line } m : real; { slope of best-fit line } n : real; { number of data points } r : real; { squared correlation coeff } str : array [1..80] of char; { input string } sumx : real; { sum of x } sumx2 : real; { sum of x**2 } sumxy : real; { sum of x * y } sumy : real; { sum of y } sumy2 : real; { sum of y**2 } x : real; { input x data } y : real; { input y data } {*****************************************************************************} { Main program } {*****************************************************************************} begin {-----------------------------------------------------------------------------} { Print introductory message. } {-----------------------------------------------------------------------------} writeln('LINREG - Perform linear regression'); writeln(' (Enter x=-9999 to stop data entry ', 'and compute linear regression.)'); writeln; {-----------------------------------------------------------------------------} { Initialize sums. } {-----------------------------------------------------------------------------} n := 0.0; sumx := 0.0; sumx2 := 0.0; sumxy := 0.0; sumy := 0.0; sumy2 := 0.0; {-----------------------------------------------------------------------------} { Enter data and accumulate sums. } {-----------------------------------------------------------------------------} repeat { loop for all data points } writeln; write ('Enter x: '); { prompt for x } readln (x); { read in x } if x = -9999.0 then { if no more data.. } goto 1; { ..then break out of loop } write ('Enter y: '); { prompt for y } readln (y); { read in y } n := n + 1.0; { increment number of data points } sumx := sumx + x; { compute sum of x } sumx2 := sumx2 + sqr(x); { compute sum of sqr(x) } sumxy := sumxy + (x * y); { compute sum of x * y } sumy := sumy + y; { compute sum of y } sumy2 := sumy2 + sqr(y) { compute sum of sqr(y) } until (false); {-----------------------------------------------------------------------------} { Compute least-squares best fit straight line. } {-----------------------------------------------------------------------------} 1: m := (n * sumxy - sumx * sumy) / { compute slope } (n * sumx2 - sqr(sumx)); b := (sumy * sumx2 - sumx * sumxy) / { compute y-intercept } (n * sumx2 - sqr(sumx)); r := (sumxy - sumx * sumy / n) / { compute correlation coefficient } sqrt((sumx2 - sqr(sumx)/n) * (sumy2 - sqr(sumy)/n)); {-----------------------------------------------------------------------------} { Print results and return to operating system. } {-----------------------------------------------------------------------------} writeln; writeln ('Slope m = ', m:15:6); writeln ('y-intercept b = ', b:15:6); writeln ('Correlation r = ', r:15:6) end.