home *** CD-ROM | disk | FTP | other *** search
/ RBBS in a Box Volume 1 #3.1 / RBBSIABOX31.cdr / rock / regress.bas < prev    next >
BASIC Source File  |  1984-06-23  |  6KB  |  128 lines

  1. 10 ' Programme to calculate correlation coeffiecnt and best fit line
  2. 20 ' through a set of data points by Mark A. Herkommer
  3. 30 ' Programme is written for use on a IBM PC running Basica.
  4. 40 '
  5. 50 OPTION BASE 1:KEY OFF:DEFINT I-N:SCREEN 0,0,0
  6. 60 '
  7. 70 DIM X(1000),Y(1000)
  8. 80 '
  9. 90 CLS:PRINT STRING$(34,"=");" REGRESS.BAS ";STRING$(33,"=")
  10. 100 PRINT "Mark A. Herkommer":PRINT "4005 Burning Tree Lane":PRINT "Garland, TX
  11. 110 PRINT
  12. 120 PRINT "REGRESS.BAS is a program for the IBM PC written in BASICA language.
  13. 130 PRINT "The program calculates the best-fit line through a set of XY data
  14. 140 PRINT "points by the method of linear least-squares.  The best fitting
  15. 150 PRINT "line is the regression curve of Y on X (that is, Y is the dependent
  16. 160 PRINT "variable and X is the independent variable).  Standard Error,
  17. 170 PRINT "Standard Deviation, and the Correlation Coefficent are determined.
  18. 180 PRINT "The equation of the line in slope-intercept form is :":PRINT
  19. 190 PRINT TAB(19)"Y = slope * X + Y-intercept  (Y = m*X + b)":PRINT
  20. 200 PRINT "Up to 1000 XY data points can be used in the calculation, however,
  21. 210 PRINT "input data must be in a sequential disk file and in ASCII format.
  22. 220 PRINT "Use shift-Prtsc to have the statistics printed on your line printer.
  23. 230 PRINT "After the statistics are calculated, press any key to make a graph.
  24. 240 PRINT "When the graph is finished (indicated with a beep), press any key to
  25. 250 PRINT "end the program.":LOCATE 25,1
  26. 260 INPUT "Enter the name of the file that holds the X,Y data  : ",INFIL$
  27. 270 '
  28. 280 ' read input data and calculate sums
  29. 290 '
  30. 300 XMIN=1E+32:XMAX=-1E+32:YMIN=1E+32:YMAX=-1E+32
  31. 310 IF INFIL$="" THEN 90
  32. 320 OPEN INFIL$ FOR INPUT AS #1
  33. 330 IF EOF(1) THEN 420 ELSE I=I+1
  34. 340 INPUT #1,X(I),Y(I)
  35. 350 SUMX=SUMX+X(I):SUMY=SUMY+Y(I)
  36. 360 IF X(I)>XMAX THEN XMAX=X(I):INDXMAX=I
  37. 370 IF X(I)<XMIN THEN XMIN=X(I):INDXMIN=I
  38. 380 IF Y(I)>YMAX THEN YMAX=Y(I)
  39. 390 IF Y(I)<YMIN THEN YMIN=Y(I)
  40. 400 SUMXY=SUMXY+X(I)*Y(I):SUMXX=SUMXX+X(I)*X(I):SUMYY=SUMYY+Y(I)*Y(I)
  41. 410 GOTO 330
  42. 420 CLOSE #1:CLS:NP=I
  43. 430 XMEAN=SUMX/NP:YMEAN=SUMY/NP
  44. 440 '
  45. 450 PRINT STRING$(80,"=")
  46. 460 PRINT "Total number of points read from ";INFIL$;TAB(57)"="NP:PRINT
  47. 470 PRINT "X minimum        =";XMIN;TAB(40);"Y minimum        =";YMIN
  48. 480 PRINT "X maximum        =";XMAX;TAB(40);"Y maximum        =";YMAX
  49. 490 PRINT "Range  of X      =";XMAX-XMIN;TAB(40);"Range of Y       =";YMAX-YMIN
  50. 500 PRINT "Mean value of X  =";XMEAN;TAB(40);"Mean value of Y  =";YMEAN
  51. 510 '
  52. 520 ' Linear least-squares fit of the data
  53. 530 '
  54. 540 PRINT:PRINT STRING$(80,"="):PRINT "Equation of the best fit line =====>";
  55. 550 SLOPE=(NP*SUMXY-SUMX*SUMY)/(NP*SUMXX-SUMX*SUMX)      ' slope of line
  56. 560 B=YMEAN-SLOPE*XMEAN                                  ' y-intercept
  57. 570 PRINT TAB(40)"Y = ";SLOPE;"* X +";B
  58. 580 '
  59. 590 ' Calculate Standard deviation and Standard Error
  60. 600 '
  61. 610 FOR I=1 TO NP
  62. 620 YC=SLOPE*X(I)+B     ' calculated y-value
  63. 630 D=Y(I)-YC           ' deviation
  64. 640 SUMDD=SUMDD+D*D     ' sum of the deviation squared
  65. 650 DX=X(I)-XMEAN:SUMDXDX=SUMDXDX+DX*DX    ' X-deviation squared
  66. 660 DY=Y(I)-YMEAN:SUMDYDY=SUMDYDY+DY*DY    ' Y-deviation squared
  67. 670 SUMDXDY=SUMDXDY+DX*DY
  68. 680 NEXT
  69. 690 STDERR=SQR(SUMDD/(NP-1))                        ' standard error
  70. 700 SIGMAX=SQR((SUMXX-SUMX*SUMX/NP)/(NP-1))         ' standard deviation of X
  71. 710 SIGMAY=SQR((SUMYY-SUMY*SUMY/NP)/(NP-1))         ' standard deviation of Y
  72. 720 IF SIGMAY=0 THEN PRINT "Y values are invariate-R is undefined":GOTO 800
  73. 730 R=SUMDXDY/SQR(SUMDXDX*SUMDYDY)
  74. 740 PRINT
  75. 750 PRINT STRING$(80,"="):PRINT "Elementary statistics on the best fit line"
  76. 760 PRINT:PRINT "Standard Error              =";STDERR
  77. 770 PRINT "Standard Deviation of X     =";SIGMAX
  78. 780 PRINT "Standard Deviation of Y     =";SIGMAY
  79. 790 PRINT "Correlation Coefficent      =";R
  80. 800 PRINT:PRINT STRING$(80,"=");
  81. 810 LOCATE 25,1:PRINT "press any key to produce a graph...";
  82. 820 I$=INKEY$:IF I$="" THEN 820 ELSE GOSUB 860
  83. 830 SCREEN 0,0,0:CLS:END
  84. 840 '
  85. 850 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  86. 860 ' Subroutine plots a graph of any XY data
  87. 870 '
  88. 880 SCREEN 2:CLS
  89. 890 '
  90. 900 ' Calculate increment and axis
  91. 910 '
  92. 920 XLEN=530:YLEN=176:NTICS=5
  93. 930 XMAX=INT(XMAX+.9990001):XMIN=INT(XMIN-.9990001)
  94. 940 XINC=(XMAX-XMIN)/XLEN
  95. 950 YTEMP=SLOPE*X(INDXMIN)+B:IF YTEMP<YMIN THEN YMIN=YTEMP ELSE:IF YTEMP>YMAX       THEN YMAX=YTEMP
  96. 960 YTEMP=SLOPE*X(INDXMAX)+B:IF YTEMP<YMIN THEN YMIN=YTEMP ELSE:IF YTEMP>YMAX       THEN YMAX=YTEMP
  97. 970 YMAX=INT(YMAX+.9990001):YMIN=INT(YMIN-.9990001)
  98. 980 YINC=(YMAX-YMIN)/YLEN
  99. 990 '
  100. 1000 ' Plot axis
  101. 1010 '
  102. 1020 LINE (79,YLEN+3)-(79+XLEN,YLEN+3)                   ' x-axis
  103. 1030 LINE (79,YLEN+3)-(79,3):LINE (80,YLEN+3)-(80,3)     ' y-axis
  104. 1040 XLABINC=(XMAX-XMIN)/NTICS:YLABINC=(YMAX-YMIN)/NTICS
  105. 1050 FOR I=0 TO NTICS
  106. 1060 XLOC=79+I*(XLEN-1)/NTICS:LINE (XLOC,YLEN+4)-(XLOC,YLEN+6):XLOC=XLOC+1:          LINE (XLOC,YLEN+4)-(XLOC,YLEN+6)                    ' x-axis tic marks
  107. 1070 XLABLOC=6+I*(XLEN+1)/(8*NTICS):LOCATE 24,XLABLOC:                               PRINT USING "##.#^^^^";XMIN+XLABINC*I;
  108. 1080 YLOC=I*YLEN/NTICS:LINE (73,YLOC+3)-(79,YLOC+3)      ' y-axis tic marks
  109. 1090 YLABLOC=1+I*22/NTICS:LOCATE YLABLOC,1:PRINT USING "##.#^^^^";YMAX-YLABINC*I
  110. 1100 NEXT
  111. 1110 LOCATE 25,1:PRINT "Y / X";
  112. 1120 '
  113. 1130 ' Plot the points
  114. 1140 '
  115. 1150 FOR I=1 TO NP
  116. 1160 XLOC=((X(I)-XMIN)/(XMAX-XMIN))*XLEN+80
  117. 1170 YLOC=((YMAX-Y(I))/(YMAX-YMIN))*YLEN+3
  118. 1180 PSET (XLOC,YLOC)                            ' center of data point
  119. 1190 DRAW "U2 L1 D4 R1 U2 NL4 NR3"               ' data point symbol
  120. 1200 NEXT
  121. 1210 '
  122. 1220 ' Draw best fit line
  123. 1230 '
  124. 1240 PSET (((X(INDXMIN)-XMIN)/(XMAX-XMIN))*XLEN+80,((YMAX-(SLOPE*X(INDXMIN)+B))      /(YMAX-YMIN))*YLEN+4)
  125. 1250 LINE -(((X(INDXMAX)-XMIN)/(XMAX-XMIN))*XLEN+80,((YMAX-(SLOPE*X(INDXMAX)+B))     /(YMAX-YMIN))*YLEN+4)
  126. 1260 BEEP
  127. 1270 I$=INKEY$:IF I$="" THEN 1270 ELSE RETURN
  128. IN))*XLEN+80,((YMAX-(SL