1 '                       LINEAR REGRESSION
2 '               Written by Tracy L. Gustafson, M.D.
3 '              Round Rock, Texas. Version 3.0, 1984
4 ON ERROR GOTO 5000:CHAIN MERGE "EPIMRG",5
15 DIM D(1,1),CS(1,1),T(1),N$(1),X(1),X2(1),MD(1),SD(1),ST(1)
22 DATA "LINEAR REGRESSION",28,19
30 LOCATE 7,27:PRINT "1.)  Linear regression":PRINT
35 PRINT TAB(27);"2.)  Data transformations":PRINT
40 PRINT TAB(27);"3.)  Exit"
45 LOCATE 14,31:INPUT "Enter choice:  ",ASUB:IF ABS(ASUB-2)>1 THEN BEEP:GOTO 45
50 ON ASUB GOTO 55,195,385
55 CLS:PRINT TAB(28);DTTL:PRINT TAB(28);STRING$(17,205):PRINT:GOSUB 4000
60 PRINT:PRINT "   What are the SAMPLE NUMBERS of the 2 variables you want to compare?":PRINT TAB(10);"Predictor variable (X)";TAB(45);"Dependent variable (Y)"
65 AR=CSRLIN:AC=13:GOSUB 4200:NS1=NS:AC=48:GOSUB 4200:NS2=NS:PRINT
70 IF T(NS1)<>T(NS2) THEN BEEP:PRINT "These 2 samples do not have the same number of elements----":PRINT TAB(37);"a regression analysis cannot be performed.":FOR Z=1 TO 5000:NEXT:GOTO 20
75 XC=0:N=T(NS1):FOR Z=1 TO N:XC=XC+VAL(D(NS1,Z))*VAL(D(NS2,Z)):NEXT
80 SC=XC-X(NS1)*X(NS2)/N:SX=X2(NS1)-X(NS1)*X(NS1)/N
85 SY=X2(NS2)-X(NS2)*X(NS2)/N:SB=SC/SX:IA=(X(NS2)-SB*X(NS1))/N
90 COLOR CLR2,CLR1:PRINT TAB(13);"Regression equation:  Y =";IA;:IF SB>0 THEN PRINT "+"; ELSE PRINT "-";
95 PRINT ABS(SB);"* X";TAB(79):COLOR CLR1,CLR2:PRINT:PRINT
100 V1=N-2:S2=(SY-SC*SC/SX)/V1:SEB=SQR(S2/SX):ST=ABS(SB)/SEB
105 PRINT " Significance of slope:    T =";ST;TAB(45);"df =";V1;
110 R=ATN(ST/SQR(V1)):RC=COS(R):R2=RC*RC:RS=SIN(R):X=1
115 IF V1 MOD 2=0 THEN 140
120 IF V1=1 THEN Y=R:GOTO 135
125 Y=RC:FOR Z=3 TO (V1-2) STEP 2:X=X*R2*(Z-1)/Z:Y=Y+X*RC:NEXT
130 Y=R+RS*Y
135 P=1-Y*.6366197723657157#:GOTO 150
140 Y=1:FOR Z=2 TO (V1-2) STEP 2:X=X*R2*(Z-1)/Z:Y=Y+X:NEXT
145 P=1-Y*RS
150 PLAY "MB O3 T240 L8 G L16 F#GF#GF#GF#GF# L12 G L10 F#EF# L2 G"
155 PRINT TAB(60);"p = ";: IF P<.000001 THEN PRINT "< 10 (-6)" ELSE PRINT P
160 PRINT:COLOR CLR2,CLR1:PRINT TAB(10);"The slope of this line is ";
165 IF P>.05 THEN PRINT "NOT ";
170 PRINT "significantly different than 0";TAB(79):COLOR CLR1,CLR2:PRINT
175 PRINT:PRINT " Confidence limits on the slope can be calculated as:":PRINT
180 DF=STR$(V1):DF=RIGHT$(DF,LEN(DF)-1):PRINT TAB(20);SB;"+/- T(";DF;") *";SEB
185 LOCATE 24,5:INPUT;"Do you want another regression calculation with this DATAFILE?  ",A$
190 IF A$="y" OR A$="Y" THEN CLS:GOTO 60 ELSE 375
195 CLS:PRINT TAB(28);"DATA TRANSFORMATIONS":PRINT TAB(28);STRING$(20,205):PRINT:LOCATE 4,1:GOSUB 4000
200 PRINT:PRINT TAB(25);"1.)  X' = Ax + B":PRINT TAB(25);"2.)  X' = Ax? + B"
205 PRINT TAB(25);"3.)  X' = A*?x + B":PRINT TAB(25);"4.)  X' = A/x + B"
210 PRINT TAB(25);"5.)  X' = x - mean":PRINT TAB(25);"6.)  X' = A*ln(x) + B"
215 PRINT TAB(25);"7.)  X' = ln(x/100-x)":PRINT TAB(25);"8.)  X' = Sample A + Sample B"
220 PRINT TAB(25);"9.)  X' = Sample A * Sample B":PRINT
225 PRINT TAB(27);:INPUT "Choose transformation:  ",TN:IF ABS(TN-5)>4 THEN BEEP:GOTO 225
230 IF TN>7 THEN 240 ELSE AR=CSRLIN:PRINT "    Enter the SAMPLE NUMBER of the variable you want transformed:":AC=68:GOSUB 4200:N=T(NS)
235 IF TN=5 OR TN=7 THEN 245 ELSE PRINT TAB(23);:INPUT;"A = ",KA:PRINT TAB(45);:INPUT "B = ",KB:GOTO 245
240 PRINT TAB(23);"A = ";:AR=CSRLIN:AC=27:GOSUB 4200:KA=NS:LOCATE AR,45:PRINT "B = ";:AC=49:GOSUB 4200:KB=NS:IF T(KB)>T(KA) THEN N=T(KB) ELSE N=T(KA)
245 A=AN:X(A)=0:X2(A)=0:PRINT:AR=CSRLIN:COLOR 23:LOCATE AR,26:PRINT "PERFORMING TRANSFORMATION";:COLOR CLR1,CLR2
250 ON TN GOTO 255,260,265,270,275,280,285,290,295
255 FOR Z=1 TO N:ST(Z)=KA*VAL(D(NS,Z))+KB:NEXT:GOTO 300
260 FOR Z=1 TO N:L=VAL(D(NS,Z)):ST(Z)=KA*L*L+KB:NEXT:GOTO 300
265 FOR Z=1 TO N:ST(Z)=KA*SQR(VAL(D(NS,Z)))+KB:NEXT:GOTO 300
270 FOR Z=1 TO N:ST(Z)=KA/VAL(D(NS,Z))+KB:NEXT:GOTO 300
275 LM=X(NS)/T(NS):FOR Z=1 TO N:ST(Z)=VAL(D(NS,Z))-LM:NEXT:GOTO 300
280 FOR Z=1 TO N:ST(Z)=KA*LOG(VAL(D(NS,Z)))+KB:NEXT:GOTO 300
285 FOR Z=1 TO N:L=VAL(D(NS,Z)):ST(Z)=LOG(L/(100-L)):NEXT:GOTO 300
290 FOR Z=1 TO N:ST(Z)=VAL(D(KA,Z))+VAL(D(KB,Z)):GOSUB 350:NEXT Z:GOTO 305
295 FOR Z=1 TO N:ST(Z)=VAL(D(KA,Z))*VAL(D(KB,Z)):GOSUB 350:NEXT Z:GOTO 305
300 FOR Z=1 TO N:XX=ST(Z):X(A)=X(A)+XX:X2(A)=X2(A)+XX*XX:NEXT
305 FOR Z=1 TO N:SP=INT(ST(Z)*1E+07)*.0000001:DS=STR$(SP)
310 IF SP>0 THEN D(A,Z)=RIGHT$(DS,LEN(DS)-1) ELSE D(A,Z)=DS
315 NEXT:IF TN=4 THEN 325 ELSE IF TN>7 THEN 330
320 FOR Z=1 TO N:CS(A,Z)=CS(NS,Z):NEXT:GOTO 330
325 FOR Z=1 TO N:CS(A,Z)=CS(NS,(N-Z+1)):NEXT
330 T(A)=N:IF N>1 THEN IF X2(A)>X(A)*X(A)/N THEN SD(A)=SQR((X2(A)-X(A)*X(A)/N)/(N-1)) ELSE SD(A)=0
335 IF N>0 THEN IF N MOD 2=0 THEN MD(A)=(VAL(D(A,CS(A,N/2)))+VAL(D(A,CS(A,N/2+1))))*.5 ELSE MD(A)=VAL(D(A,CS(A,N/2+.5)))
340 LOCATE AR,5:PRINT "The transformed variable has been added to ";FILE$;" as sample #";A
345 PRINT:PRINT TAB(7);"Enter name for the TRANSFORMED `";N$(NS);:INPUT "' data sample:  ",N$(A):AR=CSRLIN:GOSUB 4100:GOTO 20
350 X(A)=X(A)+ST(Z):X2(A)=X2(A)+ST(Z)*ST(Z)
355 FOR ZZ=1 TO (Z-1):VX=ST(CS(A,ZZ)):IF VX<=ST(Z) THEN 365
360 FOR TZ=Z TO (ZZ+1) STEP -1:CS(A,TZ)=CS(A,TZ-1):NEXT:GOTO 370
365 NEXT ZZ
370 CS(A,ZZ)=Z:RETURN
375 LOCATE 25,20:PRINT "Press any key to return to menu.";
380 A$=INKEY$:IF A$="" THEN 380 ELSE 20
385 LOCATE 23,1:END
4025 IF ASUB=1 THEN AN=A ELSE AN=A+1
4027 ERASE D,CS,T,N$,X,X2,SD,MD,ST
4030 DIM D(AN,C),CS(AN,C),T(AN),N$(AN),X(AN),X2(AN),MD(AN),SD(AN),ST(C)
5000 BEEP:IF ERR<>53 AND ERR<>71 THEN 5010 ELSE LOCATE 10,10:PRINT "Please place EPISTAT in drive A: (or other default).":PRINT TAB(25);"Press any key to continue:"
5005 A$=INKEY$:IF A$="" THEN 5005 ELSE RESUME
5010 ON ERROR GOTO 0:END