Linear Regression

See all articles from SYNC v2 n1

The “Linear Regression” Program computes the linear relationship between two sets of variables, expressed as the linear regression equation, and calculates the coefficient of determination, an indicator of the strength of the relationship. Given a set of two variables labelled X and Y, the program will yield an equation describing Y as a function of X.

These variables can be taken from any situation in which a logical relationship is expected, such as rainfall and crop yield, the prime interest rate and auto sales, or time and any variable which changes (generally in one direction) over a period of time. For a time series, X can be expressed in periods, starting with period 1.

The coefficient of determination, R2 or R squared, is a measure of how much of the variability in Y is “explained” by, or related to, the variability in X. R2 varies between 0 and 1, and R2 multiplied by 100 gives a percent indication of the validity of, or accuracy in, expressing Y as a function of X.

For a quick example, let Y = 1 + 2 * X. If X = 1, 2, and 3, then Y would be 3, 5, and 7. Run the program, enter a 3 in response to the number of entries, then enter X’s and Y’s pairwise, or, to mix things up a bit, enter 2, 5, 3, 7 and 1, 3. You should get the equation Y = 1 + 2 * X back, and an R2 of 1, or 100%, since the equation perfectly describes the relationship between each pair of entries.

Linear regression can be used to approximate the value of one variable (given the value of another), identify the trend in time series and forecast future values, or evaluate the influence of one variable on another (R2).

  10 REM LINEAR REG
20 PRINT "N OF ITEMS?"
30 INPUT N
40 LET SX=0
50 LET SY=0
60 LET XX=0
70 LET YY=0
80 LET XY=0
90 PRINT "INPUT X/S AND Y/S"
100 FOR A=1 TO N
110 IF A>20 THEN SCROLL
120 INPUT X
130 PRINT X;" ";
140 INPUT Y
150 PRINT Y
160 LET SX=SX+X
170 LET SY=SY+Y
180 LET XX=XX+ABS X**2
190 LET YY=YY+ABS Y**2
200 LET XY=XY+X*Y
210 NEXT A
220 CLS
230 PRINT
240 PRINT "Y = ";(XX*SY-SX*XY)/
(N*XX-ABS SX**2);" + ";(N*XY-SX*
SY)/(N*XX-ABS SX**2);" * X"
250 PRINT
260 PRINT "R2 = ";ABS ((N*XY-SX
*SY)/((N*XX-ABS SX**2)**.5*(N*YY
-ABS SY**2)**.5))**2

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.