      PROGRAM LSQUAR
************************************************************************
* Program to find the equation of the least squares line for a set     *
* of data points.  Variables used are:                                 *
*     X, Y    : (X,Y) is the observed data point                       *
*     COUNT   : number of data points                                  *
*     SUMX    : sum of the Xs                                          *
*     SUMX2   : sum of the squares of the Xs                           *
*     SUMY    : sum of the Ys                                          *
*     SUMXY   : sum of the products X*Y                                *
*     XMEAN   : mean of the Xs                                         *
*     YMEAN   : mean of the Ys                                         *
*     SLOPE   : slope of least squares line                            *
*     YINT    : y-intercept of the line                                *
*                                                                      *
* Input:  A collection of data points (X, Y)                           *
* Output: The equation of the least squares line                       *
************************************************************************

      INTEGER COUNT
      REAL X, Y, SUMX, SUMX2, SUMY, SUMXY, XMEAN, YMEAN, SLOPE, YINT

* Initialize counter and the sums to 0 and read first data point

      COUNT = 0
      SUMX = 0
      SUMX2 = 0
      SUMY = 0
      SUMXY = 0
      PRINT *, 'ENTER POINT (-999, -999 TO STOP)'
      READ *, X, Y

* While there is more data, calculate the necessary sums
* and read the next data point (X, Y)

10    IF ((X .NE. -999.) .AND. (Y .NE. -999.)) THEN
          COUNT = COUNT + 1
          SUMX = SUMX + X
          SUMX2 = SUMX2 + X ** 2
          SUMY = SUMY + Y
          SUMXY = SUMXY + X * Y
          PRINT *, 'ENTER NEXT POINT'
          READ *, X, Y
      GO TO 10
      END IF

* Find equation of least squares line

      XMEAN = SUMX / COUNT
      YMEAN = SUMY / COUNT
      SLOPE = (SUMXY - SUMX * YMEAN) / (SUMX2 - SUMX * XMEAN)
      YINT = YMEAN - SLOPE * XMEAN
      PRINT *
      PRINT *, 'EQUATION OF LEAST SQUARES LINE IS Y = MX + B, WHERE'
      PRINT *, 'SLOPE = M =      ', SLOPE
      PRINT *, 'Y-INTERCEPT = B =', YINT
      END

