home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
World of Ham Radio 1997
/
WOHR97_AmSoft_(1997-02-01).iso
/
antenna
/
ant_21
/
prog
/
linreg.bas
< prev
next >
Wrap
BASIC Source File
|
1997-02-01
|
5KB
|
171 lines
REM LINREG.BAS
cls: PRINT : PRINT " This program fits a straight line to a given set of coordinates"
PRINT " using the method of least squares. The equation of the line,"
PRINT " coefficient of determination, coefficient of correlation and standard"
PRINT " error of estimate are printed. Once the line has been fitted, you may"
PRINT " predict values of y for given values of x."
PRINT
PRINT " From SOME COMMON BASIC PROGRAMS, 3rd edition, by Lon Poole and"
PRINT " Mary Borchers, Osborne/McGraw-Hill, Berkeley CA, 1979. The authors"
PRINT " state that the programs had been tested on a Wang 2200 and the"
PRINT " Commodore PET (ancient history!). Adapted where necessary to"
PRINT " Microsoft QuickBasic 4.5, and modified for more aesthetic and useful"
PRINT " output (with attention to curve-fitting for programmers) and compiled"
PRINT " by Orrin C. Winton WN1Z, 11/94 and 11/95."
PRINT
PRINT " The plot routine came from PROGRAMMING WITH BASIC 3rd ed., by"
PRINT " Byron S. Gottfried, McGraw-Hill, NY, 1986, extensively modified by"
PRINT " O.C. Winton. The menu program is from ADVANCED QuickBASIC, by Ken"
PRINT " Knecht, Scott, Foresman and Company, Glenview, Illinois, 1989"
PRINT " and is modified only slightly."
PRINT
PRINT " linear regression"
PRINT
PRINT " number of known points: ";
INPUT n
DIM x(30)
DIM y(30)
DIM o(30)
DIM p(30)
j = 0
k = 0
l = 0
m = 0
r2 = 0
REM loop to enter coordinates of points
FOR i = 1 TO n
PRINT " x,y of point"; i;
INPUT x(i), y(i)
REM accumulate intermediate sums
j = j + x(i)
k = k + y(i)
l = l + x(i) ^ 2
m = m + y(i) ^ 2
r2 = r2 + x(i) * y(i)
NEXT i
REM compute curve coefficient
b = (n * r2 - k * j) / (n * l - j ^ 2)
a = (k - b * j) / n
PRINT
PRINT " f(x) = ("; b; "* x) + "; a
REM compute regression analysis
j = b * (r2 - j * k / n)
m = m - k ^ 2 / n
k = m - j
PRINT
r2 = j / m
PRINT " coefficient of determination (r^2) = "; r2
PRINT " coefficient of correlation = "; SQR(r2)
IF k < 0 OR (n - 2) <= 0 THEN GOTO d
PRINT " standard error of estimate = "; SQR(k / (n - 2)): GOTO e
d: PRINT " standard error of estimate = "
e: PRINT
REM estimate y-coordinates of points with entered x-coordinates
INPUT " do you wish to do interpolation? enter 'y' for yes"; y$
IF y$ = "y" THEN
PRINT " enter -999 to quit interpolation"
GOTO b
ELSE GOTO c
END IF
b: PRINT " x = ";
INPUT x
IF x = -999 THEN GOTO c
REM restart or end program?
PRINT " y = "; a + b * x
PRINT
GOTO b
c: REM
2115 ' ******* Prepare to plot regression-equation y-values against
2116 ' user-input x-values. Not user-input y-values.
2117 '
2120 FOR i = 1 TO n
2122 o(i) = x(i)
2124 NEXT i
2126 FOR i = 1 TO n
2128 p(i) = b * (o(i)) + a
2130 NEXT i
2270 ' ******* Find Largest and Smallest X and Y (the user-input x,y values)
2280 '
2290 XMAX = -100000!: YMAX = -100000!: XMIN = 100000!: YMIN = 100000!
2300 FOR i = 1 TO n
2310 IF x(i) > XMAX THEN XMAX = x(i)
2320 IF y(i) > YMAX THEN YMAX = y(i)
2330 IF x(i) < XMIN THEN XMIN = x(i)
2340 IF y(i) < YMIN THEN YMIN = y(i)
2350 NEXT i
2360
2370 ' ******* Scale the Xs and Ys
2380 '
2390 FOR i = 1 TO n
2400 x(i) = 29 + INT(280 * (x(i) - XMIN) / (XMAX - XMIN))
2410 y(i) = 189 - INT(150 * (y(i) - YMIN) / (YMAX - YMIN))
2420 NEXT i
2470 ' ******* Plot the Graphical Display of user-input x and y values
2480 '
2490 SCREEN 9
2500 LINE (19, 29)-(19, 199): LINE -(319, 199)
2510 FOR IY = 59 TO 179 STEP 20: LINE (19, IY)-(23, IY): NEXT IY
2520 FOR IX = 39 TO 299 STEP 20: LINE (IX, 199)-(IX, 195): NEXT IX
2530 '
2540 FOR i = 1 TO n
2550 PSET (x(i), y(i)): LINE (x(i), y(i) - 2)-(x(i) - 4, y(i) + 2)
2560 LINE -(x(i) + 4, y(i) + 2): LINE -(x(i), y(i) - 2)
2570 NEXT i
2580 '
2590 FOR i = 1 TO n - 1
2600 LINE (x(i), y(i))-(x(i + 1), y(i + 1))
2610 NEXT i
2770 ' ******* Find Largest and Smallest o and p
2780 '
2790 OMAX = -100000!: PMAX = -100000!: OMIN = 100000!: PMIN = 100000!
2800 FOR i = 1 TO n
2810 IF o(i) > OMAX THEN OMAX = o(i)
2820 IF p(i) > PMAX THEN PMAX = p(i)
2830 IF o(i) < OMIN THEN OMIN = o(i)
2840 IF p(i) < PMIN THEN PMIN = p(i)
2850 NEXT i
2860
2870 ' ******* Scale the Os and Ps
2880 '
2890 FOR i = 1 TO n
2900 o(i) = 29 + INT(280 * (o(i) - OMIN) / (OMAX - OMIN))
2910 p(i) = 189 - INT(150 * (p(i) - PMIN) / (PMAX - PMIN))
2920 NEXT i
3000 ' ******* Plot the regression equation y-values against
3002 ' user-input x values. Not user-input y values.
3010 '
3015 COLOR 4
3020 FOR i = 1 TO (n - 1)
3030 LINE (o(i), p(i))-(o(i + 1), p(i + 1))
3040 NEXT i
3045 COLOR 7
3050 PRINT " f(x) = ("; b; "* x) + "; a
3060 PRINT : PRINT : PRINT : PRINT : PRINT : PRINT : PRINT : PRINT : PRINT : PRINT : PRINT : PRINT : PRINT : PRINT : PRINT : PRINT
3064 PRINT " White curve w/markers: your x,y inputs."
3066 COLOR 4: PRINT " Red curve is eqn's y based on your x-inputs."
3068 PRINT " Eqn's fit is close to perfect if red line overwrites white line."
3069 COLOR 2
3070 INPUT " hit any key to end LINEAR REGRESSION sub-program", dummy
3080 RUN "menu"
3099 END