home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / lambda / soundpot / p / stats1.lbr / CHISQ.BZS / CHISQ.BAS
Encoding:
BASIC Source File  |  1993-10-25  |  3.3 KB  |  130 lines

  1. rem This is a program for any chi square situation.
  2. var t,chitot,b,n,ac,bc,cc,dc,ab,num,den,chisq=real
  3. var o,e,totob,totex,totchi=real
  4. var a,k,r,df,j,m,c,d,dof=integer
  5. dim real tc(20) ob(20,20) tr(20) ex(20,20) diff(20,20)
  6. dim real chi(20,20) obs(20) expt(20) dif(20) chis(20)
  7. 10 print
  8. print "This is a program to calculate the value of chi"
  9. print "square.  If you wish to compute a simple chi square,"
  10. print "enter a 1.  If you are dealing with a 2x2 table, enter"
  11. print "a 2.  If you have a table which is larger than 2x2,"
  12. print "enter a 3.  To exit, enter a 4.  This program will"
  13. print "handle up to 20 rows and 20 columns."
  14. input2 a
  15. if a=1 then 20
  16. if a=2 then 30
  17. if a=4 then 40
  18. t=0
  19. chitot=0
  20. print
  21. print "How many columns are there?"
  22. input2 k
  23. print
  24. print "How many rows are there?"
  25. input2 r
  26. rem There are k columns (j) and r rows (m)
  27. df=(k-1)*(r-1)
  28. for j=1 to k
  29. tc(j)=0
  30. for m=1 to r
  31. print "In column ";j;" what is number ";m;"?"
  32. input2 b
  33. ob(j,m)=b
  34. tc(j)=tc(j)+b
  35. next m
  36. next j
  37. for m=1 to r
  38. tr(m)=0
  39. for j=1 to k
  40. tr(m)=tr(m)+ob(j,m)
  41. next j
  42. next m
  43. for j=1 to k
  44. t=t+tc(j)
  45. next j
  46. for j=1 to k
  47. for m=1 to r
  48. ex(j,m)=tc(j)*tr(m)/t
  49. next m
  50. next j
  51. print
  52. print "If you wish to have the answers on the console, enter a"
  53. print "zero.  Otherwise, enter a 1."
  54. input2 c
  55. print #c;"Column","Row","Observed","Expected","Difference","D*D/E"
  56. print #c;
  57. for j=1 to k
  58. for m=1 to r
  59. diff (j,m)=ob(j,m)-ex(j,m)
  60. chi (j,m)=diff(j,m)*diff(j,m)/ex(j,m)
  61. print #c;j,m,ob(j,m),ex(j,m),diff(j,m),chi(j,m)
  62. chitot=chitot+chi(j,m)
  63. next m
  64. next j
  65. print #c;
  66. print #c;"Chi Square = ";chitot;", Degrees of Freedom are ";df;"."
  67. go to 10
  68. 30 print "To calculate the value of chi square for a 2x2 table, enter"
  69. print "the number in the upper left first, then the number in the upper"
  70. print "right, then the number in the lower left, and finally the number"
  71. print "in the lower right.  Separate the numbers by commas."
  72. print "Enter them now."
  73. input2 ac,bc,cc,dc
  74. n=ac+bc+cc+dc
  75. ab=abs(ac*dc-bc*cc)
  76. num=n*(ab-n/2)*(ab-n/2)
  77. den=(ac+bc)*(cc+dc)*(ac+cc)*(bc+dc)
  78. chisq=num/den
  79. print
  80. print "If you wish to have the answers on the console, enter a"
  81. print "zero.  Otherwise, enter a 1."
  82. input2 c
  83. if c=0 then 50
  84. print #c;
  85. print #c;"The numbers are: ";ac;",";bc;",";cc;",";dc
  86. print #c;
  87. 50 print #c;"N","Abs Value","Numerator","Denomenator","Chi Square"
  88. print #c;
  89. print #c;n,ab,num,den,chisq
  90. print #c;
  91. print #c;"There is one degree of freedom."
  92. goto 10
  93. 20 totob=0
  94. totex=0
  95. totchi=0
  96. print "How many pairs of numbers are you dealing with?"
  97. input2 d
  98. print
  99. print "Enter each pair of numbers, with commas between them, the"
  100. print "observed first and then the expected."
  101. for j=1 to d
  102. print "Pair number ";j;" is:"
  103. input2 o,e
  104. obs(j)=o
  105. expt(j)=e
  106. totob=totob+obs(j)
  107. totex=totex+expt(j)
  108. dif(j)=obs(j)-expt(j)
  109. chis(j)=dif(j)*dif(j)/expt(j)
  110. totchi=totchi+chis(j)
  111. next j
  112. print
  113. print "If you wish to have the answer on the console, enter a"
  114. print "zero.  Otherwise, enter a 1."
  115. input2 c
  116. print #c;
  117. print #c;"Observed","Expected","Difference","Chi Square"
  118. print #c;
  119. for j=1 to d
  120. print #c;obs(j),expt(j),dif(j),chis(j)
  121. next j
  122. dof=d-1
  123. print #c;
  124. print #c;"The total value of chi square is ";totchi;".  There are"
  125. print #c;dof;" degrees of freedom."
  126. goto 10
  127. 40 end
  128.  
  129. ;cc;",";dc
  130.