comment ********************************************************************** 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: February 4, 2002 Language: Algol-68 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. ************************************************************* comment co ---------------------------------------------------------------------------- Variable declarations ------------------------------------------------------------------------- co begin real b; # y-intercept of best fit line # real m; # slope of best fit line # real n := 0.0; # number of data points # real r; # correlation coefficient # real sumx := 0.0; # sum of x # real sumx2 := 0.0; # sum of x ** 2 # real sumxy := 0.0; # sum of x * y # real sumy := 0.0; # sum of y # real sumy2 := 0.0; # sum of y * y # real x; # input x data # real y; # input y data # co ---------------------------------------------------------------------------- Print introductory message ------------------------------------------------------------------------- co print (newline, "LINREG - Perform linear regression"); print (newline); print (" (Enter X=-9999 to stop data entry and compute linear regression."); co ---------------------------------------------------------------------------- Input data and accumulate sums ------------------------------------------------------------------------- co do print (newline); # start new line # print ("Enter x: ") # prompt for x # read (x); # read in x # if x = -9999 then goto done fi # if = -9999, then no more data # print ("Enter y: ") # prompt for y # read (y); # read in y # n plusab 1; # increment number of data points # sumx plusab x; # sum of x # sumx2 plusab x * x; # sum of x ** 2 # sumxy plusab x * y; # sum of x * y # sumy plusab y; # sum of y # sumy2 plusab y * y; # sum of y ** 2 # od co ---------------------------------------------------------------------------- Compute results ------------------------------------------------------------------------- co done: m := (n * sumxy - sumx * sumy) / # compute slope # (n * sumx2 - sumx*sumx); b := (sumy * sumx2 - sumx * sumxy) / # compute y-intercept # (n * sumx2 - sumx*sumx); r := (sumxy - sumx * sumy / n) / # compute correlation coefficient # sqrt((sumx2 - sumx*sumx/n) * (sumy2 - sumy*sumy/n)); co ---------------------------------------------------------------------------- Print results ------------------------------------------------------------------------- co print ("Slope m = ", m); print ("y-intercept b = ", b); print ("Correlation r = ", r) end