REM *----------------------------------------------*
REM *              LINEAR REGRESSION		   *
REM *		   -----------------		   *
REM *	UPDATED: January 10, 1983		   *
REM *	COPYRIGHT: E.A. Valenzuela		   *
REM *	VERSION: 1.30				   *
REM *----------------------------------------------*

REM ... INPUT VARIABLES ...

REM	XR ... INDEPENDENT VARIABLE
REM	YR ... DEPENDENT VARIABLE
REM	NR ... NUMBER OF REGRESSION POINTS

REM ... OUTPUT VARIABLES ...

REM     XAVG ... AVERAGE OF X
REM     YAVG ... AVERAGE OF Y
REM     XSD  ... STANDARD DEVIATION OF X
REM     YSD  ... STANDARD DEVIATION OF Y
REM     VX   ... VARIATION IN X
REM     VY   ... VARIATION IN Y
REM     R    ... CORRELATION COEFFICIENT OF X WITH Y
REM     R2   ... R-SQUARED
REM     A    ... INTERCEPT 
REM     B    ... SLOPE
REM     SA   ... STANDARD ERROR OF INTERCEPT
REM     SB   ... STANDARD ERROR OF SLOPE
rem     see  ... standard error of the estimate
  
9100 rem ... set up summation counters ...
  sx  = 0
  sy  = 0
  sy2 = 0
  sx2 = 0
  sxy = 0

rem ... accumulate sums ...

 for i%=1 to nr%
  sx  = sx  + xr(i%)
  sx2 = sx2 + xr(i%)*xr(i%)
  sy  = sy  + yr(i%)
  sy2 = sy2 + yr(i%)*yr(i%)
  sxy = sxy + xr(i%)*yr(i%)
 next i%

9110 rem ... calculate averages ...

xavg = sx/nr%
yavg = sy/nr%

rem ... store useful parameters ...

t1 = sxy - xavg*yavg*NR% : rem b*t1 = sum of square about regression
t2 = sx2 - xavg*xavg*NR%  
t3 = sy2 - yavg*yavg*NR% : rem t3 = sum of squares about mean

rem ...determine slope and intercept...

b = t1 / t2
a = yavg - b*xavg

rem ... calculate variances ...

vx = t2 / (nr%-1)
vy = t3 / (nr%-1)

rem ...test for negative square root ...
rem ... calculate standard deviation of variables ...

XSD = SQR(vx)
YSD = SQR(vy)

rem ...standard error of estimate...(standard deviation)

see = SQR(( t3 - b*t1 ) / (nr%-2))

rem ... standard error of the slope ...

sb = see / SQR( sx2 - nr%*xavg*xavg)

rem ... standard error of the intercept ...

SA =  SB * SQR( SX2 / NR% )

rem ... calculate correlation coefficient and "r-squared"

r2 = b*t1 / t3

r = SQR(R2)

return

REM ---------------- END OF REGRESSION ---------------------
